OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
FlexibleCollimator.cpp
Go to the documentation of this file.
1 //
2 // Class FlexibleCollimator
3 // Defines the abstract interface for a collimator.
4 //
5 // Copyright (c) 200x - 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 //
19 
23 #include "Fields/Fieldmap.h"
24 #include "Physics/Physics.h"
26 #include "Structure/LossDataSink.h"
27 #include "Utilities/Options.h"
28 #include "Utilities/Util.h"
29 
30 #include <memory>
31 
32 extern Inform *gmsg;
33 
36 {}
37 
38 
40  Component(right),
41  description_m(right.description_m),
42  bb_m(right.bb_m),
43  tree_m(/*right.tree_m*/),
44  informed_m(right.informed_m),
45  losses_m(0),
46  lossDs_m(nullptr),
47  parmatint_m(NULL)
48 {
49  for (const std::shared_ptr<mslang::Base>& obj: right.holes_m) {
50  holes_m.emplace_back(obj->clone());
51  }
52 
53  tree_m.bb_m = bb_m;
54  tree_m.objects_m.insert(tree_m.objects_m.end(), holes_m.begin(), holes_m.end());
55  tree_m.buildUp();
56 }
57 
58 
60  Component(name),
61  description_m(""),
62  informed_m(false),
63  losses_m(0),
64  lossDs_m(nullptr),
65  parmatint_m(NULL)
66 {}
67 
68 
70  if (online_m)
71  goOffline();
72 
73  // for (mslang::Base *obj: holes_m) {
74  // delete obj;
75  // }
76 }
77 
78 
80  visitor.visitFlexibleCollimator(*this);
81 }
82 
83 
85  const double z = R(2);
86 
87  if ((z < 0.0) ||
88  (z > getElementLength()) ||
89  (!isInsideTransverse(R))) return false;
90 
91  if (!bb_m.isInside(R)) {
92  return true;
93  }
94 
95  if (!tree_m.isInside(R)) {
96  return true;
97  }
98 
99  return false;
100 }
101 
102 bool FlexibleCollimator::apply(const size_t &i, const double &t, Vector_t &/*E*/, Vector_t &/*B*/) {
103  const Vector_t &R = RefPartBunch_m->R[i];
104  const Vector_t &P = RefPartBunch_m->P[i];
105  const double &dt = RefPartBunch_m->dt[i];
106  const double recpgamma = Physics::c * dt / std::sqrt(1.0 + dot(P, P));
107  bool pdead = isStopped(R);
108 
109  if (pdead) {
110  if (lossDs_m) {
111  double frac = -R(2) / P(2) * recpgamma;
112  lossDs_m->addParticle(OpalParticle(RefPartBunch_m->ID[i],
113  R, P,
114  t + frac * dt,
115  RefPartBunch_m->Q[i], RefPartBunch_m->M[i]));
116  }
117  ++ losses_m;
118  }
119 
120  return pdead;
121 }
122 
124  const Vector_t &/*R*/,
125  const Vector_t &/*P*/,
126  const double &/*t*/,
127  Vector_t &/*E*/,
128  Vector_t &/*B*/) {
129  return false;
130 }
131 
132 // rectangle collimators in cyclotron cyclindral coordinates
133 // without particlematterinteraction, the particle hitting collimator is deleted directly
135  PartBunchBase<double, 3> */*bunch*/,
136  const int /*turnnumber*/,
137  const double /*t*/,
138  const double /*tstep*/) {
139  return false;
140 }
141 
142 void FlexibleCollimator::initialise(PartBunchBase<double, 3> *bunch, double &startField, double &endField) {
143  RefPartBunch_m = bunch;
144  endField = startField + getElementLength();
145 
147 
148  lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getOutputFN(), !Options::asciidump));
149 
150  goOnline(-1e6);
151 }
152 
154  RefPartBunch_m = bunch;
155 
157 
158  lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getOutputFN(), !Options::asciidump));
159 
160  goOnline(-1e6);
161 }
162 
164  if (online_m)
165  goOffline();
166  *gmsg << "* Finalize flexible collimator " << getName() << endl;
167 }
168 
169 void FlexibleCollimator::goOnline(const double &) {
170  print();
171 
172  online_m = true;
173 }
174 
176  if (RefPartBunch_m == NULL) {
177  if (!informed_m) {
178  std::string errormsg = Fieldmap::typeset_msg("BUNCH SIZE NOT SET", "warning");
179  ERRORMSG(errormsg << endl);
180  if (Ippl::myNode() == 0) {
181  std::ofstream omsg("errormsg.txt", std::ios_base::app);
182  omsg << errormsg << std::endl;
183  omsg.close();
184  }
185  informed_m = true;
186  }
187  return;
188  }
189 
190  *gmsg << level3;
191 }
192 
194  if (online_m && lossDs_m)
195  lossDs_m->save();
196  lossDs_m.reset(0);
197  online_m = false;
198 }
199 
200 void FlexibleCollimator::getDimensions(double &zBegin, double &zEnd) const {
201  zBegin = 0.0;
202  zEnd = getElementLength();
203 }
204 
206  return FLEXIBLECOLLIMATOR;
207 }
208 
209 void FlexibleCollimator::setDescription(const std::string &desc) {
210  tree_m.reset();
211  holes_m.clear();
212 
213  mslang::Function *fun;
214 
215  if (!mslang::parse(desc, fun))
216  throw GeneralClassicException("FlexibleCollimator::setDescription",
217  "Couldn't parse input file");
218 
219  fun->apply(holes_m);
220 
221  if (holes_m.size() == 0) return;
222 
223  for (std::shared_ptr<mslang::Base> & it: holes_m) {
224  it->computeBoundingBox();
225  }
226 
227  std::shared_ptr<mslang::Base> &first = holes_m.front();
228  const mslang::BoundingBox &bb = first->bb_m;
229 
230  Vector_t llc(bb.center_m[0] - 0.5 * bb.width_m,
231  bb.center_m[1] - 0.5 * bb.height_m,
232  0.0);
233  Vector_t urc(bb.center_m[0] + 0.5 * bb.width_m,
234  bb.center_m[1] + 0.5 * bb.height_m,
235  0.0);
236 
237  for (const std::shared_ptr<mslang::Base> & it: holes_m) {
238  const mslang::BoundingBox &bb = it->bb_m;
239  llc[0] = std::min(llc[0], bb.center_m[0] - 0.5 * bb.width_m);
240  llc[1] = std::min(llc[1], bb.center_m[1] - 0.5 * bb.height_m);
241  urc[0] = std::max(urc[0], bb.center_m[0] + 0.5 * bb.width_m);
242  urc[1] = std::max(urc[1], bb.center_m[1] + 0.5 * bb.height_m);
243  }
244 
245  double width = urc[0] - llc[0];
246  double height = urc[1] - llc[1];
247 
248  llc[0] -= 1e-3 * width;
249  urc[0] += 1e-3 * width;
250  llc[1] -= 1e-3 * height;
251  urc[1] += 1e-3 * height;
252 
253  bb_m = mslang::BoundingBox(llc, urc);
254 
255  tree_m.bb_m = bb_m;
256  tree_m.objects_m.insert(tree_m.objects_m.end(), holes_m.begin(), holes_m.end());
257  tree_m.buildUp();
258 
259  delete fun;
260 }
261 
262 void FlexibleCollimator::writeHolesAndQuadtree(const std::string &baseFilename) const {
263  if (Ippl::myNode() == 0) {
264  std::string fname = Util::combineFilePath({
266  baseFilename
267  });
268 
269  std::ofstream out(fname + "_quadtree.gpl");
270  tree_m.writeGnuplot(out);
271  out.close();
272 
273  out.open(fname + "_holes.gpl");
274  for (const std::shared_ptr<mslang::Base> &obj: holes_m) {
275  obj->writeGnuplot(out);
276  }
277  out.close();
278  }
279 }
Inform * gmsg
Definition: Main.cpp:62
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition: Vector3D.cpp:118
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:84
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:76
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
Inform & level3(Inform &inf)
Definition: Inform.cpp:47
#define ERRORMSG(msg)
Definition: IpplInfo.h:350
const std::string name
constexpr double e
The value of.
Definition: Physics.h:39
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:51
bool parse(std::string str, Function *&fun)
Definition: MSLang.cpp:37
bool asciidump
Definition: Options.cpp:85
std::string combineFilePath(std::initializer_list< std::string > ilist)
Definition: Util.cpp:139
ParticlePos_t & R
ParticleAttrib< double > M
ParticleAttrib< Vector_t > P
ParticleAttrib< double > Q
ParticleAttrib< double > dt
ParticleIndex_t & ID
static OpalData * getInstance()
Definition: OpalData.cpp:195
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
Definition: OpalData.cpp:650
virtual void visitFlexibleCollimator(const FlexibleCollimator &)=0
Apply the algorithm to a flexible collimator.
Interface for a single beam element.
Definition: Component.h:50
bool online_m
Definition: Component.h:195
PartBunchBase< double, 3 > * RefPartBunch_m
Definition: Component.h:194
virtual const std::string & getName() const
Get element name.
virtual ParticleMatterInteractionHandler * getParticleMatterInteraction() const
Definition: ElementBase.h:500
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:432
bool isInsideTransverse(const Vector_t &r) const
std::string getOutputFN() const
Get output filename.
@ FLEXIBLECOLLIMATOR
Definition: ElementBase.h:114
std::unique_ptr< LossDataSink > lossDs_m
bool isStopped(const Vector_t &R)
std::vector< std::shared_ptr< mslang::Base > > holes_m
virtual ElementBase::ElementType getType() const override
Get element type std::string.
ParticleMatterInteractionHandler * parmatint_m
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) override
virtual bool checkCollimator(PartBunchBase< double, 3 > *bunch, const int turnnumber, const double t, const double tstep)
virtual void finalise() override
virtual bool applyToReferenceParticle(const Vector_t &R, const Vector_t &P, const double &t, Vector_t &E, Vector_t &B) override
void setDescription(const std::string &desc)
virtual void goOffline() override
virtual void goOnline(const double &kineticEnergy) override
virtual void accept(BeamlineVisitor &) const override
Apply visitor to FlexibleCollimator.
void writeHolesAndQuadtree(const std::string &baseFilename) const
mslang::BoundingBox bb_m
mslang::QuadTree tree_m
virtual void getDimensions(double &zBegin, double &zEnd) const override
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:652
bool isInside(const Vector_t &X) const
Definition: BoundingBox.cpp:11
void writeGnuplot(std::ostream &out) const
Definition: QuadTree.cpp:91
BoundingBox bb_m
Definition: QuadTree.h:12
bool isInside(const Vector_t &R) const
Definition: QuadTree.cpp:104
std::list< std::shared_ptr< Base > > objects_m
Definition: QuadTree.h:11
virtual void apply(std::vector< std::shared_ptr< Base >> &bfuncs)=0
Definition: Inform.h:42
static int myNode()
Definition: IpplInfo.cpp:691