forked from lukasmerten/CRPropa_FieldLineIntegrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FieldlineIntegrator.cpp
171 lines (123 loc) · 3.96 KB
/
FieldlineIntegrator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "FieldlineIntegrator.h"
//#include <crpropa/Random.h>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <stdexcept>
using namespace crpropa;
// Defining Cash-Karp coefficients
const double a[] = { 0., 0., 0., 0., 0., 0., 1. / 5., 0., 0., 0., 0.,
0., 3. / 40., 9. / 40., 0., 0., 0., 0., 3. / 10., -9. / 10., 6. / 5.,
0., 0., 0., -11. / 54., 5. / 2., -70. / 27., 35. / 27., 0., 0., 1631.
/ 55296., 175. / 512., 575. / 13824., 44275. / 110592., 253.
/ 4096., 0. };
const double b[] = { 37. / 378., 0, 250. / 621., 125. / 594., 0., 512.
/ 1771. };
const double bs[] = { 2825. / 27648., 0., 18575. / 48384., 13525.
/ 55296., 277. / 14336., 1. / 4. };
FieldlineIntegrator::FieldlineIntegrator(ref_ptr<MagneticField> field, double tolerance,
double minStep, double maxStep, double direction) :
minStep(0)
{
setField(field);
setMaximumStep(maxStep);
setMinimumStep(minStep);
setTolerance(tolerance);
setDirection(direction);
}
void FieldlineIntegrator::process(Candidate *candidate) const {
// save the new previous particle state
ParticleState ¤t = candidate->current;
candidate->previous = current;
double step = clip(candidate->getNextStep(), minStep, maxStep);
Vector3d PosIn = current.getPosition();
Vector3d PosOut, Err;
double r=42.; //arbitrary number larger than one
double newstep = step;
while (r >1) {
step = newstep;
tryStep(PosIn, PosOut, Err, step / c_light);
r = Err.getR() / tolerance;
newstep = step * 0.95 * pow(r, -0.2);
newstep = clip(newstep, 0.1 * step, 5 * step);
newstep = clip(newstep, minStep, maxStep);
if (step == minStep)
break;
}
current.setPosition(PosOut);
candidate->setCurrentStep(step);
candidate->setNextStep(newstep);
}
void FieldlineIntegrator::tryStep(const Vector3d &PosIn, Vector3d &PosOut, Vector3d &Err, double h) const {
Vector3d k[] = {Vector3d(0.),Vector3d(0.),Vector3d(0.),Vector3d(0.),Vector3d(0.),Vector3d(0.)};
PosOut = PosIn;
Err = Vector3d(0.);
//calculate the sum k_i * b_i
for (size_t i = 0; i < 6; i++) {
Vector3d y_n = PosIn;
for (size_t j = 0; j < i; j++)
y_n += k[j] * a[i * 6 + j] * h;
// update k_i = direction of the regular magnetic mean field
Vector3d BField(0.);
try {
BField = field->getField(y_n, 0.); // redshift evolution not taken into account
}
catch (std::exception &e) {
std::cerr << "FieldlineIntegrator: Exception in getField." << std::endl;
std::cerr << e.what() << std::endl;
}
k[i] = BField.getUnitVector() * c_light * direction;
PosOut += k[i] * b[i] * h;
Err += (k[i] * (b[i] - bs[i])) * h / kpc;
}
//std::cout << "PosErr = " << PosErr <<"\n";
}
void FieldlineIntegrator::setMinimumStep(double min) {
if (min < 0)
throw std::runtime_error("FieldlineIntegrator: minStep < 0 ");
if (min > maxStep)
throw std::runtime_error("FieldlineIntegrator: minStep > maxStep");
minStep = min;
}
void FieldlineIntegrator::setMaximumStep(double max) {
if (max < minStep)
throw std::runtime_error("FieldlineIntegrator: maxStep < minStep");
maxStep = max;
}
void FieldlineIntegrator::setTolerance(double tol) {
if ((tol > 1) or (tol < 0))
throw std::runtime_error(
"FieldlineIntegrator: tolerance error not in range 0-1");
tolerance = tol;
}
void FieldlineIntegrator::setDirection(bool d) {
if (d) {
direction = 1.;
}
else {
direction = -1;
}
}
void FieldlineIntegrator::setField(ref_ptr<MagneticField> f) {
field = f;
}
double FieldlineIntegrator::getMinimumStep() const {
return minStep;
}
double FieldlineIntegrator::getMaximumStep() const {
return maxStep;
}
double FieldlineIntegrator::getTolerance() const {
return tolerance;
}
double FieldlineIntegrator::getDirection() const {
return direction;
}
std::string FieldlineIntegrator::getDescription() const {
std::stringstream s;
s << "minStep: " << minStep / kpc << " kpc, ";
s << "maxStep: " << maxStep / kpc << " kpc, ";
s << "tolerance: " << tolerance << "\n";
return s.str();
}