OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
FieldWriter.hpp
Go to the documentation of this file.
1//
2// Class FieldWriter
3// This class writes the bunch internal fields on the grid to
4// file. It supports single core execution only.
5//
6// Copyright (c) 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
7// All rights reserved
8//
9// This file is part of OPAL.
10//
11// OPAL is free software: you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation, either version 3 of the License, or
14// (at your option) any later version.
15//
16// You should have received a copy of the GNU General Public License
17// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
18//
19#include <iomanip>
20#include <fstream>
21
22#include <boost/filesystem.hpp>
23#include <boost/format.hpp>
24
25#include "Utilities/Util.h"
28
29template<typename FieldType>
30void FieldWriter::dumpField(FieldType& field, std::string name,
31 std::string unit, long long step,
32 FieldType* image)
33{
34 if (Ippl::getNodes() > 1) {
35 return;
36 }
37
38 constexpr bool isVectorField = std::is_same<VField_t, FieldType>::value;
39 std::string type = (isVectorField) ? "field" : "scalar";
40
41 INFOMSG("*** START DUMPING " + Util::toUpper(name) + " FIELD ***" << endl);
42
43 /* Save the files in the output directory of the simulation. The file
44 * name of vector fields is
45 *
46 * 'basename'-'name'_field-'******'.dat
47 *
48 * and of scalar fields
49 *
50 * 'basename'-'name'_scalar-'******'.dat
51 *
52 * with
53 * 'basename': OPAL input file name (*.in)
54 * 'name': field name (input argument of function)
55 * '******': step padded with zeros to 6 digits
56 */
57 std::string dirname = OpalData::getInstance()->getAuxiliaryOutputDirectory();
58 boost::filesystem::path file(dirname);
59 boost::format filename("%1%-%2%-%|3$06|.dat");
60 std::string basename = OpalData::getInstance()->getInputBasename();
61 filename % basename % (name + std::string("_") + type) % step;
62 file /= filename.str();
63
64 std::ofstream fout(file.string(), std::ios::out);
65 fout.precision(9);
66
67 fout << "# " << name << " " << type << " data on grid" << std::endl
68 << "#"
69 << std::setw(4) << "i"
70 << std::setw(5) << "j"
71 << std::setw(5) << "k"
72 << std::setw(17) << "x [m]"
73 << std::setw(17) << "y [m]"
74 << std::setw(17) << "z [m]";
75 if (isVectorField) {
76 fout << std::setw(10) << name << "x [" << unit << "]"
77 << std::setw(10) << name << "y [" << unit << "]"
78 << std::setw(10) << name << "z [" << unit << "]";
79 } else {
80 fout << std::setw(13) << name << " [" << unit << "]";
81 }
82
83 if (image) {
84 fout << std::setw(13) << name << " image [" << unit << "]";
85 }
86
87 fout << std::endl;
88
89 Vector_t origin = field.get_mesh().get_origin();
90 Vector_t spacing(field.get_mesh().get_meshSpacing(0),
91 field.get_mesh().get_meshSpacing(1),
92 field.get_mesh().get_meshSpacing(2));
93
94 NDIndex<3> localIdx = field.getLayout().getLocalNDIndex();
95 for (int x = localIdx[0].first(); x <= localIdx[0].last(); x++) {
96 for (int y = localIdx[1].first(); y <= localIdx[1].last(); y++) {
97 for (int z = localIdx[2].first(); z <= localIdx[2].last(); z++) {
98 NDIndex<3> idx(Index(x, x), Index(y, y), Index(z, z));
99 fout << std::setw(5) << x + 1
100 << std::setw(5) << y + 1
101 << std::setw(5) << z + 1
102 << std::setw(17) << origin(0) + x * spacing(0)
103 << std::setw(17) << origin(1) + y * spacing(1)
104 << std::setw(17) << origin(2) + z * spacing(2);
105 if (isVectorField) {
106 Vector_t vfield = field.localElement(idx);
107 fout << std::setw(17) << vfield[0]
108 << std::setw(17) << vfield[1]
109 << std::setw(17) << vfield[2];
110 } else {
111 fout << std::setw(17) << field.localElement(idx);
112 }
113
114 if (image) {
115 fout << std::setw(17) << image->localElement(idx);
116 }
117 fout << std::endl;
118 }
119 }
120 }
121 fout.close();
122 INFOMSG("*** FINISHED DUMPING " + Util::toUpper(name) + " FIELD ***" << endl);
123}
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
#define INFOMSG(msg)
Definition: IpplInfo.h:348
const std::string name
std::string toUpper(const std::string &str)
Definition: Util.cpp:146
boost::function< boost::tuple< double, bool >(arguments_t)> type
Definition: function.hpp:21
std::string getInputBasename()
get input file name without extension
Definition: OpalData.cpp:669
static OpalData * getInstance()
Definition: OpalData.cpp:196
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
Definition: OpalData.cpp:661
void dumpField(FieldType &field, std::string name, std::string unit, long long step, FieldType *image=nullptr)
Dump a scalar or vector field to a file.
Definition: FieldWriter.hpp:30
Definition: Index.h:237
static int getNodes()
Definition: IpplInfo.cpp:670