criptic v1
Cosmic Ray Interstellar Propagation Tool using Itô Calculus
Loading...
Searching...
No Matches
CRSource.H
Go to the documentation of this file.
1
10#ifndef _CRSOURCE_H_
11#define _CRSOURCE_H_
12
13#include <cmath>
14#include <gsl/gsl_sf.h>
15#include "CRPacket.H"
16#include "../Utils/Constants.H"
17#include "../Utils/PartTypes.H"
18#include "../Utils/RngThread.H"
19#include "../Utils/SR.H"
20#include "../Utils/Types.H"
21
22namespace criptic {
23
33 class CRSource {
34
35 public:
36
37 // Data
44#ifdef TRACK_PITCH_ANGLE
45 Real mu0;
46 Real mu1;
47#endif
55 Real T0() const {
56 return sr::T_from_p(type, p0);
57 }
58
63 Real T1() const {
64 return sr::T_from_p(type, p1);
65 }
66
71 void set_T0(const Real T0) {
73 }
74
79 void set_T1(const Real T1) {
81 }
82
87 Real E0() const {
88 return sr::E_from_p(type, p0);
89 }
90
95 Real E1() const {
96 return sr::E_from_p(type, p1);
97 }
98
103 Real R0() const {
104 return p0 / partTypes::charge[type];
105 }
106
111 Real R1() const {
112 return p1 / partTypes::charge[type];
113 }
114
122 Real nRate() const {
123 if (p1 <= p0) return k;
124 else if (q == -1.0) return k * std::log(p1/p0);
125 else return k * (std::pow(p1, q+1) - std::pow(p0, q+1)) / (q+1);
126 }
127
135 Real pRate() const {
136 if (p1 <= p0) return k * p0;
137 else if (q == -2.0) return k * std::log(p1/p0);
138 else return k * (std::pow(p1, q+2) - std::pow(p0, q+2)) / (q+2);
139 }
140
150 Real TRate() const {
151 if (p1 <= p0) return k * sr::T_from_p(type, p0);
152 else {
154 Real m2 = m*m;
155 Real p02 = p0*p0;
156 Real p12 = p1*p1;
157 if (q == -2.0) {
158 return k * m * (1/p1 - 1/p0) +
159 std::sqrt(1 + m2/p02) - std::sqrt(1 + m2/p12) +
160 std::asinh(p1/m) - std::asinh(p0/m);
161 } else if (q == 0.0) {
162 // Use series expansions to avoid underflows
163 Real p1fac = (m2/p12 > 1e-6) ?
164 std::log( p1 * (std::sqrt(1 + m2/p12) - 1) ) :
165 std::log( m2 / (2 * p1) - m2*m2 / (8 * p1 * p12));
166 Real p0fac = (m2/p12 > 1e-6) ?
167 std::log( p0 * (std::sqrt(1 + m2/p02) - 1) ) :
168 std::log( m2 / (2 * p0) - m2*m2 / (8 * p0 * p02));
169 return k * 0.5 * (p1 * std::sqrt(p12 + m2) -
170 p0 * std::sqrt(p02 + m2) -
171 2 * m * (p1 - p0) -
172 m2 * (p1fac - p0fac) );
173 } else {
174 Real a = -1-q/2.0;
175 Real b = (1+q)/2.0;
176 Real term1 = m/(q+1) *
177 (std::pow(p0, q+1) - std::pow(p1, q+1));
178 Real term2 = 0.5 * std::pow(m, q+2) * gsl_sf_beta(a, b) *
179 ( gsl_sf_beta_inc( a, b, 1/(1+p02/m2) ) -
180 gsl_sf_beta_inc( a, b, 1/(1+p12/m2) ) );
181 return k * (term1 + term2);
182 }
183 }
184 }
185
193 Real pBar() const {
194 return pRate() / nRate();
195 }
196
204 Real TBar() const {
205 return TRate() / nRate();
206 }
207
218 Real wgt(const Real qSamp) const {
219 if (p1 <= p0)
220 return k / std::pow(p0, qSamp);
221 else if (qSamp == -1.0)
222 return k * std::log(p1/p0) / std::pow(p0, qSamp);
223 else
224 return k * (std::pow(p1, qSamp+1) - std::pow(p0, qSamp+1)) /
225 ((qSamp+1) * std::pow(p0, qSamp));
226 }
227
240 void setLum(const Real lum, const bool codeUnits = false) {
241 k = 1.0;
242 if (codeUnits) k = lum / TRate();
243 else k = (lum / constants::mp_c2) / TRate();
244 }
245
262 std::vector<CRPacket> draw(const IdxType n,
263 const Real t0,
264 const Real t1,
265 const Real qSamp,
266 const RngThread& rng) const;
267 };
268
269}
270
271#endif
272// _CRSOURCE_H_
Class to hold data on a CR packet.
A class to describe a source of CR packets.
Definition CRSource.H:33
void setLum(const Real lum, const bool codeUnits=false)
Set the rate coefficient to produce a specified luminosity.
Definition CRSource.H:240
Real p0
Definition CRSource.H:40
Real nRate() const
Returns total CR injection rate per unit time.
Definition CRSource.H:122
Real T1() const
Returns kinetic energy corresponding to maximum momentum.
Definition CRSource.H:63
Real E1() const
Returns total energy corresponding to maximum momentum.
Definition CRSource.H:95
Real pRate() const
Returns total linear momentum injection rate per time.
Definition CRSource.H:135
RealVec a
Definition CRSource.H:49
Real TRate() const
Returns total kinetic energy injection rate per time.
Definition CRSource.H:150
void set_T0(const Real T0)
Set the momentum lower limit by specifying the kinetic energy.
Definition CRSource.H:71
Real pBar() const
Return mean CR momentum injected.
Definition CRSource.H:193
Real k
Definition CRSource.H:43
Real p1
Definition CRSource.H:41
Real TBar() const
Return mean CR kinetic energy injected.
Definition CRSource.H:204
Real q
Definition CRSource.H:42
RealVec v
Definition CRSource.H:48
Real E0() const
Returns total energy corresponding to minimum momentum.
Definition CRSource.H:87
Real T0() const
Returns kinetic energy corresponding to minimum momentum.
Definition CRSource.H:55
Real wgt(const Real qSamp) const
Return the statistical weight of this source.
Definition CRSource.H:218
IdxType uniqueID
Definition CRSource.H:39
Real R1() const
Returns rigidity correspondning to minimum momentum.
Definition CRSource.H:111
std::vector< CRPacket > draw(const IdxType n, const Real t0, const Real t1, const Real qSamp, const RngThread &rng) const
Draw CR packets.
Definition CRSource.cpp:11
Real R0() const
Returns rigidity correspondning to minimum momentum.
Definition CRSource.H:103
partTypes::pType type
Definition CRSource.H:38
void set_T1(const Real T1)
Set the momentum upper limit by specifying the kinetic energy.
Definition CRSource.H:79
Thread-safe random number generator.
Definition RngThread.H:39
static constexpr Real mp_c2
Definition Constants.H:118
constexpr Real mass[nPartType]
Definition PartTypes.H:44
constexpr int charge[nPartType]
Definition PartTypes.H:55
pType
Enum of particle types.
Definition PartTypes.H:32
static Real T_from_p(const Real m, const Real p)
Compute kinetic energy from rest mass and momentum.
Definition SR.H:119
static Real E_from_p(const Real m, const Real p)
Compute total energy from rest mass and momentum.
Definition SR.H:143
static Real p_from_T(const Real m, const Real T)
Compute momentum from rest mass and kinetic energy.
Definition SR.H:81
The primary namespace for criptic objects.
Definition AdvancePacket.H:25
std::vector< Real >::size_type IdxType
Definition Types.H:45
double Real
Definition Types.H:38