OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
FM3DMagnetoStaticExtended.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 <algorithm>
8#include <cmath>
9#include <fstream>
10#include <ios>
11
12extern Inform* gmsg;
13
15 : Fieldmap(aFilename),
16 FieldstrengthBx_m(nullptr),
17 FieldstrengthBy_m(nullptr),
18 FieldstrengthBz_m(nullptr) {
19 std::ifstream file;
20 std::string tmpString;
21 double tmpDouble;
22
24
25 // open field map, parse it and disable element on error
26 file.open(Filename_m.c_str());
27 if (file.good()) {
28 bool parsing_passed = true;
29 try {
30 parsing_passed = interpretLine<std::string>(file, tmpString);
31 } catch (GeneralClassicException& e) {
32 parsing_passed = interpretLine<std::string, std::string>(file, tmpString, tmpString);
33
34 tmpString = Util::toUpper(tmpString);
35 if (tmpString != "TRUE" && tmpString != "FALSE")
37 "FM3DMagnetoStaticExtended::FM3DMagnetoStaticExtended",
38 "The second string on the first line of 3D field "
39 "maps has to be either TRUE or FALSE");
40
41 normalize_m = (tmpString == "TRUE");
42 }
43 parsing_passed =
44 (parsing_passed
46 parsing_passed =
47 (parsing_passed
49 parsing_passed =
50 (parsing_passed
52
53 for (unsigned long i = 0; (i < (num_gridpz_m + 1) * (num_gridpx_m + 1)) && parsing_passed;
54 ++i) {
55 parsing_passed = parsing_passed && interpretLine<double>(file, tmpDouble);
56 }
57
58 parsing_passed = parsing_passed && interpreteEOF(file);
59
60 file.close();
61 lines_read_m = 0;
62
63 if (!parsing_passed) {
65 zend_m = zbegin_m - 1e-3;
67 "FM3DMagnetoStaticExtended::FM3DMagnetoStaticExtended(std::string)",
68 "Format of fieldmap '" + Filename_m + "' didn't pass basic test");
69 } else {
70 // conversion from cm to m
73 yend_m = std::max(std::abs(ybegin_m), yend_m) * Units::cm2m;
74 ybegin_m = 0.0;
78
82
83 // num spacings -> num grid points
87 }
88 } else {
90 "FM3DMagnetoStaticExtended::FM3DMagnetoStaticExtended(std::string)",
91 "Couldn't read fieldmap '" + Filename_m + "'");
92 }
93}
94
98
100 if (FieldstrengthBz_m == nullptr) {
101 // declare variables and allocate memory
102 std::ifstream in;
103 std::string tmpString;
104 const size_t totalSize = num_gridpx_m * num_gridpy_m * num_gridpz_m;
105 FieldstrengthBx_m = new double[totalSize];
106 FieldstrengthBy_m = new double[totalSize];
107 FieldstrengthBz_m = new double[totalSize];
108
109 // read in and parse field map
110 in.open(Filename_m.c_str());
111 getLine(in, tmpString);
112 getLine(in, tmpString);
113 getLine(in, tmpString);
114 getLine(in, tmpString);
115
116 for (unsigned int i = 0; i < num_gridpx_m; i++) {
117 for (unsigned int k = 0; k < num_gridpz_m; k++) {
118 unsigned long index = getIndex(i, 0, k);
120 }
121 }
122 in.close();
123
124 std::fill(FieldstrengthBx_m, FieldstrengthBx_m + totalSize, 0.0);
125 std::fill(FieldstrengthBz_m, FieldstrengthBz_m + totalSize, 0.0);
126
127 if (normalize_m) {
128 double Bymax = 0.0;
129
130 // find maximum field
131 unsigned int centerX = static_cast<unsigned int>(std::round(-xbegin_m / hx_m));
132 for (unsigned int k = 0; k < num_gridpz_m; ++k) {
133 double By = FieldstrengthBy_m[getIndex(centerX, 0, k)];
134 if (std::abs(By) > std::abs(Bymax)) {
135 Bymax = By;
136 }
137 }
138
139 // normalize field
140 for (unsigned int i = 0; i < num_gridpx_m; i++) {
141 for (unsigned int k = 0; k < num_gridpz_m; k++) {
142 unsigned long index = getIndex(i, 0, k);
143 FieldstrengthBy_m[index] /= Bymax;
144 }
145 }
146 }
147
149 for (unsigned int j = 1; j < num_gridpy_m; j++) {
150 integrateBx(j);
151 integrateBz(j);
152 integrateBy(j);
153
157 }
158
159 *ippl::Info << level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info")
160 << endl;
161 }
162}
163
165 if (j == 1) {
166 {
167 unsigned int i = 0;
168 // treat cells at i = 0, j = 1, k = 0:num_gridpz_m - 1;
169 for (unsigned int k = 0; k < num_gridpz_m; k++) {
170 unsigned long index = getIndex(i, j, k);
171 FieldstrengthBx_m[index] = 0.5 * hy_m / hx_m
172 * (-3 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
173 + 4 * FieldstrengthBy_m[getIndex(i + 1, j - 1, k)]
174 - FieldstrengthBy_m[getIndex(i + 2, j - 1, k)]);
175 }
176 }
177 // treat cells at i = 1:num_gridpx_m - 2, j = 1, k = 0:num_gridpz_m - 1;
178 for (unsigned int i = 1; i < num_gridpx_m - 1; i++) {
179 for (unsigned int k = 0; k < num_gridpz_m; k++) {
180 unsigned long index = getIndex(i, j, k);
181 FieldstrengthBx_m[index] = 0.5 * hy_m / hx_m
182 * (FieldstrengthBy_m[getIndex(i + 1, j - 1, k)]
183 - FieldstrengthBy_m[getIndex(i - 1, j - 1, k)]);
184 }
185 }
186 {
187 unsigned int i = num_gridpx_m - 1;
188 // treat cells at i = num_gridpx_m - 1, j = 1, k = 0:num_gridpz_m - 1;
189 for (unsigned int k = 0; k < num_gridpz_m; k++) {
190 unsigned long index = getIndex(i, j, k);
191 FieldstrengthBx_m[index] = 0.5 * hy_m / hx_m
192 * (FieldstrengthBy_m[getIndex(i - 2, j - 1, k)]
193 - 4 * FieldstrengthBy_m[getIndex(i - 1, j - 1, k)]
194 + 3 * FieldstrengthBy_m[getIndex(i, j - 1, k)]);
195 }
196 }
197
198 } else {
199 { // treat cells at i = 0, j > 1, k = 0:num_gridpz_m - 1;
200 unsigned int i = 0;
201 for (unsigned int k = 0; k < num_gridpz_m; k++) {
202 unsigned long index = getIndex(i, j, k);
203 FieldstrengthBx_m[index] =
204 (4 * FieldstrengthBx_m[getIndex(i, j - 1, k)]
205 - FieldstrengthBx_m[getIndex(i, j - 2, k)]
206 + hy_m / hx_m
207 * (-3 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
208 + 4 * FieldstrengthBy_m[getIndex(i + 1, j - 1, k)]
209 - FieldstrengthBy_m[getIndex(i + 2, j - 1, k)]))
210 / 3;
211 }
212 }
213 // treat cells at i = 1:num_gridpx_m - 2, j > 1, k = 0:num_gridpz_m - 1;
214 for (unsigned int i = 1; i < num_gridpx_m - 1; i++) {
215 for (unsigned int k = 0; k < num_gridpz_m; k++) {
216 unsigned long index = getIndex(i, j, k);
217 FieldstrengthBx_m[index] =
218 (4 * FieldstrengthBx_m[getIndex(i, j - 1, k)]
219 - FieldstrengthBx_m[getIndex(i, j - 2, k)]
220 + hy_m / hx_m
221 * (FieldstrengthBy_m[getIndex(i + 1, j - 1, k)]
222 - FieldstrengthBy_m[getIndex(i - 1, j - 1, k)]))
223 / 3;
224 }
225 }
226 { // treat cells at i = num_gridpx_m - 1, j > 1, k = 0:num_gridpz_m - 1;
227 unsigned int i = num_gridpx_m - 1;
228 for (unsigned int k = 1; k < num_gridpz_m - 1; k++) {
229 unsigned long index = getIndex(i, j, k);
230 FieldstrengthBx_m[index] =
231 (4 * FieldstrengthBx_m[getIndex(i, j - 1, k)]
232 - FieldstrengthBx_m[getIndex(i, j - 2, k)]
233 + hy_m / hx_m
234 * (FieldstrengthBy_m[getIndex(i - 2, j - 1, k)]
235 - 4 * FieldstrengthBy_m[getIndex(i - 1, j - 1, k)]
236 + 3 * FieldstrengthBy_m[getIndex(i, j - 1, k)]))
237 / 3;
238 }
239 }
240 }
241}
242
244 if (j == 1) {
245 // treat cells at i = 0:num_gridpx_m - 1, j = 1, k = 1:num_gridpz_m - 2;
246 for (unsigned int i = 0; i < num_gridpx_m; i++) {
247 for (unsigned int k = 1; k < num_gridpz_m - 1; k++) {
248 unsigned long index = getIndex(i, j, k);
249 FieldstrengthBz_m[index] = 0.5 * hy_m / hz_m
250 * (FieldstrengthBy_m[getIndex(i, j - 1, k + 1)]
251 - FieldstrengthBy_m[getIndex(i, j - 1, k - 1)]);
252 }
253
254 { // treat cells at i = 0:num_gridpx_m - 1, j = 1, k = 0;
255 unsigned int k = 0;
256 unsigned int index = getIndex(i, j, k);
257 FieldstrengthBz_m[index] = 0.5 * hy_m / hz_m
258 * (-3 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
259 + 4 * FieldstrengthBy_m[getIndex(i, j - 1, k + 1)]
260 - FieldstrengthBy_m[getIndex(i, j - 1, k + 2)]);
261 }
262 { // treat cells at i = 0:num_gridpx_m - 1, j = 1, k = num_gridpz_m - 1;
263 unsigned int k = num_gridpz_m - 1;
264 unsigned int index = getIndex(i, j, k);
265 FieldstrengthBz_m[index] = 0.5 * hy_m / hz_m
266 * (FieldstrengthBy_m[getIndex(i, j - 1, k - 2)]
267 - 4 * FieldstrengthBy_m[getIndex(i, j - 1, k - 1)]
268 + 3 * FieldstrengthBy_m[getIndex(i, j - 1, k)]);
269 }
270 }
271 } else {
272 // treat cells at i = 0:num_gridpx_m - 1, j > 1, k = 1:num_gridpz_m - 2;
273 for (unsigned int i = 0; i < num_gridpx_m; i++) {
274 for (unsigned int k = 1; k < num_gridpz_m - 1; k++) {
275 unsigned long index = getIndex(i, j, k);
276 FieldstrengthBz_m[index] =
277 (4 * FieldstrengthBz_m[getIndex(i, j - 1, k)]
278 - FieldstrengthBz_m[getIndex(i, j - 2, k)]
279 + hy_m / hz_m
280 * (FieldstrengthBy_m[getIndex(i, j - 1, k + 1)]
281 - FieldstrengthBy_m[getIndex(i, j - 1, k - 1)]))
282 / 3;
283 }
284
285 { // treat cells at i = 0:num_gridpx_m - 1, j > 1, k = 0;
286 unsigned int k = 0;
287 unsigned int index = getIndex(i, j, k);
288 FieldstrengthBz_m[index] =
289 (4 * FieldstrengthBz_m[getIndex(i, j - 1, k)]
290 - FieldstrengthBz_m[getIndex(i, j - 2, k)]
291 + hy_m / hz_m
292 * (-3 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
293 + 4 * FieldstrengthBy_m[getIndex(i, j - 1, k + 1)]
294 - FieldstrengthBy_m[getIndex(i, j - 1, k + 2)]))
295 / 3;
296 }
297 { // treat cells at i = 0:num_gridpx_m - 1, j > 1, k = num_gridpz_m - 1;
298 unsigned int k = num_gridpz_m - 1;
299 unsigned int index = getIndex(i, j, k);
300 FieldstrengthBz_m[index] =
301 (4 * FieldstrengthBz_m[getIndex(i, j - 1, k)]
302 - FieldstrengthBz_m[getIndex(i, j - 2, k)]
303 + hy_m / hz_m
304 * (FieldstrengthBy_m[getIndex(i, j - 1, k - 2)]
305 - 4 * FieldstrengthBy_m[getIndex(i, j - 1, k - 1)]
306 + 3 * FieldstrengthBy_m[getIndex(i, j - 1, k)]))
307 / 3;
308 }
309 }
310 }
311}
312
314 if (j == 1) {
315 for (unsigned int i = 1; i < num_gridpx_m - 1; i++) {
316 { // treat cells at i = 1:num_gridpx_m - 2, j = 1, k = 0
317 unsigned int k = 0;
318 unsigned long index = getIndex(i, j, k);
319 FieldstrengthBy_m[index] =
320 (FieldstrengthBy_m[getIndex(i, j - 1, k)]
321 + hy_m
322 * ((FieldstrengthBx_m[getIndex(i + 1, j, k)]
323 - FieldstrengthBx_m[getIndex(i - 1, j, k)])
324 / hx_m
325 - (-3 * FieldstrengthBz_m[getIndex(i, j, k)]
326 + 4 * FieldstrengthBz_m[getIndex(i, j, k + 1)]
327 - FieldstrengthBz_m[getIndex(i, j, k + 2)])
328 / hz_m));
329 }
330 // treat cells at i = 1:num_gridpx_m - 2, j = 1, k = 1:num_gridpz_m - 2
331 for (unsigned int k = 1; k < num_gridpz_m - 1; k++) {
332 unsigned long index = getIndex(i, j, k);
333 FieldstrengthBy_m[index] =
334 (FieldstrengthBy_m[getIndex(i, j - 1, k)]
335 + hy_m
336 * ((FieldstrengthBx_m[getIndex(i + 1, j, k)]
337 - FieldstrengthBx_m[getIndex(i - 1, j, k)])
338 / hx_m
339 - (FieldstrengthBz_m[getIndex(i, j, k + 1)]
340 - FieldstrengthBz_m[getIndex(i, j, k - 1)])
341 / hz_m));
342 }
343 { // treat cells at i = 1:num_gridpx_m - 2, j = 1, k = num_gridpz_m - 1
344 unsigned int k = num_gridpz_m - 1;
345 unsigned long index = getIndex(i, j, k);
346 FieldstrengthBy_m[index] =
347 (FieldstrengthBy_m[getIndex(i, j - 1, k)]
348 + hy_m
349 * ((FieldstrengthBx_m[getIndex(i + 1, j, k)]
350 - FieldstrengthBx_m[getIndex(i - 1, j, k)])
351 / hx_m
352 - (FieldstrengthBz_m[getIndex(i, j, k - 2)]
353 - 4 * FieldstrengthBz_m[getIndex(i, j, k - 1)]
354 + 3 * FieldstrengthBz_m[getIndex(i, j, k)])
355 / hz_m));
356 }
357 }
358 {
359 unsigned int i = 0;
360 { // treat cells at i = 0, j = 1, k = 0
361 unsigned int k = 0;
362 unsigned long index = getIndex(i, j, k);
363 FieldstrengthBy_m[index] =
364 (FieldstrengthBy_m[getIndex(i, j - 1, k)]
365 + hy_m
366 * ((-3 * FieldstrengthBx_m[getIndex(i, j, k)]
367 + 4 * FieldstrengthBx_m[getIndex(i + 1, j, k)]
368 - FieldstrengthBx_m[getIndex(i + 2, j, k)])
369 / hx_m
370 - (-3 * FieldstrengthBz_m[getIndex(i, j, k)]
371 + 4 * FieldstrengthBz_m[getIndex(i, j, k + 1)]
372 - FieldstrengthBz_m[getIndex(i, j, k + 2)])
373 / hz_m));
374 }
375 // treat cells at i = 0, j = 1, k = 1:num_gridpz_m - 2
376 for (unsigned int k = 1; k < num_gridpz_m - 1; k++) {
377 unsigned long index = getIndex(i, j, k);
378 FieldstrengthBy_m[index] =
379 (FieldstrengthBy_m[getIndex(i, j - 1, k)]
380 + hy_m
381 * ((-3 * FieldstrengthBx_m[getIndex(i, j, k)]
382 + 4 * FieldstrengthBx_m[getIndex(i + 1, j, k)]
383 - FieldstrengthBx_m[getIndex(i + 2, j, k)])
384 / hx_m
385 - (FieldstrengthBz_m[getIndex(i, j, k + 1)]
386 - FieldstrengthBz_m[getIndex(i, j, k - 1)])
387 / hz_m));
388 }
389 { // treat cells at i = 0, j = 1, k = num_gridpz_m - 1
390 unsigned int k = num_gridpz_m - 1;
391 unsigned long index = getIndex(i, j, k);
392 FieldstrengthBy_m[index] =
393 (FieldstrengthBy_m[getIndex(i, j - 1, k)]
394 + hy_m
395 * ((-3 * FieldstrengthBx_m[getIndex(i, j, k)]
396 + 4 * FieldstrengthBx_m[getIndex(i + 1, j, k)]
397 - FieldstrengthBx_m[getIndex(i + 2, j, k)])
398 / hx_m
399 - (FieldstrengthBz_m[getIndex(i, j, k - 2)]
400 - 4 * FieldstrengthBz_m[getIndex(i, j, k - 1)]
401 + 3 * FieldstrengthBz_m[getIndex(i, j, k)])
402 / hz_m));
403 }
404 }
405 {
406 unsigned int i = num_gridpx_m - 1;
407 { // treat cells at i = 0, j = 1, k = 0
408 unsigned int k = 0;
409 unsigned long index = getIndex(i, j, k);
410 FieldstrengthBy_m[index] =
411 (FieldstrengthBy_m[getIndex(i, j - 1, k)]
412 + hy_m
413 * ((FieldstrengthBx_m[getIndex(i - 2, j, k)]
414 - 4 * FieldstrengthBx_m[getIndex(i - 1, j, k)]
415 + 3 * FieldstrengthBx_m[getIndex(i, j, k)])
416 / hx_m
417 - (-3 * FieldstrengthBz_m[getIndex(i, j, k)]
418 + 4 * FieldstrengthBz_m[getIndex(i, j, k + 1)]
419 - FieldstrengthBz_m[getIndex(i, j, k + 2)])
420 / hz_m));
421 }
422 // treat cells at i = 0, j = 1, k = 1:num_gridpz_m - 2
423 for (unsigned int k = 1; k < num_gridpz_m - 1; k++) {
424 unsigned long index = getIndex(i, j, k);
425 FieldstrengthBy_m[index] =
426 (FieldstrengthBy_m[getIndex(i, j - 1, k)]
427 + hy_m
428 * ((FieldstrengthBx_m[getIndex(i - 2, j, k)]
429 - 4 * FieldstrengthBx_m[getIndex(i - 1, j, k)]
430 + 3 * FieldstrengthBx_m[getIndex(i, j, k)])
431 / hx_m
432 - (FieldstrengthBz_m[getIndex(i, j, k + 1)]
433 - FieldstrengthBz_m[getIndex(i, j, k - 1)])
434 / hz_m));
435 }
436 { // treat cells at i = 0, j = 1, k = num_gridpz_m - 1
437 unsigned int k = num_gridpz_m - 1;
438 unsigned long index = getIndex(i, j, k);
439 FieldstrengthBy_m[index] =
440 (FieldstrengthBy_m[getIndex(i, j - 1, k)]
441 + hy_m
442 * ((FieldstrengthBx_m[getIndex(i - 2, j, k)]
443 - 4 * FieldstrengthBx_m[getIndex(i - 1, j, k)]
444 + 3 * FieldstrengthBx_m[getIndex(i, j, k)])
445 / hx_m
446 - (FieldstrengthBz_m[getIndex(i, j, k - 2)]
447 - 4 * FieldstrengthBz_m[getIndex(i, j, k - 1)]
448 + 3 * FieldstrengthBz_m[getIndex(i, j, k)])
449 / hz_m));
450 }
451 }
452 } else {
453 for (unsigned int i = 1; i < num_gridpx_m - 1; i++) {
454 { // treat cells at i = 1:num_gridpx_m - 2, j > 1, k = 0
455 unsigned int k = 0;
456 unsigned long index = getIndex(i, j, k);
457 FieldstrengthBy_m[index] =
458 (4 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
459 - FieldstrengthBy_m[getIndex(i, j - 2, k)]
460 + hy_m
461 * ((FieldstrengthBx_m[getIndex(i + 1, j, k)]
462 - FieldstrengthBx_m[getIndex(i - 1, j, k)])
463 / hx_m
464 - (-3 * FieldstrengthBz_m[getIndex(i, j, k)]
465 + 4 * FieldstrengthBz_m[getIndex(i, j, k + 1)]
466 - FieldstrengthBz_m[getIndex(i, j, k + 2)])
467 / hz_m))
468 / 3;
469 }
470 // treat cells at i = 1:num_gridpx_m - 2, j > 1, k = 1:num_gridpz_m - 2
471 for (unsigned int k = 1; k < num_gridpz_m - 1; k++) {
472 unsigned long index = getIndex(i, j, k);
473 FieldstrengthBy_m[index] = (4 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
474 - FieldstrengthBy_m[getIndex(i, j - 2, k)]
475 + hy_m
476 * ((FieldstrengthBx_m[getIndex(i + 1, j, k)]
477 - FieldstrengthBx_m[getIndex(i - 1, j, k)])
478 / hx_m
479 - (FieldstrengthBz_m[getIndex(i, j, k + 1)]
480 - FieldstrengthBz_m[getIndex(i, j, k - 1)])
481 / hz_m))
482 / 3;
483 }
484 { // treat cells at i = 1:num_gridpx_m - 2, j > 1, k = num_gridpz_m - 1
485 unsigned int k = num_gridpz_m - 1;
486 unsigned long index = getIndex(i, j, k);
487 FieldstrengthBy_m[index] =
488 (4 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
489 - FieldstrengthBy_m[getIndex(i, j - 2, k)]
490 + hy_m
491 * ((FieldstrengthBx_m[getIndex(i + 1, j, k)]
492 - FieldstrengthBx_m[getIndex(i - 1, j, k)])
493 / hx_m
494 - (FieldstrengthBz_m[getIndex(i, j, k - 2)]
495 - 4 * FieldstrengthBz_m[getIndex(i, j, k - 1)]
496 + 3 * FieldstrengthBz_m[getIndex(i, j, k)])
497 / hz_m))
498 / 3;
499 }
500 }
501 {
502 unsigned int i = 0;
503 { // treat cells at i = 0, j > 1, k = 0
504 unsigned int k = 0;
505 unsigned long index = getIndex(i, j, k);
506 FieldstrengthBy_m[index] =
507 (4 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
508 - FieldstrengthBy_m[getIndex(i, j - 2, k)]
509 + hy_m
510 * ((-3 * FieldstrengthBx_m[getIndex(i, j, k)]
511 + 4 * FieldstrengthBx_m[getIndex(i + 1, j, k)]
512 - FieldstrengthBx_m[getIndex(i + 2, j, k)])
513 / hx_m
514 - (-3 * FieldstrengthBz_m[getIndex(i, j, k)]
515 + 4 * FieldstrengthBz_m[getIndex(i, j, k + 1)]
516 - FieldstrengthBz_m[getIndex(i, j, k + 2)])
517 / hz_m))
518 / 3;
519 }
520 // treat cells at i = 0, j > 1, k = 1:num_gridpz_m - 2
521 for (unsigned int k = 1; k < num_gridpz_m - 1; k++) {
522 unsigned long index = getIndex(i, j, k);
523 FieldstrengthBy_m[index] = (4 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
524 - FieldstrengthBy_m[getIndex(i, j - 2, k)]
525 + hy_m
526 * ((-3 * FieldstrengthBx_m[getIndex(i, j, k)]
527 + 4 * FieldstrengthBx_m[getIndex(i + 1, j, k)]
528 - FieldstrengthBx_m[getIndex(i + 2, j, k)])
529 / hx_m
530 - (FieldstrengthBz_m[getIndex(i, j, k + 1)]
531 - FieldstrengthBz_m[getIndex(i, j, k - 1)])
532 / hz_m))
533 / 3;
534 }
535 { // treat cells at i = 0, j > 1, k = num_gridpz_m - 1
536 unsigned int k = num_gridpz_m - 1;
537 unsigned long index = getIndex(i, j, k);
538 FieldstrengthBy_m[index] =
539 (4 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
540 - FieldstrengthBy_m[getIndex(i, j - 2, k)]
541 + hy_m
542 * ((-3 * FieldstrengthBx_m[getIndex(i, j, k)]
543 + 4 * FieldstrengthBx_m[getIndex(i + 1, j, k)]
544 - FieldstrengthBx_m[getIndex(i + 2, j, k)])
545 / hx_m
546 - (FieldstrengthBz_m[getIndex(i, j, k - 2)]
547 - 4 * FieldstrengthBz_m[getIndex(i, j, k - 1)]
548 + 3 * FieldstrengthBz_m[getIndex(i, j, k)])
549 / hz_m))
550 / 3;
551 }
552 }
553 {
554 unsigned int i = num_gridpx_m - 1;
555 { // treat cells at i = 0, j > 1, k = 0
556 unsigned int k = 0;
557 unsigned long index = getIndex(i, j, k);
558 FieldstrengthBy_m[index] =
559 (4 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
560 - FieldstrengthBy_m[getIndex(i, j - 2, k)]
561 + hy_m
562 * ((FieldstrengthBx_m[getIndex(i - 2, j, k)]
563 - 4 * FieldstrengthBx_m[getIndex(i - 1, j, k)]
564 + 3 * FieldstrengthBx_m[getIndex(i, j, k)])
565 / hx_m
566 - (-3 * FieldstrengthBz_m[getIndex(i, j, k)]
567 + 4 * FieldstrengthBz_m[getIndex(i, j, k + 1)]
568 - FieldstrengthBz_m[getIndex(i, j, k + 2)])
569 / hz_m))
570 / 3;
571 }
572 // treat cells at i = 0, j > 1, k = 1:num_gridpz_m - 2
573 for (unsigned int k = 1; k < num_gridpz_m - 1; k++) {
574 unsigned long index = getIndex(i, j, k);
575 FieldstrengthBy_m[index] = (4 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
576 - FieldstrengthBy_m[getIndex(i, j - 2, k)]
577 + hy_m
578 * ((FieldstrengthBx_m[getIndex(i - 2, j, k)]
579 - 4 * FieldstrengthBx_m[getIndex(i - 1, j, k)]
580 + 3 * FieldstrengthBx_m[getIndex(i, j, k)])
581 / hx_m
582 - (FieldstrengthBz_m[getIndex(i, j, k + 1)]
583 - FieldstrengthBz_m[getIndex(i, j, k - 1)])
584 / hz_m))
585 / 3;
586 }
587 { // treat cells at i = 0, j > 1, k = num_gridpz_m - 1
588 unsigned int k = num_gridpz_m - 1;
589 unsigned long index = getIndex(i, j, k);
590 FieldstrengthBy_m[index] =
591 (4 * FieldstrengthBy_m[getIndex(i, j - 1, k)]
592 - FieldstrengthBy_m[getIndex(i, j - 2, k)]
593 + hy_m
594 * ((FieldstrengthBx_m[getIndex(i - 2, j, k)]
595 - 4 * FieldstrengthBx_m[getIndex(i - 1, j, k)]
596 + 3 * FieldstrengthBx_m[getIndex(i, j, k)])
597 / hx_m
598 - (FieldstrengthBz_m[getIndex(i, j, k - 2)]
599 - 4 * FieldstrengthBz_m[getIndex(i, j, k - 1)]
600 + 3 * FieldstrengthBz_m[getIndex(i, j, k)])
601 / hz_m))
602 / 3;
603 }
604 }
605 }
606}
607
608void FM3DMagnetoStaticExtended::smoothData(double* data, unsigned j) {
609 const double offWeight = 0.1, sumWeightInv = 1.0 / (1.0 + 4 * (1 + offWeight) * offWeight);
610 double* tmp = new double[num_gridpx_m * num_gridpy_m * num_gridpz_m];
611
612 for (unsigned int i = 1; i < num_gridpx_m - 1; ++i) {
613 for (unsigned int k = 1; k < num_gridpz_m - 1; ++k) {
614 double sum = 0.0;
615 for (int i2 = -1; i2 < 2; ++i2) {
616 for (int k2 = -1; k2 < 2; ++k2) {
617 double weight = std::pow(offWeight, std::abs(i2) + std::abs(k2));
618 sum += weight * data[getIndex(i + i2, j, k + k2)];
619 }
620 }
621
622 tmp[getIndex(i, j, k)] = sum * sumWeightInv;
623 }
624 }
625 {
626 const double sumWeightInv = 1.0 / (1.0 + (3 + 2 * offWeight) * offWeight);
627 unsigned int i = 0;
628 for (unsigned int k = 1; k < num_gridpz_m - 1; ++k) {
629 double sum = 0.0;
630 for (int i2 = 0; i2 < 2; ++i2) {
631 for (int k2 = -1; k2 < 2; ++k2) {
632 double weight = std::pow(offWeight, std::abs(i2) + std::abs(k2));
633 sum += weight * data[getIndex(i + i2, j, k + k2)];
634 }
635 }
636
637 tmp[getIndex(i, j, k)] = sum * sumWeightInv;
638 }
639 }
640 {
641 const double sumWeightInv = 1.0 / (1.0 + (3 + 2 * offWeight) * offWeight);
642 unsigned int i = num_gridpx_m - 1;
643 for (unsigned int k = 1; k < num_gridpz_m - 1; ++k) {
644 double sum = 0.0;
645 for (int i2 = -1; i2 < 1; ++i2) {
646 for (int k2 = -1; k2 < 2; ++k2) {
647 double weight = std::pow(offWeight, std::abs(i2) + std::abs(k2));
648 sum += weight * data[getIndex(i + i2, j, k + k2)];
649 }
650 }
651
652 tmp[getIndex(i, j, k)] = sum * sumWeightInv;
653 }
654 }
655 {
656 const double sumWeightInv = 1.0 / (1.0 + (3 + 2 * offWeight) * offWeight);
657 unsigned int k = 0;
658 for (unsigned int i = 1; i < num_gridpx_m - 1; ++i) {
659 double sum = 0.0;
660 for (int i2 = -1; i2 < 2; ++i2) {
661 for (int k2 = 0; k2 < 2; ++k2) {
662 double weight = std::pow(offWeight, std::abs(i2) + std::abs(k2));
663 sum += weight * data[getIndex(i + i2, j, k + k2)];
664 }
665 }
666
667 tmp[getIndex(i, j, k)] = sum * sumWeightInv;
668 }
669 }
670 {
671 const double sumWeightInv = 1.0 / (1.0 + (3 + 2 * offWeight) * offWeight);
672 unsigned int k = num_gridpz_m - 1;
673 for (unsigned int i = 1; i < num_gridpx_m - 1; ++i) {
674 double sum = 0.0;
675 for (int i2 = -1; i2 < 2; ++i2) {
676 for (int k2 = -1; k2 < 1; ++k2) {
677 double weight = std::pow(offWeight, std::abs(i2) + std::abs(k2));
678 sum += weight * data[getIndex(i + i2, j, k + k2)];
679 }
680 }
681
682 tmp[getIndex(i, j, k)] = sum * sumWeightInv;
683 }
684 }
685 {
686 unsigned long index = getIndex(0, j, 0);
687 tmp[index] = data[index];
688 }
689 {
690 unsigned long index = getIndex(num_gridpx_m - 1, j, 0);
691 tmp[index] = data[index];
692 }
693 {
694 unsigned long index = getIndex(num_gridpx_m - 1, j, num_gridpz_m - 1);
695 tmp[index] = data[index];
696 }
697 {
698 unsigned long index = getIndex(0, j, num_gridpz_m - 1);
699 tmp[index] = data[index];
700 }
701
702 for (unsigned int i = 0; i < num_gridpx_m; ++i) {
703 for (unsigned int k = 0; k < num_gridpz_m; ++k) {
704 unsigned long index = getIndex(i, j, k);
705 data[index] = tmp[index];
706 }
707 }
708
709 delete[] tmp;
710}
711
712void FM3DMagnetoStaticExtended::saveField(const std::string& fname, unsigned int j) const {
713 std::ofstream out(fname);
714 out.precision(6);
715 double x = xbegin_m, y = j * hy_m;
716 for (unsigned int i = 0; i < num_gridpx_m; ++i, x += hx_m) {
717 double z = zbegin_m;
718 for (unsigned int k = 0; k < num_gridpz_m; ++k, z += hz_m) {
719 unsigned long index = getIndex(i, j, k);
720 out << std::setw(14) << x << std::setw(14) << y << std::setw(14) << z << std::setw(14)
721 << FieldstrengthBx_m[index] << std::setw(14) << FieldstrengthBy_m[index]
722 << std::setw(14) << FieldstrengthBz_m[index] << "\n";
723 }
724 }
725
726 out.close();
727}
728
730 if (FieldstrengthBz_m != nullptr) {
731 delete[] FieldstrengthBx_m;
732 delete[] FieldstrengthBy_m;
733 delete[] FieldstrengthBz_m;
734
735 FieldstrengthBx_m = nullptr;
736 FieldstrengthBy_m = nullptr;
737 FieldstrengthBz_m = nullptr;
738
739 *ippl::Info << level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << endl;
740 }
741}
742
744 const Vector_t<double, 3>& X) const {
745 IndexTriplet idx = getIndex(X);
746 Vector_t<double, 3> B(0.0);
747
748 B(0) =
757
758 B(1) =
767
768 B(2) =
777
778 return B;
779}
780
782 double* data, const IndexTriplet& idx, unsigned short corner) const {
783 unsigned short switchX = ((corner & HX) >> 2), switchY = ((corner & HY) >> 1),
784 switchZ = (corner & HZ);
785 double factorX = 0.5 + (1 - 2 * switchX) * (0.5 - idx.weight(0));
786 double factorY = 0.5 + (1 - 2 * switchY) * (0.5 - idx.weight(1));
787 double factorZ = 0.5 + (1 - 2 * switchZ) * (0.5 - idx.weight(2));
788
789 unsigned long i = idx.i + switchX, j = idx.j + switchY, k = idx.k + switchZ;
790
791 return factorX * factorY * factorZ * data[getIndex(i, j, k)];
792}
793
796 if (isInside(R)) {
798 suppB(0) *= copysign(1, R(1));
799 suppB(2) *= copysign(1, R(1));
800
801 B += suppB;
802 }
803
804 return false;
805}
806
809 const DiffDirection& /*dir*/) const {
810 return false;
811}
812
813void FM3DMagnetoStaticExtended::getFieldDimensions(double& zBegin, double& zEnd) const {
814 zBegin = zbegin_m;
815 zEnd = zend_m;
816}
817
819 double& xIni, double& xFinal, double& yIni, double& yFinal, double& zIni,
820 double& zFinal) const {
821 xIni = xbegin_m;
822 xFinal = xend_m;
823 yIni = -yend_m;
824 yFinal = yend_m;
825 zIni = zbegin_m;
826 zFinal = zend_m;
827}
828
831
833 (*msg) << Filename_m << " (3D magnetostatic, extended); zini= " << zbegin_m
834 << " m; zfinal= " << zend_m << " m;" << endl;
835}
836
838 return 0.0;
839}
840
842 ;
843}
Inform * gmsg
Definition changes.cpp:7
ippl::Vector< T, Dim > Vector_t
@ T2DMagnetoStatic
Definition Fieldmap.h:29
DiffDirection
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 bool getFieldDerivative(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B, const DiffDirection &dir) const
FM3DMagnetoStaticExtended(std::string aFilename)
void smoothData(double *data, unsigned j)
virtual bool getFieldstrength(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B) const
double getWeightedData(double *data, const IndexTriplet &idx, unsigned short corner) const
virtual bool isInside(const Vector_t< double, 3 > &r) const
virtual void setFrequency(double freq)
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
void saveField(const std::string &fname, unsigned int j) const
IndexTriplet getIndex(const Vector_t< double, 3 > &X) const
Vector_t< double, 3 > interpolateTrilinearly(const Vector_t< double, 3 > &X) const