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