OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Eigen.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Eigen.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2.4.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: Eigen
10 // The class for OPAL EIGEN commands.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2002/12/09 15:06:08 $
15 // $Author: jsberg $
16 //
17 // ------------------------------------------------------------------------
18 
19 #include "Tables/Eigen.h"
22 #include "Attributes/Attributes.h"
23 #include "Tables/Twiss.h"
25 
26 #include <fstream>
27 #include <iomanip>
28 #include <sstream>
29 #include <iostream>
30 
31 
32 // Class Eigen
33 // ------------------------------------------------------------------------
34 
35 // The attributes of class Eigen.
36 namespace {
37  enum {
38  TABLE, // The name of the table to be listed.
39  FNAME, // The name of the file to be written.
40  SIZE
41  };
42 }
43 
44 
46  Action(SIZE, "EIGEN",
47  "The \"EIGEN\" statement lists the eigenvectors for a named "
48  "\"TWISS\" table.") {
50  ("TABLE", "Name of table to be listed");
52  ("FILE", "Name of file to receive output", "EIGEN");
53 
55 }
56 
57 
58 Eigen::Eigen(const std::string &name, Eigen *parent):
59  Action(name, parent)
60 {}
61 
62 
64 {}
65 
66 
67 Eigen *Eigen::clone(const std::string &name) {
68  return new Eigen(name, this);
69 }
70 
71 
73  std::string tableName = Attributes::getString(itsAttr[TABLE]);
74  Twiss *table = dynamic_cast<Twiss *>(OpalData::getInstance()->find(tableName));
75 
76  if(table) {
77  std::string fileName = Attributes::getString(itsAttr[FNAME]);
78  if(fileName == "TERM") {
79  format(std::cout, table);
80  } else {
81  std::ofstream os(fileName.c_str());
82 
83  if(os.good()) {
84  format(os, table);
85  } else {
86  throw OpalException("Eigen::execute()",
87  "Unable to open output stream \"" +
88  fileName + "\".");
89  }
90  }
91  } else {
92  throw OpalException("Eigen::execute()",
93  "Twiss table \"" + tableName + "\" not found.");
94  }
95 }
96 
97 
98 void Eigen::format(std::ostream &os, const Twiss *table) {
99  formatPrint(os, table);
100 }
101 
102 
103 void Eigen::formatPrint(std::ostream &os, const Twiss *table) const {
104  // Save the formatting flags.
105  std::streamsize old_prec = os.precision(6);
106  os.setf(std::ios::fixed, std::ios::floatfield);
107 
108  // Print table specific header.
109  table->printTableTitle(os, "Eigenvectors");
110  os << std::string(118, '-') << '\n';
111  os << "Element" << std::string(24, ' ') << "S Orbit |"
112  << std::string(25, ' ') << "E i g e n v e c t o r s\n";
113  os << std::string(118, '-') << '\n';
114 
115  // Print table body.
116  for(Twiss::TLine::const_iterator row = table->begin();
117  row != table->end(); ++row) {
118  if(row->getSelectionFlag()) {
119  os << '\n';
120  std::string name = row->getElement()->getName();
121  if(int occur = row->getCounter()) {
122  std::ostringstream tos;
123  tos << name << '[' << occur << ']' << std::ends;
124  name = tos.str();
125  }
126 
127  if(name.length() > 16) {
128  // Truncate the element name.
129  os << std::string(name, 0, 13) << ".. ";
130  } else {
131  // Left adjust the element name.
132  os << name << std::string(16 - name.length(), ' ');
133  }
134  os << std::setw(16) << table->getS(*row);
135 
136  FMatrix<double, 6, 6> eigen = table->getCurlyA(*row);
137  FVector<double, 6> orbit = table->getOrbit(*row);
138  for(int i = 0; i < 6; ++i) {
139  if(i != 0) os << std::string(32, ' ');
140  os << std::setw(12) << orbit[i] << " |";
141  for(int j = 0; j < 6; ++j) {
142  os << std::setw(12) << eigen[i][j];
143  }
144  os << '\n';
145  }
146  }
147  }
148 
149  os << std::string(118, '-') << std::endl;
150 
151  // Restore the formatting flags.
152  os.precision(old_prec);
153  os.setf(std::ios::fixed, std::ios::floatfield);
154 }
The EIGEN command.
Definition: Eigen.h:32
Eigen()
Exemplar constructor.
Definition: Eigen.cpp:45
TLine::const_iterator end() const
Access to last row.
Definition: Twiss.cpp:561
The base class for all OPAL actions.
Definition: Action.h:30
TLine::const_iterator begin() const
Access to first row.
Definition: Twiss.cpp:551
virtual ~Eigen()
Definition: Eigen.cpp:63
The base class for all OPAL exceptions.
Definition: OpalException.h:28
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
static OpalData * getInstance()
Definition: OpalData.cpp:209
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
Class Twiss.
Definition: Twiss.h:41
int precision() const
Definition: Inform.h:115
void formatPrint(std::ostream &, const Twiss *) const
Print Twiss table in eigenvector representation.
Definition: Eigen.cpp:103
void format(std::ostream &, const Twiss *)
Definition: Eigen.cpp:98
Object * find(const std::string &name)
Find entry.
Definition: OpalData.cpp:618
virtual Eigen * clone(const std::string &name)
Make clone.
Definition: Eigen.cpp:67
const std::string name
void printTableTitle(std::ostream &, const char *title) const
Print standard information about the TWISS table.
Definition: Twiss.cpp:847
FMatrix< double, 6, 6 > getCurlyA() const
Return initial curly A matrix.
Definition: Twiss.cpp:669
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
virtual void execute()
Execute the command.
Definition: Eigen.cpp:72
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307