OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
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 <cmath>
8#include <fstream>
9#include <ios>
10
12 : Fieldmap(aFilename), FieldstrengthEz_m(nullptr), FieldstrengthEr_m(nullptr) {
13 std::ifstream file;
14 std::string tmpString;
15 double tmpDouble;
16
18
19 // open field map, parse it and disable element on error
20 file.open(Filename_m.c_str());
21 if (file.good()) {
22 bool parsing_passed = true;
23 try {
24 parsing_passed = interpretLine<std::string, std::string>(file, tmpString, tmpString);
25 } catch (GeneralClassicException& e) {
27 file, tmpString, tmpString, tmpString);
28
29 tmpString = Util::toUpper(tmpString);
30 if (tmpString != "TRUE" && tmpString != "FALSE")
32 "FM2DElectroStatic::FM2DElectroStatic",
33 "The third string on the first line of 2D field "
34 "maps has to be either TRUE or FALSE");
35
36 normalize_m = (tmpString == "TRUE");
37 }
38
39 if (tmpString == "ZX") {
40 swap_m = true;
41 parsing_passed =
42 parsing_passed
44 parsing_passed =
45 parsing_passed
47 } else if (tmpString == "XZ") {
48 swap_m = false;
49 parsing_passed =
50 parsing_passed
52 parsing_passed =
53 parsing_passed
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 =
62 parsing_passed && interpretLine<double, double>(file, tmpDouble, tmpDouble);
63 }
64
65 parsing_passed = parsing_passed && interpreteEOF(file);
66
67 file.close();
68 lines_read_m = 0;
69
70 if (!parsing_passed) {
72 zend_m = zbegin_m - 1e-3;
74 "FM2DElectroStatic::FM2DElectroStatic",
75 "An error occured when reading the fieldmap '" + Filename_m + "'");
76 } else {
77 // conversion from cm to m
82
85
86 // num spacings -> num grid points
89 }
90 } else {
91 noFieldmapWarning();
92 zbegin_m = 0.0;
93 zend_m = -1e-3;
94 }
95}
96
100
102 if (FieldstrengthEz_m == nullptr) {
103 // declare variables and allocate memory
104 std::ifstream in;
105 std::string tmpString;
106 double Ezmax = 0.0;
107
110
111 // read in and parse field map
112 in.open(Filename_m.c_str());
113 getLine(in, tmpString);
114 getLine(in, tmpString);
115 getLine(in, tmpString);
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) {
121 in, FieldstrengthEr_m[i + j * num_gridpz_m],
123 }
124 if (std::abs(FieldstrengthEz_m[i]) > Ezmax)
125 Ezmax = std::abs(FieldstrengthEz_m[i]);
126 }
127 } else {
128 for (int j = 0; j < num_gridpr_m; ++j) {
129 for (int i = 0; i < num_gridpz_m; ++i) {
131 in, FieldstrengthEz_m[i + j * num_gridpz_m],
133 }
134 }
135
136 for (int i = 0; i < num_gridpz_m; ++i) {
137 if (std::abs(FieldstrengthEz_m[i]) > Ezmax) {
138 Ezmax = std::abs(FieldstrengthEz_m[i]);
139 }
140 }
141 }
142 in.close();
143
144 if (!normalize_m)
145 Ezmax = 1.0;
146
147 // conversion MV/m to V/m and normalization to Ez_max = 1 MV/m
148 for (int i = 0; i < num_gridpr_m * num_gridpz_m; ++i) {
151 }
152 *ippl::Info << typeset_msg("read in field map '" + Filename_m + "'", "info") << "\n"
153 << endl;
154 }
155}
156
158 if (FieldstrengthEz_m != nullptr) {
159 delete[] FieldstrengthEz_m;
160 FieldstrengthEz_m = nullptr;
161 delete[] FieldstrengthEr_m;
162 FieldstrengthEr_m = nullptr;
163
164 *ippl::Info << typeset_msg("freed field map '" + Filename_m + "'", "info") << "\n" << endl;
165 }
166}
167
170 // do bi-linear interpolation
171 const double RR = std::sqrt(R(0) * R(0) + R(1) * R(1));
172
173 const int indexr = (int)std::floor(RR / hr_m);
174 const double leverr = (RR / hr_m) - indexr;
175
176 const int indexz = (int)std::floor((R(2) - zbegin_m) / hz_m);
177 const double leverz = (R(2) - zbegin_m) / hz_m - indexz;
178
179 if ((indexz < 0) || (indexz + 2 > num_gridpz_m))
180 return false;
181 if (indexr + 2 > num_gridpr_m)
182 return true;
183
184 const int index1 = indexz + indexr * num_gridpz_m;
185 const int index2 = index1 + num_gridpz_m;
186 const double EfieldR = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEr_m[index1]
187 + leverz * (1.0 - leverr) * FieldstrengthEr_m[index1 + 1]
188 + (1.0 - leverz) * leverr * FieldstrengthEr_m[index2]
189 + leverz * leverr * FieldstrengthEr_m[index2 + 1];
190
191 const double EfieldZ = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEz_m[index1]
192 + leverz * (1.0 - leverr) * FieldstrengthEz_m[index1 + 1]
193 + (1.0 - leverz) * leverr * FieldstrengthEz_m[index2]
194 + leverz * leverr * FieldstrengthEz_m[index2 + 1];
195
196 if (RR > 1e-10) {
197 E(0) += EfieldR * R(0) / RR;
198 E(1) += EfieldR * R(1) / RR;
199 }
200 E(2) += EfieldZ;
201 return false;
202}
203
206 const DiffDirection& /*dir*/) const {
207 return false;
208}
209
210void FM2DElectroStatic::getFieldDimensions(double& zBegin, double& zEnd) const {
211 zBegin = zbegin_m;
212 zEnd = zend_m;
213}
215 double& /*xIni*/, double& /*xFinal*/, double& /*yIni*/, double& /*yFinal*/, double& /*zIni*/,
216 double& /*zFinal*/) const {
217}
218
220 if (swap_m)
221 swap_m = false;
222 else
223 swap_m = true;
224}
225
226void FM2DElectroStatic::getInfo(Inform* msg) {
227 (*msg) << Filename_m << " (2D electrostatic); zini= " << zbegin_m << " m; zfinal= " << zend_m
228 << " m;" << endl;
229}
230
232 return 0.0;
233}
234
235void FM2DElectroStatic::setFrequency(double /*freq*/) {
236 ;
237}
ippl::Vector< T, Dim > Vector_t
@ T2DElectroStatic
Definition Fieldmap.h:27
DiffDirection
Definition Fieldmap.h:55
constexpr double cm2m
Definition Units.h:35
constexpr double MVpm2Vpm
Definition Units.h:128
std::string toUpper(const std::string &str)
Definition Util.cpp:145
MapType Type
Definition Fieldmap.h:118
bool interpreteEOF(std::ifstream &in)
Definition Fieldmap.cpp:516
void disableFieldmapWarning()
Definition Fieldmap.cpp:569
bool normalize_m
Definition Fieldmap.h:123
int lines_read_m
Definition Fieldmap.h:121
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition Fieldmap.cpp:607
std::string Filename_m
Definition Fieldmap.h:120
bool interpretLine(std::ifstream &in, S &value, const bool &file_length_known=true)
void getLine(std::ifstream &in, std::string &buffer)
Definition Fieldmap.h:124
virtual bool getFieldstrength(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B) const
virtual double getFrequency() const
virtual bool getFieldDerivative(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B, const DiffDirection &dir) const
FM2DElectroStatic(std::string aFilename)
virtual void setFrequency(double freq)
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
virtual void getInfo(Inform *msg)