OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
FM2DDynamic.cpp
Go to the documentation of this file.
1 #include "Fields/FM2DDynamic.h"
2 #include "Fields/Fieldmap.hpp"
3 #include "Physics/Physics.h"
5 #include "Utilities/Util.h"
6 
7 #include <fstream>
8 #include <ios>
9 #include <cmath>
10 
11 
12 FM2DDynamic::FM2DDynamic(std::string aFilename)
13  : Fieldmap(aFilename),
14  FieldstrengthEz_m(NULL),
15  FieldstrengthEr_m(NULL),
16  FieldstrengthBt_m(NULL) {
17  std::ifstream file;
18  std::string tmpString;
19  double tmpDouble;
20 
21  Type = T2DDynamic;
22 
23  // open field map, parse it and disable element on error
24  file.open(Filename_m.c_str());
25  if(file.good()) {
26  bool parsing_passed = true;
27  try {
28  parsing_passed = interpretLine<std::string, std::string>(file, tmpString, tmpString);
29  } catch (GeneralClassicException &e) {
30  parsing_passed = interpretLine<std::string, std::string, std::string>(file, tmpString, tmpString, tmpString);
31 
32  tmpString = Util::toUpper(tmpString);
33  if (tmpString != "TRUE" &&
34  tmpString != "FALSE")
35  throw GeneralClassicException("FM2DDynamic::FM2DDynamic",
36  "The third string on the first line of 2D field "
37  "maps has to be either TRUE or FALSE");
38 
39  normalize_m = (tmpString == "TRUE");
40  }
41 
42  if(tmpString == "ZX") {
43  swap_m = true;
44  parsing_passed = parsing_passed &&
45  interpretLine<double, double, int>(file, rbegin_m, rend_m, num_gridpr_m);
46  parsing_passed = parsing_passed &&
47  interpretLine<double>(file, frequency_m);
48  parsing_passed = parsing_passed &&
49  interpretLine<double, double, int>(file, zbegin_m, zend_m, num_gridpz_m);
50  } else if(tmpString == "XZ") {
51  swap_m = false;
52  parsing_passed = parsing_passed &&
53  interpretLine<double, double, int>(file, zbegin_m, zend_m, num_gridpz_m);
54  parsing_passed = parsing_passed &&
55  interpretLine<double>(file, frequency_m);
56  parsing_passed = parsing_passed &&
57  interpretLine<double, double, int>(file, rbegin_m, rend_m, num_gridpr_m);
58  } else {
59  std::cerr << "unknown orientation of 2D dynamic fieldmap" << std::endl;
60  parsing_passed = false;
61  zbegin_m = 0.0;
62  zend_m = -1e-3;
63  }
64 
65  for(long i = 0; (i < (num_gridpz_m + 1) * (num_gridpr_m + 1)) && parsing_passed; ++ i) {
66  parsing_passed = parsing_passed && interpretLine<double, double, double, double>(file, tmpDouble, tmpDouble, tmpDouble, tmpDouble);
67  }
68 
69  parsing_passed = parsing_passed &&
70  interpreteEOF(file);
71 
72  file.close();
73  lines_read_m = 0;
74 
75  if(!parsing_passed) {
77  zend_m = zbegin_m - 1e-3;
78  throw GeneralClassicException("FM2DDynamic::FM2DDynamic",
79  "An error occured when reading the fieldmap '" + Filename_m + "'");
80  } else {
81  // convert MHz to Hz and frequency to angular frequency
83 
84  // convert cm to m
85  rbegin_m /= 100.0;
86  rend_m /= 100.0;
87  zbegin_m /= 100.0;
88  zend_m /= 100.0;
89 
92 
93  // num spacings -> num grid points
94  num_gridpr_m++;
95  num_gridpz_m++;
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 == NULL) {
111  // declare variables and allocate memory
112  std::ifstream in;
113  std::string tmpString;
114  double tmpDouble, Ezmax = 0.0;
115 
119 
120  // read in field map and parse it
121  in.open(Filename_m.c_str());
122  getLine(in, tmpString);
123  getLine(in, tmpString);
124  getLine(in, tmpString);
125  getLine(in, tmpString);
126 
127  if(swap_m) {
128  for(int i = 0; i < num_gridpz_m; i++) {
129  for(int j = 0; j < num_gridpr_m; j++) {
130  interpretLine<double, double, double, double>(in,
134  tmpDouble);
135  }
136  }
137  } else {
138  for(int j = 0; j < num_gridpr_m; j++) {
139  for(int i = 0; i < num_gridpz_m; i++) {
140  interpretLine<double, double, double, double>(in,
143  tmpDouble,
145  }
146  }
147  }
148 
149  in.close();
150 
151 
152  if (normalize_m) {
153  // find maximum field
154  for(int i = 0; i < num_gridpz_m; ++ i) {
155  if(std::abs(FieldstrengthEz_m[i]) > Ezmax) {
156  Ezmax = std::abs(FieldstrengthEz_m[i]);
157  }
158  }
159  } else {
160  Ezmax = 1.0;
161  }
162 
163  for(int i = 0; i < num_gridpr_m * num_gridpz_m; i++) {
164  FieldstrengthEz_m[i] *= 1.e6 / Ezmax; // conversion MV/m to V/m and normalization
165  FieldstrengthEr_m[i] *= 1.e6 / Ezmax;
166  FieldstrengthBt_m[i] *= Physics::mu_0 / Ezmax; // H -> B
167  }
168 
169  INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info") << "\n"
170  << endl);
171  }
172 }
173 
175  if(FieldstrengthEz_m != NULL) {
176  delete[] FieldstrengthEz_m;
177  FieldstrengthEz_m = NULL;
178  delete[] FieldstrengthEr_m;
179  FieldstrengthEr_m = NULL;
180  delete[] FieldstrengthBt_m;
181  FieldstrengthBt_m = NULL;
182 
183  INFOMSG(level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << "\n"
184  << endl);
185  }
186 }
187 
189  // do bi-linear interpolation
190  const double RR = std::sqrt(R(0) * R(0) + R(1) * R(1));
191 
192  const int indexr = (int)std::floor(RR / hr_m);
193  const double leverr = RR / hr_m - indexr;
194 
195  const int indexz = (int)std::floor((R(2) - zbegin_m) / hz_m);
196  const double leverz = (R(2) - zbegin_m) / hz_m - indexz;
197 
198  if((indexz < 0) || (indexz + 2 > num_gridpz_m))
199  return false;
200  if(indexr + 2 > num_gridpr_m)
201  return true;
202 
203  const int index1 = indexz + indexr * num_gridpz_m;
204  const int index2 = index1 + num_gridpz_m;
205 
206  double EfieldR = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEr_m[index1]
207  + leverz * (1.0 - leverr) * FieldstrengthEr_m[index1 + 1]
208  + (1.0 - leverz) * leverr * FieldstrengthEr_m[index2]
209  + leverz * leverr * FieldstrengthEr_m[index2 + 1];
210 
211  double EfieldZ = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEz_m[index1]
212  + leverz * (1.0 - leverr) * FieldstrengthEz_m[index1 + 1]
213  + (1.0 - leverz) * leverr * FieldstrengthEz_m[index2]
214  + leverz * leverr * FieldstrengthEz_m[index2 + 1];
215 
216  double BfieldT = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthBt_m[index1]
217  + leverz * (1.0 - leverr) * FieldstrengthBt_m[index1 + 1]
218  + (1.0 - leverz) * leverr * FieldstrengthBt_m[index2]
219  + leverz * leverr * FieldstrengthBt_m[index2 + 1];
220 
221  if(RR > 1e-10) {
222  E(0) += EfieldR * R(0) / RR;
223  E(1) += EfieldR * R(1) / RR;
224  B(0) -= BfieldT * R(1) / RR;
225  B(1) += BfieldT * R(0) / RR;
226  }
227  E(2) += EfieldZ;
228 
229  return false;
230 }
231 
232 bool FM2DDynamic::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
233  return false;
234 }
235 
236 void FM2DDynamic::getFieldDimensions(double &zBegin, double &zEnd) const {
237  zBegin = zbegin_m;
238  zEnd = zend_m;
239 }
240 void FM2DDynamic::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/, double &/*yIni*/, double &/*yFinal*/, double &/*zIni*/, double &/*zFinal*/) const {}
241 
243  if(swap_m) swap_m = false;
244  else swap_m = true;
245 }
246 
248  (*msg) << Filename_m << " (2D dynamic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
249 }
250 
252  return frequency_m;
253 }
254 
255 void FM2DDynamic::setFrequency(double freq) {
256  frequency_m = freq;
257 }
258 
259 void FM2DDynamic::getOnaxisEz(std::vector<std::pair<double, double> > & F) {
260  double dz = (zend_m - zbegin_m) / (num_gridpz_m - 1);
261  F.resize(num_gridpz_m);
262 
263  for(int i = 0; i < num_gridpz_m; ++ i) {
264  F[i].first = dz * i;
265  F[i].second = FieldstrengthEz_m[i] / 1e6;
266 
267  }
268 }
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
@ T2DDynamic
Definition: Fieldmap.h:24
DiffDirection
Definition: Fieldmap.h:54
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
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:54
std::string toUpper(const std::string &str)
Definition: Util.cpp:132
MapType Type
Definition: Fieldmap.h:114
bool interpreteEOF(std::ifstream &in)
Definition: Fieldmap.cpp:558
void disableFieldmapWarning()
Definition: Fieldmap.cpp:613
bool normalize_m
Definition: Fieldmap.h:120
int lines_read_m
Definition: Fieldmap.h:118
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
double * FieldstrengthEz_m
Definition: FM2DDynamic.h:29
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
virtual void swap()
double frequency_m
Definition: FM2DDynamic.h:33
double hr_m
Definition: FM2DDynamic.h:40
virtual double getFrequency() const
virtual void readMap()
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
virtual void setFrequency(double freq)
double rbegin_m
Definition: FM2DDynamic.h:35
double rend_m
Definition: FM2DDynamic.h:36
double hz_m
Definition: FM2DDynamic.h:39
double zbegin_m
Definition: FM2DDynamic.h:37
virtual void getInfo(Inform *msg)
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &F)
double * FieldstrengthBt_m
Definition: FM2DDynamic.h:31
virtual void freeMap()
FM2DDynamic(std::string aFilename)
Definition: FM2DDynamic.cpp:12
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
int num_gridpz_m
Definition: FM2DDynamic.h:42
int num_gridpr_m
Definition: FM2DDynamic.h:41
double zend_m
Definition: FM2DDynamic.h:38
double * FieldstrengthEr_m
Definition: FM2DDynamic.h:30
Definition: Inform.h:42