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