OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Fieldmap.cpp
Go to the documentation of this file.
1 #include "Fields/Fieldmap.h"
2 
3 #include "Utility/PAssert.h"
4 
5 #include "Fields/FM3DDynamic.h"
7 #include "Fields/FM3DH5Block.h"
12 #include "Fields/FM2DDynamic.h"
15 #include "Fields/FM1DDynamic.h"
17 #include "Fields/Astra1DDynamic.h"
27 #include "Fields/FM1DProfile1.h"
28 #include "Fields/FM1DProfile2.h"
29 #include "Fields/FMDummy.h"
31 #include "Utilities/Options.h"
32 #include "Utilities/Util.h"
34 #include "Physics/Physics.h"
35 
36 #include "H5hut.h"
37 
38 #include <boost/filesystem.hpp>
39 
40 #include <cmath>
41 #include <iostream>
42 #include <fstream>
43 #include <ios>
44 
45 namespace fs = boost::filesystem;
46 
47 #define REGISTER_PARSE_TYPE(X) template <> struct Fieldmap::TypeParseTraits<X> \
48  { static const char* name; } ; const char* Fieldmap::TypeParseTraits<X>::name = #X
49 
50 Fieldmap *Fieldmap::getFieldmap(std::string Filename, bool fast) {
52  if (position != FieldmapDictionary.end()) {
53  (*position).second.RefCounter++;
54  return (*position).second.Map;
55  } else {
56  MapType type;
58  type = readHeader(Filename);
59  switch(type) {
60  case T1DDynamic:
61  if (fast) {
62  position = FieldmapDictionary.insert(
63  std::make_pair(
64  Filename,
66  T1DDynamic, new FM1DDynamic_fast(Filename))));
67  } else {
68  position = FieldmapDictionary.insert(
69  std::make_pair(
70  Filename,
72  T1DDynamic, new FM1DDynamic(Filename))));
73  }
74  return (*position.first).second.Map;
75  break;
76 
77  case TAstraDynamic:
78  if (fast) {
79  position = FieldmapDictionary.insert(
80  std::make_pair(
81  Filename,
83  TAstraDynamic, new Astra1DDynamic_fast(Filename))));
84  } else {
85  position = FieldmapDictionary.insert(
86  std::make_pair(
87  Filename,
89  TAstraDynamic, new Astra1DDynamic(Filename))));
90  }
91  return (*position.first).second.Map;
92  break;
93 
94  case T1DElectroStatic:
95  if (fast) {
96  position = FieldmapDictionary.insert(
97  std::make_pair(
98  Filename,
100  T1DElectroStatic, new FM1DElectroStatic_fast(Filename))));
101  } else {
102  position = FieldmapDictionary.insert(
103  std::make_pair(
104  Filename,
106  T1DElectroStatic, new FM1DElectroStatic(Filename))));
107  }
108  return (*position.first).second.Map;
109  break;
110 
111  case TAstraElectroStatic:
112  if (fast) {
113  position = FieldmapDictionary.insert(
114  std::make_pair(
115  Filename,
118  } else {
119  position = FieldmapDictionary.insert(
120  std::make_pair(
121  Filename,
123  TAstraElectroStatic, new Astra1DElectroStatic(Filename))));
124  }
125  return (*position.first).second.Map;
126  break;
127 
128  case T1DMagnetoStatic:
129  if (fast) {
130  position = FieldmapDictionary.insert(
131  std::make_pair(
132  Filename,
134  T1DMagnetoStatic, new FM1DMagnetoStatic_fast(Filename))));
135  } else {
136  position = FieldmapDictionary.insert(
137  std::make_pair(
138  Filename,
140  T1DMagnetoStatic, new FM1DMagnetoStatic(Filename))));
141  }
142  return (*position.first).second.Map;
143  break;
144 
145  case TAstraMagnetoStatic:
146  if (fast) {
147  position = FieldmapDictionary.insert(
148  std::make_pair(
149  Filename,
152  } else {
153  position = FieldmapDictionary.insert(
154  std::make_pair(
155  Filename,
157  TAstraMagnetoStatic, new Astra1DMagnetoStatic(Filename))));
158  }
159  return (*position.first).second.Map;
160  break;
161 
162  case T1DProfile1:
163  position = FieldmapDictionary.insert(
164  std::make_pair(
165  Filename,
166  FieldmapDescription(T1DProfile1, new FM1DProfile1(Filename))));
167  return (*position.first).second.Map;
168  break;
169 
170  case T1DProfile2:
171  position = FieldmapDictionary.insert(
172  std::make_pair(
173  Filename,
175  T1DProfile2, new FM1DProfile2(Filename))));
176  return (*position.first).second.Map;
177  break;
178 
179  case T2DDynamic:
180  position = FieldmapDictionary.insert(
181  std::make_pair(
182  Filename,
184  T2DDynamic, new FM2DDynamic(Filename))));
185  return (*position.first).second.Map;
186  break;
187 
188  case T2DElectroStatic:
189  position = FieldmapDictionary.insert(
190  std::make_pair(
191  Filename,
193  T2DElectroStatic, new FM2DElectroStatic(Filename))));
194  return (*position.first).second.Map;
195  break;
196 
197  case T2DMagnetoStatic:
198  position = FieldmapDictionary.insert(
199  std::make_pair(
200  Filename,
202  T2DMagnetoStatic, new FM2DMagnetoStatic(Filename))));
203  return (*position.first).second.Map;
204  break;
205 
206  case T3DDynamic:
207  position = FieldmapDictionary.insert(
208  std::make_pair(
209  Filename,
211  T3DDynamic, new FM3DDynamic(Filename))));
212  return (*position.first).second.Map;
213  break;
214 
216  position = FieldmapDictionary.insert(
217  std::make_pair(
218  Filename, FieldmapDescription(
220  return (*position.first).second.Map;
221  break;
222 
223  case T3DMagnetoStatic:
224  position = FieldmapDictionary.insert(
225  std::make_pair(
226  Filename, FieldmapDescription(
227  T3DMagnetoStatic, new FM3DMagnetoStatic(Filename))));
228  return (*position.first).second.Map;
229  break;
230 
232  position = FieldmapDictionary.insert(
233  std::make_pair(
234  Filename, FieldmapDescription(
236  return (*position.first).second.Map;
237  break;
238 
239  case T3DDynamicH5Block:
240  if (fast) {
241  position = FieldmapDictionary.insert(
242  std::make_pair(
243  Filename,
245  T3DDynamic, new FM3DH5Block_nonscale(Filename))));
246  } else {
247  position = FieldmapDictionary.insert(
248  std::make_pair(
249  Filename,
251  T3DDynamic, new FM3DH5Block(Filename))));
252  }
253  return (*position.first).second.Map;
254  break;
255 
256  default:
257  throw GeneralClassicException("Fieldmap::getFieldmap()",
258  "Couldn't determine type of fieldmap in file \"" + Filename + "\"");
259  }
260  }
261 }
262 
263 std::vector<std::string> Fieldmap::getListFieldmapNames() {
264  std::vector<std::string> name_list;
265  for (std::map<std::string, FieldmapDescription>::const_iterator it = FieldmapDictionary.begin(); it != FieldmapDictionary.end(); ++ it) {
266  name_list.push_back((*it).first);
267  }
268  return name_list;
269 }
270 
271 void Fieldmap::deleteFieldmap(std::string Filename) {
272  freeMap(Filename);
273 }
274 
277  for (;it != FieldmapDictionary.end(); ++ it) {
278  delete it->second.Map;
279  it->second.Map = NULL;
280  }
281  FieldmapDictionary.clear();
282 }
283 
284 MapType Fieldmap::readHeader(std::string Filename) {
285  char magicnumber[5] = " ";
286  std::string buffer;
287  int lines_read_m = 0;
288 
289  // Check for default map(s).
290  if (Filename == "1DPROFILE1-DEFAULT")
291  return T1DProfile1;
292 
293  if (Filename.empty())
294  throw GeneralClassicException("Fieldmap::readHeader()",
295  "No field map file specified");
296 
297  if (!fs::exists(Filename))
298  throw GeneralClassicException("Fieldmap::readHeader()",
299  "File \"" + Filename + "\" doesn't exist");
300 
301  std::ifstream File(Filename.c_str());
302  if (!File.good()) {
303  std::cerr << "could not open file " << Filename << std::endl;
304  return UNKNOWN;
305  }
306 
307  getLine(File, lines_read_m, buffer);
308  std::istringstream interpreter(buffer, std::istringstream::in);
309 
310  interpreter.read(magicnumber, 4);
311 
312  if (std::strcmp(magicnumber, "3DDy") == 0)
313  return T3DDynamic;
314 
315  if (std::strcmp(magicnumber, "3DMa") == 0) {
316  char tmpString[21] = " ";
317  interpreter.read(tmpString, 20);
318 
319  if (std::strcmp(tmpString, "gnetoStatic_Extended") == 0)
321  else
322  return T3DMagnetoStatic;
323  }
324 
325  if (std::strcmp(magicnumber, "3DEl") == 0)
326  return T3DElectroStatic;
327 
328  if (std::strcmp(magicnumber, "2DDy") == 0) {
329  // char tmpString[14] = " ";
330  // interpreter.read(tmpString, 13);
331  return T2DDynamic;
332  }
333 
334  if (std::strcmp(magicnumber, "2DMa") == 0) {
335  // char tmpString[20] = " ";
336  // interpreter.read(tmpString, 19);
337  return T2DMagnetoStatic;
338  }
339 
340  if (std::strcmp(magicnumber, "2DEl") == 0) {
341  // char tmpString[20] = " ";
342  // interpreter.read(tmpString, 19);
343  return T2DElectroStatic;
344  }
345 
346  if (std::strcmp(magicnumber, "1DDy") == 0)
347  return T1DDynamic;
348 
349  if (std::strcmp(magicnumber, "1DMa") == 0)
350  return T1DMagnetoStatic;
351 
352  if (std::strcmp(magicnumber, "1DPr") == 0) {
353  // char tmpString[7] = " ";
354  // interpreter.read(tmpString, 6);
355  // if (strcmp(tmpString, "ofile1") == 0)
356  return T1DProfile1;
357  // if (strcmp(tmpString, "ofile2") == 0)
358  // return T1DProfile2;
359  }
360 
361  if (std::strcmp(magicnumber, "1DEl") == 0)
362  return T1DElectroStatic;
363 
364  if (std::strcmp(magicnumber, "\211HDF") == 0) {
365  h5_err_t h5err = 0;
366 #if defined (NDEBUG)
367  // mark variable as unused
368  (void)h5err;
369 #endif
370  char name[20];
371  h5_size_t len_name = sizeof(name);
372  h5_prop_t props = H5CreateFileProp ();
373  MPI_Comm comm = Ippl::getComm();
374  h5err = H5SetPropFileMPIOCollective (props, &comm);
375  PAssert (h5err != H5_ERR);
376  h5_file_t file = H5OpenFile (Filename.c_str(), H5_O_RDONLY, props);
377  PAssert (file != (h5_file_t)H5_ERR);
378  H5CloseProp (props);
379 
380  h5err = H5SetStep(file, 0);
381  PAssert (h5err != H5_ERR);
382 
383  h5_int64_t num_fields = H5BlockGetNumFields(file);
384  PAssert (num_fields != H5_ERR);
385  MapType maptype = UNKNOWN;
386 
387  for (h5_ssize_t i = 0; i < num_fields; ++ i) {
388  h5err = H5BlockGetFieldInfo(
389  file, (h5_size_t)i, name, len_name, NULL, NULL, NULL, NULL);
390  PAssert (h5err != H5_ERR);
391  // using field name "Bfield" and "Hfield" to distinguish the type
392  if (std::strcmp(name, "Bfield") == 0) {
393  maptype = T3DMagnetoStaticH5Block;
394  break;
395  } else if (std::strcmp(name, "Hfield") == 0) {
396  maptype = T3DDynamicH5Block;
397  break;
398  }
399  }
400  h5err = H5CloseFile(file);
401  PAssert (h5err != H5_ERR);
402  if (maptype != UNKNOWN)
403  return maptype;
404  }
405 
406  if (std::strcmp(magicnumber, "Astr") == 0) {
407  char tmpString[3] = " ";
408  interpreter.read(tmpString, 2);
409  if (std::strcmp(tmpString, "aE") == 0) {
410  return TAstraElectroStatic;
411  }
412  if (std::strcmp(tmpString, "aM") == 0) {
413  return TAstraMagnetoStatic;
414  }
415  if (std::strcmp(tmpString, "aD") == 0) {
416  return TAstraDynamic;
417  }
418  }
419 
420 
421  return UNKNOWN;
422 }
423 
424 void Fieldmap::readMap(std::string Filename) {
426  if (position != FieldmapDictionary.end())
427  if (!(*position).second.read) {
428  (*position).second.Map->readMap();
429  (*position).second.read = true;
430  }
431 }
432 
433 void Fieldmap::freeMap(std::string Filename) {
435  /*
436  FIXME: find( ) make problem, crashes
437  */
438  if (position != FieldmapDictionary.end()) {
439  if ((*position).second.RefCounter > 0) {
440  (*position).second.RefCounter--;
441  }
442 
443  if ((*position).second.RefCounter == 0) {
444  delete (*position).second.Map;
445  (*position).second.Map = NULL;
446  FieldmapDictionary.erase(position);
447  }
448  }
449 }
450 
451 void Fieldmap::checkMap(unsigned int accuracy,
452  std::pair<double, double> fieldDimensions,
453  double deltaZ,
454  const std::vector<double> &fourierCoefficients,
455  gsl_spline *splineCoefficients,
456  gsl_interp_accel *splineAccelerator) {
457  double length = fieldDimensions.second - fieldDimensions.first;
458  unsigned int sizeSampling = std::round(length / deltaZ);
459  std::vector<double> zSampling(sizeSampling);
460  zSampling[0] = fieldDimensions.first;
461  for (unsigned int i = 1; i < sizeSampling; ++ i) {
462  zSampling[i] = zSampling[i-1] + deltaZ;
463  }
464  checkMap(accuracy, length, zSampling, fourierCoefficients, splineCoefficients, splineAccelerator);
465 }
466 
467 void Fieldmap::checkMap(unsigned int accuracy,
468  double length,
469  const std::vector<double> &zSampling,
470  const std::vector<double> &fourierCoefficients,
471  gsl_spline *splineCoefficients,
472  gsl_interp_accel *splineAccelerator) {
473  double error = 0.0;
474  double maxDiff = 0.0;
475  double ezMax = 0.0;
476  double ezSquare = 0.0;
477  size_t lastDot = Filename_m.find_last_of(".");
478  size_t lastSlash = Filename_m.find_last_of("/");
479  lastSlash = (lastSlash == std::string::npos)? 0: lastSlash + 1;
480 
481  auto opal = OpalData::getInstance();
482  std::ofstream out;
483  if (Ippl::myNode() == 0 && !opal->isOptimizerRun()) {
484  std::string fname = Util::combineFilePath({
485  opal->getAuxiliaryOutputDirectory(),
486  Filename_m.substr(lastSlash, lastDot) + ".check"
487  });
488  out.open(fname);
489  out << "# z original reproduced\n";
490  }
491  auto it = zSampling.begin();
492  auto end = zSampling.end();
493  for (; it != end; ++ it) {
494  const double kz = Physics::two_pi * (*it / length + 0.5);
495  double onAxisFieldCheck = fourierCoefficients[0];
496  unsigned int n = 1;
497  for (unsigned int l = 1; l < accuracy; ++l, n += 2) {
498  double coskzl = std::cos(kz * l);
499  double sinkzl = std::sin(kz * l);
500 
501  onAxisFieldCheck += (fourierCoefficients[n] * coskzl - fourierCoefficients[n + 1] * sinkzl);
502  }
503  double ez = gsl_spline_eval(splineCoefficients, *it, splineAccelerator);
504  double difference = std::abs(ez - onAxisFieldCheck);
505  maxDiff = difference > maxDiff? difference: maxDiff;
506  ezMax = std::abs(ez) > ezMax? std::abs(ez): ezMax;
507  error += std::pow(difference, 2.0);
508  ezSquare += std::pow(ez, 2.0);
509 
510  if (Ippl::myNode() == 0 && !opal->isOptimizerRun()) {
511  out << std::setw(16) << std::setprecision(8) << *it
512  << std::setw(16) << std::setprecision(8) << ez
513  << std::setw(16) << std::setprecision(8) << onAxisFieldCheck
514  << std::endl;
515  }
516  }
517  out.close();
518 
519  if (std::sqrt(error / ezSquare) > 1e-1 || maxDiff > 1e-1 * ezMax) {
520  lowResolutionWarning(std::sqrt(error / ezSquare), maxDiff / ezMax);
521 
522  throw GeneralClassicException("Fieldmap::checkMap",
523  "Field map can't be reproduced properly with the given number of fourier components");
524  }
525  if (std::sqrt(error / ezSquare) > 1e-2 || maxDiff > 1e-2 * ezMax) {
526  lowResolutionWarning(std::sqrt(error / ezSquare), maxDiff / ezMax);
527  }
528 }
529 
530 void Fieldmap::setEdgeConstants(const double &/*bendAngle*/, const double &/*entranceAngle*/, const double &/*exitAngle*/)
531 {};
532 
533 void Fieldmap::setFieldLength(const double &)
534 {};
535 
536 void Fieldmap::getLine(std::ifstream &in, int &lines_read, std::string &buffer) {
537  size_t firstof = 0;
538  size_t lastof;
539 
540  do {
541  ++ lines_read;
542  in.getline(buffer_m, READ_BUFFER_LENGTH);
543 
544  buffer = std::string(buffer_m);
545 
546  size_t comment = buffer.find("#");
547  buffer = buffer.substr(0, comment);
548 
549  lastof = buffer.find_last_of(alpha_numeric);
550  firstof = buffer.find_first_of(alpha_numeric);
551  } while(!in.eof() && lastof == std::string::npos);
552 
553  if (firstof != std::string::npos) {
554  buffer = buffer.substr(firstof, lastof - firstof + 1);
555  }
556 }
557 
558 bool Fieldmap::interpreteEOF(std::ifstream &in) {
559  while(!in.eof()) {
560  ++lines_read_m;
561  in.getline(buffer_m, READ_BUFFER_LENGTH);
562  std::string buffer(buffer_m);
563  size_t comment = buffer.find_first_of("#");
564  buffer = buffer.substr(0, comment);
565  size_t lasto = buffer.find_first_of(alpha_numeric);
566  if (lasto != std::string::npos) {
568  return false;
569  }
570  }
571  return true;
572 }
573 
574 void Fieldmap::interpretWarning(const std::ios_base::iostate &state,
575  const bool &read_all,
576  const std::string &expecting,
577  const std::string &found) {
578  std::stringstream errormsg;
579  errormsg << "THERE SEEMS TO BE SOMETHING WRONG WITH YOUR FIELD MAP '" << Filename_m << "'.\n";
580  if (!read_all) {
581  errormsg << "Didn't find enough values!" << std::endl;
582  } else if (state & std::ios_base::eofbit) {
583  errormsg << "Found more values than expected!" << std::endl;
584  } else if (state & std::ios_base::failbit) {
585  errormsg << "Found wrong type of values!" << "\n"
586  << "expecting: '" << expecting << "' on line " << lines_read_m << ",\n"
587  << "instead found: '" << found << "'." << std::endl;
588  }
589  throw GeneralClassicException("Fieldmap::interpretWarning()",
590  errormsg.str());
591 }
592 
594  std::stringstream errormsg;
595  errormsg << "THERE SEEMS TO BE SOMETHING WRONG WITH YOUR FIELD MAP '" << Filename_m << "'.\n"
596  << "There are only " << lines_read_m - 1 << " lines in the file, expecting more.\n"
597  << "Please check the section about field maps in the user manual.";
598 
599  throw GeneralClassicException("Fieldmap::missingValuesWarning()",
600  errormsg.str());
601 }
602 
604  std::stringstream errormsg;
605  errormsg << "THERE SEEMS TO BE SOMETHING WRONG WITH YOUR FIELD MAP '" << Filename_m << "'.\n"
606  << "There are too many lines in the file, expecting only " << lines_read_m << " lines.\n"
607  << "Please check the section about field maps in the user manual.";
608 
609  throw GeneralClassicException("Fieldmap::exceedingValuesWarning()",
610  errormsg.str());
611 }
612 
614  std::stringstream errormsg;
615  errormsg << "DISABLING FIELD MAP '" + Filename_m + "' DUE TO PARSING ERRORS." ;
616 
617  throw GeneralClassicException("Fieldmap::disableFieldmapsWarning()",
618  errormsg.str());
619 }
620 
622  std::stringstream errormsg;
623  errormsg << "DISABLING FIELD MAP '" << Filename_m << "' SINCE FILE COULDN'T BE FOUND!";
624 
625  throw GeneralClassicException("Fieldmap::noFieldmapsWarning()",
626  errormsg.str());
627 }
628 
629 void Fieldmap::lowResolutionWarning(double squareError, double maxError) {
630  std::stringstream errormsg;
631  errormsg << "IT SEEMS THAT YOU USE TOO FEW FOURIER COMPONENTS TO SUFFICIENTLY WELL\n"
632  << "RESOLVE THE FIELD MAP '" << Filename_m << "'.\n"
633  << "PLEASE INCREASE THE NUMBER OF FOURIER COMPONENTS!\n"
634  << "The ratio (e_i - E_i)^2 / E_i^2 is " << std::to_string(squareError) << " and\n"
635  << "the ratio (max_i(|e_i - E_i|) / max_i(|E_i|) is " << std::to_string(maxError) << ".\n"
636  << "Here E_i is the field as in the field map and e_i is the reconstructed field.\n"
637  << "The lower limit for the two ratios is 1e-2\n"
638  << "Have a look into the directory "
640  << " for a reconstruction of the field map.\n";
641  std::string errormsg_str = typeset_msg(errormsg.str(), "warning");
642 
643  ERRORMSG(errormsg_str << "\n" << endl);
644 
645  if (Ippl::myNode() == 0) {
646  std::ofstream omsg("errormsg.txt", std::ios_base::app);
647  omsg << errormsg.str() << std::endl;
648  omsg.close();
649  }
650 }
651 
652 std::string Fieldmap::typeset_msg(const std::string &msg, const std::string &title) {
653  static std::string frame("* ******************************************************************************\n");
654  static unsigned int frame_width = frame.length() - 5;
655  static std::string closure(" *\n");
656 
657  std::string return_string("\n" + frame);
658 
659  int remaining_length = msg.length();
660  unsigned int current_position = 0;
661 
662  unsigned int ii = 0;
663  for (; ii < title.length(); ++ ii) {
664  char c = title[ii];
665  c = std::toupper(c);
666  return_string.replace(15 + 2 * ii, 1, " ");
667  return_string.replace(16 + 2 * ii, 1, &c, 1);
668  }
669  return_string.replace(15 + 2 * ii, 1, " ");
670 
671  while(remaining_length > 0) {
672  size_t eol = msg.find("\n", current_position);
673  std::string next_to_process;
674  if (eol != std::string::npos) {
675  next_to_process = msg.substr(current_position, eol - current_position);
676  } else {
677  next_to_process = msg.substr(current_position);
678  eol = msg.length();
679  }
680 
681  if (eol - current_position < frame_width) {
682  return_string += "* " + next_to_process + closure.substr(eol - current_position + 2);
683  } else {
684  unsigned int last_space = next_to_process.rfind(" ", frame_width);
685  if (last_space > 0) {
686  if (last_space < frame_width) {
687  return_string += "* " + next_to_process.substr(0, last_space) + closure.substr(last_space + 2);
688  } else {
689  return_string += "* " + next_to_process.substr(0, last_space) + " *\n";
690  }
691  if (next_to_process.length() - last_space + 1 < frame_width) {
692  return_string += "* " + next_to_process.substr(last_space + 1) + closure.substr(next_to_process.length() - last_space + 1);
693  } else {
694  return_string += "* " + next_to_process.substr(last_space + 1) + " *\n";
695  }
696  } else {
697  return_string += "* " + next_to_process + " *\n";
698  }
699  }
700 
701  current_position = eol + 1;
702  remaining_length = msg.length() - current_position;
703  }
704  return_string += frame;
705 
706  return return_string;
707 }
708 
709 void Fieldmap::getOnaxisEz(std::vector<std::pair<double, double> > &/*onaxis*/)
710 { }
711 
712 void Fieldmap::get1DProfile1EngeCoeffs(std::vector<double> &/*engeCoeffsEntry*/,
713  std::vector<double> &/*engeCoeffsExit*/) {
714 
715 }
716 
717 void Fieldmap::get1DProfile1EntranceParam(double &/*entranceParameter1*/,
718  double &/*entranceParameter2*/,
719  double &/*entranceParameter3*/) {
720 
721 }
722 
723 void Fieldmap::get1DProfile1ExitParam(double &/*exitParameter1*/,
724  double &/*exitParameter2*/,
725  double &/*exitParameter3*/) {
726 
727 }
728 
730  return 0.0;
731 }
732 
733 void Fieldmap::setFieldGap(double /*gap*/) {
734 
735 }
736 
737 void Fieldmap::write3DField(unsigned int nx,
738  unsigned int ny,
739  unsigned int nz,
740  const std::pair<double, double> &xrange,
741  const std::pair<double, double> &yrange,
742  const std::pair<double, double> &zrange,
743  const std::vector<Vector_t> &ef,
744  const std::vector<Vector_t> &bf) {
745  const size_t numpoints = nx * ny * nz;
746  if (Ippl::myNode() != 0 ||
747  (ef.size() != numpoints && bf.size() != numpoints)) return;
748 
749  boost::filesystem::path p(Filename_m);
750  std::string fname = Util::combineFilePath({
752  p.stem().string() + ".vtk"
753  });
754  std::ofstream of;
755  of.open (fname);
756  PAssert (of.is_open ());
757  of.precision (6);
758 
759  const double hx = (xrange.second - xrange.first) / (nx - 1);
760  const double hy = (yrange.second - yrange.first) / (ny - 1);
761  const double hz = (zrange.second - zrange.first) / (nz - 1);
762 
763  of << "# vtk DataFile Version 2.0" << std::endl;
764  of << "generated by 3D fieldmaps" << std::endl;
765  of << "ASCII" << std::endl << std::endl;
766  of << "DATASET RECTILINEAR_GRID" << std::endl;
767  of << "DIMENSIONS " << nx << " " << ny << " " << nz << std::endl;
768 
769  of << "X_COORDINATES " << nx << " float" << std::endl;
770  of << xrange.first;
771  for (unsigned int i = 1; i < nx - 1; ++ i) {
772  of << " " << xrange.first + i * hx;
773  }
774  of << " " << xrange.second << std::endl;
775 
776  of << "Y_COORDINATES " << ny << " float" << std::endl;
777  of << yrange.first;
778  for (unsigned int i = 1; i < ny - 1; ++ i) {
779  of << " " << yrange.first + i * hy;
780  }
781  of << " " << yrange.second << std::endl;
782 
783  of << "Z_COORDINATES " << nz << " float" << std::endl;
784  of << zrange.first;
785  for (unsigned int i = 1; i < nz - 1; ++ i) {
786  of << " " << zrange.first + i * hz;
787  }
788  of << " " << zrange.second << std::endl;
789 
790  of << "POINT_DATA " << numpoints << std::endl;
791 
792  if (ef.size() == numpoints) {
793  of << "VECTORS EField float" << std::endl;
794  // of << "LOOKUP_TABLE default" << std::endl;
795  for (size_t i = 0; i < numpoints; ++ i) {
796  of << ef[i](0) << " " << ef[i](1) << " " << ef[i](2) << std::endl;
797  }
798  // of << std::endl;
799  }
800 
801  if (bf.size() == numpoints) {
802  of << "VECTORS BField float" << std::endl;
803  // of << "LOOKUP_TABLE default" << std::endl;
804  for (size_t i = 0; i < numpoints; ++ i) {
805  of << bf[i](0) << " " << bf[i](1) << " " << bf[i](2) << std::endl;
806  }
807  // of << std::endl;
808  }
809 }
810 
812 REGISTER_PARSE_TYPE(unsigned int);
814 REGISTER_PARSE_TYPE(std::string);
815 
816 std::string Fieldmap::alpha_numeric("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-+\211");
817 std::map<std::string, Fieldmap::FieldmapDescription> Fieldmap::FieldmapDictionary = std::map<std::string, Fieldmap::FieldmapDescription>();
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
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
#define REGISTER_PARSE_TYPE(X)
Definition: Fieldmap.cpp:47
#define READ_BUFFER_LENGTH
Definition: Fieldmap.h:4
MapType
Definition: Fieldmap.h:14
@ T1DProfile2
Definition: Fieldmap.h:23
@ TAstraElectroStatic
Definition: Fieldmap.h:19
@ T3DElectroStatic
Definition: Fieldmap.h:31
@ T3DDynamic
Definition: Fieldmap.h:30
@ T2DDynamic
Definition: Fieldmap.h:24
@ T2DMagnetoStatic
Definition: Fieldmap.h:28
@ T3DMagnetoStatic
Definition: Fieldmap.h:32
@ T1DDynamic
Definition: Fieldmap.h:16
@ UNKNOWN
Definition: Fieldmap.h:15
@ TAstraMagnetoStatic
Definition: Fieldmap.h:21
@ T3DMagnetoStatic_Extended
Definition: Fieldmap.h:33
@ T1DProfile1
Definition: Fieldmap.h:22
@ T3DMagnetoStaticH5Block
Definition: Fieldmap.h:34
@ T3DDynamicH5Block
Definition: Fieldmap.h:35
@ T1DElectroStatic
Definition: Fieldmap.h:18
@ T1DMagnetoStatic
Definition: Fieldmap.h:20
@ T2DElectroStatic
Definition: Fieldmap.h:26
@ TAstraDynamic
Definition: Fieldmap.h:17
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
#define ERRORMSG(msg)
Definition: IpplInfo.h:350
#define PAssert(c)
Definition: PAssert.h:102
const std::string name
constexpr double two_pi
The value of.
Definition: Physics.h:33
constexpr double e
The value of.
Definition: Physics.h:39
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:51
std::string::iterator iterator
Definition: MSLang.h:16
std::string combineFilePath(std::initializer_list< std::string > ilist)
Definition: Util.cpp:139
boost::function< boost::tuple< double, bool >arguments_t)> type
Definition: function.hpp:21
FRONT * fs
Definition: hypervolume.cpp:59
static OpalData * getInstance()
Definition: OpalData.cpp:195
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
Definition: OpalData.cpp:650
static Fieldmap * getFieldmap(std::string Filename, bool fast=false)
Definition: Fieldmap.cpp:50
static void deleteFieldmap(std::string Filename)
Definition: Fieldmap.cpp:271
static std::string alpha_numeric
Definition: Fieldmap.h:182
static std::map< std::string, FieldmapDescription > FieldmapDictionary
Definition: Fieldmap.h:197
static MapType readHeader(std::string Filename)
Definition: Fieldmap.cpp:284
bool interpreteEOF(std::ifstream &in)
Definition: Fieldmap.cpp:558
virtual void setFieldLength(const double &)
Definition: Fieldmap.cpp:533
virtual void freeMap()=0
void missingValuesWarning()
Definition: Fieldmap.cpp:593
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &onaxis)
Definition: Fieldmap.cpp:709
virtual void get1DProfile1EntranceParam(double &entranceParameter1, double &entranceParameter2, double &entranceParameter3)
Definition: Fieldmap.cpp:717
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 lowResolutionWarning(double squareError, double maxError)
Definition: Fieldmap.cpp:629
void interpretWarning(const std::ios_base::iostate &state, const bool &read_all, const std::string &error_msg, const std::string &found)
Definition: Fieldmap.cpp:574
void disableFieldmapWarning()
Definition: Fieldmap.cpp:613
static char buffer_m[256]
Definition: Fieldmap.h:181
int lines_read_m
Definition: Fieldmap.h:118
static std::vector< std::string > getListFieldmapNames()
Definition: Fieldmap.cpp:263
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:652
static void clearDictionary()
Definition: Fieldmap.cpp:275
virtual double getFieldGap()
Definition: Fieldmap.cpp:729
virtual void setEdgeConstants(const double &bendAngle, const double &entranceAngle, const double &exitAngle)
Definition: Fieldmap.cpp:530
std::string Filename_m
Definition: Fieldmap.h:117
virtual void setFieldGap(double gap)
Definition: Fieldmap.cpp:733
void exceedingValuesWarning()
Definition: Fieldmap.cpp:603
virtual void get1DProfile1EngeCoeffs(std::vector< double > &engeCoeffsEntry, std::vector< double > &engeCoeffsExit)
Definition: Fieldmap.cpp:712
void getLine(std::ifstream &in, std::string &buffer)
Definition: Fieldmap.h:121
void noFieldmapWarning()
Definition: Fieldmap.cpp:621
virtual void get1DProfile1ExitParam(double &exitParameter1, double &exitParameter2, double &exitParameter3)
Definition: Fieldmap.cpp:723
virtual void readMap()=0
void write3DField(unsigned int nx, unsigned int ny, unsigned int nz, const std::pair< double, double > &xrange, const std::pair< double, double > &yrange, const std::pair< double, double > &zrange, const std::vector< Vector_t > &ef, const std::vector< Vector_t > &bf)
Definition: Fieldmap.cpp:737
static MPI_Comm getComm()
Definition: IpplInfo.h:152
static int myNode()
Definition: IpplInfo.cpp:691