OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Period.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Period.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2.4.2 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: Period
10 // The class for the OPAL TWISS command.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2004/11/12 20:10:11 $
15 // $Author: adelmann $
16 //
17 // ------------------------------------------------------------------------
18 
19 #include "Tables/Period.h"
20 #include "Algorithms/IdealMapper.h"
21 #include "Algorithms/Mapper.h"
22 #include "Attributes/Attributes.h"
26 #include "FixedAlgebra/FStaticFP.h"
27 #include "FixedAlgebra/LinearFun.h"
28 #include "FixedAlgebra/LinearMap.h"
29 #include "FixedAlgebra/FTps.h"
30 #include "FixedAlgebra/FVps.h"
31 #include "Physics/Physics.h"
32 #include "Structure/Beam.h"
34 #include "Utilities/Options.h"
35 #include "Utilities/Round.h"
36 #include <cmath>
37 #include <iomanip>
38 #include <iostream>
39 using namespace std;
40 using std::setw;
41 
42 
43 // Class Period
44 // ------------------------------------------------------------------------
45 
47  Twiss(SIZE, "TWISS",
48  "The \"TWISS\" command defines a table of lattice functions\n"
49  "which can be matched or tabulated over a periodic line.") {
51  ("MICADO", "Number of iterations for MICADO algorithm", 0.0);
53  ("CORRECTORS", "Number of correctors for MICADO algorithm", 0.0);
55  ("THREAD", "Name of method for closed orbit threader");
57  ("TOLQ", "Tolerance for positions in closed orbit threader", 1.0e-3);
59  ("TOLP", "Tolerance for momenta in closed orbit threader", 1.0e-3);
60 
61  // READ ONLY.
63  ("CIRCUM", "Circumference in m");
64  itsAttr[CIRCUM].setReadOnly(true);
65 
67  ("FREQ0", "Revolution frequency in Hz");
68  itsAttr[FREQ].setReadOnly(true);
69 
71  ("Q1", "Tune for mode 1");
72  itsAttr[Q1].setReadOnly(true);
73 
75  ("Q2", "Tune for mode 2");
76  itsAttr[Q2].setReadOnly(true);
77 
79  ("Q3", "Tune for mode 3");
80  itsAttr[Q3].setReadOnly(true);
81 
83  ("U0", "Energy loss per turn in MeV");
84  itsAttr[U0].setReadOnly(true);
85 
87  ("J1", "Damping partition number for mode 1");
88  itsAttr[J1].setReadOnly(true);
89 
91  ("J2", "Damping partition number for mode 2");
92  itsAttr[J2].setReadOnly(true);
93 
95  ("J3", "Damping partition number for mode 3");
96  itsAttr[J3].setReadOnly(true);
97 
99  ("DELTAP", "Differential momentum variation");
100  itsAttr[DELTAP].setReadOnly(true);
101 
103 }
104 
105 
106 Period::Period(const std::string &name, Period *parent):
107  Twiss(name, parent)
108 {}
109 
110 
112 {}
113 
114 
115 Period *Period::clone(const std::string &name) {
116  return new Period(name, this);
117 }
118 
119 
120 void Period::fill() {
121  //std::cerr << "==> In Period::fill()..." << std::endl;
122  if(refill) {
123  // Set truncation order.
124  //DTA: FTps<double,6>::setGlobalTruncOrder(order);
125 
126  // Search for closed orbit.
127  findClosedOrbit();
128  //std::cerr << "Closed orbit \"found\"" << std::endl;
129 
130  // Now map is the map around the fixed point.
131  FVps<double, 6> map;
132  itsMapper->getMap(map);
133  orbit = map.constantTerm();
134  //std::cerr << " co = " << orbit << std::endl;
135  //std::cerr << " map = " << map << std::endl;
136  for(int i = 0; i < 6; ++i) map[i][0] = 0.0;
137  FTps<double, 6> A_lie;
138  FVps<double, 6> A_scr;
139  if(map[5] == FTps<double, 6>::makeVariable(5)) {
140  // Map is static.
141  //std::cerr << " static map = " << map << std::endl;
142  FStaticFP<6> fix(map);
143  //std::cerr << " fix(map) constructed" << std::endl;
144  map = fix.getFixedPointMap();
145  //std::cerr << " got fixedPt map --> " << map << std::endl;
146  const FNormalForm<6> normal(map);
147  A_lie = normal.normalisingMap();
148  //std::cerr << " got Lie form of normalising map --> " << A_lie << std::endl;
149  A_scr = FVps<double, 6>(normal.eigenVectors());
150  //std::cerr << " got matrix form of normalising map --> " << A_scr << std::endl;
151  for(int i = order; i >= 3; --i) {
152  A_scr = ExpMap(A_lie.filter(i, i), A_scr);
153  //std::cerr << " A_scr(" << i << ") --> " << A_scr << std::endl;
154  }
155  A_scr = fix.getFixedPoint().substitute(A_scr) + fixPoint;
156  //std::cerr << " A_scr(-1) --> " << A_scr << std::endl;
157  } else {
158  // Map is dynamic; fixed point is already known.
159  const FNormalForm<6> normal(map);
160  A_lie = normal.normalisingMap();
161  A_scr = FVps<double, 6>(normal.eigenVectors());
162  for(int i = order; i >= 3; --i) {
163  A_scr = ExpMap(A_lie.filter(i, i), A_scr);
164  }
165  A_scr += fixPoint;
166 
167  if(Options::warn) {
168  std::cerr << "\n### Warning ### Momentum is not constant, "
169  << "Twiss is three-dimensional.\n" << std::endl;
170  }
171  }
172 
173  // Initialise mapper, clear local data, and fill table.
174  curly_A = A_scr.linearTerms();
176  put();
177 
178  // Fill in the read-only data.
179  const Row &row = itsTable->back();
180  double arc = getS(row);
186 
187  // Fill is complete.
188  refill = false;
189  }
190  //std::cerr << "==> Leaving Period::fill()" << std::endl;
191 }
192 
193 
194 void Period::printTable(std::ostream &os, const CellArray &cells) const {
195  // Save the formatting flags.
196  std::streamsize old_prec = os.precision(6);
197  os.setf(std::ios::fixed, std::ios::floatfield);
198 
199  // Print table header.
200  printTableTitle(os, "Periodic lattice functions");
201 
202  // Print table body.
203  printTableBody(os, cells);
204 
205  // Write table specific summary.
206  const Row &row = itsTable->back();
207  os << "Period length = " << setw(16)
209  << " Qx = " << setw(16) << getMUi(row, 0)
210  << " Qy = " << setw(16) << getMUi(row, 1)
211  << '\n'
212  << "DeltaP = " << setw(16)
214  << " BetaX(max) = " << setw(16)
216  << " BetaY(max) = " << setw(16)
217  << Attributes::getReal(itsAttr[BETYMAX]) << '\n'
218  << " "
219  << " x(max) = " << setw(16)
221  << " y(max) = " << setw(16)
222  << Attributes::getReal(itsAttr[YCMAX]) << '\n'
223  << " "
224  << " x(rms) = " << setw(16)
226  << " y(rms) = " << setw(16)
227  << Attributes::getReal(itsAttr[YCRMS]) << '\n'
228  << " "
229  << " Dx(max) = " << setw(16)
231  << " Dy(max) = " << setw(16)
232  << Attributes::getReal(itsAttr[DYMAX]) << '\n'
233  << " "
234  << " Dx(rms) = " << setw(16)
236  << " Dy(rms) = " << setw(16)
237  << Attributes::getReal(itsAttr[DYRMS]) << '\n';
238 
239  // Restore the formatting flags.
240  os.precision(old_prec);
241  os.setf(std::ios::fixed, std::ios::floatfield);
242 }
243 
244 
246  //std::cerr << "==> In Period::findClosedOrbit()" << std::endl;
247  static const int iteration_limit = 20;
248  static const double itsTolerance = 1.0e-8;
250  double deltap = Attributes::getReal(itsAttr[DELTAP]);
251 
252  // Initialize fixPoint.
253  fixPoint[0] = 0.e0;
254  fixPoint[1] = 0.e0;
255  fixPoint[2] = 0.e0;
256  fixPoint[3] = 0.e0;
257  fixPoint[4] = 0.e0;
258  fixPoint[5] = deltap;
259  //fixPoint[5]=0.e0;
260  //std::cerr << " [findCO:] fixPoint =" << fixPoint << std::endl;
261 
262  double error = 0.0;
263  for(int count = 0; count < iteration_limit; ++count) {
264  //std::cerr << " [findCO:] count = " << count << std::endl;
265  // Initial guess for closed orbit.
266  LinearMap<double, 6> identity;
267  LinearMap<double, 6> mapAtEnd;
268 
269  // Compute the one-turn map around the closed orbit.
270  //std::cerr << " [findCO:] computing map about fixPoint = " << fixPoint << std::endl;
271  itsMapper->setMap(identity + fixPoint);
272  itsMapper->execute();
273  itsMapper->getMap(mapAtEnd);
274  //std::cerr << " [findCO:] have map about fixPoint" << std::endl;
275 
276  // Get system of equations for fixed point.
277  FMatrix<double, 6, 6> A = mapAtEnd.linearTerms();
278  FVector<double, 6> Error = mapAtEnd.constantTerm() - fixPoint;
279  double errold = error;
280  error = 0.0;
281 
282  //std::cerr << " [findCO:] count " << count << ": Error =\n {";
283  //for(int i=0;i<6;i++) std::cerr << " " << Error(i);
284  //std::cerr << " }" << std::endl;
285  //std::cerr << " [findCO:] A =\n" << A << std::endl;
286 
287  //std::cerr << " [findCO:] finding fixed point ";
288  if(mapAtEnd[5] == nrgy + deltap) {
289  // Finding static fixed point.
290  //std::cerr << "for static map ...\n" << std::endl;
291  for(int i = 0; i < 4; i++) {
292  A(i, i) -= 1.0;
293  if(abs(Error(i)) > error) error = abs(Error(i));
294  }
295  for(int i = 4; i < 6; i++) {
296  for(int j = 0; j < 6; j++) A(i, j) = A(j, i) = 0.0;
297  A(i, i) = 1.0;
298  Error(i) = 0.0;
299  }
300  } else {
301  // Finding dynamic fixed point.
302  //std::cerr << "for dynamic map ...\n" << std::endl;
303  for(int i = 0; i < 6; i++) {
304  A(i, i) -= 1.0;
305  if(abs(Error(i)) > error) error = abs(Error(i));
306  }
307  }
308 
309  //std::cerr << " [findCO:] count " << count << ": Error =\n {";
310  //for(int i=0;i<6;i++) std::cerr << " " << Error(i);
311  //std::cerr << " }" << std::endl;
312  //std::cerr << " [findCO:] A =\n" << A << std::endl;
313 
314  // Correction for fixed point.
315  FLUMatrix<double, 6> lu(A);
316  //std::cerr << " [findCO:] have lu(A)" << std::endl;
317  lu.backSubstitute(Error);
318  //std::cerr << " [findCO:] backSub done" << std::endl;
319  fixPoint -= Error;
320  //std::cerr << " [findCO:] count errold error" << count << errold << error << std::endl;
321  // return if the error vanishes or the error has fallen below some crude
322  // tolerance (machineEps^(1/2)) and bounced (because of round-off error)
323  if(count && (error == 0.0 || (error < itsTolerance && error >= errold))) break;
324  //if (error<itsTolerance) break;
325  //std::cerr << " [findCO:] again" << std::endl;
326  }
327  //std::cerr << " [findCO:] fixPoint = " << fixPoint << std::endl;
328  //std::cerr << "==> Leaving Period::findClosedOrbit()" << std::endl;
329 }
void setReal(Attribute &attr, double val)
Set real value.
Definition: Attributes.cpp:236
std::vector< Cell > CellArray
An array of cell descriptors.
Definition: Table.h:63
virtual void fill()
Fill the buffer using the defined algorithm.
Definition: Period.cpp:120
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
bool refill
Refill flag.
Definition: Table.h:158
Static fixed point of a Truncated power series map.
Definition: FStaticFP.h:32
constexpr double e
The value of .
Definition: Physics.h:40
virtual void printTable(std::ostream &, const CellArray &) const
Print the table on an ASCII stream.
Definition: Period.cpp:194
virtual ~Period()
Definition: Period.cpp:111
Period()
Exemplar constructor.
Definition: Period.cpp:46
The TWISS command.
Definition: Period.h:28
virtual Period * clone(const std::string &name)
Make clone.
Definition: Period.cpp:115
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
double getBeta() const
The relativistic beta per particle.
Definition: PartData.h:127
static LinearFun makeVariable(int var)
Make variable.
Definition: LinearFun.hpp:91
bool warn
Warn flag.
Definition: Options.cpp:10
const FVps< double, N > & getFixedPoint() const
Get the transformation to the fixed point.
Definition: FStaticFP.h:199
const FMatrix< double, N, N > & eigenVectors() const
Get eigenvectors of the linear part in packed form.
Definition: FNormalForm.h:380
TLine * itsTable
Definition: Twiss.h:290
void printTableBody(std::ostream &, const CellArray &) const
Print the body to this TWISS table.
Definition: Twiss.cpp:786
FMatrix< double, 6, 6 > curly_A
The initial curly A matrix.
Definition: Twiss.h:272
double getMUi(const Row &, int i1, int=0) const
Three modes, &quot;naive&quot; Twiss functions.
Definition: Twiss.cpp:1106
FVector< T, N > constantTerm(const FVector< T, N > &P) const
Evaluate map at point [b]P[/b].
Definition: LinearMap.hpp:220
A templated representation of a LU-decomposition.
Definition: FLUMatrix.h:42
FVector< T, N > constantTerm() const
Extract the constant part of the map.
Definition: FVps.hpp:540
FVector< double, 6 > fixPoint
Definition: Period.h:59
const FVps< double, N > & getFixedPointMap() const
Get the map around the fixed point.
Definition: FStaticFP.h:205
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
const FTps< double, N > & normalisingMap() const
Get normalising map as a Lie transform.
Definition: FNormalForm.h:368
void backSubstitute(FVector< T, N > &B) const
Back substitution.
Definition: FLUMatrix.h:222
FVps< T, N > ExpMap(const FTps< T, N > &H, int trunc=FTps< T, N >::EXACT)
Build the exponential series.
Definition: FTps.hpp:1994
FVector< double, 6 > orbit
The initial closed orbit.
Definition: Twiss.h:269
virtual void setMap(const LinearMap< double, 6 > &)=0
Reset the linear part of the accumulated map for restart.
Class Twiss.
Definition: Twiss.h:41
Normal form of a truncated Taylor series map.
Definition: FNormalForm.h:51
virtual void execute()
Apply the algorithm to the top-level beamline.
FMatrix< T, N, N > linearTerms() const
Extract the linear part of the map.
Definition: FVps.hpp:561
Structure for a row of the Twiss table.
Definition: Twiss.h:49
FTps filter(int minOrder, int maxOrder, int trcOrder=EXACT) const
Extract given range of orders, with truncation.
Definition: FTps.hpp:451
const PartData * reference
Definition: Twiss.h:299
AbstractMapper * itsMapper
Definition: Twiss.h:293
const std::string name
FMatrix< T, N, N > linearTerms() const
Extract linear terms at origin.
Definition: LinearMap.hpp:243
int order
Definition: Twiss.h:302
void printTableTitle(std::ostream &, const char *title) const
Print standard information about the TWISS table.
Definition: Twiss.cpp:847
Linear function in N variables of type T.
Definition: LinearFun.h:39
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
FVps substitute(const FMatrix< T, N, N > &M, int n) const
Substitute.
Definition: FVps.hpp:608
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
void findClosedOrbit()
Definition: Period.cpp:245
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:205
double getS(const Row &, int=0, int=0) const
Arc length for given row.
Definition: Twiss.cpp:1101
virtual void getMap(LinearMap< double, 6 > &) const =0
Return the linear part of the accumulated map.
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
void put()
Definition: Twiss.cpp:878