OPAL (Object Oriented Parallel Accelerator Library)  2024.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 
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 &&
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 {
65  xbegin_m *= Units::cm2m;
66  xend_m *= Units::cm2m;
67  ybegin_m *= Units::cm2m;
68  yend_m *= Units::cm2m;
69  zbegin_m *= Units::cm2m;
70  zend_m *= Units::cm2m;
71 
72  hx_m = (xend_m - xbegin_m) / num_gridpx_m;
73  hy_m = (yend_m - ybegin_m) / num_gridpy_m;
74  hz_m = (zend_m - zbegin_m) / num_gridpz_m;
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 
92 FM3DMagnetoStatic _FM3DMagnetoStatic::create(const std::string& filename)
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) {
117  interpretLine<double>(in,
118  FieldstrengthBx_m[ii],
119  FieldstrengthBy_m[ii],
120  FieldstrengthBz_m[ii]);
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 
169  B += interpolateTrilinearly(R);
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 
208 double _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 
219 bool _FM3DMagnetoStatic::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
220  return false;
221 }
222 
223 void _FM3DMagnetoStatic::getFieldDimensions(double &zBegin, double &zEnd) const {
224  zBegin = zbegin_m;
225  zEnd = zend_m;
226 }
227 void _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 }
MapType Type
Definition: Fieldmap.h:115
DiffDirection
Definition: Fieldmap.h:55
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
virtual void getInfo(Inform *msg)
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
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
constexpr double cm2m
Definition: Units.h:35
_FM3DMagnetoStatic(const std::string &filename)
virtual bool isInside(const Vector_t &r) const
std::shared_ptr< _FM3DMagnetoStatic > FM3DMagnetoStatic
Definition: Definitions.h:69
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:649
std::string toUpper(const std::string &str)
Definition: Util.cpp:147
#define INFOMSG(msg)
Definition: IpplInfo.h:348
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
bool interpreteEOF(std::ifstream &in)
Definition: Fieldmap.cpp:555
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
unsigned int num_gridpx_m
Vector_t interpolateTrilinearly(const Vector_t &X) const
bool normalize_m
Definition: Fieldmap.h:121
Definition: Inform.h:42
#define X(arg)
Definition: fftpack.cpp:112
static FM3DMagnetoStatic create(const std::string &filename)
void getLine(std::ifstream &in, std::string &buffer)
Definition: Fieldmap.h:122
Inform & level3(Inform &inf)
Definition: Inform.cpp:47
constexpr double e
The value of .
Definition: Physics.h:39
std::string Filename_m
Definition: Fieldmap.h:118
IndexTriplet getIndex(const Vector_t &X) const
virtual void readMap()
unsigned int num_gridpy_m
unsigned int num_gridpz_m