OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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//
27
30#include "Physics/Physics.h"
31#include "Physics/Units.h"
33
34extern Inform *gmsg;
35
37{}
38
39Stripper::Stripper(const std::string &name):
41 opcharge_m(0.0),
42 opmass_m(0.0),
43 opyield_m(1.0),
44 stop_m(true)
45{}
46
48 PluginElement(right),
49 opcharge_m(right.opcharge_m),
50 opmass_m(right.opmass_m),
51 opyield_m(right.opyield_m),
52 stop_m(right.stop_m)
53{}
54
56
57void Stripper::accept(BeamlineVisitor &visitor) const {
58 visitor.visitStripper(*this);
59}
60
62 *gmsg << "* Finalize stripper " << getName() << endl;
63}
64
65void Stripper::setOPCharge(double charge) {
66 opcharge_m = charge;
67}
68
69void Stripper::setOPMass(double mass) {
70 opmass_m = mass;
71}
72
73void Stripper::setOPYield(double yield) {
74 opyield_m = yield;
75}
76
77void Stripper::setStop(bool stopflag) {
78 stop_m = stopflag;
79}
80
81double Stripper::getOPCharge() const {
82 return opcharge_m;
83}
84
85double Stripper::getOPMass() const {
86 return opmass_m;
87}
88
89double Stripper::getOPYield() const {
90 return opyield_m;
91}
92
93bool Stripper::getStop () const {
94 return stop_m;
95}
96
98 Vector_t rmin, rmax, strippoint;
99 bunch->get_bounds(rmin, rmax);
100 // interested in absolute maximum
101 double xmax = std::max(std::abs(rmin(0)), std::abs(rmax(0)));
102 double ymax = std::max(std::abs(rmin(1)), std::abs(rmax(1)));
103 double rbunch_max = std::hypot(xmax, ymax);
104
105 if (rbunch_max > rmin_m - 1e-2) {
106 return true;
107 }
108 return false;
109}
110
111//change the stripped particles to outcome particles
112bool Stripper::doCheck(PartBunchBase<double, 3> *bunch, const int turnnumber, const double t, const double tstep) {
113 bool flagNeedUpdate = false;
114 Vector_t strippoint;
115
116 size_t count = 0;
117 size_t tempnum = bunch->getLocalNum();
118
119 Inform gmsgALL("OPAL", INFORM_ALL_NODES);
120 for (unsigned int i = 0; i < tempnum; ++i) {
121 if (bunch->POrigin[i] != ParticleOrigin::REGULAR) continue;
122
123 double tangle = calculateIncidentAngle(bunch->P[i](0), bunch->P[i](1));
124 changeWidth(bunch, i, tstep, tangle);
125 int pflag = checkPoint(bunch->R[i](0), bunch->R[i](1));
126 if (pflag == 0) continue;
127
128 // dist1 > 0, right hand, dt > 0; dist1 < 0, left hand, dt < 0
129 double dist1 = (A_m * bunch->R[i](0) + B_m * bunch->R[i](1) + C_m) / R_m; // [m]
130 double dist2 = dist1 * std::sqrt(1.0 + 1.0 / tangle / tangle);
131 double dt = dist2 / (std::sqrt(1.0 - 1.0 / (1.0 + dot(bunch->P[i], bunch->P[i]))) * Physics::c);
132 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);
133 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);
134 strippoint(2) = bunch->R[i](2);
135 lossDs_m->addParticle(OpalParticle(bunch->ID[i],
136 strippoint, bunch->P[i],
137 t+dt, bunch->Q[i], bunch->M[i]),
138 std::make_pair(turnnumber, bunch->bunchNum[i]));
139
140 flagNeedUpdate = true;
141 if (stop_m) {
142 bunch->Bin[i] = -1;
143 gmsgALL << level4 << getName() << ": Particle " << bunch->ID[i] << " is deleted by stripper " << getName() << endl;
144 } else {
145 gmsgALL << level4 << getName() << ": Particle " << bunch->ID[i] << " collide in stripper " << getName() << endl;
146 // change charge and mass of PartData when the reference particle hits the stripper.
147 if (bunch->ID[i] == 0)
149
150 // change the mass and charge
151 bunch->M[i] = opmass_m;
152 bunch->Q[i] = opcharge_m * Physics::q_e;
154
155 int j = 1;
156 //create new particles
157 while (j < opyield_m){
158 bunch->create(1);
159 size_t index = tempnum + count;
160 bunch->R[index] = bunch->R[i];
161 bunch->P[index] = bunch->P[i];
162 bunch->Q[index] = bunch->Q[i];
163 bunch->M[index] = bunch->M[i];
164 // once the particle is stripped, change POrigin from 0 to 1 as a flag so as to avoid repetitive stripping.
165 bunch->POrigin[index] = ParticleOrigin::STRIPPED;
166 if (bunch->weHaveBins())
167 bunch->Bin[index] = bunch->Bin[i];
168
169 count++;
170 j++;
171 }
172 }
173 }
174 return flagNeedUpdate;
175}
176
177bool Stripper::doFinaliseCheck(PartBunchBase<double, 3> *bunch, bool flagNeedUpdate) {
178 reduce(&flagNeedUpdate, &flagNeedUpdate + 1, &flagNeedUpdate, OpBitwiseOrAssign());
179
180 if (!stop_m){
181 // change charge and mass of PartData when the reference particle hits the stripper.
182 if (bunch->getPOrigin() == ParticleOrigin::STRIPPED) {
183 bunch->resetM(opmass_m * Units::GeV2eV);
184 bunch->resetQ(opcharge_m); // elementary charge
185 }
186 }
187
188 return flagNeedUpdate;
189}
190
193}
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition: Vector3D.cpp:118
Inform * gmsg
Definition: Main.cpp:61
ElementType
Definition: ElementBase.h:88
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
bool reduce(Communicate &, InputIterator, InputIterator, OutputIterator, const ReduceOp &, bool *IncludeVal=0)
Definition: GlobalComm.hpp:55
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:84
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Inform & level4(Inform &inf)
Definition: Inform.cpp:48
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
#define INFORM_ALL_NODES
Definition: Inform.h:39
const std::string name
constexpr double q_e
The elementary charge in As.
Definition: Physics.h:69
constexpr double e
The value of.
Definition: Physics.h:39
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:45
constexpr double GeV2eV
Definition: Units.h:68
ParticlePos_t & R
ParticleAttrib< int > Bin
ParticleOrigin getPOrigin() const
void get_bounds(Vector_t &rmin, Vector_t &rmax) 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)
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:55
virtual bool doPreCheck(PartBunchBase< double, 3 > *) override
Virtual hook for preCheck.
Definition: Stripper.cpp:97
void setOPCharge(double charge)
Member variable access.
Definition: Stripper.cpp:65
double getOPMass() const
Definition: Stripper.cpp:85
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Stripper.
Definition: Stripper.cpp:57
Stripper()
Definition: Stripper.cpp:36
double getOPCharge() const
Definition: Stripper.cpp:81
void setStop(bool stopflag)
Definition: Stripper.cpp:77
bool getStop() const
Definition: Stripper.cpp:93
void setOPYield(double yield)
Definition: Stripper.cpp:73
void setOPMass(double mass)
Definition: Stripper.cpp:69
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:112
double getOPYield() const
Definition: Stripper.cpp:89
double opyield_m
Yield of the out-coming particle.
Definition: Stripper.h:73
virtual void doFinalise() override
Virtual hook for finalise.
Definition: Stripper.cpp:61
bool stop_m
Flag if particles should be stripped or stopped.
Definition: Stripper.h:74
virtual ElementType getType() const override
Override implementation of PluginElement.
Definition: Stripper.cpp:191
virtual bool doFinaliseCheck(PartBunchBase< double, 3 > *bunch, bool flagNeedUpdate) override
Virtual hook for finaliseCheck.
Definition: Stripper.cpp:177
Definition: Inform.h:42