OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
Monitor.cpp
Go to the documentation of this file.
1//
2// Class Monitor
3// Defines the abstract interface for a beam position monitor.
4//
5// Copyright (c) 2000 - 2021, Paul Scherrer Institut, Villigen PSI, Switzerland
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//
18#include "AbsBeamline/Monitor.h"
19
22#include "Fields/Fieldmap.h"
23#include "PartBunch/PartBunch.h"
24#include "Physics/Physics.h"
27#include "Utilities/Options.h"
28#include "Utilities/Util.h"
29
30#include <boost/filesystem.hpp>
31
32#include <fstream>
33#include <memory>
34
35std::map<double, SetStatistics> Monitor::statFileEntries_sm;
36const double Monitor::halfLength_s = 0.005;
37
40
42 : Component(right),
44 plane_m(right.plane_m),
45 type_m(right.type_m),
46 numPassages_m(0) {
47}
48
49Monitor::Monitor(const std::string& name)
50 : Component(name),
51 filename_m(""),
52 plane_m(OFF),
54 numPassages_m(0) {
55}
56
59
60void Monitor::accept(BeamlineVisitor& visitor) const {
61 visitor.visitMonitor(*this);
62}
63
65 const size_t& i, const double& t, Vector_t<double, 3>& /*E*/, Vector_t<double, 3>& /*B*/) {
66 // const Vector_t<double, 3>& R = RefPartBunch_m->R(i);
67 // const Vector_t<double, 3>& P = RefPartBunch_m->P(i);
68 // const double& dt = RefPartBunch_m->dt(i);
69 // const Vector_t<double, 3> singleStep = Physics::c * dt * Util::getBeta(P);
70 // if (online_m && type_m == CollectionType::SPATIAL) {
71 // if (dt * R(2) < 0.0 && dt * (R(2) + singleStep(2)) > 0.0) {
72 // // if R(2) is negative then frac should be positive and vice versa
73 // double frac = -R(2) / singleStep(2);
74
75 // lossDs_m->addParticle(OpalParticle(
76 // RefPartBunch_m->ID[i], R + frac * singleStep, P, t + frac * dt,
77 // RefPartBunch_m->Q[i], RefPartBunch_m->M[i]));
78 // }
79 // }
80
81 throw std::runtime_error("Fix this function please");
82 return false;
83}
84
86 const Vector_t<double, 3>& R, const Vector_t<double, 3>& P, const double& t,
88 throw std::runtime_error("Fix this function please");
89 return false;
90}
91
93 const Vector_t<double, 3>& refR, const Vector_t<double, 3>& refP) {
94 // const double cdt = Physics::c * RefPartBunch_m->getdT();
95 // const Vector_t<double, 3> driftPerTimeStep = cdt * Util::getBeta(refP);
96 // const double tau = -refR(2) / driftPerTimeStep(2);
97 // const CoordinateSystemTrafo update(
98 // refR + tau * driftPerTimeStep, getQuaternion(refP, Vector_t<double, 3>(0, 0, 1)));
99 // const CoordinateSystemTrafo refToLocalCSTrafo =
100 // update * (getCSTrafoGlobal2Local() * RefPartBunch_m->toLabTrafo_m);
101
102 // for (OpalParticle particle : *RefPartBunch_m) {
103 // Vector_t<double, 3> beta = refToLocalCSTrafo.rotateTo(Util::getBeta(particle.getP()));
104 // Vector_t<double, 3> dS =
105 // (tau - 0.5) * cdt
106 // * beta; // the particles are half a step ahead relative to the reference particle
107 // particle.setR(refToLocalCSTrafo.transformTo(particle.getR()) + dS);
108 // lossDs_m->addParticle(particle);
109 // }
110 throw std::runtime_error("Fix this function please");
111}
112
114 const Vector_t<double, 3>& R, const Vector_t<double, 3>& P, const double& t,
116 // if (!OpalData::getInstance()->isInPrepState()) {
117 // const double dt = RefPartBunch_m->getdT();
118 // const double cdt = Physics::c * dt;
119 // const Vector_t<double, 3> singleStep = cdt * Util::getBeta(P);
120
121 // if (dt * R(2) < 0.0 && dt * (R(2) + singleStep(2)) > 0.0) {
122 // double frac = -R(2) / singleStep(2);
123 // double time = t + frac * dt;
124 // Vector_t<double, 3> dR = frac * singleStep;
125 // double ds = euclidean_norm(dR + 0.5 * singleStep);
126 // lossDs_m->addReferenceParticle(
127 // csTrafoGlobal2Local_m.transformFrom(R + dR), csTrafoGlobal2Local_m.rotateFrom(P),
128 // time, RefPartBunch_m->get_sPos() + ds, RefPartBunch_m->getGlobalTrackStep());
129
130 // if (type_m == CollectionType::TEMPORAL) {
131 // driftToCorrectPositionAndSave(R, P);
132 // auto stats = lossDs_m->computeStatistics(1);
133 // if (!stats.empty()) {
134 // statFileEntries_sm.insert(
135 // std::make_pair(stats.begin()->spos_m, *stats.begin()));
136 // OpalData::OpenMode openMode;
137 // if (numPassages_m > 0) {
138 // openMode = OpalData::OpenMode::APPEND;
139 // } else {
140 // openMode = OpalData::getInstance()->getOpenMode();
141 // }
142 // lossDs_m->save(1, openMode);
143 // }
144 // }
145
146 // ++numPassages_m;
147 // }
148 // }
149 throw std::runtime_error("Fix this function please");
150 return false;
151}
152
153void Monitor::initialise(PartBunch_t* bunch, double& startField, double& endField) {
154 RefPartBunch_m = bunch;
155 endField = startField + halfLength_s;
156 startField -= halfLength_s;
157
158 const size_t totalNum = bunch->getTotalNum();
159 double currentPosition = endField;
160 if (totalNum > 0) {
161 currentPosition = bunch->get_sPos();
162 }
163
165
166 // if (OpalData::getInstance()->getOpenMode() == OpalData::OpenMode::WRITE
167 // || currentPosition < startField) {
168 // namespace fs = boost::filesystem;
169
170 // fs::path lossFileName = fs::path(filename_m + ".h5");
171 // if (fs::exists(lossFileName)) {
172 // Ippl::Comm->barrier();
173 // if (ippl::Comm->rank() == 0) {
174 // fs::remove(lossFileName);
175 // }
176 // Ippl::Comm->barrier();
177 // }
178 // }
179
180 // lossDs_m =
181 // std::unique_ptr<LossDataSink>(new LossDataSink(filename_m, !Options::asciidump, type_m));
182 throw std::runtime_error("Fix this function please");
183}
184
186}
187
188void Monitor::goOnline(const double&) {
189 online_m = true;
190}
191
193 auto stats = lossDs_m->computeStatistics(numPassages_m);
194 for (auto& stat : stats) {
195 statFileEntries_sm.insert(std::make_pair(stat.spos_m, stat));
196 }
197
199 lossDs_m->save(numPassages_m);
200 }
201}
202
203bool Monitor::bends() const {
204 return false;
205}
206
207void Monitor::getDimensions(double& zBegin, double& zEnd) const {
208 zBegin = -halfLength_s;
209 zEnd = halfLength_s;
210}
211
213 // return ElementType::MONITOR;
214 throw std::runtime_error("Fix this function please");
215}
216
218 if (statFileEntries_sm.size() == 0)
219 return;
220
221 std::string fileName =
222 OpalData::getInstance()->getInputBasename() + std::string("_Monitors.stat");
223 auto instance = OpalData::getInstance();
224 bool hasPriorTrack = instance->hasPriorTrack();
225 bool inRestartRun = instance->inRestartRun();
226
227 auto it = statFileEntries_sm.begin();
228 double spos = it->first;
229 Util::rewindLinesSDDS(fileName, spos, false);
230
231 MonitorStatisticsWriter writer(fileName, hasPriorTrack || inRestartRun);
232
233 for (const auto& entry : statFileEntries_sm) {
234 writer.addRow(entry.second);
235 }
236
237 statFileEntries_sm.clear();
238}
ElementType
Definition ElementBase.h:88
PartBunch< PLayout_t< double, 3 >, double, 3 > PartBunch_t
ippl::Vector< T, Dim > Vector_t
CollectionType
unsigned int rewindLinesSDDS(const std::string &fileName, double maxSPos, bool checkForTime)
rewind the SDDS file such that the spos of the last step is less or equal to maxSPos
Definition Util.cpp:219
virtual void visitMonitor(const Monitor &)=0
Apply the algorithm to a beam position monitor.
bool online_m
Definition Component.h:186
Component(const std::string &name)
Constructor with given name.
Definition Component.cpp:44
PartBunch_t * RefPartBunch_m
Definition Component.h:185
std::string getOutputFN() const
Get output filename.
CollectionType type_m
Definition Monitor.h:104
static void writeStatistics()
Definition Monitor.cpp:217
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Monitor.
Definition Monitor.cpp:60
std::string filename_m
Definition Monitor.h:102
virtual void goOnline(const double &kineticEnergy) override
Definition Monitor.cpp:188
@ OFF
Monitor is off (inactive).
Definition Monitor.h:36
virtual ElementType getType() const override
Get element type std::string.
Definition Monitor.cpp:212
virtual void goOffline() override
Definition Monitor.cpp:192
std::unique_ptr< LossDataSink > lossDs_m
Definition Monitor.h:107
virtual void getDimensions(double &zBegin, double &zEnd) const override
Definition Monitor.cpp:207
Monitor(const std::string &name)
Constructor with given name.
Definition Monitor.cpp:49
Monitor()
Definition Monitor.cpp:38
virtual ~Monitor()
Definition Monitor.cpp:57
Plane plane_m
Definition Monitor.h:103
static std::map< double, SetStatistics > statFileEntries_sm
Definition Monitor.h:109
virtual void finalise() override
Definition Monitor.cpp:185
unsigned int numPassages_m
Definition Monitor.h:105
virtual bool applyToReferenceParticle(const Vector_t< double, 3 > &R, const Vector_t< double, 3 > &P, const double &t, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B) override
Definition Monitor.cpp:113
virtual void initialise(PartBunch_t *bunch, double &startField, double &endField) override
Definition Monitor.cpp:153
virtual bool apply(const size_t &i, const double &t, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B) override
Definition Monitor.cpp:64
static const double halfLength_s
Definition Monitor.h:110
void driftToCorrectPositionAndSave(const Vector_t< double, 3 > &R, const Vector_t< double, 3 > &P)
Definition Monitor.cpp:92
virtual bool bends() const override
Definition Monitor.cpp:203
std::string getInputBasename()
get input file name without extension
Definition OpalData.cpp:685
static OpalData * getInstance()
Definition OpalData.cpp:195
double get_sPos() const
size_t getTotalNum() const
void addRow(const SetStatistics &set)