OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
CCollimator.cpp
Go to the documentation of this file.
1 //
2 // Class CCollimator
3 // Interface for cyclotron collimator
4 //
5 // Copyright (c) 2018-2020, 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 
22 #include "Fields/Fieldmap.h"
24 #include "Structure/LossDataSink.h"
25 
26 #include <cmath>
27 #include <fstream>
28 
29 extern Inform *gmsg;
30 
32 {}
33 
34 CCollimator::CCollimator(const std::string &name):
36  setDimensions(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
37  setGeom(0.0);
38 }
39 
41  PluginElement(right),
42  informed_m(right.informed_m) {
43  setDimensions(right.xstart_m, right.xend_m,
44  right.ystart_m, right.yend_m,
45  right.zstart_m, right.zend_m,
46  right.width_m);
48 }
49 
51 
52 void CCollimator::accept(BeamlineVisitor &visitor) const {
53  visitor.visitCCollimator(*this);
54 }
55 
57  Vector_t rmin, rmax;
58  bunch->get_bounds(rmin, rmax);
59 
60  if (rmax(2) >= zstart_m && rmin(2) <= zend_m) {
61  // interested in absolute minimum and maximum
62  double xmin = std::min(std::abs(rmin(0)), std::abs(rmax(0)));
63  double xmax = std::max(std::abs(rmin(0)), std::abs(rmax(0)));
64  double ymin = std::min(std::abs(rmin(1)), std::abs(rmax(1)));
65  double ymax = std::max(std::abs(rmin(1)), std::abs(rmax(1)));
66  double rbunch_min = std::hypot(xmin, ymin);
67  double rbunch_max = std::hypot(xmax, ymax);
68 
69  if ( rbunch_max > rmin_m && rbunch_min < rmax_m ){ // check similar to z
70  return true;
71  }
72  }
73  return false;
74 }
75 
76 // rectangle collimators in cyclotron cylindrical coordinates
77 // when there is no particlematterinteraction, the particle hitting collimator is deleted directly
78 bool CCollimator::doCheck(PartBunchBase<double, 3> *bunch, const int turnnumber, const double t, const double /*tstep*/) {
79 
80  bool flagNeedUpdate = false;
81  size_t tempnum = bunch->getLocalNum();
82  int pflag = 0;
83  // now check each particle in bunch
84  for (unsigned int i = 0; i < tempnum; ++i) {
85  if (bunch->R[i](2) < zend_m && bunch->R[i](2) > zstart_m ) {
86  // only now careful check in r
87  pflag = checkPoint(bunch->R[i](0), bunch->R[i](1));
89  if ((pflag != 0) && (bunch->Bin[i] != -1)) {
90  if (!parmatint_m) {
91  lossDs_m->addParticle(OpalParticle(bunch->ID[i],
92  bunch->R[i], bunch->P[i],
93  t, bunch->Q[i], bunch->M[i]),
94  std::make_pair(turnnumber, bunch->bunchNum[i]));
95  }
96  bunch->Bin[i] = -1;
97  flagNeedUpdate = true;
98  }
99  }
100  }
101  return flagNeedUpdate;
102 }
103 
104 bool CCollimator::doFinaliseCheck(PartBunchBase<double, 3> *bunch, bool flagNeedUpdate) {
105  reduce(&flagNeedUpdate, &flagNeedUpdate + 1, &flagNeedUpdate, OpBitwiseOrAssign());
106  if (flagNeedUpdate && parmatint_m) {
107  Vector_t rmin, rmax;
108  bunch->get_bounds(rmin, rmax);
109  std::pair<Vector_t, double> boundingSphere;
110  boundingSphere.first = 0.5 * (rmax + rmin);
111  boundingSphere.second = euclidean_norm(rmax - boundingSphere.first);
112  parmatint_m->apply(bunch, boundingSphere);
113  }
114  return flagNeedUpdate;
115 }
116 
119 }
120 
121 void CCollimator::goOnline(const double &) {
122  print();
123  online_m = true;
124 }
125 
127  *gmsg << "* Finalize cyclotron collimator " << getName() << endl;
128 }
129 
131  if (RefPartBunch_m == nullptr) {
132  if (!informed_m) {
133  std::string errormsg = Fieldmap::typeset_msg("BUNCH SIZE NOT SET", "warning");
134  ERRORMSG(errormsg << endl);
135  if (Ippl::myNode() == 0) {
136  std::ofstream omsg("errormsg.txt", std::ios_base::app);
137  omsg << errormsg << std::endl;
138  omsg.close();
139  }
140  informed_m = true;
141  }
142  return;
143  }
144 }
145 
146 void CCollimator::setDimensions(double xstart, double xend, double ystart, double yend, double zstart, double zend, double width) {
147  setDimensions(xstart, xend, ystart, yend);
148  zstart_m = zstart;
149  zend_m = zend;
150  width_m = width;
151  // zstart and zend are independent from x, y
152  if (zstart_m > zend_m) {
153  std::swap(zstart_m, zend_m);
154  }
155  setGeom(width_m);
156 }
157 
158 void CCollimator::getDimensions(double &zBegin, double &zEnd) const {
159  zBegin = 0.0;
160  zEnd = getElementLength();
161 }
162 
164  return CCOLLIMATOR;
165 }
166 
168  // calculate maximum r from these
170  for (int i=0; i<4; i++) {
171  double r = std::hypot(geom_m[i].x, geom_m[i].y);
172  rmax_m = std::max(rmax_m, r);
173  }
174  // some debug printout
175  // for (int i=0; i<4; i++) {
176  // *gmsg << "point " << i << " ( " << geom_m[i].x << ", " << geom_m[i].y << ")" << endl;
177  // }
178  // *gmsg << "rmin " << rmin_m << " rmax " << rmax_m << endl;
179 }
Inform * gmsg
Definition: Main.cpp:62
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Definition: Vector.h:243
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
bool reduce(Communicate &, InputIterator, InputIterator, OutputIterator, const ReduceOp &, bool *IncludeVal=0)
Definition: GlobalComm.hpp:55
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
#define ERRORMSG(msg)
Definition: IpplInfo.h:350
const std::string name
ParticlePos_t & R
ParticleAttrib< int > Bin
size_t getLocalNum() const
ParticleAttrib< double > M
ParticleAttrib< Vector_t > P
ParticleAttrib< double > Q
void get_bounds(Vector_t &rmin, Vector_t &rmax)
ParticleAttrib< short > bunchNum
ParticleIndex_t & ID
virtual void visitCCollimator(const CCollimator &)=0
Apply the algorithm to a collimator.
double zend_m
Definition: CCollimator.h:81
virtual void doSetGeom() override
Calculate extend in r.
virtual void accept(BeamlineVisitor &) const override
Apply visitor to CCollimator.
Definition: CCollimator.cpp:52
virtual void goOnline(const double &kineticEnergy) override
Override implementation of PluginElement.
virtual void getDimensions(double &zBegin, double &zEnd) const override
virtual ElementBase::ElementType getType() const override
Get element type std::string.
virtual bool doPreCheck(PartBunchBase< double, 3 > *) override
Virtual hook for preCheck.
Definition: CCollimator.cpp:56
bool informed_m
Flag if error information already printed.
Definition: CCollimator.h:77
virtual void doInitialise(PartBunchBase< double, 3 > *bunch) override
Initialise particle matter interaction.
double zstart_m
input geometry positions
Definition: CCollimator.h:80
void print()
unused check method
virtual bool doFinaliseCheck(PartBunchBase< double, 3 > *bunch, bool flagNeedUpdate) override
Virtual hook for finaliseCheck.
virtual bool doCheck(PartBunchBase< double, 3 > *bunch, const int turnnumber, const double t, const double tstep) override
Record hits when bunch particles pass.
Definition: CCollimator.cpp:78
double width_m
Definition: CCollimator.h:82
double rmax_m
maximum extend in r
Definition: CCollimator.h:84
ParticleMatterInteractionHandler * parmatint_m
Definition: CCollimator.h:86
void setDimensions(double xstart, double xend, double ystart, double yend)
unhide PluginElement::setDimensions(double xstart, double xend, double ystart, double yend)
virtual ~CCollimator()
Definition: CCollimator.cpp:50
virtual void doFinalise() override
Virtual hook for finalise.
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
int checkPoint(const double &x, const double &y) const
Checks if coordinate is within element.
void setGeom(const double dist)
Sets geometry geom_m with element width dist.
Point geom_m[5]
actual geometry positions with adaptive width such that each particle hits element once per turn
double xstart_m
input geometry positions
std::unique_ptr< LossDataSink > lossDs_m
Pointer to Loss instance.
double rmin_m
radius closest to the origin
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition: Fieldmap.cpp:652
virtual void apply(PartBunchBase< double, 3 > *bunch, const std::pair< Vector_t, double > &boundingSphere)=0
Definition: Inform.h:42
static int myNode()
Definition: IpplInfo.cpp:691