OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
Degrader.cpp
Go to the documentation of this file.
1 //
2 // Class Degrader
3 // Defines the abstract interface for a beam degrader.
4 //
5 // Copyright (c) 2000 - 2023, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // All rights reserved.
7 //
8 // This file is part of OPAL.
9 //
10 // OPAL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17 //
18 #include "AbsBeamline/Degrader.h"
19 
22 #include "Physics/Physics.h"
24 #include "Utilities/Options.h"
25 
26 #include <cmath>
27 #include <memory>
28 
29 extern Inform* gmsg;
30 
31 
33  Degrader("")
34 {}
35 
37  Component(right),
38  PosX_m(right.PosX_m),
39  PosY_m(right.PosY_m),
40  PosZ_m(right.PosZ_m),
41  MomentumX_m(right.MomentumX_m),
42  MomentumY_m(right.MomentumY_m),
43  MomentumZ_m(right.MomentumZ_m),
44  time_m(right.time_m),
45  id_m(right.id_m),
46  width_m(right.width_m),
47  height_m(right.height_m)
48 {}
49 
50 Degrader::Degrader(const std::string& name):
51  Component(name),
52  width_m(0.0),
53  height_m(0.0)
54 { }
55 
57  if(online_m)
58  goOffline();
59 }
60 
61 void Degrader::accept(BeamlineVisitor& visitor) const {
62  visitor.visitDegrader(*this);
63 }
64 
65 void Degrader::setDimensions(double xsize, double ysize) {
66  width_m = xsize;
67  height_m = ysize;
68 }
69 
70 
71 bool Degrader::isInside(const Vector_t& R) const {
75  bool hit = false;
76  if ( R(2) > 0.0 && R(2) <= getElementLength() &&
77  (4 * (std::pow(R(0) / width_m, 2) + std::pow(R(1) / height_m, 2)) <= 1) ) {
78  hit = true;
79  }
80  return hit;
81 }
82 
83 bool Degrader::apply(const size_t& i, const double& t, Vector_t& /*E*/, Vector_t& /*B*/) {
84 
85  const Vector_t& R = RefPartBunch_m->R[i];
86  const Vector_t& P = RefPartBunch_m->P[i];
87 
88  if ( isInside(R) ) {
89  //if particle was already marked as -1 (it means it should have gone into degrader but didn't)
90  //set the label to -2 (will not go into degrader and will be deleted when particles per core > 2)
91  if (RefPartBunch_m->Bin[i] < 0) {
92  RefPartBunch_m->Bin[i] = -2;
93  } else {
94  RefPartBunch_m->Bin[i] = -1;
95  }
96  const double& dt = RefPartBunch_m->dt[i];
97  const double recpgamma = Physics::c * dt / std::sqrt(1.0 + dot(P, P));
98  double frac = -R(2) / (P(2) * recpgamma);
99  PosX_m.push_back(R(0));
100  PosY_m.push_back(R(1));
101  PosZ_m.push_back(R(2));
102  MomentumX_m.push_back(P(0));
103  MomentumY_m.push_back(P(1));
104  MomentumZ_m.push_back(P(2));
105  time_m.push_back(t + frac * dt);
106  id_m.push_back(RefPartBunch_m->ID[i]);
107  }
108 
109  return false;
110 }
111 
113  const Vector_t& P,
114  const double& /*t*/,
115  Vector_t& E,
116  Vector_t& /*B*/) {
117  if ( !isInside(R) ) return false;
118 
119  Vector_t updatedP = P;
121  double deltaP = euclidean_norm(updatedP) - euclidean_norm(P);
122  E(2) += deltaP * RefPartBunch_m->getM() / (RefPartBunch_m->getdT() * RefPartBunch_m->getQ() * Physics::c);
123 
124  return isDead;
125 }
126 
127 void Degrader::initialise(PartBunchBase<double, 3>* bunch, double& startField, double& endField) {
128  initialise(bunch);
129  endField = startField + getElementLength();
130 }
131 
133  RefPartBunch_m = bunch;
134 }
135 
137  *gmsg << "* Finalize degrader " << getName() << endl;
138 }
139 
140 void Degrader::goOnline(const double&) {
141  Inform msg("Degrader::goOnline ");
142 
143  int maximumSize = (int)(1.1 * RefPartBunch_m->getLocalNum());
144 
145  PosX_m.reserve(maximumSize);
146  PosY_m.reserve(maximumSize);
147  PosZ_m.reserve(maximumSize);
148  MomentumX_m.reserve(maximumSize);
149  MomentumY_m.reserve(maximumSize);
150  MomentumZ_m.reserve(maximumSize);
151  time_m.reserve(maximumSize);
152  id_m.reserve(maximumSize);
153  online_m = true;
154 }
155 
157  Inform msg("Degrader::goOffline ");
158  online_m = false;
159  msg << " done..." << endl;
160 }
161 
162 bool Degrader::bends() const {
163  return false;
164 }
165 
166 void Degrader::getDimensions(double& zBegin, double& zEnd) const {
167  zBegin = 0.0;
168  zEnd = getElementLength();
169 }
170 
172  return ElementType::DEGRADER;
173 }
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:45
virtual void getDimensions(double &zBegin, double &zEnd) const override
Definition: Degrader.cpp:166
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Degrader.
Definition: Degrader.cpp:61
virtual bool isInside(const Vector_t &R) const override
Definition: Degrader.cpp:71
Degrader()
Definition: Degrader.cpp:32
virtual ~Degrader()
Definition: Degrader.cpp:56
virtual void finalise() override
Definition: Degrader.cpp:136
ParticleAttrib< Vector_t > P
virtual ElementType getType() const override
Get element type std::string.
Definition: Degrader.cpp:171
PartBunchBase< double, 3 > * RefPartBunch_m
Definition: Component.h:191
std::vector< double > PosX_m
Definition: Degrader.h:85
virtual const std::string & getName() const
Get element name.
double getQ() const
Access to reference data.
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
double getdT() const
double getM() const
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Definition: Vector.h:243
virtual bool bends() const override
Definition: Degrader.cpp:162
bool online_m
Definition: Component.h:192
size_t getLocalNum() const
virtual bool computeEnergyLoss(PartBunchBase< double, 3 > *bunch, Vector_t &P, const double deltat, bool includeFluctuations=true) const =0
Definition: Inform.h:42
std::vector< double > PosZ_m
Definition: Degrader.h:87
ElementType
Definition: ElementBase.h:88
virtual void visitDegrader(const Degrader &)=0
Apply the algorithm to a degrader.
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:415
std::vector< double > MomentumY_m
Definition: Degrader.h:89
double width_m
Definition: Degrader.h:94
std::vector< double > time_m
Definition: Degrader.h:91
ParticleAttrib< double > dt
virtual void goOffline() override
Definition: Degrader.cpp:156
virtual void goOnline(const double &kineticEnergy) override
Definition: Degrader.cpp:140
const std::string name
ParticlePos_t & R
double height_m
Definition: Degrader.h:95
ParticleIndex_t & ID
virtual ParticleMatterInteractionHandler * getParticleMatterInteraction() const
Definition: ElementBase.h:483
std::vector< int > id_m
Definition: Degrader.h:92
std::vector< double > MomentumX_m
Definition: Degrader.h:88
ParticleAttrib< int > Bin
Interface for a single beam element.
Definition: Component.h:50
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
std::vector< double > MomentumZ_m
Definition: Degrader.h:90
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
Definition: Degrader.cpp:127
void setDimensions(double xsize, double ysize)
Definition: Degrader.cpp:65
Inform * gmsg
Definition: Main.cpp:70
virtual bool applyToReferenceParticle(const Vector_t &R, const Vector_t &P, const double &t, Vector_t &E, Vector_t &B) override
Definition: Degrader.cpp:112
std::vector< double > PosY_m
Definition: Degrader.h:86
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) override
Definition: Degrader.cpp:83
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition: Vector3D.cpp:118