OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
Source.cpp
Go to the documentation of this file.
1 //
2 // Class Source
3 // Defines the abstract interface for a source.
4 //
5 // Copyright (c) 200x - 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/Source.h"
19 
22 #include "Elements/OpalBeamline.h"
23 #include "Fields/Fieldmap.h"
24 #include "Physics/Physics.h"
25 #include "Structure/LossDataSink.h"
26 #include "Utilities/Options.h"
27 #include "Utilities/Util.h"
28 
29 #include <iostream>
30 #include <fstream>
31 
32 
34  Source("")
35 {}
36 
37 Source::Source(const Source& right):
38  Component(right),
39  startField_m(right.startField_m),
40  endField_m(right.endField_m),
41  isTransparent_m(right.isTransparent_m)
42 {}
43 
44 Source::Source(const std::string& name):
45  Component(name),
46  startField_m(0.0),
47  endField_m(0.0),
48  isTransparent_m(false)
49 {}
50 
52 }
53 
54 void Source::accept(BeamlineVisitor& visitor) const {
55  visitor.visitSource(*this);
56 }
57 
58 bool Source::apply(const size_t& i, const double& t, Vector_t& /*E*/, Vector_t& /*B*/) {
59 
60  if (isTransparent_m) {
61  return false;
62  }
63 
64  const Vector_t& R = RefPartBunch_m->R[i];
65  const Vector_t& P = RefPartBunch_m->P[i];
66 
67  if (online_m && R(2) <= 0.0 && P(2) < 0.0) {
68  const double& dt = RefPartBunch_m->dt[i];
69  const Vector_t singleStep = Physics::c * dt * Util::getBeta(P);
70  double frac = -R(2) / singleStep(2);
71 
72  lossDs_m->addParticle(OpalParticle(RefPartBunch_m->ID[i],
73  R + frac * singleStep, P,
74  t + frac * dt,
75  RefPartBunch_m->Q[i], RefPartBunch_m->M[i]));
76 
77  return true;
78  }
79 
80  return false;
81 }
82 
83 void Source::initialise(PartBunchBase<double, 3>* bunch, double& startField, double& endField) {
84  RefPartBunch_m = bunch;
85  endField = startField;
86  startField -= getElementLength();
87 
88  lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getOutputFN(),
90 }
91 
93 {}
94 
95 bool Source::bends() const {
96  return false;
97 }
98 
99 void Source::goOnline(const double&) {
100  online_m = true;
101 }
102 
104  online_m = false;
105  lossDs_m->save();
106 }
107 
108 void Source::getDimensions(double& zBegin, double& zEnd) const {
109  zBegin = startField_m;
110  zEnd = endField_m;
111 }
112 
114  return ElementType::SOURCE;
115 }
116 
118  isTransparent_m = true;
119 }
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
Definition: Source.cpp:83
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:45
double endField_m
Definition: Source.h:68
virtual void finalise() override
Definition: Source.cpp:92
ParticleAttrib< Vector_t > P
Source()
Definition: Source.cpp:33
bool asciidump
Definition: Options.cpp:85
ParticleAttrib< double > M
PartBunchBase< double, 3 > * RefPartBunch_m
Definition: Component.h:191
std::unique_ptr< LossDataSink > lossDs_m
Definition: Source.h:72
virtual ~Source()
Definition: Source.cpp:51
virtual void goOnline(const double &kineticEnergy) override
Definition: Source.cpp:99
virtual ElementType getType() const override
Get element type std::string.
Definition: Source.cpp:113
virtual void goOffline() override
Definition: Source.cpp:103
virtual void visitSource(const Source &)=0
Apply the algorithm to a source.
bool online_m
Definition: Component.h:192
Vector_t getBeta(Vector_t p)
Definition: Util.h:51
ElementType
Definition: ElementBase.h:88
ParticleAttrib< double > Q
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:415
ParticleAttrib< double > dt
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Source.
Definition: Source.cpp:54
Definition: Source.h:30
const std::string name
virtual bool bends() const override
Definition: Source.cpp:95
ParticlePos_t & R
ParticleIndex_t & ID
void setTransparent()
Definition: Source.cpp:117
double startField_m
Definition: Source.h:67
virtual void getDimensions(double &zBegin, double &zEnd) const override
Definition: Source.cpp:108
Interface for a single beam element.
Definition: Component.h:50
std::string getOutputFN() const
Get output filename.
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) override
Definition: Source.cpp:58
bool isTransparent_m
Definition: Source.h:70