OpalBeamline.h 10.3 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5
#ifndef OPAL_BEAMLINE_H
#define OPAL_BEAMLINE_H

#include <list>
#include <limits>
6 7
#include <set>
#include <string>
gsell's avatar
gsell committed
8 9 10 11 12

#include "Beamlines/Beamline.h"
#include "AbsBeamline/AlignWrapper.h"
#include "AbsBeamline/BeamBeam.h"
#include "AbsBeamline/Corrector.h"
adelmann's avatar
adelmann committed
13
#include "AbsBeamline/Degrader.h"
gsell's avatar
gsell committed
14 15 16 17 18 19
#include "AbsBeamline/Diagnostic.h"
#include "AbsBeamline/Lambertson.h"
#include "AbsBeamline/Marker.h"
#include "AbsBeamline/RFQuadrupole.h"
#include "AbsBeamline/Separator.h"
#include "AbsBeamline/Septum.h"
20
#include "AbsBeamline/Source.h"
gsell's avatar
gsell committed
21

22
#include "BasicActions/Option.h"
23
#include "Utilities/Options.h"
gsell's avatar
gsell committed
24
#include "Utilities/OpalSection.h"
kraus's avatar
kraus committed
25
#include "Utilities/ClassicField.h"
gsell's avatar
gsell committed
26

27 28
#include "Algorithms/CoordinateSystemTrafo.h"

29
class Tracker;
frey_m's avatar
frey_m committed
30 31
template <class T, unsigned Dim>
class PartBunchBase;
32
class ParticleMatterInteractionHandler;
gsell's avatar
gsell committed
33
class BoundaryGeometry;
34
class WakeFunction;
gsell's avatar
gsell committed
35 36

#define BEAMLINE_EOL  0x80000000   // end of line
37
#define BEAMLINE_PARTICLEMATTERINTERACTION 0x08000000 // has particle matter interaction
gsell's avatar
gsell committed
38 39 40 41 42

class OpalBeamline {

public:
    OpalBeamline();
43
    OpalBeamline(const Vector_t& origin,
44
                 const Quaternion& rotation);
gsell's avatar
gsell committed
45 46 47 48
    ~OpalBeamline();

    OpalSection &getSectionAt(const Vector_t &, long &);
    OpalSection &getSection(const unsigned int &);
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

    void activateElements();
    std::set<std::shared_ptr<Component>> getElements(const Vector_t &x);
    Vector_t transformTo(const Vector_t &r) const;
    Vector_t transformFrom(const Vector_t &r) const;
    Vector_t rotateTo(const Vector_t &r) const;
    Vector_t rotateFrom(const Vector_t &r) const;

    Vector_t transformToLocalCS(const std::shared_ptr<Component> &comp,
                                const Vector_t &r) const;
    Vector_t transformFromLocalCS(const std::shared_ptr<Component> &comp,
                                  const Vector_t &r) const;
    Vector_t rotateToLocalCS(const std::shared_ptr<Component> &comp,
                             const Vector_t &r) const;
    Vector_t rotateFromLocalCS(const std::shared_ptr<Component> &comp,
                               const Vector_t &r) const;
    CoordinateSystemTrafo getCSTrafoLab2Local(const std::shared_ptr<Component> &comp) const;
    CoordinateSystemTrafo getCSTrafoLab2Local() const;
    CoordinateSystemTrafo getMisalignment(const std::shared_ptr<Component> &comp) const;
    // void getSectionIndexAt(const Vector_t &, long &) const;
    // double getSectionStart(const long &) const;
    // double getSectionEnd(const unsigned int &) const;
    // double getSectionEnd(const Vector_t &, long);
gsell's avatar
gsell committed
72 73 74 75

    double getStart(const Vector_t &) const;
    double getEnd(const Vector_t &) const;

76 77 78
    // void setOrientation(const Vector_t &, const Vector_t &);
    // void setOrientation(const Vector_t &, const unsigned int &);
    // void updateOrientation(const Vector_t &, const Vector_t &, const double &, const double &);
gsell's avatar
gsell committed
79

80 81
    // const Vector_t &getOrientation(const Vector_t &) const;
    // const Vector_t &getOrientation(const long &) const;
gsell's avatar
gsell committed
82

83 84 85
    // void resetStatus();
    // void setStatus(const unsigned int &, const bool &);
    // const bool &getStatus(const unsigned int &) const;
gsell's avatar
gsell committed
86

kraus's avatar
kraus committed
87
    void switchElements(const double &, const double &, const double &kineticEnergy, const bool &nomonitors = false);
gsell's avatar
gsell committed
88 89
    void switchAllElements();

90
    void switchElementsOff(const double &, ElementBase::ElementType eltype = ElementBase::ANY);
gsell's avatar
gsell committed
91 92 93
    void switchElementsOff();

