OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Degrader.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Degrader.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.1.1.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: Degrader
10 // Defines the abstract interface for a beam Degrader.
11 //
12 // ------------------------------------------------------------------------
13 // Class category: AbsBeamline
14 // ------------------------------------------------------------------------
15 //
16 // $Date: 2000/03/27 09:32:31 $
17 // $Author: fci $
18 //
19 // ------------------------------------------------------------------------
20 
21 #include "AbsBeamline/Degrader.h"
24 #include "Fields/Fieldmap.h"
25 #include "Structure/LossDataSink.h"
26 #include "Utilities/Options.h"
28 #include "Physics/Physics.h"
29 #include "Utilities/Util.h"
30 #include <memory>
31 
32 extern Inform *gmsg;
33 
34 using namespace std;
35 
36 // Class Degrader
37 // ------------------------------------------------------------------------
38 
40  Degrader("")
41 {}
42 
44  Component(right),
45  filename_m(right.filename_m),
46  PosX_m(right.PosX_m),
47  PosY_m(right.PosY_m),
48  PosZ_m(right.PosZ_m),
49  MomentumX_m(right.MomentumX_m),
50  MomentumY_m(right.MomentumY_m),
51  MomentumZ_m(right.MomentumZ_m),
52  time_m(right.time_m),
53  id_m(right.id_m)
54 {}
55 
56 Degrader::Degrader(const std::string &name):
57  Component(name),
58  filename_m(""),
59  PosX_m(0),
60  PosY_m(0),
61  PosZ_m(0),
62  MomentumX_m(0),
63  MomentumY_m(0),
64  MomentumZ_m(0),
65  time_m(0),
66  id_m(0)
67 {}
68 
69 
71 
72  if(online_m)
73  goOffline();
74 }
75 
76 
77 void Degrader::accept(BeamlineVisitor &visitor) const {
78  visitor.visitDegrader(*this);
79 }
80 
81 
82 inline bool Degrader::isInMaterial(double z ) {
87  return ((z > 0.0) && (z <= getElementLength()));
88 }
89 
90 bool Degrader::apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) {
91 
92  const Vector_t &R = RefPartBunch_m->R[i];
93  const Vector_t &P = RefPartBunch_m->P[i];
94  const double recpgamma = Physics::c * RefPartBunch_m->dt[i] / sqrt(1.0 + dot(P, P));
95 
96  if(isInMaterial(R(2))) {
97  //if particle was allready marked as -1 (it means it should have gone into degrader but didn't)
98  //set the label to -2 (will not go into degrader and will be deleted when particles per core > 2)
99  if (RefPartBunch_m->Bin[i] < 0)
100  RefPartBunch_m->Bin[i] = -2;
101  else
102  RefPartBunch_m->Bin[i] = -1;
103 
104  double frac = -R(2) / P(2) * recpgamma;
105  PosX_m.push_back(R(0));
106  PosY_m.push_back(R(1));
107  PosZ_m.push_back(R(2));
108  MomentumX_m.push_back(P(0));
109  MomentumY_m.push_back(P(1));
110  MomentumZ_m.push_back(P(2));
111  time_m.push_back(t + frac * RefPartBunch_m->dt[i]);
112  id_m.push_back(RefPartBunch_m->ID[i]);
113  }
114 
115  return false;
116 }
117 
119  const Vector_t &P,
120  const double &t,
121  Vector_t &E,
122  Vector_t &B) {
123  if (!isInMaterial(R(2))) return false;
124 
125  Vector_t updatedP = P;
126  bool isDead = getParticleMatterInteraction()->computeEnergyLoss(updatedP, RefPartBunch_m->getdT(), false);
127  double deltaP = euclidean_norm(updatedP) - euclidean_norm(P);
128  E(2) += deltaP * RefPartBunch_m->getM() / (RefPartBunch_m->getdT() * RefPartBunch_m->getQ() * Physics::c);
129 
130  return isDead;
131 }
132 
133 void Degrader::initialise(PartBunchBase<double, 3> *bunch, double &startField, double &endField) {
134  RefPartBunch_m = bunch;
135  endField = startField + getElementLength();
136 
137  if (filename_m == std::string(""))
138  lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getName(), !Options::asciidump));
139  else
140  lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(filename_m.substr(0, filename_m.rfind(".")), !Options::asciidump));
141 }
142 
144  RefPartBunch_m = bunch;
145  if (filename_m == std::string(""))
146  lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getName(), !Options::asciidump));
147  else
148  lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(filename_m.substr(0, filename_m.rfind(".")), !Options::asciidump));
149 }
150 
151 
153 {
154  *gmsg << "* Finalize Degrader" << endl;
155 }
156 
157 void Degrader::goOnline(const double &) {
158  Inform msg("Degrader::goOnline ");
159 
160  PosX_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
161  PosY_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
162  PosZ_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
163  MomentumX_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
164  MomentumY_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
165  MomentumZ_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
166  time_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
167  id_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
168  online_m = true;
169 }
170 
172  Inform msg("Degrader::goOffline ");
173  online_m = false;
174  lossDs_m->save();
175  msg << " done..." << endl;
176 }
177 
178 bool Degrader::bends() const {
179  return false;
180 }
181 
182 void Degrader::setOutputFN(std::string fn) {
183  filename_m = fn;
184 }
185 
187  if (filename_m == std::string(""))
188  return getName();
189  else
190  return filename_m.substr(0, filename_m.rfind("."));
191 }
192 
193 void Degrader::getDimensions(double &zBegin, double &zEnd) const {
194  zBegin = 0.0;
195  zEnd = getElementLength();
196 
197 }
198 
200  return DEGRADER;
201 }
202 
204  return "DEGRADER";
205 
206 }
virtual bool computeEnergyLoss(Vector_t &P, const double deltat, bool includeFluctuations=true) const =0
virtual ParticleMatterInteractionHandler * getParticleMatterInteraction() const
Definition: ElementBase.h:579
ParticleAttrib< Vector_t > P
virtual bool isInMaterial(double z)
Definition: Degrader.cpp:82
std::vector< double > PosZ_m
Definition: Degrader.h:103
virtual ElementBase::ElementType getType() const
Get element type std::string.
Definition: Degrader.cpp:199
std::vector< double > MomentumY_m
Definition: Degrader.h:105
virtual void visitDegrader(const Degrader &)=0
Apply the algorithm to a diagnostic.
virtual bool bends() const
Definition: Degrader.cpp:178
std::vector< double > PosY_m
Definition: Degrader.h:102
std::vector< double > MomentumZ_m
Definition: Degrader.h:106
Degrader()
Definition: Degrader.cpp:39
double getdT() const
Inform * gmsg
Definition: Main.cpp:21
double getM() const
bool online_m
Definition: Component.h:201
virtual void getDimensions(double &zBegin, double &zEnd) const
Definition: Degrader.cpp:193
virtual const std::string & getName() const
Get element name.
Definition: ElementBase.cpp:95
std::vector< double > MomentumX_m
Definition: Degrader.h:104
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition: Vector3D.cpp:118
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B)
Definition: Degrader.cpp:90
virtual void accept(BeamlineVisitor &) const
Apply visitor to Degrader.
Definition: Degrader.cpp:77
ParticleIndex_t & ID
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:511
std::vector< int > id_m
Definition: Degrader.h:108
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
std::string filename_m
Definition: Degrader.h:99
std::vector< double > PosX_m
Definition: Degrader.h:101
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField)
Definition: Degrader.cpp:133
PartBunchBase< double, 3 > * RefPartBunch_m
Definition: Component.h:200
virtual bool applyToReferenceParticle(const Vector_t &R, const Vector_t &P, const double &t, Vector_t &E, Vector_t &B)
Definition: Degrader.cpp:118
virtual void goOnline(const double &kineticEnergy)
Definition: Degrader.cpp:157
size_t getLocalNum() const
std::unique_ptr< LossDataSink > lossDs_m
Definition: Degrader.h:110
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
virtual void finalise()
Definition: Degrader.cpp:152
ParticleAttrib< double > dt
std::vector< double > time_m
Definition: Degrader.h:107
virtual void goOffline()
Definition: Degrader.cpp:171
bool asciidump
Definition: Options.cpp:18
std::string getOutputFN()
Definition: Degrader.cpp:186
Abstract collimator.
Definition: Degrader.h:37
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Definition: Vector.h:243
double getQ() const
Access to reference data.
const std::string name
std::string getDegraderShape()
Definition: Degrader.cpp:203
ParticleAttrib< int > Bin
ParticlePos_t & R
Interface for a single beam element.
Definition: Component.h:51
Abstract algorithm.
Definition: Inform.h:41
void setOutputFN(std::string fn)
Definition: Degrader.cpp:182
virtual ~Degrader()
Definition: Degrader.cpp:70
Inform & endl(Inform &inf)
Definition: Inform.cpp:42