chombo-discharge
CD_KMCSolver.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_KMCSolver_H
13 #define CD_KMCSolver_H
14 
15 // Std includes
16 #include <vector>
17 #include <memory>
18 
19 // Chombo includes
20 #include <REAL.H>
21 
22 // Our includes
23 #include <CD_NamespaceHeader.H>
24 
29 {
30  TauPlain,
31  TauMidpoint
32 };
33 
47 template <typename R, typename State, typename T = long long>
48 class KMCSolver
49 {
50 public:
51  using ReactionList = std::vector<std::shared_ptr<const R>>;
52 
56  KMCSolver() noexcept;
57 
61  KMCSolver(const KMCSolver&) = default;
62 
66  KMCSolver(const KMCSolver&&) = delete;
67 
72  inline KMCSolver(const ReactionList& a_reactions) noexcept;
73 
77  virtual ~KMCSolver() noexcept;
78 
82  KMCSolver&
83  operator=(const KMCSolver&) = default;
84 
88  KMCSolver&
89  operator=(const KMCSolver&&) = delete;
90 
95  inline void
96  define(const ReactionList& a_reactions) noexcept;
97 
105  inline void
106  setSolverParameters(const T a_numCrit, const T a_numSSA, const Real a_eps, const Real a_SSAlim) noexcept;
107 
113  inline std::vector<Real>
114  propensities(const State& a_state) const noexcept;
115 
121  inline std::vector<Real>
122  propensities(const State& a_state, const ReactionList& a_reactions) const noexcept;
123 
129  inline Real
130  totalPropensity(const State& a_state) const noexcept;
131 
137  inline Real
138  totalPropensity(const State& a_state, const ReactionList& a_reactions) const noexcept;
139 
146  inline std::pair<ReactionList, ReactionList>
147  partitionReactions(const State& a_state) const noexcept;
148 
155  inline std::pair<ReactionList, ReactionList>
156  partitionReactions(const State& a_state, const ReactionList& a_reactions) const noexcept;
157 
163  inline Real
164  getCriticalTimeStep(const State& a_state) const noexcept;
165 
172  inline Real
173  getCriticalTimeStep(const State& a_state, const ReactionList& a_criticalReactions) const noexcept;
174 
179  inline Real
180  getCriticalTimeStep(const std::vector<Real>& a_propensities) const noexcept;
181 
186  inline Real
187  getCriticalTimeStep(const Real& a_totalPropensity) const noexcept;
188 
194  inline Real
195  getNonCriticalTimeStep(const State& a_state) const noexcept;
196 
203  inline Real
204  getNonCriticalTimeStep(const State& a_state, const ReactionList& a_reactions) const noexcept;
205 
212  inline Real
213  getNonCriticalTimeStep(const State& a_state,
214  const ReactionList& a_nonCriticalReactions,
215  const std::vector<Real>& a_nonCriticalPropensities) const noexcept;
216 
223  inline void
224  stepTauPlain(State& a_state, const Real a_dt) const noexcept;
225 
233  inline void
234  stepTauPlain(State& a_state, const ReactionList& a_reactions, const Real a_dt) const noexcept;
235 
243  inline void
244  stepTauPlain(State& a_state,
245  const ReactionList& a_reactions,
246  const std::vector<Real>& a_propensities,
247  const Real a_dt) const noexcept;
248 
255  inline void
256  advanceTauPlain(State& a_state, const Real a_dt) const noexcept;
257 
264  inline void
265  advanceTauPlain(State& a_state, const ReactionList& a_reactions, const Real a_dt) const noexcept;
266 
273  inline void
274  stepTauMidpoint(State& a_state, const Real a_dt) const noexcept;
275 
282  inline void
283  stepTauMidpoint(State& a_state, const ReactionList& a_reactions, const Real a_dt) const noexcept;
284 
291  inline void
292  advanceTauMidpoint(State& a_state, const Real a_dt) const noexcept;
293 
300  inline void
301  advanceTauMidpoint(State& a_state, const ReactionList& a_reactions, const Real a_dt) const noexcept;
302 
308  inline void
309  stepSSA(State& a_state) const noexcept;
310 
317  inline void
318  stepSSA(State& a_state, const ReactionList& a_reactions) const noexcept;
319 
326  inline void
327  stepSSA(State& a_state, const ReactionList& a_reactions, const std::vector<Real>& a_propensities) const noexcept;
328 
335  inline void
336  advanceSSA(State& a_state, const Real a_dt) const noexcept;
337 
344  inline void
345  advanceSSA(State& a_state, const ReactionList& a_reactions, const Real a_dt) const noexcept;
346 
354  inline void
355  advanceHybrid(State& a_state,
356  const Real a_dt,
357  const KMCLeapPropagator& a_leapPropagator = KMCLeapPropagator::TauPlain) const noexcept;
358 
367  inline void
368  advanceHybrid(State& a_state,
369  const ReactionList& a_reactions,
370  const Real a_dt,
371  const KMCLeapPropagator& a_leapPropagator = KMCLeapPropagator::TauPlain) const noexcept;
372 
380  inline void
382  State& a_state,
383  const ReactionList& a_reactions,
384  const Real a_dt,
385  const std::function<void(State&, const ReactionList& a_reactions, const Real a_dt)>& a_propagator) const noexcept;
386 
387 protected:
391  ReactionList m_reactions;
392 
398 
403 
407  Real m_eps;
408 
412  Real m_SSAlim;
413 };
414 
415 #include <CD_NamespaceFooter.H>
416 
417 #include <CD_KMCSolverImplem.H>
418 
419 #endif
Implementation of CD_KMCSolver.H.
KMCLeapPropagator
Supported propagators for hybrid tau leaping.
Definition: CD_KMCSolver.H:29
Class for running Kinetic Monte-Carlo simulations.
Definition: CD_KMCSolver.H:49
KMCSolver(const KMCSolver &)=default
Disallowed copy constructor.
Real m_eps
Maximum permitted change in propensities for non-critical reactions.
Definition: CD_KMCSolver.H:407
void setSolverParameters(const T a_numCrit, const T a_numSSA, const Real a_eps, const Real a_SSAlim) noexcept
Set solver parameters.
Definition: CD_KMCSolverImplem.H:59
virtual ~KMCSolver() noexcept
Destructor.
Definition: CD_KMCSolverImplem.H:40
void advanceTauMidpoint(State &a_state, const Real a_dt) const noexcept
Perform one leaping step using the midpoint method all reactions over a time step a_dt.
Definition: CD_KMCSolverImplem.H:445
void define(const ReactionList &a_reactions) noexcept
Define function. Sets the reactions.
Definition: CD_KMCSolverImplem.H:47
KMCSolver() noexcept
Default constructor – must subsequently define the object.
Definition: CD_KMCSolverImplem.H:28
KMCSolver(const KMCSolver &&)=delete
Disallowed move constructor.
ReactionList m_reactions
List of reactions used when advancing states.
Definition: CD_KMCSolver.H:391
Real getCriticalTimeStep(const State &a_state) const noexcept
Get the time to the next critical reaction.
Definition: CD_KMCSolverImplem.H:160
void stepTauMidpoint(State &a_state, const Real a_dt) const noexcept
Perform one leaping step using the midpoint method for ALL reactions over a time step a_dt.
Definition: CD_KMCSolverImplem.H:408
void stepTauPlain(State &a_state, const Real a_dt) const noexcept
Perform one plain tau-leaping step using ALL reactions.
Definition: CD_KMCSolverImplem.H:315
T m_numSSA
Maximum number of SSA steps to run when switching into SSA-based advancement for non-critical reactio...
Definition: CD_KMCSolver.H:402
T m_Ncrit
Definition of critical reactions.
Definition: CD_KMCSolver.H:397
void advanceSSA(State &a_state, const Real a_dt) const noexcept
Advance with the SSA over the input time. This can end up using substepping.
Definition: CD_KMCSolverImplem.H:562
std::pair< ReactionList, ReactionList > partitionReactions(const State &a_state) const noexcept
Partition reactions into critical and non-critical reactions.
Definition: CD_KMCSolverImplem.H:126
void advanceTauPlain(State &a_state, const Real a_dt) const noexcept
Advance with plain tau-leaping step over the input time. This can end up using substepping.
Definition: CD_KMCSolverImplem.H:358
void stepSSA(State &a_state) const noexcept
Perform a single SSA step.
Definition: CD_KMCSolverImplem.H:497
Real getNonCriticalTimeStep(const State &a_state) const noexcept
Get the non-critical time step.
Definition: CD_KMCSolverImplem.H:216
void advanceHybrid(State &a_state, const Real a_dt, const KMCLeapPropagator &a_leapPropagator=KMCLeapPropagator::TauPlain) const noexcept
Advance using Cao et. al. hybrid algorithm over the input time. This can end up using substepping.
Definition: CD_KMCSolverImplem.H:601
Real totalPropensity(const State &a_state) const noexcept
Compute the total propensity for ALL reactions.
Definition: CD_KMCSolverImplem.H:100
Real m_SSAlim
Threshold for switching to SSA-based algorithm within the Cao algorithm.
Definition: CD_KMCSolver.H:412
std::vector< Real > propensities(const State &a_state) const noexcept
Compute propensities for ALL reactions.
Definition: CD_KMCSolverImplem.H:74