    WakeFunction *getWakeFunction(const unsigned int &);
94
    std::shared_ptr<const ElementBase> getWakeFunctionOwner(const unsigned int &);
gsell's avatar
gsell committed
95

96
    ParticleMatterInteractionHandler *getParticleMatterInteractionHandler(const unsigned int &);
gsell's avatar
gsell committed
97 98 99 100 101 102 103 104

    BoundaryGeometry *getBoundaryGeometry(const unsigned int &);

    void getKFactors(const unsigned int &index, const Vector_t &pos, const long &sindex, const double &t, Vector_t &KR, Vector_t &KT);
    unsigned long getFieldAt(const unsigned int &, const Vector_t &, const long &, const double &, Vector_t &, Vector_t &);
    unsigned long getFieldAt(const Vector_t &, const Vector_t &, const double &, Vector_t &, Vector_t &);

    template<class T>
105
    void visit(const T &, BeamlineVisitor &, PartBunchBase<double, 3> *);
gsell's avatar
gsell committed
106 107

    void prepareSections();
108 109 110 111
    void compute3DLattice();
    void plot3DLattice();
    void save3DLattice();
    void save3DInput();
gsell's avatar
gsell committed
112 113
    void print(Inform &) const;

114
    FieldList getElementByType(ElementBase::ElementType);
gsell's avatar
gsell committed
115 116

    // need this for autophasing in case we have multiple tracks
117
    double calcBeamlineLength();
gsell's avatar
gsell committed
118

119
    void removeElement(const std::string &ElName);
gsell's avatar
gsell committed
120

121 122 123 124
    void swap(OpalBeamline & rhs);
    void merge(OpalBeamline &rhs);

    bool containsSource();
gsell's avatar
gsell committed
125
private:
126 127 128 129 130
    FieldList::iterator partiallyInsideDipole(const FieldList::iterator &it,
                                              const FieldList::iterator &begin,
                                              const FieldList::iterator &end,
                                              const unsigned int &minOrder);

gsell's avatar
gsell committed
131 132
    FieldList elements_m;
    bool prepared_m;
133 134 135
    bool containsSource_m;

    CoordinateSystemTrafo coordTransformationTo_m;
gsell's avatar
gsell committed
136 137 138 139 140 141

    static CompVec dummy_list_m;
    static OpalSection dummy_section_m;
};


142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
// inline WakeFunction *OpalBeamline::getWakeFunction(const unsigned int &index) {
//     if(index < sections_m.size()) {
//         return sections_m[index].getWakeFunction();
//     }
//     return NULL;
// }

// inline std::shared_ptr<const ElementBase> OpalBeamline::getWakeFunctionOwner(const unsigned int &index) {
//     if(index < sections_m.size()) {
//         return sections_m[index].getWakeFunctionOwner();
//     }
//     return NULL;
// }

// inline BoundaryGeometry *OpalBeamline::getBoundaryGeometry(const unsigned int &index) {
//     if(index < sections_m.size()) {
//         return sections_m[index].getBoundaryGeometry();
//     }
//     return NULL;
// }

163
// inline ParticleMatterInteractionHandler *OpalBeamline::getParticleMatterInteractionHandler(const unsigned int &index) {
164
//     if(index < sections_m.size()) {
165
//         return sections_m[index].getParticleMatterInteractionHandler();
166 167 168
//     }
//     return 0;
// }
gsell's avatar
gsell committed
169 170

template<class T> inline
171
void OpalBeamline::visit(const T &element, BeamlineVisitor &, PartBunchBase<double, 3> *bunch) {
gsell's avatar
gsell committed
172
    Inform msg("OPAL ");
173 174
    double startField = 0.0;
    double endField = 0.0;
175
    std::shared_ptr<T> elptr(dynamic_cast<T *>(element.removeWrappers()->clone()));
176

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
177
    if (elptr->isElementPositionSet())
178
        startField = elptr->getElementPosition();
gsell's avatar
gsell committed
179

180
    elptr->initialise(bunch, startField, endField);
kraus's avatar
kraus committed
181
    elements_m.push_back(ClassicField(elptr, startField, endField));
gsell's avatar
gsell committed
182 183 184
}

template<> inline
185
void OpalBeamline::visit<Source>(const Source &element, BeamlineVisitor &, PartBunchBase<double, 3> *bunch) {
186
    containsSource_m = true;
gsell's avatar
gsell committed
187 188 189
}

template<> inline
190
void OpalBeamline::visit<AlignWrapper>(const AlignWrapper &wrap, BeamlineVisitor &visitor, PartBunchBase<double, 3> *) {
191
    wrap.getElement()->accept(visitor);
adelmann's avatar
adelmann committed
192
}
193

