OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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
25#include "Utilities/Util.h"
26
27#include <filesystem>
28#include <fstream>
29
30extern Inform* gmsg;
31
32std::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
73DumpFields::DumpFields(const std::string& name, DumpFields* parent):
74 Action(name, parent)
75{}
76
78 delete grid_m;
79 dumpsSet_m.erase(this);
80}
81
82DumpFields* 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({
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
191void 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}
Inform * gmsg
Definition: Main.cpp:70
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
@ SIZE
Definition: IndexMap.cpp:174
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
const std::string name
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:252
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:240
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:343
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:332
std::string::iterator iterator
Definition: MSLang.h:15
std::string combineFilePath(std::initializer_list< std::string > ilist)
Definition: Util.cpp:197
void checkInt(double real, std::string name, double tolerance)
Definition: Util.cpp:205
The base class for all OPAL actions.
Definition: Action.h:30
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:191
std::vector< Attribute > itsAttr
The object attributes.
Definition: Object.h:216
static OpalData * getInstance()
Definition: OpalData.cpp:196
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
Definition: OpalData.cpp:666
DumpFields dumps the static magnetic field of a Ring in a user-defined grid.
Definition: DumpFields.h:54
virtual void writeFieldThis(Component *field)
Definition: DumpFields.cpp:135
std::string filename_m
Definition: DumpFields.h:122
void print(std::ostream &os) const
Print the attributes of DumpFields to standard out.
Definition: DumpFields.cpp:191
virtual void buildGrid()
Definition: DumpFields.cpp:103
static void writeFields(Component *field)
Write the fields for all defined DumpFields objects.
Definition: DumpFields.cpp:128
static std::unordered_set< DumpFields * > dumpsSet_m
Definition: DumpFields.h:124
interpolation::ThreeDGrid * grid_m
Definition: DumpFields.h:120
virtual void execute()
Builds the grid but does not write the field map.
Definition: DumpFields.cpp:94
virtual DumpFields * clone(const std::string &name)
Make a clone (overloadable copy-constructor).
Definition: DumpFields.cpp:82
DumpFields()
Constructor.
Definition: DumpFields.cpp:34
virtual ~DumpFields()
Destructor deletes grid_m and if in the dumps set, take it out.
Definition: DumpFields.cpp:77
Interface for a single beam element.
Definition: Component.h:50
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B)
Definition: Component.cpp:99
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.
ThreeDGrid holds grid information for a rectangular grid used in e.g.
Definition: ThreeDGrid.h:57
Mesh::Iterator begin() const
Return an iterator pointing at the first element in the ThreeDGrid.
Definition: ThreeDGrid.cpp:91
Mesh::Iterator end() const
Return an iterator pointing at the last+1 element in the ThreeDGrid.
Definition: ThreeDGrid.cpp:95
ThreeDGrid * clone()
Deep copy the grid.
Definition: ThreeDGrid.h:62
The base class for all OPAL exceptions.
Definition: OpalException.h:28
Definition: Inform.h:42