OPAL (Object Oriented Parallel Accelerator Library) 2022.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
12 : Fieldmap(aFilename),
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
101 if (FieldstrengthEz_m == nullptr) {
102 // declare variables and allocate memory
103 std::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 interpretLine<double, double>(in,
123 }
124 if (std::abs(FieldstrengthEz_m[i]) > Ezmax) Ezmax = std::abs(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 interpretLine<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) {
150 }
151 INFOMSG(typeset_msg("read in field map '" + Filename_m + "'", "info") << "\n"
152 << endl);
153 }
154}
155
157 if (FieldstrengthEz_m != nullptr) {
158 delete[] FieldstrengthEz_m;
159 FieldstrengthEz_m = nullptr;
160 delete[] FieldstrengthEr_m;
161 FieldstrengthEr_m = nullptr;
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 = std::sqrt(R(0) * R(0) + R(1) * R(1));
171
172 const int indexr = (int)std::floor(RR / hr_m);
173 const double leverr = (RR / hr_m) - indexr;
174
175 const int indexz = (int)std::floor((R(2) - zbegin_m) / hz_m);
176 const double leverz = (R(2) - zbegin_m) / 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
203bool FM2DElectroStatic::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
204 return false;
205}
206
207void FM2DElectroStatic::getFieldDimensions(double &zBegin, double &zEnd) const {
208 zBegin = zbegin_m;
209 zEnd = zend_m;
210}
211void FM2DElectroStatic::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/, double &/*yIni*/, double &/*yFinal*/, double &/*zIni*/, double &/*zFinal*/) const {}
212
214 if (swap_m) swap_m = false;
215 else swap_m = true;
216}
217
219 (*msg) << Filename_m << " (2D electrostatic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
220}
221
223 return 0.0;
224}
225
227{ ;}
@ T2DElectroStatic
Definition: Fieldmap.h:26
DiffDirection
Definition: Fieldmap.h:54
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
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
constexpr double cm2m
Definition: Units.h:35
constexpr double MVpm2Vpm
Definition: Units.h:128
std::string toUpper(const std::string &str)
Definition: Util.cpp:146
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