OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
MakeSequence.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $CVSfile: MakeSequence.cc,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: MakeSequence
10 // The class for the OPAL MAKESEQ command.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2001/08/13 15:22:04 $
15 // $Author: jowett $
16 //
17 // ------------------------------------------------------------------------
18 
20 #include "AbsBeamline/Drift.h"
24 #include "AbstractObjects/Object.h"
28 #include "Attributes/Attributes.h"
31 #include "Utilities/Options.h"
32 #include <fstream>
33 
34 extern Inform *gmsg;
35 
36 // Class MakeSequence
37 // ------------------------------------------------------------------------
38 
39 namespace MakeSequenceNS {
40 
41  // Functor for flagging an object.
43  virtual void operator()(Object *) const;
44  };
45 
46  void ObjectFlagger::operator()(Object *object) const {
47  // Only output objects which have a parent, and which are not built-in.
48  object->setFlag(object->getParent() != 0 && ! object->isBuiltin());
49  }
50 
51  // Functor for saving an element.
53  ElementWriter(std::ostream &ostr): os(ostr) { }
54  virtual void operator()(Object *) const;
55  private:
56  std::ostream &os;
57  };
58 
59  void ElementWriter::operator()(Object *object) const {
60  if(object->isFlagged() && dynamic_cast<Element *>(object) &&
61  ! dynamic_cast<BeamSequence *>(object)) {
62  if(object->getOpalName()[0] != '#') {
63  (*this)(object->getParent());
64  os << object;
65  }
66  object->setFlag(false);
67  }
68  }
69 
70  // Functor for saving a variable.
72  VariableWriter(std::ostream &ostr): os(ostr) { }
73  virtual void operator()(Object *) const;
74  private:
75  std::ostream &os;
76  };
77 
78  void VariableWriter::operator()(Object *object) const {
79  if(object->isFlagged() && dynamic_cast<ValueDefinition *>(object)) {
80  os << object;
81  object->setFlag(false);
82  }
83  }
84 
85  // Visitor class for writing the sequence.
87  public:
88  // Construction/destruction.
89  SequenceWriter(const Beamline &beamline, const std::string &name,
90  std::ostream &os);
91  virtual ~SequenceWriter();
92 
93  // Override.
94  virtual void execute();
95 
96  // Visit drift; must override to do nothing.
97  virtual void visitDrift(const Drift &);
98 
99  protected:
100  // Apply the default to an element.
101  // All visitXXX() methods use applyDefault() which is overridden here.
102  virtual void applyDefault(const ElementBase &element);
103 
104  private:
105  // The sequence name.
106  std::string itsName;
107 
108  // The output stream to be written.
109  std::ostream &itsStream;
110 
111  // The accumulated length.
112  double sum_length;
113  };
114 
115  SequenceWriter::SequenceWriter(const Beamline &beamline, const std::string &name,
116  std::ostream &os):
117  DefaultVisitor(beamline, false, false),
118  itsName(name), itsStream(os), sum_length(0.0)
119  {}
120 
122  std::string comment = "// ";
123  std::string line(72, '-');
124  itsStream << comment << line << '\n'
125  << comment << "Sequence definition.\n"
126  << comment << line << '\n'
127  << itsName << ":SEQUENCE,REFER=CENTRE";
128  itsStream << ",L=" << sum_length << ";\n";
129 
130  sum_length = 0.0;
132  itsStream << "ENDSEQUENCE;" << std::endl;
133 
134  }
135 
137  {}
138 
139  void SequenceWriter::visitDrift(const Drift &drift) {
140  sum_length += drift.getElementLength();
141  }
142 
144  std::string objectName = element.getName();
145  if(objectName[0] != '#') {
146  Element *elem = Element::find(objectName);
148  itsStream << " " << objectName << ",AT=" << sum_length;
149  itsStream << ";\n";
150  sum_length += elem->getExit(Element::IS_CENTRE);
151  }
152  }
153 
154  // The attributes of class MakeSequence.
155  enum {
156  LINE, // The lattice to be used.
157  NAME, // The name for the new sequence.
158  FNAME, // The file to be written.
160  };
161 }
162 
163 using namespace MakeSequenceNS;
164 
166  Action(SIZE, "MAKESEQ",
167  "The \"MAKESEQ\" statement constructs a flat sequence from a "
168  "\"LINE\" object.") {
170  ("LINE", "Name of the lattice to be flattened");
172  ("NAME",
173  "Name to be given to the generated seqence (default = original name).");
175  ("FILE",
176  "Name to be given to the generated file (default = new sequence name).");
177 
179 }
180 
181 
182 MakeSequence::MakeSequence(const std::string &name, MakeSequence *parent):
183  Action(name, parent)
184 {}
185 
186 
188 {}
189 
190 
191 MakeSequence *MakeSequence::clone(const std::string &name) {
192  return new MakeSequence(name, this);
193 }
194 
195 
197  // Get relevant names.
198  std::string useName = Attributes::getString(itsAttr[LINE]);
199  std::string name = Attributes::getString(itsAttr[NAME]);
200  std::string file = Attributes::getString(itsAttr[FNAME]);
201  if(name.empty()) name = useName;
202  if(file.empty()) file = name;
203 
204  // Find BeamSequence definition.
205  BeamSequence *use = BeamSequence::find(useName);
206 
207  // Open the output stream.
208  std::ofstream os(file.c_str());
209  if(os.bad()) {
210  throw OpalException("MakeSequence::execute()",
211  "Unable to open output stream \"" + file + "\".");
212  }
213  os.precision(12);
214  std::string line(72, '-');
215  std::string comment = "// ";
216 
217  // Flag all objects which should be output.
219 
220  // Write all variables.
221  os << comment << line << '\n'
222  << comment << "Variable definitions." << '\n'
223  << comment << line << '\n';
225  os << '\n';
226 
227  // Write all elements.
228  os << comment << line << '\n'
229  << comment << "Element definitions." << '\n'
230  << comment << line << '\n';
232  os << std::endl;
233 
234  // Write the sequence.
235 
236  SequenceWriter writer(*use->fetchLine(), name, os);
237  writer.execute();
238  os.close();
239 }
virtual double getExit(ReferenceType) const
Return arc length from origin to exit (positive !).
Definition: Element.cpp:78
bool isFlagged() const
True, if [b]this[/b] is flagged by setFlag(true).
Definition: Object.cpp:273
VariableWriter(std::ostream &ostr)
ElementWriter(std::ostream &ostr)
MakeSequence()
Exemplar constructor.
virtual MakeSequence * clone(const std::string &name)
Make clone.
Interface for basic beam line object.
Definition: ElementBase.h:128
virtual void execute()
Apply the algorithm to the top-level beamline.
The base class for all OPAL actions.
Definition: Action.h:30
bool isBuiltin() const
True, if [b]this[/b] is a built-in object.
Definition: Object.cpp:242
The base class for all OPAL exceptions.
Definition: OpalException.h:28
virtual ~MakeSequence()
virtual void operator()(Object *) const
The function to be executed.
virtual void operator()(Object *) const
The function to be executed.
virtual void visitDrift(const Drift &)
Apply the algorithm to a drift.
Object * getParent() const
Return parent pointer.
Definition: Object.cpp:289
Inform * gmsg
Definition: Main.cpp:21
virtual const std::string & getName() const
Get element name.
Definition: ElementBase.cpp:95
Interface for drift space.
Definition: Drift.h:33
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
Default algorithms.
void apply(const ObjectFunction &)
Apply a function to all objects.
Definition: OpalData.cpp:516
static OpalData * getInstance()
Definition: OpalData.cpp:209
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:284
static BeamSequence * find(const std::string &name)
Find a BeamSequence by name.
The base class for all OPAL elements.
Definition: Element.h:46
The MAKESEQ command.
Definition: MakeSequence.h:28
virtual void execute()
Execute the command.
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:511
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
virtual Beamline * fetchLine() const =0
Return the embedded CLASSIC beam line.
virtual void applyDefault(const ElementBase &element)
virtual void operator()(Object *) const
The function to be executed.
An abstract sequence of beam line components.
Definition: Beamline.h:37
virtual void execute()
Apply the algorithm to the top-level beamline.
The base class for all OPAL beam lines and sequences.
Definition: BeamSequence.h:32
static Element * find(const std::string &name)
Find named Element.
Definition: Element.cpp:37
The base class for all OPAL objects.
Definition: Object.h:48
SequenceWriter(const Beamline &beamline, const std::string &name, std::ostream &os)
The base class for all OPAL value definitions.
const std::string name
Abstract base class for functor objects whose argument is an Object.
Definition: Inform.h:41
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
void setFlag(bool)
Flag/unflag this object, e. g. to control output of objects for.
Definition: Object.cpp:268
virtual double getEntrance(ReferenceType) const
Return arc length from origin to entrance (negative !).
Definition: Element.cpp:63
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307