OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Stripper.cpp
Go to the documentation of this file.
1 #include "AbsBeamline/Stripper.h"
2 
5 #include "Physics/Physics.h"
7 
8 extern Inform *gmsg;
9 
11 {}
12 
13 Stripper::Stripper(const std::string &name):
14  PluginElement(name),
15  opcharge_m(0.0),
16  opmass_m(0.0),
17  opyield_m(1.0),
18  stop_m(true)
19 {}
20 
22  PluginElement(right),
23  opcharge_m(right.opcharge_m),
24  opmass_m(right.opmass_m),
25  opyield_m(right.opyield_m),
26  stop_m(right.stop_m)
27 {}
28 
30 
31 void Stripper::accept(BeamlineVisitor &visitor) const {
32  visitor.visitStripper(*this);
33 }
34 
36  *gmsg << "* Finalize stripper " << getName() << endl;
37 }
38 
39 void Stripper::setOPCharge(double charge) {
40  opcharge_m = charge;
41 }
42 
43 void Stripper::setOPMass(double mass) {
44  opmass_m = mass;
45 }
46 
47 void Stripper::setOPYield(double yield) {
48  opyield_m = yield;
49 }
50 
51 void Stripper::setStop(bool stopflag) {
52  stop_m = stopflag;
53 }
54 
55 double Stripper::getOPCharge() const {
56  return opcharge_m;
57 }
58 
59 double Stripper::getOPMass() const {
60  return opmass_m;
61 }
62 
63 double Stripper::getOPYield() const {
64  return opyield_m;
65 }
66 
67 bool Stripper::getStop () const {
68  return stop_m;
69 }
70 
72  Vector_t rmin, rmax, strippoint;
73  bunch->get_bounds(rmin, rmax);
74  // interested in absolute maximum
75  double xmax = std::max(std::abs(rmin(0)), std::abs(rmax(0)));
76  double ymax = std::max(std::abs(rmin(1)), std::abs(rmax(1)));
77  double rbunch_max = std::hypot(xmax, ymax);
78 
79  if(rbunch_max > rmin_m - 10.0) {
80  return true;
81  }
82  return false;
83 }
84 
85 //change the stripped particles to outcome particles
86 bool Stripper::doCheck(PartBunchBase<double, 3> *bunch, const int turnnumber, const double t, const double tstep) {
87  bool flagNeedUpdate = false;
88  Vector_t strippoint;
89 
90  size_t count = 0;
91  size_t tempnum = bunch->getLocalNum();
92 
93  Inform gmsgALL("OPAL", INFORM_ALL_NODES);
94  for(unsigned int i = 0; i < tempnum; ++i) {
95  if(bunch->PType[i] != ParticleType::REGULAR) continue;
96 
97  double tangle = calculateIncidentAngle(bunch->P[i](0), bunch->P[i](1));
98  changeWidth(bunch, i, tstep, tangle);
99  int pflag = checkPoint(bunch->R[i](0), bunch->R[i](1));
100  if(pflag == 0) continue;
101 
102  // dist1 > 0, right hand, dt > 0; dist1 < 0, left hand, dt < 0
103  double dist1 = (A_m * bunch->R[i](0) + B_m * bunch->R[i](1) + C_m) / R_m * 1.0e-3; // [m]
104  double dist2 = dist1 * sqrt(1.0 + 1.0 / tangle / tangle);
105  double dt = dist2 / (sqrt(1.0 - 1.0 / (1.0 + dot(bunch->P[i], bunch->P[i]))) * Physics::c) * 1.0e9; // [ns]
106  strippoint(0) = (B_m * B_m * bunch->R[i](0) - A_m * B_m* bunch->R[i](1) - A_m * C_m) / (R_m * R_m);
107  strippoint(1) = (A_m * A_m * bunch->R[i](1) - A_m * B_m* bunch->R[i](0) - B_m * C_m) / (R_m * R_m);
108  strippoint(2) = bunch->R[i](2);
109  strippoint = strippoint*0.001; // mm->m
110  lossDs_m->addParticle(strippoint, bunch->P[i], bunch->ID[i], t+dt,
111  turnnumber, bunch->bunchNum[i]);
112 
113  flagNeedUpdate = true;
114  if (stop_m) {
115  bunch->Bin[i] = -1;
116  gmsgALL << level4 << getName() << ": Particle " << bunch->ID[i] << " is deleted by stripper " << getName() << endl;
117  } else {
118  gmsgALL << level4 << getName() << ": Particle " << bunch->ID[i] << " collide in stripper " << getName() << endl;
119  // change charge and mass of PartData when the reference particle hits the stripper.
120  if(bunch->ID[i] == 0)
122 
123  // change the mass and charge
124  bunch->M[i] = opmass_m;
125  bunch->Q[i] = opcharge_m * Physics::q_e;
126  bunch->PType[i] = ParticleType::STRIPPED;
127 
128  int j = 1;
129  //create new particles
130  while (j < opyield_m){
131  bunch->create(1);
132  size_t index = tempnum + count;
133  bunch->R[index] = bunch->R[i];
134  bunch->P[index] = bunch->P[i];
135  bunch->Q[index] = bunch->Q[i];
136  bunch->M[index] = bunch->M[i];
137  // once the particle is stripped, change PType from 0 to 1 as a flag so as to avoid repetitive stripping.
138  bunch->PType[index] = ParticleType::STRIPPED;
139  if(bunch->weHaveBins())
140  bunch->Bin[index] = bunch->Bin[i];
141 
142  count++;
143  j++;
144  }
145  }
146  }
147  return flagNeedUpdate;
148 }
149 
150 bool Stripper::doFinaliseCheck(PartBunchBase<double, 3> *bunch, bool flagNeedUpdate) {
151  reduce(&flagNeedUpdate, &flagNeedUpdate + 1, &flagNeedUpdate, OpBitwiseOrAssign());
152 
153  if(!stop_m){
154  // change charge and mass of PartData when the reference particle hits the stripper.
155  if (bunch->getPType() == ParticleType::STRIPPED) {
156  bunch->resetM(opmass_m * 1.0e9); // GeV -> eV
157  bunch->resetQ(opcharge_m); // elementary charge
158  }
159  }
160 
161  return flagNeedUpdate;
162 }
163 
165  return STRIPPER;
166 }
bool weHaveBins() const
ParticleAttrib< Vector_t > P
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
double opyield_m
Yield of the out-coming particle.
Definition: Stripper.h:50
void setOPCharge(double charge)
Member variable access.
Definition: Stripper.cpp:39
constexpr double e
The value of .
Definition: Physics.h:40
void setPType(ParticleType::type)
bool getStop() const
Definition: Stripper.cpp:67
double calculateIncidentAngle(double xp, double yp) const
Calculate angle of particle/bunch wrt to element.
void create(size_t M)
ParticleType::type getPType() const
#define INFORM_ALL_NODES
Definition: Inform.h:38
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:123
virtual bool doCheck(PartBunchBase< double, 3 > *bunch, const int turnnumber, const double t, const double tstep) override
Record hits when bunch particles pass.
Definition: Stripper.cpp:86
ParticleAttrib< double > Q
virtual bool doPreCheck(PartBunchBase< double, 3 > *) override
Virtual hook for preCheck.
Definition: Stripper.cpp:71
Inform * gmsg
Definition: Main.cpp:21
double C_m
Geometric lengths used in calculations.
virtual ~Stripper()
Definition: Stripper.cpp:29
virtual bool doFinaliseCheck(PartBunchBase< double, 3 > *bunch, bool flagNeedUpdate) override
Virtual hook for finaliseCheck.
Definition: Stripper.cpp:150
std::unique_ptr< LossDataSink > lossDs_m
Pointer to Loss instance.
virtual const std::string & getName() const
Get element name.
Definition: ElementBase.cpp:95
ParticleAttrib< short > bunchNum
void setOPYield(double yield)
Definition: Stripper.cpp:47
void resetQ(double q)
Inform & level4(Inform &inf)
Definition: Inform.cpp:48
double rmin_m
radius closest to the origin
void resetM(double m)
void setStop(bool stopflag)
Definition: Stripper.cpp:51
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition: Vector3D.cpp:118
double opmass_m
Mass of the out-coming particle.
Definition: Stripper.h:49
void changeWidth(PartBunchBase< double, 3 > *bunch, int i, const double tstep, const double tangle)
Change probe width depending on step size and angle of particle.
double opcharge_m
Charge number of the out-coming particle.
Definition: Stripper.h:48
bool reduce(Communicate &, InputIterator, InputIterator, OutputIterator, const ReduceOp &, bool *IncludeVal=0)
Definition: GlobalComm.hpp:55
void setOPMass(double mass)
Definition: Stripper.cpp:43
ParticleAttrib< short > PType
ParticleIndex_t & ID
virtual void visitStripper(const Stripper &)=0
Apply the algorithm to a particle stripper.
virtual void doFinalise() override
Virtual hook for finalise.
Definition: Stripper.cpp:35
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
double getOPYield() const
Definition: Stripper.cpp:63
double getOPMass() const
Definition: Stripper.cpp:59
virtual ElementBase::ElementType getType() const override
Override implementation of PluginElement.
Definition: Stripper.cpp:164
size_t getLocalNum() const
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
bool stop_m
Flag if particles should be stripped or stopped.
Definition: Stripper.h:51
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Stripper.
Definition: Stripper.cpp:31
constexpr double q_e
The elementary charge in As.
Definition: Physics.h:76
const std::string name
ParticleAttrib< int > Bin
ParticleAttrib< double > M
ParticlePos_t & R
void get_bounds(Vector_t &rmin, Vector_t &rmax)
Abstract algorithm.
Definition: Inform.h:41
Stripper()
Definition: Stripper.cpp:10
int checkPoint(const double &x, const double &y) const
Checks if coordinate is within element.
double getOPCharge() const
Definition: Stripper.cpp:55
Inform & endl(Inform &inf)
Definition: Inform.cpp:42