OPAL (Object Oriented Parallel Accelerator Library) 2022.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
23#include "Fields/Fieldmap.h"
24#include "Physics/Physics.h"
26#include "Utilities/Options.h"
27#include "Utilities/Util.h"
28
29#include <iostream>
30#include <fstream>
31
32
34 Source("")
35{}
36
37Source::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
44Source::Source(const std::string& name):
46 startField_m(0.0),
47 endField_m(0.0),
48 isTransparent_m(false)
49{}
50
52}
53
54void Source::accept(BeamlineVisitor& visitor) const {
55 visitor.visitSource(*this);
56}
57
58bool 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,
76
77 return true;
78 }
79
80 return false;
81}
82
83void 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
95bool Source::bends() const {
96 return false;
97}
98
99void Source::goOnline(const double&) {
100 online_m = true;
101}
102
104 online_m = false;
105 lossDs_m->save();
106}
107
108void 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}
ElementType
Definition: ElementBase.h:88
const std::string name
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:45
bool asciidump
Definition: Options.cpp:85
Vector_t getBeta(Vector_t p)
Definition: Util.h:50
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:192
PartBunchBase< double, 3 > * RefPartBunch_m
Definition: Component.h:191
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:414
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:108
virtual void goOffline() override
Definition: Source.cpp:103
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 ElementType getType() const override
Get element type std::string.
Definition: Source.cpp:113
virtual void goOnline(const double &kineticEnergy) override
Definition: Source.cpp:99
double endField_m
Definition: Source.h:68
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
Definition: Source.cpp:83
virtual bool bends() const override
Definition: Source.cpp:95
void setTransparent()
Definition: Source.cpp:117
virtual ~Source()
Definition: Source.cpp:51
virtual void finalise() override
Definition: Source.cpp:92
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