9#ifndef _ADVANCEPACKET_H_
10#define _ADVANCEPACKET_H_
15#include "../Gas/Gas.H"
16#include "../Losses/Losses.H"
17#include "../Propagation/Propagation.H"
18#include "../Utils/RealTensor2.H"
19#include "../Utils/RKF.H"
20#include "../Utils/RngThread.H"
21#include "../Utils/Types.H"
22#include "../Utils/Vec3.H"
66 const criptic::Losses& loss,
80 std::vector<RealVec>& secondaryPositions,
81 std::vector<CRPacket>& secondaryPackets) {
85 Real t = std::max(packet.
tInj, tStart);
89 bool packetDone =
false;
100 prop(x, t, gd, packet, qty, qtyGrad);
105 Real dpdtCts, lossRate, wSec;
106 Losses::MechMatrix<Real> pSec;
107 loss.computeLoss(gd, packet, dpdtCts, lossRate, wSec, pSec);
136#ifndef TRACK_PITCH_ANGLE
144#ifdef TRACK_PITCH_ANGLE
147 Real muDrift = -2 * packet.mu * pd.kMu +
148 (1.0 - packet.mu * packet.mu) * pd.dkMudMu;
158 if (h != 0) dx = std::min(dx, h);
160 Real dtAdv = (xDriftMag != 0.0 && dx != 0) ?
161 cStep * dx / xDriftMag :
maxReal;
163 Real dtDiff = (kMag != 0.0 && dx != 0.0) ?
164 cStep * dx * dx / kMag :
maxReal;
165 Real dtPAdv = pDrift != 0.0 ?
166 cStep * packet.
p / std::abs(pDrift) :
maxReal;
169 Real dtLoss = lossRate != 0.0 ? cStep / lossRate :
maxReal;
170#ifdef TRACK_PITCH_ANGLE
171 Real dtMuAdv = fabs(muDrift) != 0.0 ?
172 0.02 / fabs(muDrift) :
maxReal;
173 Real dtMuDiff = pd.kMu != 0.0 ?
174 0.02 * cStep / pd.kMu :
maxReal;
185#ifdef TRACK_PITCH_ANGLE
189 std::cout <<
"AdvancePacket: warning: unable to compute time "
190 <<
"step size; note that in problems where the gas "
191 <<
"does not contain a characteristic length scale, "
192 <<
"the length scale dx must be set by hand in order "
193 <<
"to compute valid spatial drift or diffusion "
194 <<
"timescales; calculation will continue, but time "
195 <<
"step will not be self-consistently computed for "
196 <<
"this packet advance";
199 Real dtStep = 1.0 / (1.0/dtAdv + 1.0/dtDiff + 1.0/dtPAdv
200 + 1.0/dtPP + 1.0/dtLoss
201#ifdef TRACK_PITCH_ANGLE
202 + 1.0/dtMuAdv + 1.0/dtMuDiff
207 if (!(dtStep >= packetDtMin)) {
208 std::stringstream ss;
209 ss <<
"AdvancePacket: packet dt = " << dtStep
210 <<
" < min dt = " << packetDtMin
211 <<
"; individual time steps are: "
212 <<
"dtAdv = " << dtAdv
213 <<
", dtDiff = " << dtDiff
214 <<
", dtPAdv = " << dtPAdv
215 <<
", dtPP = " << dtPP
216 <<
", dtLoss = " << dtLoss
217#ifdef TRACK_PITCH_ANGLE
218 <<
", dtMuAdv = " << dtMuAdv
219 <<
", dtMuDiff = " << dtMuDiff
221 <<
"; packet x = " << x
222 <<
", p = " << packet.
p
223 <<
", type = " << packet.
type
224 <<
"; propagation parameters "
225#ifdef TRACK_PITCH_ANGLE
226 <<
"kMu = " << pd.kMu
227 <<
", dkMu_dMu = " << pd.dkMudMu <<
", "
229 <<
"vStr = " << pd.
vStr
233 <<
", kPar = " << pd.
kPar
234 <<
", kPerp = " << pd.
kPerp
235 <<
", kPP = " << pd.
kPP
238 <<
", dkPP_dp = " << pd.
dkPP_dp;
244 if (t + dtStep >= tStop) {
252 Real wFac = std::exp( -lossRate * dtStep);
254 packet.
p += pDrift * dtStep +
255 std::sqrt(2 * pd.
kPP * dtStep) * rng.
normal();
256#ifdef TRACK_PITCH_ANGLE
257 packet.mu += dtStep * muDrift +
258 std::sqrt(2 * pd.kMu * (1.0 - packet.mu * packet.mu) * dtStep)
260 while (packet.mu > 2) packet.mu -= 2;
261 while (packet.mu < -2) packet.mu += 2;
262 if (packet.mu > 1) packet.mu = 2 - packet.mu;
263 if (packet.mu < -1) packet.mu = -2 - packet.mu;
267 for (
int i = 0; i < lossMech::nMech; i++) {
269 if (pSec[i][j] == 0.0)
continue;
270 if (rng.
uniform() < pSec[i][j] * (1 - wFac)) {
271 secondaryPositions.push_back(x);
273 push_back(loss.drawSecondary(gd, packet, t, wSec / wFac,
274 (lossMech::mechType) i,
282 + std::sqrt( 2 * pd.
kPar / dtStep) * rng.
normal();
283 Real vN = xDrift[1] +
285 Real vB = xDrift[2] +
286 + std::sqrt( 2 * pd.
kPerp / dtStep ) * rng.
normal();
287 Real vMag = gd.
v.
mag() + std::sqrt( vT*vT + vN*vN + vB*vB );
299 while (dtStep * vMag > nDiv * gasBG.
dxGhost()) nDiv *= 2;
306 bool success =
false;
310 Real dtTmp = dtStep / nDiv;
322 for (
int n=0; n<nDiv; n++) {
326 gasBG.
frame(x, t + dtTmp * n, vCom, b);
328 x = xInit + dtTmp *
rkf::ah[0] * k1;
330#ifdef TRACK_PITCH_ANGLE
340#ifdef TRACK_PITCH_ANGLE
351#ifdef TRACK_PITCH_ANGLE
362#ifdef TRACK_PITCH_ANGLE
374#ifdef TRACK_PITCH_ANGLE
382 x = xInit + dtTmp * (
rkf::c1 * k1 +
388#ifdef TRACK_PITCH_ANGLE
394 err2 += (dtTmp/dx) * (dtTmp/dx) *
404 if (err2 > errTol * errTol) {
415 packet.
w = -packet.
w;
428 if (packet.
w / packet.
wInj < wFracMin ||
431 packet.
w = -packet.
w;
Class to hold data on a CR packet.
Define classes for field quantities and their gradients.
A class to describe the geometry of a calculation.
A class that holds data to describe a CR packet.
Definition CRPacket.H:28
Real tInj
Definition CRPacket.H:45
Real gr
Definition CRPacket.H:49
Real v() const
Velocity of packet.
Definition CRPacket.H:118
Real wInj
Definition CRPacket.H:46
Real T() const
Kinetic energy of packet.
Definition CRPacket.H:94
Real w
Definition CRPacket.H:48
partTypes::pType type
Definition CRPacket.H:42
Real p
Definition CRPacket.H:47
Class to hold and compute gradients of field quantities.
Definition FieldQty.H:293
Class to hold and compute field quantities.
Definition FieldQty.H:53
A class that describes the geometry of a calculation.
Definition Geometry.H:32
bool contains(const RealVec &x) const
Report if a specified point is in the problem domain.
Definition Geometry.H:245
void applyBC(RealVec &x) const
Apply boundary conditions to a packet.
Definition Geometry.H:124
Real contract(const RealVec &a, const RealVec &b) const
Compute the tensor contraction a_i b_j T_ij.
Definition RealTensor2.H:395
Real trace() const
Return the trace of this tensor.
Definition RealTensor2.H:449
Thread-safe random number generator.
Definition RngThread.H:39
Real uniform(const Real a=0.0, const Real b=1.0) const
Return a random number uniformly-distributed in (a,b)
Definition RngThread.H:66
Real normal(const Real mean=0.0, const Real stddev=1.0) const
Return a normally-distributed random number.
Definition RngThread.H:90
Real mag() const
Computes the magnitude of the vector.
Definition Vec3.H:470
auto dot(const Vec3< U > &a) const
Returns the dot product of this vector with another vector.
Definition Vec3.H:575
Trivial class to hold gas data.
Definition GasData.H:29
Real den
Definition GasData.H:36
RealVec v
Definition GasData.H:38
RealTensor2 vGrad
Definition GasData.H:44
Real dx
Definition GasData.H:33
RealVec B
Definition GasData.H:39
RealTensor2 BGrad
Definition GasData.H:45
Interface class to describe background gas.
Definition Gas.H:44
virtual void frame(const RealVec &x, const Real t, RealVec &v, TNBBasis &tnb) const
Compute the comoving TNB frame for the gas.
Definition Gas.H:67
virtual GasData gasData(const RealVec &x, const Real t) const =0
Return background gas state.
virtual Real dxGhost() const
Size of the ghost region in the gas data.
Definition Gas.H:107
Interface to describe the CR propagation model.
Definition Propagation.H:79
void applyLimits(const gas::GasData &gd, const CRPacket &packet, PropagationData &pd) const
Apply the Bohm limit and manually-set limits.
Definition Propagation.H:147
void haltRun(std::string msg, errCodes errcode)
Halt a run because an error has occurred.
Definition MPIUtil.cpp:287
static constexpr Real c
Definition Constants.H:34
pType
Enum of particle types.
Definition PartTypes.H:32
@ nPartType
Definition PartTypes.H:41
static constexpr Real ah[]
Definition RKF.H:20
static constexpr Real c6
Definition RKF.H:44
static constexpr Real b5[]
Definition RKF.H:28
static constexpr Real b3[]
Definition RKF.H:23
static constexpr Real c5
Definition RKF.H:42
static constexpr Real c1
Definition RKF.H:36
static constexpr Real ec[]
Definition RKF.H:45
static constexpr Real c3
Definition RKF.H:38
static constexpr Real c4
Definition RKF.H:40
static constexpr Real b6[]
Definition RKF.H:31
static constexpr Real b4[]
Definition RKF.H:25
The primary namespace for criptic objects.
Definition AdvancePacket.H:25
criptic::Real advancePacket(const criptic::Real tStart, const criptic::Real tStop, const criptic::Geometry &geom, const criptic::gas::Gas &gasBG, const criptic::propagation::Propagation &prop, const criptic::Losses &loss, const criptic::RngThread &rng, const criptic::FieldQty &qty, const criptic::FieldQtyGrad &qtyGrad, const criptic::Real cStep, const criptic::Real errTol, const criptic::Real wFracMin, const criptic::Real pMin, const criptic::Real TMin, const criptic::Real packetDtMin, const criptic::Real &h, criptic::RealVec &x, criptic::CRPacket &packet, criptic::Real &dt, std::vector< RealVec > &secondaryPositions, std::vector< CRPacket > &secondaryPackets)
Advance a packet by the specified time.
Definition AdvancePacket.H:61
@ errTimeStepTooSmall
Definition ErrCodes.H:39
TNBBasis TNBVectors(const RealVec &v, const RealTensor2 &vGrad)
Compute tangent, normal, and binormal vectors.
Definition RealTensor2.H:913
constexpr Real maxReal
Definition Types.H:49
double Real
Definition Types.H:38
Structure to hold TNB basis data.
Definition RealTensor2.H:898
RealVec eB
Definition RealTensor2.H:901
RealVec eN
Definition RealTensor2.H:900
RealVec eT
Definition RealTensor2.H:899
Structure contain the requires propagation parameters.
Definition Propagation.H:46
Real kPP
Definition Propagation.H:49
RealVec kParGrad
Definition Propagation.H:59
Real dvStr_dp
Definition Propagation.H:57
Real kPar
Definition Propagation.H:47
Real vStr
Definition Propagation.H:53
Real dkPP_dp
Definition Propagation.H:55
RealVec vStrGrad
Definition Propagation.H:66
RealVec kPerpGrad
Definition Propagation.H:61
Real kPerp
Definition Propagation.H:48