OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
FM2DMagnetoStatic.cpp
Go to the documentation of this file.
2#include "Fields/Fieldmap.hpp"
3#include "Physics/Units.h"
5#include "Utilities/Util.h"
6
7#include <cmath>
8#include <fstream>
9#include <ios>
10
12 : Fieldmap(aFilename), FieldstrengthBz_m(nullptr), FieldstrengthBr_m(nullptr) {
13 std::ifstream file;
14 std::string tmpString;
15 double tmpDouble;
16
18
19 // open field map, parse it and disable element on error
20 file.open(Filename_m.c_str());
21 if (file.good()) {
22 bool parsing_passed = true;
23 try {
24 parsing_passed = interpretLine<std::string, std::string>(file, tmpString, tmpString);
25 } catch (GeneralClassicException& e) {
27 file, tmpString, tmpString, tmpString);
28
29 tmpString = Util::toUpper(tmpString);
30 if (tmpString != "TRUE" && tmpString != "FALSE")
32 "FM2DMagnetoStatic::FM2DMagnetoStatic",
33 "The third string on the first line of 2D field "
34 "maps has to be either TRUE or FALSE");
35
36 normalize_m = (tmpString == "TRUE");
37 }
38
39 if (tmpString == "ZX") {
40 swap_m = true;
41 parsing_passed =
42 parsing_passed
44 parsing_passed =
45 parsing_passed
47 } else if (tmpString == "XZ") {
48 swap_m = false;
49 parsing_passed =
50 parsing_passed
52 parsing_passed =
53 parsing_passed
55 } else {
56 std::cerr << "unknown orientation of 2D magnetostatic fieldmap" << std::endl;
57 parsing_passed = false;
58 }
59
60 for (long i = 0; (i < (num_gridpz_m + 1) * (num_gridpr_m + 1)) && parsing_passed; ++i) {
61 parsing_passed =
62 parsing_passed && interpretLine<double, double>(file, tmpDouble, tmpDouble);
63 }
64
65 parsing_passed = parsing_passed && interpreteEOF(file);
66
67 file.close();
68 lines_read_m = 0;
69
70 if (!parsing_passed) {
72 zend_m = zbegin_m - 1e-3;
74 "FM2DMagnetoStatic::FM2DMagnetoStatic",
75 "An error occured when reading the fieldmap '" + Filename_m + "'");
76 } else {
77 // conversion from cm to m
82
85
86 // num spacings -> num grid points
89 }
90 } else {
91 noFieldmapWarning();
92 zbegin_m = 0.0;
93 zend_m = -1e-3;
94 }
95}
96
100
102 if (FieldstrengthBz_m == nullptr) {
103 // declare variables and allocate memory
104 std::ifstream in;
105 std::string tmpString;
106
109
110 // read in and parse field map
111 in.open(Filename_m.c_str());
112 getLine(in, tmpString);
113 getLine(in, tmpString);
114 getLine(in, tmpString);
115
116 if (swap_m) {
117 for (int i = 0; i < num_gridpz_m; i++) {
118 for (int j = 0; j < num_gridpr_m; j++) {
120 in, FieldstrengthBr_m[i + j * num_gridpz_m],
122 }
123 }
124 } else {
125 for (int j = 0; j < num_gridpr_m; j++) {
126 for (int i = 0; i < num_gridpz_m; i++) {
128 in, FieldstrengthBz_m[i + j * num_gridpz_m],
130 }
131 }
132 }
133 in.close();
134
135 if (normalize_m) {
136 double Bzmax = 0.0;
137 // find maximum field
138 for (int i = 0; i < num_gridpz_m; ++i) {
139 if (std::abs(FieldstrengthBz_m[i]) > Bzmax) {
140 Bzmax = std::abs(FieldstrengthBz_m[i]);
141 }
142 }
143
144 // normalize field
145 for (int i = 0; i < num_gridpr_m * num_gridpz_m; ++i) {
146 FieldstrengthBz_m[i] /= Bzmax;
147 FieldstrengthBr_m[i] /= Bzmax;
148 }
149 }
150
151 *ippl::Info << level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info")
152 << endl;
153 }
154}
155
157 if (FieldstrengthBz_m != nullptr) {
158 delete[] FieldstrengthBz_m;
159 delete[] FieldstrengthBr_m;
160
161 FieldstrengthBz_m = nullptr;
162 FieldstrengthBr_m = nullptr;
163
164 *ippl::Info << level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << endl;
165 }
166}
167
170 // do bi-linear interpolation
171 const double RR = std::sqrt(R(0) * R(0) + R(1) * R(1));
172
173 const int indexr = std::abs((int)std::floor(RR / hr_m));
174 const double leverr = (RR / hr_m) - indexr;
175
176 const int indexz = std::abs((int)std::floor((R(2) - zbegin_m) / hz_m));
177 const double leverz = (R(2) - zbegin_m) / hz_m - indexz;
178
179 if ((indexz < 0) || (indexz + 2 > num_gridpz_m))
180 return false;
181
182 if (indexr + 2 > num_gridpr_m)
183 return true;
184
185 const int index1 = indexz + indexr * num_gridpz_m;
186 const int index2 = index1 + num_gridpz_m;
187 const double BfieldR = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthBr_m[index1]
188 + leverz * (1.0 - leverr) * FieldstrengthBr_m[index1 + 1]
189 + (1.0 - leverz) * leverr * FieldstrengthBr_m[index2]
190 + leverz * leverr * FieldstrengthBr_m[index2 + 1];
191
192 const double BfieldZ = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthBz_m[index1]
193 + leverz * (1.0 - leverr) * FieldstrengthBz_m[index1 + 1]
194 + (1.0 - leverz) * leverr * FieldstrengthBz_m[index2]
195 + leverz * leverr * FieldstrengthBz_m[index2 + 1];
196
197 if (RR != 0) {
198 B(0) += BfieldR * R(0) / RR;
199 B(1) += BfieldR * R(1) / RR;
200 }
201 B(2) += BfieldZ;
202 return false;
203}
204
207 const DiffDirection& dir) const {
208 double BfieldR, BfieldZ;
209
210 const double RR = std::sqrt(R(0) * R(0) + R(1) * R(1));
211
212 const int indexr = (int)std::floor(RR / hr_m);
213 const double leverr = (RR / hr_m) - indexr;
214
215 const int indexz = (int)std::floor((R(2)) / hz_m);
216 const double leverz = (R(2) / hz_m) - indexz;
217
218 if ((indexz < 0) || (indexz + 2 > num_gridpz_m))
219 return false;
220
221 if (indexr + 2 > num_gridpr_m)
222 return true;
223
224 const int index1 = indexz + indexr * num_gridpz_m;
225 const int index2 = index1 + num_gridpz_m;
226 if (dir == DZ) {
227 if (indexz > 0) {
228 if (indexz < num_gridpz_m - 1) {
229 BfieldR = (1.0 - leverr)
230 * ((FieldstrengthBr_m[index1 - 1] - 2. * FieldstrengthBr_m[index1]
231 + FieldstrengthBr_m[index1 + 1])
232 * (R(2) - indexz * hz_m) / (hz_m * hz_m)
233 + (FieldstrengthBr_m[index1 + 1] - FieldstrengthBr_m[index1 - 1])
234 / (2. * hz_m))
235 + leverr
236 * ((FieldstrengthBr_m[index2 - 1] - 2. * FieldstrengthBr_m[index2]
237 + FieldstrengthBr_m[index2 + 1])
238 * (R(2) - indexz * hz_m) / (hz_m * hz_m)
239 + (FieldstrengthBr_m[index2 + 1] - FieldstrengthBr_m[index2 - 1])
240 / (2. * hz_m));
241
242 BfieldZ = (1.0 - leverr)
243 * ((FieldstrengthBz_m[index1 - 1] - 2. * FieldstrengthBz_m[index1]
244 + FieldstrengthBz_m[index1 + 1])
245 * (R(2) - indexz * hz_m) / (hz_m * hz_m)
246 + (FieldstrengthBz_m[index1 + 1] - FieldstrengthBz_m[index1 - 1])
247 / (2. * hz_m))
248 + leverr
249 * ((FieldstrengthBz_m[index2 - 1] - 2. * FieldstrengthBz_m[index2]
250 + FieldstrengthBz_m[index2 + 1])
251 * (R(2) - indexz * hz_m) / (hz_m * hz_m)
252 + (FieldstrengthBz_m[index2 + 1] - FieldstrengthBz_m[index2 - 1])
253 / (2. * hz_m));
254 } else {
255 BfieldR =
256 (1.0 - leverr)
257 * ((FieldstrengthBr_m[index1] - 2. * FieldstrengthBr_m[index1 - 1]
258 + FieldstrengthBr_m[index1 - 2])
259 * (R(2) - indexz * hz_m) / (hz_m * hz_m)
260 - (3. * FieldstrengthBr_m[index1] - 4. * FieldstrengthBr_m[index1 - 1]
261 + FieldstrengthBr_m[index1 - 2])
262 / (2. * hz_m))
263 + leverr
264 * ((FieldstrengthBr_m[index2] - 2. * FieldstrengthBr_m[index2 - 1]
265 + FieldstrengthBr_m[index2 - 2])
266 * (R(2) - indexz * hz_m) / (hz_m * hz_m)
267 - (3. * FieldstrengthBr_m[index2] - 4. * FieldstrengthBr_m[index1 - 1]
268 + FieldstrengthBr_m[index2 - 2])
269 / (2. * hz_m));
270
271 BfieldZ =
272 (1.0 - leverr)
273 * ((FieldstrengthBz_m[index1] - 2. * FieldstrengthBz_m[index1 - 1]
274 + FieldstrengthBz_m[index1 - 2])
275 * (R(2) - indexz * hz_m) / (hz_m * hz_m)
276 - (3. * FieldstrengthBz_m[index1] - 4. * FieldstrengthBr_m[index1 - 1]
277 + FieldstrengthBz_m[index1 - 2])
278 / (2. * hz_m))
279 + leverr
280 * ((FieldstrengthBz_m[index2] - 2. * FieldstrengthBz_m[index2 - 1]
281 + FieldstrengthBz_m[index2 - 2])
282 * (R(2) - indexz * hz_m) / (hz_m * hz_m)
283 - (3. * FieldstrengthBz_m[index2] - 4. * FieldstrengthBr_m[index1 - 1]
284 + FieldstrengthBz_m[index2 - 2])
285 / (2. * hz_m));
286 }
287 } else {
288 BfieldR =
289 (1.0 - leverr)
290 * ((FieldstrengthBr_m[index1] - 2. * FieldstrengthBr_m[index1 + 1]
291 + FieldstrengthBr_m[index1 + 2])
292 * (R(2) - indexz * hz_m) / (hz_m * hz_m)
293 - (3. * FieldstrengthBr_m[index1] - 4. * FieldstrengthBr_m[index1 + 1]
294 + FieldstrengthBr_m[index1 + 2])
295 / (2. * hz_m))
296 + leverr
297 * ((FieldstrengthBr_m[index2] - 2. * FieldstrengthBr_m[index2 + 1]
298 + FieldstrengthBr_m[index2 + 2])
299 * (R(2) - indexz * hz_m) / (hz_m * hz_m)
300 - (3. * FieldstrengthBr_m[index2] - 4. * FieldstrengthBr_m[index1 + 1]
301 + FieldstrengthBr_m[index2 + 2])
302 / (2. * hz_m));
303
304 BfieldZ =
305 (1.0 - leverr)
306 * ((FieldstrengthBz_m[index1] - 2. * FieldstrengthBz_m[index1 + 1]
307 + FieldstrengthBz_m[index1 + 2])
308 * (R(2) - indexz * hz_m) / (hz_m * hz_m)
309 - (3. * FieldstrengthBz_m[index1] - 4. * FieldstrengthBr_m[index1 + 1]
310 + FieldstrengthBz_m[index1 + 2])
311 / (2. * hz_m))
312 + leverr
313 * ((FieldstrengthBz_m[index2] - 2. * FieldstrengthBz_m[index2 + 1]
314 + FieldstrengthBz_m[index2 + 2])
315 * (R(2) - indexz * hz_m) / (hz_m * hz_m)
316 - (3. * FieldstrengthBz_m[index2] - 4. * FieldstrengthBr_m[index1 + 1]
317 + FieldstrengthBz_m[index2 + 2])
318 / (2. * hz_m));
319 }
320 } else {
321 if (indexr > 0) {
322 const int index_1 = index1 - num_gridpz_m;
323 if (indexr < num_gridpr_m - 1) {
324 BfieldR =
325 (1.0 - leverz)
326 * ((FieldstrengthBr_m[index_1 + 1] - 2. * FieldstrengthBr_m[index1 + 1]
327 + FieldstrengthBr_m[index2 + 1])
328 * (RR - indexr * hr_m) / (hr_m * hr_m)
329 + (FieldstrengthBr_m[index2 + 1] - FieldstrengthBr_m[index_1 + 1])
330 / (2. * hr_m))
331 + leverz
332 * ((FieldstrengthBr_m[index_1] - 2. * FieldstrengthBr_m[index1]
333 + FieldstrengthBr_m[index2])
334 * (RR - indexr * hr_m) / (hr_m * hr_m)
335 + (FieldstrengthBr_m[index2] - FieldstrengthBr_m[index_1])
336 / (2. * hr_m));
337
338 BfieldZ =
339 (1.0 - leverz)
340 * ((FieldstrengthBz_m[index_1 + 1] - 2. * FieldstrengthBz_m[index1 + 1]
341 + FieldstrengthBz_m[index2 + 1])
342 * (RR - indexr * hr_m) / (hr_m * hr_m)
343 + (FieldstrengthBz_m[index2 + 1] - FieldstrengthBz_m[index_1 + 1])
344 / (2. * hr_m))
345 + leverz
346 * ((FieldstrengthBz_m[index_1] - 2. * FieldstrengthBz_m[index1]
347 + FieldstrengthBz_m[index2])
348 * (RR - indexr * hr_m) / (hr_m * hr_m)
349 + (FieldstrengthBz_m[index2] - FieldstrengthBz_m[index_1])
350 / (2. * hr_m));
351 } else {
352 const int index_2 = index_1 - num_gridpz_m;
353 BfieldR =
354 (1.0 - leverz)
355 * ((FieldstrengthBr_m[index1 + 1] - 2. * FieldstrengthBr_m[index_1 + 1]
356 + FieldstrengthBr_m[index_2 + 1])
357 * (RR - indexr * hr_m) / (hr_m * hr_m)
358 - (3. * FieldstrengthBr_m[index1 + 1]
359 - 4. * FieldstrengthBr_m[index_1 + 1]
360 + FieldstrengthBr_m[index_2 + 1])
361 / (2. * hr_m))
362 + leverz
363 * ((FieldstrengthBr_m[index1] - 2. * FieldstrengthBr_m[index_1]
364 + FieldstrengthBr_m[index_2])
365 * (RR - indexr * hr_m) / (hr_m * hr_m)
366 - (3. * FieldstrengthBr_m[index1] - 4. * FieldstrengthBr_m[index_1]
367 + FieldstrengthBr_m[index_2])
368 / (2. * hr_m));
369
370 BfieldZ =
371 (1.0 - leverz)
372 * ((FieldstrengthBz_m[index1 + 1] - 2. * FieldstrengthBz_m[index_1 + 1]
373 + FieldstrengthBz_m[index_2 + 1])
374 * (RR - indexr * hr_m) / (hr_m * hr_m)
375 - (3. * FieldstrengthBz_m[index1 + 1]
376 - 4. * FieldstrengthBr_m[index_1 + 1]
377 + FieldstrengthBz_m[index_2 + 1])
378 / (2. * hr_m))
379 + leverz
380 * ((FieldstrengthBz_m[index1] - 2. * FieldstrengthBz_m[index_1]
381 + FieldstrengthBz_m[index_2])
382 * (RR - indexr * hr_m) / (hr_m * hr_m)
383 - (3. * FieldstrengthBz_m[index1] - 4. * FieldstrengthBr_m[index_1]
384 + FieldstrengthBz_m[index_2])
385 / (2. * hr_m));
386 }
387 } else {
388 const int index3 = index2 + num_gridpz_m;
389 BfieldR =
390 (1.0 - leverz)
391 * ((FieldstrengthBr_m[index1 + 1] - 2. * FieldstrengthBr_m[index2 + 1]
392 + FieldstrengthBr_m[index3 + 1])
393 * (RR - indexr * hr_m) / (hr_m * hr_m)
394 - (3. * FieldstrengthBr_m[index1 + 1] - 4. * FieldstrengthBr_m[index2 + 1]
395 + FieldstrengthBr_m[index3 + 1])
396 / (2. * hr_m))
397 + leverz
398 * ((FieldstrengthBr_m[index1] - 2. * FieldstrengthBr_m[index2]
399 + FieldstrengthBr_m[index3])
400 * (RR - indexr * hr_m) / (hr_m * hr_m)
401 - (3. * FieldstrengthBr_m[index1] - 4. * FieldstrengthBr_m[index2]
402 + FieldstrengthBr_m[index3])
403 / (2. * hr_m));
404
405 BfieldZ =
406 (1.0 - leverz)
407 * ((FieldstrengthBz_m[index1 + 1] - 2. * FieldstrengthBz_m[index2 + 1]
408 + FieldstrengthBz_m[index3 + 1])
409 * (RR - indexr * hr_m) / (hr_m * hr_m)
410 - (3. * FieldstrengthBz_m[index1 + 1] - 4. * FieldstrengthBr_m[index2 + 1]
411 + FieldstrengthBz_m[index3 + 1])
412 / (2. * hr_m))
413 + leverz
414 * ((FieldstrengthBz_m[index1] - 2. * FieldstrengthBz_m[index2]
415 + FieldstrengthBz_m[index3])
416 * (RR - indexr * hr_m) / (hr_m * hr_m)
417 - (3. * FieldstrengthBz_m[index1] - 4. * FieldstrengthBr_m[index2]
418 + FieldstrengthBz_m[index3])
419 / (2. * hr_m));
420 }
421 }
422
423 if (RR > 1.e-12) {
424 B(0) += BfieldR * R(0) / RR;
425 B(1) += BfieldR * R(1) / RR;
426 }
427 B(2) += BfieldZ;
428 return false;
429}
430
431void FM2DMagnetoStatic::getFieldDimensions(double& zBegin, double& zEnd) const {
432 zBegin = zbegin_m;
433 zEnd = zend_m;
434}
435
437 double& /*xIni*/, double& /*xFinal*/, double& /*yIni*/, double& /*yFinal*/, double& /*zIni*/,
438 double& /*zFinal*/) const {
439}
440
442 if (swap_m)
443 swap_m = false;
444 else
445 swap_m = true;
446}
447
448void FM2DMagnetoStatic::getInfo(Inform* msg) {
449 (*msg) << Filename_m << " (2D magnetostatic); zini= " << zbegin_m << " m; zfinal= " << zend_m
450 << " m;" << endl;
451}
452
454 return 0.0;
455}
456
457void FM2DMagnetoStatic::setFrequency(double /*freq*/) {
458 ;
459}
ippl::Vector< T, Dim > Vector_t
@ T2DMagnetoStatic
Definition Fieldmap.h:29
DiffDirection
Definition Fieldmap.h:55
@ DZ
Definition Fieldmap.h:55
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
virtual double getFrequency() const
virtual void getInfo(Inform *msg)
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
virtual void setFrequency(double freq)
FM2DMagnetoStatic(std::string aFilename)
virtual bool getFieldstrength(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B) const
virtual bool getFieldDerivative(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B, const DiffDirection &dir) const