OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Astra1DElectroStatic_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  numHeaderLines_m = 2;
12 
13  onAxisField_m = NULL;
14 
16 
17  // open field map, parse it and disable element on error
18  std::ifstream file(Filename_m.c_str());
19  if(!file.good()) {
21  zbegin_m = 0.0;
22  zend_m = -1e-3;
23  }
24 
25  bool parsing_passed = readFileHeader(file);
26  parsing_passed = parsing_passed && determineNumSamplingPoints(file);
27  file.close();
28 
29  if(!parsing_passed && !file.eof()) {
31  zend_m = zbegin_m - 1e-3;
32  throw GeneralClassicException("Astra1DElectroStatic_fast::Astra1DElectroStatic_fast",
33  "An error occured when reading the fieldmap '" + Filename_m + "'");
34  }
35  hz_m = (zend_m - zbegin_m) / (num_gridpz_m - 1);
36  length_m = 2.0 * num_gridpz_m * hz_m;
37 }
38 
40  freeMap();
41 }
42 
44  if(onAxisField_m == NULL) {
45  // declare variables and allocate memory
46 
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 maxEz = readFieldData(file);
53  file.close();
54 
55  normalizeFieldData(maxEz * 1e-6);
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 ez = gsl_spline_eval(onAxisInterpolants_m[0], R(2) - zbegin_m, onAxisAccel_m[0]);
80  double ezp = gsl_spline_eval(onAxisInterpolants_m[1], R(2) - zbegin_m, onAxisAccel_m[1]);
81  double ezpp = gsl_spline_eval(onAxisInterpolants_m[2], R(2) - zbegin_m, onAxisAccel_m[2]);
82  double ezppp = gsl_spline_eval(onAxisInterpolants_m[3], R(2) - zbegin_m, onAxisAccel_m[3]);
83 
84  // expand to off-axis
85  const double EfieldR = -ezp / 2. + ezppp / 16. * RR2;
86 
87  E(0) += EfieldR * R(0);
88  E(1) += EfieldR * R(1);
89  E(2) += ez - ezpp * RR2 / 4.;
90  return false;
91 }
92 
93 bool Astra1DElectroStatic_fast::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
94  return false;
95 }
96 
97 void Astra1DElectroStatic_fast::getFieldDimensions(double &zBegin, double &zEnd) const {
98  zBegin = zbegin_m;
99  zEnd = zend_m;
100 }
101 void Astra1DElectroStatic_fast::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/, double &/*yIni*/, double &/*yFinal*/, double &/*zIni*/, double &/*zFinal*/) const {}
102 
104 { }
105 
107  (*msg) << Filename_m << " (1D electrostatic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
108 }
109 
111  return 0.0;
112 }
113 
115 { }
116 
117 bool Astra1DElectroStatic_fast::readFileHeader(std::ifstream &file) {
118  std::string tmpString;
119  int tmpInt;
120 
121  bool passed;
122  try {
123  passed = interpretLine<std::string, int>(file, tmpString, tmpInt);
124  } catch (GeneralClassicException &e) {
125  passed = interpretLine<std::string, int, std::string>(file, tmpString, tmpInt, tmpString);
126 
127  tmpString = Util::toUpper(tmpString);
128  if (tmpString != "TRUE" &&
129  tmpString != "FALSE")
130  throw GeneralClassicException("Astra1DDynamic_fast::readFileHeader",
131  "The third string on the first line of 1D field "
132  "maps has to be either TRUE or FALSE");
133 
134  normalize_m = (tmpString == "TRUE");
135  }
136 
137  return passed;
138 }
139 
141  std::string tmpString;
142  int accuracy;
143 
144  try {
145  interpretLine<std::string, int>(file, tmpString, accuracy);
146  } catch (GeneralClassicException &e) {
147  interpretLine<std::string, int, std::string>(file, tmpString, accuracy, tmpString);
148  }
149 
150  return accuracy;
151 }
152 
@ TAstraElectroStatic
Definition: Fieldmap.h:19
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)
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
Astra1DElectroStatic_fast(std::string aFilename)
bool readFileHeader(std::ifstream &file)
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
virtual void setFrequency(double freq)
int stripFileHeader(std::ifstream &file)
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
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