OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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  if (isTransparent_m) {
60  return false;
61  }
62 
63  const Vector_t &R = RefPartBunch_m->R[i];
64  const Vector_t &P = RefPartBunch_m->P[i];
65  const double &dt = RefPartBunch_m->dt[i];
66  const double recpgamma = Physics::c * dt / Util::getGamma(P);
67  if (online_m && R(2) <= 0.0 && P(2) < 0.0) {
68  double frac = -R(2) / (P(2) * recpgamma);
69 
70  lossDs_m->addParticle(OpalParticle(RefPartBunch_m->ID[i],
71  R + frac * recpgamma * P, P,
72  t + frac * dt,
73  RefPartBunch_m->Q[i], RefPartBunch_m->M[i]));
74 
75  return true;
76  }
77 
78  return false;
79 }
80 
81 void Source::initialise(PartBunchBase<double, 3> *bunch, double &startField, double &endField) {
82  RefPartBunch_m = bunch;
83  endField = startField;
84  startField -= getElementLength();
85 
86  lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getOutputFN(),
88 }
89 
91 {}
92 
93 bool Source::bends() const {
94  return false;
95 }
96 
97 void Source::goOnline(const double &) {
98  online_m = true;
99 }
100 
102  online_m = false;
103  lossDs_m->save();
104 }
105 
106 void Source::getDimensions(double &zBegin, double &zEnd) const {
107  zBegin = startField_m;
108  zEnd = endField_m;
109 }
110 
112  return SOURCE;
113 }
114 
116  isTransparent_m = true;
117 }
const std::string name
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:51
bool asciidump
Definition: Options.cpp:85
double getGamma(Vector_t p)
Definition: Util.h:27
ParticlePos_t & R
ParticleAttrib< double > M
ParticleAttrib< Vector_t > P
ParticleAttrib< double > Q
ParticleAttrib< double > dt
ParticleIndex_t & ID
virtual void visitSource(const Source &)=0
Apply the algorithm to a source.
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 double getElementLength() const
Get design length.
Definition: ElementBase.h:432
std::string getOutputFN() const
Get output filename.
Definition: Source.h:30
bool isTransparent_m
Definition: Source.h:70
virtual void getDimensions(double &zBegin, double &zEnd) const override
Definition: Source.cpp:106
virtual ElementBase::ElementType getType() const override
Get element type std::string.
Definition: Source.cpp:111
virtual void goOffline() override
Definition: Source.cpp:101
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Source.
Definition: Source.cpp:54
double startField_m
Definition: Source.h:67
Source()
Definition: Source.cpp:33
virtual void goOnline(const double &kineticEnergy) override
Definition: Source.cpp:97
double endField_m
Definition: Source.h:68
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
Definition: Source.cpp:81
virtual bool bends() const override
Definition: Source.cpp:93
void setTransparent()
Definition: Source.cpp:115
virtual ~Source()
Definition: Source.cpp:51
virtual void finalise() override
Definition: Source.cpp:90
std::unique_ptr< LossDataSink > lossDs_m
Definition: Source.h:72
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) override
Definition: Source.cpp:58