38 #include <boost/filesystem.hpp>
45 namespace fs = boost::filesystem;
47 #define REGISTER_PARSE_TYPE(X) template <> struct Fieldmap::TypeParseTraits<X> \
48 { static const char* name; } ; const char* Fieldmap::TypeParseTraits<X>::name = #X
53 (*position).second.RefCounter++;
54 return (*position).second.Map;
74 return (*position.first).second.Map;
91 return (*position.first).second.Map;
108 return (*position.first).second.Map;
125 return (*position.first).second.Map;
142 return (*position.first).second.Map;
159 return (*position.first).second.Map;
167 return (*position.first).second.Map;
176 return (*position.first).second.Map;
185 return (*position.first).second.Map;
194 return (*position.first).second.Map;
203 return (*position.first).second.Map;
212 return (*position.first).second.Map;
220 return (*position.first).second.Map;
228 return (*position.first).second.Map;
236 return (*position.first).second.Map;
253 return (*position.first).second.Map;
258 "Couldn't determine type of fieldmap in file \"" + Filename +
"\"");
264 std::vector<std::string> name_list;
266 name_list.push_back((*it).first);
278 delete it->second.Map;
279 it->second.Map = NULL;
285 char magicnumber[5] =
" ";
290 if (Filename ==
"1DPROFILE1-DEFAULT")
293 if (Filename.empty())
295 "No field map file specified");
297 if (!fs::exists(Filename))
299 "File \"" + Filename +
"\" doesn't exist");
301 std::ifstream File(Filename.c_str());
303 std::cerr <<
"could not open file " << Filename <<
std::endl;
308 std::istringstream interpreter(buffer, std::istringstream::in);
310 interpreter.read(magicnumber, 4);
312 if (std::strcmp(magicnumber,
"3DDy") == 0)
315 if (std::strcmp(magicnumber,
"3DMa") == 0) {
316 char tmpString[21] =
" ";
317 interpreter.read(tmpString, 20);
319 if (std::strcmp(tmpString,
"gnetoStatic_Extended") == 0)
325 if (std::strcmp(magicnumber,
"3DEl") == 0)
328 if (std::strcmp(magicnumber,
"2DDy") == 0) {
334 if (std::strcmp(magicnumber,
"2DMa") == 0) {
340 if (std::strcmp(magicnumber,
"2DEl") == 0) {
346 if (std::strcmp(magicnumber,
"1DDy") == 0)
349 if (std::strcmp(magicnumber,
"1DMa") == 0)
352 if (std::strcmp(magicnumber,
"1DPr") == 0) {
361 if (std::strcmp(magicnumber,
"1DEl") == 0)
364 if (std::strcmp(magicnumber,
"\211HDF") == 0) {
371 h5_size_t len_name =
sizeof(
name);
372 h5_prop_t props = H5CreateFileProp ();
374 h5err = H5SetPropFileMPIOCollective (props, &comm);
376 h5_file_t file = H5OpenFile (Filename.c_str(), H5_O_RDONLY, props);
377 PAssert (file != (h5_file_t)H5_ERR);
380 h5err = H5SetStep(file, 0);
383 h5_int64_t num_fields = H5BlockGetNumFields(file);
384 PAssert (num_fields != H5_ERR);
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);
392 if (std::strcmp(
name,
"Bfield") == 0) {
395 }
else if (std::strcmp(
name,
"Hfield") == 0) {
400 h5err = H5CloseFile(file);
406 if (std::strcmp(magicnumber,
"Astr") == 0) {
407 char tmpString[3] =
" ";
408 interpreter.read(tmpString, 2);
409 if (std::strcmp(tmpString,
"aE") == 0) {
412 if (std::strcmp(tmpString,
"aM") == 0) {
415 if (std::strcmp(tmpString,
"aD") == 0) {
427 if (!(*position).second.read) {
428 (*position).second.Map->readMap();
429 (*position).second.read =
true;
439 if ((*position).second.RefCounter > 0) {
440 (*position).second.RefCounter--;
443 if ((*position).second.RefCounter == 0) {
444 delete (*position).second.Map;
445 (*position).second.Map = NULL;
452 std::pair<double, double> fieldDimensions,
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;
464 checkMap(accuracy, length, zSampling, fourierCoefficients, splineCoefficients, splineAccelerator);
469 const std::vector<double> &zSampling,
470 const std::vector<double> &fourierCoefficients,
471 gsl_spline *splineCoefficients,
472 gsl_interp_accel *splineAccelerator) {
474 double maxDiff = 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;
485 opal->getAuxiliaryOutputDirectory(),
486 Filename_m.substr(lastSlash, lastDot) +
".check"
489 out <<
"# z original reproduced\n";
491 auto it = zSampling.begin();
492 auto end = zSampling.end();
493 for (; it !=
end; ++ it) {
495 double onAxisFieldCheck = fourierCoefficients[0];
497 for (
unsigned int l = 1; l < accuracy; ++l,
n += 2) {
501 onAxisFieldCheck += (fourierCoefficients[
n] * coskzl - fourierCoefficients[
n + 1] * sinkzl);
503 double ez = gsl_spline_eval(splineCoefficients, *it, splineAccelerator);
504 double difference =
std::abs(ez - onAxisFieldCheck);
505 maxDiff = difference > maxDiff? difference: maxDiff;
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
519 if (
std::sqrt(error / ezSquare) > 1
e-1 || maxDiff > 1
e-1 * ezMax) {
523 "Field map can't be reproduced properly with the given number of fourier components");
525 if (
std::sqrt(error / ezSquare) > 1
e-2 || maxDiff > 1
e-2 * ezMax) {
546 size_t comment = buffer.find(
"#");
547 buffer = buffer.substr(0, comment);
551 }
while(!in.eof() && lastof == std::string::npos);
553 if (firstof != std::string::npos) {
554 buffer = buffer.substr(firstof, lastof - firstof + 1);
563 size_t comment = buffer.find_first_of(
"#");
564 buffer = buffer.substr(0, comment);
566 if (lasto != std::string::npos) {
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";
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;
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.";
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.";
614 std::stringstream errormsg;
615 errormsg <<
"DISABLING FIELD MAP '" +
Filename_m +
"' DUE TO PARSING ERRORS." ;
622 std::stringstream errormsg;
623 errormsg <<
"DISABLING FIELD MAP '" <<
Filename_m <<
"' SINCE FILE COULDN'T BE FOUND!";
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");
646 std::ofstream omsg(
"errormsg.txt", std::ios_base::app);
653 static std::string frame(
"* ******************************************************************************\n");
654 static unsigned int frame_width = frame.length() - 5;
655 static std::string closure(
" *\n");
657 std::string return_string(
"\n" + frame);
659 int remaining_length = msg.length();
660 unsigned int current_position = 0;
663 for (; ii < title.length(); ++ ii) {
666 return_string.replace(15 + 2 * ii, 1,
" ");
667 return_string.replace(16 + 2 * ii, 1, &
c, 1);
669 return_string.replace(15 + 2 * ii, 1,
" ");
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);
677 next_to_process = msg.substr(current_position);
681 if (eol - current_position < frame_width) {
682 return_string +=
"* " + next_to_process + closure.substr(eol - current_position + 2);
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);
689 return_string +=
"* " + next_to_process.substr(0, last_space) +
" *\n";
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);
694 return_string +=
"* " + next_to_process.substr(last_space + 1) +
" *\n";
697 return_string +=
"* " + next_to_process +
" *\n";
701 current_position = eol + 1;
702 remaining_length = msg.length() - current_position;
704 return_string += frame;
706 return return_string;
713 std::vector<double> &) {
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;
747 (ef.size() != numpoints && bf.size() != numpoints))
return;
752 p.stem().string() +
".vtk"
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);
763 of <<
"# vtk DataFile Version 2.0" <<
std::endl;
764 of <<
"generated by 3D fieldmaps" <<
std::endl;
766 of <<
"DATASET RECTILINEAR_GRID" <<
std::endl;
767 of <<
"DIMENSIONS " << nx <<
" " << ny <<
" " << nz <<
std::endl;
769 of <<
"X_COORDINATES " << nx <<
" float" <<
std::endl;
771 for (
unsigned int i = 1; i < nx - 1; ++ i) {
772 of <<
" " << xrange.first + i * hx;
776 of <<
"Y_COORDINATES " << ny <<
" float" <<
std::endl;
778 for (
unsigned int i = 1; i < ny - 1; ++ i) {
779 of <<
" " << yrange.first + i * hy;
783 of <<
"Z_COORDINATES " << nz <<
" float" <<
std::endl;
785 for (
unsigned int i = 1; i < nz - 1; ++ i) {
786 of <<
" " << zrange.first + i * hz;
790 of <<
"POINT_DATA " << numpoints <<
std::endl;
792 if (ef.size() == numpoints) {
793 of <<
"VECTORS EField float" <<
std::endl;
795 for (
size_t i = 0; i < numpoints; ++ i) {
796 of << ef[i](0) <<
" " << ef[i](1) <<
" " << ef[i](2) <<
std::endl;
801 if (bf.size() == numpoints) {
802 of <<
"VECTORS BField float" <<
std::endl;
804 for (
size_t i = 0; i < numpoints; ++ i) {
805 of << bf[i](0) <<
" " << bf[i](1) <<
" " << bf[i](2) <<
std::endl;
817 std::map<std::string, Fieldmap::FieldmapDescription>
Fieldmap::FieldmapDictionary = std::map<std::string, Fieldmap::FieldmapDescription>();
Tps< T > cos(const Tps< T > &x)
Cosine.
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Tps< T > sin(const Tps< T > &x)
Sine.
Tps< T > sqrt(const Tps< T > &x)
Square root.
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
#define REGISTER_PARSE_TYPE(X)
#define READ_BUFFER_LENGTH
@ T3DMagnetoStatic_Extended
@ T3DMagnetoStaticH5Block
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Inform & endl(Inform &inf)
constexpr double two_pi
The value of.
constexpr double e
The value of.
constexpr double c
The velocity of light in m/s.
std::string::iterator iterator
std::string combineFilePath(std::initializer_list< std::string > ilist)
boost::function< boost::tuple< double, bool >arguments_t)> type
static OpalData * getInstance()
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
static Fieldmap * getFieldmap(std::string Filename, bool fast=false)
static void deleteFieldmap(std::string Filename)
static std::string alpha_numeric
static std::map< std::string, FieldmapDescription > FieldmapDictionary
static MapType readHeader(std::string Filename)
bool interpreteEOF(std::ifstream &in)
virtual void setFieldLength(const double &)
void missingValuesWarning()
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &onaxis)
virtual void get1DProfile1EntranceParam(double &entranceParameter1, double &entranceParameter2, double &entranceParameter3)
void checkMap(unsigned int accuracy, std::pair< double, double > fieldDimensions, double deltaZ, const std::vector< double > &fourierCoefficients, gsl_spline *splineCoefficients, gsl_interp_accel *splineAccelerator)
void lowResolutionWarning(double squareError, double maxError)
void interpretWarning(const std::ios_base::iostate &state, const bool &read_all, const std::string &error_msg, const std::string &found)
void disableFieldmapWarning()
static char buffer_m[256]
static std::vector< std::string > getListFieldmapNames()
static std::string typeset_msg(const std::string &msg, const std::string &title)
static void clearDictionary()
virtual double getFieldGap()
virtual void setEdgeConstants(const double &bendAngle, const double &entranceAngle, const double &exitAngle)
virtual void setFieldGap(double gap)
void exceedingValuesWarning()
virtual void get1DProfile1EngeCoeffs(std::vector< double > &engeCoeffsEntry, std::vector< double > &engeCoeffsExit)
void getLine(std::ifstream &in, std::string &buffer)
virtual void get1DProfile1ExitParam(double &exitParameter1, double &exitParameter2, double &exitParameter3)
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)
static MPI_Comm getComm()