OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
PluginElement.cpp
Go to the documentation of this file.
1//
2// Class PluginElement
3// Abstract Interface for (Cyclotron) Plugin Elements (CCollimator, Probe, Stripper, Septum)
4// Implementation via Non-Virtual Interface Template Method
5//
6// Copyright (c) 2018-2020, Paul Scherrer Institut, Villigen PSI, Switzerland
7// All rights reserved
8//
9// This file is part of OPAL.
10//
11// OPAL is free software: you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation, either version 3 of the License, or
14// (at your option) any later version.
15//
16// You should have received a copy of the GNU General Public License
17// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
18//
20
23#include "Physics/Physics.h"
24#include "Physics/Units.h"
26#include "Utilities/Options.h"
27#include "Utilities/Util.h"
28
29
32
35 setDimensions(0.0, 0.0, 0.0, 0.0);
36}
37
39 Component(right) {
40 setDimensions(right.xstart_m, right.xend_m, right.ystart_m, right.yend_m);
41}
44 if (online_m)
45 goOffline();
46}
47
48void PluginElement::initialise(PartBunchBase<double, 3> *bunch, double &, double &) {
49 initialise(bunch);
50}
51
53 RefPartBunch_m = bunch;
54 lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getOutputFN(), !Options::asciidump));
55 // virtual hook
56 doInitialise(bunch);
57 goOnline(-1e6);
58}
59
61 doFinalise();
62 if (online_m)
64}
65
67 // After per-turn saves (PSDUMPEACHTURN) the file holds the earlier turns: append the
68 // records since the last one, as save() does, instead of starting the file anew.
69 if (online_m && lossDs_m)
72 lossDs_m.reset(nullptr);
74 online_m = false;
75}
76
78 return false;
79}
80
81bool PluginElement::apply(const size_t &/*i*/, const double &, Vector_t &, Vector_t &) {
82 return false;
83}
84
85bool PluginElement::applyToReferenceParticle(const Vector_t &, const Vector_t &, const double &, Vector_t &, Vector_t &) {
86 return false;
87}
88
89void PluginElement::setDimensions(double xstart, double xend, double ystart, double yend) {
90 xstart_m = xstart;
91 ystart_m = ystart;
92 xend_m = xend;
93 yend_m = yend;
94 rstart_m = std::hypot(xstart, ystart);
95 rend_m = std::hypot(xend, yend);
96 // start position is the one with lowest radius
97 if (rstart_m > rend_m) {
98 std::swap(xstart_m, xend_m);
99 std::swap(ystart_m, yend_m);
100 std::swap(rstart_m, rend_m);
101 }
102 A_m = yend_m - ystart_m;
103 B_m = xstart_m - xend_m;
104 R_m = std::sqrt(A_m*A_m+B_m*B_m);
106
107 // element equation: A*X + B*Y + C = 0
108 // point closest to origin https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
109 double x_close = 0.0;
110 if (R_m > 0.0)
111 x_close = - A_m * C_m / (R_m * R_m);
112
113 if (x_close > std::min(xstart_m, xend_m) && x_close < std::max(xstart_m, xend_m) )
114 rmin_m = std::abs(C_m) / std::hypot(A_m,B_m);
115 else
117}
118
119void PluginElement::setGeom(const double dist) {
120
121 double slope;
122 if (xend_m == xstart_m) {
123 slope = 1.0e12;
124 } else {
125 slope = (yend_m - ystart_m) / (xend_m - xstart_m);
126 }
127 double coeff2 = std::sqrt(1 + slope * slope);
128 double coeff1 = slope / coeff2;
129 double halfdist = dist / 2.0;
130 geom_m[0].x = xstart_m - halfdist * coeff1;
131 geom_m[0].y = ystart_m + halfdist / coeff2;
132
133 geom_m[1].x = xstart_m + halfdist * coeff1;
134 geom_m[1].y = ystart_m - halfdist / coeff2;
135
136 geom_m[2].x = xend_m + halfdist * coeff1;
137 geom_m[2].y = yend_m - halfdist / coeff2;
138
139 geom_m[3].x = xend_m - halfdist * coeff1;
140 geom_m[3].y = yend_m + halfdist / coeff2;
141
142 geom_m[4].x = geom_m[0].x;
143 geom_m[4].y = geom_m[0].y;
144
145 doSetGeom();
146}
147
148void PluginElement::changeWidth(PartBunchBase<double, 3> *bunch, int i, const double tstep, const double tangle) {
149 double lstep = euclidean_norm(bunch->P[i]) / Util::getGamma(bunch->P[i]) * Physics::c * tstep;
150 double sWidth = lstep / std::sqrt( 1 + 1/tangle/tangle );
151 setGeom(sWidth);
152}
153
154double PluginElement::calculateIncidentAngle(double xp, double yp) const {
155 double k1, k2, tangle = 0.0;
156 if ( B_m == 0.0 && xp == 0.0) {
157 // width is 0.0, keep non-zero
158 tangle = 0.1;
159 } else if ( B_m == 0.0 ){
160 k1 = yp / xp;
161 if (k1 == 0.0)
162 tangle = 1.0e12;
163 else
164 tangle = std::abs(1 / k1);
165 } else if ( xp == 0.0 ) {
166 k2 = - A_m / B_m;
167 if ( k2 == 0.0 )
168 tangle = 1.0e12;
169 else
170 tangle = std::abs(1 / k2);
171 } else {
172 k1 = yp / xp;
173 k2 = - A_m / B_m;
174 tangle = std::abs(( k1-k2 ) / (1 + k1*k2));
175 }
176 return tangle;
177}
178
180 return xstart_m;
181}
182
184 return xend_m;
185}
186
188 return ystart_m;
189}
190
192 return yend_m;
193}
194
195bool PluginElement::check(PartBunchBase<double, 3> *bunch, const int turnnumber, const double t, const double tstep) {
196 bool flag = false;
197 // check if bunch close
198 bool bunchClose = preCheck(bunch);
199
200 if (bunchClose == true) {
201 flag = doCheck(bunch, turnnumber, t, tstep); // virtual hook
202 }
203 // finalise, can have reduce
204 flag = finaliseCheck(bunch, flag);
205
206 return flag;
207}
208
209void PluginElement::getDimensions(double &zBegin, double &zEnd) const {
210 zBegin = -0.005;
211 zEnd = 0.005;
212}
213
214int PluginElement::checkPoint(const double &x, const double &y) const {
215 int cn = 0;
216 for (int i = 0; i < 4; i++) {
217 if (( (geom_m[i].y <= y) && (geom_m[i+1].y > y))
218 || ((geom_m[i].y > y) && (geom_m[i+1].y <= y))) {
219
220 float vt = (float)(y - geom_m[i].y) / (geom_m[i+1].y - geom_m[i].y);
221 if(x < geom_m[i].x + vt * (geom_m[i+1].x - geom_m[i].x))
222 ++cn;
223 }
224 }
225 return (cn & 1); // 0 if even (out), and 1 if odd (in)
226}
227
229 OpalData::OpenMode openMode;
230 if (numPassages_m > 0) {
232 } else {
233 openMode = OpalData::getInstance()->getOpenMode();
234 }
235 // Count only saves that wrote the file: after per-turn saves without any records the
236 // next one must still use the run's open mode (WRITE truncates a file of an earlier run).
237 if (lossDs_m->save(1, openMode))
239}
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Definition Vector.h:243
const std::string name
constexpr double c
The velocity of light in m/s.
Definition Physics.h:45
bool asciidump
Definition Options.cpp:83
double getGamma(Vector_t p)
Definition Util.h:49
ParticleAttrib< Vector_t > P
OpenMode getOpenMode() const
Definition OpalData.cpp:353
static OpalData * getInstance()
Definition OpalData.cpp:196
OpenMode
Enum for writing to files.
Definition OpalData.h:64
double x
Definition Component.h:38
double y
Definition Component.h:39
Interface for a single beam element.
Definition Component.h:50
bool online_m
Definition Component.h:192
virtual void goOnline(const double &kineticEnergy)
Definition Component.cpp:83
PartBunchBase< double, 3 > * RefPartBunch_m
Definition Component.h:191
std::string getOutputFN() const
Get output filename.
virtual void doInitialise(PartBunchBase< double, 3 > *)
Pure virtual hook for initialise.
int checkPoint(const double &x, const double &y) const
Checks if coordinate is within element.
void setGeom(const double dist)
Sets geometry geom_m with element width dist.
virtual void getDimensions(double &zBegin, double &zEnd) const override
virtual bool bends() const override
double C_m
Geometric lengths used in calculations.
double getYStart() const
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
Pure virtual implementation of Component.
virtual void finalise() final
virtual void doSetGeom()
Virtual hook for setGeom.
virtual void doGoOffline()
Virtual hook for goOffline.
void changeWidth(PartBunchBase< double, 3 > *bunch, int i, const double tstep, const double tangle)
Change probe width depending on step size and angle of particle.
int numPassages_m
Number of save() calls in this run that wrote the file.
Point geom_m[5]
actual geometry positions with adaptive width such that each particle hits element once per turn
bool check(PartBunchBase< double, 3 > *bunch, const int turnnumber, const double t, const double tstep)
virtual void doFinalise()
Virtual hook for finalise.
virtual void goOffline() final
double getXEnd() const
double calculateIncidentAngle(double xp, double yp) const
Calculate angle of particle/bunch wrt to element.
double xstart_m
input geometry positions
void setDimensions(double xstart, double xend, double ystart, double yend)
Set dimensions and consistency checks.
std::unique_ptr< LossDataSink > lossDs_m
Pointer to Loss instance.
double getXStart() const
Member variable access.
bool finaliseCheck(PartBunchBase< double, 3 > *bunch, bool flagNeedUpdate)
Finalise call after check.
bool preCheck(PartBunchBase< double, 3 > *bunch)
Check if bunch is close to element.
double getYEnd() const
virtual bool applyToReferenceParticle(const Vector_t &R, const Vector_t &P, const double &t, Vector_t &E, Vector_t &B) override
double rmin_m
radius closest to the origin
virtual bool doCheck(PartBunchBase< double, 3 > *bunch, const int turnnumber, const double t, const double tstep)=0
Pure virtual hook for check.
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) override
virtual ~PluginElement()
void save()
Save output.