OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
ThreadBpm.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: ThreadBpm.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2.4.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: ThreadBpm
10 // The abstract class ThreadBpm implements the interface for a table buffer
11 // holding lattice function.
12 //
13 // ------------------------------------------------------------------------
14 //
15 // $Date: 2002/12/09 15:06:08 $
16 // $Author: jsberg $
17 //
18 // ------------------------------------------------------------------------
19 
20 #include "Tables/ThreadBpm.h"
24 #include "Algorithms/ThickMapper.h"
26 #include "Algorithms/ThinMapper.h"
27 #include "Attributes/Attributes.h"
28 #include "Structure/Beam.h"
29 #include "Tables/Flatten.h"
30 #include "Utilities/DomainError.h"
32 #include "Utilities/Options.h"
33 #include "Utilities/Round.h"
34 #include <iomanip>
35 #include <iostream>
36 
37 
38 // Class ThreadBpm
39 // ------------------------------------------------------------------------
40 
42  CorrectionBase(SIZE, "THREADBPM",
43  "The \"THREADBPM\" command threads the closed orbit "
44  "using the positions at all orbit monitors.") {
46  ("TOL", "The tolerance for the closed orbit positions.");
48  ("LISTC", "List the correctors after correction");
50  ("LISTM", "List the monitors after correction");
51 
53 }
54 
55 
56 ThreadBpm::ThreadBpm(const std::string &name, ThreadBpm *parent):
57  CorrectionBase(name, parent)
58 {}
59 
60 
62 {}
63 
64 
65 ThreadBpm *ThreadBpm::clone(const std::string &name) {
66  return new ThreadBpm(name, this);
67 }
68 
69 
71  // Find Table definition.
72  const std::string &lineName = Attributes::getString(itsAttr[LINE]);
73  BeamSequence *use = BeamSequence::find(lineName);
74 
75  // Find Beam data.
76  const std::string &beamName = Attributes::getString(itsAttr[BEAM]);
77  Beam *beam = Beam::find(beamName);
78  reference = beam->getReference();
79 
80  // Get the data for threading.
82  double tol = Attributes::getReal(itsAttr[TOL]);
83  bool listc = Attributes::getBool(itsAttr[LISTC]);
84  bool listm = Attributes::getBool(itsAttr[LISTM]);
85 
86  // Make sure all is up-to-date.
88 
89  // Create flat line for correction.
90  Flatten<Row> flattener(*use->fetchLine(), itsLine, range);
91  flattener.execute();
92 
93  if(itsLine.empty() && Options::warn) {
94  std::cerr << "\n### Warning ### Lattice function table \""
95  << lineName << "\" contains no elements.\n" << std::endl;
96  }
97 
98  // Starting point for threading.
99  const int iteration_limit = 100;
100  int count = 0;
101 
102  // Set up the tables of correctors and monitors.
103  setupTables();
104 
105  // Orbit at start position.
106  itsTracker = new OrbitTracker(itsLine, reference, false, false);
108  TLine::iterator iter = itsLine.begin();
109 
110  while(iter != itsLine.end()) {
111  // Store orbit before current element.
113  iter->orbit = orbit;
114 
115  // Advance through current element and determine element type.
116  try {
117  iter->accept(*itsTracker);
118  orbit = itsTracker->getOrbit();
119  test(iter->getElement());
120 
121  if(isMoni[0] && !iter->isUsed[0] && std::abs(orbit[0]) > tol) {
122  // Overflow detected in x.
123  if(++count > iteration_limit) break;
124  iter->isUsed[0] = true;
125  correct(0, iter);
126  } else if(isMoni[1] && !iter->isUsed[1] && std::abs(orbit[2]) > tol) {
127  // Overflow detected in y.
128  if(++count > iteration_limit) break;
129  iter->isUsed[1] = true;
130  correct(1, iter);
131  } else {
132  // Go to next element.
133  ++iter;
134  }
135  } catch(const DomainError &) {
136  break;
137  }
138  }
139 
140  std::cout << "After threading with \"THREADBPM\":\n";
141  for(int plane = 0; plane < 2; ++plane) {
142  listCorrectors(listc, plane);
143  listMonitors(listm, plane);
144  }
145 }
146 
147 
148 void ThreadBpm::correct(int plane, TLine::iterator &iter) {
149  // Common information.
150  int p1 = 2 * plane;
151  int p2 = 2 * plane + 1;
152 
153  // Extract orbit and transfer matrix at current position.
154  FVector<double, 6> orbit = iter->orbit;
155  FMatrix<double, 6, 6> omat = iter->matrix;
156 
157  // Backtrack to find two correctors for the given plane.
158  TLine::iterator corr[2];
159  FMatrix<double, 6, 6> cmat[2];
160  int count = 2;
161 
162  while(iter != itsLine.begin()) {
163  // Backtrack and test for corrector.
164  --iter;
165  test(iter->getElement());
166 
167  if(isCorr[plane]) {
168  // Extract transfer matrix to this corrector.
169  if(count > 0) --count;
170  corr[count] = iter;
171  cmat[count] = iter->matrix;
172 
173  if(count == 0) {
174  // We have found two correctors.
175  double r1 = orbit(p1);
176  double r2 = orbit(p2);
177 
178  double a11 =
179  omat(p1, 1) * cmat[0](p1, 0) - omat(p1, 0) * cmat[0](p1, 1) +
180  omat(p1, 3) * cmat[0](p1, 2) - omat(p1, 2) * cmat[0](p1, 3) +
181  omat(p1, 5) * cmat[0](p1, 4) - omat(p1, 4) * cmat[0](p1, 5);
182  double a12 =
183  omat(p1, 1) * cmat[1](p1, 0) - omat(p1, 0) * cmat[1](p1, 1) +
184  omat(p1, 3) * cmat[1](p1, 2) - omat(p1, 2) * cmat[1](p1, 3) +
185  omat(p1, 5) * cmat[1](p1, 4) - omat(p1, 4) * cmat[1](p1, 5);
186  double a21 =
187  omat(p2, 1) * cmat[0](p1, 0) - omat(p2, 0) * cmat[0](p1, 1) +
188  omat(p2, 3) * cmat[0](p1, 2) - omat(p2, 2) * cmat[0](p1, 3) +
189  omat(p2, 5) * cmat[0](p1, 4) - omat(p2, 4) * cmat[0](p1, 5);
190  double a22 =
191  omat(p2, 1) * cmat[1](p1, 0) - omat(p2, 0) * cmat[1](p1, 1) +
192  omat(p2, 3) * cmat[1](p1, 2) - omat(p2, 2) * cmat[1](p1, 3) +
193  omat(p2, 5) * cmat[1](p1, 4) - omat(p2, 4) * cmat[1](p1, 5);
194  double det = a11 * a22 - a12 * a21;
195 
196  if(std::abs(det) > 0.01) {
197  double k1 = - (a22 * r1 - a12 * r2) / det;
198  double k2 = - (a11 * r2 - a21 * r1) / det;
199 
200  // Store the kicks.
201  addKick(plane, *corr[0], k1);
202  addKick(plane, *corr[1], k2);
203 
204  // Reset to start before the first kicker.
205  --iter;
206  itsTracker->setOrbit(iter->orbit);
207  return;
208  }
209  }
210  }
211  }
212 
213  // No corrector pair found; change initial conditions.
214  FLUMatrix<double, 6>(omat).backSubstitute(orbit);
215  orbitGuess -= orbit;
217 }
Abstract base class for all orbit correction commands.
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
void correct(int plane, TLine::iterator &)
Definition: ThreadBpm.cpp:148
RangeRep getRange(const Attribute &attr)
Get range value.
Definition: Attributes.cpp:177
T det(const AntiSymTenzor< T, 3 > &t)
void test(ElementBase *)
Routine to test for corrector or monitor.
Flatten a beamline.
Definition: Flatten.h:42
void update()
Update all objects.
Definition: OpalData.cpp:723
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
bool warn
Warn flag.
Definition: Options.cpp:10
void setOrbit(const FVector< double, 6 > orbit)
Reset the current orbit.
bool getBool(const Attribute &attr)
Return logical value.
Definition: Attributes.cpp:66
void listCorrectors(bool list, int plane)
List correctors before or after correction.
static OpalData * getInstance()
Definition: OpalData.cpp:209
Class ThreadBpm.
Definition: ThreadBpm.h:29
static BeamSequence * find(const std::string &name)
Find a BeamSequence by name.
A templated representation of a LU-decomposition.
Definition: FLUMatrix.h:42
void listMonitors(bool list, int plane)
List monitors before or after correction.
virtual ThreadBpm * clone(const std::string &name)
Make clone.
Definition: ThreadBpm.cpp:65
TLine itsLine
The flat beam line on which the correction is done.
static Beam * find(const std::string &name)
Find named BEAM.
Definition: Beam.cpp:150
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
virtual Beamline * fetchLine() const =0
Return the embedded CLASSIC beam line.
ThreadBpm()
Exemplar constructor.
Definition: ThreadBpm.cpp:41
Track closed orbit.
Definition: OrbitTracker.h:37
virtual void execute()
Apply the algorithm to the top-level beamline.
Definition: Flatten.h:92
bool isMoni[2]
Flag telling wether a monitor exists.
PartData reference
The particle reference data.
Representation of a range within a beam line or sequence.
Definition: RangeRep.h:34
FVector< double, 6 > orbitGuess
The closed orbit guess.
The base class for all OPAL beam lines and sequences.
Definition: BeamSequence.h:32
OwnPtr< OrbitTracker > itsTracker
Definition: ThreadBpm.h:68
Domain error exception.
Definition: DomainError.h:32
const PartData & getReference() const
Return the embedded CLASSIC PartData.
Definition: Beam.cpp:183
The BEAM definition.
Definition: Beam.h:35
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:56
const std::string name
const FVector< double, 6 > & getOrbit() const
Return the current orbit.
std::string::iterator iterator
Definition: MSLang.h:16
virtual ~ThreadBpm()
Definition: ThreadBpm.cpp:61
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
bool isCorr[2]
Flags telling wether a corrector exists.
void setupTables()
Set up the corrector and monitor tables.
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:205
void addKick(int plane, Row &, double kick)
Add to kicker strength.
virtual void execute()
Check validity of the table definition.
Definition: ThreadBpm.cpp:70
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307