OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
DumpFields.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Chris Rogers
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * 1. Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above copyright notice,
9  * this list of conditions and the following disclaimer in the documentation
10  * and/or other materials provided with the distribution.
11  * 3. Neither the name of STFC nor the names of its contributors may be used to
12  * endorse or promote products derived from this software without specific
13  * prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 
29 #include <fstream>
30 
31 #include "Fields/Interpolation/ThreeDGrid.h" // classic
32 #include "AbsBeamline/Component.h" // classic
34 #include "Attributes/Attributes.h"
36 
37 std::unordered_set<DumpFields*> DumpFields::dumpsSet_m;
38 
40 std::string("The \"DUMPFIELDS\" statement dumps a field map to a user-defined")+
41 std::string(" field file, for checking that fields are read in correctly")+
42 std::string(" from disk. The fields are written out on a Cartesian grid.");
43 
45  Action(10, "DUMPFIELDS", dumpfields_docstring.c_str()) {
46  // would be nice if "steps" could be integer
48  ("X_START", "Start point in the grid in x [m]");
50  ("DX", "Grid step size in x [m]");
52  ("X_STEPS", "Number of steps in x");
54  ("Y_START", "Start point in the grid in y [m]");
56  ("DY", "Grid step size in y [m]");
58  ("Y_STEPS", "Number of steps in y");
60  ("Z_START", "Start point in the grid in z [m]");
62  ("DZ", "Grid step size in z [m]");
64  ("Z_STEPS", "Number of steps in z");
66  ("FILE_NAME", "Name of the file to which field data is dumped");
67 
69 }
70 
71 DumpFields::DumpFields(const std::string &name, DumpFields *parent):
72  Action(name, parent)
73 {}
74 
76  delete grid_m;
77  dumpsSet_m.erase(this);
78 }
79 
80 DumpFields* DumpFields::clone(const std::string &name) {
81  DumpFields* dumper = new DumpFields(name, this);
82  if (grid_m != NULL) {
83  dumper->grid_m = grid_m->clone();
84  }
85  dumper->filename_m = filename_m;
86  if (dumpsSet_m.find(this) != dumpsSet_m.end()) {
87  dumpsSet_m.insert(dumper);
88  }
89  return dumper;
90 }
91 
93  buildGrid();
94  // the routine for action (OpalParser/OpalParser) calls execute and then
95  // deletes 'this'; so we must build a copy that lasts until the field maps
96  // are constructed and we are ready for tracking (which is when the field
97  // maps are written). Hence the clone call below.
98  dumpsSet_m.insert(this->clone(""));
99 }
100 
102  double x0 = Attributes::getReal(itsAttr[0]);
103  double dx = Attributes::getReal(itsAttr[1]);
104  double nx = Attributes::getReal(itsAttr[2]);
105 
106  double y0 = Attributes::getReal(itsAttr[3]);
107  double dy = Attributes::getReal(itsAttr[4]);
108  double ny = Attributes::getReal(itsAttr[5]);
109 
110  double z0 = Attributes::getReal(itsAttr[6]);
111  double dz = Attributes::getReal(itsAttr[7]);
112  double nz = Attributes::getReal(itsAttr[8]);
113 
114  checkInt(nx, "X_STEPS");
115  checkInt(ny, "Y_STEPS");
116  checkInt(nz, "Z_STEPS");
117  delete grid_m;
118 
119  grid_m = new interpolation::ThreeDGrid(dx, dy, dz,
120  x0, y0, z0,
121  nx, ny, nz);
122 
124 }
125 
128  for (dump_iter it = dumpsSet_m.begin(); it != dumpsSet_m.end(); ++it) {
129  (*it)->writeFieldThis(field);
130  }
131 }
132 
133 void DumpFields::checkInt(double real, std::string name, double tolerance) {
134  if (fabs(floor(real) - real) > tolerance) {
135  throw OpalException("DumpFields::checkInt",
136  "Value for "+name+
137  " should be an integer but a real value was found");
138  }
139  if (floor(real) < 0.5) {
140  throw OpalException("DumpFields::checkInt",
141  "Value for "+name+" should be 1 or more");
142  }
143 }
144 
146  if (grid_m == NULL) {
147  throw OpalException("DumpFields::writeFieldThis",
148  "The grid was NULL; there was a problem with the DumpFields initialisation.");
149  }
150  if (field == NULL) {
151  throw OpalException("DumpFields::writeFieldThis",
152  "The field to be written was NULL.");
153  }
154  double time = 0.;
155  Vector_t point(0., 0., 0.);
156  Vector_t centroid(0., 0., 0.);
157  std::ofstream fout(filename_m.c_str(), std::ofstream::out);
158  if (!fout.good()) {
159  throw OpalException("DumpFields::writeFieldThis",
160  "Failed to open DumpFields file "+filename_m);
161  }
162  // set precision
163  fout << grid_m->end().toInteger() << "\n";
164  fout << 1 << " x [m]\n";
165  fout << 2 << " y [m]\n";
166  fout << 3 << " z [m]\n";
167  fout << 4 << " Bx [kGauss]\n";
168  fout << 5 << " By [kGauss]\n";
169  fout << 6 << " Bz [kGauss]\n";
170  fout << 0 << std::endl;
172  it < grid_m->end();
173  ++it) {
174  Vector_t E(0., 0., 0.);
175  Vector_t B(0., 0., 0.);
176  it.getPosition(&point[0]);
177  field->apply(point, centroid, time, E, B);
178  fout << point[0] << " " << point[1] << " " << point[2] << " ";
179  fout << B[0] << " " << B[1] << " " << B[2] << "\n";
180  }
181  if (!fout.good()) {
182  throw OpalException("DumpFields::writeFieldThis",
183  "Something went wrong during writing "+filename_m);
184  }
185  fout.close();
186 }
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B)
Definition: Component.cpp:99
Mesh::Iterator end() const
Definition: ThreeDGrid.cpp:95
The base class for all OPAL actions.
Definition: Action.h:30
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
Definition: PETE.h:815
The base class for all OPAL exceptions.
Definition: OpalException.h:28
virtual void writeFieldThis(Component *field)
Definition: DumpFields.cpp:145
ThreeDGrid * clone()
Definition: ThreeDGrid.h:63
static std::string dumpfields_docstring
Definition: DumpFields.h:114
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
virtual void execute()
Definition: DumpFields.cpp:92
FLieGenerator< T, N > real(const FLieGenerator< std::complex< T >, N > &)
Take real part of a complex generator.
static void checkInt(double value, std::string name, double tolerance=1e-9)
Definition: DumpFields.cpp:133
static void writeFields(Component *field)
Definition: DumpFields.cpp:126
std::string filename_m
Definition: DumpFields.h:111
virtual DumpFields * clone(const std::string &name)
Definition: DumpFields.cpp:80
interpolation::ThreeDGrid * grid_m
Definition: DumpFields.h:110
Mesh::Iterator begin() const
Definition: ThreeDGrid.cpp:91
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
virtual void buildGrid()
Definition: DumpFields.cpp:101
const std::string name
virtual ~DumpFields()
Definition: DumpFields.cpp:75
std::string::iterator iterator
Definition: MSLang.h:16
Interface for a single beam element.
Definition: Component.h:51
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
static std::unordered_set< DumpFields * > dumpsSet_m
Definition: DumpFields.h:113
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:205
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:816
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307