OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
FM2DElectroStatic.cpp
Go to the documentation of this file.
2 #include "Fields/Fieldmap.hpp"
4 #include "Utilities/Util.h"
5 
6 #include <fstream>
7 #include <ios>
8 #include <cmath>
9 
11  : Fieldmap(aFilename),
12  FieldstrengthEz_m(NULL),
13  FieldstrengthEr_m(NULL) {
14  std::ifstream file;
15  std::string tmpString;
16  double tmpDouble;
17 
19 
20  // open field map, parse it and disable element on error
21  file.open(Filename_m.c_str());
22  if (file.good()) {
23  bool parsing_passed = true;
24  try {
25  parsing_passed = interpretLine<std::string, std::string>(file, tmpString, tmpString);
26  } catch (GeneralClassicException &e) {
27  parsing_passed = interpretLine<std::string, std::string, std::string>(file,
28  tmpString,
29  tmpString,
30  tmpString);
31 
32  tmpString = Util::toUpper(tmpString);
33  if (tmpString != "TRUE" &&
34  tmpString != "FALSE")
35  throw GeneralClassicException("FM2DElectroStatic::FM2DElectroStatic",
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, double, int>(file, zbegin_m, zend_m, num_gridpz_m);
48  } else if (tmpString == "XZ") {
49  swap_m = false;
50  parsing_passed = parsing_passed &&
51  interpretLine<double, double, int>(file, zbegin_m, zend_m, num_gridpz_m);
52  parsing_passed = parsing_passed &&
53  interpretLine<double, double, int>(file, rbegin_m, rend_m, num_gridpr_m);
54  } else {
55  std::cerr << "unknown orientation of 2D electrostatic fieldmap" << std::endl;
56  parsing_passed = false;
57  }
58 
59  for (long i = 0; (i < (num_gridpz_m + 1) * (num_gridpr_m + 1)) && parsing_passed; ++ i) {
60  parsing_passed = parsing_passed && interpretLine<double, double>(file, tmpDouble, tmpDouble);
61  }
62 
63  parsing_passed = parsing_passed &&
64  interpreteEOF(file);
65 
66  file.close();
67  lines_read_m = 0;
68 
69  if (!parsing_passed) {
71  zend_m = zbegin_m - 1e-3;
72  throw GeneralClassicException("FM2DElectroStatic::FM2DElectroStatic",
73  "An error occured when reading the fieldmap '" + Filename_m + "'");
74  } else {
75  // conversion from cm to m
76  rbegin_m /= 100.;
77  rend_m /= 100.;
78  zbegin_m /= 100.;
79  zend_m /= 100.;
80 
83 
84  // num spacings -> num grid points
85  ++ num_gridpr_m;
86  ++ num_gridpz_m;
87  }
88  } else {
90  zbegin_m = 0.0;
91  zend_m = -1e-3;
92  }
93 }
94 
96  freeMap();
97 }
98 
100  if (FieldstrengthEz_m == NULL) {
101  // declare variables and allocate memory
102  std::ifstream in;
103  std::string tmpString;
104  double Ezmax = 0.0;
105 
108 
109  // read in and parse field map
110  in.open(Filename_m.c_str());
111  getLine(in, tmpString);
112  getLine(in, tmpString);
113  getLine(in, tmpString);
114 
115 
116  if (swap_m) {
117  for (int i = 0; i < num_gridpz_m; ++ i) {
118  for (int j = 0; j < num_gridpr_m; ++ j) {
119  interpretLine<double, double>(in,
122  }
123  if (std::abs(FieldstrengthEz_m[i]) > Ezmax) Ezmax = std::abs(FieldstrengthEz_m[i]);
124  }
125  } else {
126  for (int j = 0; j < num_gridpr_m; ++ j) {
127  for (int i = 0; i < num_gridpz_m; ++ i) {
128  interpretLine<double, double>(in,
131  }
132  }
133 
134  for (int i = 0; i < num_gridpz_m; ++ i) {
135  if (std::abs(FieldstrengthEz_m[i]) > Ezmax) {
136  Ezmax = std::abs(FieldstrengthEz_m[i]);
137  }
138  }
139  }
140  in.close();
141 
142  if (!normalize_m)
143  Ezmax = 1.0;
144 
145  // conversion MV/m to V/m and normalization to Ez_max = 1 MV/m
146  for (int i = 0; i < num_gridpr_m * num_gridpz_m; ++ i) {
147  FieldstrengthEz_m[i] *= 1e6 / Ezmax;
148  FieldstrengthEr_m[i] *= 1e6 / Ezmax;
149  }
150  INFOMSG(typeset_msg("read in field map '" + Filename_m + "'", "info") << "\n"
151  << endl);
152  }
153 }
154 
156  if (FieldstrengthEz_m != NULL) {
157  delete[] FieldstrengthEz_m;
158  FieldstrengthEz_m = NULL;
159  delete[] FieldstrengthEr_m;
160  FieldstrengthEr_m = NULL;
161 
162  INFOMSG(typeset_msg("freed field map '" + Filename_m + "'", "info") << "\n"
163  << endl)
164  }
165 }
166 
168  // do bi-linear interpolation
169  const double RR = std::sqrt(R(0) * R(0) + R(1) * R(1));
170 
171  const int indexr = (int)std::floor(RR / hr_m);
172  const double leverr = (RR / hr_m) - indexr;
173 
174  const int indexz = (int)std::floor((R(2) - zbegin_m) / hz_m);
175  const double leverz = (R(2) - zbegin_m) / hz_m - indexz;
176 
177  if ((indexz < 0) || (indexz + 2 > num_gridpz_m))
178  return false;
179  if (indexr + 2 > num_gridpr_m)
180  return true;
181 
182  const int index1 = indexz + indexr * num_gridpz_m;
183  const int index2 = index1 + num_gridpz_m;
184  const double EfieldR = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEr_m[index1]
185  + leverz * (1.0 - leverr) * FieldstrengthEr_m[index1 + 1]
186  + (1.0 - leverz) * leverr * FieldstrengthEr_m[index2]
187  + leverz * leverr * FieldstrengthEr_m[index2 + 1];
188 
189  const double EfieldZ = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEz_m[index1]
190  + leverz * (1.0 - leverr) * FieldstrengthEz_m[index1 + 1]
191  + (1.0 - leverz) * leverr * FieldstrengthEz_m[index2]
192  + leverz * leverr * FieldstrengthEz_m[index2 + 1];
193 
194  if (RR > 1e-10) {
195  E(0) += EfieldR * R(0) / RR;
196  E(1) += EfieldR * R(1) / RR;
197  }
198  E(2) += EfieldZ;
199  return false;
200 }
201 
202 bool FM2DElectroStatic::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
203  return false;
204 }
205 
206 void FM2DElectroStatic::getFieldDimensions(double &zBegin, double &zEnd) const {
207  zBegin = zbegin_m;
208  zEnd = zend_m;
209 }
210 void FM2DElectroStatic::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/, double &/*yIni*/, double &/*yFinal*/, double &/*zIni*/, double &/*zFinal*/) const {}
211 
213  if (swap_m) swap_m = false;
214  else swap_m = true;
215 }
216 
218  (*msg) << Filename_m << " (2D electrostatic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
219 }
220 
222  return 0.0;
223 }
224 
225 void FM2DElectroStatic::setFrequency(double /*freq*/)
226 { ;}
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
@ T2DElectroStatic
Definition: Fieldmap.h:26
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
#define INFOMSG(msg)
Definition: IpplInfo.h:348
constexpr double e
The value of.
Definition: Physics.h:39
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
virtual double getFrequency() const
virtual void readMap()
virtual void freeMap()
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
FM2DElectroStatic(std::string aFilename)
virtual void setFrequency(double freq)
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
virtual void getInfo(Inform *msg)
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
Definition: Inform.h:42