OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
FM1DMagnetoStatic.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
17
18 std::ifstream fieldFile(Filename_m.c_str());
19 if(fieldFile.good()) {
20
21 bool parsingPassed = readFileHeader(fieldFile);
22 parsingPassed = checkFileData(fieldFile, parsingPassed);
23 fieldFile.close();
24
25 if(!parsingPassed) {
27 zEnd_m = zBegin_m - 1.0e-3;
28 } else
30
33 } else {
35 zBegin_m = 0.0;
36 zEnd_m = -1.0e-3;
37 }
38}
39
41 freeMap();
42}
43
45
46 if(fourierCoefs_m.empty()) {
47
48 std::ifstream fieldFile(Filename_m.c_str());
49 stripFileHeader(fieldFile);
50
51 double *fieldData = new double[2 * numberOfGridPoints_m - 1];
52 double maxBz = readFileData(fieldFile, fieldData);
53 fieldFile.close();
54 computeFourierCoefficients(maxBz, fieldData);
55 delete [] fieldData;
56
57 INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m + "'", "info")
58 << endl);
59 }
60}
61
63
64 if(!fourierCoefs_m.empty()) {
65 fourierCoefs_m.clear();
66
67 INFOMSG(level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info")
68 << endl);
69 }
70}
71
73 Vector_t &B) const {
74
75 std::vector<double> fieldComponents;
76 computeFieldOnAxis(R(2) - zBegin_m, fieldComponents);
77 computeFieldOffAxis(R, E, B, fieldComponents);
78
79 return false;
80}
81
83 Vector_t &/*E*/,
84 Vector_t &B,
85 const DiffDirection &/*dir*/) const {
86
87 double kz = Physics::two_pi * (R(2) - zBegin_m) / length_m + Physics::pi;
88 double bZPrime = 0.0;
89
90 int coefIndex = 1;
91 for(int n = 1; n < accuracy_m; n++) {
92
93 bZPrime += n * Physics::two_pi / length_m
94 * (-fourierCoefs_m.at(coefIndex) * sin(kz * n)
95 - fourierCoefs_m.at(coefIndex + 1) * cos(kz * n));
96 coefIndex += 2;
97
98 }
99
100 B(2) += bZPrime;
101
102 return false;
103}
104
105void FM1DMagnetoStatic::getFieldDimensions(double &zBegin, double &zEnd) const {
106 zBegin = zBegin_m;
107 zEnd = zEnd_m;
108}
109void FM1DMagnetoStatic::getFieldDimensions(double &/*xIni*/, double &/*xFinal*/,
110 double &/*yIni*/, double &/*yFinal*/,
111 double &/*zIni*/, double &/*zFinal*/) const {
112}
113
115{ }
116
118 (*msg) << Filename_m
119 << " (1D magnetostatic); zini= "
120 << zBegin_m << " m; zfinal= "
121 << zEnd_m << " m;" << endl;
122}
123
125 return 0.0;
126}
127
129{ }
130
131bool FM1DMagnetoStatic::checkFileData(std::ifstream &fieldFile,
132 bool parsingPassed) {
133
134 double tempDouble;
135 for(int dataIndex = 0; dataIndex < numberOfGridPoints_m; ++ dataIndex)
136 parsingPassed = parsingPassed
137 && interpretLine<double>(fieldFile, tempDouble);
138
139 return parsingPassed && interpreteEOF(fieldFile);
140
141}
142
144 Vector_t &/*E*/,
145 Vector_t &B,
146 std::vector<double> fieldComponents) const {
147
148 double radiusSq = pow(R(0), 2.0) + pow(R(1), 2.0);
149 double transverseBFactor = -fieldComponents.at(1) / 2.0
150 + radiusSq * fieldComponents.at(3) / 16.0;
151
152 B(0) += R(0) * transverseBFactor;
153 B(1) += R(1) * transverseBFactor;
154 B(2) += fieldComponents.at(0) - fieldComponents.at(2) * radiusSq / 4.0;
155
156}
157
159 std::vector<double> &fieldComponents) const {
160
161 double kz = Physics::two_pi * z / length_m + Physics::pi;
162 fieldComponents.push_back(fourierCoefs_m.at(0));
163 fieldComponents.push_back(0.0);
164 fieldComponents.push_back(0.0);
165 fieldComponents.push_back(0.0);
166
167 int coefIndex = 1;
168 for(int n = 1; n < accuracy_m; n++) {
169
170 double kn = n * Physics::two_pi / length_m;
171 double coskzn = cos(kz * n);
172 double sinkzn = sin(kz * n);
173
174 fieldComponents.at(0) += fourierCoefs_m.at(coefIndex) * coskzn
175 - fourierCoefs_m.at(coefIndex + 1) * sinkzn;
176
177 fieldComponents.at(1) += kn * (-fourierCoefs_m.at(coefIndex) * sinkzn
178 - fourierCoefs_m.at(coefIndex + 1) * coskzn);
179
180 double derivCoeff = pow(kn, 2.0);
181 fieldComponents.at(2) += derivCoeff * (-fourierCoefs_m.at(coefIndex) * coskzn
182 + fourierCoefs_m.at(coefIndex + 1) * sinkzn);
183 derivCoeff *= kn;
184 fieldComponents.at(3) += derivCoeff * (fourierCoefs_m.at(coefIndex) * sinkzn
185 + fourierCoefs_m.at(coefIndex + 1) * coskzn);
186
187 coefIndex += 2;
188 }
189}
190
192 double fieldData[]) {
193 const unsigned int totalSize = 2 * numberOfGridPoints_m - 1;
194 gsl_fft_real_wavetable *waveTable = gsl_fft_real_wavetable_alloc(totalSize);
195 gsl_fft_real_workspace *workSpace = gsl_fft_real_workspace_alloc(totalSize);
196
197 gsl_fft_real_transform(fieldData, 1, totalSize, waveTable, workSpace);
198
199 /*
200 * Normalize the Fourier coefficients such that the max field
201 * value is 1 V/m.
202 */
203
204 fourierCoefs_m.push_back(fieldData[0] / (totalSize * maxBz));
205 for(int coefIndex = 1; coefIndex < 2 * accuracy_m - 1; coefIndex++)
206 fourierCoefs_m.push_back(2.0 * fieldData[coefIndex] / (totalSize * maxBz));
207
208 gsl_fft_real_workspace_free(workSpace);
209 gsl_fft_real_wavetable_free(waveTable);
210
211}
212
214
215 // Convert to m.
220}
221
222double FM1DMagnetoStatic::readFileData(std::ifstream &fieldFile,
223 double fieldData[]) {
224
225 double maxBz = 0.0;
226 for(int dataIndex = 0; dataIndex < numberOfGridPoints_m; dataIndex++) {
227 interpretLine<double>(fieldFile, fieldData[numberOfGridPoints_m
228 - 1 + dataIndex]);
229 if(std::abs(fieldData[numberOfGridPoints_m + dataIndex]) > maxBz)
230 maxBz = std::abs(fieldData[numberOfGridPoints_m + dataIndex]);
231
232 /*
233 * Mirror the field map about minimum z value to ensure that
234 * it is periodic.
235 */
236 if(dataIndex != 0)
237 fieldData[numberOfGridPoints_m - 1 - dataIndex]
238 = fieldData[numberOfGridPoints_m + dataIndex];
239 }
240
241 if (!normalize_m)
242 maxBz = 1.0;
243
244 return maxBz;
245}
246
247bool FM1DMagnetoStatic::readFileHeader(std::ifstream &fieldFile) {
248
249 std::string tempString;
250 int tempInt;
251
252 bool parsingPassed = true;
253 try {
254 parsingPassed = interpretLine<std::string, int>(fieldFile,
255 tempString,
256 accuracy_m);
257 } catch (GeneralClassicException &e) {
258 parsingPassed = interpretLine<std::string, int, std::string>(fieldFile,
259 tempString,
261 tempString);
262
263 tempString = Util::toUpper(tempString);
264 if (tempString != "TRUE" &&
265 tempString != "FALSE")
266 throw GeneralClassicException("FM1DMagnetoStatic::readFileHeader",
267 "The third string on the first line of 1D field "
268 "maps has to be either TRUE or FALSE");
269
270 normalize_m = (tempString == "TRUE");
271 }
272 parsingPassed = parsingPassed &&
273 interpretLine<double, double, int>(fieldFile, zBegin_m,
274 zEnd_m,
276 parsingPassed = parsingPassed &&
277 interpretLine<double, double, int>(fieldFile, rBegin_m,
278 rEnd_m, tempInt);
279
281
284
285 return parsingPassed;
286}
287
288void FM1DMagnetoStatic::stripFileHeader(std::ifstream &fieldFile) {
289
290 std::string tempString;
291
292 getLine(fieldFile, tempString);
293 getLine(fieldFile, tempString);
294 getLine(fieldFile, tempString);
295}
@ T1DMagnetoStatic
Definition: Fieldmap.h:20
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
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 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
bool readFileHeader(std::ifstream &fieldFile)
virtual void readMap()
std::vector< double > fourierCoefs_m
Number of Fourier coefficients to use reconstructing field.
virtual void setFrequency(double freq)
int numberOfGridPoints_m
Field length.
int accuracy_m
Number of grid points in field input file.
void stripFileHeader(std::ifstream &fieldFile)
virtual bool getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const
virtual double getFrequency() const
double length_m
Longitudinal end of field.
virtual void freeMap()
void computeFourierCoefficients(double maxEz, double fieldData[])
double rEnd_m
Minimum radius of field.
bool checkFileData(std::ifstream &fieldFile, bool parsingPassed)
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const
double zBegin_m
Maximum radius of field.
virtual void getFieldDimensions(double &zBegin, double &zEnd) const
double readFileData(std::ifstream &fieldFile, double fieldData[])
void computeFieldOnAxis(double z, std::vector< double > &fieldComponents) const
double zEnd_m
Longitudinal start of field.
virtual void getInfo(Inform *)
FM1DMagnetoStatic(std::string aFilename)
void computeFieldOffAxis(const Vector_t &R, Vector_t &E, Vector_t &B, std::vector< double > fieldComponents) const
Definition: Inform.h:42