OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Astra1DDynamic_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 = 3;
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  if(!file.good()) {
22  zbegin_m = 0.0;
23  zend_m = -1e-3;
24  return;
25  }
26 
27  bool parsing_passed = readFileHeader(file);
28 
29  parsing_passed = parsing_passed && determineNumSamplingPoints(file);
30  file.close();
31 
32  if(!parsing_passed) {
34  zend_m = zbegin_m - 1e-3;
35  throw GeneralClassicException("Astra1DDynamic_fast::Astra1DDynamic_fast",
36  "An error occured when reading the fieldmap '" + Filename_m + "'");
37  }
38 
39  // conversion from MHz to Hz and from frequency to angular frequency
42 
43  hz_m = (zend_m - zbegin_m) / (num_gridpz_m - 1);
44  length_m = 2.0 * num_gridpz_m * hz_m;
45 }
46 
48  freeMap();
49 }
50 
52  if(onAxisField_m == NULL) {
53  std::ifstream file(Filename_m.c_str());
54 
55  onAxisField_m = new double[num_gridpz_m];
56  zvals_m = new double[num_gridpz_m];
57 
58  int accuracy = stripFileHeader(file);
59  double maxEz = readFieldData(file);
60  file.close();
61 
62  normalizeFieldData(maxEz * 1e-6);
63 
64  std::vector<double> zvals = getEvenlyDistributedSamplingPoints();
65  std::vector<double> evenFieldSampling = interpolateFieldData(zvals);
66  std::vector<double> fourierCoefs = computeFourierCoefficients(accuracy, evenFieldSampling);
67 
68  computeFieldDerivatives(fourierCoefs, accuracy);
69 
70  checkMap(accuracy,
71  length_m,
72  zvals,
73  fourierCoefs,
75  onAxisAccel_m[0]);
76 
77  INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info") << endl);
78  }
79 }
80 
82  // do fourier interpolation in z-direction
83  const double RR2 = R(0) * R(0) + R(1) * R(1);
84 
85  double ez = gsl_spline_eval(onAxisInterpolants_m[0], R(2), onAxisAccel_m[0]);
86  double ezp = gsl_spline_eval(onAxisInterpolants_m[1], R(2), onAxisAccel_m[1]);
87  double ezpp = gsl_spline_eval(onAxisInterpolants_m[2], R(2), onAxisAccel_m[2]);
88  double ezppp = gsl_spline_eval(onAxisInterpolants_m[3], R(2), onAxisAccel_m[3]);
89 
90  // expand the field off-axis
91  const double f = -(ezpp + ez * xlrep_m * xlrep_m) / 16.;
92  const double fp = -(ezppp + ezp * xlrep_m * xlrep_m) / 16.;
93 
94  const double EfieldR = -(ezp / 2. + fp * RR2);
95  const double BfieldT = (ez / 2. + f * RR2) * xlrep_m / Physics::c;
96 
97  E(0) += EfieldR * R(0);
98  E(1) += EfieldR * R(1);
99  E(2) += ez + 4. * f * RR2;
100  B(0) += -BfieldT * R(1);
101  B(1) += BfieldT * R(0);
102 
103  return false;
104 }
105 
107  double ezp = gsl_spline_eval(onAxisInterpolants_m[1], R(2), onAxisAccel_m[1]);
108 
109  E(2) += ezp;
110 
111  return false;
112 }
113 
114 void Astra1DDynamic_fast::getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const {
115  zBegin = zbegin_m;
116  zEnd = zend_m;
117 }
118 void Astra1DDynamic_fast::getFieldDimensions(double &xIni, double &xFinal, double &yIni, double &yFinal, double &zIni, double &zFinal) const {}
119 
121 { }
122 
124  (*msg) << Filename_m << " (1D dynamic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
125 }
126 
128  return frequency_m;
129 }
130 
132  frequency_m = freq;
133 }
134 
135 void Astra1DDynamic_fast::getOnaxisEz(std::vector<std::pair<double, double> > & F) {
136  F.resize(num_gridpz_m);
137  if(onAxisField_m == NULL) {
138  double Ez_max = 0.0;
139  double tmpDouble;
140  int tmpInt;
141  std::string tmpString;
142 
143  std::ifstream in(Filename_m.c_str());
144  interpreteLine<std::string, int>(in, tmpString, tmpInt);
145  interpreteLine<double, double, int>(in, tmpDouble, tmpDouble, tmpInt);
146  interpreteLine<double>(in, tmpDouble);
147  interpreteLine<double, double, int>(in, tmpDouble, tmpDouble, tmpInt);
148 
149  for(int i = 0; i < num_gridpz_m; ++ i) {
150  F[i].first = hz_m * i;
151  interpreteLine<double>(in, F[i].second);
152  if(fabs(F[i].second) > Ez_max) {
153  Ez_max = fabs(F[i].second);
154  }
155  }
156  in.close();
157 
158  for(int i = 0; i < num_gridpz_m; ++ i) {
159  F[i].second /= Ez_max;
160  }
161  } else {
162  for(int i = 0; i < num_gridpz_m; ++ i) {
163  F[i].first = zvals_m[i];
164  F[i].second = onAxisField_m[i] / 1e6;
165  }
166  }
167 }
168 
169 bool Astra1DDynamic_fast::readFileHeader(std::ifstream &file) {
170  std::string tmpString;
171  int tmpInt;
172  bool passed = true;
173 
174  try {
175  passed = interpreteLine<std::string, int>(file, tmpString, tmpInt);
176  } catch (GeneralClassicException &e) {
177  passed = interpreteLine<std::string, int, std::string>(file, tmpString, tmpInt, tmpString);
178 
179  tmpString = Util::toUpper(tmpString);
180  if (tmpString != "TRUE" &&
181  tmpString != "FALSE")
182  throw GeneralClassicException("Astra1DDynamic_fast::readFileHeader",
183  "The third string on the first line of 1D field "
184  "maps has to be either TRUE or FALSE");
185 
186  normalize_m = (tmpString == "TRUE");
187  }
188 
189  passed = passed && interpreteLine<double>(file, frequency_m);
190 
191  return passed;
192 }
193 
194 int Astra1DDynamic_fast::stripFileHeader(std::ifstream &file) {
195  std::string tmpString;
196  double tmpDouble;
197  int accuracy;
198 
199  try {
200  interpreteLine<std::string, int>(file, tmpString, accuracy);
201  } catch (GeneralClassicException &e) {
202  interpreteLine<std::string, int, std::string>(file, tmpString, accuracy, tmpString);
203  }
204 
205  interpreteLine<double>(file, tmpDouble);
206 
207  return accuracy;
208 }
virtual void getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const
double zend_m
Definition: Astra1D_fast.h:44
constexpr double e
The value of .
Definition: Physics.h:40
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
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()
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
Definition: PETE.h:815
constexpr double two_pi
The value of .
Definition: Physics.h:34
std::string toUpper(const std::string &str)
Definition: Util.cpp:130
virtual void setFrequency(double freq)
void disableFieldmapWarning()
Definition: Fieldmap.cpp:610
double * onAxisField_m
Definition: Astra1D_fast.h:36
std::vector< double > getEvenlyDistributedSamplingPoints()
virtual void getInfo(Inform *)
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:647
void noFieldmapWarning()
Definition: Fieldmap.cpp:618
bool determineNumSamplingPoints(std::ifstream &file)
bool normalize_m
Definition: Fieldmap.h:113
Astra1DDynamic_fast(std::string aFilename)
std::vector< double > interpolateFieldData(std::vector< double > &samplingPoints)
double readFieldData(std::ifstream &file)
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
#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)
virtual double getFrequency() const
int stripFileHeader(std::ifstream &file)
bool readFileHeader(std::ifstream &file)
std::string Filename_m
Definition: Fieldmap.h:110
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &F)
int numHeaderLines_m
Definition: Astra1D_fast.h:48
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
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