OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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
12_FM3DMagnetoStatic::_FM3DMagnetoStatic(const std::string& filename):
13 _Fieldmap(filename),
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 &&
43 parsing_passed = parsing_passed &&
45 parsing_passed = parsing_passed &&
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 &&
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
91
93{
94 return FM3DMagnetoStatic(new _FM3DMagnetoStatic(filename));
95}
96
98 if(FieldstrengthBz_m == nullptr) {
99
100 std::ifstream in(Filename_m.c_str());
101 std::string tmpString;
102 const size_t totalSize = num_gridpx_m * num_gridpy_m * num_gridpz_m;
103
104 getLine(in, tmpString);
105 getLine(in, tmpString);
106 getLine(in, tmpString);
107 getLine(in, tmpString);
108
109 FieldstrengthBz_m = new double[totalSize];
110 FieldstrengthBx_m = new double[totalSize];
111 FieldstrengthBy_m = new double[totalSize];
112
113 long ii = 0;
114 for(unsigned int i = 0; i < num_gridpx_m; ++ i) {
115 for(unsigned int j = 0; j < num_gridpy_m; ++ j) {
116 for(unsigned int k = 0; k < num_gridpz_m; ++ k) {
121 ++ ii;
122 }
123 }
124 }
125 in.close();
126
127 if (normalize_m) {
128 double Bymax = 0.0;
129 // find maximum field
130 unsigned int centerX = static_cast<unsigned int>(std::round(-xbegin_m / hx_m));
131 unsigned int centerY = static_cast<unsigned int>(std::round(-ybegin_m / hy_m));
132 for(unsigned int k = 0; k < num_gridpz_m; ++ k) {
133 double By = FieldstrengthBy_m[getIndex(centerX, centerY, k)];
134 if(std::abs(By) > Bymax) {
135 Bymax = std::abs(By);
136 }
137 }
138
139 // normalize field
140 for(size_t i = 0; i < totalSize; ++ i) {
141 FieldstrengthBx_m[i] /= Bymax;
142 FieldstrengthBy_m[i] /= Bymax;
143 FieldstrengthBz_m[i] /= Bymax;
144 }
145 }
146
147 INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info") << "\n"
148 << endl);
149 }
150}
151
153 if(FieldstrengthBz_m != nullptr) {
154 delete[] FieldstrengthBz_m;
155 delete[] FieldstrengthBx_m;
156 delete[] FieldstrengthBy_m;
157
158 FieldstrengthBz_m = nullptr;
159 FieldstrengthBx_m = nullptr;
160 FieldstrengthBy_m = nullptr;
161 }
162}
163
165 if (!isInside(R)) {
166 return true;
167 }
168
170
171 return false;
172}
173
175 IndexTriplet idx = getIndex(X);
176 Vector_t B(0.0);
177
178 B(0) = (getWeightedData(FieldstrengthBx_m, idx, LX|LY|LZ) +
186
187 B(1) = (getWeightedData(FieldstrengthBy_m, idx, LX|LY|LZ) +
195
196 B(2) = (getWeightedData(FieldstrengthBz_m, idx, LX|LY|LZ) +
204
205 return B;
206}
207
208double _FM3DMagnetoStatic::getWeightedData(double *data, const IndexTriplet &idx, unsigned short corner) const {
209 unsigned short switchX = ((corner & HX) >> 2), switchY = ((corner & HY) >> 1), switchZ = (corner & HZ);
210 double factorX = 0.5 + (1 - 2 * switchX) * (0.5 - idx.weight(0));
211 double factorY = 0.5 + (1 - 2 * switchY) * (0.5 - idx.weight(1));
212 double factorZ = 0.5 + (1 - 2 * switchZ) * (0.5 - idx.weight(2));
213
214 unsigned long i = idx.i + switchX, j = idx.j + switchY, k = idx.k + switchZ;
215
216 return factorX * factorY * factorZ * data[getIndex(i, j, k)];
217}
218
219bool _FM3DMagnetoStatic::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
220 return false;
221}
222
223void _FM3DMagnetoStatic::getFieldDimensions(double &zBegin, double &zEnd) const {
224 zBegin = zbegin_m;
225 zEnd = zend_m;
226}
227void _FM3DMagnetoStatic::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/, double &/*yIni*/, double &/*yFinal*/, double &/*zIni*/, double &/*zFinal*/) const {}
228
230
232 (*msg) << Filename_m << " (3D magnetostatic) "
233 << " xini= " << xbegin_m << " xfinal= " << xend_m
234 << " yini= " << ybegin_m << " yfinal= " << yend_m
235 << " zini= " << zbegin_m << " zfinal= " << zend_m << " (m) " << endl;
236}
std::shared_ptr< _FM3DMagnetoStatic > FM3DMagnetoStatic
@ T3DMagnetoStatic
Definition Fieldmap.h:33
DiffDirection
Definition Fieldmap.h:55
#define X(arg)
Definition fftpack.cpp:106
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 cm2m
Definition Units.h:35
std::string toUpper(const std::string &str)
Definition Util.cpp:147
bool interpreteEOF(std::ifstream &in)
Definition Fieldmap.cpp:555
bool normalize_m
Definition Fieldmap.h:121
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition Fieldmap.cpp:649
void getLine(std::ifstream &in, std::string &buffer)
Definition Fieldmap.h:122
bool interpretLine(std::ifstream &in, S &value, const bool &file_length_known=true)
std::string Filename_m
Definition Fieldmap.h:118
MapType Type
Definition Fieldmap.h:116
unsigned int num_gridpx_m
double getWeightedData(double *data, const IndexTriplet &idx, unsigned short corner) const
IndexTriplet getIndex(const Vector_t &X) const
unsigned int num_gridpz_m
unsigned int num_gridpy_m
virtual void getInfo(Inform *msg)
static FM3DMagnetoStatic create(const std::string &filename)
_FM3DMagnetoStatic(const std::string &filename)
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
virtual bool isInside(const Vector_t &r) const
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
Vector_t interpolateTrilinearly(const Vector_t &X) const
Vektor< double, 3 > Vector_t