OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
FM2DDynamic.cpp
Go to the documentation of this file.
2#include "Fields/Fieldmap.hpp"
3#include "Physics/Physics.h"
4#include "Physics/Units.h"
6#include "Utilities/Util.h"
7
8#include <cmath>
9#include <fstream>
10#include <ios>
11
12FM2DDynamic::FM2DDynamic(std::string aFilename)
13 : Fieldmap(aFilename),
14 FieldstrengthEz_m(nullptr),
15 FieldstrengthEr_m(nullptr),
16 FieldstrengthBt_m(nullptr) {
17 std::ifstream file;
18 std::string tmpString;
19 double tmpDouble;
20
22
23 // open field map, parse it and disable element on error
24 file.open(Filename_m.c_str());
25 if (file.good()) {
26 bool parsing_passed = true;
27 try {
28 parsing_passed = interpretLine<std::string, std::string>(file, tmpString, tmpString);
29 } catch (GeneralClassicException& e) {
31 file, tmpString, tmpString, tmpString);
32
33 tmpString = Util::toUpper(tmpString);
34 if (tmpString != "TRUE" && tmpString != "FALSE")
36 "FM2DDynamic::FM2DDynamic",
37 "The third string on the first line of 2D field "
38 "maps has to be either TRUE or FALSE");
39
40 normalize_m = (tmpString == "TRUE");
41 }
42
43 if (tmpString == "ZX") {
44 swap_m = true;
45 parsing_passed =
46 parsing_passed
48 parsing_passed = parsing_passed && interpretLine<double>(file, frequency_m);
49 parsing_passed =
50 parsing_passed
52 } else if (tmpString == "XZ") {
53 swap_m = false;
54 parsing_passed =
55 parsing_passed
57 parsing_passed = parsing_passed && interpretLine<double>(file, frequency_m);
58 parsing_passed =
59 parsing_passed
61 } else {
62 std::cerr << "unknown orientation of 2D dynamic fieldmap" << std::endl;
63 parsing_passed = false;
64 zbegin_m = 0.0;
65 zend_m = -1e-3;
66 }
67
68 for (long i = 0; (i < (num_gridpz_m + 1) * (num_gridpr_m + 1)) && parsing_passed; ++i) {
69 parsing_passed = parsing_passed
71 file, tmpDouble, tmpDouble, tmpDouble, tmpDouble);
72 }
73
74 parsing_passed = parsing_passed && interpreteEOF(file);
75
76 file.close();
77 lines_read_m = 0;
78
79 if (!parsing_passed) {
81 zend_m = zbegin_m - 1e-3;
83 "FM2DDynamic::FM2DDynamic",
84 "An error occured when reading the fieldmap '" + Filename_m + "'");
85 } else {
86 // convert MHz to Hz and frequency to angular frequency
88
89 // convert cm to m
94
97
98 // num spacings -> num grid points
100 num_gridpz_m++;
101 }
102 } else {
103 noFieldmapWarning();
104 zbegin_m = 0.0;
105 zend_m = -1e-3;
106 }
107}
108
112
114 if (FieldstrengthEz_m == nullptr) {
115 // declare variables and allocate memory
116 std::ifstream in;
117 std::string tmpString;
118 double tmpDouble, Ezmax = 0.0;
119
123
124 // read in field map and parse it
125 in.open(Filename_m.c_str());
126 getLine(in, tmpString);
127 getLine(in, tmpString);
128 getLine(in, tmpString);
129 getLine(in, tmpString);
130
131 if (swap_m) {
132 for (int i = 0; i < num_gridpz_m; i++) {
133 for (int j = 0; j < num_gridpr_m; j++) {
135 in, FieldstrengthEr_m[i + j * num_gridpz_m],
137 FieldstrengthBt_m[i + j * num_gridpz_m], tmpDouble);
138 }
139 }
140 } else {
141 for (int j = 0; j < num_gridpr_m; j++) {
142 for (int i = 0; i < num_gridpz_m; i++) {
144 in, FieldstrengthEz_m[i + j * num_gridpz_m],
145 FieldstrengthEr_m[i + j * num_gridpz_m], tmpDouble,
147 }
148 }
149 }
150
151 in.close();
152
153 if (normalize_m) {
154 // find maximum field
155 for (int i = 0; i < num_gridpz_m; ++i) {
156 if (std::abs(FieldstrengthEz_m[i]) > Ezmax) {
157 Ezmax = std::abs(FieldstrengthEz_m[i]);
158 }
159 }
160 } else {
161 Ezmax = 1.0;
162 }
163
164 for (int i = 0; i < num_gridpr_m * num_gridpz_m; i++) {
165 FieldstrengthEz_m[i] *= 1.e6 / Ezmax; // conversion MV/m to V/m and normalization
166 FieldstrengthEr_m[i] *= 1.e6 / Ezmax;
167 FieldstrengthBt_m[i] *= Physics::mu_0 / Ezmax; // H -> B
168 }
169
170 *ippl::Info << level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info")
171 << "\n"
172 << endl;
173 }
174}
175
177 if (FieldstrengthEz_m != nullptr) {
178 delete[] FieldstrengthEz_m;
179 FieldstrengthEz_m = nullptr;
180 delete[] FieldstrengthEr_m;
181 FieldstrengthEr_m = nullptr;
182 delete[] FieldstrengthBt_m;
183 FieldstrengthBt_m = nullptr;
184
185 *ippl::Info << level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << "\n"
186 << endl;
187 }
188}
189
192 // do bi-linear interpolation
193 const double RR = std::sqrt(R(0) * R(0) + R(1) * R(1));
194
195 const int indexr = (int)std::floor(RR / hr_m);
196 const double leverr = RR / hr_m - indexr;
197
198 const int indexz = (int)std::floor((R(2) - zbegin_m) / hz_m);
199 const double leverz = (R(2) - zbegin_m) / hz_m - indexz;
200
201 if ((indexz < 0) || (indexz + 2 > num_gridpz_m))
202 return false;
203 if (indexr + 2 > num_gridpr_m)
204 return true;
205
206 const int index1 = indexz + indexr * num_gridpz_m;
207 const int index2 = index1 + num_gridpz_m;
208
209 double EfieldR = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEr_m[index1]
210 + leverz * (1.0 - leverr) * FieldstrengthEr_m[index1 + 1]
211 + (1.0 - leverz) * leverr * FieldstrengthEr_m[index2]
212 + leverz * leverr * FieldstrengthEr_m[index2 + 1];
213
214 double EfieldZ = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEz_m[index1]
215 + leverz * (1.0 - leverr) * FieldstrengthEz_m[index1 + 1]
216 + (1.0 - leverz) * leverr * FieldstrengthEz_m[index2]
217 + leverz * leverr * FieldstrengthEz_m[index2 + 1];
218
219 double BfieldT = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthBt_m[index1]
220 + leverz * (1.0 - leverr) * FieldstrengthBt_m[index1 + 1]
221 + (1.0 - leverz) * leverr * FieldstrengthBt_m[index2]
222 + leverz * leverr * FieldstrengthBt_m[index2 + 1];
223
224 if (RR > 1e-10) {
225 E(0) += EfieldR * R(0) / RR;
226 E(1) += EfieldR * R(1) / RR;
227 B(0) -= BfieldT * R(1) / RR;
228 B(1) += BfieldT * R(0) / RR;
229 }
230 E(2) += EfieldZ;
231
232 return false;
233}
234
237 const DiffDirection& /*dir*/) const {
238 return false;
239}
240
241void FM2DDynamic::getFieldDimensions(double& zBegin, double& zEnd) const {
242 zBegin = zbegin_m;
243 zEnd = zend_m;
244}
246 double& /*xIni*/, double& /*xFinal*/, double& /*yIni*/, double& /*yFinal*/, double& /*zIni*/,
247 double& /*zFinal*/) const {
248}
249
251 if (swap_m)
252 swap_m = false;
253 else
254 swap_m = true;
255}
256
257void FM2DDynamic::getInfo(Inform* msg) {
258 (*msg) << Filename_m << " (2D dynamic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;"
259 << endl;
260}
261
263 return frequency_m;
264}
265
266void FM2DDynamic::setFrequency(double freq) {
267 frequency_m = freq;
268}
269
270void FM2DDynamic::getOnaxisEz(std::vector<std::pair<double, double> >& F) {
271 double dz = (zend_m - zbegin_m) / (num_gridpz_m - 1);
272 F.resize(num_gridpz_m);
273
274 for (int i = 0; i < num_gridpz_m; ++i) {
275 F[i].first = dz * i;
276 F[i].second = FieldstrengthEz_m[i] / 1e6;
277 }
278}
ippl::Vector< T, Dim > Vector_t
@ T2DDynamic
Definition Fieldmap.h:25
DiffDirection
Definition Fieldmap.h:55
constexpr double two_pi
The value of.
Definition Physics.h:33
constexpr double mu_0
The permeability of vacuum in Vs/Am.
Definition Physics.h:48
constexpr double MHz2Hz
Definition Units.h:113
constexpr double cm2m
Definition Units.h:35
std::string toUpper(const std::string &str)
Definition Util.cpp:145
MapType Type
Definition Fieldmap.h:118
bool interpreteEOF(std::ifstream &in)
Definition Fieldmap.cpp:516
void disableFieldmapWarning()
Definition Fieldmap.cpp:569
bool normalize_m
Definition Fieldmap.h:123
int lines_read_m
Definition Fieldmap.h:121
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition Fieldmap.cpp:607
std::string Filename_m
Definition Fieldmap.h:120
bool interpretLine(std::ifstream &in, S &value, const bool &file_length_known=true)
void getLine(std::ifstream &in, std::string &buffer)
Definition Fieldmap.h:124
double * FieldstrengthEz_m
Definition FM2DDynamic.h:29
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
virtual void swap()
double frequency_m
Definition FM2DDynamic.h:33
double hr_m
Definition FM2DDynamic.h:40
virtual double getFrequency() const
virtual void readMap()
virtual bool getFieldDerivative(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B, const DiffDirection &dir) const
virtual void setFrequency(double freq)
double rbegin_m
Definition FM2DDynamic.h:35
friend class Fieldmap
Definition FM2DDynamic.h:45
double rend_m
Definition FM2DDynamic.h:36
double hz_m
Definition FM2DDynamic.h:39
double zbegin_m
Definition FM2DDynamic.h:37
virtual void getInfo(Inform *msg)
virtual bool getFieldstrength(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B) const
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &F)
double * FieldstrengthBt_m
Definition FM2DDynamic.h:31
virtual void freeMap()
FM2DDynamic(std::string aFilename)
double zend_m
Definition FM2DDynamic.h:38
double * FieldstrengthEr_m
Definition FM2DDynamic.h:30