OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
Fieldmap.cpp
Go to the documentation of this file.
1 #include "Fields/Fieldmap.h"
2 
3 #include "Utility/PAssert.h"
4 
12 #include "Fields/FM1DDynamic.h"
18 #include "Fields/FM1DProfile1.h"
19 #include "Fields/FM1DProfile2.h"
20 #include "Fields/FM2DDynamic.h"
23 #include "Fields/FM3DDynamic.h"
24 #include "Fields/FM3DH5Block.h"
25 #include "Fields/FM3DH5BlockBase.h"
30 #include "Physics/Physics.h"
32 #include "Utilities/Options.h"
33 #include "Utilities/Util.h"
34 
35 #include "H5hut.h"
36 
37 #include <cmath>
38 #include <iostream>
39 #include <filesystem>
40 #include <fstream>
41 #include <ios>
42 
43 namespace fs = std::filesystem;
44 
45 #define REGISTER_PARSE_TYPE(X) template <> struct _Fieldmap::TypeParseTraits<X> \
46  { static const char* name; } ; const char* _Fieldmap::TypeParseTraits<X>::name = #X
47 
48 Fieldmap _Fieldmap::getFieldmap(std::string Filename, bool fast) {
50  if (position != FieldmapDictionary.end()) {
51  (*position).second.RefCounter++;
52  return (*position).second.Map;
53  } else {
54  MapType type;
56  type = readHeader(Filename);
57  switch(type) {
58  case T1DDynamic:
59  if (fast) {
60  position = FieldmapDictionary.insert(
61  std::make_pair(
62  Filename,
64  _FM1DDynamic_fast::create(Filename))));
65  } else {
66  position = FieldmapDictionary.insert(
67  std::make_pair(
68  Filename,
70  _FM1DDynamic::create(Filename))));
71  }
72  return (*position.first).second.Map;
73  break;
74 
75  case TAstraDynamic:
76  if (fast) {
77  position = FieldmapDictionary.insert(
78  std::make_pair(
79  Filename,
81  _Astra1DDynamic_fast::create(Filename))));
82  } else {
83  position = FieldmapDictionary.insert(
84  std::make_pair(
85  Filename,
87  _Astra1DDynamic::create(Filename))));
88  }
89  return (*position.first).second.Map;
90  break;
91 
92  case T1DElectroStatic:
93  if (fast) {
94  position = FieldmapDictionary.insert(
95  std::make_pair(
96  Filename,
99  } else {
100  position = FieldmapDictionary.insert(
101  std::make_pair(
102  Filename,
104  _FM1DElectroStatic::create(Filename))));
105  }
106  return (*position.first).second.Map;
107  break;
108 
109  case TAstraElectroStatic:
110  if (fast) {
111  position = FieldmapDictionary.insert(
112  std::make_pair(
113  Filename,
116  } else {
117  position = FieldmapDictionary.insert(
118  std::make_pair(
119  Filename,
121  _Astra1DElectroStatic::create(Filename))));
122  }
123  return (*position.first).second.Map;
124  break;
125 
126  case T1DMagnetoStatic:
127  if (fast) {
128  position = FieldmapDictionary.insert(
129  std::make_pair(
130  Filename,
132  _FM1DMagnetoStatic_fast::create(Filename))));
133  } else {
134  position = FieldmapDictionary.insert(
135  std::make_pair(
136  Filename,
138  _FM1DMagnetoStatic::create(Filename))));
139  }
140  return (*position.first).second.Map;
141  break;
142 
143  case TAstraMagnetoStatic:
144  if (fast) {
145  position = FieldmapDictionary.insert(
146  std::make_pair(
147  Filename,
150  } else {
151  position = FieldmapDictionary.insert(
152  std::make_pair(
153  Filename,
155  _Astra1DMagnetoStatic::create(Filename))));
156  }
157  return (*position.first).second.Map;
158  break;
159 
160  case T1DProfile1:
161  position = FieldmapDictionary.insert(
162  std::make_pair(
163  Filename,
165  _FM1DProfile1::create(Filename))));
166  return (*position.first).second.Map;
167  break;
168 
169  case T1DProfile2:
170  position = FieldmapDictionary.insert(
171  std::make_pair(
172  Filename,
174  _FM1DProfile2::create(Filename))));
175  return (*position.first).second.Map;
176  break;
177 
178  case T2DDynamic:
179  position = FieldmapDictionary.insert(
180  std::make_pair(
181  Filename,
183  _FM2DDynamic::create(Filename))));
184  return (*position.first).second.Map;
185  break;
186 
187  case T2DElectroStatic:
188  position = FieldmapDictionary.insert(
189  std::make_pair(
190  Filename,
192  _FM2DElectroStatic::create(Filename))));
193  return (*position.first).second.Map;
194  break;
195 
196  case T2DMagnetoStatic:
197  position = FieldmapDictionary.insert(
198  std::make_pair(
199  Filename,
201  _FM2DMagnetoStatic::create(Filename))));
202  return (*position.first).second.Map;
203  break;
204 
205  case T3DDynamic:
206  position = FieldmapDictionary.insert(
207  std::make_pair(
208  Filename,
210  _FM3DDynamic::create(Filename))));
211  return (*position.first).second.Map;
212  break;
213 
215  position = FieldmapDictionary.insert(
216  std::make_pair(
219  return (*position.first).second.Map;
220  break;
221 
222  case T3DMagnetoStatic:
223  position = FieldmapDictionary.insert(
224  std::make_pair(
226  _FM3DMagnetoStatic::create(Filename))));
227  return (*position.first).second.Map;
228  break;
229 
231  position = FieldmapDictionary.insert(
232  std::make_pair(
235  return (*position.first).second.Map;
236  break;
237 
238  case T3DDynamicH5Block:
239  if (fast) {
240  position = FieldmapDictionary.insert(
241  std::make_pair(
242  Filename,
244  _FM3DH5Block_nonscale::create(Filename))));
245  } else {
246  position = FieldmapDictionary.insert(
247  std::make_pair(
248  Filename,
250  _FM3DH5Block::create(Filename))));
251  }
252  return (*position.first).second.Map;
253  break;
254 
255  default:
256  throw GeneralClassicException("_Fieldmap::getFieldmap()",
257  "Couldn't determine type of fieldmap in file \"" + Filename + "\"");
258  }
259  }
260 }
261 
262 std::vector<std::string> _Fieldmap::getListFieldmapNames() {
263  std::vector<std::string> name_list;
264  for (std::map<std::string, FieldmapDescription>::const_iterator it = FieldmapDictionary.begin(); it != FieldmapDictionary.end(); ++ it) {
265  name_list.push_back((*it).first);
266  }
267  return name_list;
268 }
269 
270 void _Fieldmap::deleteFieldmap(std::string Filename) {
271  freeMap(Filename);
272 }
273 
276  for (;it != FieldmapDictionary.end(); ++ it) {
277  it->second.Map.reset();
278  }
279  FieldmapDictionary.clear();
280 }
281 
282 MapType _Fieldmap::readHeader(std::string Filename) {
283  char magicnumber[5] = " ";
284  std::string buffer;
285  int lines_read_m = 0;
286 
287  // Check for default map(s).
288  if (Filename == "1DPROFILE1-DEFAULT")
289  return T1DProfile1;
290 
291  if (Filename.empty())
292  throw GeneralClassicException("_Fieldmap::readHeader()",
293  "No field map file specified");
294 
295  if (!fs::exists(Filename))
296  throw GeneralClassicException("_Fieldmap::readHeader()",
297  "File '" + Filename + "' doesn't exist");
298 
299  std::ifstream File(Filename.c_str());
300  if (!File.good()) {
301  std::cerr << "could not open file " << Filename << std::endl;
302  return UNKNOWN;
303  }
304 
305  getLine(File, lines_read_m, buffer);
306  std::istringstream interpreter(buffer, std::istringstream::in);
307 
308  interpreter.read(magicnumber, 4);
309 
310  if (std::strcmp(magicnumber, "3DDy") == 0)
311  return T3DDynamic;
312 
313  if (std::strcmp(magicnumber, "3DMa") == 0) {
314  char tmpString[21] = " ";
315  interpreter.read(tmpString, 20);
316 
317  if (std::strcmp(tmpString, "gnetoStatic_Extended") == 0)
319  else
320  return T3DMagnetoStatic;
321  }
322 
323  if (std::strcmp(magicnumber, "3DEl") == 0)
324  return T3DElectroStatic;
325 
326  if (std::strcmp(magicnumber, "2DDy") == 0) {
327  // char tmpString[14] = " ";
328  // interpreter.read(tmpString, 13);
329  return T2DDynamic;
330  }
331 
332  if (std::strcmp(magicnumber, "2DMa") == 0) {
333  // char tmpString[20] = " ";
334  // interpreter.read(tmpString, 19);
335  return T2DMagnetoStatic;
336  }
337 
338  if (std::strcmp(magicnumber, "2DEl") == 0) {
339  // char tmpString[20] = " ";
340  // interpreter.read(tmpString, 19);
341  return T2DElectroStatic;
342  }
343 
344  if (std::strcmp(magicnumber, "1DDy") == 0)
345  return T1DDynamic;
346 
347  if (std::strcmp(magicnumber, "1DMa") == 0)
348  return T1DMagnetoStatic;
349 
350  if (std::strcmp(magicnumber, "1DPr") == 0) {
351  // char tmpString[7] = " ";
352  // interpreter.read(tmpString, 6);
353  // if (strcmp(tmpString, "ofile1") == 0)
354  return T1DProfile1;
355  // if (strcmp(tmpString, "ofile2") == 0)
356  // return T1DProfile2;
357  }
358 
359  if (std::strcmp(magicnumber, "1DEl") == 0)
360  return T1DElectroStatic;
361 
362  if (std::strcmp(magicnumber, "\211HDF") == 0) {
363  h5_err_t h5err = 0;
364 #if defined (NDEBUG)
365  // mark variable as unused
366  (void)h5err;
367 #endif
368  char name[20];
369  h5_size_t len_name = sizeof(name);
370  h5_prop_t props = H5CreateFileProp ();
371  MPI_Comm comm = Ippl::getComm();
372  h5err = H5SetPropFileMPIOCollective (props, &comm);
373  PAssert (h5err != H5_ERR);
374  h5_file_t file = H5OpenFile (Filename.c_str(), H5_O_RDONLY, props);
375  PAssert (file != (h5_file_t)H5_ERR);
376  H5CloseProp (props);
377 
378  h5err = H5SetStep(file, 0);
379  PAssert (h5err != H5_ERR);
380 
381  h5_int64_t num_fields = H5BlockGetNumFields(file);
382  PAssert (num_fields != H5_ERR);
383  MapType maptype = UNKNOWN;
384 
385  for (h5_ssize_t i = 0; i < num_fields; ++ i) {
386  h5err = H5BlockGetFieldInfo(
387  file, (h5_size_t)i, name, len_name, nullptr, nullptr, nullptr, nullptr);
388  PAssert (h5err != H5_ERR);
389  // using field name "Bfield" and "Hfield" to distinguish the type
390  if (std::strcmp(name, "Bfield") == 0) {
391  maptype = T3DMagnetoStaticH5Block;
392  break;
393  } else if (std::strcmp(name, "Hfield") == 0) {
394  maptype = T3DDynamicH5Block;
395  break;
396  }
397  }
398  h5err = H5CloseFile(file);
399  PAssert (h5err != H5_ERR);
400  if (maptype != UNKNOWN)
401  return maptype;
402  }
403 
404  if (std::strcmp(magicnumber, "Astr") == 0) {
405  char tmpString[3] = " ";
406  interpreter.read(tmpString, 2);
407  if (std::strcmp(tmpString, "aE") == 0) {
408  return TAstraElectroStatic;
409  }
410  if (std::strcmp(tmpString, "aM") == 0) {
411  return TAstraMagnetoStatic;
412  }
413  if (std::strcmp(tmpString, "aD") == 0) {
414  return TAstraDynamic;
415  }
416  }
417 
418 
419  return UNKNOWN;
420 }
421 
422 void _Fieldmap::readMap(std::string Filename) {
424  if (position != FieldmapDictionary.end())
425  if (!(*position).second.read) {
426  (*position).second.Map->readMap();
427  (*position).second.read = true;
428  }
429 }
430 
431 void _Fieldmap::freeMap(std::string Filename) {
433  /*
434  FIXME: find( ) make problem, crashes
435  */
436  if (position != FieldmapDictionary.end()) {
437  if ((*position).second.RefCounter > 0) {
438  (*position).second.RefCounter--;
439  }
440 
441  if ((*position).second.RefCounter == 0) {
442  (*position).second.Map.reset();
443  FieldmapDictionary.erase(position);
444  }
445  }
446 }
447 
448 void _Fieldmap::checkMap(unsigned int accuracy,
449  std::pair<double, double> fieldDimensions,
450  double deltaZ,
451  const std::vector<double> &fourierCoefficients,
452  gsl_spline *splineCoefficients,
453  gsl_interp_accel *splineAccelerator) {
454  double length = fieldDimensions.second - fieldDimensions.first;
455  unsigned int sizeSampling = std::round(length / deltaZ);
456  std::vector<double> zSampling(sizeSampling);
457  zSampling[0] = fieldDimensions.first;
458  for (unsigned int i = 1; i < sizeSampling; ++ i) {
459  zSampling[i] = zSampling[i-1] + deltaZ;
460  }
461  checkMap(accuracy, length, zSampling, fourierCoefficients, splineCoefficients, splineAccelerator);
462 }
463 
464 void _Fieldmap::checkMap(unsigned int accuracy,
465  double length,
466  const std::vector<double> &zSampling,
467  const std::vector<double> &fourierCoefficients,
468  gsl_spline *splineCoefficients,
469  gsl_interp_accel *splineAccelerator) {
470  double error = 0.0;
471  double maxDiff = 0.0;
472  double ezMax = 0.0;
473  double ezSquare = 0.0;
474  size_t lastDot = Filename_m.find_last_of(".");
475  size_t lastSlash = Filename_m.find_last_of("/");
476  lastSlash = (lastSlash == std::string::npos)? 0: lastSlash + 1;
477 
478  auto opal = OpalData::getInstance();
479  std::ofstream out;
480  if (Ippl::myNode() == 0 && !opal->isOptimizerRun()) {
481  std::string fname = Util::combineFilePath({
482  opal->getAuxiliaryOutputDirectory(),
483  Filename_m.substr(lastSlash, lastDot) + ".check"
484  });
485  out.open(fname);
486  out << "# z original reproduced\n";
487  }
488  auto it = zSampling.begin();
489  auto end = zSampling.end();
490  for (; it != end; ++ it) {
491  const double kz = Physics::two_pi * (*it / length + 0.5);
492  double onAxisFieldCheck = fourierCoefficients[0];
493  unsigned int n = 1;
494  for (unsigned int l = 1; l < accuracy; ++l, n += 2) {
495  double coskzl = std::cos(kz * l);
496  double sinkzl = std::sin(kz * l);
497 
498  onAxisFieldCheck += (fourierCoefficients[n] * coskzl - fourierCoefficients[n + 1] * sinkzl);
499  }
500  double ez = gsl_spline_eval(splineCoefficients, *it, splineAccelerator);
501  double difference = std::abs(ez - onAxisFieldCheck);
502  maxDiff = difference > maxDiff? difference: maxDiff;
503  ezMax = std::abs(ez) > ezMax? std::abs(ez): ezMax;
504  error += std::pow(difference, 2.0);
505  ezSquare += std::pow(ez, 2.0);
506 
507  if (Ippl::myNode() == 0 && !opal->isOptimizerRun()) {
508  out << std::setw(16) << std::setprecision(8) << *it
509  << std::setw(16) << std::setprecision(8) << ez
510  << std::setw(16) << std::setprecision(8) << onAxisFieldCheck
511  << std::endl;
512  }
513  }
514  out.close();
515 
516  if (std::sqrt(error / ezSquare) > 1e-1 || maxDiff > 1e-1 * ezMax) {
517  lowResolutionWarning(std::sqrt(error / ezSquare), maxDiff / ezMax);
518 
519  throw GeneralClassicException("_Fieldmap::checkMap",
520  "Field map can't be reproduced properly with the given number of fourier components");
521  }
522  if (std::sqrt(error / ezSquare) > 1e-2 || maxDiff > 1e-2 * ezMax) {
523  lowResolutionWarning(std::sqrt(error / ezSquare), maxDiff / ezMax);
524  }
525 }
526 
527 void _Fieldmap::setEdgeConstants(const double &/*bendAngle*/, const double &/*entranceAngle*/, const double &/*exitAngle*/)
528 {};
529 
530 void _Fieldmap::setFieldLength(const double &)
531 {};
532 
533 void _Fieldmap::getLine(std::ifstream &in, int &lines_read, std::string &buffer) {
534  size_t firstof = 0;
535  size_t lastof;
536 
537  do {
538  ++ lines_read;
539  in.getline(buffer_m, READ_BUFFER_LENGTH);
540 
541  buffer = std::string(buffer_m);
542 
543  size_t comment = buffer.find("#");
544  buffer = buffer.substr(0, comment);
545 
546  lastof = buffer.find_last_of(alpha_numeric);
547  firstof = buffer.find_first_of(alpha_numeric);
548  } while(!in.eof() && lastof == std::string::npos);
549 
550  if (firstof != std::string::npos) {
551  buffer = buffer.substr(firstof, lastof - firstof + 1);
552  }
553 }
554 
555 bool _Fieldmap::interpreteEOF(std::ifstream &in) {
556  while(!in.eof()) {
557  ++lines_read_m;
558  in.getline(buffer_m, READ_BUFFER_LENGTH);
559  std::string buffer(buffer_m);
560  size_t comment = buffer.find_first_of("#");
561  buffer = buffer.substr(0, comment);
562  size_t lasto = buffer.find_first_of(alpha_numeric);
563  if (lasto != std::string::npos) {
565  return false;
566  }
567  }
568  return true;
569 }
570 
571 void _Fieldmap::interpretWarning(const std::ios_base::iostate &state,
572  const bool &read_all,
573  const std::string &expecting,
574  const std::string &found) {
575  std::stringstream errormsg;
576  errormsg << "THERE SEEMS TO BE SOMETHING WRONG WITH YOUR FIELD MAP '" << Filename_m << "'.\n";
577  if (!read_all) {
578  errormsg << "Didn't find enough values!" << std::endl;
579  } else if (state & std::ios_base::eofbit) {
580  errormsg << "Found more values than expected!" << std::endl;
581  } else if (state & std::ios_base::failbit) {
582  errormsg << "Found wrong type of values!" << "\n"
583  << "expecting: '" << expecting << "' on line " << lines_read_m << ",\n"
584  << "instead found: '" << found << "'." << std::endl;
585  }
586  throw GeneralClassicException("_Fieldmap::interpretWarning()",
587  errormsg.str());
588 }
589 
591  std::stringstream errormsg;
592  errormsg << "THERE SEEMS TO BE SOMETHING WRONG WITH YOUR FIELD MAP '" << Filename_m << "'.\n"
593  << "There are only " << lines_read_m - 1 << " lines in the file, expecting more.\n"
594  << "Please check the section about field maps in the user manual.";
595 
596  throw GeneralClassicException("_Fieldmap::missingValuesWarning()",
597  errormsg.str());
598 }
599 
601  std::stringstream errormsg;
602  errormsg << "THERE SEEMS TO BE SOMETHING WRONG WITH YOUR FIELD MAP '" << Filename_m << "'.\n"
603  << "There are too many lines in the file, expecting only " << lines_read_m << " lines.\n"
604  << "Please check the section about field maps in the user manual.";
605 
606  throw GeneralClassicException("_Fieldmap::exceedingValuesWarning()",
607  errormsg.str());
608 }
609 
611  std::stringstream errormsg;
612  errormsg << "DISABLING FIELD MAP '" + Filename_m + "' DUE TO PARSING ERRORS." ;
613 
614  throw GeneralClassicException("_Fieldmap::disableFieldmapsWarning()",
615  errormsg.str());
616 }
617 
619  std::stringstream errormsg;
620  errormsg << "DISABLING FIELD MAP '" << Filename_m << "' SINCE FILE COULDN'T BE FOUND!";
621 
622  throw GeneralClassicException("_Fieldmap::noFieldmapsWarning()",
623  errormsg.str());
624 }
625 
626 void _Fieldmap::lowResolutionWarning(double squareError, double maxError) {
627  std::stringstream errormsg;
628  errormsg << "IT SEEMS THAT YOU USE TOO FEW FOURIER COMPONENTS TO SUFFICIENTLY WELL\n"
629  << "RESOLVE THE FIELD MAP '" << Filename_m << "'.\n"
630  << "PLEASE INCREASE THE NUMBER OF FOURIER COMPONENTS!\n"
631  << "The ratio (e_i - E_i)^2 / E_i^2 is " << std::to_string(squareError) << " and\n"
632  << "the ratio (max_i(|e_i - E_i|) / max_i(|E_i|) is " << std::to_string(maxError) << ".\n"
633  << "Here E_i is the field as in the field map and e_i is the reconstructed field.\n"
634  << "The lower limit for the two ratios is 1e-2\n"
635  << "Have a look into the directory "
637  << " for a reconstruction of the field map.\n";
638  std::string errormsg_str = typeset_msg(errormsg.str(), "warning");
639 
640  ERRORMSG(errormsg_str << "\n" << endl);
641 
642  if (Ippl::myNode() == 0) {
643  std::ofstream omsg("errormsg.txt", std::ios_base::app);
644  omsg << errormsg.str() << std::endl;
645  omsg.close();
646  }
647 }
648 
649 std::string _Fieldmap::typeset_msg(const std::string &msg, const std::string &title) {
650  static std::string frame("* ******************************************************************************\n");
651  static unsigned int frame_width = frame.length() - 5;
652  static std::string closure(" *\n");
653 
654  std::string return_string("\n" + frame);
655 
656  int remaining_length = msg.length();
657  unsigned int current_position = 0;
658 
659  unsigned int ii = 0;
660  for (; ii < title.length(); ++ ii) {
661  char c = title[ii];
662  c = std::toupper(c);
663  return_string.replace(15 + 2 * ii, 1, " ");
664  return_string.replace(16 + 2 * ii, 1, &c, 1);
665  }
666  return_string.replace(15 + 2 * ii, 1, " ");
667 
668  while(remaining_length > 0) {
669  size_t eol = msg.find("\n", current_position);
670  std::string next_to_process;
671  if (eol != std::string::npos) {
672  next_to_process = msg.substr(current_position, eol - current_position);
673  } else {
674  next_to_process = msg.substr(current_position);
675  eol = msg.length();
676  }
677 
678  if (eol - current_position < frame_width) {
679  return_string += "* " + next_to_process + closure.substr(eol - current_position + 2);
680  } else {
681  unsigned int last_space = next_to_process.rfind(" ", frame_width);
682  if (last_space > 0) {
683  if (last_space < frame_width) {
684  return_string += "* " + next_to_process.substr(0, last_space) + closure.substr(last_space + 2);
685  } else {
686  return_string += "* " + next_to_process.substr(0, last_space) + " *\n";
687  }
688  if (next_to_process.length() - last_space + 1 < frame_width) {
689  return_string += "* " + next_to_process.substr(last_space + 1) + closure.substr(next_to_process.length() - last_space + 1);
690  } else {
691  return_string += "* " + next_to_process.substr(last_space + 1) + " *\n";
692  }
693  } else {
694  return_string += "* " + next_to_process + " *\n";
695  }
696  }
697 
698  current_position = eol + 1;
699  remaining_length = msg.length() - current_position;
700  }
701  return_string += frame;
702 
703  return return_string;
704 }
705 
706 void _Fieldmap::getOnaxisEz(std::vector<std::pair<double, double> > &/*onaxis*/)
707 { }
708 
709 void _Fieldmap::get1DProfile1EngeCoeffs(std::vector<double> &/*engeCoeffsEntry*/,
710  std::vector<double> &/*engeCoeffsExit*/) {
711 
712 }
713 
714 void _Fieldmap::get1DProfile1EntranceParam(double &/*entranceParameter1*/,
715  double &/*entranceParameter2*/,
716  double &/*entranceParameter3*/) {
717 
718 }
719 
720 void _Fieldmap::get1DProfile1ExitParam(double &/*exitParameter1*/,
721  double &/*exitParameter2*/,
722  double &/*exitParameter3*/) {
723 
724 }
725 
727  return 0.0;
728 }
729 
730 void _Fieldmap::setFieldGap(double /*gap*/) {
731 
732 }
733 
734 void _Fieldmap::write3DField(unsigned int nx,
735  unsigned int ny,
736  unsigned int nz,
737  const std::pair<double, double> &xrange,
738  const std::pair<double, double> &yrange,
739  const std::pair<double, double> &zrange,
740  const std::vector<Vector_t> &ef,
741  const std::vector<Vector_t> &bf) {
742  const size_t numpoints = nx * ny * nz;
743  if (Ippl::myNode() != 0 ||
744  (ef.size() != numpoints && bf.size() != numpoints)) return;
745 
746  std::filesystem::path p(Filename_m);
747  std::string fname = Util::combineFilePath({
749  p.stem().string() + ".vtk"
750  });
751  std::ofstream of;
752  of.open (fname);
753  PAssert (of.is_open ());
754  of.precision (6);
755 
756  const double hx = (xrange.second - xrange.first) / (nx - 1);
757  const double hy = (yrange.second - yrange.first) / (ny - 1);
758  const double hz = (zrange.second - zrange.first) / (nz - 1);
759 
760  of << "# vtk DataFile Version 2.0" << std::endl;
761  of << "generated by 3D fieldmaps" << std::endl;
762  of << "ASCII" << std::endl << std::endl;
763  of << "DATASET RECTILINEAR_GRID" << std::endl;
764  of << "DIMENSIONS " << nx << " " << ny << " " << nz << std::endl;
765 
766  of << "X_COORDINATES " << nx << " float" << std::endl;
767  of << xrange.first;
768  for (unsigned int i = 1; i < nx - 1; ++ i) {
769  of << " " << xrange.first + i * hx;
770  }
771  of << " " << xrange.second << std::endl;
772 
773  of << "Y_COORDINATES " << ny << " float" << std::endl;
774  of << yrange.first;
775  for (unsigned int i = 1; i < ny - 1; ++ i) {
776  of << " " << yrange.first + i * hy;
777  }
778  of << " " << yrange.second << std::endl;
779 
780  of << "Z_COORDINATES " << nz << " float" << std::endl;
781  of << zrange.first;
782  for (unsigned int i = 1; i < nz - 1; ++ i) {
783  of << " " << zrange.first + i * hz;
784  }
785  of << " " << zrange.second << std::endl;
786 
787  of << "POINT_DATA " << numpoints << std::endl;
788 
789  if (ef.size() == numpoints) {
790  of << "VECTORS EField float" << std::endl;
791  // of << "LOOKUP_TABLE default" << std::endl;
792  for (size_t i = 0; i < numpoints; ++ i) {
793  of << ef[i](0) << " " << ef[i](1) << " " << ef[i](2) << std::endl;
794  }
795  // of << std::endl;
796  }
797 
798  if (bf.size() == numpoints) {
799  of << "VECTORS BField float" << std::endl;
800  // of << "LOOKUP_TABLE default" << std::endl;
801  for (size_t i = 0; i < numpoints; ++ i) {
802  of << bf[i](0) << " " << bf[i](1) << " " << bf[i](2) << std::endl;
803  }
804  // of << std::endl;
805  }
806 }
807 
809 REGISTER_PARSE_TYPE(unsigned int);
811 REGISTER_PARSE_TYPE(std::string);
812 
813 std::string _Fieldmap::alpha_numeric("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-+\211");
814 std::map<std::string, _Fieldmap::FieldmapDescription> _Fieldmap::FieldmapDictionary = std::map<std::string, _Fieldmap::FieldmapDescription>();
static Astra1DElectroStatic create(const std::string &filename)
static OpalData * getInstance()
Definition: OpalData.cpp:196
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:45
static void clearDictionary()
Definition: Fieldmap.cpp:274
static Astra1DDynamic_fast create(const std::string &filename)
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
Definition: LICENSE:43
virtual void setFieldLength(const double &)
Definition: Fieldmap.cpp:530
virtual void freeMap()=0
constexpr double two_pi
The value of .
Definition: Physics.h:33
static FM2DElectroStatic create(const std::string &filename)
void noFieldmapWarning()
Definition: Fieldmap.cpp:618
static Astra1DElectroStatic_fast create(const std::string &filename)
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
static void deleteFieldmap(std::string Filename)
Definition: Fieldmap.cpp:270
virtual void setEdgeConstants(const double &bendAngle, const double &entranceAngle, const double &exitAngle)
Definition: Fieldmap.cpp:527
static MPI_Comm getComm()
Definition: IpplInfo.h:152
static int myNode()
Definition: IpplInfo.cpp:691
static FM1DDynamic create(const std::string &filename)
Definition: FM1DDynamic.cpp:45
static FM1DDynamic_fast create(const std::string &filename)
#define ERRORMSG(msg)
Definition: IpplInfo.h:350
virtual void get1DProfile1ExitParam(double &exitParameter1, double &exitParameter2, double &exitParameter3)
Definition: Fieldmap.cpp:720
static FM2DDynamic create(const std::string &filename)
virtual void get1DProfile1EntranceParam(double &entranceParameter1, double &entranceParameter2, double &entranceParameter3)
Definition: Fieldmap.cpp:714
int lines_read_m
Definition: Fieldmap.h:119
void interpretWarning(const std::ios_base::iostate &state, const bool &read_all, const std::string &error_msg, const std::string &found)
Definition: Fieldmap.cpp:571
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:649
virtual double getFieldGap()
Definition: Fieldmap.cpp:726
static std::map< std::string, FieldmapDescription > FieldmapDictionary
Definition: Fieldmap.h:198
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE:162
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
static MapType readHeader(std::string Filename)
Definition: Fieldmap.cpp:282
#define REGISTER_PARSE_TYPE(X)
Definition: Fieldmap.cpp:45
bool interpreteEOF(std::ifstream &in)
Definition: Fieldmap.cpp:555
static Astra1DMagnetoStatic create(const std::string &filename)
static char buffer_m[256]
Definition: Fieldmap.h:182
#define READ_BUFFER_LENGTH
Definition: Fieldmap.h:4
static FM3DH5Block create(const std::string &filename)
Definition: FM3DH5Block.cpp:39
std::shared_ptr< _Fieldmap > Fieldmap
Definition: Definitions.h:24
std::string::iterator iterator
Definition: MSLang.h:15
static FM1DProfile2 create(const std::string &filename)
static FM3DMagnetoStaticH5Block create(const std::string &filename)
virtual void readMap()=0
virtual void get1DProfile1EngeCoeffs(std::vector< double > &engeCoeffsEntry, std::vector< double > &engeCoeffsExit)
Definition: Fieldmap.cpp:709
virtual void setFieldGap(double gap)
Definition: Fieldmap.cpp:730
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:734
static Astra1DDynamic create(const std::string &filename)
FRONT * fs
Definition: hypervolume.cpp:59
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
Definition: OpalData.cpp:666
static FM1DElectroStatic_fast create(const std::string &filename)
void exceedingValuesWarning()
Definition: Fieldmap.cpp:600
static FM1DElectroStatic create(const std::string &filename)
static Astra1DMagnetoStatic_fast create(const std::string &filename)
static std::string alpha_numeric
Definition: Fieldmap.h:183
static FM2DMagnetoStatic create(const std::string &filename)
void disableFieldmapWarning()
Definition: Fieldmap.cpp:610
static FM3DDynamic create(const std::string &filename)
Tps< T > cos(const Tps< T > &x)
Cosine.
Definition: TpsMath.h:129
const std::string name
static FM3DMagnetoStatic create(const std::string &filename)
std::string combineFilePath(std::initializer_list< std::string > ilist)
Definition: Util.cpp:197
void getLine(std::ifstream &in, std::string &buffer)
Definition: Fieldmap.h:122
MapType
Definition: Fieldmap.h:15
static FM1DProfile1 create(const std::string &filename)
constexpr double e
The value of .
Definition: Physics.h:39
static FM1DMagnetoStatic_fast create(const std::string &filename)
#define PAssert(c)
Definition: PAssert.h:102
std::string Filename_m
Definition: Fieldmap.h:118
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &onaxis)
Definition: Fieldmap.cpp:706
static std::vector< std::string > getListFieldmapNames()
Definition: Fieldmap.cpp:262
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
void missingValuesWarning()
Definition: Fieldmap.cpp:590
Tps< T > sin(const Tps< T > &x)
Sine.
Definition: TpsMath.h:111
static FM3DMagnetoStaticExtended create(const std::string &filename)
void lowResolutionWarning(double squareError, double maxError)
Definition: Fieldmap.cpp:626
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:448
static Fieldmap getFieldmap(std::string Filename, bool fast=false)
Definition: Fieldmap.cpp:48
SDDS1 &description type
Definition: test.stat:4
end
Definition: multipole_t.tex:9
static FM3DH5Block_nonscale create(const std::string &filename)
static FM1DMagnetoStatic create(const std::string &filename)