OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
Probe.cpp
Go to the documentation of this file.
1//
2// Class Probe
3// Interface for a probe
4//
5// Copyright (c) 2016-2020, 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/Probe.h"
19
22#include "Physics/Physics.h"
23#include "Physics/Units.h"
26
27#include <algorithm>
28#include <cmath>
29#include <cstddef>
30#include <utility>
31
32extern Inform *gmsg;
33
36
37Probe::Probe(const std::string &name):
39 step_m(0.0)
40{}
41
42Probe::Probe(const Probe &right):
43 PluginElement(right),
44 step_m(right.step_m)
45{}
46
48
49void Probe::accept(BeamlineVisitor &visitor) const {
50 visitor.visitProbe(*this);
51}
52
54 bool singlemode = (bunch->getTotalNum() == 1) ? true : false;
55 // PeakFinder works in millimetre (doCheck() passes it the radii in mm), the element in metre
56 peakfinder_m = std::unique_ptr<PeakFinder> (new PeakFinder(getOutputFN(),
60 singlemode));
61}
62
64 *gmsg << "* Probe " << getName() << " goes offline" << endl;
66 peakfinder_m->save();
67 peakfinder_m.reset(nullptr);
68}
69
70void Probe::setStep(double step) {
71 step_m = step;
72}
73
74double Probe::getStep() const {
75 return step_m;
76}
77
79 Vector_t rmin, rmax;
80 bunch->get_bounds(rmin, rmax);
81 // Absolute minimum and maximum of |x| and |y| over the bunch's bounding box.
82 // When an interval straddles zero the smallest attainable |coordinate| is 0,
83 // not min(|lo|,|hi|); otherwise rbunch_min is far too large and the probe can
84 // silently miss particles. Same defect as CCollimator::doPreCheck.
85 double xmin = (rmin(0) <= 0.0 && rmax(0) >= 0.0)
86 ? 0.0 : std::min(std::abs(rmin(0)), std::abs(rmax(0)));
87 double xmax = std::max(std::abs(rmin(0)), std::abs(rmax(0)));
88 double ymin = (rmin(1) <= 0.0 && rmax(1) >= 0.0)
89 ? 0.0 : std::min(std::abs(rmin(1)), std::abs(rmax(1)));
90 double ymax = std::max(std::abs(rmin(1)), std::abs(rmax(1)));
91 double rbunch_min = std::hypot(xmin, ymin);
92 double rbunch_max = std::hypot(xmax, ymax);
93
94 if (rbunch_max > rmin_m - 0.01 && rbunch_min < rend_m + 0.01 ) {
95 return true;
96 }
97 return false;
98}
99
100bool Probe::doCheck(PartBunchBase<double, 3> *bunch, const int turnnumber, const double t, const double tstep) {
101 Vector_t probepoint;
102 size_t tempnum = bunch->getLocalNum();
103
104 // checkPoint() tests a strip around the probe line that is at most half a step wide on
105 // either side, and half a step is beta*c*tstep/2 < c*tstep/2. Testing the distance to the
106 // line first skips the per-particle strip for all but a thin slice of the bunch; the
107 // margin covers the single-precision arithmetic in checkPoint(), so exactly the same
108 // particles are recorded.
109 const double reach = 0.5 * Physics::c * std::abs(tstep) + 1.0e-3; // [m]
110
111 for (unsigned int i = 0; i < tempnum; ++i) {
112 double dist1 = (A_m * bunch->R[i](0) + B_m * bunch->R[i](1) + C_m) / R_m; // [m]
113 if (std::abs(dist1) > reach) continue;
114 // flagged lost (Bin < 0) but not deleted yet: the tracker deletes every DELPARTFREQ steps
115 if (bunch->Bin[i] < 0) continue;
116 double tangle = calculateIncidentAngle(bunch->P[i](0), bunch->P[i](1));
117 changeWidth(bunch, i, tstep, tangle);
118 int pflag = checkPoint(bunch->R[i](0), bunch->R[i](1));
119 if (pflag == 0) continue;
120 // calculate closest point at probe -> better to use momentum direction
121 // probe: y = -A/B * x - C/B or A*X + B*Y + C = 0
122 // perpendicular line through R: y = B/A * x + R(1) - B/A * R(0)
123 // probepoint(0) = (B_m*B_m*bunch->R[i](0) - A_m*B_m*bunch->R[i](1) - A_m*C_m) / (R_m*R_m);
124 // probepoint(1) = (A_m*A_m*bunch->R[i](1) - A_m*B_m*bunch->R[i](0) - B_m*C_m) / (R_m*R_m);
125 // probepoint(2) = bunch->R[i](2);
126 // calculate time correction for probepoint
127 // dist1 > 0, right hand, dt > 0; dist1 < 0, left hand, dt < 0
128 double dist2 = dist1 * std::sqrt(1.0 + 1.0 / tangle / tangle);
129 double dt = dist2 / (std::sqrt(1.0 - 1.0 / (1.0 + dot(bunch->P[i], bunch->P[i]))) * Physics::c);
130
131 probepoint = bunch->R[i] + dist2 * bunch->P[i] / euclidean_norm(bunch->P[i]);
132
133 // peak finder uses millimetre not metre
134 peakfinder_m->addParticle(probepoint * Units::m2mm);
135
136 // OpalParticle takes the rest mass in MeV. M[i] is the macro-particle mass in
137 // GeV, except for particles made by a Stripper (STOP=FALSE) or by beam
138 // stripping, whose M[i] is their own mass in GeV.
139 const double mass = (bunch->POrigin[i] == ParticleOrigin::REGULAR) ?
140 bunch->getM() * Units::eV2MeV : bunch->M[i] * Units::GeV2MeV;
141 lossDs_m->addParticle(OpalParticle(bunch->ID[i], probepoint, bunch->P[i],
142 t+dt, bunch->Q[i], mass),
143 std::make_pair(turnnumber, bunch->bunchNum[i]));
144 }
145
146 peakfinder_m->evaluate(turnnumber);
147
148 // we do not lose particles in the probe
149 return false;
150}
151
Inform * gmsg
Definition Main.cpp:70
ElementType
Definition ElementBase.h:89
Inform * gmsg
Definition Main.cpp:70
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Definition Vector.h:243
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition Vector3D.cpp:118
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
constexpr double GeV2MeV
Definition Units.h:80
constexpr double m2mm
Definition Units.h:26
constexpr double eV2MeV
Definition Units.h:77
ParticlePos_t & R
ParticleAttrib< int > Bin
void get_bounds(Vector_t &rmin, Vector_t &rmax) const
size_t getLocalNum() const
ParticleAttrib< double > M
size_t getTotalNum() const
ParticleAttrib< Vector_t > P
ParticleAttrib< ParticleOrigin > POrigin
ParticleAttrib< double > Q
ParticleAttrib< short > bunchNum
ParticleIndex_t & ID
double getM() const
virtual void visitProbe(const Probe &)=0
Apply the algorithm to a probe.
bool online_m
Definition Component.h:192
virtual const std::string & getName() const
Get element name.
std::string getOutputFN() const
Get output filename.
int checkPoint(const double &x, const double &y) const
Checks if coordinate is within element.
double C_m
Geometric lengths used in calculations.
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.
double calculateIncidentAngle(double xp, double yp) const
Calculate angle of particle/bunch wrt to element.
std::unique_ptr< LossDataSink > lossDs_m
Pointer to Loss instance.
double rmin_m
radius closest to the origin
Definition Probe.h:31
void setStep(double step)
Set probe histogram bin width.
Definition Probe.cpp:70
virtual ~Probe()
Definition Probe.cpp:47
std::unique_ptr< PeakFinder > peakfinder_m
Pointer to Peakfinder instance.
Definition Probe.h:63
virtual bool doCheck(PartBunchBase< double, 3 > *bunch, const int turnnumber, const double t, const double tstep) override
Record probe hits when bunch particles pass.
Definition Probe.cpp:100
virtual void doInitialise(PartBunchBase< double, 3 > *bunch) override
Initialise peakfinder file.
Definition Probe.cpp:53
double step_m
Step size of the probe (bin width in histogram file)
Definition Probe.h:62
Probe()
Definition Probe.cpp:34
virtual void doGoOffline() override
Hook for goOffline.
Definition Probe.cpp:63
virtual ElementType getType() const override
Get element type std::string.
Definition Probe.cpp:152
virtual bool doPreCheck(PartBunchBase< double, 3 > *) override
Virtual hook for preCheck.
Definition Probe.cpp:78
virtual double getStep() const
Member variable access.
Definition Probe.cpp:74
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Probe.
Definition Probe.cpp:49