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