OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
OpalTrimCoil.cpp
Go to the documentation of this file.
2 
9 #include "Utilities/Util.h"
10 #include "Utility/IpplInfo.h"
11 
12 extern Inform *gmsg;
13 
14 
15 // Class OpalTrimCoil
16 // ------------------------------------------------------------------------
17 
18 // The attributes of class OpalTrimCoil.
19 namespace {
20  enum {
21  TYPE, // The type of trim coil
22  COEFNUM, //
23  COEFDENOM, //
24  COEFNUMPHI,
25  COEFDENOMPHI,
26  BMAX, //
27  PHIMIN,
28  PHIMAX,
29  RMIN, //
30  RMAX, //
31  SLPTC,
32  SIZE
33  };
34 }
35 
37  Definition(SIZE, "TRIMCOIL",
38  "The \"TRIMCOIL\" statement defines a trim coil."),
39  trimcoil_m(nullptr) {
41  ("TYPE", "Specifies the type of trim coil: PSI-BFIELD, PSI-PHASE, PSI-BFIELD-MIRRORED");
42 
44  ("COEFNUM", "Radial profile: list of polynomial coefficients for the numerator (not for PSI-BFIELD-MIRRORED)");
45 
47  ("COEFDENOM", "Radial profile: list of polynomial coefficients for the denominator (not for PSI-BFIELD-MIRRORED)");
48 
50  ("COEFNUMPHI", "Angular profile: list of polynomial coefficients for the numerator (not for PSI-BFIELD-MIRRORED)");
51 
52  itsAttr[COEFDENOMPHI] = Attributes::makeRealArray
53  ("COEFDENOMPHI", "Angular profile: list of polynomial coefficients for the denominator (not for PSI-BFIELD-MIRRORED)");
54 
56  ("BMAX", "Maximum magnetic field in Tesla.");
57 
59  ("PHIMIN", "Minimal azimuth [deg] (default 0)");
60 
62  ("PHIMAX", "Maximal azimuth [deg] (default 360)");
63 
65  ("RMIN", "Minimum radius [mm].");
66 
68  ("RMAX", "Maximum radius [mm].");
69 
71  ("SLPTC", "Slopes of the rising edge [1/mm] (for PSI-BFIELD-MIRRORED)");
72 
73 
75 
76  OpalTrimCoil *defTrimCoil = clone("UNNAMED_TRIMCOIL");
77  defTrimCoil->builtin = true;
78 
79  try {
80  defTrimCoil->update();
81  OpalData::getInstance()->define(defTrimCoil);
82  } catch(...) {
83  delete defTrimCoil;
84  }
85 }
86 
87 
88 OpalTrimCoil::OpalTrimCoil(const std::string &name, OpalTrimCoil *parent):
89  Definition(name, parent),
90  trimcoil_m(nullptr)
91 {}
92 
93 
95 }
96 
97 
99  // Can replace only by another trim coil.
100  return dynamic_cast<OpalTrimCoil *>(object) != nullptr;
101 }
102 
103 
104 OpalTrimCoil *OpalTrimCoil::clone(const std::string &name) {
105  return new OpalTrimCoil(name, this);
106 }
107 
108 
110  update();
111 }
112 
113 
114 OpalTrimCoil *OpalTrimCoil::find(const std::string &name) {
115  OpalTrimCoil *trimcoil = dynamic_cast<OpalTrimCoil *>(OpalData::getInstance()->find(name));
116 
117  if (trimcoil == nullptr) {
118  throw OpalException("OpalTrimCoil::find()", "OpalTrimCoil \"" + name + "\" not found.");
119  }
120  return trimcoil;
121 }
122 
123 
125  // Set default name.
126  if (getOpalName().empty()) setOpalName("UNNAMED_TRIMCOIL");
127 }
128 
129 
131  if (trimcoil_m != nullptr) return;
132 
134 
135  double bmax = Attributes::getReal(itsAttr[BMAX]);
136  double phimin = Attributes::getReal(itsAttr[PHIMIN]);
137  double phimax = Attributes::getReal(itsAttr[PHIMAX]);
138  double rmin = Attributes::getReal(itsAttr[RMIN]);
139  double rmax = Attributes::getReal(itsAttr[RMAX]);
140 
141  if (type == "PSI-BFIELD" || type == "PSI-PHASE") {
142  std::vector<double> coefnum = Attributes::getRealArray(itsAttr[COEFNUM]);
143  std::vector<double> coefdenom = Attributes::getRealArray(itsAttr[COEFDENOM]);
144  std::vector<double> coefnumphi = Attributes::getRealArray(itsAttr[COEFNUMPHI]);
145  std::vector<double> coefdenomphi = Attributes::getRealArray(itsAttr[COEFDENOMPHI]);
146  if (type == "PSI-BFIELD")
147  trimcoil_m = std::unique_ptr<TrimCoilBFit> (new TrimCoilBFit (bmax, rmin, rmax, coefnum, coefdenom, coefnumphi, coefdenomphi));
148  else // type == "PSI-PHASE"
149  trimcoil_m = std::unique_ptr<TrimCoilPhaseFit> (new TrimCoilPhaseFit(bmax, rmin, rmax, coefnum, coefdenom, coefnumphi, coefdenomphi));
150 
151  } else if (type == "PSI-BFIELD-MIRRORED") {
152  double slope = Attributes::getReal(itsAttr[SLPTC]);
153  trimcoil_m = std::unique_ptr<TrimCoilMirrored> (new TrimCoilMirrored(bmax, rmin, rmax, slope));
154  } else {
155  throw OpalException("OpalTrimCoil::initOpalTrimCoil",
156  type + " is not a valid trim coil type");
157  }
158 
159  trimcoil_m->setAzimuth(phimin, phimax);
160 
161  *gmsg << level3 << *this << endl;
162 }
163 
165  os << "* ******************************** T R I M C O I L ********************************\n"
166  << "* TRIMCOIL " << getOpalName() << '\n'
167  << "* TYPE " << Attributes::getString(itsAttr[TYPE]) << '\n';
168 
169  std::string type = Util::toUpper(Attributes::getString(itsAttr[TYPE]));
170  if (type == "PSI-BFIELD" || type == "PSI-PHASE") {
171  printPolynom(os,itsAttr[COEFNUM]);
172  printPolynom(os,itsAttr[COEFDENOM]);
173  printPolynom(os,itsAttr[COEFNUMPHI]);
174  printPolynom(os,itsAttr[COEFDENOMPHI]);
175  }
176 
177  os << "* BMAX " << Attributes::getReal(itsAttr[BMAX]) << '\n'
178  << "* RMIN " << Attributes::getReal(itsAttr[RMIN]) << '\n'
179  << "* RMAX " << Attributes::getReal(itsAttr[RMAX]) << '\n';
180 
181  if (Util::toUpper(Attributes::getString(itsAttr[TYPE])) == "PSI-BFIELD-MIRRORED") {
182  os << "* SLPTC " << Attributes::getReal(itsAttr[SLPTC]) << '\n';
183  }
184  os << "* *********************************************************************************" << endl;
185  return os;
186 }
187 
188 void OpalTrimCoil::printPolynom(Inform& os, const Attribute& attr) const {
189  std::stringstream ss;
190  std::vector<double> coef = Attributes::getRealArray(attr);
191  for (std::size_t i = 0; i < coef.size(); ++i) {
192  ss << ((i > 0) ? "+ " : "") << coef[i]
193  << ((i > 0) ? (" * x^" + std::to_string(i)) : "") << ' ';
194  }
195  os << "* POLYNOM " << attr.getName() << " " << ss.str() << '\n';
196 }
void define(Object *newObject)
Define a new object.
Definition: OpalData.cpp:538
The base class for all OPAL definitions.
Definition: Definition.h:30
void printPolynom(Inform &os, const Attribute &attr) const
Helper method for printing.
The base class for all OPAL exceptions.
Definition: OpalException.h:28
The TRIMCOIL definition.
Definition: OpalTrimCoil.h:17
Inform * gmsg
Definition: Main.cpp:21
std::string toUpper(const std::string &str)
Definition: Util.cpp:130
Inform & print(Inform &os) const
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
virtual bool canReplaceBy(Object *object)
Test if replacement is allowed.
virtual void update()
Update the OpalTrimCoil data.
static OpalData * getInstance()
Definition: OpalData.cpp:209
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:284
A representation of an Object attribute.
Definition: Attribute.h:55
virtual void execute()
Check the OpalTrimCoil data.
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
std::vector< double > getRealArray(const Attribute &attr)
Get array value.
Definition: Attributes.cpp:258
std::unique_ptr< TrimCoil > trimcoil_m
Actual implementation.
Definition: OpalTrimCoil.h:49
OpalTrimCoil()
Exemplar constructor.
virtual OpalTrimCoil * clone(const std::string &name)
Make clone.
Object * find(const std::string &name)
Find entry.
Definition: OpalData.cpp:618
The base class for all OPAL objects.
Definition: Object.h:48
const std::string name
void setOpalName(const std::string &name)
Set object name.
Definition: Object.cpp:305
void initOpalTrimCoil()
Initialise implementation.
Attribute makeRealArray(const std::string &name, const std::string &help)
Create real array attribute.
Definition: Attributes.cpp:253
bool builtin
Built-in flag.
Definition: Object.h:231
Inform & level3(Inform &inf)
Definition: Inform.cpp:47
virtual ~OpalTrimCoil()
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
static OpalTrimCoil * find(const std::string &name)
Find named trim coil.
Definition: Inform.h:41
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:205
const std::string & getName() const
Return the attribute name.
Definition: Attribute.cpp:90
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307