OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
CCollimator.cpp
Go to the documentation of this file.
1//
2// Class CCollimator
3// Interface for cyclotron collimator
4//
5// Copyright (c) 2018-2022, 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//
19
22#include "Fields/Fieldmap.h"
23#include "Physics/Units.h"
26
27#include <algorithm>
28#include <cmath>
29#include <fstream>
30#include <limits>
31#include <utility>
32
33extern Inform *gmsg;
34
37
38CCollimator::CCollimator(const std::string& name):
40 setDimensions(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
41 setGeom(0.0);
42}
43
45 PluginElement(right),
46 informed_m(right.informed_m) {
47 setDimensions(right.xstart_m, right.xend_m,
48 right.ystart_m, right.yend_m,
49 right.zstart_m, right.zend_m,
50 right.width_m);
52}
53
55
57 visitor.visitCCollimator(*this);
58}
59
61 Vector_t rmin, rmax;
62 bunch->get_bounds(rmin, rmax);
63
64 if (rmax(2) >= zstart_m && rmin(2) <= zend_m) {
65 // Absolute minimum and maximum of |x| and |y| over the bunch's bounding box.
66 // When an interval straddles zero the smallest attainable |coordinate| is 0,
67 // not min(|lo|,|hi|). Getting this wrong makes rbunch_min far too large, so
68 // the precheck reports the bunch out of range and the collimator silently
69 // never collimates anything.
70 double xmin = (rmin(0) <= 0.0 && rmax(0) >= 0.0)
71 ? 0.0 : std::min(std::abs(rmin(0)), std::abs(rmax(0)));
72 double xmax = std::max(std::abs(rmin(0)), std::abs(rmax(0)));
73 double ymin = (rmin(1) <= 0.0 && rmax(1) >= 0.0)
74 ? 0.0 : std::min(std::abs(rmin(1)), std::abs(rmax(1)));
75 double ymax = std::max(std::abs(rmin(1)), std::abs(rmax(1)));
76 double rbunch_min = std::hypot(xmin, ymin);
77 double rbunch_max = std::hypot(xmax, ymax);
78
79 if ( rbunch_max > rmin_m && rbunch_min < rmax_m ){ // check similar to z
80 return true;
81 }
82 }
83 return false;
84}
85
86// rectangle collimators in cyclotron cylindrical coordinates
87// when there is no particlematterinteraction, the particle hitting collimator is deleted directly
88bool CCollimator::doCheck(PartBunchBase<double, 3>* bunch, const int turnnumber,
89 const double t, const double /*tstep*/) {
90
91 bool flagNeedUpdate = false;
92 size_t tempnum = bunch->getLocalNum();
93 int pflag = 0;
94 // now check each particle in bunch
95 for (unsigned int i = 0; i < tempnum; ++i) {
96 if (bunch->R[i](2) < zend_m && bunch->R[i](2) > zstart_m ) {
97 // only now careful check in r
98 pflag = checkPoint(bunch->R[i](0), bunch->R[i](1));
100 if ((pflag != 0) && (bunch->Bin[i] != -1)) {
101 // OpalParticle takes the rest mass in MeV. M[i] is the macro-particle mass in
102 // GeV, except for particles made by a Stripper (STOP=FALSE) or by beam
103 // stripping, whose M[i] is their own mass in GeV.
104 const double mass = (bunch->POrigin[i] == ParticleOrigin::REGULAR) ?
105 bunch->getM() * Units::eV2MeV : bunch->M[i] * Units::GeV2MeV;
106 lossDs_m->addParticle(OpalParticle(bunch->ID[i],
107 bunch->R[i], bunch->P[i],
108 t, bunch->Q[i], mass),
109 std::make_pair(turnnumber, bunch->bunchNum[i]));
110
111 bunch->Bin[i] = -1;
112 flagNeedUpdate = true;
113 }
114 }
115 }
116 return flagNeedUpdate;
117}
118
120
121 reduce(&flagNeedUpdate, &flagNeedUpdate + 1, &flagNeedUpdate, OpBitwiseOrAssign());
122
123 if (flagNeedUpdate && parmatint_m) {
124 *gmsg << level2 << "============== START PARTICLE MATTER INTERACTION CALCULATION =============" << endl;
125 do {
127
128 bool collWithParticles = (parmatint_m->getParticlesInMat() > 0);
129
130 unsigned int localNum = bunch->getLocalNum();
131 unsigned int totalNum = 0;
132 reduce(localNum, totalNum, OpAddAssign());
133
134 bool allParticlesInMat = (totalNum == 0 && collWithParticles);
135 if (allParticlesInMat) {
137 }
138
139 Vector_t rmin, rmax;
140 bunch->get_bounds(rmin, rmax);
141 std::pair<Vector_t, double> boundingSphere;
142 boundingSphere.first = 0.5 * (rmax + rmin);
143 boundingSphere.second = euclidean_norm(rmax - boundingSphere.first);
144
145 parmatint_m->apply(bunch, boundingSphere);
147
148 } while (parmatint_m->stillActive());
149
150 *gmsg << level2 << "============== END PARTICLE MATTER INTERACTION CALCULATION =============" << endl;
151 }
152 return flagNeedUpdate;
153}
154
158
159void CCollimator::goOnline(const double&) {
160 print();
161 online_m = true;
162}
163
165 *gmsg << "* Finalize cyclotron collimator " << getName() << endl;
166}
167
169 if (RefPartBunch_m == nullptr) {
170 if (!informed_m) {
171 std::string errormsg = _Fieldmap::typeset_msg("BUNCH SIZE NOT SET", "warning");
172 ERRORMSG(errormsg << endl);
173 if (Ippl::myNode() == 0) {
174 std::ofstream omsg("errormsg.txt", std::ios_base::app);
175 omsg << errormsg << std::endl;
176 omsg.close();
177 }
178 informed_m = true;
179 }
180 return;
181 }
182}
183
184void CCollimator::setDimensions(double xstart, double xend,
185 double ystart, double yend,
186 double zstart, double zend,
187 double width) {
188 setDimensions(xstart, xend, ystart, yend);
189 zstart_m = zstart;
190 zend_m = zend;
191 width_m = width;
192 // zstart and zend are independent from x, y
193 if (zstart_m > zend_m) {
194 std::swap(zstart_m, zend_m);
195 }
197}
198
199void CCollimator::getDimensions(double& zBegin, double& zEnd) const {
200 zBegin = 0.0;
201 zEnd = getElementLength();
202}
203
207
209 // calculate maximum r from these
210 rmax_m = -std::numeric_limits<double>::max();
211 for (int i=0; i<4; i++) {
212 double r = std::hypot(geom_m[i].x, geom_m[i].y);
213 rmax_m = std::max(rmax_m, r);
214 }
215 // some debug printout
216 // for (int i=0; i<4; i++) {
217 // *gmsg << "point " << i << " ( " << geom_m[i].x << ", " << geom_m[i].y << ")" << endl;
218 // }
219 // *gmsg << "rmin " << rmin_m << " rmax " << rmax_m << endl;
220}
Inform * gmsg
Definition Main.cpp:70
Inform * gmsg
Definition Main.cpp:70
ElementType
Definition ElementBase.h:89
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Definition Vector.h:243
bool reduce(Communicate &, InputIterator, InputIterator, OutputIterator, const ReduceOp &, bool *IncludeVal=0)
#define ERRORMSG(msg)
Definition IpplInfo.h:350
Inform & level2(Inform &inf)
Definition Inform.cpp:46
Inform & endl(Inform &inf)
Definition Inform.cpp:42
const std::string name
constexpr double GeV2MeV
Definition Units.h:80
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
ParticleAttrib< Vector_t > P
ParticleAttrib< ParticleOrigin > POrigin
ParticleAttrib< double > Q
ParticleAttrib< short > bunchNum
ParticleIndex_t & ID
double getM() const
virtual void visitCCollimator(const CCollimator &)=0
Apply the algorithm to a collimator.
double zend_m
Definition CCollimator.h:86
virtual void doSetGeom() override
Calculate extend in r.
virtual void accept(BeamlineVisitor &) const override
Apply visitor to CCollimator.
virtual void goOnline(const double &kineticEnergy) override
Override implementation of PluginElement.
virtual void getDimensions(double &zBegin, double &zEnd) const override
void setDimensions(double xstart, double xend, double ystart, double yend, double zstart, double zend, double width)
Set dimensions and consistency checks.
virtual ElementType getType() const override
Get element type std::string.
virtual bool doPreCheck(PartBunchBase< double, 3 > *) override
Virtual hook for preCheck.
bool informed_m
Flag if error information already printed.
Definition CCollimator.h:82
virtual void doInitialise(PartBunchBase< double, 3 > *bunch) override
Initialise particle matter interaction.
double zstart_m
input geometry positions
Definition CCollimator.h:85
void print()
unused check method
virtual bool doFinaliseCheck(PartBunchBase< double, 3 > *bunch, bool flagNeedUpdate) override
Virtual hook for finaliseCheck.
virtual bool doCheck(PartBunchBase< double, 3 > *bunch, const int turnnumber, const double t, const double tstep) override
Record hits when bunch particles pass.
double width_m
Definition CCollimator.h:87
double rmax_m
maximum extend in r
Definition CCollimator.h:89
ParticleMatterInteractionHandler * parmatint_m
Definition CCollimator.h:91
virtual ~CCollimator()
virtual void doFinalise() override
Virtual hook for finalise.
bool online_m
Definition Component.h:192
PartBunchBase< double, 3 > * RefPartBunch_m
Definition Component.h:191
virtual const std::string & getName() const
Get element name.
virtual ParticleMatterInteractionHandler * getParticleMatterInteraction() const
virtual double getElementLength() const
Get design length.
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.
Point geom_m[5]
actual geometry positions with adaptive width such that each particle hits element once per turn
double xstart_m
input geometry positions
std::unique_ptr< LossDataSink > lossDs_m
Pointer to Loss instance.
double rmin_m
radius closest to the origin
static std::string typeset_msg(const std::string &msg, const std::string &title)
Definition Fieldmap.cpp:649
virtual size_t getParticlesInMat()=0
virtual void print(Inform &os)=0
virtual void apply(PartBunchBase< double, 3 > *bunch, const std::pair< Vector_t, double > &boundingSphere)=0
static int myNode()
Definition IpplInfo.cpp:710