OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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), onAxisAccel_m[0]);
80  double bzp = gsl_spline_eval(onAxisInterpolants_m[1], R(2), onAxisAccel_m[1]);
81  double bzpp = gsl_spline_eval(onAxisInterpolants_m[2], R(2), onAxisAccel_m[2]);
82  double bzppp = gsl_spline_eval(onAxisInterpolants_m[3], R(2), 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 
94  return false;
95 }
96 
97 void Astra1DMagnetoStatic_fast::getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) 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 = interpreteLine<std::string, int>(file, tmpString, tmpInt);
125  } catch (GeneralClassicException &e) {
126  passed = interpreteLine<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  interpreteLine<std::string, int>(file, tmpString, accuracy);
147  } catch (GeneralClassicException &e) {
148  interpreteLine<std::string, int, std::string>(file, tmpString, accuracy, tmpString);
149  }
150 
151  return accuracy;
152 }
double zend_m
Definition: Astra1D_fast.h:44
constexpr double e
The value of .
Definition: Physics.h:40
int stripFileHeader(std::ifstream &file)
gsl_spline * onAxisInterpolants_m[4]
Definition: Astra1D_fast.h:38
double length_m
Definition: Astra1D_fast.h:45
DiffDirection
Definition: Fieldmap.h:54
virtual void freeMap()
std::string toUpper(const std::string &str)
Definition: Util.cpp:130
void disableFieldmapWarning()
Definition: Fieldmap.cpp:610
double * onAxisField_m
Definition: Astra1D_fast.h:36
std::vector< double > getEvenlyDistributedSamplingPoints()
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:647
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
virtual double getFrequency() const
void noFieldmapWarning()
Definition: Fieldmap.cpp:618
bool determineNumSamplingPoints(std::ifstream &file)
bool normalize_m
Definition: Fieldmap.h:113
virtual void getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const
bool readFileHeader(std::ifstream &file)
std::vector< double > interpolateFieldData(std::vector< double > &samplingPoints)
double readFieldData(std::ifstream &file)
#define INFOMSG(msg)
Definition: IpplInfo.h:397
double * zvals_m
Definition: Astra1D_fast.h:37
gsl_interp_accel * onAxisAccel_m[4]
Definition: Astra1D_fast.h:39
void computeFieldDerivatives(std::vector< double > &fourierComponents, int accuracy)
double zbegin_m
Definition: Astra1D_fast.h:43
std::vector< double > computeFourierCoefficients(int accuracy, std::vector< double > &evenSampling)
std::string Filename_m
Definition: Fieldmap.h:110
int numHeaderLines_m
Definition: Astra1D_fast.h:48
MapType Type
Definition: Fieldmap.h:107
Inform & level3(Inform &inf)
Definition: Inform.cpp:47
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
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:443
Definition: Inform.h:41
void normalizeFieldData(double maxEz)
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
Astra1DMagnetoStatic_fast(std::string aFilename)
virtual void setFrequency(double freq)