OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
FM1DElectroStatic_fast.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_fft_real.h"
9
10#include <fstream>
11#include <ios>
12
14 Fieldmap(aFilename),
15 accuracy_m(0)
16{
17
19 onAxisField_m = nullptr;
20
21 std::ifstream fieldFile(Filename_m.c_str());
22 if (fieldFile.good()) {
23
24 bool parsingPassed = readFileHeader(fieldFile);
25 parsingPassed = checkFileData(fieldFile, parsingPassed);
26 fieldFile.close();
27
28 if (!parsingPassed) {
30 zEnd_m = zBegin_m - 1.0e-3;
31 } else
33
36
37 } else {
39 zBegin_m = 0.0;
40 zEnd_m = -1.0e-3;
41 }
42}
43
45 freeMap();
46}
47
49 if (onAxisField_m == nullptr) {
50
51 std::ifstream fieldFile(Filename_m.c_str());
52 stripFileHeader(fieldFile);
53
55 double maxEz = readFileData(fieldFile, onAxisField_m);
56 fieldFile.close();
57
58 std::vector<double> fourierCoefs
60 normalizeField(maxEz, fourierCoefs);
61
62 double *onAxisFieldP = new double[numberOfGridPoints_m];
63 double *onAxisFieldPP = new double[numberOfGridPoints_m];
64 double *onAxisFieldPPP = new double[numberOfGridPoints_m];
65 computeFieldDerivatives(fourierCoefs, onAxisFieldP,
66 onAxisFieldPP, onAxisFieldPPP);
67 computeInterpolationVectors(onAxisFieldP, onAxisFieldPP,
68 onAxisFieldPPP);
69
70 prepareForMapCheck(fourierCoefs);
71
72 delete [] onAxisFieldP;
73 delete [] onAxisFieldPP;
74 delete [] onAxisFieldPPP;
75
76 INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info")
77 << endl);
78 }
79}
80
82 if (onAxisField_m != nullptr) {
83 delete [] onAxisField_m;
84 onAxisField_m = nullptr;
85
86 gsl_spline_free(onAxisFieldInterpolants_m);
87 gsl_spline_free(onAxisFieldPInterpolants_m);
88 gsl_spline_free(onAxisFieldPPInterpolants_m);
89 gsl_spline_free(onAxisFieldPPPInterpolants_m);
90 gsl_interp_accel_free(onAxisFieldAccel_m);
91 gsl_interp_accel_free(onAxisFieldPAccel_m);
92 gsl_interp_accel_free(onAxisFieldPPAccel_m);
93 gsl_interp_accel_free(onAxisFieldPPPAccel_m);
94
95 INFOMSG(level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info")
96 << endl);
97 }
98}
99
101 Vector_t &B) const {
102
103 std::vector<double> fieldComponents;
104 computeFieldOnAxis(R(2) - zBegin_m, fieldComponents);
105 computeFieldOffAxis(R, E, B, fieldComponents);
106
107 return false;
108
109}
110
112 Vector_t &E,
113 Vector_t &/*B*/,
114 const DiffDirection &/*dir*/) const {
115
116 E(2) += gsl_spline_eval(onAxisFieldPInterpolants_m, R(2) - zBegin_m,
118
119 return false;
120
121}
122
123void FM1DElectroStatic_fast::getFieldDimensions(double &zBegin, double &zEnd) const {
124 zBegin = zBegin_m;
125 zEnd = zEnd_m;
126}
127void FM1DElectroStatic_fast::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/,
128 double &/*yIni*/, double &/*yFinal*/,
129 double &/*zIni*/, double &/*zFinal*/) const {}
130
132{ }
133
135 (*msg) << Filename_m
136 << " (1D electrotostatic); zini= "
137 << zBegin_m << " m; zfinal= "
138 << zEnd_m << " m;" << endl;
139}
140
142 return 0.0;
143}
144
146{ }
147
148bool FM1DElectroStatic_fast::checkFileData(std::ifstream &fieldFile,
149 bool parsingPassed) {
150
151 double tempDouble;
152 for (unsigned int dataIndex = 0; dataIndex < numberOfGridPoints_m; ++ dataIndex)
153 parsingPassed = parsingPassed
154 && interpretLine<double>(fieldFile, tempDouble);
155
156 return parsingPassed && interpreteEOF(fieldFile);
157
158}
159
160void FM1DElectroStatic_fast::computeFieldDerivatives(std::vector<double> fourierCoefs,
161 double onAxisFieldP[],
162 double onAxisFieldPP[],
163 double onAxisFieldPPP[]) {
164
165 for (unsigned int zStepIndex = 0; zStepIndex < numberOfGridPoints_m; ++ zStepIndex) {
166
167 double z = deltaZ_m * zStepIndex;
168 double kz = Physics::two_pi * z / length_m + Physics::pi;
169 onAxisFieldP[zStepIndex] = 0.0;
170 onAxisFieldPP[zStepIndex] = 0.0;
171 onAxisFieldPPP[zStepIndex] = 0.0;
172
173 int coefIndex = 1;
174 for (unsigned int n = 1; n < accuracy_m; ++ n) {
175
176 double kn = n * Physics::two_pi / length_m;
177 double coskzn = cos(kz * n);
178 double sinkzn = sin(kz * n);
179
180 onAxisFieldP[zStepIndex] += kn * (-fourierCoefs.at(coefIndex) * sinkzn
181 - fourierCoefs.at(coefIndex + 1) * coskzn);
182
183 double derivCoeff = pow(kn, 2.0);
184 onAxisFieldPP[zStepIndex] += derivCoeff * (-fourierCoefs.at(coefIndex) * coskzn
185 + fourierCoefs.at(coefIndex + 1) * sinkzn);
186 derivCoeff *= kn;
187 onAxisFieldPPP[zStepIndex] += derivCoeff * (fourierCoefs.at(coefIndex) * sinkzn
188 + fourierCoefs.at(coefIndex + 1) * coskzn);
189
190 coefIndex += 2;
191 }
192 }
193}
194
196 std::vector<double> fieldComponents) const {
197
198 double radiusSq = pow(R(0), 2.0) + pow(R(1), 2.0);
199 double transverseEFactor = -fieldComponents.at(1) / 2.0
200 + radiusSq * fieldComponents.at(3) / 16.0;
201
202 E(0) += R(0) * transverseEFactor;
203 E(1) += R(1) * transverseEFactor;
204 E(2) += fieldComponents.at(0) - fieldComponents.at(2) * radiusSq / 4.0;
205
206}
207
209 std::vector<double> &fieldComponents) const {
210
211 fieldComponents.push_back(gsl_spline_eval(onAxisFieldInterpolants_m,
213 fieldComponents.push_back(gsl_spline_eval(onAxisFieldPInterpolants_m,
215 fieldComponents.push_back(gsl_spline_eval(onAxisFieldPPInterpolants_m,
217 fieldComponents.push_back(gsl_spline_eval(onAxisFieldPPPInterpolants_m,
219}
220
221std::vector<double> FM1DElectroStatic_fast::computeFourierCoefficients(double fieldData[]) {
222
223 const unsigned int totalSize = 2 * numberOfGridPoints_m - 1;
224 gsl_fft_real_wavetable *waveTable = gsl_fft_real_wavetable_alloc(totalSize);
225 gsl_fft_real_workspace *workSpace = gsl_fft_real_workspace_alloc(totalSize);
226
227 // Reflect field about minimum z value to ensure that it is periodic.
228 double *fieldDataReflected = new double[totalSize];
229 for (unsigned int dataIndex = 0; dataIndex < numberOfGridPoints_m; ++ dataIndex) {
230 fieldDataReflected[numberOfGridPoints_m - 1 + dataIndex] =
231 fieldData[dataIndex];
232 fieldDataReflected[numberOfGridPoints_m - 1 - dataIndex] =
233 fieldData[dataIndex];
234 }
235
236 gsl_fft_real_transform(fieldDataReflected, 1,
237 totalSize,
238 waveTable, workSpace);
239
240 std::vector<double> fourierCoefs;
241 fourierCoefs.push_back(fieldDataReflected[0] / totalSize);
242 for (unsigned int coefIndex = 1; coefIndex + 1 < 2 * accuracy_m; ++ coefIndex)
243 fourierCoefs.push_back(2.0 * fieldDataReflected[coefIndex] / totalSize);
244
245 delete [] fieldDataReflected;
246 gsl_fft_real_workspace_free(workSpace);
247 gsl_fft_real_wavetable_free(waveTable);
248
249 return fourierCoefs;
250
251}
252
254 double onAxisFieldPP[],
255 double onAxisFieldPPP[]) {
256
257 onAxisFieldInterpolants_m = gsl_spline_alloc(gsl_interp_cspline,
259 onAxisFieldPInterpolants_m = gsl_spline_alloc(gsl_interp_cspline,
261 onAxisFieldPPInterpolants_m = gsl_spline_alloc(gsl_interp_cspline,
263 onAxisFieldPPPInterpolants_m = gsl_spline_alloc(gsl_interp_cspline,
265
266 double *z = new double[numberOfGridPoints_m];
267 for (unsigned int zStepIndex = 0; zStepIndex < numberOfGridPoints_m; ++ zStepIndex)
268 z[zStepIndex] = deltaZ_m * zStepIndex;
269
270 gsl_spline_init(onAxisFieldInterpolants_m, z,
272 gsl_spline_init(onAxisFieldPInterpolants_m, z,
273 onAxisFieldP, numberOfGridPoints_m);
274 gsl_spline_init(onAxisFieldPPInterpolants_m, z,
275 onAxisFieldPP, numberOfGridPoints_m);
276 gsl_spline_init(onAxisFieldPPPInterpolants_m, z,
277 onAxisFieldPPP, numberOfGridPoints_m);
278
279 onAxisFieldAccel_m = gsl_interp_accel_alloc();
280 onAxisFieldPAccel_m = gsl_interp_accel_alloc();
281 onAxisFieldPPAccel_m = gsl_interp_accel_alloc();
282 onAxisFieldPPPAccel_m = gsl_interp_accel_alloc();
283
284 delete [] z;
285}
286
288
289 // Convert to m.
294
295}
296
297void FM1DElectroStatic_fast::normalizeField(double maxEz, std::vector<double> &fourierCoefs) {
298
299 for (unsigned int dataIndex = 0; dataIndex < numberOfGridPoints_m; ++ dataIndex)
300 onAxisField_m[dataIndex] /= (maxEz * Units::Vpm2MVpm);
301
302 for (auto fourierIt = fourierCoefs.begin(); fourierIt < fourierCoefs.end(); ++ fourierIt)
303 *fourierIt /= (maxEz * Units::Vpm2MVpm);
304
305}
306
307double FM1DElectroStatic_fast::readFileData(std::ifstream &fieldFile,
308 double fieldData[]) {
309
310 double maxEz = 0.0;
311 for (unsigned int dataIndex = 0; dataIndex < numberOfGridPoints_m; ++ dataIndex) {
312 interpretLine<double>(fieldFile, fieldData[dataIndex]);
313 if (std::abs(fieldData[dataIndex]) > maxEz)
314 maxEz = std::abs(fieldData[dataIndex]);
315 }
316
317 if (!normalize_m)
318 maxEz = 1.0;
319
320 return maxEz;
321}
322
323bool FM1DElectroStatic_fast::readFileHeader(std::ifstream &fieldFile) {
324
325 std::string tempString;
326 int tempInt;
327
328 bool parsingPassed = true;
329 try {
330 parsingPassed = interpretLine<std::string, unsigned int>(fieldFile,
331 tempString,
332 accuracy_m);
333 } catch (GeneralClassicException &e) {
334 parsingPassed = interpretLine<std::string, unsigned int, std::string>(fieldFile,
335 tempString,
337 tempString);
338
339 tempString = Util::toUpper(tempString);
340 if (tempString != "TRUE" &&
341 tempString != "FALSE")
342 throw GeneralClassicException("FM1DElectroStatic_fast::readFileHeader",
343 "The third string on the first line of 1D field "
344 "maps has to be either TRUE or FALSE");
345
346 normalize_m = (tempString == "TRUE");
347 }
348 parsingPassed = parsingPassed &&
349 interpretLine<double, double, unsigned int>(fieldFile,
350 zBegin_m,
351 zEnd_m,
353 parsingPassed = parsingPassed &&
354 interpretLine<double, double, int>(fieldFile,
355 rBegin_m,
356 rEnd_m,
357 tempInt);
358
362
363 return parsingPassed;
364}
365
366void FM1DElectroStatic_fast::stripFileHeader(std::ifstream &fieldFile) {
367
368 std::string tempString;
369
370 getLine(fieldFile, tempString);
371 getLine(fieldFile, tempString);
372 getLine(fieldFile, tempString);
373}
374
375void FM1DElectroStatic_fast::prepareForMapCheck(std::vector<double> &fourierCoefs) {
376 std::vector<double> zSampling(numberOfGridPoints_m);
377 for (unsigned int zStepIndex = 0; zStepIndex < numberOfGridPoints_m; ++ zStepIndex)
378 zSampling[zStepIndex] = deltaZ_m * zStepIndex;
379
381 length_m,
382 zSampling,
383 fourierCoefs,
386}
@ T1DElectroStatic
Definition: Fieldmap.h:18
DiffDirection
Definition: Fieldmap.h:54
Tps< T > cos(const Tps< T > &x)
Cosine.
Definition: TpsMath.h:129
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
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 cm2m
Definition: Units.h:35
constexpr double Vpm2MVpm
Definition: Units.h:125
std::string toUpper(const std::string &str)
Definition: Util.cpp:146
MapType Type
Definition: Fieldmap.h:114
bool interpreteEOF(std::ifstream &in)
Definition: Fieldmap.cpp:558
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 getLine(std::ifstream &in, std::string &buffer)
Definition: Fieldmap.h:121
void noFieldmapWarning()
Definition: Fieldmap.cpp:621
gsl_spline * onAxisFieldPInterpolants_m
On axis field interpolation structure.
std::vector< double > computeFourierCoefficients(double fieldData[])
void computeFieldDerivatives(std::vector< double > fourierCoefs, double onAxisFieldP[], double onAxisFieldPP[], double onAxisFieldPPP[])
void normalizeField(double maxEz, std::vector< double > &fourierCoefs)
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
gsl_interp_accel * onAxisFieldPAccel_m
void computeFieldOnAxis(double z, std::vector< double > &fieldComponents) const
void stripFileHeader(std::ifstream &fieldFile)
unsigned int numberOfGridPoints_m
Field length.
virtual void getInfo(Inform *)
unsigned int accuracy_m
Field grid point spacing.
FM1DElectroStatic_fast(std::string aFilename)
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
double zEnd_m
Longitudinal start of field.
gsl_spline * onAxisFieldInterpolants_m
On axis field data.
virtual double getFrequency() const
double readFileData(std::ifstream &fieldFile, double fieldData[])
gsl_interp_accel * onAxisFieldPPPAccel_m
gsl_spline * onAxisFieldPPInterpolants_m
On axis field first derivative interpolation structure.
gsl_interp_accel * onAxisFieldAccel_m
On axis field third derivative interpolation structure.
double rEnd_m
Minimum radius of field.
virtual void setFrequency(double freq)
bool readFileHeader(std::ifstream &fieldFile)
double length_m
Longitudinal end of field.
double zBegin_m
Maximum radius of field.
void computeFieldOffAxis(const Vector_t &R, Vector_t &E, Vector_t &B, std::vector< double > fieldComponents) const
bool checkFileData(std::ifstream &fieldFile, bool parsingPassed)
double deltaZ_m
Number of grid points in field input file.
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
gsl_spline * onAxisFieldPPPInterpolants_m
On axis field second derivative interpolation structure.
void computeInterpolationVectors(double onAxisFieldP[], double onAxisFieldPP[], double onAxisFieldPPP[])
gsl_interp_accel * onAxisFieldPPAccel_m
void prepareForMapCheck(std::vector< double > &fourierCoefs)
Definition: Inform.h:42