OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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//
19
22#include "Physics/Physics.h"
24#include "Utilities/Options.h"
25
26#include <cmath>
27#include <memory>
28
29extern Inform* gmsg;
30
31
35
37 Component(right),
38 PosX_m(right.PosX_m),
39 PosY_m(right.PosY_m),
40 PosZ_m(right.PosZ_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
50Degrader::Degrader(const std::string& name):
52 width_m(0.0),
53 height_m(0.0)
54{ }
55
60
61void Degrader::accept(BeamlineVisitor& visitor) const {
62 visitor.visitDegrader(*this);
63}
64
65void Degrader::setDimensions(double xsize, double ysize) {
66 width_m = xsize;
67 height_m = ysize;
68}
69
70
71bool 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
83bool 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;
120 bool isDead = getParticleMatterInteraction()->computeEnergyLoss(RefPartBunch_m, updatedP, RefPartBunch_m->getdT(), false);
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
127void Degrader::initialise(PartBunchBase<double, 3>* bunch, double& startField, double& endField) {
128 initialise(bunch);
129 endField = startField + getElementLength();
130}
131
135
137 *gmsg << "* Finalize degrader " << getName() << endl;
138}
139
140void 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
162bool Degrader::bends() const {
163 return false;
164}
165
166void Degrader::getDimensions(double& zBegin, double& zEnd) const {
167 zBegin = 0.0;
168 zEnd = getElementLength();
169}
170
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition Vector3D.cpp:118
ElementType
Definition ElementBase.h:88
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Definition Vector.h:243
Inform * gmsg
Definition Main.cpp:70
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:45
virtual void visitDegrader(const Degrader &)=0
Apply the algorithm to a degrader.
bool online_m
Definition Component.h:192
Component(const std::string &name)
Constructor with given name.
Definition Component.cpp:53
PartBunchBase< double, 3 > * RefPartBunch_m
Definition Component.h:191
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Degrader.
Definition Degrader.cpp:61
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 > MomentumZ_m
Definition Degrader.h:90
double height_m
Definition Degrader.h:95
std::vector< double > time_m
Definition Degrader.h:91
void setDimensions(double xsize, double ysize)
Definition Degrader.cpp:65
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) override
Definition Degrader.cpp:83
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
Definition Degrader.cpp:127
virtual void finalise() override
Definition Degrader.cpp:136
std::vector< double > MomentumY_m
Definition Degrader.h:89
virtual ElementType getType() const override
Get element type std::string.
Definition Degrader.cpp:171
Degrader(const std::string &name)
Constructor with given name.
Definition Degrader.cpp:50
double width_m
Definition Degrader.h:94
std::vector< double > PosY_m
Definition Degrader.h:86
std::vector< double > PosX_m
Definition Degrader.h:85
virtual ~Degrader()
Definition Degrader.cpp:56
virtual void goOnline(const double &kineticEnergy) override
Definition Degrader.cpp:140
std::vector< double > PosZ_m
Definition Degrader.h:87
virtual bool bends() const override
Definition Degrader.cpp:162
virtual void getDimensions(double &zBegin, double &zEnd) const override
Definition Degrader.cpp:166
std::vector< int > id_m
Definition Degrader.h:92
virtual void goOffline() override
Definition Degrader.cpp:156
std::vector< double > MomentumX_m
Definition Degrader.h:88
virtual bool isInside(const Vector_t &R) const override
Definition Degrader.cpp:71
virtual const std::string & getName() const
Get element name.
virtual ParticleMatterInteractionHandler * getParticleMatterInteraction() const
virtual double getElementLength() const
Get design length.
virtual bool computeEnergyLoss(PartBunchBase< double, 3 > *bunch, Vector_t &P, const double deltat, bool includeFluctuations=true) const =0
Vektor< double, 3 > Vector_t