gsell's avatar
gsell committed
194
template<> inline
195
void OpalBeamline::visit<BeamBeam>(const BeamBeam &element, BeamlineVisitor &, PartBunchBase<double, 3> *) {
kraus's avatar
kraus committed
196
    WARNMSG(element.getTypeString() << " not implemented yet!" << endl);
gsell's avatar
gsell committed
197 198 199
}

template<> inline
200
void OpalBeamline::visit<Diagnostic>(const Diagnostic &element, BeamlineVisitor &, PartBunchBase<double, 3> *) {
kraus's avatar
kraus committed
201
    WARNMSG(element.getTypeString() << " not implemented yet!" << endl);
gsell's avatar
gsell committed
202 203 204
}

template<> inline
205
void OpalBeamline::visit<Lambertson>(const Lambertson &element, BeamlineVisitor &, PartBunchBase<double, 3> *) {
kraus's avatar
kraus committed
206
    WARNMSG(element.getTypeString() << " not implemented yet!" << endl);
gsell's avatar
gsell committed
207 208 209
}

template<> inline
210
void OpalBeamline::visit<Marker>(const Marker &element, BeamlineVisitor &, PartBunchBase<double, 3> *) {
gsell's avatar
gsell committed
211 212 213
}

template<> inline
214
void OpalBeamline::visit<RFQuadrupole>(const RFQuadrupole &element, BeamlineVisitor &, PartBunchBase<double, 3> *) {
kraus's avatar
kraus committed
215
    WARNMSG(element.getTypeString() << " not implemented yet!" << endl);
gsell's avatar
gsell committed
216 217 218
}

template<> inline
219
void OpalBeamline::visit<Separator>(const Separator &element, BeamlineVisitor &, PartBunchBase<double, 3> *) {
kraus's avatar
kraus committed
220
    WARNMSG(element.getTypeString() << " not implemented yet!" << endl);
gsell's avatar
gsell committed
221 222 223
}

template<> inline
224
void OpalBeamline::visit<Septum>(const Septum &element, BeamlineVisitor &, PartBunchBase<double, 3> *) {
kraus's avatar
kraus committed
225
    WARNMSG(element.getTypeString() << " not implemented yet!" << endl);
gsell's avatar
gsell committed
226 227 228
}

inline
229
void OpalBeamline::removeElement(const std::string &ElName) {
gsell's avatar
gsell committed
230 231 232 233 234 235 236 237
    for(FieldList::iterator flit = elements_m.begin(); flit != elements_m.end(); ++ flit) {
        if(flit->getElement()->getName() == ElName) {
            flit->setStart(-flit->getEnd());
            flit->setEnd(-flit->getEnd());
        }
    }
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
inline
Vector_t OpalBeamline::transformTo(const Vector_t &r) const {
    return coordTransformationTo_m.transformTo(r);
}

inline
Vector_t OpalBeamline::transformFrom(const Vector_t &r) const {
    return coordTransformationTo_m.transformFrom(r);
}

inline
Vector_t OpalBeamline::rotateTo(const Vector_t &r) const {
    return coordTransformationTo_m.rotateTo(r);
}

inline
Vector_t OpalBeamline::rotateFrom(const Vector_t &r) const {
    return coordTransformationTo_m.rotateFrom(r);
}

inline
Vector_t OpalBeamline::transformToLocalCS(const std::shared_ptr<Component> &comp,
                                          const Vector_t &r) const {
    return comp->getCSTrafoGlobal2Local().transformTo(r);
}

inline
Vector_t OpalBeamline::transformFromLocalCS(const std::shared_ptr<Component> &comp,
                                            const Vector_t &r) const {
    return comp->getCSTrafoGlobal2Local().transformFrom(r);
}

inline
Vector_t OpalBeamline::rotateToLocalCS(const std::shared_ptr<Component> &comp,
                                       const Vector_t &r) const {
    return comp->getCSTrafoGlobal2Local().rotateTo(r);
}

inline
Vector_t OpalBeamline::rotateFromLocalCS(const std::shared_ptr<Component> &comp,
                                         const Vector_t &r) const {
    return comp->getCSTrafoGlobal2Local().rotateFrom(r);
}

inline
CoordinateSystemTrafo OpalBeamline::getCSTrafoLab2Local(const std::shared_ptr<Component> &comp) const {
    return comp->getCSTrafoGlobal2Local();
}

inline
CoordinateSystemTrafo OpalBeamline::getCSTrafoLab2Local() const {
    return coordTransformationTo_m;
}

inline
CoordinateSystemTrafo OpalBeamline::getMisalignment(const std::shared_ptr<Component> &comp) const {
    return comp->getMisalignment();
}

inline
bool OpalBeamline::containsSource() {
    return containsSource_m;
}
301
#endif // OPAL_BEAMLINE_H