12 #ifndef CD_ItoPlasmaPhysicsImplem_H
13 #define CD_ItoPlasmaPhysicsImplem_H
16 #include <ParmParse.H>
23 #include <CD_NamespaceHeader.H>
25 using namespace Physics::ItoPlasma;
29 CH_TIME(
"ItoPlasmaPhysics::ItoPlasmaPhysics");
49 bool useCentroid =
false;
50 ParmParse pp(
"ItoPlasmaPhysics");
51 pp.query(
"use_centroid", useCentroid);
63 CH_TIME(
"ItoPlasmaPhysics::computeDt");
71 CH_TIME(
"ItoPlasmaPhysics::defineKMC");
84 CH_TIME(
"ItoPlasmaPhysics::parseRuntimeOptions");
97 CH_TIME(
"ItoPlasmaPhysics::parsePPC");
108 CH_TIME(
"ItoPlasmaPhysics::parseDebug");
118 CH_TIME(
"ItoPlasmaPhysics::parseAlgorithm");
124 pp.get(
"algorithm", str);
127 pp.get(
"prop_eps",
m_eps);
130 if (str ==
"hybrid") {
133 else if (str ==
"tau") {
136 else if (str ==
"ssa") {
140 MayDay::Error(
"ItoPlasmaPhysics::parseAlgorithm - unknown algorithm requested");
144 inline const Vector<RefCountedPtr<ItoSpecies>>&
150 inline const Vector<RefCountedPtr<RtSpecies>>&
176 Vector<FPR>& a_numNewPhotons,
180 const Real a_kappa)
const
182 CH_TIME(
"ItoPlasmaPhysics::advanceKMC");
188 for (
size_t i = 0; i < a_numParticles.size(); i++) {
189 kmcParticles[i] = a_numParticles[i];
192 for (
auto& p : kmcPhotons) {
211 for (
size_t i = 0; i < a_numParticles.size(); i++) {
212 a_numParticles[i] = (
FPR)kmcParticles[i];
214 for (
size_t i = 0; i < a_numNewPhotons.size(); i++) {
215 a_numNewPhotons[i] = (
FPR)kmcPhotons[i];
221 const Vector<FPR>& a_newNumParticles,
222 const Vector<FPR>& a_oldNumParticles,
223 const RealVect a_cellPos,
224 const RealVect a_centroidPos,
227 const RealVect a_bndryCentroid,
228 const RealVect a_bndryNormal,
230 const Real a_kappa)
const noexcept
232 CH_TIME(
"ItoPlasmaPhysics::reconcileParticles(Vector<List<ItoParticle>*>, ...)");
234 CH_assert(a_particles.size() == a_newNumParticles.size());
235 CH_assert(a_oldNumParticles.size() == a_newNumParticles.size());
238 for (
int i = 0; i < a_particles.size(); i++) {
239 const FPR& numNew = a_newNumParticles[i];
240 const FPR& numOld = a_oldNumParticles[i];
242 if (numNew < (
FPR)0) {
243 MayDay::Warning(
"ItoPlasmaPhysics::reconcileParticles - new number of particles is < 0 (overflow issue?)");
245 else if ((
long long)numNew < 0LL) {
246 MayDay::Warning(
"ItoPlasmaPhysics::reconcileParticles - integer overflow!");
250 MayDay::Warning(
"ItoPlasmaPhysics::reconcileParticles - old number of particles is < 0");
252 else if ((
long long)numOld < 0LL) {
253 MayDay::Warning(
"ItoPlasmaPhysics::reconcileParticles - integer overflow for old particles!");
258 for (
int i = 0; i < a_particles.size(); i++) {
259 const long long diff = (
long long)(a_newNumParticles[i] - a_oldNumParticles[i]);
263 const std::vector<long long> particleWeights =
264 ParticleManagement::partitionParticleWeights(diff, (
long long)m_maxNewParticles);
266 for (
const auto& w : particleWeights) {
267 RealVect x = RealVect::Zero;
270 switch (m_particlePlacement) {
271 case ParticlePlacement::Random: {
276 case ParticlePlacement::Centroid: {
277 x = a_cellPos + a_centroidPos * a_dx;
282 MayDay::Error(
"ItoPlasmaPhysics::reconcileParticles - logic bust");
291 else if (diff < 0LL) {
293 this->removeParticles(*a_particles[i], -diff);
301 CH_TIME(
"ItoPlasmaPhysics::removeParticles");
303 constexpr
long long zero = 0LL;
305 CH_assert(a_numParticlesToRemove >= zero);
308 auto getTotalWeight = [&]() ->
long long {
311 for (ListIterator<ItoParticle> lit(a_particles); lit.ok(); ++lit) {
312 W += (
long long)lit().weight();
314 if (lit().weight() < 1.0) {
315 MayDay::Error(
"ItoPlasmaPhysics::removeParticles -- bad particle mass!");
322 if (a_numParticlesToRemove > zero) {
325 long long totalWeightBefore = 0;
326 long long totalWeightAfter = 0;
330 totalWeightBefore = getTotalWeight();
332 if (totalWeightBefore < a_numParticlesToRemove) {
333 MayDay::Error(
"ItoPlasmaPhysics::removeParticles: logic bust (trying to remove too many particles)");
338 ParticleManagement::removePhysicalParticles(a_particles, a_numParticlesToRemove);
346 totalWeightAfter = getTotalWeight();
348 const long long errDiff = std::abs(totalWeightBefore - totalWeightAfter) - a_numParticlesToRemove;
349 if (std::abs(errDiff) != zero) {
351 pout() <<
"ItoPlasmaPhysics::removeParticles: Total weight before = " << totalWeightBefore << endl;
352 pout() <<
"ItoPlasmaPhysics::removeParticles: Total weight after = " << totalWeightAfter << endl;
353 pout() <<
"ItoPlasmaPhysics::removeParticles: Should have removed = " << a_numParticlesToRemove << endl;
354 pout() <<
"ItoPlasmaPhysics::removeParticles: Error = " << errDiff << endl;
356 MayDay::Abort(
"ItoPlasmaPhysics::removeParticles - incorrect mass removed");
364 const Vector<FPR>& a_numNewPhotons,
365 const RealVect a_cellPos,
366 const RealVect a_centroidPos,
369 const RealVect a_bndryCentroid,
370 const RealVect a_bndryNormal,
372 const Real a_kappa)
const noexcept
374 CH_TIME(
"ItoPlasmaPhysics::reconcilePhotons");
376 for (
int i = 0; i < a_newPhotons.size(); i++) {
377 if (a_numNewPhotons[i] > 0LL) {
379 const std::vector<long long> photonWeights =
380 ParticleManagement::partitionParticleWeights((
long long)a_numNewPhotons[i], (
long long)m_maxNewPhotons);
382 for (
const auto& w : photonWeights) {
383 const RealVect x =
Random::randomPosition(a_cellPos, a_lo, a_hi, a_bndryCentroid, a_bndryNormal, a_dx, a_kappa);
386 a_newPhotons[i]->add(
Photon(x, v, m_rtSpecies[i]->getAbsorptionCoefficient(x), 1.0 * w));
394 const Vector<List<Photon>*>& a_absorbedPhotons)
const noexcept
396 CH_TIME(
"ItoPlasmaPhysics::reconcilePhotoionization");
398 for (
const auto& r : m_photoReactions) {
401 const size_t& src = r->getSourcePhoton();
402 const std::list<size_t>& targets = r->getTargetSpecies();
404 for (ListIterator<Photon> lit(*a_absorbedPhotons[src]); lit.ok(); ++lit) {
405 const RealVect x = lit().position();
406 const Real w = lit().weight();
408 for (
const auto& t : targets) {
415 #include <CD_NamespaceFooter.H>
Main file for describing Ito-based plasma physics.
Real FPR
Numerical representation of the KMC state. Can be floating-point or integer type.
Definition: CD_ItoPlasmaPhysics.H:41
Namespace containing various particle management utilities.
File containing some useful static methods related to random number generation.
Declaration of various useful units.
A particle class for use with ItoSolvers, i.e. drifting Brownian walkers.
Definition: CD_ItoParticle.H:40
State & getNonReactiveState() noexcept
Get modifiable non-reactive state.
Definition: CD_KMCDualStateImplem.H:80
void define(const size_t a_numReactiveSpecies, const size_t a_numNonReactiveSpecies) noexcept
Define function constructor.
Definition: CD_KMCDualStateImplem.H:33
State & getReactiveState() noexcept
Get modifiable reactive state.
Definition: CD_KMCDualStateImplem.H:66
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
void advanceTau(State &a_state, const Real a_dt) const noexcept
Advance with tau-leaping step over the input time. This can end up using substepping.
Definition: CD_KMCSolverImplem.H:358
void advanceHybrid(State &a_state, const Real a_dt) const noexcept
Advance using Cao et. al. hybrid algorithm over the input time. This can end up using substepping.
Definition: CD_KMCSolverImplem.H:512
void define(const ReactionList &a_reactions) noexcept
Define function. Sets the reactions.
Definition: CD_KMCSolverImplem.H:47
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:473
Particle class for usage with Monte Carlo radiative transfer.
Definition: CD_Photon.H:29
const Vector< RefCountedPtr< ItoSpecies > > & getItoSpecies() const
Get all particle species.
Definition: CD_ItoPlasmaPhysicsImplem.H:145
void defineKMC() noexcept
Define the KMC solver and state.
Definition: CD_ItoPlasmaPhysicsImplem.H:69
virtual Real computeDt(const RealVect a_E, const RealVect a_pos, const Vector< FPR > a_numParticles) const noexcept
Compute a time step to use.
Definition: CD_ItoPlasmaPhysicsImplem.H:61
KMCSolverType m_kmcSolver
Kinetic Monte Carlo solver.
Definition: CD_ItoPlasmaPhysics.H:324
std::string m_className
Class name. Used for options parsing.
Definition: CD_ItoPlasmaPhysics.H:309
void parseDebug() noexcept
Parse the maximum number of particles generated per cell.
Definition: CD_ItoPlasmaPhysicsImplem.H:106
Real m_eps
Solver setting for the Cao et. al. algorithm.
Definition: CD_ItoPlasmaPhysics.H:378
bool m_debug
Turn on/off debugging.
Definition: CD_ItoPlasmaPhysics.H:314
void removeParticles(List< ItoParticle > &a_particles, const long long a_numToRemove) const
Remove particles from the input list.
Definition: CD_ItoPlasmaPhysicsImplem.H:299
std::vector< std::shared_ptr< const KMCReaction > > m_kmcReactions
List of reactions for the KMC solver.
Definition: CD_ItoPlasmaPhysics.H:329
KMCState m_kmcState
Kinetic Monte Carlo state.
Definition: CD_ItoPlasmaPhysics.H:319
int getNumPlasmaSpecies() const
Return number of ion equations.
Definition: CD_ItoPlasmaPhysicsImplem.H:157
virtual ~ItoPlasmaPhysics()
Destructor. Does nothing.
Definition: CD_ItoPlasmaPhysicsImplem.H:58
ParticlePlacement m_particlePlacement
Particle placement algorithm.
Definition: CD_ItoPlasmaPhysics.H:304
const Vector< RefCountedPtr< RtSpecies > > & getRtSpecies() const
Get all photon species.
Definition: CD_ItoPlasmaPhysicsImplem.H:151
void reconcileParticles(Vector< List< ItoParticle > * > &a_particles, const Vector< FPR > &a_newNumParticles, const Vector< FPR > &a_oldNumParticles, const RealVect a_cellPos, const RealVect a_centroidPos, const RealVect a_lo, const RealVect a_hi, const RealVect a_bndryCentroid, const RealVect a_bndryNormal, const Real a_dx, const Real a_kappa) const noexcept
Reconcile the number of particles.
Definition: CD_ItoPlasmaPhysicsImplem.H:220
virtual void updateReactionRates(const RealVect a_E, const Real a_dx, const Real a_kappa) const noexcept=0
Update reaction rates.
void parsePPC() noexcept
Parse the maximum number of particles generated per cell.
Definition: CD_ItoPlasmaPhysicsImplem.H:95
int m_maxNewParticles
Maximum new number of particles generated by the chemistry advance.
Definition: CD_ItoPlasmaPhysics.H:349
Vector< RefCountedPtr< ItoSpecies > > m_plasmaSpecies
List of solver-tracked particle species.
Definition: CD_ItoPlasmaPhysics.H:339
Algorithm m_algorithm
Algorithm to use for KMC advance.
Definition: CD_ItoPlasmaPhysics.H:299
void parseAlgorithm() noexcept
Parse reaction algorithm.
Definition: CD_ItoPlasmaPhysicsImplem.H:116
virtual void parseRuntimeOptions() noexcept
Parse run-time options.
Definition: CD_ItoPlasmaPhysicsImplem.H:82
ItoPlasmaPhysics()
Constructor. Does nothing.
Definition: CD_ItoPlasmaPhysicsImplem.H:27
void reconcilePhotons(Vector< List< Photon > * > &a_newPhotons, const Vector< FPR > &a_numNewPhotons, const RealVect a_cellPos, const RealVect a_centroidPos, const RealVect a_lo, const RealVect a_hi, const RealVect a_bndryCentroid, const RealVect a_bndryNormal, const Real a_dx, const Real a_kappa) const noexcept
Generate new photons.
Definition: CD_ItoPlasmaPhysicsImplem.H:363
virtual Real initialSigma(const Real a_time, const RealVect a_pos) const
Set initial surface charge. Default is 0, override if you want.
Definition: CD_ItoPlasmaPhysicsImplem.H:169
void advanceKMC(Vector< FPR > &a_numParticles, Vector< FPR > &a_numNewPhotons, const Real a_dt, const RealVect a_E, const Real a_dx, const Real a_kappa) const
Advance particles.
Definition: CD_ItoPlasmaPhysicsImplem.H:175
Vector< RefCountedPtr< RtSpecies > > m_rtSpecies
List of solver-tracked photon species.
Definition: CD_ItoPlasmaPhysics.H:344
int getNumPhotonSpecies() const
Return number of RTE equations.
Definition: CD_ItoPlasmaPhysicsImplem.H:163
int m_Ncrit
Solver setting for the Cao et. al algorithm.
Definition: CD_ItoPlasmaPhysics.H:360
void reconcilePhotoionization(Vector< List< ItoParticle > * > &a_particles, const Vector< List< Photon > * > &a_absorbedPhotons) const noexcept
Reconcile photoionization reactions.
Definition: CD_ItoPlasmaPhysicsImplem.H:393
int m_NSSA
Solver setting for the Cao et. al algorithm.
Definition: CD_ItoPlasmaPhysics.H:366
int m_maxNewPhotons
Maximum new number of photons generated by the chemistry advance.
Definition: CD_ItoPlasmaPhysics.H:354
std::vector< std::shared_ptr< const ItoPlasmaPhotoReaction > > m_photoReactions
List of photoionization reactions.
Definition: CD_ItoPlasmaPhysics.H:334
Real m_SSAlim
Solver setting for the Cao et. al. algorithm.
Definition: CD_ItoPlasmaPhysics.H:372
static RealVect getDirection()
Get a random direction in space.
Definition: CD_RandomImplem.H:157
static RealVect randomPosition(const RealVect a_lo, const RealVect a_hi) noexcept
Return a random position in the cube (a_lo, a_hi);.
Definition: CD_RandomImplem.H:247
Real max(const Real &a_input) noexcept
Get the maximum of the input, reduced over MPI ranks (in the Chombo communicator)
Definition: CD_ParallelOpsImplem.H:142
Real min(const Real &a_input) noexcept
Get the minimum of the input, reduced over MPI ranks (in the Chombo communicator)
Definition: CD_ParallelOpsImplem.H:54
constexpr Real c
Speed of light.
Definition: CD_Units.H:36