OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
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: 2000/03/27 09:32:32 $
17 // $Author: fci $
18 //
19 // ------------------------------------------------------------------------
20 
21 #include "AbsBeamline/Solenoid.h"
24 #include "Fields/Fieldmap.h"
25 #include "Physics/Physics.h"
26 
27 #include <iostream>
28 #include <fstream>
29 
30 extern Inform *gmsg;
31 
32 // Class Solenoid
33 // ------------------------------------------------------------------------
34 
36  Solenoid("")
37 {}
38 
39 
41  Component(right),
42  filename_m(right.filename_m),
43  myFieldmap_m(right.myFieldmap_m),
44  scale_m(right.scale_m),
45  scaleError_m(right.scaleError_m),
46  startField_m(right.startField_m),
47  fast_m(right.fast_m) {
48 }
49 
50 
51 Solenoid::Solenoid(const std::string &name):
52  Component(name),
53  filename_m(""),
54  myFieldmap_m(NULL),
55  scale_m(1.0),
56  scaleError_m(0.0),
57  startField_m(0.0),
58  fast_m(true) {
59 }
60 
61 
63  // Fieldmap::deleteFieldmap(filename_m);
64 }
65 
66 
67 void Solenoid::accept(BeamlineVisitor &visitor) const {
68  visitor.visitSolenoid(*this);
69 }
70 
71 void Solenoid::setFieldMapFN(std::string fn) {
72  filename_m = fn;
73 }
74 
75 void Solenoid::setFast(bool fast) {
76  fast_m = fast;
77 }
78 
79 
80 bool Solenoid::getFast() const {
81  return fast_m;
82 }
83 
84 bool Solenoid::apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) {
85  return apply(RefPartBunch_m->R[i], RefPartBunch_m->P[i], t, E, B);
86 }
87 
88 bool Solenoid::apply(const Vector_t &R, const Vector_t &/*P*/, const double &/*t*/, Vector_t &/*E*/, Vector_t &B) {
89  if (R(2) >= startField_m
90  && R(2) < startField_m + getElementLength()) {
91  Vector_t tmpE(0.0, 0.0, 0.0), tmpB(0.0, 0.0, 0.0);
92 
93  const bool outOfBounds = myFieldmap_m->getFieldstrength(R, tmpE, tmpB);
94  if (outOfBounds) return true;
95 
96  B += (scale_m + scaleError_m) * tmpB;
97  }
98 
99  return false;
100 }
101 
102 bool Solenoid::applyToReferenceParticle(const Vector_t &R, const Vector_t &/*P*/, const double &/*t*/, Vector_t &/*E*/, Vector_t &B) {
103 
104  if (R(2) >= startField_m
105  && R(2) < startField_m + getElementLength()) {
106  Vector_t tmpE(0.0, 0.0, 0.0), tmpB(0.0, 0.0, 0.0);
107 
108  const bool outOfBounds = myFieldmap_m->getFieldstrength(R, tmpE, tmpB);
109  if (outOfBounds) return true;
110 
111  B += scale_m * tmpB;
112  }
113 
114  return false;
115 }
116 
117 void Solenoid::initialise(PartBunchBase<double, 3> *bunch, double &startField, double &endField) {
118  Inform msg("Solenoid ", *gmsg);
119 
120  RefPartBunch_m = bunch;
121 
123 
124  if(myFieldmap_m != NULL) {
125  msg << level2 << getName() << " using file ";
126  myFieldmap_m->getInfo(&msg);
127 
128  double zBegin = 0.0, zEnd = 0.0;
129  myFieldmap_m->getFieldDimensions(zBegin, zEnd);
130 
131  startField_m = zBegin;
132  setElementLength(zEnd - zBegin);
133  endField = startField + getElementLength();
134  } else {
135  endField = startField;
136  }
137 }
138 
140 {}
141 
142 bool Solenoid::bends() const {
143  return false;
144 }
145 
146 
147 void Solenoid::goOnline(const double &) {
149  online_m = true;
150 }
151 
154  online_m = false;
155 }
156 
157 void Solenoid::setKS(double ks) {
158  scale_m = ks;
159 }
160 
161 void Solenoid::setDKS(double ks) {
162  scaleError_m = ks;
163 }
164 
165 void Solenoid::getDimensions(double &zBegin, double &zEnd) const {
166  zBegin = startField_m;
167  zEnd = startField_m + getElementLength();
168 }
169 
170 
172  return SOLENOID;
173 }
174 
175 bool Solenoid::isInside(const Vector_t &r) const {
176  return isInsideTransverse(r)
177  && myFieldmap_m->isInside(r);
178 }
179 
181  double &end) const {
183  end = begin + getElementLength();
184 }
Inform * gmsg
Definition: Main.cpp:62
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
Inform & level2(Inform &inf)
Definition: Inform.cpp:46
const std::string name
ParticlePos_t & R
ParticleAttrib< Vector_t > P
virtual void visitSolenoid(const Solenoid &)=0
Apply the algorithm to a solenoid.
Interface for a single beam element.
Definition: Component.h:50
bool online_m
Definition: Component.h:195
PartBunchBase< double, 3 > * RefPartBunch_m
Definition: Component.h:194
virtual const std::string & getName() const
Get element name.
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:432
virtual void setElementLength(double length)
Set design length.
Definition: ElementBase.h:436
bool isInsideTransverse(const Vector_t &r) const
Interface for solenoids.
Definition: Solenoid.h:36
virtual void getDimensions(double &zBegin, double &zEnd) const override
Definition: Solenoid.cpp:165
double scaleError_m
Definition: Solenoid.h:96
void setFast(bool fast)
Definition: Solenoid.cpp:75
virtual void getElementDimensions(double &zBegin, double &zEnd) const override
Definition: Solenoid.cpp:180
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) override
Definition: Solenoid.cpp:84
double startField_m
Definition: Solenoid.h:98
virtual void goOffline() override
Definition: Solenoid.cpp:152
virtual ~Solenoid()
Definition: Solenoid.cpp:62
virtual void goOnline(const double &kineticEnergy) override
Definition: Solenoid.cpp:147
virtual void finalise() override
Definition: Solenoid.cpp:139
bool fast_m
Definition: Solenoid.h:100
Fieldmap * myFieldmap_m
Definition: Solenoid.h:94
virtual bool applyToReferenceParticle(const Vector_t &R, const Vector_t &P, const double &t, Vector_t &E, Vector_t &B) override
Definition: Solenoid.cpp:102
bool getFast() const
Definition: Solenoid.cpp:80
virtual bool bends() const override
Definition: Solenoid.cpp:142
void setDKS(double ks)
Definition: Solenoid.cpp:161
virtual bool isInside(const Vector_t &r) const override
Definition: Solenoid.cpp:175
Solenoid()
Definition: Solenoid.cpp:35
std::string filename_m
Definition: Solenoid.h:93
void setKS(double ks)
Definition: Solenoid.cpp:157
void setFieldMapFN(std::string fn)
Definition: Solenoid.cpp:71
virtual void accept(BeamlineVisitor &) const override
Apply visitor to Solenoid.
Definition: Solenoid.cpp:67
virtual ElementBase::ElementType getType() const override
Get element type std::string.
Definition: Solenoid.cpp:171
double scale_m
Definition: Solenoid.h:95
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
Definition: Solenoid.cpp:117
virtual void getInfo(Inform *msg)=0
virtual void getFieldDimensions(double &zBegin, double &zEnd) const =0
static Fieldmap * getFieldmap(std::string Filename, bool fast=false)
Definition: Fieldmap.cpp:50
virtual bool isInside(const Vector_t &) const
Definition: Fieldmap.h:101
virtual void freeMap()=0
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const =0
virtual void readMap()=0
Definition: Inform.h:42