OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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(aFilename)
11 {
12  numHeaderLines_m = 2;
13 
14  onAxisField_m = NULL;
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  if(onAxisField_m == NULL) {
47  onAxisField_m = new double[num_gridpz_m];
48  zvals_m = new double[num_gridpz_m];
49 
50  std::ifstream file(Filename_m.c_str());
51  int accuracy = stripFileHeader(file);
52  double maxBz = readFieldData(file);
53  file.close();
54 
55  normalizeFieldData(maxBz);
56 
57  std::vector<double> zvals = getEvenlyDistributedSamplingPoints();
58  std::vector<double> evenFieldSampling = interpolateFieldData(zvals);
59  std::vector<double> fourierCoefs = computeFourierCoefficients(accuracy, evenFieldSampling);
60 
61  computeFieldDerivatives(fourierCoefs, accuracy);
62 
63  checkMap(accuracy,
64  length_m,
65  zvals,
66  fourierCoefs,
68  onAxisAccel_m[0]);
69 
70 
71  INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info") << endl);
72  }
73 }
74 
76  // do fourier interpolation in z-direction
77  const double RR2 = R(0) * R(0) + R(1) * R(1);
78 
79  double bz = gsl_spline_eval(onAxisInterpolants_m[0], R(2) - zbegin_m, onAxisAccel_m[0]);
80  double bzp = gsl_spline_eval(onAxisInterpolants_m[1], R(2) - zbegin_m, onAxisAccel_m[1]);
81  double bzpp = gsl_spline_eval(onAxisInterpolants_m[2], R(2) - zbegin_m, onAxisAccel_m[2]);
82  double bzppp = gsl_spline_eval(onAxisInterpolants_m[3], R(2) - zbegin_m, onAxisAccel_m[3]);
83 
84  // expand to off-axis
85  const double BfieldR = -bzp / 2. + bzppp / 16. * RR2;
86 
87  B(0) += BfieldR * R(0);
88  B(1) += BfieldR * R(1);
89  B(2) += bz - bzpp * RR2 / 4.;
90  return false;
91 }
92 
93 bool Astra1DMagnetoStatic_fast::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
94  return false;
95 }
96 
97 void Astra1DMagnetoStatic_fast::getFieldDimensions(double &zBegin, double &zEnd) const {
98  zBegin = zbegin_m;
99  zEnd = zend_m;
100 }
101 
102 void Astra1DMagnetoStatic_fast::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/, double &/*yIni*/, double &/*yFinal*/, double &/*zIni*/, double &/*zFinal*/) const {}
103 
105 { }
106 
108  (*msg) << Filename_m << " (1D magnetostatic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
109 }
110 
112  return 0.0;
113 }
114 
116 { }
117 
118 bool Astra1DMagnetoStatic_fast::readFileHeader(std::ifstream &file) {
119  std::string tmpString;
120  int tmpInt;
121 
122  bool passed;
123  try {
124  passed = interpretLine<std::string, int>(file, tmpString, tmpInt);
125  } catch (GeneralClassicException &e) {
126  passed = interpretLine<std::string, int, std::string>(file, tmpString, tmpInt, tmpString);
127 
128  tmpString = Util::toUpper(tmpString);
129  if (tmpString != "TRUE" &&
130  tmpString != "FALSE")
131  throw GeneralClassicException("Astra1DDynamic_fast::readFileHeader",
132  "The third string on the first line of 1D field "
133  "maps has to be either TRUE or FALSE");
134 
135  normalize_m = (tmpString == "TRUE");
136  }
137 
138  return passed;
139 }
140 
142  std::string tmpString;
143  int accuracy;
144 
145  try {
146  interpretLine<std::string, int>(file, tmpString, accuracy);
147  } catch (GeneralClassicException &e) {
148  interpretLine<std::string, int, std::string>(file, tmpString, accuracy, tmpString);
149  }
150 
151  return accuracy;
152 }
153 
@ TAstraMagnetoStatic
Definition: Fieldmap.h:21
DiffDirection
Definition: Fieldmap.h:54
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
std::string toUpper(const std::string &str)
Definition: Util.cpp:132
bool determineNumSamplingPoints(std::ifstream &file)
virtual void freeMap()
double zend_m
Definition: Astra1D_fast.h:44
double readFieldData(std::ifstream &file)
void computeFieldDerivatives(std::vector< double > &fourierComponents, int accuracy)
gsl_spline * onAxisInterpolants_m[4]
Definition: Astra1D_fast.h:38
int numHeaderLines_m
Definition: Astra1D_fast.h:48
double length_m
Definition: Astra1D_fast.h:45
std::vector< double > getEvenlyDistributedSamplingPoints()
double * onAxisField_m
Definition: Astra1D_fast.h:36
void normalizeFieldData(double maxEz)
double zbegin_m
Definition: Astra1D_fast.h:43
double * zvals_m
Definition: Astra1D_fast.h:37
std::vector< double > computeFourierCoefficients(int accuracy, std::vector< double > &evenSampling)
gsl_interp_accel * onAxisAccel_m[4]
Definition: Astra1D_fast.h:39
std::vector< double > interpolateFieldData(std::vector< double > &samplingPoints)
int stripFileHeader(std::ifstream &file)
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
virtual void setFrequency(double freq)
bool readFileHeader(std::ifstream &file)
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
Astra1DMagnetoStatic_fast(std::string aFilename)
MapType Type
Definition: Fieldmap.h:114
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:451
void disableFieldmapWarning()
Definition: Fieldmap.cpp:613
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 noFieldmapWarning()
Definition: Fieldmap.cpp:621
Definition: Inform.h:42