OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
FM3DMagnetoStatic.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
12 : Fieldmap(aFilename),
13 FieldstrengthBz_m(nullptr),
14 FieldstrengthBx_m(nullptr),
15 FieldstrengthBy_m(nullptr) {
16 std::string tmpString;
17 double tmpDouble;
18
20 std::ifstream file(Filename_m.c_str());
21
22 if (file.good()) {
23 bool parsing_passed = true;
24 try {
25 interpretLine<std::string>(file, tmpString);
26 } catch (GeneralClassicException& e) {
27 parsing_passed = interpretLine<std::string, std::string>(file, tmpString, tmpString);
28
29 tmpString = Util::toUpper(tmpString);
30 if (tmpString != "TRUE" && tmpString != "FALSE")
32 "FM3DMagnetoStatic::FM3DMagnetoStatic",
33 "The second string on the first line of 3D field "
34 "maps has to be either TRUE or FALSE");
35
36 normalize_m = (tmpString == "TRUE");
37 }
38
39 parsing_passed =
40 parsing_passed
42 parsing_passed =
43 parsing_passed
45 parsing_passed =
46 parsing_passed
48
49 for (unsigned long i = 0;
50 (i < (num_gridpz_m + 1) * (num_gridpx_m + 1) * (num_gridpy_m + 1)) && parsing_passed;
51 ++i) {
52 parsing_passed =
53 parsing_passed && interpretLine<double>(file, tmpDouble, tmpDouble, tmpDouble);
54 }
55
56 parsing_passed = parsing_passed && interpreteEOF(file);
57
58 file.close();
59
60 if (!parsing_passed) {
62 "FM3DMagnetoStatic::FM3DMagnetoStatic",
63 "An error occured when reading the fieldmap '" + Filename_m + "'");
64 } else {
71
75
79 }
80 } else {
82 "FM3DMagnetoStatic::FM3DMagnetoStatic",
83 "An error occured when reading the fieldmap '" + Filename_m + "'");
84 }
85}
86
90
92 if (FieldstrengthBz_m == nullptr) {
93 std::ifstream in(Filename_m.c_str());
94 std::string tmpString;
95 const size_t totalSize = num_gridpx_m * num_gridpy_m * num_gridpz_m;
96
97 getLine(in, tmpString);
98 getLine(in, tmpString);
99 getLine(in, tmpString);
100 getLine(in, tmpString);
101
102 FieldstrengthBz_m = new double[totalSize];
103 FieldstrengthBx_m = new double[totalSize];
104 FieldstrengthBy_m = new double[totalSize];
105
106 long ii = 0;
107 for (unsigned int i = 0; i < num_gridpx_m; ++i) {
108 for (unsigned int j = 0; j < num_gridpy_m; ++j) {
109 for (unsigned int k = 0; k < num_gridpz_m; ++k) {
112 ++ii;
113 }
114 }
115 }
116 in.close();
117
118 if (normalize_m) {
119 double Bymax = 0.0;
120 // find maximum field
121 unsigned int centerX = static_cast<unsigned int>(std::round(-xbegin_m / hx_m));
122 unsigned int centerY = static_cast<unsigned int>(std::round(-ybegin_m / hy_m));
123 for (unsigned int k = 0; k < num_gridpz_m; ++k) {
124 double By = FieldstrengthBy_m[getIndex(centerX, centerY, k)];
125 if (std::abs(By) > Bymax) {
126 Bymax = std::abs(By);
127 }
128 }
129
130 // normalize field
131 for (size_t i = 0; i < totalSize; ++i) {
132 FieldstrengthBx_m[i] /= Bymax;
133 FieldstrengthBy_m[i] /= Bymax;
134 FieldstrengthBz_m[i] /= Bymax;
135 }
136 }
137
138 *ippl::Info << level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info")
139 << "\n"
140 << endl;
141 }
142}
143
145 if (FieldstrengthBz_m != nullptr) {
146 delete[] FieldstrengthBz_m;
147 delete[] FieldstrengthBx_m;
148 delete[] FieldstrengthBy_m;
149
150 FieldstrengthBz_m = nullptr;
151 FieldstrengthBx_m = nullptr;
152 FieldstrengthBy_m = nullptr;
153
154 *ippl::Info << level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << "\n"
155 << endl;
156 }
157}
158
161 if (!isInside(R)) {
162 return true;
163 }
164
166
167 return false;
168}
169
171 IndexTriplet idx = getIndex(X);
172 Vector_t<double, 3> B(0.0);
173
174 B(0) =
183
184 B(1) =
193
194 B(2) =
203
204 return B;
205}
206
208 double* data, const IndexTriplet& idx, unsigned short corner) const {
209 unsigned short switchX = ((corner & HX) >> 2), switchY = ((corner & HY) >> 1),
210 switchZ = (corner & HZ);
211 double factorX = 0.5 + (1 - 2 * switchX) * (0.5 - idx.weight(0));
212 double factorY = 0.5 + (1 - 2 * switchY) * (0.5 - idx.weight(1));
213 double factorZ = 0.5 + (1 - 2 * switchZ) * (0.5 - idx.weight(2));
214
215 unsigned long i = idx.i + switchX, j = idx.j + switchY, k = idx.k + switchZ;
216
217 return factorX * factorY * factorZ * data[getIndex(i, j, k)];
218}
219
222 const DiffDirection& /*dir*/) const {
223 return false;
224}
225
226void FM3DMagnetoStatic::getFieldDimensions(double& zBegin, double& zEnd) const {
227 zBegin = zbegin_m;
228 zEnd = zend_m;
229}
231 double& /*xIni*/, double& /*xFinal*/, double& /*yIni*/, double& /*yFinal*/, double& /*zIni*/,
232 double& /*zFinal*/) const {
233}
234
237
238void FM3DMagnetoStatic::getInfo(Inform* msg) {
239 (*msg) << Filename_m << " (3D magnetostatic) "
240 << " xini= " << xbegin_m << " xfinal= " << xend_m << " yini= " << ybegin_m
241 << " yfinal= " << yend_m << " zini= " << zbegin_m << " zfinal= " << zend_m << " (m) "
242 << endl;
243}
ippl::Vector< T, Dim > Vector_t
@ T3DMagnetoStatic
Definition Fieldmap.h:33
DiffDirection
Definition Fieldmap.h:55
constexpr double cm2m
Definition Units.h:35
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
bool normalize_m
Definition Fieldmap.h:123
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 bool isInside(const Vector_t< double, 3 > &r) const
unsigned int num_gridpz_m
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
IndexTriplet getIndex(const Vector_t< double, 3 > &X) const
double getWeightedData(double *data, const IndexTriplet &idx, unsigned short corner) const
unsigned int num_gridpx_m
Vector_t< double, 3 > interpolateTrilinearly(const Vector_t< double, 3 > &X) const
virtual bool getFieldDerivative(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B, const DiffDirection &dir) const
virtual void getInfo(Inform *msg)
FM3DMagnetoStatic(std::string aFilename)
unsigned int num_gridpy_m