OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
FM3DDynamic.cpp
Go to the documentation of this file.
2
4#include "Fields/Fieldmap.hpp"
5#include "Physics/Physics.h"
6#include "Physics/Units.h"
8#include "Utilities/Options.h"
9#include "Utilities/Util.h"
10
11#include <fstream>
12#include <ios>
13
14
15FM3DDynamic::FM3DDynamic(std::string aFilename):
16 Fieldmap(aFilename),
17 FieldstrengthEz_m(nullptr),
18 FieldstrengthEx_m(nullptr),
19 FieldstrengthEy_m(nullptr),
20 FieldstrengthBz_m(nullptr),
21 FieldstrengthBx_m(nullptr),
22 FieldstrengthBy_m(nullptr)
23{
24
25 std::string tmpString;
26 double tmpDouble;
27
29 std::ifstream file(Filename_m.c_str());
30
31 if(file.good()) {
32 bool parsing_passed = true;
33 try {
34 parsing_passed = interpretLine<std::string>(file, tmpString);
35 } catch (GeneralClassicException &e) {
36 parsing_passed = interpretLine<std::string, std::string>(file, tmpString, tmpString);
37
38 tmpString = Util::toUpper(tmpString);
39 if (tmpString != "TRUE" &&
40 tmpString != "FALSE")
41 throw GeneralClassicException("FM3DDynamic::FM3DDynamic",
42 "The second string on the first line of 3D field "
43 "maps has to be either TRUE or FALSE");
44
45 normalize_m = (tmpString == "TRUE");
46 }
47
48 parsing_passed = parsing_passed &&
49 interpretLine<double>(file, frequency_m);
50 parsing_passed = parsing_passed &&
51 interpretLine<double, double, unsigned int>(file, xbegin_m, xend_m, num_gridpx_m);
52 parsing_passed = parsing_passed &&
53 interpretLine<double, double, unsigned int>(file, ybegin_m, yend_m, num_gridpy_m);
54 parsing_passed = parsing_passed &&
55 interpretLine<double, double, unsigned int>(file, zbegin_m, zend_m, num_gridpz_m);
56
57 for(unsigned long i = 0; (i < (num_gridpz_m + 1) * (num_gridpx_m + 1) * (num_gridpy_m + 1)) && parsing_passed; ++ i) {
58 parsing_passed = parsing_passed &&
59 interpretLine<double>(file,
60 tmpDouble,
61 tmpDouble,
62 tmpDouble,
63 tmpDouble,
64 tmpDouble,
65 tmpDouble);
66 }
67
68 parsing_passed = parsing_passed &&
69 interpreteEOF(file);
70
71 file.close();
72
73 if(!parsing_passed) {
75 zend_m = zbegin_m - 1e-3;
76 throw GeneralClassicException("FM3DDynamic::FM3DDynamic",
77 "An error occured when reading the fieldmap '" + Filename_m + "'");
78 } else {
80
87
91
95
96 }
97 } else {
99 zbegin_m = 0.0;
100 zend_m = -1e-3;
101 }
102}
103
104
106 freeMap();
107}
108
110 if(FieldstrengthEz_m == nullptr) {
111
112 std::ifstream in(Filename_m.c_str());
113 std::string tmpString;
114 const size_t totalSize = num_gridpx_m * num_gridpy_m * num_gridpz_m;
115
116 getLine(in, tmpString);
117 getLine(in, tmpString);
118 getLine(in, tmpString);
119 getLine(in, tmpString);
120 getLine(in, tmpString);
121
122 FieldstrengthEz_m = new double[totalSize];
123 FieldstrengthEx_m = new double[totalSize];
124 FieldstrengthEy_m = new double[totalSize];
125 FieldstrengthBz_m = new double[totalSize];
126 FieldstrengthBx_m = new double[totalSize];
127 FieldstrengthBy_m = new double[totalSize];
128
129 long ii = 0;
130 for(unsigned int i = 0; i < num_gridpx_m; ++ i) {
131 for(unsigned int j = 0; j < num_gridpy_m; ++ j) {
132 for(unsigned int k = 0; k < num_gridpz_m; ++ k) {
133 interpretLine<double>(in,
140 ++ ii;
141 }
142 }
143 }
144 in.close();
145
146 const unsigned int deltaX = num_gridpy_m * num_gridpz_m;
147 const unsigned int deltaY = num_gridpz_m;
148 double Ezmax = 0.0;
149
150 if (normalize_m) {
151 int index_x = static_cast<int>(ceil(-xbegin_m / hx_m));
152 double lever_x = index_x * hx_m + xbegin_m;
153 if(lever_x > 0.5) {
154 -- index_x;
155 }
156
157 int index_y = static_cast<int>(ceil(-ybegin_m / hy_m));
158 double lever_y = index_y * hy_m + ybegin_m;
159 if(lever_y > 0.5) {
160 -- index_y;
161 }
162
163
164 ii = index_x * deltaX + index_y * deltaY;
165 for(unsigned int i = 0; i < num_gridpz_m; i++) {
166 if(std::abs(FieldstrengthEz_m[ii]) > Ezmax) {
167 Ezmax = std::abs(FieldstrengthEz_m[ii]);
168 }
169 ++ ii;
170 }
171 } else {
172 Ezmax = 1.0;
173 }
174
175 for(unsigned long i = 0; i < totalSize; ++ i) {
176
180 FieldstrengthBx_m[i] *= Physics::mu_0 / Ezmax;
181 FieldstrengthBy_m[i] *= Physics::mu_0 / Ezmax;
182 FieldstrengthBz_m[i] *= Physics::mu_0 / Ezmax;
183 }
184
185 INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info") << "\n"
186 << endl);
187
188 if (Options::ebDump) {
189 std::vector<Vector_t> ef(num_gridpz_m * num_gridpy_m * num_gridpx_m, 0.0);
190 std::vector<Vector_t> bf(ef);
191 unsigned long l = 0;
192 for (unsigned long k = 0; k < num_gridpz_m; ++ k) {
193 const unsigned long index_z = k;
194 for (unsigned long j = 0; j < num_gridpy_m; ++ j) {
195 const unsigned long index_y = j * deltaY;
196 for (unsigned long i = 0; i < num_gridpx_m; ++ i) {
197 const unsigned long index = i * deltaX + index_y + index_z;
198 ef[l] = Vector_t(FieldstrengthEx_m[index],
199 FieldstrengthEy_m[index],
200 FieldstrengthEz_m[index]);
201 bf[l] = Vector_t(FieldstrengthBx_m[index],
202 FieldstrengthBy_m[index],
203 FieldstrengthBz_m[index]);
204 ++ l;
205 }
206 }
207 }
211 std::make_pair(xbegin_m, xend_m),
212 std::make_pair(ybegin_m, yend_m),
213 std::make_pair(zbegin_m, zend_m),
214 ef,
215 bf);
216 }
217 }
218}
219
221 if(FieldstrengthEz_m != nullptr) {
222 delete[] FieldstrengthEz_m;
223 delete[] FieldstrengthEx_m;
224 delete[] FieldstrengthEy_m;
225 delete[] FieldstrengthBz_m;
226 delete[] FieldstrengthBx_m;
227 delete[] FieldstrengthBy_m;
228
229 FieldstrengthEz_m = nullptr;
230 FieldstrengthEx_m = nullptr;
231 FieldstrengthEy_m = nullptr;
232 FieldstrengthBz_m = nullptr;
233 FieldstrengthBx_m = nullptr;
234 FieldstrengthBy_m = nullptr;
235
236 INFOMSG(level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << "\n"
237 << endl);
238 }
239}
240
242 const unsigned int index_x = static_cast<int>(std::floor((R(0) - xbegin_m) / hx_m));
243 const double lever_x = (R(0) - xbegin_m) / hx_m - index_x;
244
245 const unsigned int index_y = static_cast<int>(std::floor((R(1) - ybegin_m) / hy_m));
246 const double lever_y = (R(1) - ybegin_m) / hy_m - index_y;
247
248 const unsigned int index_z = (int)std::floor((R(2) - zbegin_m)/ hz_m);
249 const double lever_z = (R(2) - zbegin_m) / hz_m - index_z;
250
251 if(index_z >= num_gridpz_m - 2) {
252 return false;
253 }
254
255 if(index_x >= num_gridpx_m - 2|| index_y >= num_gridpy_m - 2) {
256 return true;
257 }
258 const unsigned int deltaX = num_gridpy_m * num_gridpz_m;
259 const unsigned int deltaY = num_gridpz_m;
260 const unsigned int deltaZ = 1;
261
262 const unsigned long index1 = index_x * deltaX + index_y * deltaY + index_z * deltaZ;
263
264 E(0) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEx_m[index1 ]
265 + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEx_m[index1 + deltaX ]
266 + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthEx_m[index1 + deltaY ]
267 + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthEx_m[index1 + deltaX + deltaY ]
268 + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthEx_m[index1 + deltaZ]
269 + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthEx_m[index1 + deltaX + deltaZ]
270 + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthEx_m[index1 + deltaY + deltaZ]
271 + lever_x * lever_y * lever_z * FieldstrengthEx_m[index1 + deltaX + deltaY + deltaZ];
272
273 E(1) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEy_m[index1 ]
274 + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEy_m[index1 + deltaX ]
275 + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthEy_m[index1 + deltaY ]
276 + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthEy_m[index1 + deltaX + deltaY ]
277 + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthEy_m[index1 + deltaZ]
278 + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthEy_m[index1 + deltaX + deltaZ]
279 + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthEy_m[index1 + deltaY + deltaZ]
280 + lever_x * lever_y * lever_z * FieldstrengthEy_m[index1 + deltaX + deltaY + deltaZ];
281
282 E(2) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEz_m[index1 ]
283 + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEz_m[index1 + deltaX ]
284 + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthEz_m[index1 + deltaY ]
285 + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthEz_m[index1 + deltaX + deltaY ]
286 + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthEz_m[index1 + deltaZ]
287 + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthEz_m[index1 + deltaX + deltaZ]
288 + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthEz_m[index1 + deltaY + deltaZ]
289 + lever_x * lever_y * lever_z * FieldstrengthEz_m[index1 + deltaX + deltaY + deltaZ];
290
291 B(0) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBx_m[index1 ]
292 + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBx_m[index1 + deltaX ]
293 + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthBx_m[index1 + deltaY ]
294 + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthBx_m[index1 + deltaX + deltaY ]
295 + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthBx_m[index1 + deltaZ]
296 + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthBx_m[index1 + deltaX + deltaZ]
297 + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthBx_m[index1 + deltaY + deltaZ]
298 + lever_x * lever_y * lever_z * FieldstrengthBx_m[index1 + deltaX + deltaY + deltaZ];
299
300 B(1) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBy_m[index1 ]
301 + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBy_m[index1 + deltaX ]
302 + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthBy_m[index1 + deltaY ]
303 + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthBy_m[index1 + deltaX + deltaY ]
304 + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthBy_m[index1 + deltaZ]
305 + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthBy_m[index1 + deltaX + deltaZ]
306 + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthBy_m[index1 + deltaY + deltaZ]
307 + lever_x * lever_y * lever_z * FieldstrengthBy_m[index1 + deltaX + deltaY + deltaZ];
308
309 B(2) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBz_m[index1 ]
310 + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBz_m[index1 + deltaX ]
311 + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthBz_m[index1 + deltaY ]
312 + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthBz_m[index1 + deltaX + deltaY ]
313 + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthBz_m[index1 + deltaZ]
314 + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthBz_m[index1 + deltaX + deltaZ]
315 + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthBz_m[index1 + deltaY + deltaZ]
316 + lever_x * lever_y * lever_z * FieldstrengthBz_m[index1 + deltaX + deltaY + deltaZ];
317
318 return false;
319}
320
321bool FM3DDynamic::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
322 return false;
323}
324
325void FM3DDynamic::getFieldDimensions(double &zBegin, double &zEnd) const {
326 zBegin = zbegin_m;
327 zEnd = zend_m;
328}
329void FM3DDynamic::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/, double &/*yIni*/, double &/*yFinal*/, double &/*zIni*/, double &/*zFinal*/) const {}
330
332
334 (*msg) << Filename_m << " (3D dynamic) "
335 << " xini= " << xbegin_m << " xfinal= " << xend_m
336 << " yini= " << ybegin_m << " yfinal= " << yend_m
337 << " zini= " << zbegin_m << " zfinal= " << zend_m << " (m) " << endl;
338}
339
341 return frequency_m;
342}
343
344void FM3DDynamic::setFrequency(double freq) {
345 frequency_m = freq;
346}
347
348void FM3DDynamic::getOnaxisEz(std::vector<std::pair<double, double> > & F) {
349 F.resize(num_gridpz_m);
350
351 int index_x = static_cast<int>(ceil(-xbegin_m / hx_m));
352 double lever_x = index_x * hx_m + xbegin_m;
353 if(lever_x > 0.5) {
354 -- index_x;
355 }
356
357 int index_y = static_cast<int>(ceil(-ybegin_m / hy_m));
358 double lever_y = index_y * hy_m + ybegin_m;
359 if(lever_y > 0.5) {
360 -- index_y;
361 }
362
363 unsigned int ii = (index_y + index_x * num_gridpy_m) * num_gridpz_m;
364 for(unsigned int i = 0; i < num_gridpz_m; ++ i) {
365 F[i].first = hz_m * i;
366 F[i].second = FieldstrengthEz_m[ii ++] / 1e6;
367 }
368
369 auto opal = OpalData::getInstance();
370 if (opal->isOptimizerRun()) return;
371
372 std::string fname = Util::combineFilePath({
373 opal->getAuxiliaryOutputDirectory(),
375 });
376 std::ofstream out(fname);
377 for(unsigned int i = 0; i < num_gridpz_m; ++ i) {
378 Vector_t R(0,0,zbegin_m + F[i].first), B(0.0), E(0.0);
379 getFieldstrength(R, E, B);
380 out << std::setw(16) << std::setprecision(8) << F[i].first
381 << std::setw(16) << std::setprecision(8) << E(0)
382 << std::setw(16) << std::setprecision(8) << E(1)
383 << std::setw(16) << std::setprecision(8) << E(2)
384 << std::setw(16) << std::setprecision(8) << B(0)
385 << std::setw(16) << std::setprecision(8) << B(1)
386 << std::setw(16) << std::setprecision(8) << B(2) << "\n";
387 }
388 out.close();
389}
@ T3DDynamic
Definition: Fieldmap.h:30
DiffDirection
Definition: Fieldmap.h:54
PETE_TUTree< FnCeil, typename T::PETE_Expr_t > ceil(const PETE_Expr< T > &l)
Definition: PETE.h:728
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
Inform & level3(Inform &inf)
Definition: Inform.cpp:47
#define INFOMSG(msg)
Definition: IpplInfo.h:348
constexpr double two_pi
The value of.
Definition: Physics.h:33
constexpr double e
The value of.
Definition: Physics.h:39
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
constexpr double MVpm2Vpm
Definition: Units.h:128
bool ebDump
Definition: Options.cpp:65
std::string combineFilePath(std::initializer_list< std::string > ilist)
Definition: Util.cpp:196
std::string toUpper(const std::string &str)
Definition: Util.cpp:146
static OpalData * getInstance()
Definition: OpalData.cpp:196
MapType Type
Definition: Fieldmap.h:114
bool interpreteEOF(std::ifstream &in)
Definition: Fieldmap.cpp:558
void disableFieldmapWarning()
Definition: Fieldmap.cpp:613
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:652
std::string Filename_m
Definition: Fieldmap.h:117
void getLine(std::ifstream &in, std::string &buffer)
Definition: Fieldmap.h:121
void noFieldmapWarning()
Definition: Fieldmap.cpp:621
void write3DField(unsigned int nx, unsigned int ny, unsigned int nz, const std::pair< double, double > &xrange, const std::pair< double, double > &yrange, const std::pair< double, double > &zrange, const std::vector< Vector_t > &ef, const std::vector< Vector_t > &bf)
Definition: Fieldmap.cpp:737
virtual void getInfo(Inform *msg)
double * FieldstrengthEx_m
Definition: FM3DDynamic.h:28
double hy_m
Definition: FM3DDynamic.h:46
double * FieldstrengthBx_m
Definition: FM3DDynamic.h:31
double xend_m
Definition: FM3DDynamic.h:37
unsigned int num_gridpz_m
Definition: FM3DDynamic.h:50
double zbegin_m
Definition: FM3DDynamic.h:42
unsigned int num_gridpy_m
Definition: FM3DDynamic.h:49
double frequency_m
Definition: FM3DDynamic.h:34
double * FieldstrengthEz_m
Definition: FM3DDynamic.h:27
double ybegin_m
Definition: FM3DDynamic.h:39
unsigned int num_gridpx_m
Definition: FM3DDynamic.h:48
double zend_m
Definition: FM3DDynamic.h:43
bool normalize_m
Definition: FM3DDynamic.h:52
virtual void swap()
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &F)
virtual void freeMap()
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
double * FieldstrengthBy_m
Definition: FM3DDynamic.h:32
virtual void setFrequency(double freq)
FM3DDynamic(std::string aFilename)
Definition: FM3DDynamic.cpp:15
virtual void readMap()
double hx_m
Definition: FM3DDynamic.h:45
double xbegin_m
Definition: FM3DDynamic.h:36
double * FieldstrengthBz_m
Definition: FM3DDynamic.h:30
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
double yend_m
Definition: FM3DDynamic.h:40
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
virtual double getFrequency() const
double hz_m
Definition: FM3DDynamic.h:47
double * FieldstrengthEy_m
Definition: FM3DDynamic.h:29
Definition: Inform.h:42
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6