chombo-discharge
Loading...
Searching...
No Matches
CD_KMCSingleStateReactionImplem.H
Go to the documentation of this file.
1/* chombo-discharge
2 * Copyright © 2022 SINTEF Energy Research.
3 * Please refer to Copyright.txt and LICENSE in the chombo-discharge root directory.
4 */
5
12#ifndef CD_KMCSingleStateReactionImplem_H
13#define CD_KMCSingleStateReactionImplem_H
14
15// Chombo includes
16#include <CH_Timer.H>
17
18// Our includes
20#include <CD_NamespaceHeader.H>
21
22template <typename State, typename T>
24 const std::list<size_t>& a_products) noexcept
25{
26 CH_TIME("KMCSingleStateReaction::KMCSingleStateReaction");
27
28 m_reactants = a_reactants;
29 m_products = a_products;
30
31 this->computeStateChanges();
32}
33
34template <typename State, typename T>
36{
37 CH_TIME("KMCSingleStateReaction::~KMCSingleStateReaction");
38}
39
40template <typename State, typename T>
41inline void
43{
44 CH_TIME("KMCSingleStateReaction::computeStateChanges");
45
46 // Consumed particles.
47 for (const auto& r : m_reactants) {
48 if (m_stateChange.find(r) == m_stateChange.end()) {
49 m_stateChange.emplace(r, -1);
50 }
51 else {
52 m_stateChange[r]--;
53 }
54 }
55
56 // Produced particles.
57 for (const auto& p : m_products) {
58 if (m_stateChange.find(p) == m_stateChange.end()) {
59 m_stateChange.emplace(p, +1);
60 }
61 else {
62 m_stateChange[p]++;
63 }
64 }
65
66 // Compute the propensity factor that we need when there are bi- or tri-particle reactions involving the same species. We need to do this
67 // because for biparticle reactions of N particles of the same type, there are 0.5 * N * (N-1) unique pairs of particles. Or in general when
68 // we have a k-order reaction we have 'N choose k' = N!/(k! * (N-k)!) combinations. In the propensity function we compute the propensity as
69 //
70 // a = rate * factor * N * (N-1) * (N-2) ...
71 //
72 // For this to make sense we have
73 //
74 // a = rate * N * (N-1) * (N-2) ... (N-k+1) * (N-k)!/(k! * (N-k)!)
75 // = rate * N * (N-1) * (N-2) ... * 1/k!
76 //
77 // so the factor is just 1/k! (for each species).
78
79 m_propensityFactor = 1.0;
80
82 for (const auto& r : m_reactants) {
83 if (reactantNumbers.find(r) == reactantNumbers.end()) {
84 reactantNumbers.emplace(r, 1);
85 }
86 else {
88 }
89 }
90
91 for (const auto& rn : reactantNumbers) {
92 m_propensityFactor *= 1.0 / factorial(rn.second);
93 }
94}
95
96template <typename State, typename T>
97inline Real&
102
103template <typename State, typename T>
104inline T
106{
107 return a_state[a_reactant];
108}
109
110template <typename State, typename T>
111inline Real
113{
114 CH_TIME("KMCSingleStateReaction::propensity");
115
116 Real A = m_rate * m_propensityFactor;
117
118 State tmp = a_state;
119
120 for (const auto& r : m_reactants) {
121 A *= tmp[r];
122
123 tmp[r]--;
124 }
125
126 return A;
127}
128
129template <typename State, typename T>
130inline T
132{
133 CH_TIME("KMCSingleStateReaction::computeCriticalNumberOfReactions");
134
135 T Lj = std::numeric_limits<T>::max();
136
137 for (const auto& s : m_stateChange) {
138
139 const size_t rI = s.first;
140 const T nuIJ = s.second;
141
142 if (nuIJ < 0) {
143 Lj = std::min(Lj, a_state[rI] / std::abs(nuIJ));
144 }
145 }
146
147 return Lj;
148}
149
150template <typename State, typename T>
156
157template <typename State, typename T>
158inline T
160{
161 CH_TIME("KMCSingleStateReaction::getStateChange");
162
163 T nuIJ = 0;
164
165 if (m_stateChange.find(a_particleReactant) != m_stateChange.end()) {
166 nuIJ = m_stateChange.at(a_particleReactant);
167 }
168
169 return nuIJ;
170}
171
172template <typename State, typename T>
173inline void
175{
176 CH_TIME("KMCSingleStateReaction::advanceState");
177
178 for (const auto& s : m_stateChange) {
179 a_state[s.first] += a_numReactions * s.second;
180 }
181}
182
183#include <CD_NamespaceFooter.H>
184
185#endif
Declaration of a simple reaction type for advancing "single states" in Kinetic Monte Carlo codes.
std::list< size_t > getReactants() const noexcept
Get the reactants involved in the reaction.
Definition CD_KMCSingleStateReactionImplem.H:152
virtual ~KMCSingleStateReaction()
Destructor.
Definition CD_KMCSingleStateReactionImplem.H:35
Real propensity(const State &a_state) const noexcept
Compute the propensity function for this reaction type.
Definition CD_KMCSingleStateReactionImplem.H:112
T getStateChange(const size_t a_reactant) const noexcept
Get the state change due to a change in the input reactant species.
Definition CD_KMCSingleStateReactionImplem.H:159
void computeStateChanges() noexcept
Compute state change.
Definition CD_KMCSingleStateReactionImplem.H:42
static T population(const size_t &a_reactant, const State &a_state) noexcept
Get the population of the reactant in the input state.
Definition CD_KMCSingleStateReactionImplem.H:105
T computeCriticalNumberOfReactions(const State &a_state) const noexcept
Compute the number of times the reaction can fire before exhausting one of the reactants.
Definition CD_KMCSingleStateReactionImplem.H:131
void advanceState(State &a_state, const T &a_numReactions) const noexcept
Advance the incoming state with the number of reactions.
Definition CD_KMCSingleStateReactionImplem.H:174
KMCSingleStateReaction()=default
Disallowed constructor. Use the full constructor.
Real & rate() const noexcept
Get modifiable reaction rate.
Definition CD_KMCSingleStateReactionImplem.H:98
Base class for a tracer particle solver. This solver can advance particles in a pre-defined velocity ...
Definition CD_TracerParticleSolver.H:37
TracerParticleSolver()
Default constructor.
Definition CD_TracerParticleSolverImplem.H:25