OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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 - 2021, 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 "Structure/LossDataSink.h"
25 #include "Utilities/Options.h"
26 
27 #include <memory>
28 #include <string>
29 
30 extern Inform *gmsg;
31 
32 
34  Degrader("")
35 {}
36 
38  Component(right),
39  PosX_m(right.PosX_m),
40  PosY_m(right.PosY_m),
41  PosZ_m(right.PosZ_m),
42  MomentumX_m(right.MomentumX_m),
43  MomentumY_m(right.MomentumY_m),
44  MomentumZ_m(right.MomentumZ_m),
45  time_m(right.time_m),
46  id_m(right.id_m)
47 {}
48 
49 Degrader::Degrader(const std::string &name):
51 {}
52 
53 
55  if(online_m)
56  goOffline();
57 }
58 
59 
60 void Degrader::accept(BeamlineVisitor &visitor) const {
61  visitor.visitDegrader(*this);
62 }
63 
64 
65 inline bool Degrader::isInMaterial(double z ) {
70  return ((z > 0.0) && (z <= getElementLength()));
71 }
72 
73 bool Degrader::apply(const size_t &i, const double &t, Vector_t &/*E*/, Vector_t &/*B*/) {
74 
75  const Vector_t &R = RefPartBunch_m->R[i];
76  const Vector_t &P = RefPartBunch_m->P[i];
77  const double recpgamma = Physics::c * RefPartBunch_m->dt[i] / std::sqrt(1.0 + dot(P, P));
78 
79  if (isInMaterial(R(2))) {
80  //if particle was allready marked as -1 (it means it should have gone into degrader but didn't)
81  //set the label to -2 (will not go into degrader and will be deleted when particles per core > 2)
82  if (RefPartBunch_m->Bin[i] < 0) {
83  RefPartBunch_m->Bin[i] = -2;
84  } else {
85  RefPartBunch_m->Bin[i] = -1;
86  }
87  double frac = -R(2) / P(2) * recpgamma;
88  PosX_m.push_back(R(0));
89  PosY_m.push_back(R(1));
90  PosZ_m.push_back(R(2));
91  MomentumX_m.push_back(P(0));
92  MomentumY_m.push_back(P(1));
93  MomentumZ_m.push_back(P(2));
94  time_m.push_back(t + frac * RefPartBunch_m->dt[i]);
95  id_m.push_back(RefPartBunch_m->ID[i]);
96  }
97 
98  return false;
99 }
100 
102  const Vector_t &P,
103  const double &/*t*/,
104  Vector_t &E,
105  Vector_t &/*B*/) {
106  if (!isInMaterial(R(2))) return false;
107 
108  Vector_t updatedP = P;
110  double deltaP = euclidean_norm(updatedP) - euclidean_norm(P);
111  E(2) += deltaP * RefPartBunch_m->getM() / (RefPartBunch_m->getdT() * RefPartBunch_m->getQ() * Physics::c);
112 
113  return isDead;
114 }
115 
116 void Degrader::initialise(PartBunchBase<double, 3> *bunch, double &startField, double &endField) {
117  initialise(bunch);
118  endField = startField + getElementLength();
119 }
120 
122  RefPartBunch_m = bunch;
123  lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getOutputFN(), !Options::asciidump));
124 }
125 
127  *gmsg << "* Finalize Degrader" << endl;
128 }
129 
130 void Degrader::goOnline(const double &) {
131  Inform msg("Degrader::goOnline ");
132 
133  int maximumSize = (int)(1.1 * RefPartBunch_m->getLocalNum());
134 
135  PosX_m.reserve(maximumSize);
136  PosY_m.reserve(maximumSize);
137  PosZ_m.reserve(maximumSize);
138  MomentumX_m.reserve(maximumSize);
139  MomentumY_m.reserve(maximumSize);
140  MomentumZ_m.reserve(maximumSize);
141  time_m.reserve(maximumSize);
142  id_m.reserve(maximumSize);
143  online_m = true;
144 }
145 
147  Inform msg("Degrader::goOffline ");
148  online_m = false;
149  lossDs_m->save();
150  msg << " done..." << endl;
151 }
152 
153 bool Degrader::bends() const {
154  return false;
155 }
156 
157 void Degrader::getDimensions(double &zBegin, double &zEnd) const {
158  zBegin = 0.0;
159  zEnd = getElementLength();
160 }
161 
163  return DEGRADER;
164 }
165 
167  return "DEGRADER";
168 }
Inform * gmsg
Definition: Main.cpp:62
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Definition: Vector.h:243
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition: Vector3D.cpp:118
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
const std::string name
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:51
bool asciidump
Definition: Options.cpp:85
ParticlePos_t & R
ParticleAttrib< int > Bin
double getQ() const
Access to reference data.
size_t getLocalNum() const
ParticleAttrib< Vector_t > P
double getdT() const
ParticleAttrib< double > dt
ParticleIndex_t & ID
double getM() const
virtual void visitDegrader(const Degrader &)=0
Apply the algorithm to a degrader.
Interface for a single beam element.
Definition: Component.h:50
bool online_m
Definition: Component.h:195
PartBunchBase< double, 3 > * RefPartBunch_m
Definition: Component.h:194
virtual bool bends() const
Definition: Degrader.cpp:153
virtual bool applyToReferenceParticle(const Vector_t &R, const Vector_t &P, const double &t, Vector_t &E, Vector_t &B)
Definition: Degrader.cpp:101
virtual ElementBase::ElementType getType() const
Get element type std::string.
Definition: Degrader.cpp:162
std::vector< double > MomentumZ_m
Definition: Degrader.h:94
std::string getDegraderShape()
Definition: Degrader.cpp:166
virtual void finalise()
Definition: Degrader.cpp:126
virtual void goOnline(const double &kineticEnergy)
Definition: Degrader.cpp:130
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B)
Definition: Degrader.cpp:73
std::vector< double > time_m
Definition: Degrader.h:95
virtual bool isInMaterial(double z)
Definition: Degrader.cpp:65
virtual void getDimensions(double &zBegin, double &zEnd) const
Definition: Degrader.cpp:157
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField)
Definition: Degrader.cpp:116
std::vector< double > MomentumY_m
Definition: Degrader.h:93
Degrader()
Definition: Degrader.cpp:33
std::vector< double > PosY_m
Definition: Degrader.h:90
std::vector< double > PosX_m
Definition: Degrader.h:89
virtual ~Degrader()
Definition: Degrader.cpp:54
std::unique_ptr< LossDataSink > lossDs_m
Definition: Degrader.h:98
virtual void goOffline()
Definition: Degrader.cpp:146
std::vector< double > PosZ_m
Definition: Degrader.h:91
virtual void accept(BeamlineVisitor &) const
Apply visitor to Degrader.
Definition: Degrader.cpp:60
std::vector< int > id_m
Definition: Degrader.h:96
std::vector< double > MomentumX_m
Definition: Degrader.h:92
virtual ParticleMatterInteractionHandler * getParticleMatterInteraction() const
Definition: ElementBase.h:500
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:432
std::string getOutputFN() const
Get output filename.
virtual bool computeEnergyLoss(PartBunchBase< double, 3 > *bunch, Vector_t &P, const double deltat, bool includeFluctuations=true) const =0
Definition: Inform.h:42