OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
CorrectionBase.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: CorrectionBase.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.3 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Classes: CorrectionBase
10 //
11 // ------------------------------------------------------------------------
12 //
13 // $Date: 2001/08/13 15:25:21 $
14 // $Author: jowett $
15 //
16 // ------------------------------------------------------------------------
17 
18 #include "Tables/CorrectionBase.h"
19 #include "AbsBeamline/Corrector.h"
20 #include "AbsBeamline/Monitor.h"
23 #include "Algorithms/IdealMapper.h"
24 #include "Attributes/Attributes.h"
25 #include "Elements/OpalHKicker.h"
26 #include "Elements/OpalKicker.h"
27 #include "Elements/OpalVKicker.h"
28 #include "Fields/BDipoleField.h"
29 #include "Physics/Physics.h"
31 
32 
33 // Nested class CorrectionBase::Row
34 // ------------------------------------------------------------------------
35 
36 
37 // Class CorrectionBase
38 // ------------------------------------------------------------------------
39 
41  FlaggedElmPtr(elem, occur), orbit(), matrix()
42 {}
43 
44 
46  FlaggedElmPtr(rhs), orbit(), matrix()
47 {}
48 
49 
51 {}
52 
53 
54 // Class CorrectionBase
55 // ------------------------------------------------------------------------
56 // Abstract base class for all orbit correction commands.
57 // Factors out all common behaviour for these algorithms.
58 
59 CorrectionBase::CorrectionBase(int size, const char *name, const char *help):
60  Action(size, name, help) {
62  ("LINE", "The beam line to be Micadoed");
64  ("BEAM", "The beam to be used", "UNNAMED_BEAM");
66  ("RANGE", "The range in the lattice");
67 }
68 
69 
70 CorrectionBase::CorrectionBase(const std::string &name, CorrectionBase *parent):
71  Action(name, parent)
72 {}
73 
74 
76 {}
77 
78 
79 void CorrectionBase::addKick(int plane, Row &row, double kick) {
80  // Find object corresponding to current corrector.
81  const std::string &name = row.getElement()->getName();
82  Object *elem = OpalData::getInstance()->find(name);
83 
84  // Decide on type of corrector.
85  if(dynamic_cast<OpalKicker *>(elem)) {
86  int attrNumber = (plane == 0) ? OpalKicker::HKICK : OpalKicker::VKICK;
87  kick += Attributes::getReal(elem->itsAttr[attrNumber]);
88  Attributes::setReal(elem->itsAttr[attrNumber], kick);
89  } else if(dynamic_cast<OpalHKicker *>(elem)) {
92  } else if(dynamic_cast<OpalVKicker *>(elem)) {
95  } else {
96  throw OpalException("CorrectionBase::addKick()",
97  "Orbit Corrector \"" + name + "\" not found.");
98  }
99 
100  // Update the CLASSIC corrector.
101  elem->update();
102 }
103 
104 
105 void CorrectionBase::listCorrectors(bool list, int plane) {
106  double rms = 0.0;
107  double top = 0.0;
108  std::cout << "\nTotal corrector settings for plane "
109  << (plane == 0 ? 'X' : 'Y') << '\n';
110 
111  for(LocalIter iter = correctorTable[plane].begin();
112  iter != correctorTable[plane].end(); ++iter) {
113  ElementBase *design = (*iter)->getElement()->removeWrappers();
114  Corrector *corr = dynamic_cast<Corrector *>(design);
115  BDipoleField &field = corr->getField();
116  double setting = (plane == 0) ? - field.getBy() : field.getBx();
117  setting *= Physics::c / OpalData::getInstance()->getP0();
118 
119  if(list) {
120  std::cout << std::setw(20) << corr->getName()
121  << std::setw(12) << setting << '\n';
122  }
123 
124  rms += setting * setting;
125  top = std::max(std::abs(setting), top);
126  }
127 
128  rms = sqrt(rms / double(correctorTable[plane].size()));
129  std::cout << "R.m.s. : " << std::setw(20) << rms << '\n'
130  << "Maximum: " << std::setw(20) << top << std::endl;
131 }
132 
133 
134 void CorrectionBase::listMonitors(bool list, int plane) {
135  double rms = 0.0;
136  double top = 0.0;
137  int q = 2 * plane;
138  std::cout << "\nFinal monitor readings for plane "
139  << (plane == 0 ? 'X' : 'Y') << '\n';
140 
141  for(LocalIter iter = monitorTable[plane].begin();
142  iter != monitorTable[plane].end(); ++iter) {
143  double reading = (*iter)->orbit[q];
144  if(list) {
145  std::cout << std::setw(20) << (*iter)->getElement()->getName()
146  << std::setw(12) << reading << '\n';
147  }
148 
149  rms += reading * reading;
150  top = std::max(std::abs(reading), top);
151  }
152 
153  rms = sqrt(rms / double(monitorTable[plane].size()));
154  std::cout << "R.m.s. : " << std::setw(20) << rms << '\n'
155  << "Maximum: " << std::setw(20) << top << std::endl;
156 }
157 
158 
160  IdealMapper mapper(itsLine, reference, false, false);
161  double arc = 0.0;
162 
163  for(TLine::iterator iter = itsLine.begin();
164  iter != itsLine.end(); ++iter) {
165  // Build ideal matrix.
166  iter->accept(mapper);
167  mapper.getMatrix(iter->matrix);
168  iter->isUsed[0] = iter->isUsed[1] = false;
169 
170  // Accumulate arc length.
171  ElementBase *elem = iter->getElement();
172  arc += elem->getElementLength();
173  iter->arc = arc;
174 
175  // Put correctors and monitors in relevant table.
176  test(elem);
177  if(isCorr[0]) correctorTable[0].push_back(&*iter);
178  if(isCorr[1]) correctorTable[1].push_back(&*iter);
179  if(isMoni[0]) monitorTable[0].push_back(&*iter);
180  if(isMoni[1]) monitorTable[1].push_back(&*iter);
181  }
182 }
183 
184 
186  ElementBase *design = base->removeWrappers();
187  isCorr[0] = isCorr[1] = isMoni[0] = isMoni[1] = false;
188 
189  if(Corrector *corr = dynamic_cast<Corrector *>(design)) {
190  switch(corr->getPlane()) {
191 
192  case Corrector::X:
193  isCorr[0] = true;
194  break;
195 
196  case Corrector::Y:
197  isCorr[1] = true;
198  break;
199 
200  case Corrector::XY:
201  isCorr[0] = isCorr[1] = true;
202  break;
203 
204  default:
205  break;
206  }
207  } else if(Monitor *moni = dynamic_cast<Monitor *>(design)) {
208  switch(moni->getPlane()) {
209 
210  case Monitor::X:
211  isMoni[0] = true;
212  break;
213 
214  case Monitor::Y:
215  isMoni[1] = true;
216  break;
217 
218  case Monitor::XY:
219  isMoni[0] = isMoni[1] = true;
220  break;
221 
222  default:
223  break;
224  }
225  }
226 }
void setReal(Attribute &attr, double val)
Set real value.
Definition: Attributes.cpp:236
Abstract base class for all orbit correction commands.
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
virtual BDipoleField & getField()=0
Return the corrector field.
Interface for basic beam line object.
Definition: ElementBase.h:128
The field of a magnetic dipole.
Definition: BDipoleField.h:31
double getP0() const
Return value of global reference momentum.
Definition: OpalData.cpp:623
Corrector acts on both planes.
Definition: Corrector.h:48
The base class for all OPAL actions.
Definition: Action.h:30
Build a map using the linear map around the design orbit for each element.
Definition: IdealMapper.h:69
Interface for beam position monitors.
Definition: Monitor.h:41
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:123
The base class for all OPAL exceptions.
Definition: OpalException.h:28
virtual ElementBase * removeWrappers()
Return the design element.
Interface for general corrector.
Definition: Corrector.h:35
void test(ElementBase *)
Routine to test for corrector or monitor.
Attribute makeRange(const std::string &name, const std::string &help)
Create a range attribute.
Definition: Attributes.cpp:171
virtual double getBy() const
Get vertical component.
virtual const std::string & getName() const
Get element name.
Definition: ElementBase.cpp:95
Monitor acts on both planes.
Definition: Monitor.h:54
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
LocalList monitorTable[2]
Monitor acts on x-plane.
Definition: Monitor.h:50
void listCorrectors(bool list, int plane)
List correctors before or after correction.
static OpalData * getInstance()
Definition: OpalData.cpp:209
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:511
void listMonitors(bool list, int plane)
List monitors before or after correction.
TLine itsLine
The flat beam line on which the correction is done.
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
Corrector acts on y-plane.
Definition: Corrector.h:46
LocalList::iterator LocalIter
LocalList correctorTable[2]
Corrector acts on x-plane.
Definition: Corrector.h:44
virtual double getBx() const
Get horizontal component.
Monitor acts on y-plane.
Definition: Monitor.h:52
Row(ElementBase *, int)
virtual void getMatrix(FMatrix< double, 6, 6 > &) const
Return the linear part of the accumulated map.
Definition: IdealMapper.cpp:57
bool isMoni[2]
Flag telling wether a monitor exists.
PartData reference
The particle reference data.
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
virtual ~CorrectionBase()
virtual void update()
Update this object.
Definition: Object.cpp:237
Object * find(const std::string &name)
Find entry.
Definition: OpalData.cpp:618
The base class for all OPAL objects.
Definition: Object.h:48
const std::string name
std::string::iterator iterator
Definition: MSLang.h:16
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
bool isCorr[2]
Flags telling wether a corrector exists.
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
void setupTables()
Set up the corrector and monitor tables.
ElementBase * getElement() const
Get the element pointer.
Definition: ElmPtr.h:58
void addKick(int plane, Row &, double kick)
Add to kicker strength.
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
Structure for a row of the Twiss table.
A section of a beam line.
Definition: FlaggedElmPtr.h:36