OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
FM3DDynamic.cpp
Go to the documentation of this file.
1 #include "Fields/FM3DDynamic.h"
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 
15 _FM3DDynamic::_FM3DDynamic(const std::string& filename):
16  _Fieldmap(filename),
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 
28  Type = T3DDynamic;
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 
81  xbegin_m *= Units::cm2m;
82  xend_m *= Units::cm2m;
83  ybegin_m *= Units::cm2m;
84  yend_m *= Units::cm2m;
85  zbegin_m *= Units::cm2m;
86  zend_m *= Units::cm2m;
87 
88  hx_m = (xend_m - xbegin_m) / num_gridpx_m;
89  hy_m = (yend_m - ybegin_m) / num_gridpy_m;
90  hz_m = (zend_m - zbegin_m) / num_gridpz_m;
91 
92  num_gridpx_m++;
93  num_gridpy_m++;
94  num_gridpz_m++;
95 
96  }
97  } else {
99  zbegin_m = 0.0;
100  zend_m = -1e-3;
101  }
102 }
103 
104 
106  freeMap();
107 }
108 
109 FM3DDynamic _FM3DDynamic::create(const std::string& filename)
110 {
111  return FM3DDynamic(new _FM3DDynamic(filename));
112 }
113 
115  if(FieldstrengthEz_m == nullptr) {
116 
117  std::ifstream in(Filename_m.c_str());
118  std::string tmpString;
119  const size_t totalSize = num_gridpx_m * num_gridpy_m * num_gridpz_m;
120 
121  getLine(in, tmpString);
122  getLine(in, tmpString);
123  getLine(in, tmpString);
124  getLine(in, tmpString);
125  getLine(in, tmpString);
126 
127  FieldstrengthEz_m = new double[totalSize];
128  FieldstrengthEx_m = new double[totalSize];
129  FieldstrengthEy_m = new double[totalSize];
130  FieldstrengthBz_m = new double[totalSize];
131  FieldstrengthBx_m = new double[totalSize];
132  FieldstrengthBy_m = new double[totalSize];
133 
134  long ii = 0;
135  for(unsigned int i = 0; i < num_gridpx_m; ++ i) {
136  for(unsigned int j = 0; j < num_gridpy_m; ++ j) {
137  for(unsigned int k = 0; k < num_gridpz_m; ++ k) {
138  interpretLine<double>(in,
139  FieldstrengthEx_m[ii],
140  FieldstrengthEy_m[ii],
141  FieldstrengthEz_m[ii],
142  FieldstrengthBx_m[ii],
143  FieldstrengthBy_m[ii],
144  FieldstrengthBz_m[ii]);
145  ++ ii;
146  }
147  }
148  }
149  in.close();
150 
151  const unsigned int deltaX = num_gridpy_m * num_gridpz_m;
152  const unsigned int deltaY = num_gridpz_m;
153  double Ezmax = 0.0;
154 
155  if (normalize_m) {
156  int index_x = static_cast<int>(ceil(-xbegin_m / hx_m));
157  double lever_x = index_x * hx_m + xbegin_m;
158  if(lever_x > 0.5) {
159  -- index_x;
160  }
161 
162  int index_y = static_cast<int>(ceil(-ybegin_m / hy_m));
163  double lever_y = index_y * hy_m + ybegin_m;
164  if(lever_y > 0.5) {
165  -- index_y;
166  }
167 
168 
169  ii = index_x * deltaX + index_y * deltaY;
170  for(unsigned int i = 0; i < num_gridpz_m; i++) {
171  if(std::abs(FieldstrengthEz_m[ii]) > Ezmax) {
172  Ezmax = std::abs(FieldstrengthEz_m[ii]);
173  }
174  ++ ii;
175  }
176  } else {
177  Ezmax = 1.0;
178  }
179 
180  for(unsigned long i = 0; i < totalSize; ++ i) {
181 
182  FieldstrengthEx_m[i] *= Units::MVpm2Vpm / Ezmax;
183  FieldstrengthEy_m[i] *= Units::MVpm2Vpm / Ezmax;
184  FieldstrengthEz_m[i] *= Units::MVpm2Vpm / Ezmax;
185  FieldstrengthBx_m[i] *= Physics::mu_0 / Ezmax;
186  FieldstrengthBy_m[i] *= Physics::mu_0 / Ezmax;
187  FieldstrengthBz_m[i] *= Physics::mu_0 / Ezmax;
188  }
189 
190  INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info") << "\n"
191  << endl);
192 
193  if (Options::ebDump) {
194  std::vector<Vector_t> ef(num_gridpz_m * num_gridpy_m * num_gridpx_m, 0.0);
195  std::vector<Vector_t> bf(ef);
196  unsigned long l = 0;
197  for (unsigned long k = 0; k < num_gridpz_m; ++ k) {
198  const unsigned long index_z = k;
199  for (unsigned long j = 0; j < num_gridpy_m; ++ j) {
200  const unsigned long index_y = j * deltaY;
201  for (unsigned long i = 0; i < num_gridpx_m; ++ i) {
202  const unsigned long index = i * deltaX + index_y + index_z;
203  ef[l] = Vector_t(FieldstrengthEx_m[index],
204  FieldstrengthEy_m[index],
205  FieldstrengthEz_m[index]);
206  bf[l] = Vector_t(FieldstrengthBx_m[index],
207  FieldstrengthBy_m[index],
208  FieldstrengthBz_m[index]);
209  ++ l;
210  }
211  }
212  }
213  write3DField(num_gridpx_m,
214  num_gridpy_m,
215  num_gridpz_m,
216  std::make_pair(xbegin_m, xend_m),
217  std::make_pair(ybegin_m, yend_m),
218  std::make_pair(zbegin_m, zend_m),
219  ef,
220  bf);
221  }
222  }
223 }
224 
226  if(FieldstrengthEz_m != nullptr) {
227  delete[] FieldstrengthEz_m;
228  delete[] FieldstrengthEx_m;
229  delete[] FieldstrengthEy_m;
230  delete[] FieldstrengthBz_m;
231  delete[] FieldstrengthBx_m;
232  delete[] FieldstrengthBy_m;
233 
234  FieldstrengthEz_m = nullptr;
235  FieldstrengthEx_m = nullptr;
236  FieldstrengthEy_m = nullptr;
237  FieldstrengthBz_m = nullptr;
238  FieldstrengthBx_m = nullptr;
239  FieldstrengthBy_m = nullptr;
240  }
241 }
242 
244  const unsigned int index_x = static_cast<int>(std::floor((R(0) - xbegin_m) / hx_m));
245  const double lever_x = (R(0) - xbegin_m) / hx_m - index_x;
246 
247  const unsigned int index_y = static_cast<int>(std::floor((R(1) - ybegin_m) / hy_m));
248  const double lever_y = (R(1) - ybegin_m) / hy_m - index_y;
249 
250  const unsigned int index_z = (int)std::floor((R(2) - zbegin_m)/ hz_m);
251  const double lever_z = (R(2) - zbegin_m) / hz_m - index_z;
252 
253  if(index_z >= num_gridpz_m - 2) {
254  return false;
255  }
256 
257  if(index_x >= num_gridpx_m - 2|| index_y >= num_gridpy_m - 2) {
258  return true;
259  }
260  const unsigned int deltaX = num_gridpy_m * num_gridpz_m;
261  const unsigned int deltaY = num_gridpz_m;
262  const unsigned int deltaZ = 1;
263 
264  const unsigned long index1 = index_x * deltaX + index_y * deltaY + index_z * deltaZ;
265 
266  E(0) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEx_m[index1 ]
267  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEx_m[index1 + deltaX ]
268  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthEx_m[index1 + deltaY ]
269  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthEx_m[index1 + deltaX + deltaY ]
270  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthEx_m[index1 + deltaZ]
271  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthEx_m[index1 + deltaX + deltaZ]
272  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthEx_m[index1 + deltaY + deltaZ]
273  + lever_x * lever_y * lever_z * FieldstrengthEx_m[index1 + deltaX + deltaY + deltaZ];
274 
275  E(1) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEy_m[index1 ]
276  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEy_m[index1 + deltaX ]
277  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthEy_m[index1 + deltaY ]
278  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthEy_m[index1 + deltaX + deltaY ]
279  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthEy_m[index1 + deltaZ]
280  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthEy_m[index1 + deltaX + deltaZ]
281  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthEy_m[index1 + deltaY + deltaZ]
282  + lever_x * lever_y * lever_z * FieldstrengthEy_m[index1 + deltaX + deltaY + deltaZ];
283 
284  E(2) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEz_m[index1 ]
285  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEz_m[index1 + deltaX ]
286  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthEz_m[index1 + deltaY ]
287  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthEz_m[index1 + deltaX + deltaY ]
288  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthEz_m[index1 + deltaZ]
289  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthEz_m[index1 + deltaX + deltaZ]
290  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthEz_m[index1 + deltaY + deltaZ]
291  + lever_x * lever_y * lever_z * FieldstrengthEz_m[index1 + deltaX + deltaY + deltaZ];
292 
293  B(0) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBx_m[index1 ]
294  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBx_m[index1 + deltaX ]
295  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthBx_m[index1 + deltaY ]
296  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthBx_m[index1 + deltaX + deltaY ]
297  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthBx_m[index1 + deltaZ]
298  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthBx_m[index1 + deltaX + deltaZ]
299  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthBx_m[index1 + deltaY + deltaZ]
300  + lever_x * lever_y * lever_z * FieldstrengthBx_m[index1 + deltaX + deltaY + deltaZ];
301 
302  B(1) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBy_m[index1 ]
303  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBy_m[index1 + deltaX ]
304  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthBy_m[index1 + deltaY ]
305  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthBy_m[index1 + deltaX + deltaY ]
306  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthBy_m[index1 + deltaZ]
307  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthBy_m[index1 + deltaX + deltaZ]
308  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthBy_m[index1 + deltaY + deltaZ]
309  + lever_x * lever_y * lever_z * FieldstrengthBy_m[index1 + deltaX + deltaY + deltaZ];
310 
311  B(2) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBz_m[index1 ]
312  + lever_x * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthBz_m[index1 + deltaX ]
313  + (1.0 - lever_x) * lever_y * (1.0 - lever_z) * FieldstrengthBz_m[index1 + deltaY ]
314  + lever_x * lever_y * (1.0 - lever_z) * FieldstrengthBz_m[index1 + deltaX + deltaY ]
315  + (1.0 - lever_x) * (1.0 - lever_y) * lever_z * FieldstrengthBz_m[index1 + deltaZ]
316  + lever_x * (1.0 - lever_y) * lever_z * FieldstrengthBz_m[index1 + deltaX + deltaZ]
317  + (1.0 - lever_x) * lever_y * lever_z * FieldstrengthBz_m[index1 + deltaY + deltaZ]
318  + lever_x * lever_y * lever_z * FieldstrengthBz_m[index1 + deltaX + deltaY + deltaZ];
319 
320  return false;
321 }
322 
323 bool _FM3DDynamic::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
324  return false;
325 }
326 
327 void _FM3DDynamic::getFieldDimensions(double &zBegin, double &zEnd) const {
328  zBegin = zbegin_m;
329  zEnd = zend_m;
330 }
331 void _FM3DDynamic::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/, double &/*yIni*/, double &/*yFinal*/, double &/*zIni*/, double &/*zFinal*/) const {}
332 
334 
336  (*msg) << Filename_m << " (3D dynamic) "
337  << " xini= " << xbegin_m << " xfinal= " << xend_m
338  << " yini= " << ybegin_m << " yfinal= " << yend_m
339  << " zini= " << zbegin_m << " zfinal= " << zend_m << " (m) " << endl;
340 }
341 
343  return frequency_m;
344 }
345 
346 void _FM3DDynamic::setFrequency(double freq) {
347  frequency_m = freq;
348 }
349 
350 void _FM3DDynamic::getOnaxisEz(std::vector<std::pair<double, double> > & F) {
351  F.resize(num_gridpz_m);
352 
353  int index_x = static_cast<int>(ceil(-xbegin_m / hx_m));
354  double lever_x = index_x * hx_m + xbegin_m;
355  if(lever_x > 0.5) {
356  -- index_x;
357  }
358 
359  int index_y = static_cast<int>(ceil(-ybegin_m / hy_m));
360  double lever_y = index_y * hy_m + ybegin_m;
361  if(lever_y > 0.5) {
362  -- index_y;
363  }
364 
365  unsigned int ii = (index_y + index_x * num_gridpy_m) * num_gridpz_m;
366  for(unsigned int i = 0; i < num_gridpz_m; ++ i) {
367  F[i].first = hz_m * i;
368  F[i].second = FieldstrengthEz_m[ii ++] / 1e6;
369  }
370 
371  auto opal = OpalData::getInstance();
372  if (opal->isOptimizerRun()) return;
373 
374  std::string fname = Util::combineFilePath({
375  opal->getAuxiliaryOutputDirectory(),
376  Filename_m
377  });
378  std::ofstream out(fname);
379  for(unsigned int i = 0; i < num_gridpz_m; ++ i) {
380  Vector_t R(0,0,zbegin_m + F[i].first), B(0.0), E(0.0);
381  getFieldstrength(R, E, B);
382  out << std::setw(16) << std::setprecision(8) << F[i].first
383  << std::setw(16) << std::setprecision(8) << E(0)
384  << std::setw(16) << std::setprecision(8) << E(1)
385  << std::setw(16) << std::setprecision(8) << E(2)
386  << std::setw(16) << std::setprecision(8) << B(0)
387  << std::setw(16) << std::setprecision(8) << B(1)
388  << std::setw(16) << std::setprecision(8) << B(2) << "\n";
389  }
390  out.close();
391 }
double * FieldstrengthBy_m
Definition: FM3DDynamic.h:35
static OpalData * getInstance()
Definition: OpalData.cpp:196
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
double zend_m
Definition: FM3DDynamic.h:46
bool normalize_m
Definition: FM3DDynamic.h:55
MapType Type
Definition: Fieldmap.h:115
std::shared_ptr< _FM3DDynamic > FM3DDynamic
Definition: Definitions.h:60
DiffDirection
Definition: Fieldmap.h:55
constexpr double MVpm2Vpm
Definition: Units.h:128
constexpr double two_pi
The value of .
Definition: Physics.h:33
void noFieldmapWarning()
Definition: Fieldmap.cpp:618
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
double * FieldstrengthEy_m
Definition: FM3DDynamic.h:32
double hy_m
Definition: FM3DDynamic.h:49
unsigned int num_gridpz_m
Definition: FM3DDynamic.h:53
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6
constexpr double cm2m
Definition: Units.h:35
double * FieldstrengthEz_m
Definition: FM3DDynamic.h:30
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:649
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
std::string toUpper(const std::string &str)
Definition: Util.cpp:147
#define INFOMSG(msg)
Definition: IpplInfo.h:348
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
bool interpreteEOF(std::ifstream &in)
Definition: Fieldmap.cpp:555
bool ebDump
Definition: Options.cpp:65
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
double xbegin_m
Definition: FM3DDynamic.h:39
double hx_m
Definition: FM3DDynamic.h:48
virtual void readMap()
virtual void setFrequency(double freq)
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:734
double xend_m
Definition: FM3DDynamic.h:40
constexpr double mu_0
The permeability of vacuum in Vs/Am.
Definition: Physics.h:48
double ybegin_m
Definition: FM3DDynamic.h:42
Definition: Inform.h:42
double zbegin_m
Definition: FM3DDynamic.h:45
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &F)
double frequency_m
Definition: FM3DDynamic.h:37
unsigned int num_gridpx_m
Definition: FM3DDynamic.h:51
unsigned int num_gridpy_m
Definition: FM3DDynamic.h:52
double * FieldstrengthBx_m
Definition: FM3DDynamic.h:34
void disableFieldmapWarning()
Definition: Fieldmap.cpp:610
static FM3DDynamic create(const std::string &filename)
virtual void getInfo(Inform *msg)
double yend_m
Definition: FM3DDynamic.h:43
constexpr double MHz2Hz
Definition: Units.h:113
std::string combineFilePath(std::initializer_list< std::string > ilist)
Definition: Util.cpp:197
void getLine(std::ifstream &in, std::string &buffer)
Definition: Fieldmap.h:122
double hz_m
Definition: FM3DDynamic.h:50
Inform & level3(Inform &inf)
Definition: Inform.cpp:47
constexpr double e
The value of .
Definition: Physics.h:39
virtual void swap()
PETE_TUTree< FnCeil, typename T::PETE_Expr_t > ceil(const PETE_Expr< T > &l)
Definition: PETE.h:728
std::string Filename_m
Definition: Fieldmap.h:118
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
double * FieldstrengthBz_m
Definition: FM3DDynamic.h:33
_FM3DDynamic(const std::string &filename)
Definition: FM3DDynamic.cpp:15
virtual void freeMap()
virtual ~_FM3DDynamic()
double * FieldstrengthEx_m
Definition: FM3DDynamic.h:31
virtual double getFrequency() const