OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Stripper.cpp
Go to the documentation of this file.
1 //
2 // Class Stripper
3 // The Stripper element defines the interface for a stripping foil
4 //
5 // Copyright (c) 2011, Jianjun Yang, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // Copyright (c) 2017-2020, Paul Scherrer Institut, Villigen PSI, Switzerland
7 // All rights reserved
8 //
9 // Implemented as part of the PhD thesis
10 // "Beam dynamics in high intensity cyclotrons including neighboring bunch effects"
11 // and the paper
12 // "Beam dynamics in high intensity cyclotrons including neighboring bunch effects:
13 // Model, implementation, and application"
14 // (https://journals.aps.org/prab/pdf/10.1103/PhysRevSTAB.13.064201)
15 //
16 // This file is part of OPAL.
17 //
18 // OPAL is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
25 //
26 #include "AbsBeamline/Stripper.h"
27 
30 #include "Physics/Physics.h"
31 #include "Structure/LossDataSink.h"
32 
33 extern Inform *gmsg;
34 
36 {}
37 
38 Stripper::Stripper(const std::string &name):
40  opcharge_m(0.0),
41  opmass_m(0.0),
42  opyield_m(1.0),
43  stop_m(true)
44 {}
45 
47  PluginElement(right),
48  opcharge_m(right.opcharge_m),
49  opmass_m(right.opmass_m),
50  opyield_m(right.opyield_m),
51  stop_m(right.stop_m)
52 {}
53 
55 
56 void Stripper::accept(BeamlineVisitor &visitor) const {
57  visitor.visitStripper(*this);
58 }
59 
61  *gmsg << "* Finalize stripper " << getName() << endl;
62 }
63 
64 void Stripper::setOPCharge(double charge) {
65  opcharge_m = charge;
66 }
67 
68 void Stripper::setOPMass(double mass) {
69  opmass_m = mass;
70 }
71 
72 void Stripper::setOPYield(double yield) {
73  opyield_m = yield;
74 }
75 
76 void Stripper::setStop(bool stopflag) {
77  stop_m = stopflag;
78 }
79 
80 double Stripper::getOPCharge() const {
81  return opcharge_m;
82 }
83 
84 double Stripper::getOPMass() const {
85  return opmass_m;
86 }
87 
88 double Stripper::getOPYield() const {
89  return opyield_m;
90 }
91 
92 bool Stripper::getStop () const {
93  return stop_m;
94 }
95 
97  Vector_t rmin, rmax, strippoint;
98  bunch->get_bounds(rmin, rmax);
99  // interested in absolute maximum
100  double xmax = std::max(std::abs(rmin(0)), std::abs(rmax(0)));
101  double ymax = std::max(std::abs(rmin(1)), std::abs(rmax(1)));
102  double rbunch_max = std::hypot(xmax, ymax);
103 
104  if (rbunch_max > rmin_m - 1e-2) {
105  return true;
106  }
107  return false;
108 }
109 
110 //change the stripped particles to outcome particles
111 bool Stripper::doCheck(PartBunchBase<double, 3> *bunch, const int turnnumber, const double t, const double tstep) {
112  bool flagNeedUpdate = false;
113  Vector_t strippoint;
114 
115  size_t count = 0;
116  size_t tempnum = bunch->getLocalNum();
117 
118  Inform gmsgALL("OPAL", INFORM_ALL_NODES);
119  for (unsigned int i = 0; i < tempnum; ++i) {
120  if (bunch->POrigin[i] != ParticleOrigin::REGULAR) continue;
121 
122  double tangle = calculateIncidentAngle(bunch->P[i](0), bunch->P[i](1));
123  changeWidth(bunch, i, tstep, tangle);
124  int pflag = checkPoint(bunch->R[i](0), bunch->R[i](1));
125  if (pflag == 0) continue;
126 
127  // dist1 > 0, right hand, dt > 0; dist1 < 0, left hand, dt < 0
128  double dist1 = (A_m * bunch->R[i](0) + B_m * bunch->R[i](1) + C_m) / R_m; // [m]
129  double dist2 = dist1 * std::sqrt(1.0 + 1.0 / tangle / tangle);
130  double dt = dist2 / (std::sqrt(1.0 - 1.0 / (1.0 + dot(bunch->P[i], bunch->P[i]))) * Physics::c);
131  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);
132  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);
133  strippoint(2) = bunch->R[i](2);
134  lossDs_m->addParticle(OpalParticle(bunch->ID[i],
135  strippoint, bunch->P[i],
136  t+dt, bunch->Q[i], bunch->M[i]),
137  std::make_pair(turnnumber, bunch->bunchNum[i]));
138 
139  flagNeedUpdate = true;
140  if (stop_m) {
141  bunch->Bin[i] = -1;
142  gmsgALL << level4 << getName() << ": Particle " << bunch->ID[i] << " is deleted by stripper " << getName() << endl;
143  } else {
144  gmsgALL << level4 << getName() << ": Particle " << bunch->ID[i] << " collide in stripper " << getName() << endl;
145  // change charge and mass of PartData when the reference particle hits the stripper.
146  if (bunch->ID[i] == 0)
148 
149  // change the mass and charge
150  bunch->M[i] = opmass_m;
151  bunch->Q[i] = opcharge_m * Physics::q_e;
152  bunch->POrigin[i] = ParticleOrigin::STRIPPED;
153 
154  int j = 1;
155  //create new particles
156  while (j < opyield_m){
157  bunch->create(1);
158  size_t index = tempnum + count;
159  bunch->R[index] = bunch->R[i];
160  bunch->P[index] = bunch->P[i];
161  bunch->Q[index] = bunch->Q[i];
162  bunch->M[index] = bunch->M[i];
163  // once the particle is stripped, change POrigin from 0 to 1 as a flag so as to avoid repetitive stripping.
164  bunch->POrigin[index] = ParticleOrigin::STRIPPED;
165  if (bunch->weHaveBins())
166  bunch->Bin[index] = bunch->Bin[i];
167 
168  count++;
169  j++;
170  }
171  }
172  }
173  return flagNeedUpdate;
174 }
175 
176 bool Stripper::doFinaliseCheck(PartBunchBase<double, 3> *bunch, bool flagNeedUpdate) {
177  reduce(&flagNeedUpdate, &flagNeedUpdate + 1, &flagNeedUpdate, OpBitwiseOrAssign());
178 
179  if (!stop_m){
180  // change charge and mass of PartData when the reference particle hits the stripper.
181  if (bunch->getPOrigin() == ParticleOrigin::STRIPPED) {
182  bunch->resetM(opmass_m * 1.0e9); // GeV -> eV
183  bunch->resetQ(opcharge_m); // elementary charge
184  }
185  }
186 
187  return flagNeedUpdate;
188 }
189 
191  return STRIPPER;
192 }
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
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
Inform & level4(Inform &inf)
Definition: Inform.cpp:48
#define INFORM_ALL_NODES
Definition: Inform.h:39
const std::string name
constexpr double q_e
The elementary charge in As.
Definition: Physics.h:75
constexpr double e
The value of.
Definition: Physics.h:39
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:51
ParticlePos_t & R
ParticleAttrib< int > Bin
ParticleOrigin getPOrigin() const
void resetM(double m)
size_t getLocalNum() const
ParticleAttrib< double > M
ParticleAttrib< Vector_t > P
ParticleAttrib< ParticleOrigin > POrigin
bool weHaveBins() const
ParticleAttrib< double > Q
void setPOrigin(ParticleOrigin)
void get_bounds(Vector_t &rmin, Vector_t &rmax)
ParticleAttrib< short > bunchNum
void create(size_t M)
void resetQ(double q)
ParticleIndex_t & ID
virtual void visitStripper(const Stripper &)=0
Apply the algorithm to a particle stripper.
virtual const std::string & getName() const
Get element name.
int checkPoint(const double &x, const double &y) const
Checks if coordinate is within element.
double C_m
Geometric lengths used in calculations.
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 calculateIncidentAngle(double xp, double yp) const
Calculate angle of particle/bunch wrt to element.
std::unique_ptr< LossDataSink > lossDs_m
Pointer to Loss instance.
double rmin_m
radius closest to the origin
double opcharge_m
Charge number of the out-coming particle.
Definition: Stripper.h:71
virtual ~Stripper()
Definition: Stripper.cpp:54
virtual bool doPreCheck(PartBunchBase< double, 3 > *) override
Virtual hook for preCheck.
Definition: Stripper.cpp:96
void setOPCharge(double charge)
Member variable access.
Definition: Stripper.cpp:64
double getOPMass() const
Definition: Stripper.cpp:84
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Stripper.
Definition: Stripper.cpp:56
virtual ElementBase::ElementType getType() const override
Override implementation of PluginElement.
Definition: Stripper.cpp:190
Stripper()
Definition: Stripper.cpp:35
double getOPCharge() const
Definition: Stripper.cpp:80
void setStop(bool stopflag)
Definition: Stripper.cpp:76
bool getStop() const
Definition: Stripper.cpp:92
void setOPYield(double yield)
Definition: Stripper.cpp:72
void setOPMass(double mass)
Definition: Stripper.cpp:68
double opmass_m
Mass of the out-coming particle.
Definition: Stripper.h:72
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:111
double getOPYield() const
Definition: Stripper.cpp:88
double opyield_m
Yield of the out-coming particle.
Definition: Stripper.h:73
virtual void doFinalise() override
Virtual hook for finalise.
Definition: Stripper.cpp:60
bool stop_m
Flag if particles should be stripped or stopped.
Definition: Stripper.h:74
virtual bool doFinaliseCheck(PartBunchBase< double, 3 > *bunch, bool flagNeedUpdate) override
Virtual hook for finaliseCheck.
Definition: Stripper.cpp:176
Definition: Inform.h:42