OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
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"
20#include "PartBunch/PartBunch.h"
21#include "Physics/Physics.h"
22#include "Physics/Units.h"
25
26extern Inform* gmsg;
27
29}
30
31Probe::Probe(const std::string& name) : PluginElement(name), step_m(0.0) {
32}
33
34Probe::Probe(const Probe& right) : PluginElement(right), step_m(right.step_m) {
35}
36
39
40void Probe::accept(BeamlineVisitor& visitor) const {
41 visitor.visitProbe(*this);
42}
43
45 bool singlemode = (bunch->getTotalNum() == 1) ? true : false;
46 peakfinder_m = std::unique_ptr<PeakFinder>(
47 new PeakFinder(getOutputFN(), rmin_m, rend_m, step_m, singlemode));
48}
49
51 *gmsg << "* Probe " << getName() << " goes offline" << endl;
53 peakfinder_m->save();
54 peakfinder_m.reset(nullptr);
55}
56
57void Probe::setStep(double step) {
58 step_m = step;
59}
60
61double Probe::getStep() const {
62 return step_m;
63}
64
66 Vector_t<double, 3> rmin, rmax;
67 bunch->get_bounds(rmin, rmax);
68 // interested in absolute minimum and maximum
69 double xmin = std::min(std::abs(rmin(0)), std::abs(rmax(0)));
70 double xmax = std::max(std::abs(rmin(0)), std::abs(rmax(0)));
71 double ymin = std::min(std::abs(rmin(1)), std::abs(rmax(1)));
72 double ymax = std::max(std::abs(rmin(1)), std::abs(rmax(1)));
73 double rbunch_min = std::hypot(xmin, ymin);
74 double rbunch_max = std::hypot(xmax, ymax);
75
76 if (rbunch_max > rmin_m - 0.01 && rbunch_min < rend_m + 0.01) {
77 return true;
78 }
79 return false;
80}
81
82bool Probe::doCheck(PartBunch_t* bunch, const int turnnumber, const double t, const double tstep) {
83 Vector_t<double, 3> probepoint;
84 size_t tempnum = bunch->getLocalNum();
85
86 for (unsigned int i = 0; i < tempnum; ++i) {
87 double tangle = calculateIncidentAngle(bunch->P(i)(0), bunch->P(i)(1));
88 changeWidth(bunch, i, tstep, tangle);
89 int pflag = checkPoint(bunch->R(i)(0), bunch->R(i)(1));
90 if (pflag == 0)
91 continue;
92 // calculate closest point at probe -> better to use momentum direction
93 // probe: y = -A/B * x - C/B or A*X + B*Y + C = 0
94 // perpendicular line through R: y = B/A * x + R(1) - B/A * R(0)
95 // 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);
96 // 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);
97 // probepoint(2) = bunch->R(i)(2);
98 // calculate time correction for probepoint
99 // dist1 > 0, right hand, dt > 0; dist1 < 0, left hand, dt < 0
100 double dist1 = (A_m * bunch->R(i)(0) + B_m * bunch->R(i)(1) + C_m) / R_m; // [m]
101 double dist2 = dist1 * std::sqrt(1.0 + 1.0 / tangle / tangle);
102 //double dt =
103 // dist2 / (std::sqrt(1.0 - 1.0 / (1.0 + dot(bunch->P(i), bunch->P(i)))) * Physics::c);
104
105 // ADA probepoint = bunch->R(i) + dist2 * bunch->P(i) / euclidean_norm(bunch->P(i));
106 probepoint = bunch->R(i) + dist2 * bunch->P(i) / std::sqrt(dot(bunch->P(i), bunch->P(i)));
107
108 // peak finder uses millimetre not metre
109 peakfinder_m->addParticle(probepoint * Units::m2mm);
110
111 // ADA lossDs_m->addParticle(
112 // OpalParticle(bunch->ID(i), probepoint, bunch->P(i), t + dt, bunch->Q(i), bunch->M(i)),
113 // std::make_pair(turnnumber, (int)
114 //
115 // 3bunch->bunchNum(i)));
116 }
117
118 peakfinder_m->evaluate(turnnumber);
119
120 // we do not lose particles in the probe
121 return false;
122}
123
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition Vector3D.cpp:118
Inform * gmsg
Definition changes.cpp:7
ElementType
Definition ElementBase.h:88
PartBunch< PLayout_t< double, 3 >, double, 3 > PartBunch_t
ippl::Vector< T, Dim > Vector_t
constexpr double m2mm
Definition Units.h:26
virtual void visitProbe(const Probe &)=0
Apply the algorithm to a Probe.
bool online_m
Definition Component.h:186
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(PartBunch_t *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.
double rmin_m
radius closest to the origin
PluginElement(const std::string &name)
Constructor with given name.
void setStep(double step)
Set probe histogram bin width.
Definition Probe.cpp:57
virtual ~Probe()
Definition Probe.cpp:37
std::unique_ptr< PeakFinder > peakfinder_m
Pointer to Peakfinder instance.
Definition Probe.h:60
Probe(const std::string &name)
Constructor with given name.
Definition Probe.cpp:31
virtual void doInitialise(PartBunch_t *bunch) override
Initialise peakfinder file.
Definition Probe.cpp:44
virtual bool doCheck(PartBunch_t *bunch, const int turnnumber, const double t, const double tstep) override
Record probe hits when bunch particles pass.
Definition Probe.cpp:82
virtual bool doPreCheck(PartBunch_t *) override
Virtual hook for preCheck.
Definition Probe.cpp:65
double step_m
Step size of the probe (bin width in histogram file).
Definition Probe.h:59
Probe()
Definition Probe.cpp:28
virtual void doGoOffline() override
Hook for goOffline.
Definition Probe.cpp:50
virtual ElementType getType() const override
Get element type std::string.
Definition Probe.cpp:124
virtual double getStep() const
Member variable access.
Definition Probe.cpp:61
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Probe.
Definition Probe.cpp:40
void get_bounds(Vector_t< T, Dim > &rmin, Vector_t< T, Dim > &rmax) const
size_t getLocalNum() const
Vector_t< double, Dim > R(size_t i)
Definition PartBunch.h:276
ParticleAttrib< Vector_t< T, Dim > > P
size_t getTotalNum() const