Commit 2a60483b authored by kraus's avatar kraus
Browse files

more elegant implementation of SpecificElementVisitor; replaced exit statements with exceptions

parent 23656bfe
......@@ -47,12 +47,29 @@
#include "Algorithms/MapIntegrator.h"
#include "Algorithms/TrackIntegrator.h"
template <class E>
template <class ELEM1, class ELEM2>
struct CastsTrait {
typedef std::list<const ELEM1*> ElementList_t;
static void apply(ElementList_t &, const ELEM2 &)
{ }
};
template <class ELEM>
struct CastsTrait<ELEM,ELEM> {
typedef std::list<const ELEM*> ElementList_t;
static void apply(ElementList_t &allElements, const ELEM &element)
{
allElements.push_back(dynamic_cast<const ELEM*>(&element));
}
};
template <class ELEM>
class SpecificElementVisitor: public BeamlineVisitor {
public:
SpecificElementVisitor(const Beamline &beamline);
/// Apply the algorithm to the top-level beamline.
virtual void execute();
/// Apply the algorithm to a beam-beam.
......@@ -179,7 +196,7 @@ public:
size_t size() const;
typedef std::list<const Component*> ElementList_t;
typedef std::list<const ELEM*> ElementList_t;
typedef typename ElementList_t::iterator iterator_t;
typedef typename ElementList_t::const_iterator const_iterator_t;
......@@ -196,203 +213,164 @@ public:
const_reference_t front() const;
private:
const Beamline &itsLine;
const std::string ElementType;
std::list<const ElementBase*> matchingElements;
ElementList_t allElementsOfTypeE;
};
template<class ELEM>
SpecificElementVisitor<ELEM>::SpecificElementVisitor(const Beamline &beamline):
BeamlineVisitor(),
itsLine(beamline),
matchingElements(),
allElementsOfTypeE()
{ }
{
beamline.iterate(*this, false);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::execute() {
itsLine.iterate(*this, false);
for (auto it = matchingElements.begin(); it != matchingElements.end(); ++ it) {
allElementsOfTypeE.push_back(dynamic_cast<const ELEM*>(*it));
}
}
void SpecificElementVisitor<ELEM>::execute()
{ }
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitBeamBeam(const BeamBeam &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, BeamBeam>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitCollimator(const Collimator &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Collimator>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitComponent(const Component &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Component>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitCyclotron(const Cyclotron &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Cyclotron>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitRing(const Ring &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Ring>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitCorrector(const Corrector &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Corrector>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitDegrader(const Degrader &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Degrader>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitDiagnostic(const Diagnostic &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Diagnostic>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitDrift(const Drift &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Drift>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitLambertson(const Lambertson &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Lambertson>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitMarker(const Marker &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Marker>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitMonitor(const Monitor &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Monitor>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitMultipole(const Multipole &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Multipole>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitOffset(const Offset &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Offset>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitPatch(const Patch &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Patch>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitProbe(const Probe &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Probe>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitRBend(const RBend &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, RBend>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitVariableRFCavity(const VariableRFCavity &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, VariableRFCavity>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitRFCavity(const RFCavity &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, RFCavity>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitTravelingWave(const TravelingWave &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, TravelingWave>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitRFQuadrupole(const RFQuadrupole &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, RFQuadrupole>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitSBend(const SBend &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, SBend>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitSBend3D(const SBend3D &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, SBend3D>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitSeparator(const Separator &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Separator>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitSeptum(const Septum &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Septum>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitSolenoid(const Solenoid &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Solenoid>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitParallelPlate(const ParallelPlate &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, ParallelPlate>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitCyclotronValley(const CyclotronValley &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, CyclotronValley>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitStripper(const Stripper &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, Stripper>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
......@@ -414,32 +392,27 @@ void SpecificElementVisitor<ELEM>::visitAlignWrapper(const AlignWrapper &element
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitCorrectorWrapper(const CorrectorWrapper &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, CorrectorWrapper>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitCyclotronWrapper(const CyclotronWrapper &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, CyclotronWrapper>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitMultipoleWrapper(const MultipoleWrapper &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, MultipoleWrapper>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitRBendWrapper(const RBendWrapper &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, RBendWrapper>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
void SpecificElementVisitor<ELEM>::visitSBendWrapper(const SBendWrapper &element) {
if (dynamic_cast<const ELEM*>(&element))
matchingElements.push_back(&element);
CastsTrait<ELEM, SBendWrapper>::apply(allElementsOfTypeE, element);
}
template<class ELEM>
......
......@@ -13,7 +13,7 @@
#include "Distribution/Distribution.h"
#include "Distribution/SigmaGenerator.h"
#include "Distribution/SpecificElementVisitor.h"
#include "AbsBeamline/SpecificElementVisitor.h"
#include <cmath>
#include <cfloat>
......@@ -589,8 +589,8 @@ void Distribution::DoRestartOpalT(PartBunch &beam, size_t Np, int restartStep) {
#endif
if(H5file == (void*)H5_ERR) {
ERRORMSG("could not open file '" << fn << "'; exiting!" << endl);
exit(0);
throw OpalException("Distribution::DoRestartOpalT",
"could not open file '" + fn + "'");
}
if(restartStep == -1) {
......@@ -598,8 +598,8 @@ void Distribution::DoRestartOpalT(PartBunch &beam, size_t Np, int restartStep) {
OpalData::getInstance()->setRestartStep(restartStep);
} else {
if(restartStep != H5GetNumSteps(H5file) - 1 && !OpalData::getInstance()->hasRestartFile()) {
ERRORMSG("can't append to the file '" << fn << "' exiting!" << endl);
exit(0);
throw OpalException("Distribution::DoRestartOpalT",
"can't append to the file '" + fn + "'");
}
}
......@@ -734,8 +734,8 @@ void Distribution::DoRestartOpalCycl(PartBunch &beam, size_t Np, int restartStep
#endif
if(H5file == (void*)H5_ERR) {
ERRORMSG("File open failed: exiting!" << endl);
exit(0);
throw OpalException("Distribution::DoRestartOpalCycl",
"could not open file '" + fn + "'");
}
if(restartStep == -1) {
......@@ -743,8 +743,8 @@ void Distribution::DoRestartOpalCycl(PartBunch &beam, size_t Np, int restartStep
OpalData::getInstance()->setRestartStep(restartStep);
} else {
if(restartStep != H5GetNumSteps(H5file) - 1 && !OpalData::getInstance()->hasRestartFile()) {
ERRORMSG("can't append to the file '" << fn << "' exiting!" << endl);
exit(0);
throw OpalException("Distribution::DoRestartOpalCycl",
"can't append to the file '" + fn + "'");
}
}
......@@ -1050,8 +1050,8 @@ void Distribution::DoRestartOpalE(EnvelopeBunch &beam, size_t Np, int restartSte
#endif
if(H5file == (void*)H5_ERR) {
ERRORMSG("could not open file '" << fn << "'; exiting!" << endl);
exit(0);
throw OpalException("Distribution::DoRestartOpalE",
"could not open file '" + fn + "'");
}
if(restartStep == -1) {
......@@ -1059,8 +1059,8 @@ void Distribution::DoRestartOpalE(EnvelopeBunch &beam, size_t Np, int restartSte
OpalData::getInstance()->setRestartStep(restartStep);
} else {
if(restartStep != H5GetNumSteps(H5file) - 1 && !OpalData::getInstance()->hasRestartFile()) {
ERRORMSG("can't append to the file '" << fn << "' exiting!" << endl);
exit(0);
throw OpalException("Distribution::DoRestartOpalE",
"can't append to the file '" + fn + "'");
}
}
......@@ -1731,7 +1731,7 @@ void Distribution::CreateMatchedGaussDistribution(size_t numberOfParticles, doub
throw OpalException("Distribution::CreateMatchedGaussDistribution",
"didn't find any Cyclotron element in line");
}
const Cyclotron* CyclotronElement = dynamic_cast<const Cyclotron*>(CyclotronVisitor.front());
const Cyclotron* CyclotronElement = CyclotronVisitor.front();
*gmsg << "----------------------------------------------------" << endl;
*gmsg << "About to find closed orbit and matched distribution " << endl;
......@@ -1750,8 +1750,8 @@ void Distribution::CreateMatchedGaussDistribution(size_t numberOfParticles, doub
const double fmHighE = CyclotronElement->getFMHighE();
if ((fmLowE>fmHighE) || (fmLowE<0) || (fmHighE<0)) {
ERRORMSG("FMHIGHE of FMLOWE not set propperly" << endl);
exit(1);
throw OpalException("Distribution::CreateMatchedGaussDistribution",
"FMHIGHE or FMLOWE not set properly");
}
int nr, nth, nsc;
......@@ -2961,8 +2961,8 @@ void Distribution::GenerateGaussZ(size_t numberOfParticles) {
int errcode = gsl_linalg_cholesky_decomp(m);
if (errcode == GSL_EDOM) {
INFOMSG("gsl_linalg_cholesky_decomp faliled" << endl);
exit(1);
throw OpalException("Distribution::GenerateGaussZ",
"gsl_linalg_cholesky_decomp failed");
}
// so make the lower part zero.
for (int i = 0; i < 6; i++) {
......@@ -3022,8 +3022,8 @@ void Distribution::GenerateGaussZ(size_t numberOfParticles) {
if (Ippl::myNode() == saveProcessor) {
if (gsl_blas_dgemv(CblasNoTrans,1.0,m,rx,0.0,ry)) {
INFOMSG("oops... something wrong with GSL matvec\n");
exit(1);
throw OpalException("Distribution::GenerateGaussZ",
"oops... something wrong with GSL matvec");
}
xDist_m.push_back( sigmaR_m[0]*gsl_vector_get(ry, 0));
pxDist_m.push_back( sigmaP_m[0]*gsl_vector_get(ry, 1));
......@@ -4564,8 +4564,8 @@ void Distribution::SetDistParametersGauss(double massIneV) {
}
else {
*gmsg << "* Inconsitent set of correlations specified, check manual" << endl;
exit(1);
throw OpalException("Distribution::SetDistParametersGauss",
"Inconsitent set of correlations specified, check manual");
}
}
else {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment