OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
Solenoid.cpp
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2// $RCSfile: Solenoid.cpp,v $
3// ------------------------------------------------------------------------
4// $Revision: 1.1.1.1 $
5// ------------------------------------------------------------------------
6// Copyright: see Copyright.readme
7// ------------------------------------------------------------------------
8//
9// Class: Solenoid
10// Defines the abstract interface for a solenoid magnet.
11//
12// ------------------------------------------------------------------------
13// Class category: AbsBeamline
14// ------------------------------------------------------------------------
15//
16// $Date: 20.1.2026 $
17// $Author: PSI $
18//
19// ------------------------------------------------------------------------
20
23#include "Fields/Fieldmap.h"
24#include "PartBunch/PartBunch.h"
25#include "Physics/Physics.h"
26
27#include <cmath>
28#include <fstream>
29#include <iostream>
30
31extern Inform* gmsg;
32
33/* ============================== Constructors ============================== */
35
37 : Component(right),
38 filename_m(right.filename_m),
39 fieldmap_m(right.fieldmap_m),
40 scale_m(right.scale_m),
41 scaleError_m(right.scaleError_m),
42 startField_m(right.startField_m),
43 endField_m(right.endField_m),
44 fast_m(right.fast_m) {}
45
46Solenoid::Solenoid(const std::string& name)
47 : Component(name),
48 filename_m(""),
49 fieldmap_m(nullptr),
50 scale_m(1.0),
51 scaleError_m(0.0),
52 startField_m(0.0),
53 endField_m(0.0),
54 fast_m(true) {}
55
57 // _Fieldmap::deleteFieldmap(filename_m);
58}
59/* ========================================================================== */
60/* ============================== Apply Functions =========================== */
67bool Solenoid::apply(const std::shared_ptr<ParticleContainer_t>& pc) {
68 Inform m("Solenoid::apply");
69 m << level5 << "Solenoid::apply() called." << endl;
70
72
73 return false;
74}
75
87 const size_t& i, const double& t, Vector_t<double, 3>& E, Vector_t<double, 3>& B) {
88 std::shared_ptr<ParticleContainer_t> pc = RefPartBunch_m->getParticleContainer();
89 auto Rview = pc->R.getView();
90 auto Pview = pc->P.getView();
91
92 const Vector_t<double, 3> R = Rview(i);
93 const Vector_t<double, 3> P = Pview(i);
94 return apply(R, P, t, E, B);
95}
96
109 const Vector_t<double, 3>& R, const Vector_t<double, 3>& /*P*/, const double& /*t*/,
111 if (R(2) >= startField_m && R(2) < endField_m) {
112 Vector_t<double, 3> tmpE(0.0, 0.0, 0.0), tmpB(0.0, 0.0, 0.0);
113
114 const bool outOfBounds = fieldmap_m->getFieldstrength(R, tmpE, tmpB);
115 if (outOfBounds) {
117 }
118
119 B += (scale_m + scaleError_m) * tmpB;
120 }
121
122 return false;
123}
124
137 const Vector_t<double, 3>& R, const Vector_t<double, 3>& /*P*/, const double& /*t*/,
139 if (R(2) >= startField_m && R(2) < endField_m) {
140 Vector_t<double, 3> tmpE(0.0, 0.0, 0.0), tmpB(0.0, 0.0, 0.0);
141
142 const bool outOfBounds = fieldmap_m->getFieldstrength(R, tmpE, tmpB);
143 if (outOfBounds) return true;
144
145 B += scale_m * tmpB;
146 }
147
148 return false;
149}
150/* ========================================================================== */
151/* ============================== Functions ================================= */
153void Solenoid::accept(BeamlineVisitor& visitor) const { visitor.visitSolenoid(*this); }
154
156void Solenoid::setKS(double ks) { scale_m = ks; }
157
159void Solenoid::setDKS(double ks) { scaleError_m = ks; }
160
169void Solenoid::initialise(PartBunch_t* bunch, double& startField, double& endField) {
170 Inform msg("Solenoid ", *gmsg);
171
172 RefPartBunch_m = bunch;
173
174 fieldmap_m = Fieldmap::getFieldmap(filename_m, fast_m);
175
176 if (fieldmap_m != nullptr) {
177 msg << level2 << getName() << " using file ";
178 fieldmap_m->getInfo(&msg);
179
180 double zBegin = 0.0, zEnd = 0.0;
181 fieldmap_m->getFieldDimensions(zBegin, zEnd);
182
183 startField_m = zBegin;
184 endField_m = zEnd;
185
186 const double bodyBegin = startField;
187 startField = bodyBegin + startField_m;
188 endField = bodyBegin + endField_m;
189 } else {
190 startField_m = 0.0;
191 endField_m = 0.0;
192 endField = startField;
193 }
194}
195
197
198bool Solenoid::bends() const { return false; }
199
201void Solenoid::goOnline(const double&) {
202 Fieldmap::readMap(filename_m);
203 online_m = true;
204}
205
208 Fieldmap::freeMap(filename_m);
209 online_m = false;
210}
211
213void Solenoid::setFieldMapFN(std::string fn) { filename_m = fn; }
214
216void Solenoid::setFast(bool fast) { fast_m = fast; }
217
219bool Solenoid::getFast() const { return fast_m; }
220
224void Solenoid::getFieldExtend(double& zBegin, double& zEnd) const {
225 zBegin = startField_m;
226 zEnd = endField_m;
227}
228
232
237 return fieldmap_m != nullptr && isInsideTransverse(r) && fieldmap_m->isInside(r);
238}
239
243void Solenoid::getElementDimensions(double& begin, double& end) const {
244 begin = 0.0;
245 end = getElementLength();
246}
247
248bool Solenoid::getSupportEnvelope(double& horizontalRadius, double& verticalRadius) const {
249 const auto aperture = getAperture();
250 if (aperture.second.size() >= 2 && std::abs(aperture.second[0]) < 1e5
251 && std::abs(aperture.second[1]) < 1e5) {
252 horizontalRadius = std::abs(aperture.second[0]);
253 verticalRadius = std::abs(aperture.second[1]);
254 return horizontalRadius > 0.0 && verticalRadius > 0.0;
255 }
256
257 if (fieldmap_m == nullptr) {
258 return false;
259 }
260
261 try {
262 double xIni = 0.0, xFinal = 0.0, yIni = 0.0, yFinal = 0.0, zIni = 0.0, zFinal = 0.0;
263 fieldmap_m->getFieldDimensions(xIni, xFinal, yIni, yFinal, zIni, zFinal);
264 horizontalRadius = 0.5 * std::abs(xFinal - xIni);
265 verticalRadius = 0.5 * std::abs(yFinal - yIni);
266 return horizontalRadius > 0.0 && verticalRadius > 0.0;
267 } catch (...) {
268 return false;
269 }
270}
Inform * gmsg
Definition changes.cpp:7
ippl::Vector< T, Dim > Vector_t
ElementType
Definition ElementBase.h:94
Template PIC bunch: IPPL PicManager, shared field mesh/solver, and multiple particle containers.
Inform * gmsg
Definition changes.cpp:7
virtual void visitSolenoid(const Solenoid &)=0
Apply the algorithm to a Solenoid element.
bool online_m
Definition Component.h:226
PartBunch_t * RefPartBunch_m
Definition Component.h:225
virtual const std::string & getName() const
Get element name.
bool getFlagDeleteOnTransverseExit() const
virtual double getElementLength() const
Get design length.
std::pair< ApertureType, std::vector< double > > getAperture() const
bool isInsideTransverse(const Vector_t< double, 3 > &r) const
virtual bool getFieldstrength(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B) const =0
Get the field strength at a given point.
virtual void getInfo(Inform *msg)=0
Print info about the field map.
virtual void getFieldDimensions(double &zBegin, double &zEnd) const =0
Get the longitudinal dimensions of the field.
virtual void applyField(std::shared_ptr< ParticleContainer_t > pc, double scale=1.0)=0
Apply the FM to all the particles.
virtual bool isInside(const Vector_t< double, 3 > &) const =0
Check if a point is inside the field map.
Vector_t< double, Dim > R(size_t)
Do not use; throws (access positions via ParticleContainer::R).
Definition PartBunch.h:611
Abstract class for a solenoid magnet.
Definition Solenoid.h:33
double scaleError_m
Scale error multiplier.
Definition Solenoid.h:194
void setFast(bool fast)
Set the fast flag.
Definition Solenoid.cpp:216
double endField_m
End point of the field support in the local chart.
Definition Solenoid.h:200
virtual void initialise(PartBunch_t *bunch, double &startField, double &endField) override
initialise the solenoid element
Definition Solenoid.cpp:169
virtual void getElementDimensions(double &zBegin, double &zEnd) const override
Return the nominal body extent of the solenoid.
Definition Solenoid.cpp:243
virtual void getFieldExtend(double &zBegin, double &zEnd) const override
Return the local field-support interval of the solenoid.
Definition Solenoid.cpp:224
double startField_m
Starting point of the field.
Definition Solenoid.h:197
virtual void goOffline() override
Free field map and go offline.
Definition Solenoid.cpp:207
virtual ~Solenoid()
Definition Solenoid.cpp:56
virtual void goOnline(const double &kineticEnergy) override
Load field map and go online.
Definition Solenoid.cpp:201
virtual void finalise() override
Definition Solenoid.cpp:196
bool fast_m
Fast tracking flag.
Definition Solenoid.h:203
bool getFast() const
Get the fast flag.
Definition Solenoid.cpp:219
virtual bool bends() const override
Definition Solenoid.cpp:198
void setDKS(double ks)
Set the strength scaling error dks.
Definition Solenoid.cpp:159
Fieldmap * fieldmap_m
Fieldmap pointer.
Definition Solenoid.h:188
virtual ElementType getType() const override
Get the element type.
Definition Solenoid.cpp:231
std::string filename_m
Name of the field map file.
Definition Solenoid.h:185
virtual bool isInside(const Vector_t< double, 3 > &r) const override
Check if position r is inside the field map.
Definition Solenoid.cpp:236
bool getSupportEnvelope(double &horizontalRadius, double &verticalRadius) const
Get a finite transverse support envelope for placement/export.
Definition Solenoid.cpp:248
virtual bool applyToReferenceParticle(const Vector_t< double, 3 > &R, const Vector_t< double, 3 > &P, const double &t, Vector_t< double, 3 > &E, Vector_t< double, 3 > &B) override
Apply to reference particle with position R and momemtum P.
Definition Solenoid.cpp:136
void setKS(double ks)
Set the strength scaling factor ks.
Definition Solenoid.cpp:156
virtual bool apply(const std::shared_ptr< ParticleContainer_t > &pc) override
apply the solenoid field to all particles in the bunch
Definition Solenoid.cpp:67
void setFieldMapFN(std::string fn)
Assign the field filename.
Definition Solenoid.cpp:213
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Solenoid.
Definition Solenoid.cpp:153
double scale_m
Scale multiplier.
Definition Solenoid.h:191