OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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 
9 using namespace std;
10 
12  : Fieldmap(aFilename),
13  FieldstrengthEz_m(NULL),
14  FieldstrengthEr_m(NULL) {
15  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 = interpreteLine<std::string, std::string>(file, tmpString, tmpString);
27  } catch (GeneralClassicException &e) {
28  parsing_passed = interpreteLine<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  interpreteLine<double, double, int>(file, rbegin_m, rend_m, num_gridpr_m);
47  parsing_passed = parsing_passed &&
48  interpreteLine<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  interpreteLine<double, double, int>(file, zbegin_m, zend_m, num_gridpz_m);
53  parsing_passed = parsing_passed &&
54  interpreteLine<double, double, int>(file, rbegin_m, rend_m, num_gridpr_m);
55  } else {
56  cerr << "unknown orientation of 2D electrostatic fieldmap" << 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 && interpreteLine<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
77  rbegin_m /= 100.;
78  rend_m /= 100.;
79  zbegin_m /= 100.;
80  zend_m /= 100.;
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 
101  if (FieldstrengthEz_m == NULL) {
102  // declare variables and allocate memory
103  ifstream in;
104  std::string tmpString;
105  double Ezmax = 0.0;
106 
109 
110  // read in and parse field map
111  in.open(Filename_m.c_str());
112  getLine(in, tmpString);
113  getLine(in, tmpString);
114  getLine(in, tmpString);
115 
116 
117  if (swap_m) {
118  for (int i = 0; i < num_gridpz_m; ++ i) {
119  for (int j = 0; j < num_gridpr_m; ++ j) {
120  interpreteLine<double, double>(in,
123  }
124  if (fabs(FieldstrengthEz_m[i]) > Ezmax) Ezmax = fabs(FieldstrengthEz_m[i]);
125  }
126  } else {
127  for (int j = 0; j < num_gridpr_m; ++ j) {
128  for (int i = 0; i < num_gridpz_m; ++ i) {
129  interpreteLine<double, double>(in,
132  }
133  }
134 
135  for (int i = 0; i < num_gridpz_m; ++ i) {
136  if (std::abs(FieldstrengthEz_m[i]) > Ezmax) {
137  Ezmax = std::abs(FieldstrengthEz_m[i]);
138  }
139  }
140  }
141  in.close();
142 
143  if (!normalize_m)
144  Ezmax = 1.0;
145 
146  // conversion MV/m to V/m and normalization to Ez_max = 1 MV/m
147  for (int i = 0; i < num_gridpr_m * num_gridpz_m; ++ i) {
148  FieldstrengthEz_m[i] *= 1e6 / Ezmax;
149  FieldstrengthEr_m[i] *= 1e6 / Ezmax;
150  }
151  INFOMSG(typeset_msg("read in field map '" + Filename_m + "'", "info") << "\n"
152  << endl);
153  }
154 }
155 
157  if (FieldstrengthEz_m != NULL) {
158  delete[] FieldstrengthEz_m;
159  FieldstrengthEz_m = NULL;
160  delete[] FieldstrengthEr_m;
161  FieldstrengthEr_m = NULL;
162 
163  INFOMSG(typeset_msg("freed field map '" + Filename_m + "'", "info") << "\n"
164  << endl)
165  }
166 }
167 
169  // do bi-linear interpolation
170  const double RR = sqrt(R(0) * R(0) + R(1) * R(1));
171 
172  const int indexr = (int)floor(RR / hr_m);
173  const double leverr = (RR / hr_m) - indexr;
174 
175  const int indexz = (int)floor((R(2)) / hz_m);
176  const double leverz = (R(2) / hz_m) - indexz;
177 
178  if ((indexz < 0) || (indexz + 2 > num_gridpz_m))
179  return false;
180  if (indexr + 2 > num_gridpr_m)
181  return true;
182 
183  const int index1 = indexz + indexr * num_gridpz_m;
184  const int index2 = index1 + num_gridpz_m;
185  const double EfieldR = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEr_m[index1]
186  + leverz * (1.0 - leverr) * FieldstrengthEr_m[index1 + 1]
187  + (1.0 - leverz) * leverr * FieldstrengthEr_m[index2]
188  + leverz * leverr * FieldstrengthEr_m[index2 + 1];
189 
190  const double EfieldZ = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEz_m[index1]
191  + leverz * (1.0 - leverr) * FieldstrengthEz_m[index1 + 1]
192  + (1.0 - leverz) * leverr * FieldstrengthEz_m[index2]
193  + leverz * leverr * FieldstrengthEz_m[index2 + 1];
194 
195  if (RR > 1e-10) {
196  E(0) += EfieldR * R(0) / RR;
197  E(1) += EfieldR * R(1) / RR;
198  }
199  E(2) += EfieldZ;
200  return false;
201 }
202 
204  return false;
205 }
206 
207 void FM2DElectroStatic::getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const {
208  zBegin = zbegin_m;
209  zEnd = zend_m;
210  rBegin = rbegin_m;
211  rEnd = rend_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 
229 { ;}
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 getInfo(Inform *msg)
DiffDirection
Definition: Fieldmap.h:54
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
Definition: PETE.h:815
std::string toUpper(const std::string &str)
Definition: Util.cpp:130
void disableFieldmapWarning()
Definition: Fieldmap.cpp:610
virtual double getFrequency() const
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:647
void noFieldmapWarning()
Definition: Fieldmap.cpp:618
bool normalize_m
Definition: Fieldmap.h:113
FM2DElectroStatic(std::string aFilename)
virtual void freeMap()
int lines_read_m
Definition: Fieldmap.h:111
virtual void getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const
virtual void readMap()
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
void getLine(std::ifstream &in, std::string &buffer)
Definition: Fieldmap.h:198
std::string Filename_m
Definition: Fieldmap.h:110
virtual void setFrequency(double freq)
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
MapType Type
Definition: Fieldmap.h:107
Definition: Inform.h:41
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