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