OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
ThreadAll.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: ThreadAll.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2.4.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: ThreadAll
10 // The abstract class ThreadAll 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/ThreadAll.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 ThreadAll
39 // ------------------------------------------------------------------------
40 
42  CorrectionBase(SIZE, "THREADALL",
43  "The \"THREADALL\" command threads the closed orbit "
44  "using the position and angle at all positions.") {
46  ("TOLQ", "The tolerance for the closed orbit positions.");
48  ("TOLP", "The tolerance for the closed orbit angles.");
50  ("LISTM", "List the monitors after correction");
52  ("LISTC", "List the correctors after correction");
53 
55 }
56 
57 
58 ThreadAll::ThreadAll(const std::string &name, ThreadAll *parent):
59  CorrectionBase(name, parent)
60 {}
61 
62 
64 {}
65 
66 
67 ThreadAll *ThreadAll::clone(const std::string &name) {
68  return new ThreadAll(name, this);
69 }
70 
71 
73  // Find Table definition.
74  const std::string &lineName = Attributes::getString(itsAttr[LINE]);
75  BeamSequence *use = BeamSequence::find(lineName);
76 
77  // Find Beam data.
78  const std::string &beamName = Attributes::getString(itsAttr[BEAM]);
79  Beam *beam = Beam::find(beamName);
80  reference = beam->getReference();
81 
82  // Get the data for correction.
84  double tolp = Attributes::getReal(itsAttr[TOLP]);
85  double tolq = Attributes::getReal(itsAttr[TOLQ]);
86  bool listc = Attributes::getBool(itsAttr[LISTC]);
87  bool listm = Attributes::getBool(itsAttr[LISTM]);
88 
89  // Make sure all is up-to-date.
91 
92  // Create flat list with space for data storage.
93  Flatten<Row> flattener(*use->fetchLine(), itsLine, range);
94  flattener.execute();
95 
96  if(itsLine.empty() && Options::warn) {
97  std::cerr << "\n### Warning ### Lattice function table \""
98  << lineName << "\" contains no elements.\n" << std::endl;
99  return;
100  }
101 
102  // Set up the tables of correctors and monitors.
103  setupTables();
104 
105  // Start point for threading.
106  itsTracker = new OrbitTracker(itsLine, reference, false, false);
108  static const int iteration_limit = 100;
109  int count = 0;
110  TLine::iterator iter = itsLine.begin();
111 
112  while(iter != itsLine.end()) {
113  // Advance through element.
114  try {
115  iter->accept(*itsTracker);
116 
117  // Test for overflow in current element.
119  iter->orbit = orbit;
120 
121  if(! iter->isUsed[0] &&
122  (std::abs(orbit[0]) > tolq || std::abs(orbit[1]) > tolp)) {
123  // Overflow detected in x.
124  if(count++ > iteration_limit) break;
125  correct(0, iter);
126  } else if(! iter->isUsed[1] &&
127  (std::abs(orbit[2]) > tolq || std::abs(orbit[3]) > tolp)) {
128  // Overflow detected in y.
129  if(count++ > iteration_limit) break;
130  correct(1, iter);
131  } else {
132  ++iter;
133  }
134  } catch(const DomainError &) {
135  // Domain error detected;
136  // attempt to correct in the plane which has the larger deviation.
138  iter->orbit = orbit;
139 
140  if(std::abs(orbit[1]) >= std::abs(orbit[3])) {
141  correct(0, iter);
142  } else {
143  correct(1, iter);
144  }
145  }
146  }
147 
148  std::cout << "After threading with \"THREADALL\":\n";
149  for(int plane = 0; plane < 2; ++plane) {
150  listCorrectors(listc, plane);
151  listMonitors(listm, plane);
152  }
153 }
154 
155 
156 void ThreadAll::correct(int plane, TLine::iterator &iter) {
157  // Common information.
158  int p1 = 2 * plane;
159  int p2 = 2 * plane + 1;
160 
161  // Extract orbit and transfer matrix at current position.
162  FVector<double, 6> orbit = iter->orbit;
163  FMatrix<double, 6, 6> omat = iter->matrix;
164 
165  // Backtrack to find two correctors for the given plane.
166  TLine::iterator corr[2];
167  FMatrix<double, 6, 6> cmat[2];
168  int count = 2;
169 
170  while(iter != itsLine.begin()) {
171  // Backtrack and test for corrector.
172  --iter;
173  iter->isUsed[plane] = true;
174  test(iter->getElement());
175 
176  if(isCorr[plane]) {
177  // Extract transfer matrix to this corrector.
178  if(count > 0) --count;
179  corr[count] = iter;
180  cmat[count] = iter->matrix;
181 
182  if(count == 0) {
183  // We have found two correctors.
184  double r1 = orbit(p1);
185  double r2 = orbit(p2);
186 
187  double a11 =
188  omat(p1, 1) * cmat[0](p1, 0) - omat(p1, 0) * cmat[0](p1, 1) +
189  omat(p1, 3) * cmat[0](p1, 2) - omat(p1, 2) * cmat[0](p1, 3) +
190  omat(p1, 5) * cmat[0](p1, 4) - omat(p1, 4) * cmat[0](p1, 5);
191  double a12 =
192  omat(p1, 1) * cmat[1](p1, 0) - omat(p1, 0) * cmat[1](p1, 1) +
193  omat(p1, 3) * cmat[1](p1, 2) - omat(p1, 2) * cmat[1](p1, 3) +
194  omat(p1, 5) * cmat[1](p1, 4) - omat(p1, 4) * cmat[1](p1, 5);
195  double a21 =
196  omat(p2, 1) * cmat[0](p1, 0) - omat(p2, 0) * cmat[0](p1, 1) +
197  omat(p2, 3) * cmat[0](p1, 2) - omat(p2, 2) * cmat[0](p1, 3) +
198  omat(p2, 5) * cmat[0](p1, 4) - omat(p2, 4) * cmat[0](p1, 5);
199  double a22 =
200  omat(p2, 1) * cmat[1](p1, 0) - omat(p2, 0) * cmat[1](p1, 1) +
201  omat(p2, 3) * cmat[1](p1, 2) - omat(p2, 2) * cmat[1](p1, 3) +
202  omat(p2, 5) * cmat[1](p1, 4) - omat(p2, 4) * cmat[1](p1, 5);
203  double det = a11 * a22 - a12 * a21;
204 
205  if(std::abs(det) > 0.01) {
206  double k1 = - (a22 * r1 - a12 * r2) / det;
207  double k2 = - (a11 * r2 - a21 * r1) / det;
208 
209  // Store the kicks.
210  addKick(plane, *corr[0], k1);
211  addKick(plane, *corr[1], k2);
212 
213  // Reset to start before the first kicker.
214  --iter;
215  itsTracker->setOrbit(iter->orbit);
216  return;
217  }
218  }
219  }
220  }
221 
222  // No corrector pair found; change initial conditions.
223  FLUMatrix<double, 6>(omat).backSubstitute(orbit);
224  orbitGuess -= orbit;
226 }
Abstract base class for all orbit correction commands.
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
ThreadAll()
Exemplar constructor.
Definition: ThreadAll.cpp:41
RangeRep getRange(const Attribute &attr)
Get range value.
Definition: Attributes.cpp:177
T det(const AntiSymTenzor< T, 3 > &t)
OwnPtr< OrbitTracker > itsTracker
Definition: ThreadAll.h:70
virtual void execute()
Check validity of the table definition.
Definition: ThreadAll.cpp:72
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
virtual ~ThreadAll()
Definition: ThreadAll.cpp:63
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
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.
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.
Track closed orbit.
Definition: OrbitTracker.h:37
virtual void correct(int plane, TLine::iterator &)
Definition: ThreadAll.cpp:156
virtual void execute()
Apply the algorithm to the top-level beamline.
Definition: Flatten.h:92
virtual void accept(BeamlineVisitor &) const
Apply BeamlineVisitor to this line.
Definition: TBeamline.h:209
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.
Class ThreadAll.
Definition: ThreadAll.h:30
The base class for all OPAL beam lines and sequences.
Definition: BeamSequence.h:32
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
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
virtual ThreadAll * clone(const std::string &name)
Make clone.
Definition: ThreadAll.cpp:67
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.
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307