OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
FM2DDynamic.cpp
Go to the documentation of this file.
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
13FM2DDynamic::FM2DDynamic(std::string aFilename)
14 : Fieldmap(aFilename),
15 FieldstrengthEz_m(nullptr),
16 FieldstrengthEr_m(nullptr),
17 FieldstrengthBt_m(nullptr) {
18 std::ifstream file;
19 std::string tmpString;
20 double tmpDouble;
21
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
97 }
98 } else {
100 zbegin_m = 0.0;
101 zend_m = -1e-3;
102 }
103}
104
105
107 freeMap();
108}
109
111 if(FieldstrengthEz_m == nullptr) {
112 // declare variables and allocate memory
113 std::ifstream in;
114 std::string tmpString;
115 double tmpDouble, Ezmax = 0.0;
116
120
121 // read in field map and parse it
122 in.open(Filename_m.c_str());
123 getLine(in, tmpString);
124 getLine(in, tmpString);
125 getLine(in, tmpString);
126 getLine(in, tmpString);
127
128 if(swap_m) {
129 for(int i = 0; i < num_gridpz_m; i++) {
130 for(int j = 0; j < num_gridpr_m; j++) {
131 interpretLine<double, double, double, double>(in,
135 tmpDouble);
136 }
137 }
138 } else {
139 for(int j = 0; j < num_gridpr_m; j++) {
140 for(int i = 0; i < num_gridpz_m; i++) {
141 interpretLine<double, double, double, double>(in,
144 tmpDouble,
146 }
147 }
148 }
149
150 in.close();
151
152
153 if (normalize_m) {
154 // find maximum field
155 for(int i = 0; i < num_gridpz_m; ++ i) {
156 if(std::abs(FieldstrengthEz_m[i]) > Ezmax) {
157 Ezmax = std::abs(FieldstrengthEz_m[i]);
158 }
159 }
160 } else {
161 Ezmax = 1.0;
162 }
163
164 for(int i = 0; i < num_gridpr_m * num_gridpz_m; i++) {
165 FieldstrengthEz_m[i] *= 1.e6 / Ezmax; // conversion MV/m to V/m and normalization
166 FieldstrengthEr_m[i] *= 1.e6 / Ezmax;
167 FieldstrengthBt_m[i] *= Physics::mu_0 / Ezmax; // H -> B
168 }
169
170 INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info") << "\n"
171 << endl);
172 }
173}
174
176 if(FieldstrengthEz_m != nullptr) {
177 delete[] FieldstrengthEz_m;
178 FieldstrengthEz_m = nullptr;
179 delete[] FieldstrengthEr_m;
180 FieldstrengthEr_m = nullptr;
181 delete[] FieldstrengthBt_m;
182 FieldstrengthBt_m = nullptr;
183
184 INFOMSG(level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << "\n"
185 << endl);
186 }
187}
188
190 // do bi-linear interpolation
191 const double RR = std::sqrt(R(0) * R(0) + R(1) * R(1));
192
193 const int indexr = (int)std::floor(RR / hr_m);
194 const double leverr = RR / hr_m - indexr;
195
196 const int indexz = (int)std::floor((R(2) - zbegin_m) / hz_m);
197 const double leverz = (R(2) - zbegin_m) / hz_m - indexz;
198
199 if((indexz < 0) || (indexz + 2 > num_gridpz_m))
200 return false;
201 if(indexr + 2 > num_gridpr_m)
202 return true;
203
204 const int index1 = indexz + indexr * num_gridpz_m;
205 const int index2 = index1 + num_gridpz_m;
206
207 double EfieldR = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEr_m[index1]
208 + leverz * (1.0 - leverr) * FieldstrengthEr_m[index1 + 1]
209 + (1.0 - leverz) * leverr * FieldstrengthEr_m[index2]
210 + leverz * leverr * FieldstrengthEr_m[index2 + 1];
211
212 double EfieldZ = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEz_m[index1]
213 + leverz * (1.0 - leverr) * FieldstrengthEz_m[index1 + 1]
214 + (1.0 - leverz) * leverr * FieldstrengthEz_m[index2]
215 + leverz * leverr * FieldstrengthEz_m[index2 + 1];
216
217 double BfieldT = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthBt_m[index1]
218 + leverz * (1.0 - leverr) * FieldstrengthBt_m[index1 + 1]
219 + (1.0 - leverz) * leverr * FieldstrengthBt_m[index2]
220 + leverz * leverr * FieldstrengthBt_m[index2 + 1];
221
222 if(RR > 1e-10) {
223 E(0) += EfieldR * R(0) / RR;
224 E(1) += EfieldR * R(1) / RR;
225 B(0) -= BfieldT * R(1) / RR;
226 B(1) += BfieldT * R(0) / RR;
227 }
228 E(2) += EfieldZ;
229
230 return false;
231}
232
233bool FM2DDynamic::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
234 return false;
235}
236
237void FM2DDynamic::getFieldDimensions(double &zBegin, double &zEnd) const {
238 zBegin = zbegin_m;
239 zEnd = zend_m;
240}
241void FM2DDynamic::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/, double &/*yIni*/, double &/*yFinal*/, double &/*zIni*/, double &/*zFinal*/) const {}
242
244 if(swap_m) swap_m = false;
245 else swap_m = true;
246}
247
249 (*msg) << Filename_m << " (2D dynamic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
250}
251
253 return frequency_m;
254}
255
256void FM2DDynamic::setFrequency(double freq) {
257 frequency_m = freq;
258}
259
260void FM2DDynamic::getOnaxisEz(std::vector<std::pair<double, double> > & F) {
261 double dz = (zend_m - zbegin_m) / (num_gridpz_m - 1);
262 F.resize(num_gridpz_m);
263
264 for(int i = 0; i < num_gridpz_m; ++ i) {
265 F[i].first = dz * i;
266 F[i].second = FieldstrengthEz_m[i] / 1e6;
267
268 }
269}
@ T2DDynamic
Definition: Fieldmap.h:24
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
Inform & level3(Inform &inf)
Definition: Inform.cpp:47
#define INFOMSG(msg)
Definition: IpplInfo.h:348
constexpr double two_pi
The value of.
Definition: Physics.h:33
constexpr double e
The value of.
Definition: Physics.h:39
constexpr double mu_0
The permeability of vacuum in Vs/Am.
Definition: Physics.h:48
constexpr double MHz2Hz
Definition: Units.h:113
constexpr double cm2m
Definition: Units.h:35
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
double * FieldstrengthEz_m
Definition: FM2DDynamic.h:29
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
virtual void swap()
double frequency_m
Definition: FM2DDynamic.h:33
double hr_m
Definition: FM2DDynamic.h:40
virtual double getFrequency() const
virtual void readMap()
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
virtual void setFrequency(double freq)
double rbegin_m
Definition: FM2DDynamic.h:35
double rend_m
Definition: FM2DDynamic.h:36
double hz_m
Definition: FM2DDynamic.h:39
double zbegin_m
Definition: FM2DDynamic.h:37
virtual void getInfo(Inform *msg)
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &F)
double * FieldstrengthBt_m
Definition: FM2DDynamic.h:31
virtual void freeMap()
FM2DDynamic(std::string aFilename)
Definition: FM2DDynamic.cpp:13
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
int num_gridpz_m
Definition: FM2DDynamic.h:42
int num_gridpr_m
Definition: FM2DDynamic.h:41
double zend_m
Definition: FM2DDynamic.h:38
double * FieldstrengthEr_m
Definition: FM2DDynamic.h:30
Definition: Inform.h:42