OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
Astra1DElectroStatic_fast.cpp
Go to the documentation of this file.
3#include "Utilities/Util.h"
4#include "Physics/Physics.h"
5#include "Physics/Units.h"
6
7#include <fstream>
8#include <ios>
9
11 Astra1D_fast(aFilename) {
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 if(!file.good()) {
22 zbegin_m = 0.0;
23 zend_m = -1e-3;
24 }
25
26 bool parsing_passed = readFileHeader(file);
27 parsing_passed = parsing_passed && determineNumSamplingPoints(file);
28 file.close();
29
30 if(!parsing_passed && !file.eof()) {
32 zend_m = zbegin_m - 1e-3;
33 throw GeneralClassicException("Astra1DElectroStatic_fast::Astra1DElectroStatic_fast",
34 "An error occured when reading the fieldmap '" + Filename_m + "'");
35 }
36 hz_m = (zend_m - zbegin_m) / (num_gridpz_m - 1);
37 length_m = 2.0 * num_gridpz_m * hz_m;
38}
39
41 freeMap();
42}
43
45 if(onAxisField_m == nullptr) {
46 // declare variables and allocate memory
47
48 onAxisField_m = new double[num_gridpz_m];
49 zvals_m = new double[num_gridpz_m];
50
51 std::ifstream file(Filename_m.c_str());
52 int accuracy = stripFileHeader(file);
53 double maxEz = readFieldData(file);
54 file.close();
55
57
58 std::vector<double> zvals = getEvenlyDistributedSamplingPoints();
59 std::vector<double> evenFieldSampling = interpolateFieldData(zvals);
60 std::vector<double> fourierCoefs = computeFourierCoefficients(accuracy, evenFieldSampling);
61
62 computeFieldDerivatives(fourierCoefs, accuracy);
63
64 checkMap(accuracy,
66 zvals,
67 fourierCoefs,
69 onAxisAccel_m[0]);
70
71
72 INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info") << endl);
73 }
74}
75
77 // do fourier interpolation in z-direction
78 const double RR2 = R(0) * R(0) + R(1) * R(1);
79
80 double ez = gsl_spline_eval(onAxisInterpolants_m[0], R(2) - zbegin_m, onAxisAccel_m[0]);
81 double ezp = gsl_spline_eval(onAxisInterpolants_m[1], R(2) - zbegin_m, onAxisAccel_m[1]);
82 double ezpp = gsl_spline_eval(onAxisInterpolants_m[2], R(2) - zbegin_m, onAxisAccel_m[2]);
83 double ezppp = gsl_spline_eval(onAxisInterpolants_m[3], R(2) - zbegin_m, onAxisAccel_m[3]);
84
85 // expand to off-axis
86 const double EfieldR = -ezp / 2. + ezppp / 16. * RR2;
87
88 E(0) += EfieldR * R(0);
89 E(1) += EfieldR * R(1);
90 E(2) += ez - ezpp * RR2 / 4.;
91 return false;
92}
93
94bool Astra1DElectroStatic_fast::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
95 return false;
96}
97
98void Astra1DElectroStatic_fast::getFieldDimensions(double &zBegin, double &zEnd) const {
99 zBegin = zbegin_m;
100 zEnd = zend_m;
101}
102void Astra1DElectroStatic_fast::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/, double &/*yIni*/, double &/*yFinal*/, double &/*zIni*/, double &/*zFinal*/) const {}
103
105{ }
106
108 (*msg) << Filename_m << " (1D electrostatic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
109}
110
112 return 0.0;
113}
114
116{ }
117
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
@ 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
constexpr double Vpm2MVpm
Definition: Units.h:125
std::string toUpper(const std::string &str)
Definition: Util.cpp:146
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