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