OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
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 <boost/filesystem.hpp>
28
29#include <fstream>
30
31extern Inform* gmsg;
32
33std::unordered_set<DumpFields*> DumpFields::dumpsSet_m;
34
36 Action(SIZE, "DUMPFIELDS",
37 "The \"DUMPFIELDS\" statement dumps a field map to a user-defined "
38 "field file, for checking that fields are read in correctly "
39 "from disk. The fields are written out on a Cartesian grid.") {
40 // would be nice if "steps" could be integer
42 ("FILE_NAME", "Name of the file to which field data is dumped");
43
45 ("X_START", "Start point in the grid in x [m]");
46
48 ("DX", "Grid step size in x [m]");
49
51 ("X_STEPS", "Number of steps in x");
52
54 ("Y_START", "Start point in the grid in y [m]");
55
57 ("DY", "Grid step size in y [m]");
58
60 ("Y_STEPS", "Number of steps in y");
61
63 ("Z_START", "Start point in the grid in z [m]");
64
66 ("DZ", "Grid step size in z [m]");
67
69 ("Z_STEPS", "Number of steps in z");
70
72}
73
74DumpFields::DumpFields(const std::string& name, DumpFields* parent):
75 Action(name, parent)
76{}
77
79 delete grid_m;
80 dumpsSet_m.erase(this);
81}
82
83DumpFields* DumpFields::clone(const std::string& name) {
84 DumpFields* dumper = new DumpFields(name, this);
85 if (grid_m != nullptr) {
86 dumper->grid_m = grid_m->clone();
87 }
88 dumper->filename_m = filename_m;
89 if (dumpsSet_m.find(this) != dumpsSet_m.end()) {
90 dumpsSet_m.insert(dumper);
91 }
92 return dumper;
93}
94
96 buildGrid();
97 // the routine for action (OpalParser/OpalParser) calls execute and then
98 // deletes 'this'; so we must build a copy that lasts until the field maps
99 // are constructed and we are ready for tracking (which is when the field
100 // maps are written). Hence the clone call below.
101 dumpsSet_m.insert(this->clone(""));
102}
103
105 double x0 = Attributes::getReal(itsAttr[X_START]);
106 double dx = Attributes::getReal(itsAttr[DX]);
107 double nx = Attributes::getReal(itsAttr[X_STEPS]);
108
109 double y0 = Attributes::getReal(itsAttr[Y_START]);
110 double dy = Attributes::getReal(itsAttr[DY]);
111 double ny = Attributes::getReal(itsAttr[Y_STEPS]);
112
113 double z0 = Attributes::getReal(itsAttr[Z_START]);
114 double dz = Attributes::getReal(itsAttr[DZ]);
115 double nz = Attributes::getReal(itsAttr[Z_STEPS]);
116
117 checkInt(nx, "X_STEPS");
118 checkInt(ny, "Y_STEPS");
119 checkInt(nz, "Z_STEPS");
120 delete grid_m;
121
122 grid_m = new interpolation::ThreeDGrid(dx, dy, dz,
123 x0, y0, z0,
124 nx, ny, nz);
125
127}
128
130 typedef std::unordered_set<DumpFields*>::iterator dump_iter;
131 for (dump_iter it = dumpsSet_m.begin(); it != dumpsSet_m.end(); ++it) {
132 (*it)->writeFieldThis(field);
133 }
134}
135
136void DumpFields::checkInt(double real, std::string name, double tolerance) {
137 if (std::abs(std::floor(real) - real) > tolerance) {
138 throw OpalException("DumpFields::checkInt",
139 "Value for " + name +
140 " should be an integer but a real value was found");
141 }
142 if (std::floor(real) < 0.5) {
143 throw OpalException("DumpFields::checkInt",
144 "Value for " + name + " should be 1 or more");
145 }
146}
147
149 if (grid_m == nullptr) {
150 throw OpalException("DumpFields::writeFieldThis",
151 "The grid was nullptr; there was a problem with the DumpFields initialisation.");
152 }
153 if (field == nullptr) {
154 throw OpalException("DumpFields::writeFieldThis",
155 "The field to be written was nullptr.");
156 }
157
158 *gmsg << *this << endl;
159
160 std::string fname;
161 if (boost::filesystem::path(filename_m).is_absolute() == true) {
162 fname = filename_m;
163 } else {
164 fname = Util::combineFilePath({
167 });
168 }
169
170 double time = 0.;
171 Vector_t<double, 3> point(0., 0., 0.);
172 Vector_t<double, 3> centroid(0., 0., 0.);
173 std::ofstream fout(fname.c_str(), std::ofstream::out);
174 if (!fout.good()) {
175 throw OpalException("DumpFields::writeFieldThis",
176 "Failed to open DumpFields file " + filename_m);
177 }
178 // set precision
179 fout << grid_m->end().toInteger() << "\n";
180 fout << 1 << " x [m]\n";
181 fout << 2 << " y [m]\n";
182 fout << 3 << " z [m]\n";
183 fout << 4 << " Bx [kGauss]\n";
184 fout << 5 << " By [kGauss]\n";
185 fout << 6 << " Bz [kGauss]\n";
186 fout << 0 << std::endl;
187 for (interpolation::Mesh::Iterator it = grid_m->begin();
188 it < grid_m->end();
189 ++it) {
190 Vector_t<double, 3> E(0., 0., 0.);
191 Vector_t<double, 3> B(0., 0., 0.);
192 it.getPosition(&point[0]);
193 field->apply(point, centroid, time, E, B);
194 fout << point[0] << " " << point[1] << " " << point[2] << " ";
195 fout << B[0] << " " << B[1] << " " << B[2] << "\n";
196 }
197 if (!fout.good()) {
198 throw OpalException("DumpFields::writeFieldThis",
199 "Something went wrong during writing " + filename_m);
200 }
201 fout.close();
202}
203
204void DumpFields::print(std::ostream& os) const {
205 os << "* ************* D U M P F I E L D S *********************************************** " << std::endl;
206 os << "* File name: '" << filename_m << "'\n"
207 << "* X_START = " << Attributes::getReal(itsAttr[X_START]) << " [m]\n"
208 << "* DX = " << Attributes::getReal(itsAttr[DX]) << " [m]\n"
209 << "* X_STEPS = " << Attributes::getReal(itsAttr[X_STEPS]) << '\n'
210 << "* Y_START = " << Attributes::getReal(itsAttr[Y_START]) << " [m]\n"
211 << "* DY = " << Attributes::getReal(itsAttr[DY]) << " [m]\n"
212 << "* Y_STEPS = " << Attributes::getReal(itsAttr[Y_STEPS]) << '\n'
213 << "* Z_START = " << Attributes::getReal(itsAttr[Z_START]) << " [m]\n"
214 << "* DZ = " << Attributes::getReal(itsAttr[DZ]) << " [m]\n"
215 << "* Z_STEPS = " << Attributes::getReal(itsAttr[Z_STEPS]) << '\n';
216 os << "* ********************************************************************************** " << std::endl;
217}
Inform * gmsg
Definition changes.cpp:7
ippl::Vector< T, Dim > Vector_t
PartBunch< T, Dim >::ConstIterator end(PartBunch< T, Dim > const &bunch)
double getReal(const Attribute &attr)
Return real value.
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 combineFilePath(std::initializer_list< std::string > ilist)
Definition Util.cpp:197
Interface for a single beam element.
Definition Component.h:50
virtual bool apply(const size_t &i, const double &t, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B)
Definition Component.cpp:80
Action(int size, const char *name, const char *help)
Constructor for exemplars.
Definition Action.cpp:54
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition Object.cpp:189
std::vector< Attribute > itsAttr
The object attributes.
Definition Object.h:216
static OpalData * getInstance()
Definition OpalData.cpp:195
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
Definition OpalData.cpp:677
static void checkInt(double value, std::string name, double tolerance=1e-9)
virtual void writeFieldThis(Component *field)
std::string filename_m
Definition DumpFields.h:123
void print(std::ostream &os) const
virtual void buildGrid()
static void writeFields(Component *field)
static std::unordered_set< DumpFields * > dumpsSet_m
Definition DumpFields.h:125
interpolation::ThreeDGrid * grid_m
Definition DumpFields.h:121
virtual void execute()
virtual DumpFields * clone(const std::string &name)
virtual ~DumpFields()
The base class for all OPAL exceptions.