OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
SetIntegrator.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: SetIntegrator.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Definitions for class: SetIntegrator
10 // The class for the OPAL SETINTEGRATOR command.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2002/03/28 21:27:54 $
15 // $Author: jsberg $
16 //
17 // ------------------------------------------------------------------------
18 
21 #include "AbsBeamline/Integrator.h"
22 #include "AbsBeamline/Multipole.h"
27 #include "Attributes/Attributes.h"
28 #include "Beamlines/Beamline.h"
30 #include "Utilities/Options.h"
31 #include <iostream>
32 
33 using std::cerr;
34 
35 
36 // Class SetIntegrator
37 // ------------------------------------------------------------------------
38 
39 namespace {
40 
41  // Visitor class Setter for attaching the integrators.
42  class Setter: public DefaultVisitor {
43 
44  public:
45  // Construction/destruction.
46  Setter(const Beamline &, const std::string &type, int slices);
47  virtual ~Setter();
48 
49  // Override method for integratorconst visit.
50  virtual void visitFlaggedElmPtr(const FlaggedElmPtr &);
51 
52  // Return error counts.
53  void getCounts(int &add, int &rem, int &rep) const;
54 
55  private:
56  // The type of integrator.
57  const std::string itsType;
58 
59  // The number of slices.
60  int itsSlices;
61 
62  // Keeps track of inserted integrators.
63  int added, removed, replaced;
64  };
65 
66 
67  Setter::Setter(const Beamline &beamline, const std::string &type, int slices):
68  DefaultVisitor(beamline, false, false),
69  itsType(type), itsSlices(slices), added(0), removed(0), replaced(0)
70  {}
71 
72 
73  Setter::~Setter()
74  {}
75 
76 
77  void Setter::visitFlaggedElmPtr(const FlaggedElmPtr &fep) {
78  // The pointer "base" points to the actual element or integrator.
79  ElementBase *temp = fep.getElement();
80  ElementBase *base = temp->removeAlignWrapper();
81 
82  // The pointer "wrap" points to an AlignWrapper, if it exist,
83  // otherwise it is NULL.
84  AlignWrapper *wrap = dynamic_cast<AlignWrapper *>(temp);
85 
86  if(dynamic_cast<Beamline *>(base)) {
87  // Enter beam line via the default action.
89  } else if(fep.getSelectionFlag()) {
90  // Single element is selected.
91  if(Integrator *i = dynamic_cast<Integrator *>(base)) {
92  // The embedded element should be a multipole.
93  if(Multipole *mult = dynamic_cast<Multipole *>(i->getElement())) {
94  // Replace/remove an existing integrator.
95  ElementBase *mpsi;
96  if(itsType == "NONE") {
97  // Going to link the multipole directly, without an integrator.
98  removed++;
99  mpsi = mult;
100  } else {
101  // Going to link the multipole with a new integrator.
102  replaced++;
103  mpsi = new MPSplitIntegrator(mult, itsSlices);
104  }
105  if(wrap) {
106  wrap->setElement(mpsi);
107  } else {
108  const_cast<FlaggedElmPtr &>(fep).setElement(mpsi);
109  }
110  }
111  } else if(Multipole *mult = dynamic_cast<Multipole *>(base)) {
112  // Going to wrap the multipole with a new integrator.
113  ElementBase *mpsi = new MPSplitIntegrator(mult, itsSlices);
114  added++;
115 
116  if(wrap) {
117  wrap->setElement(mpsi);
118  } else {
119  const_cast<FlaggedElmPtr &>(fep).setElement(mpsi);
120  }
121  }
122  // Nothing to be done for any other element.
123  }
124  }
125 
126 
127  void Setter::getCounts(int &add, int &rem, int &rep) const {
128  add = added;
129  rem = removed;
130  rep = replaced;
131  }
132 
133 
134  // The attributes of class SetIntegrator.
135  enum {
136  LINE, // The lattice to be used.
137  TYPE, // The type of integrator.
138  SLICES, // The number of slices.
139  SIZE
140  };
141 }
142 
143 
145  Action(SIZE, "SETINT",
146  "The \"SETINT\" statement attaches special integrators to selected "
147  "elements.") {
149  ("LINE", "Name of the lattice to be changed");
151  ("TYPE", "Type of integrator to be attached: default = THIN", "THIN");
152  itsAttr[SLICES] = Attributes::makeReal
153  ("SLICES", "Number of slices to be used for \"TYPE=THIN\"", 1.0);
154 
156 }
157 
158 
159 SetIntegrator::SetIntegrator(const std::string &name, SetIntegrator *parent):
160  Action(name, parent)
161 {}
162 
163 
165 {}
166 
167 
169  return new SetIntegrator(name, this);
170 }
171 
172 
174  // Find BeamSequence definition.
176 
177  // Initialize the visitor.
178  int slices = int(Attributes::getReal(itsAttr[SLICES]));
179  Setter splitter(*use->fetchLine(), Attributes::getString(itsAttr[TYPE]), slices);
180  splitter.execute();
181 
182  // Confirm action to user.
183  if(Options::info) {
184  int add = 0, rem = 0, rep = 0;
185  splitter.getCounts(add, rem, rep);
186  cerr << '\n';
187 
188  if(rem == 0) {
189  cerr << "No integrator removed.\n";
190  } else if(rem == 1) {
191  cerr << "1 integrator removed.\n";
192  } else if(rem > 1) {
193  cerr << rem << " integrators removed.\n";
194  }
195 
196  if(rep == 0) {
197  cerr << "No integrator replaced.\n";
198  } else if(rep == 1) {
199  cerr << "1 integrator replaced.\n";
200  } else if(rep > 1) {
201  cerr << rep << " integrators replaced.\n";
202  }
203 
204  if(add == 0) {
205  cerr << "No integrator installed.\n";
206  } else if(add == 1) {
207  cerr << "1 integrator installed.\n";
208  } else if(add > 1) {
209  cerr << add << " integrators installed.\n";
210  }
211 
212  cerr << std::endl;
213  }
214 }
virtual ElementBase * removeAlignWrapper()
Remove align wrapper.
Interface for basic beam line object.
Definition: ElementBase.h:128
The base class for all OPAL actions.
Definition: Action.h:30
Define the position of a misaligned element.
Definition: AlignWrapper.h:39
virtual void visitFlaggedElmPtr(const FlaggedElmPtr &)
Apply the algorithm to a FlaggedElmPtr.
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
bool info
Info flag.
Definition: Options.cpp:8
Default algorithms.
Interface for general multipole.
Definition: Multipole.h:46
void setElement(ElementBase *)
Replace the contained element.
static BeamSequence * find(const std::string &name)
Find a BeamSequence by name.
SetIntegrator()
Exemplar constructor.
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
virtual Beamline * fetchLine() const =0
Return the embedded CLASSIC beam line.
bool getSelectionFlag() const
Get selection flag.
An abstract sequence of beam line components.
Definition: Beamline.h:37
The base class for all OPAL beam lines and sequences.
Definition: BeamSequence.h:32
const std::string name
Integrator replacing each multipole by a set of thin lenses.
Base class for special integrators.
Definition: Integrator.h:41
virtual ~SetIntegrator()
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
The SETINT command.
Definition: SetIntegrator.h:27
virtual void execute()
Execute the command.
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
ElementBase * getElement() const
Get the element pointer.
Definition: ElmPtr.h:58
virtual SetIntegrator * clone(const std::string &name)
Make clone.
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:205
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
A section of a beam line.
Definition: FlaggedElmPtr.h:36
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307