Line data Source code
1 : #include "crpropa/magneticField/UF26Field.h"
2 : #include "crpropa/GridTools.h"
3 : #include "crpropa/Units.h"
4 : #ifdef CRPROPA_HAVE_FFTW3F
5 : #include "crpropa/magneticField/turbulentField/SimpleGridTurbulence.h"
6 : #endif
7 :
8 : #include "kiss/logger.h"
9 :
10 : #include <cmath>
11 : #include <stdexcept>
12 :
13 : // local helper functions and constants
14 : namespace uf26 {
15 : // Internal units used in this code. Conversion to CRPropa units
16 : // happens only in the public get...() methods.
17 : const double kpc = 1;
18 : const double microgauss = 1;
19 : // galactocentric radius of the Sun (Gravity Collaboration 2019)
20 : const double kSunRadius = 8.178 * kpc;
21 : // arcosh(e): sech(arcosh(e) z/z_d) = 1/e at z = z_d
22 : const double kArcoshE = std::acosh(std::exp(1.));
23 :
24 : // Gaussian G(x; sigma) of Eq. (16)
25 : inline double gauss(const double x, const double sigma) {
26 10247 : return std::exp(-0.5 * x * x / (sigma * sigma));
27 : }
28 : }
29 :
30 : namespace crpropa {
31 :
32 10 : UF26Field::UF26Field(const ModelType mt) : fModelType(mt) {
33 : // common to both models
34 10 : fDiskRmax = 18 * uf26::kpc;
35 10 : fDiskWmax = 2 * uf26::kpc;
36 :
37 10 : switch (fModelType) {
38 5 : case expDisk:
39 5 : fDiskB = 4.4 * uf26::microgauss;
40 5 : fDiskZ = 1.0 * uf26::kpc;
41 5 : fDiskL = 15 * uf26::kpc;
42 5 : fDiskRc = 1 * uf26::kpc;
43 : fHasAnnulus = false;
44 5 : break;
45 5 : case ringDisk:
46 5 : fDiskB = 4.1 * uf26::microgauss;
47 5 : fDiskZ = 0.9 * uf26::kpc;
48 5 : fDiskL = 100 * uf26::kpc;
49 : fDiskRc = 0;
50 5 : fAnnulusB = 4.1 * uf26::microgauss;
51 5 : fAnnulusR = 4.5 * uf26::kpc;
52 5 : fAnnulusW = 2.0 * uf26::kpc;
53 5 : fAnnulusZ = 1.9 * uf26::kpc;
54 5 : fHasAnnulus = true;
55 5 : break;
56 0 : default:
57 0 : throw std::runtime_error("UF26Field: unknown model type");
58 : }
59 :
60 10 : fSunRadiusSmoothed =
61 10 : std::sqrt(uf26::kSunRadius * uf26::kSunRadius + fDiskRc * fDiskRc);
62 10 : }
63 :
64 : #ifdef CRPROPA_HAVE_FFTW3F
65 0 : void UF26Field::randomTurbulent(int seed, double lc) {
66 0 : if (!(lc > 0))
67 0 : throw std::runtime_error("UF26Field::randomTurbulent: lc must be > 0");
68 : // Kolmogorov spectrum with B_rms = 1 (scaled by b(x)) and fixed dynamic
69 : // range lMax/lMin = 34 on 256^3 grid points with lMin = 2 spacings.
70 : // All length scales are proportional to Lc, i.e. Lc(lMin, lMax) =
71 : // lMax * Lc(r, 1) with r = lMin/lMax.
72 : const double s = 5. / 3;
73 : const double r = 1. / 34;
74 0 : const double lMax = lc / SimpleTurbulenceSpectrum::turbulentCorrelationLength(r, 1, s);
75 0 : const double lMin = r * lMax;
76 : const double Brms = 1;
77 0 : const double spacing = lMin / 2;
78 : const size_t gridN = 256;
79 :
80 : auto spectrum = SimpleTurbulenceSpectrum(Brms, lMin, lMax);
81 : auto gp = GridProperties(Vector3d(0.), gridN, spacing);
82 : auto tf = SimpleGridTurbulence(spectrum, gp, seed);
83 0 : setTurbulentGrid(tf.getGrid());
84 0 : }
85 : #endif
86 :
87 4 : void UF26Field::setTurbulentGrid(ref_ptr<Grid3f> grid) {
88 4 : if (!grid.valid())
89 1 : throw std::runtime_error("UF26Field::setTurbulentGrid: invalid grid");
90 3 : const double rms = rmsFieldStrength(grid);
91 3 : if (std::fabs(rms - 1) > 0.01) {
92 0 : KISS_LOG_WARNING << "UF26Field: turbulent grid has Brms = " << rms
93 0 : << ", expected 1.";
94 : }
95 3 : turbulentGrid = grid;
96 3 : }
97 :
98 1 : ref_ptr<Grid3f> UF26Field::getTurbulentGrid() {
99 1 : return turbulentGrid;
100 : }
101 :
102 4 : bool UF26Field::isUsingTurbulentField() const {
103 4 : return turbulentGrid.valid();
104 : }
105 :
106 2 : UF26Field::ModelType UF26Field::getModelType() const {
107 2 : return fModelType;
108 : }
109 :
110 2 : std::string UF26Field::getModelName() const {
111 3 : return fModelType == expDisk ? "expDisk" : "ringDisk";
112 : }
113 :
114 4127 : Vector3d UF26Field::getField(const Vector3d &pos) const {
115 4127 : return getTurbulentField(pos);
116 : }
117 :
118 4127 : Vector3d UF26Field::getTurbulentField(const Vector3d &pos) const {
119 4127 : if (!turbulentGrid.valid())
120 : throw std::runtime_error("UF26Field: no turbulent grid set, "
121 1 : "run e.g. randomTurbulent()");
122 4126 : const double b = getTurbulentStrength(pos);
123 : // skip the grid interpolation where the model vanishes
124 4126 : if (b == 0)
125 : return Vector3d(0.);
126 4126 : return Vector3d(turbulentGrid->interpolate(pos)) * b;
127 : }
128 :
129 8403 : double UF26Field::getTurbulentStrength(const Vector3d &pos) const {
130 : const Vector3d p = pos / kpc * uf26::kpc;
131 : // rms strengths of independent components add in quadrature
132 8403 : return std::hypot(rmsDisk(p), rmsAnnulus(p)) / uf26::microgauss * microgauss;
133 : }
134 :
135 8403 : double UF26Field::rmsDisk(const Vector3d &pos) const {
136 8403 : const double r = std::sqrt(pos.x * pos.x + pos.y * pos.y);
137 : // E_d(r)/E_d(R_sun), Eq. (13)
138 : const double radial =
139 8403 : std::exp(-(std::sqrt(r * r + fDiskRc * fDiskRc) - fSunRadiusSmoothed) / fDiskL);
140 : // outer cutoff S_d(r)
141 8403 : const double cutoff = 1 / (1 + std::exp((r - fDiskRmax) / fDiskWmax));
142 : // vertical profile V_d(z), Eq. (14)
143 8403 : const double vertical = 1 / std::cosh(uf26::kArcoshE * pos.z / fDiskZ);
144 8403 : double b = fDiskB * radial * cutoff * vertical;
145 : // ringDisk: disk is phased out toward the center following the inner
146 : // flank of the annulus, G(min(r - r_a, 0); w_a)
147 8403 : if (fHasAnnulus && r < fAnnulusR)
148 1950 : b *= uf26::gauss(r - fAnnulusR, fAnnulusW);
149 8403 : return b;
150 : }
151 :
152 8403 : double UF26Field::rmsAnnulus(const Vector3d &pos) const {
153 8403 : if (!fHasAnnulus)
154 : return 0;
155 8297 : const double r = std::sqrt(pos.x * pos.x + pos.y * pos.y);
156 : // vertical Gaussian with 1/e height z_a, i.e. G(z; z_a/sqrt(2))
157 8297 : const double zz = pos.z / fAnnulusZ;
158 8297 : return fAnnulusB * uf26::gauss(r - fAnnulusR, fAnnulusW) * std::exp(-zz * zz);
159 : }
160 :
161 : } // namespace crpropa
|