29#include <boost/filesystem.hpp>
41 "The \"DUMPEMFIELDS\" statement dumps a field map to a user-defined "
42 "field file, for checking that fields are generated correctly. "
43 "The fields are written out on a grid in space and time."),
49 (
"FILE_NAME",
"Name of the file to which field data is dumped");
52 (
"COORDINATE_SYSTEM",
"Choose to use CARTESIAN or CYLINDRICAL coordinates", {
"CARTESIAN",
"CYLINDRICAL"},
"CARTESIAN");
55 (
"X_START",
"(Cartesian) Start point in the grid in x [m]");
58 (
"DX",
"(Cartesian) Grid step size in x [m]");
61 (
"X_STEPS",
"(Cartesian) Number of steps in x");
64 (
"Y_START",
"(Cartesian) Start point in the grid in y [m]");
67 (
"DY",
"(Cartesian) Grid step size in y [m]");
70 (
"Y_STEPS",
"(Cartesian) Number of steps in y");
73 (
"Z_START",
"Start point in the grid in z [m]");
76 (
"DZ",
"Grid step size in z [m]");
79 (
"Z_STEPS",
"Number of steps in z");
82 (
"T_START",
"Start point in the grid in time [ns]");
85 (
"DT",
"Grid step size in time [ns]");
88 (
"T_STEPS",
"Number of steps in time");
91 (
"R_START",
"(Cylindrical) Start point in the grid in radius [m]");
94 (
"DR",
"(Cylindrical) Grid step size in radius [m]");
97 (
"R_STEPS",
"(Cylindrical) Number of steps in radius");
100 (
"PHI_START",
"(Cylindrical) Start point in the grid in phi [rad]");
103 (
"DPHI",
"(Cylindrical) Grid step size in phi [rad]");
106 (
"PHI_STEPS",
"(Cylindrical) Number of steps in phi");
112 Action(name, parent), grid_m(nullptr)
134 static const std::map<std::string, CoordinateSystem> stringCoordinateSystem_s = {
151 std::vector<double> spacing(4);
152 std::vector<double> origin(4);
153 std::vector<int> gridSize(4);
213 (*it)->writeFieldThis(field);
219 if (std::abs(std::floor(real) - real) > 2*tolerance) {
221 "Value for " + name +
222 " should be an integer but a real value was found");
224 if (std::floor(real) < 0.5) {
226 "Value for " + name +
" should be 1 or more");
234 fout << 1 <<
" x [m]\n";
235 fout << 2 <<
" y [m]\n";
236 fout << 3 <<
" z [m]\n";
237 fout << 4 <<
" t [ns]\n";
238 fout << 5 <<
" Bx [kGauss]\n";
239 fout << 6 <<
" By [kGauss]\n";
240 fout << 7 <<
" Bz [kGauss]\n";
241 fout << 8 <<
" Ex [MV/m]\n";
242 fout << 9 <<
" Ey [MV/m]\n";
243 fout << 10 <<
" Ez [MV/m]\n";
247 fout << 1 <<
" r [m]\n";
248 fout << 2 <<
" phi [deg]\n";
249 fout << 3 <<
" z [m]\n";
250 fout << 4 <<
" t [ns]\n";
251 fout << 5 <<
" Br [kGauss]\n";
252 fout << 6 <<
" Bphi [kGauss]\n";
253 fout << 7 <<
" Bz [kGauss]\n";
254 fout << 8 <<
" Er [MV/m]\n";
255 fout << 9 <<
" Ephi [MV/m]\n";
256 fout << 10 <<
" Ez [MV/m]\n";
266 std::ofstream& fout)
const {
273 point[0] = std::cos(pointIn[1])*pointIn[0];
274 point[1] = std::sin(pointIn[1])*pointIn[0];
277 field->
apply(point, centroid, time, E, B);
282 Bout[0] = B[0]*std::cos(pointIn[1])+B[1]*std::sin(pointIn[1]);
283 Bout[1] = -B[0]*std::sin(pointIn[1])+B[1]*std::cos(pointIn[1]);
284 Eout[0] = E[0]*std::cos(pointIn[1])+E[1]*std::sin(pointIn[1]);
285 Eout[1] = -E[0]*std::sin(pointIn[1])+E[1]*std::cos(pointIn[1]);
286 fout << pointIn[0] <<
" " << pointIn[1]*
Units::rad2deg <<
" " << pointIn[2] <<
" " << time <<
" ";
288 fout << pointIn[0] <<
" " << pointIn[1] <<
" " << pointIn[2] <<
" " << time <<
" ";
291 fout << Bout[0] <<
" " << Bout[1] <<
" " << Bout[2] <<
" ";
292 fout << Eout[0] <<
" " << Eout[1] <<
" " << Eout[2] <<
"\n";
298 "The grid was nullptr; there was a problem with the DumpEMFields initialisation.");
300 if (field ==
nullptr) {
302 "The field to be written was nullptr.");
308 if (boost::filesystem::path(
filename_m).is_absolute() ==
true) {
317 std::vector<double> point_std(4);
321 fout.open(fname.c_str(), std::ofstream::out);
322 }
catch (std::exception& exc) {
324 "Failed to open DumpEMFields file " +
filename_m);
328 "Failed to open DumpEMFields file " +
filename_m);
335 it.getPosition(&point_std[0]);
336 for (
size_t i = 0; i < 3; ++i) {
337 point[i] = point_std[i];
339 double time = point_std[3];
344 "Something went wrong during writing " +
filename_m);
350 os <<
"* ************* D U M P E M F I E L D S ****************************************** " <<
std::endl;
351 os <<
"* File name: '" <<
filename_m <<
"'\n";
375 os <<
"* ********************************************************************************** " <<
std::endl;
PartBunch< T, Dim >::ConstIterator end(PartBunch< T, Dim > const &bunch)
Inform & endl(Inform &inf)
double getReal(const Attribute &attr)
Return real value.
Attribute makePredefinedString(const std::string &name, const std::string &help, const std::initializer_list< std::string > &predefinedStrings)
Make predefined string attribute.
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
std::string getString(const Attribute &attr)
Get string value.
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
std::string::iterator iterator
std::string combineFilePath(std::initializer_list< std::string > ilist)
Interface for a single beam element.
virtual bool apply(const size_t &i, const double &t, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B)
The base class for all OPAL actions.
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
std::vector< Attribute > itsAttr
The object attributes.
static OpalData * getInstance()
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
DumpEMFields dumps the dynamically changing fields of a Ring in a user- defined grid.
virtual void writeFieldThis(Component *field)
void print(std::ostream &os) const
Print the attributes of DumpEMFields to standard out.
static std::unordered_set< DumpEMFields * > dumpsSet_m
void parseCoordinateSystem()
static void writeFields(Component *field)
Write the fields for all defined DumpEMFields objects.
interpolation::NDGrid * grid_m
CoordinateSystem coordinates_m
void writeHeader(std::ofstream &fout) const
virtual void execute()
Builds the grid but does not write the field map.
static void checkInt(double value, std::string name, double tolerance=1e-9)
virtual DumpEMFields * clone(const std::string &name)
Make a clone (overloadable copy-constructor).
DumpEMFields()
Constructor.
virtual ~DumpEMFields()
Destructor deletes grid_m and if in the dumps set, take it out.
void writeFieldLine(Component *field, const Vector_t< double, 3 > &point, const double &time, std::ofstream &fout) const
Used to loop over some, or all, points in the mesh, as in stl Enables e.g.
int toInteger() const
Return an integer representation of the iterator.
NDGrid holds grid information for a rectilinear grid in some arbitrary dimensional space.
Mesh::Iterator begin() const
Returns the origin of the mesh (lowest possible index in all dimensions)
Mesh::Iterator end() const
Returns the end of the mesh (highest possible index in all dimensions)
NDGrid * clone()
Inheritable copy constructor.
The base class for all OPAL exceptions.