OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
Ring.cpp
Go to the documentation of this file.
1//
2// Class Ring
3// Defines the abstract interface for a ring type geometry.
4//
5// Copyright (c) 2012 - 2023, Chris Rogers, STFC Rutherford Appleton Laboratory, Didcot, UK
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/Ring.h"
19
20#include "Utility/Inform.h"
21
25#include "Fields/EMField.h"
26#include "Physics/Units.h"
28
29
30#include <limits>
31#include <sstream>
32
33// fairly generous tolerance here... maybe too generous? Maybe should be
34// user parameter?
35const double Ring::lengthTolerance_m = 1e-2;
36const double Ring::angleTolerance_m = 1e-2;
37
38extern Inform* gmsg;
39extern Inform* gmsgALL;
40
41Ring::Ring(const std::string& ring)
42 : Component(ring), planarArcGeometry_m(1, 1),
43 refPartBunch_m(nullptr),
44 lossDS_m(nullptr),
45 beamRInit_m(0.), beamPRInit_m(0.), beamPhiInit_m(0.),
46 latticeRInit_m(0.), latticePhiInit_m(0.), latticeThetaInit_m(0.),
47 isLocked_m(false), isClosed_m(true),
48 symmetry_m(0), cyclHarm_m(0), rfFreq_m(0),
49 phiStep_m(Physics::pi/100.),
50 ringSections_m(),
51 section_list_m() {
52 setRefPartBunch(nullptr);
53}
54
55void Ring::accept(BeamlineVisitor& visitor) const {
56 visitor.visitRing(*this);
57}
58
59Ring::Ring(const Ring& ring)
60 : Component(ring.getName()),
61 planarArcGeometry_m(ring.planarArcGeometry_m),
62 lossDS_m(nullptr),
63 beamRInit_m(ring.beamRInit_m),
64 beamPRInit_m(ring.beamPRInit_m),
65 beamPhiInit_m(ring.beamPhiInit_m),
66 beamThetaInit_m(ring.beamThetaInit_m),
67 latticeRInit_m(ring.latticeRInit_m),
68 latticePhiInit_m(ring.latticePhiInit_m),
69 latticeThetaInit_m(ring.latticeThetaInit_m),
70 willDoAperture_m(ring.willDoAperture_m),
71 minR2_m(ring.minR2_m),
72 maxR2_m(ring.maxR2_m),
73 isLocked_m(ring.isLocked_m),
74 isClosed_m(ring.isClosed_m),
75 symmetry_m(ring.symmetry_m),
76 cyclHarm_m(ring.cyclHarm_m),
77 phiStep_m(ring.phiStep_m),
78 ringSections_m(),
79 section_list_m(ring.section_list_m.size()) {
81 if (ring.lossDS_m != nullptr)
82 throw GeneralClassicException("Ring::Ring(const Ring&)",
83 "Can't copy construct LossDataSink so copy constructor fails");
84 for (size_t i = 0; i < section_list_m.size(); ++i) {
85 section_list_m[i] = new RingSection(*ring.section_list_m[i]);
86 }
88}
89
91 for (size_t i = 0; i < section_list_m.size(); ++i)
92 delete section_list_m[i];
93}
94
95bool Ring::apply(const size_t& id, const double& t,
96 Vector_t& E, Vector_t& B) {
97
98 // A particle already flagged lost (Bin < 0) keeps failing here until the tracker
99 // deletes it: record only its first loss.
100 const bool flaggedBefore = (refPartBunch_m->Bin[id] < 0);
101
102 bool flagNeedUpdate = apply(refPartBunch_m->R[id], refPartBunch_m->P[id], t, E, B);
103
104 if (flagNeedUpdate && !flaggedBefore) {
105 *gmsgALL << level4 << getName() << ": particle " << refPartBunch_m->ID[id]
106 << " at " << refPartBunch_m->R[id]
107 << " m out of the field map boundary" << endl;
108
110 refPartBunch_m->R[id] , refPartBunch_m->P[id],
111 t,
112 refPartBunch_m->Q[id], refPartBunch_m->M[id]));
113
114 refPartBunch_m->Bin[id] = -1;
115 }
116
117 return flagNeedUpdate;
118}
119
120bool Ring::apply(const Vector_t& R, const Vector_t& /*P*/,
121 const double& t, Vector_t& E, Vector_t& B) {
122 B = Vector_t({0.0, 0.0, 0.0});
123 E = Vector_t({0.0, 0.0, 0.0});
124
125 std::vector<RingSection*> sections = getSectionsAt(R); // I think this doesn't actually use R -DW
126 bool outOfBounds = true;
127 // assume field maps don't set B, E to 0...
128 if (willDoAperture_m) {
129 double rSquared = R[0] * R[0] + R[1] * R[1];
130 if (rSquared < minR2_m || rSquared > maxR2_m) {
131 return true;
132 }
133 }
134 for (size_t i = 0; i < sections.size(); ++i) {
135 Vector_t B_temp({0.0, 0.0, 0.0});
136 Vector_t E_temp({0.0, 0.0, 0.0});
137 outOfBounds &= sections[i]->getFieldValue(R, refPartBunch_m->get_centroid(), t, E_temp, B_temp);
138 B += (scale_m * B_temp);
139 E += (scale_m * E_temp);
140 }
141 return outOfBounds;
142}
143
145 // lossDS_m is a borrowed pointer - we never delete it
146 lossDS_m = sink;
147}
148
149void Ring::getDimensions(double& /*zBegin*/, double& /*zEnd*/) const {
150 throw GeneralClassicException("Ring::getDimensions",
151 "Cannot get s-dimension of a ring");
152}
153
155 online_m = true;
156 setRefPartBunch(bunch);
157 setLossDataSink(new LossDataSink(getName(), false));
158}
159
161 double& /*startField*/, double& /*endField*/) {
162 initialise(bunch);
163}
164
166 lossDS_m->save();
167 online_m = false;
168 setLossDataSink(nullptr);
169}
170
172 RefPartBunch_m = bunch; // inherited from Component
173 refPartBunch_m = bunch; // private data (obeys style guide)
174}
175
176std::vector<RingSection*> Ring::getSectionsAt(const Vector_t& /*r*/) {
177 return section_list_m;
178}
179
181 // rotation from start to end in local coordinates
182 // Euclid3D/Rotation3D doesnt have a "getAngle" method so we use fairly
183 // obscure technique to extract it.
184 Vector3D rotationTest(1., 0., 0.);
185 rotationTest = delta.getRotation() * rotationTest;
186 double deltaAngle = std::atan2(rotationTest(2), rotationTest(0));
187 Rotation3D elementRotation = Rotation3D::ZRotation(deltaAngle);
188 return elementRotation;
189}
190
191void Ring::checkMidplane(Euclid3D delta) const {
192 if (std::abs(delta.getVector()(2)) > lengthTolerance_m ||
193 std::abs(delta.getRotation().getAxis()(0)) > angleTolerance_m ||
194 std::abs(delta.getRotation().getAxis()(1)) > angleTolerance_m) {
195 throw GeneralClassicException("Ring::checkMidplane",
196 std::string("Placement of elements out of the ")+
197 "midplane is not supported by Ring");
198 }
199}
200
202 Vector3D v = delta.getVector();
203 Vector3D r = delta.getRotation().getAxis();
204 // Euclid3D euclid(v(0), -v(2), v(1), r(0), -r(2), r(1));
205 Euclid3D euclid(v(2), v(0), -v(1), r(2), r(0), -r(1));
206 delta = euclid;
207}
208
209
211 if (!section_list_m.empty()) {
212 return section_list_m.back()->getEndPosition();
213 } else {
214 return getStartPosition();
215 }
216}
217
219 return Vector_t({latticeRInit_m * std::cos(latticePhiInit_m),
221 0.});
222}
223
225 if (!section_list_m.empty()) {
226 return section_list_m.back()->getEndNormal();
227 } else {
228 return getStartNormal();
229 }
230}
231
237
238void Ring::appendElement(const Component& element) {
239 if (isLocked_m) {
240 throw GeneralClassicException("Ring::appendElement",
241 "Attempt to append element " + element.getName() +
242 " when ring is locked");
243 }
244
245 RingSection* section = new RingSection();
246 Vector_t startPos = getNextPosition();
247 Vector_t startNorm = getNextNormal();
248
249 section->setComponent(dynamic_cast<Component*>(element.clone()));
250 section->setStartPosition(startPos);
251 section->setStartNormal(startNorm);
252 section->handleOffset();
253 // delta is transform from start of bend to end with x, z as horizontal
254 // I failed to get Rotation3D to work so use rotations written by hand.
255 // Probably an error in my call to Rotation3D.
256 Euclid3D delta = section->getComponent()->getGeometry().getTotalTransform();
258 checkMidplane(delta);
259
260 double placeF = std::atan2(startNorm(0), startNorm(1)); // angle between y axis and norm
261 Vector_t endPos = Vector_t({+delta.getVector()(0) * std::sin(placeF) + delta.getVector()(1) * std::cos(placeF),
262 +delta.getVector()(0) * std::cos(placeF) - delta.getVector()(1) * std::sin(placeF),
263 0}) + startPos;
264 section->setEndPosition(endPos);
265
266 double endF = delta.getRotation().getAxis()(2);//+
267 //atan2(delta.getVector()(1), delta.getVector()(0));
268 Vector_t endNorm = Vector_t({+startNorm(0) * std::cos(endF) - startNorm(1) * std::sin(endF),
269 +startNorm(0) * std::sin(endF) + startNorm(1) * std::cos(endF),
270 0});
271 section->setEndNormal(endNorm);
272
273 section->setComponentPosition(startPos);
274 double orientation = std::atan2(startNorm(1), startNorm(0));
275 section->setComponentOrientation(Vector_t({0, 0, orientation}));
276
277 section_list_m.push_back(section);
278 *gmsg << "* Added " << element.getName() << " to Ring" << endl;
279 *gmsg << "* Start position ("
280 << section->getStartPosition()(0) << ", "
281 << section->getStartPosition()(1) << ", "
282 << section->getStartPosition()(2) << ") normal ("
283 << section->getStartNormal()(0) << ", "
284 << section->getStartNormal()(1) << ", "
285 << section->getStartNormal()(2) << ")" << endl;
286 *gmsg << "* End position ("
287 << section->getEndPosition()(0) << ", "
288 << section->getEndPosition()(1) << ", "
289 << section->getEndPosition()(2) << ") normal ("
290 << section->getEndNormal()(0) << ", "
291 << section->getEndNormal()(1) << ", "
292 << section->getEndNormal()(2) << ")" << endl;
293}
294
296 if (isLocked_m) {
297 throw GeneralClassicException("Ring::lockRing",
298 "Attempt to lock ring when it is already locked");
299 }
300 // check for any elements at all
301 size_t sectionListSize = section_list_m.size();
302 if (sectionListSize == 0) {
303 throw GeneralClassicException("Ring::lockRing",
304 "Failed to find any elements in Ring");
305 }
306 // Apply symmetry properties; I think it is fastest to just duplicate
307 // elements rather than try to do some angle manipulations when we do field
308 // lookups because we do field lookups in Cartesian coordinates in general.
309 *gmsg << "Applying symmetry to Ring" << endl;
310 for (int i = 1; i < symmetry_m; ++i) {
311 for (size_t j = 0; j < sectionListSize; ++j) {
312 appendElement(*section_list_m[j]->getComponent());
313 }
314 }
315 //resetAzimuths();
316 // Check that the ring is closed within tolerance and close exactly
317 if (isClosed_m)
320 isLocked_m = true;
321 for (size_t i = 0; i < section_list_m.size(); i++) {
322 }
323}
324
326 for (size_t i = 0; i < section_list_m.size(); ++i) {
327 Vector_t startPos = section_list_m[i]->getEndPosition();
328 Vector_t startDir = section_list_m[i]->getEndNormal();
329 Vector_t endPos = section_list_m[i]->getEndPosition();
330 Vector_t endDir = section_list_m[i]->getEndNormal();
331 if (!section_list_m[i]->isOnOrPastStartPlane(endPos)) {
332 section_list_m[i]->setEndPosition(startPos);
333 section_list_m[i]->setEndNormal(startDir);
334 section_list_m[i]->setStartPosition(endPos);
335 section_list_m[i]->setStartNormal(endDir);
336 }
337 }
338}
339
341 Vector_t first_pos = section_list_m[0]->getStartPosition();
342 Vector_t first_norm = section_list_m[0]->getStartNormal();
343 Vector_t last_pos = section_list_m.back()->getEndPosition();
344 Vector_t last_norm = section_list_m.back()->getEndNormal();
345 for (int i = 0; i < 3; ++i) {
346 if (std::abs(first_pos(i) - last_pos(i)) > lengthTolerance_m ||
347 std::abs(first_norm(i) - last_norm(i)) > angleTolerance_m)
348 throw GeneralClassicException("Ring::lockRing",
349 "Ring is not closed");
350 }
351 section_list_m.back()->setEndPosition(first_pos);
352 section_list_m.back()->setEndNormal(first_norm);
353}
354
356 size_t nSections = Physics::two_pi/phiStep_m+1;
357 ringSections_m = std::vector< std::vector<RingSection*> >(nSections);
358 for (size_t i = 0; i < ringSections_m.size(); ++i) {
359 double phi0 = i*phiStep_m;
360 double phi1 = (i+1)*phiStep_m;
361 for (size_t j = 0; j < section_list_m.size(); ++j) {
362 if (section_list_m[j]->doesOverlap(phi0, phi1))
363 ringSections_m[i].push_back(section_list_m[j]);
364 }
365 }
366}
367
369 if (section_list_m.empty()) {
370 return nullptr;
371 }
372 return section_list_m.back();
373}
374
375bool Ring::sectionCompare(RingSection const* const sec1,
376 RingSection const* const sec2) {
377 return sec1 > sec2;
378}
379
380void Ring::setRingAperture(double minR, double maxR) {
381 if (minR < 0 || maxR < 0) {
382 throw GeneralClassicException("Ring::setRingAperture",
383 "Could not parse negative or undefined aperture limit");
384 }
385
386 willDoAperture_m = true;
387 minR2_m = minR * minR;
388 maxR2_m = maxR * maxR;
389}
390
392 if (i < 0 || i >= int(getNumberOfRingSections())) {
393 std::stringstream err;
394 err << "Attempt to get RingSection for element " << i
395 << ". Should be in range 0 <= i < " << getNumberOfRingSections();
396 throw GeneralClassicException("Ring::getSection(int)", err.str());
397 }
399 if (sec == nullptr) {
400 throw GeneralClassicException("Ring::getSection",
401 "Opal internal error - RingSection was null");
402 }
403 return sec;
404}
405
407 return section_list_m.size();
408}
Inform * gmsg
Definition Main.cpp:70
Inform * gmsgALL
Definition Main.cpp:71
Inform * gmsg
Definition Main.cpp:70
Tps< T > sec(const Tps< T > &x)
Secant.
Definition TpsMath.h:153
Inform * gmsgALL
Definition Main.cpp:71
const double pi
Definition fftpack.cpp:893
Inform & level4(Inform &inf)
Definition Inform.cpp:48
Inform & endl(Inform &inf)
Definition Inform.cpp:42
Definition Air.h:27
constexpr double two_pi
The value of.
Definition Physics.h:33
constexpr double e
The value of.
Definition Physics.h:39
ParticlePos_t & R
ParticleAttrib< int > Bin
ParticleAttrib< double > M
ParticleAttrib< Vector_t > P
ParticleAttrib< double > Q
Vector_t get_centroid() const
ParticleIndex_t & ID
virtual void visitRing(const Ring &)=0
Apply the algorithm to a ring.
Interface for a single beam element.
Definition Component.h:50
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 ElementBase * clone() const =0
Return clone.
virtual BGeometryBase & getGeometry()=0
Get geometry.
Ring describes a ring type geometry for tracking.
Definition Ring.h:53
bool isClosed_m
Definition Ring.h:398
LossDataSink * lossDS_m
Definition Ring.h:374
Vector_t getNextPosition() const
Definition Ring.cpp:210
void buildRingSections()
Definition Ring.cpp:355
std::vector< RingSection * > getSectionsAt(const Vector_t &pos)
Definition Ring.cpp:176
double maxR2_m
Definition Ring.h:391
Rotation3D getRotationStartToEnd(Euclid3D delta) const
Definition Ring.cpp:180
size_t getNumberOfRingSections() const
Definition Ring.cpp:406
double minR2_m
Definition Ring.h:390
bool willDoAperture_m
Definition Ring.h:389
static bool sectionCompare(RingSection const *const sec1, RingSection const *const sec2)
Definition Ring.cpp:375
Vector_t getNextNormal() const
Definition Ring.cpp:224
virtual void finalise() override
Definition Ring.cpp:165
void checkAndClose()
Definition Ring.cpp:340
void resetAzimuths()
Definition Ring.cpp:325
virtual ~Ring()
Definition Ring.cpp:90
virtual bool apply(const size_t &id, const double &t, Vector_t &E, Vector_t &B) override
Definition Ring.cpp:95
RingSection * getLastSectionPlaced() const
Definition Ring.cpp:368
void setRefPartBunch(PartBunchBase< double, 3 > *bunch)
Definition Ring.cpp:171
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
Definition Ring.cpp:160
double latticeThetaInit_m
Definition Ring.h:385
std::vector< RingSectionList > ringSections_m
Definition Ring.h:413
virtual void accept(BeamlineVisitor &visitor) const override
Definition Ring.cpp:55
void setRingAperture(double minR, double maxR)
Definition Ring.cpp:380
double latticePhiInit_m
Definition Ring.h:384
RingSectionList section_list_m
Definition Ring.h:414
double scale_m
Definition Ring.h:403
void checkMidplane(Euclid3D delta) const
Definition Ring.cpp:191
void setLossDataSink(LossDataSink *sink)
Definition Ring.cpp:144
int symmetry_m
Definition Ring.h:401
double latticeRInit_m
Definition Ring.h:383
virtual void getDimensions(double &zBegin, double &zEnd) const override
Definition Ring.cpp:149
double phiStep_m
Definition Ring.h:412
void lockRing()
Definition Ring.cpp:295
bool isLocked_m
Definition Ring.h:394
void appendElement(const Component &element)
Definition Ring.cpp:238
Vector_t getStartNormal() const
Definition Ring.cpp:232
void rotateToCyclCoordinates(Euclid3D &euclid3d) const
Definition Ring.cpp:201
static const double lengthTolerance_m
Definition Ring.h:417
RingSection * getSection(int i) const
Definition Ring.cpp:391
Vector_t getStartPosition() const
Definition Ring.cpp:218
PartBunchBase< double, 3 > * refPartBunch_m
Definition Ring.h:369
static const double angleTolerance_m
Definition Ring.h:418
Displacement and rotation in space.
Definition Euclid3D.h:68
const Vector3D & getVector() const
Get displacement.
Definition Euclid3D.cpp:59
const Rotation3D & getRotation() const
Get rotation.
Definition Euclid3D.cpp:64
virtual Euclid3D getTotalTransform() const
Get transform.
Definition Geometry.cpp:51
Rotation in 3-dimensional space.
Definition Rotation3D.h:46
static Rotation3D ZRotation(double angle)
Make rotation.
Vector3D getAxis() const
Get axis vector.
A 3-dimension vector.
Definition Vector3D.h:31
bool save(unsigned int numSets=1, OpalData::OpenMode openMode=OpalData::OpenMode::UNDEFINED)
Returns whether the file was written; false (on all ranks) if there was nothing to save.
void addParticle(const OpalParticle &, const std::optional< std::pair< int, short int > > &turnBunchNumPair=std::nullopt)
Component placement handler in ring geometry.
Definition RingSection.h:58
void setStartNormal(Vector_t orientation)
void setComponentPosition(Vector_t position)
void setComponent(Component *component)
void handleOffset()
void setStartPosition(Vector_t pos)
Vector_t getStartNormal() const
void setComponentOrientation(Vector_t orientation)
Component * getComponent() const
Vector_t getEndPosition() const
Vector_t getStartPosition() const
void setEndPosition(Vector_t pos)
Vector_t getEndNormal() const
void setEndNormal(Vector_t orientation)
Vektor< double, 3 > Vector_t
Definition Vektor.h:6