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