OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
Astra1DMagnetoStatic_fast.cpp
Go to the documentation of this file.
3 #include "Utilities/Util.h"
4 #include "Physics/Physics.h"
5 
6 #include <fstream>
7 #include <ios>
8 
10  _Astra1D_fast(filename)
11 {
12  numHeaderLines_m = 2;
13 
14  onAxisField_m = nullptr;
15 
17 
18  // open field map, parse it and disable element on error
19  std::ifstream file(Filename_m.c_str());
20 
21  if(!file.good()) {
23  zbegin_m = 0.0;
24  zend_m = -1e-3;
25  }
26 
27  bool parsing_passed = readFileHeader(file);
28  parsing_passed = parsing_passed && determineNumSamplingPoints(file);
29  file.close();
30 
31  if(!parsing_passed) {
33  zend_m = zbegin_m - 1e-3;
34  throw GeneralClassicException("_Astra1DMagnetoStatic_fast::_Astra1DMagnetoStatic_fast",
35  "An error occured when reading the fieldmap '" + Filename_m + "'");
36  }
37  hz_m = (zend_m - zbegin_m) / (num_gridpz_m - 1);
38  length_m = 2.0 * num_gridpz_m * hz_m;
39 }
40 
42  freeMap();
43 }
44 
46 {
48 }
49 
51  if(onAxisField_m == nullptr) {
52  onAxisField_m = new double[num_gridpz_m];
53  zvals_m = new double[num_gridpz_m];
54 
55  std::ifstream file(Filename_m.c_str());
56  int accuracy = stripFileHeader(file);
57  double maxBz = readFieldData(file);
58  file.close();
59 
60  normalizeFieldData(maxBz);
61 
62  std::vector<double> zvals = getEvenlyDistributedSamplingPoints();
63  std::vector<double> evenFieldSampling = interpolateFieldData(zvals);
64  std::vector<double> fourierCoefs = computeFourierCoefficients(accuracy, evenFieldSampling);
65 
66  computeFieldDerivatives(fourierCoefs, accuracy);
67 
68  checkMap(accuracy,
69  length_m,
70  zvals,
71  fourierCoefs,
73  onAxisAccel_m[0]);
74 
75 
76  INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info") << endl);
77  }
78 }
79 
81  // do fourier interpolation in z-direction
82  const double RR2 = R(0) * R(0) + R(1) * R(1);
83 
84  double bz = gsl_spline_eval(onAxisInterpolants_m[0], R(2) - zbegin_m, onAxisAccel_m[0]);
85  double bzp = gsl_spline_eval(onAxisInterpolants_m[1], R(2) - zbegin_m, onAxisAccel_m[1]);
86  double bzpp = gsl_spline_eval(onAxisInterpolants_m[2], R(2) - zbegin_m, onAxisAccel_m[2]);
87  double bzppp = gsl_spline_eval(onAxisInterpolants_m[3], R(2) - zbegin_m, onAxisAccel_m[3]);
88 
89  // expand to off-axis
90  const double BfieldR = -bzp / 2. + bzppp / 16. * RR2;
91 
92  B(0) += BfieldR * R(0);
93  B(1) += BfieldR * R(1);
94  B(2) += bz - bzpp * RR2 / 4.;
95  return false;
96 }
97 
98 bool _Astra1DMagnetoStatic_fast::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
99  return false;
100 }
101 
102 void _Astra1DMagnetoStatic_fast::getFieldDimensions(double &zBegin, double &zEnd) const {
103  zBegin = zbegin_m;
104  zEnd = zend_m;
105 }
106 
107 void _Astra1DMagnetoStatic_fast::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/, double &/*yIni*/, double &/*yFinal*/, double &/*zIni*/, double &/*zFinal*/) const {}
108 
110 { }
111 
113  (*msg) << Filename_m << " (1D magnetostatic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
114 }
115 
117  return 0.0;
118 }
119 
121 { }
122 
124  std::string tmpString;
125  int tmpInt;
126 
127  bool passed;
128  try {
129  passed = interpretLine<std::string, int>(file, tmpString, tmpInt);
130  } catch (GeneralClassicException &e) {
131  passed = interpretLine<std::string, int, std::string>(file, tmpString, tmpInt, tmpString);
132 
133  tmpString = Util::toUpper(tmpString);
134  if (tmpString != "TRUE" &&
135  tmpString != "FALSE")
136  throw GeneralClassicException("Astra1DDynamic_fast::readFileHeader",
137  "The third string on the first line of 1D field "
138  "maps has to be either TRUE or FALSE");
139 
140  normalize_m = (tmpString == "TRUE");
141  }
142 
143  return passed;
144 }
145 
147  std::string tmpString;
148  int accuracy;
149 
150  try {
151  interpretLine<std::string, int>(file, tmpString, accuracy);
152  } catch (GeneralClassicException &e) {
153  interpretLine<std::string, int, std::string>(file, tmpString, accuracy, tmpString);
154  }
155 
156  return accuracy;
157 }
MapType Type
Definition: Fieldmap.h:115
int numHeaderLines_m
Definition: Astra1D_fast.h:49
virtual void setFrequency(double freq)
DiffDirection
Definition: Fieldmap.h:55
void noFieldmapWarning()
Definition: Fieldmap.cpp:618
double length_m
Definition: Astra1D_fast.h:46
double zbegin_m
Definition: Astra1D_fast.h:44
_Astra1DMagnetoStatic_fast(const std::string &filename)
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:649
std::shared_ptr< _Astra1DMagnetoStatic_fast > Astra1DMagnetoStatic_fast
gsl_spline * onAxisInterpolants_m[4]
Definition: Astra1D_fast.h:39
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
std::vector< double > interpolateFieldData(std::vector< double > &samplingPoints)
bool readFileHeader(std::ifstream &file)
double * onAxisField_m
Definition: Astra1D_fast.h:37
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
int stripFileHeader(std::ifstream &file)
virtual void freeMap()
void normalizeFieldData(double maxEz)
double readFieldData(std::ifstream &file)
bool normalize_m
Definition: Fieldmap.h:121
Definition: Inform.h:42
static Astra1DMagnetoStatic_fast create(const std::string &filename)
std::vector< double > getEvenlyDistributedSamplingPoints()
void disableFieldmapWarning()
Definition: Fieldmap.cpp:610
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
gsl_interp_accel * onAxisAccel_m[4]
Definition: Astra1D_fast.h:40
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
void computeFieldDerivatives(std::vector< double > &fourierComponents, int accuracy)
std::vector< double > computeFourierCoefficients(int accuracy, std::vector< double > &evenSampling)
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
double * zvals_m
Definition: Astra1D_fast.h:38
void checkMap(unsigned int accuracy, std::pair< double, double > fieldDimensions, double deltaZ, const std::vector< double > &fourierCoefficients, gsl_spline *splineCoefficients, gsl_interp_accel *splineAccelerator)
Definition: Fieldmap.cpp:448
bool determineNumSamplingPoints(std::ifstream &file)