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