chombo-discharge
Loading...
Searching...
No Matches
CD_KMCSingleStateReactionImplem.H
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021-2026 SINTEF Energy Research
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
13#ifndef CD_KMCSINGLESTATEREACTIONIMPLEM_H
14#define CD_KMCSINGLESTATEREACTIONIMPLEM_H
15
16// Chombo includes
17#include <CH_Timer.H>
18
19// Our includes
21#include <CD_NamespaceHeader.H>
22
23template <typename State, typename T>
26 : m_reactants(a_reactants), m_products(a_products)
27{
28 CH_TIME("KMCSingleStateReaction::KMCSingleStateReaction");
29
30 this->computeStateChanges();
31}
32
33template <typename State, typename T>
35{
36 CH_TIME("KMCSingleStateReaction::~KMCSingleStateReaction");
37}
38
39template <typename State, typename T>
40inline void
42{
43 CH_TIME("KMCSingleStateReaction::computeStateChanges");
44
45 // Consumed particles.
46 for (const auto& r : m_reactants) {
47 if (m_stateChange.find(r) == m_stateChange.end()) {
48 m_stateChange.emplace(r, -1);
49 }
50 else {
51 m_stateChange[r]--;
52 }
53 }
54
55 // Produced particles.
56 for (const auto& p : m_products) {
57 if (m_stateChange.find(p) == m_stateChange.end()) {
58 m_stateChange.emplace(p, +1);
59 }
60 else {
61 m_stateChange[p]++;
62 }
63 }
64
65 // Compute the propensity factor that we need when there are bi- or tri-particle reactions involving the same species. We need to do this
66 // 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
67 // 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
68 //
69 // a = rate * factor * N * (N-1) * (N-2) ...
70 //
71 // For this to make sense we have
72 //
73 // a = rate * N * (N-1) * (N-2) ... (N-k+1) * (N-k)!/(k! * (N-k)!)
74 // = rate * N * (N-1) * (N-2) ... * 1/k!
75 //
76 // so the factor is just 1/k! (for each species).
77
78 m_propensityFactor = 1.0;
79
81 for (const auto& r : m_reactants) {
82 if (reactantNumbers.find(r) == reactantNumbers.end()) {
83 reactantNumbers.emplace(r, 1);
84 }
85 else {
87 }
88 }
89
90 for (const auto& rn : reactantNumbers) {
91 m_propensityFactor *= 1.0 / factorial(rn.second);
92 }
93}
94
95template <typename State, typename T>
96inline Real&
101
102template <typename State, typename T>
103inline T
105{
106 return a_state[a_reactant];
107}
108
109template <typename State, typename T>
110inline Real
112{
113 CH_TIME("KMCSingleStateReaction::propensity");
114
115 Real A = m_rate * m_propensityFactor;
116
117 State tmp = a_state;
118
119 for (const auto& r : m_reactants) {
120 A *= tmp[r];
121
122 tmp[r]--;
123 }
124
125 return A;
126}
127
128template <typename State, typename T>
129inline T
131{
132 CH_TIME("KMCSingleStateReaction::computeCriticalNumberOfReactions");
133
134 T Lj = std::numeric_limits<T>::max();
135
136 for (const auto& s : m_stateChange) {
137
138 const size_t rI = s.first;
139 const T nuIJ = s.second;
140
141 if (nuIJ < 0) {
142 Lj = std::min(Lj, a_state[rI] / std::abs(nuIJ));
143 }
144 }
145
146 return Lj;
147}
148
149template <typename State, typename T>
155
156template <typename State, typename T>
157inline T
159{
160 CH_TIME("KMCSingleStateReaction::getStateChange");
161
162 T nuIJ = 0;
163
164 if (m_stateChange.find(a_particleReactant) != m_stateChange.end()) {
165 nuIJ = m_stateChange.at(a_particleReactant);
166 }
167
168 return nuIJ;
169}
170
171template <typename State, typename T>
172inline void
174{
175 CH_TIME("KMCSingleStateReaction::advanceState");
176
177 for (const auto& s : m_stateChange) {
178 a_state[s.first] += a_numReactions * s.second;
179 }
180}
181
182#include <CD_NamespaceFooter.H>
183
184#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:151
virtual ~KMCSingleStateReaction()
Destructor.
Definition CD_KMCSingleStateReactionImplem.H:34
Real propensity(const State &a_state) const noexcept
Compute the propensity function for this reaction type.
Definition CD_KMCSingleStateReactionImplem.H:111
T getStateChange(const size_t a_particleReactant) const noexcept
Get the state change due to a change in the input reactant species.
Definition CD_KMCSingleStateReactionImplem.H:158
void computeStateChanges() noexcept
Compute state change vectors from the reactant/product lists.
Definition CD_KMCSingleStateReactionImplem.H:41
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:104
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:130
void advanceState(State &a_state, const T &a_numReactions) const noexcept
Advance the incoming state with the number of reactions.
Definition CD_KMCSingleStateReactionImplem.H:173
KMCSingleStateReaction()=default
Default constructor.
Real & rate() const noexcept
Get modifiable reaction rate.
Definition CD_KMCSingleStateReactionImplem.H:97
Base class for a tracer particle solver. This solver can advance particles in a pre-defined velocity ...
Definition CD_TracerParticleSolver.H:38
TracerParticleSolver()
Default constructor.
Definition CD_TracerParticleSolverImplem.H:26