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