OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
DumpFields.cpp
Go to the documentation of this file.
1 //
2 // Class DumpFields
3 // DumpFields dumps the static magnetic field of a Ring in a user-defined grid
4 //
5 // Copyright (c) 2016, Chris Rogers
6 // All rights reserved
7 //
8 // This file is part of OPAL.
9 //
10 // OPAL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17 //
19 
21 #include "AbsBeamline/Component.h"
22 #include "Attributes/Attributes.h"
25 #include "Utilities/Util.h"
26 
27 #include <filesystem>
28 #include <fstream>
29 
30 extern Inform* gmsg;
31 
32 std::unordered_set<DumpFields*> DumpFields::dumpsSet_m;
33 
35  Action(SIZE, "DUMPFIELDS",
36  "The \"DUMPFIELDS\" statement dumps a field map to a user-defined "
37  "field file, for checking that fields are read in correctly "
38  "from disk. The fields are written out on a Cartesian grid.") {
39  // would be nice if "steps" could be integer
41  ("FILE_NAME", "Name of the file to which field data is dumped");
42 
44  ("X_START", "Start point in the grid in x [m]");
45 
47  ("DX", "Grid step size in x [m]");
48 
50  ("X_STEPS", "Number of steps in x");
51 
53  ("Y_START", "Start point in the grid in y [m]");
54 
56  ("DY", "Grid step size in y [m]");
57 
59  ("Y_STEPS", "Number of steps in y");
60 
62  ("Z_START", "Start point in the grid in z [m]");
63 
65  ("DZ", "Grid step size in z [m]");
66 
68  ("Z_STEPS", "Number of steps in z");
69 
71 }
72 
73 DumpFields::DumpFields(const std::string& name, DumpFields* parent):
74  Action(name, parent)
75 {}
76 
78  delete grid_m;
79  dumpsSet_m.erase(this);
80 }
81 
82 DumpFields* DumpFields::clone(const std::string& name) {
83  DumpFields* dumper = new DumpFields(name, this);
84  if (grid_m != nullptr) {
85  dumper->grid_m = grid_m->clone();
86  }
87  dumper->filename_m = filename_m;
88  if (dumpsSet_m.find(this) != dumpsSet_m.end()) {
89  dumpsSet_m.insert(dumper);
90  }
91  return dumper;
92 }
93 
95  buildGrid();
96  // the routine for action (OpalParser/OpalParser) calls execute and then
97  // deletes 'this'; so we must build a copy that lasts until the field maps
98  // are constructed and we are ready for tracking (which is when the field
99  // maps are written). Hence the clone call below.
100  dumpsSet_m.insert(this->clone(""));
101 }
102 
104  double x0 = Attributes::getReal(itsAttr[X_START]);
105  double dx = Attributes::getReal(itsAttr[DX]);
106  double nx = Attributes::getReal(itsAttr[X_STEPS]);
107 
108  double y0 = Attributes::getReal(itsAttr[Y_START]);
109  double dy = Attributes::getReal(itsAttr[DY]);
110  double ny = Attributes::getReal(itsAttr[Y_STEPS]);
111 
112  double z0 = Attributes::getReal(itsAttr[Z_START]);
113  double dz = Attributes::getReal(itsAttr[DZ]);
114  double nz = Attributes::getReal(itsAttr[Z_STEPS]);
115 
116  Util::checkInt(nx, "X_STEPS");
117  Util::checkInt(ny, "Y_STEPS");
118  Util::checkInt(nz, "Z_STEPS");
119  delete grid_m;
120 
121  grid_m = new interpolation::ThreeDGrid(dx, dy, dz,
122  x0, y0, z0,
123  nx, ny, nz);
124 
126 }
127 
130  for (dump_iter it = dumpsSet_m.begin(); it != dumpsSet_m.end(); ++it) {
131  (*it)->writeFieldThis(field);
132  }
133 }
134 
136  if (grid_m == nullptr) {
137  throw OpalException("DumpFields::writeFieldThis",
138  "The grid was nullptr; there was a problem with the DumpFields initialisation.");
139  }
140  if (field == nullptr) {
141  throw OpalException("DumpFields::writeFieldThis",
142  "The field to be written was nullptr.");
143  }
144 
145  *gmsg << *this << endl;
146 
147  std::string fname;
148  if (std::filesystem::path(filename_m).is_absolute() == true) {
149  fname = filename_m;
150  } else {
151  fname = Util::combineFilePath({
153  filename_m
154  });
155  }
156 
157  double time = 0.;
158  Vector_t point(0., 0., 0.);
159  Vector_t centroid(0., 0., 0.);
160  std::ofstream fout(fname.c_str(), std::ofstream::out);
161  if (!fout.good()) {
162  throw OpalException("DumpFields::writeFieldThis",
163  "Failed to open DumpFields file " + filename_m);
164  }
165  // set precision
166  fout << grid_m->end().toInteger() << "\n";
167  fout << 1 << " x [m]\n";
168  fout << 2 << " y [m]\n";
169  fout << 3 << " z [m]\n";
170  fout << 4 << " Bx [kGauss]\n";
171  fout << 5 << " By [kGauss]\n";
172  fout << 6 << " Bz [kGauss]\n";
173  fout << 0 << std::endl;
175  it < grid_m->end();
176  ++it) {
177  Vector_t E(0., 0., 0.);
178  Vector_t B(0., 0., 0.);
179  it.getPosition(&point[0]);
180  field->apply(point, centroid, time, E, B);
181  fout << point[0] << " " << point[1] << " " << point[2] << " ";
182  fout << B[0] << " " << B[1] << " " << B[2] << "\n";
183  }
184  if (!fout.good()) {
185  throw OpalException("DumpFields::writeFieldThis",
186  "Something went wrong during writing " + filename_m);
187  }
188  fout.close();
189 }
190 
191 void DumpFields::print(std::ostream& os) const {
192  os << "* ************* D U M P F I E L D S *********************************************** " << std::endl;
193  os << "* File name: '" << filename_m << "'\n"
194  << "* X_START = " << Attributes::getReal(itsAttr[X_START]) << " [m]\n"
195  << "* DX = " << Attributes::getReal(itsAttr[DX]) << " [m]\n"
196  << "* X_STEPS = " << Attributes::getReal(itsAttr[X_STEPS]) << '\n'
197  << "* Y_START = " << Attributes::getReal(itsAttr[Y_START]) << " [m]\n"
198  << "* DY = " << Attributes::getReal(itsAttr[DY]) << " [m]\n"
199  << "* Y_STEPS = " << Attributes::getReal(itsAttr[Y_STEPS]) << '\n'
200  << "* Z_START = " << Attributes::getReal(itsAttr[Z_START]) << " [m]\n"
201  << "* DZ = " << Attributes::getReal(itsAttr[DZ]) << " [m]\n"
202  << "* Z_STEPS = " << Attributes::getReal(itsAttr[Z_STEPS]) << '\n';
203  os << "* ********************************************************************************** " << std::endl;
204 }
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:240
static OpalData * getInstance()
Definition: OpalData.cpp:196
virtual void writeFieldThis(Component *field)
Definition: DumpFields.cpp:135
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
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:343
static void writeFields(Component *field)
Definition: DumpFields.cpp:128
Mesh::Iterator begin() const
Definition: ThreeDGrid.cpp:91
virtual ~DumpFields()
Definition: DumpFields.cpp:77
virtual DumpFields * clone(const std::string &name)
Definition: DumpFields.cpp:82
static std::unordered_set< DumpFields * > dumpsSet_m
Definition: DumpFields.h:124
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string::iterator iterator
Definition: MSLang.h:15
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B)
Definition: Component.cpp:99
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:191
The base class for all OPAL exceptions.
Definition: OpalException.h:28
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
Definition: OpalData.cpp:666
Definition: Inform.h:42
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:332
ThreeDGrid * clone()
Definition: ThreeDGrid.h:62
virtual void buildGrid()
Definition: DumpFields.cpp:103
void print(std::ostream &os) const
Definition: DumpFields.cpp:191
std::vector< Attribute > itsAttr
The object attributes.
Definition: Object.h:216
const std::string name
std::string combineFilePath(std::initializer_list< std::string > ilist)
Definition: Util.cpp:197
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:252
Mesh::Iterator end() const
Definition: ThreeDGrid.cpp:95
std::string filename_m
Definition: DumpFields.h:122
Interface for a single beam element.
Definition: Component.h:50
virtual void execute()
Definition: DumpFields.cpp:94
void checkInt(double real, std::string name, double tolerance)
Definition: Util.cpp:205
The base class for all OPAL actions.
Definition: Action.h:30
interpolation::ThreeDGrid * grid_m
Definition: DumpFields.h:120
Inform * gmsg
Definition: Main.cpp:70