OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
FM3DMagnetoStatic.cpp
Go to the documentation of this file.
2 #include "Fields/Fieldmap.hpp"
4 #include "Utilities/Util.h"
5 #include "Physics/Physics.h"
6 
7 #include <fstream>
8 #include <ios>
9 
10 extern Inform *gmsg;
11 
12 using namespace std;
13 using Physics::mu_0;
14 
15 FM3DMagnetoStatic::FM3DMagnetoStatic(std::string aFilename):
16  Fieldmap(aFilename),
17  FieldstrengthBz_m(NULL),
18  FieldstrengthBx_m(NULL),
19  FieldstrengthBy_m(NULL) {
20 
21  std::string tmpString;
22  double tmpDouble;
23 
25  ifstream file(Filename_m.c_str());
26 
27  if(file.good()) {
28  bool parsing_passed = true;
29  try {
30  interpreteLine<std::string>(file, tmpString);
31  } catch (GeneralClassicException &e) {
32  parsing_passed = interpreteLine<std::string, std::string>(file, tmpString, tmpString);
33 
34  tmpString = Util::toUpper(tmpString);
35  if (tmpString != "TRUE" &&
36  tmpString != "FALSE")
37  throw GeneralClassicException("FM3DMagnetoStatic::FM3DMagnetoStatic",
38  "The second string on the first line of 3D field "
39  "maps has to be either TRUE or FALSE");
40 
41  normalize_m = (tmpString == "TRUE");
42  }
43  parsing_passed = parsing_passed &&
44  interpreteLine<double, double, unsigned int>(file, xbegin_m, xend_m, num_gridpx_m);
45  parsing_passed = parsing_passed &&
46  interpreteLine<double, double, unsigned int>(file, ybegin_m, yend_m, num_gridpy_m);
47  parsing_passed = parsing_passed &&
48  interpreteLine<double, double, unsigned int>(file, zbegin_m, zend_m, num_gridpz_m);
49 
50  for(unsigned long i = 0; (i < (num_gridpz_m + 1) * (num_gridpx_m + 1) * (num_gridpy_m + 1)) && parsing_passed; ++ i) {
51  parsing_passed = parsing_passed &&
52  interpreteLine<double>(file,
53  tmpDouble,
54  tmpDouble,
55  tmpDouble);
56  }
57 
58  parsing_passed = parsing_passed &&
59  interpreteEOF(file);
60 
61  file.close();
62 
63  if(!parsing_passed) {
65  zend_m = zbegin_m - 1e-3;
66  throw GeneralClassicException("FM3DMagnetoStatic::FM3DMagnetoStatic",
67  "An error occured when reading the fieldmap '" + Filename_m + "'");
68  } else {
69  xbegin_m /= 100.0;
70  xend_m /= 100.0;
71  ybegin_m /= 100.0;
72  yend_m /= 100.0;
73  zbegin_m /= 100.0;
74  zend_m /= 100.0;
75 
76  hx_m = (xend_m - xbegin_m) / num_gridpx_m;
77  hy_m = (yend_m - ybegin_m) / num_gridpy_m;
78  hz_m = (zend_m - zbegin_m) / num_gridpz_m;
79 
80  ++ num_gridpx_m;
81  ++ num_gridpy_m;
82  ++ num_gridpz_m;
83 
84  }
85  } else {
87  zbegin_m = 0.0;
88  zend_m = -1e-3;
89  }
90 }
91 
92 
94  freeMap();
95 }
96 
98  if(FieldstrengthBz_m == NULL) {
99 
100  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  interpreteLine<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::floor(-xbegin_m / hx_m + 0.5));
131  unsigned int centerY = static_cast<unsigned int>(std::floor(-ybegin_m / hy_m + 0.5));
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 != NULL) {
154  delete[] FieldstrengthBz_m;
155  delete[] FieldstrengthBx_m;
156  delete[] FieldstrengthBy_m;
157 
158  FieldstrengthBz_m = NULL;
159  FieldstrengthBx_m = NULL;
160  FieldstrengthBy_m = NULL;
161 
162  INFOMSG(level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << "\n"
163  << endl);
164  }
165 }
166 
168  if (isInside(R))
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 
220  return false;
221 }
222 
223 void FM3DMagnetoStatic::getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const {
224  zBegin = zbegin_m;
225  zEnd = zend_m;
226  rBegin = xbegin_m;
227  rEnd = xend_m;
228 }
229 void FM3DMagnetoStatic::getFieldDimensions(double &xIni, double &xFinal, double &yIni, double &yFinal, double &zIni, double &zFinal) const {}
230 
232 
234  (*msg) << Filename_m << " (3D magnetostatic) "
235  << " xini= " << xbegin_m << " xfinal= " << xend_m
236  << " yini= " << ybegin_m << " yfinal= " << yend_m
237  << " zini= " << zbegin_m << " zfinal= " << zend_m << " (m) " << endl;
238 }
Vector_t interpolateTrilinearly(const Vector_t &X) const
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
constexpr double e
The value of .
Definition: Physics.h:40
DiffDirection
Definition: Fieldmap.h:54
Definition: rbendmap.h:8
Inform * gmsg
Definition: Main.cpp:21
std::string toUpper(const std::string &str)
Definition: Util.cpp:130
void disableFieldmapWarning()
Definition: Fieldmap.cpp:610
virtual void getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const
unsigned int num_gridpy_m
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:647
void noFieldmapWarning()
Definition: Fieldmap.cpp:618
bool normalize_m
Definition: Fieldmap.h:113
virtual void readMap()
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
constexpr double mu_0
The permeability of vacuum in Vs/Am.
Definition: Physics.h:55
bool interpreteEOF(std::ifstream &in)
Definition: Fieldmap.cpp:547
#define INFOMSG(msg)
Definition: IpplInfo.h:397
virtual void freeMap()
IndexTriplet getIndex(const Vector_t &X) const
unsigned int num_gridpx_m
virtual void getInfo(Inform *msg)
double getWeightedData(double *data, const IndexTriplet &idx, unsigned short corner) const
void getLine(std::ifstream &in, std::string &buffer)
Definition: Fieldmap.h:198
std::string Filename_m
Definition: Fieldmap.h:110
FM3DMagnetoStatic(std::string aFilename)
virtual bool isInside(const Vector_t &r) const
MapType Type
Definition: Fieldmap.h:107
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
Inform & level3(Inform &inf)
Definition: Inform.cpp:47
Definition: Inform.h:41
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:816
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
unsigned int num_gridpz_m