OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
Astra1DElectroStatic.cpp
Go to the documentation of this file.
2#include "Fields/Fieldmap.hpp"
3#include "Physics/Physics.h"
4#include "Physics/Units.h"
6#include "Utilities/Util.h"
7
8#include "gsl/gsl_interp.h"
9#include "gsl/gsl_spline.h"
10#include "gsl/gsl_fft_real.h"
11
12#include <fstream>
13#include <ios>
14
16 : Fieldmap(aFilename),
17 FourCoefs_m(nullptr) {
18
19 std::ifstream file;
20 int skippedValues = 0;
21 std::string tmpString;
22 double tmpDouble;
23
25
26 // open field map, parse it and disable element on error
27 file.open(Filename_m.c_str());
28 if (file.good()) {
29 bool parsing_passed = true;
30 try {
31 parsing_passed = interpretLine<std::string, int>(file, tmpString, accuracy_m);
32 } catch (GeneralClassicException &e) {
33 parsing_passed = interpretLine<std::string, int, std::string>(file,
34 tmpString,
36 tmpString);
37
38 tmpString = Util::toUpper(tmpString);
39 if (tmpString != "TRUE" &&
40 tmpString != "FALSE")
41 throw GeneralClassicException("Astra1DElectroStatic::Astra1DElectroStatic",
42 "The third string on the first line of 1D field "
43 "maps has to be either TRUE or FALSE");
44
45 normalize_m = (tmpString == "TRUE");
46 }
47
48 parsing_passed = parsing_passed &&
49 interpretLine<double, double>(file, zbegin_m, tmpDouble);
50
51 double tmpDouble2 = zbegin_m;
52 while(!file.eof() && parsing_passed) {
53 parsing_passed = interpretLine<double, double>(file, zend_m, tmpDouble, false);
54 if (zend_m - tmpDouble2 > 1e-10) {
55 tmpDouble2 = zend_m;
56 } else if (parsing_passed) {
57 ++ skippedValues;
58 }
59 }
60
61 num_gridpz_m = lines_read_m - 2 - skippedValues;
62 lines_read_m = 0;
63
64 if (!parsing_passed && !file.eof()) {
66 zend_m = zbegin_m - 1e-3;
67 throw GeneralClassicException("Astra1DElectroStatic::Astra1DElectroStatic",
68 "An error occured when reading the fieldmap '" + Filename_m + "'");
69 }
70 length_m = 2.0 * num_gridpz_m * (zend_m - zbegin_m) / (num_gridpz_m - 1);
71 file.close();
72 } else {
74 zbegin_m = 0.0;
75 zend_m = -1e-3;
76 }
77}
78
80 freeMap();
81}
82
84 if (FourCoefs_m == nullptr) {
85 // declare variables and allocate memory
86
87 std::ifstream in;
88
89 bool parsing_passed = true;
90
91 std::string tmpString;
92
93 double Ez_max = 0.0;
94 double dz = (zend_m - zbegin_m) / (num_gridpz_m - 1);
95 double tmpDouble = zbegin_m - dz;
96
97 double *RealValues = new double[2 * num_gridpz_m];
98 double *zvals = new double[num_gridpz_m];
99
100 gsl_spline *Ez_interpolant = gsl_spline_alloc(gsl_interp_cspline, num_gridpz_m);
101 gsl_interp_accel *Ez_accel = gsl_interp_accel_alloc();
102
103 gsl_fft_real_wavetable *real = gsl_fft_real_wavetable_alloc(2 * num_gridpz_m);
104 gsl_fft_real_workspace *work = gsl_fft_real_workspace_alloc(2 * num_gridpz_m);
105
106 FourCoefs_m = new double[2 * accuracy_m - 1];
107
108 // read in and parse field map
109 in.open(Filename_m.c_str());
110 getLine(in, tmpString);
111
112 for (int i = 0; i < num_gridpz_m && parsing_passed; /* skip increment of i here */) {
113 parsing_passed = interpretLine<double, double>(in, zvals[i], RealValues[i]);
114 // the sequence of z-position should be strictly increasing
115 // drop sampling points that don't comply to this
116 if (zvals[i] - tmpDouble > 1e-10) {
117 if (std::abs(RealValues[i]) > Ez_max) {
118 Ez_max = std::abs(RealValues[i]);
119 }
120 tmpDouble = zvals[i];
121 ++ i; // increment i only if sampling point is accepted
122 }
123 }
124 in.close();
125
126 gsl_spline_init(Ez_interpolant, zvals, RealValues, num_gridpz_m);
127
128 // get equidistant sampling from the, possibly, non-equidistant sampling
129 // using cubic spline for this
130 int ii = num_gridpz_m;
131 for (int i = 0; i < num_gridpz_m - 1; ++ i, ++ ii) {
132 double z = zbegin_m + dz * i;
133 RealValues[ii] = gsl_spline_eval(Ez_interpolant, z, Ez_accel);
134 }
135 RealValues[ii ++] = RealValues[num_gridpz_m - 1];
136 // prepend mirror sampling points such that field values are periodic for sure
137 -- ii; // ii == 2*num_gridpz_m at the moment
138 for (int i = 0; i < num_gridpz_m; ++ i, -- ii) {
139 RealValues[i] = RealValues[ii];
140 }
141
142
143 num_gridpz_m *= 2; // we doubled the sampling points
144
145 gsl_fft_real_transform(RealValues, 1, num_gridpz_m, real, work);
146
147 if (!normalize_m)
148 Ez_max = 1.0;
149
150 // normalize to Ez_max = 1 MV/m
151 FourCoefs_m[0] = Units::MVpm2Vpm * RealValues[0] / (Ez_max * num_gridpz_m);
152 for (int i = 1; i < 2 * accuracy_m - 1; i++) {
153 FourCoefs_m[i] = Units::MVpm2Vpm * 2.* RealValues[i] / (Ez_max * num_gridpz_m);
154 }
155
156 gsl_fft_real_workspace_free(work);
157 gsl_fft_real_wavetable_free(real);
158
159 gsl_spline_free(Ez_interpolant);
160 gsl_interp_accel_free(Ez_accel);
161
162 delete[] zvals;
163 delete[] RealValues;
164
165 INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info") << endl);
166
167 }
168}
169
171 if (FourCoefs_m != nullptr) {
172
173 delete[] FourCoefs_m;
174 FourCoefs_m = nullptr;
175
176 INFOMSG(level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << endl);
177 }
178}
179
181 // do fourier interpolation in z-direction
182 const double RR2 = R(0) * R(0) + R(1) * R(1);
183
184 const double kz = Physics::two_pi * (R(2) - zbegin_m) / length_m + Physics::pi;
185
186 double ez = FourCoefs_m[0];
187 double ezp = 0.0;
188 double ezpp = 0.0;
189 double ezppp = 0.0;
190
191 int n = 1;
192 for (int l = 1; l < accuracy_m ; l++, n += 2) {
193 double somefactor_base = Physics::two_pi / length_m * l; // = \frac{d(kz*l)}{dz}
194 double somefactor = 1.0;
195 double coskzl = cos(kz * l);
196 double sinkzl = sin(kz * l);
197 ez += (FourCoefs_m[n] * coskzl - FourCoefs_m[n + 1] * sinkzl);
198 somefactor *= somefactor_base;
199 ezp += somefactor * (-FourCoefs_m[n] * sinkzl - FourCoefs_m[n + 1] * coskzl);
200 somefactor *= somefactor_base;
201 ezpp += somefactor * (-FourCoefs_m[n] * coskzl + FourCoefs_m[n + 1] * sinkzl);
202 somefactor *= somefactor_base;
203 ezppp += somefactor * (FourCoefs_m[n] * sinkzl + FourCoefs_m[n + 1] * coskzl);
204 }
205
206 // expand the field to off-axis
207 const double EfieldR = -ezp / 2. + ezppp / 16. * RR2;
208
209 E(0) += EfieldR * R(0);
210 E(1) += EfieldR * R(1);
211 E(2) += ez - ezpp * RR2 / 4.;
212 return false;
213}
214
215bool Astra1DElectroStatic::getFieldDerivative(const Vector_t &/*R*/, Vector_t &/*E*/, Vector_t &/*B*/, const DiffDirection &/*dir*/) const {
216 return false;
217}
218
219void Astra1DElectroStatic::getFieldDimensions(double &zBegin, double &zEnd) const {
220 zBegin = zbegin_m;
221 zEnd = zend_m;
222}
223
224void Astra1DElectroStatic::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/, double &/*yIni*/, double &/*yFinal*/, double &/*zIni*/, double &/*zFinal*/) const {}
225
227{ }
228
230 (*msg) << Filename_m << " (1D electrostatic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
231}
232
234 return 0.0;
235}
236
238{ }
FLieGenerator< T, N > real(const FLieGenerator< std::complex< T >, N > &)
Take real part of a complex generator.
@ TAstraElectroStatic
Definition: Fieldmap.h:19
DiffDirection
Definition: Fieldmap.h:54
Tps< T > cos(const Tps< T > &x)
Cosine.
Definition: TpsMath.h:129
Tps< T > sin(const Tps< T > &x)
Sine.
Definition: TpsMath.h:111
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
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 two_pi
The value of.
Definition: Physics.h:33
constexpr double e
The value of.
Definition: Physics.h:39
constexpr double pi
The value of.
Definition: Physics.h:30
constexpr double MVpm2Vpm
Definition: Units.h:128
std::string toUpper(const std::string &str)
Definition: Util.cpp:146
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
virtual double getFrequency() const
Astra1DElectroStatic(std::string aFilename)
virtual void setFrequency(double freq)
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
virtual void getInfo(Inform *)
MapType Type
Definition: Fieldmap.h:114
void disableFieldmapWarning()
Definition: Fieldmap.cpp:613
bool normalize_m
Definition: Fieldmap.h:120
int lines_read_m
Definition: Fieldmap.h:118
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 getLine(std::ifstream &in, std::string &buffer)
Definition: Fieldmap.h:121
void noFieldmapWarning()
Definition: Fieldmap.cpp:621
Definition: Inform.h:42