OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Twiss3.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Twiss3.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2.4.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: Twiss3
10 // The class for OPAL TWISS3 commands.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2002/12/09 15:06:08 $
15 // $Author: jsberg $
16 //
17 // ------------------------------------------------------------------------
18 
19 #include "Tables/Twiss3.h"
22 #include "Attributes/Attributes.h"
23 #include "Tables/Twiss.h"
25 #include <fstream>
26 #include <iomanip>
27 #include <sstream>
28 #include <iostream>
29 
30 using std::setw;
31 
32 
33 // Class Twiss3
34 // ------------------------------------------------------------------------
35 
36 // The attributes of class Twiss3.
37 namespace {
38  enum {
39  TABLE, // The name of the table to be listed.
40  FNAME, // The name of the file to be written.
41  SIZE
42  };
43 }
44 
45 
47  Action(SIZE, "TWISS3",
48  "The \"TWISS3\" statement lists a named \"TWISS\" table in "
49  "Mais-Ripken representation.") {
51  ("TABLE", "Name of table to be listed");
53  ("FILE", "Name of file to receive output", "TWISS3");
54 
56 }
57 
58 
59 Twiss3::Twiss3(const std::string &name, Twiss3 *parent):
60  Action(name, parent)
61 {}
62 
63 
65 {}
66 
67 
68 Twiss3 *Twiss3::clone(const std::string &name) {
69  return new Twiss3(name, this);
70 }
71 
72 
74  std::string tableName = Attributes::getString(itsAttr[TABLE]);
75  Twiss *table = dynamic_cast<Twiss *>(OpalData::getInstance()->find(tableName));
76 
77  if(table) {
78  std::string fileName = Attributes::getString(itsAttr[FNAME]);
79  if(fileName == "TERM") {
80  format(std::cout, table);
81  } else {
82  std::ofstream os(fileName.c_str());
83 
84  if(os.good()) {
85  format(os, table);
86  } else {
87  throw OpalException("Twiss3::execute()",
88  "Unable to open output stream \"" +
89  fileName + "\".");
90  }
91  }
92  } else {
93  throw OpalException("Twiss3::execute()",
94  "Twiss table \"" + tableName + "\" not found.");
95  }
96 }
97 
98 
99 void Twiss3::format(std::ostream &os, const Twiss *table) {
100  formatPrint(os, table);
101 }
102 
103 
104 void Twiss3::formatPrint(std::ostream &os, const Twiss *table) const {
105  // Save the formatting flags.
106  std::streamsize old_prec = os.precision(6);
107  os.setf(std::ios::fixed, std::ios::floatfield);
108 
109  // Write table specific header.
110  table->printTableTitle(os, "Mais-Ripken lattice functions");
111  os << std::string(124, '-') << '\n';
112  os << "Element" << std::string(24, ' ') << "S"
113  << std::string(10, ' ') << "XC" << std::string(9, ' ') << "PXC"
114  << std::string(10, ' ') << "YC" << std::string(9, ' ') << "PYC"
115  << std::string(10, ' ') << "TC" << std::string(9, ' ') << "PTC\n";
116  os << "Mode MU"
117  << " BETX GAMX ALFX"
118  << " BETY GAMY ALFY"
119  << " BETT GAMY ALFT\n";
120  os << std::string(124, '-') << '\n';
121  // Jumbled function names affecting PRINT style output
122  // fixed at 15:26:46 on 9 Aug 2000 by JMJ
123 
124  // Write table body.
125  for(Twiss::TLine::const_iterator row = table->begin();
126  row != table->end(); ++row) {
127  if(row->getSelectionFlag()) {
128  std::string name = row->getElement()->getName();
129  if(int occur = row->getCounter()) {
130  std::ostringstream tos;
131  tos << name << '[' << occur << ']' << std::ends;
132  name = tos.str();
133  }
134 
135  if(name.length() > 16) {
136  // Truncate the element name.
137  os << std::string(name, 0, 13) << ".. ";
138  } else {
139  // Left adjust the element name.
140  os << name << std::string(16 - name.length(), ' ');
141  }
142  os << setw(16) << table->getS(*row);
143 
144  FVector<double, 6> orbit = table->getOrbit(*row);
145  for(int i = 0; i < 6; ++i) {
146  os << setw(12) << orbit[i];
147  }
148  os << '\n';
149 
150  for(int mode = 0; mode < 3; ++mode) {
151  os << setw(4) << (mode + 1) << setw(12)
152  << table->getMUi(*row, mode);
153  for(int plane = 0; plane < 3; ++plane) {
154  os << setw(12) << table->getBETik(*row, plane, mode)
155  << setw(12) << table->getGAMik(*row, plane, mode)
156  << setw(12) << table->getALFik(*row, plane, mode);
157  }
158  os << '\n';
159  }
160  }
161  }
162 
163  os << std::string(124, '-') << std::endl;
164 
165  // Restore the formatting flags.
166  os.precision(old_prec);
167  os.setf(std::ios::fixed, std::ios::floatfield);
168 }
void formatPrint(std::ostream &, const Twiss *) const
Print Twiss table in Mais-Ripken representation.
Definition: Twiss3.cpp:104
TLine::const_iterator end() const
Access to last row.
Definition: Twiss.cpp:561
double getGAMik(const Row &, int i1, int i2) const
Mais-Ripken gamma functions.
Definition: Twiss.cpp:1210
The base class for all OPAL actions.
Definition: Action.h:30
TLine::const_iterator begin() const
Access to first row.
Definition: Twiss.cpp:551
The base class for all OPAL exceptions.
Definition: OpalException.h:28
double getBETik(const Row &, int i1, int i2) const
Mais-Ripken beta functions.
Definition: Twiss.cpp:1164
virtual void execute()
Execute the command.
Definition: Twiss3.cpp:73
The TWISS3 command.
Definition: Twiss3.h:32
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
static OpalData * getInstance()
Definition: OpalData.cpp:209
double getMUi(const Row &, int i1, int=0) const
Three modes, &quot;naive&quot; Twiss functions.
Definition: Twiss.cpp:1106
void format(std::ostream &, const Twiss *)
Definition: Twiss3.cpp:99
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
Class Twiss.
Definition: Twiss.h:41
virtual Twiss3 * clone(const std::string &name)
Make clone.
Definition: Twiss3.cpp:68
int precision() const
Definition: Inform.h:115
Object * find(const std::string &name)
Find entry.
Definition: OpalData.cpp:618
const std::string name
void printTableTitle(std::ostream &, const char *title) const
Print standard information about the TWISS table.
Definition: Twiss.cpp:847
Twiss3()
Exemplar constructor.
Definition: Twiss3.cpp:46
virtual ~Twiss3()
Definition: Twiss3.cpp:64
double getALFik(const Row &, int i1, int i2) const
Mais-Ripken alpha functions.
Definition: Twiss.cpp:1183
FVector< double, 6 > getOrbit() const
Return initial closed orbit.
Definition: Twiss.cpp:714
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
double getS(const Row &, int=0, int=0) const
Arc length for given row.
Definition: Twiss.cpp:1101
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307