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