OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Probe.cpp
Go to the documentation of this file.
1 #include "AbsBeamline/Probe.h"
2 
5 #include "Physics/Physics.h"
7 #include "Structure/PeakFinder.h"
8 
9 extern Inform *gmsg;
10 
12 {}
13 
14 Probe::Probe(const std::string &name):
15  PluginElement(name),
16  step_m(0.0)
17 {}
18 
19 Probe::Probe(const Probe &right):
20  PluginElement(right),
21  step_m(right.step_m)
22 {}
23 
25 
26 void Probe::accept(BeamlineVisitor &visitor) const {
27  visitor.visitProbe(*this);
28 }
29 
31  *gmsg << "* Initialize probe" << endl;
32  bool singlemode = (bunch->getTotalNum() == 1) ? true : false;
33  peakfinder_m = std::unique_ptr<PeakFinder> (new PeakFinder(getOutputFN(), rmin_m, rend_m, step_m, singlemode));
34 }
35 
37  *gmsg << "* Probe goes offline " << getName() << endl;
38  if (online_m && peakfinder_m)
39  peakfinder_m->save();
40  peakfinder_m.reset(nullptr);
41 }
42 
43 void Probe::setStep(double step) {
44  step_m = step;
45 }
46 
47 double Probe::getStep() const {
48  return step_m;
49 }
50 
52  Vector_t rmin, rmax;
53  bunch->get_bounds(rmin, rmax);
54  // interested in absolute minimum and maximum
55  double xmin = std::min(std::abs(rmin(0)), std::abs(rmax(0)));
56  double xmax = std::max(std::abs(rmin(0)), std::abs(rmax(0)));
57  double ymin = std::min(std::abs(rmin(1)), std::abs(rmax(1)));
58  double ymax = std::max(std::abs(rmin(1)), std::abs(rmax(1)));
59  double rbunch_min = std::hypot(xmin, ymin);
60  double rbunch_max = std::hypot(xmax, ymax);
61 
62  if( rbunch_max > rmin_m - 10.0 && rbunch_min < rend_m + 10.0 ) {
63  return true;
64  }
65  return false;
66 }
67 
68 bool Probe::doCheck(PartBunchBase<double, 3> *bunch, const int turnnumber, const double t, const double tstep) {
69  Vector_t probepoint;
70  size_t tempnum = bunch->getLocalNum();
71 
72  for(unsigned int i = 0; i < tempnum; ++i) {
73  double tangle = calculateIncidentAngle(bunch->P[i](0), bunch->P[i](1));
74  changeWidth(bunch, i, tstep, tangle);
75  int pflag = checkPoint(bunch->R[i](0), bunch->R[i](1));
76  if(pflag == 0) continue;
77  // calculate closest point at probe -> better to use momentum direction
78  // probe: y = -A/B * x - C/B or A*X + B*Y + C = 0
79  // perpendicular line through R: y = B/A * x + R(1) - B/A * R(0)
80  // 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);
81  // 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);
82  // probepoint(2) = bunch->R[i](2);
83  // calculate time correction for probepoint
84  // dist1 > 0, right hand, dt > 0; dist1 < 0, left hand, dt < 0
85  double dist1 = (A_m * bunch->R[i](0) + B_m * bunch->R[i](1) + C_m) / R_m * 1.0e-3; // [m]
86  double dist2 = dist1 * sqrt(1.0 + 1.0 / tangle / tangle);
87  double dt = dist2 / (sqrt(1.0 - 1.0 / (1.0 + dot(bunch->P[i], bunch->P[i]))) * Physics::c) * 1.0e9;
88 
89  probepoint = bunch->R[i] + dist2 * 1000.0 * bunch->P[i] / euclidean_norm(bunch->P[i]);
90 
91  // peak finder uses millimetre not metre
92  peakfinder_m->addParticle(probepoint);
93 
94  /*FIXME Issue #45: mm --> m (when OPAL-Cycl uses metre instead of millimetre,
95  * this can be removed.
96  */
97  probepoint *= 0.001;
98 
99  lossDs_m->addParticle(probepoint, bunch->P[i], bunch->ID[i], t+dt,
100  turnnumber, bunch->bunchNum[i]);
101  }
102 
103  peakfinder_m->evaluate(turnnumber);
104 
105  // we do not lose particles in the probe
106  return false;
107 }
108 
110  return PROBE;
111 }
virtual ~Probe()
Definition: Probe.cpp:24
double step_m
Step size of the probe (bin width in histogram file)
Definition: Probe.h:47
ParticleAttrib< Vector_t > P
void setStep(double step)
Set probe histogram bin width.
Definition: Probe.cpp:43
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
virtual ElementBase::ElementType getType() const override
Get element type std::string.
Definition: Probe.cpp:109
constexpr double e
The value of .
Definition: Physics.h:40
virtual void doInitialise(PartBunchBase< double, 3 > *bunch) override
Initialise peakfinder file.
Definition: Probe.cpp:30
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:68
double calculateIncidentAngle(double xp, double yp) const
Calculate angle of particle/bunch wrt to element.
virtual double getStep() const
Member variable access.
Definition: Probe.cpp:47
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:123
Inform * gmsg
Definition: Main.cpp:21
double C_m
Geometric lengths used in calculations.
bool online_m
Definition: Component.h:201
std::unique_ptr< LossDataSink > lossDs_m
Pointer to Loss instance.
std::unique_ptr< PeakFinder > peakfinder_m
Pointer to Peakfinder instance.
Definition: Probe.h:48
virtual const std::string & getName() const
Get element name.
Definition: ElementBase.cpp:95
ParticleAttrib< short > bunchNum
double rmin_m
radius closest to the origin
size_t getTotalNum() const
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition: Vector3D.cpp:118
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.
virtual void visitProbe(const Probe &)=0
Apply the algorithm to a probe.
Interface for probe.
Definition: Probe.h:16
Find peaks of radial profile.
ParticleIndex_t & ID
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
std::string getOutputFN() const
Get output filename.
size_t getLocalNum() const
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Probe.
Definition: Probe.cpp:26
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Definition: Vector.h:243
const std::string name
virtual void doGoOffline() override
Hook for goOffline.
Definition: Probe.cpp:36
ParticlePos_t & R
virtual bool doPreCheck(PartBunchBase< double, 3 > *) override
Virtual hook for preCheck.
Definition: Probe.cpp:51
Probe()
Definition: Probe.cpp:11
void get_bounds(Vector_t &rmin, Vector_t &rmax)
Abstract algorithm.
Definition: Inform.h:41
int checkPoint(const double &x, const double &y) const
Checks if coordinate is within element.
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:95
Inform & endl(Inform &inf)
Definition: Inform.cpp:42