OpalBeamline.h 13.2 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12
#ifndef OPAL_BEAMLINE_H
#define OPAL_BEAMLINE_H

#include <list>
#include <limits>
#include <vector>

#include "Algorithms/Tracker.h"
#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 20
#include "AbsBeamline/Diagnostic.h"
#include "AbsBeamline/Lambertson.h"
#include "AbsBeamline/Marker.h"
#include "AbsBeamline/RFQuadrupole.h"
#include "AbsBeamline/Separator.h"
#include "AbsBeamline/Septum.h"

21
#include "BasicActions/Option.h"
22
#include "Utilities/Options.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
class Tracker;
28 29
class PartBunch;
class SurfacePhysicsHandler;
gsell's avatar
gsell committed
30
class BoundaryGeometry;
31
class WakeFunction;
gsell's avatar
gsell committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45

#define BEAMLINE_EOL  0x80000000   // end of line
#define BEAMLINE_OOB  0x40000000   // out of bounds
#define BEAMLINE_GEOM 0x30000000   // has geometry
#define BEAMLINE_WAKE 0x20000000   // has wake
#define BEAMLINE_BEND 0x10000000   // bends
#define BEAMLINE_SURFACEPHYSICS 0x08000000 // has surface physics

class OpalBeamline {

public:
    OpalBeamline();
    ~OpalBeamline();

kraus's avatar
kraus committed
46 47
    CompVec &getPredecessors(std::shared_ptr<const Component>);
    CompVec &getSuccessors(std::shared_ptr<const Component>);
gsell's avatar
gsell committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    OpalSection &getSectionAt(const Vector_t &, long &);
    OpalSection &getSection(const unsigned int &);
    void getSectionIndexAt(const Vector_t &, long &) const;
    double getSectionStart(const long &) const;
    double getSectionEnd(const unsigned int &) const;
    double getSectionEnd(const Vector_t &, long);

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

    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 &);

    const Vector_t &getOrientation(const Vector_t &) const;
    const Vector_t &getOrientation(const long &) const;

    void resetStatus();
    void setStatus(const unsigned int &, const bool &);
    const bool &getStatus(const unsigned int &) const;

kraus's avatar
kraus committed
69
    void switchElements(const double &, const double &, const double &kineticEnergy, const bool &nomonitors = false);
gsell's avatar
gsell committed
70 71
    void switchAllElements();

72
    void switchElementsOff(const double &, ElementBase::ElementType eltype = ElementBase::ANY);
gsell's avatar
gsell committed
73 74 75
    void switchElementsOff();

    WakeFunction *getWakeFunction(const unsigned int &);
76
    std::shared_ptr<const ElementBase> getWakeFunctionOwner(const unsigned int &);
gsell's avatar
gsell committed
77 78 79 80 81 82 83 84 85 86

    SurfacePhysicsHandler *getSurfacePhysicsHandler(const unsigned int &);

    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>
87
    void visit(const T &, BeamlineVisitor &, PartBunch *);
gsell's avatar
gsell committed
88 89 90 91 92 93

    void prepareSections();
    void print(Inform &) const;

    bool section_is_glued_to(const long &i, const long &j) const;

94
    FieldList getElementByType(ElementBase::ElementType);
gsell's avatar
gsell committed
95 96 97 98 99

    // need this for autophasing in case we have multiple tracks
    double calcBeamlineLenght();
    SectionList sections_m;

100
    void removeElement(const std::string &ElName);
gsell's avatar
gsell committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

private:
    FieldList elements_m;
    bool prepared_m;
    int *online_sections_m;
    bool *online_secs_m;

    static CompVec dummy_list_m;
    static OpalSection dummy_section_m;
};

inline void OpalBeamline::resetStatus() {
    for(unsigned int i = 0; i < sections_m.size(); ++ i) {
        sections_m[i].setStatus(false);
    }
}

inline void OpalBeamline::setStatus(const unsigned int &index, const bool &status) {
    if(index < sections_m.size()) {
        sections_m[index].setStatus(status);
    }
}

inline const bool &OpalBeamline::getStatus(const unsigned int &index) const {
    static const bool default_value = false;
    if(index < sections_m.size()) {
        return sections_m[index].getStatus();
    }
    return default_value;
}

inline WakeFunction *OpalBeamline::getWakeFunction(const unsigned int &index) {
    if(index < sections_m.size()) {
        return sections_m[index].getWakeFunction();
    }
136 137 138
    return NULL;
}

139
inline std::shared_ptr<const ElementBase> OpalBeamline::getWakeFunctionOwner(const unsigned int &index) {
140 141 142 143
    if(index < sections_m.size()) {
        return sections_m[index].getWakeFunctionOwner();
    }
    return NULL;
gsell's avatar
gsell committed
144 145 146 147 148 149
}

inline BoundaryGeometry *OpalBeamline::getBoundaryGeometry(const unsigned int &index) {
    if(index < sections_m.size()) {
        return sections_m[index].getBoundaryGeometry();
    }
150
    return NULL;
gsell's avatar
gsell committed
151 152 153 154 155 156 157 158 159 160
}

inline SurfacePhysicsHandler *OpalBeamline::getSurfacePhysicsHandler(const unsigned int &index) {
    if(index < sections_m.size()) {
        return sections_m[index].getSurfacePhysicsHandler();
    }
    return 0;
}

inline double OpalBeamline::getSectionStart(const long &index) const {
161
    if(index > -1 && (unsigned long) index < sections_m.size())
gsell's avatar
gsell committed
162 163 164 165 166 167
        return sections_m[index].getStart(0., 0.);

    return std::numeric_limits<double>::max();
}

inline double OpalBeamline::getSectionEnd(const unsigned int &index) const {
168
    if(index < sections_m.size())
gsell's avatar
gsell committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        return sections_m[index].getEnd(0., 0.);

    return std::numeric_limits<double>::min();
}

inline double OpalBeamline::getSectionEnd(const Vector_t &pos, long sindex) {
    if(sindex > -1 && (unsigned int) sindex < sections_m.size()) {
        OpalSection &section = getSectionAt(pos, sindex);
        if(!(sindex & BEAMLINE_EOL || sindex < 0)) {
            return section.getEnd(pos(0), pos(2));
        }
    }
    return -1.0;
}

inline void OpalBeamline::setOrientation(const Vector_t &angle, const Vector_t &pos) {
    long index = 0;
    getSectionIndexAt(pos, index);
    if(index > -1 && (unsigned long) index < sections_m.size()) {
        sections_m[index].setOrientation(angle);
    }
}

inline void OpalBeamline::setOrientation(const Vector_t &angle, const unsigned int &index) {
    if(index < sections_m.size()) {
        sections_m[index].setOrientation(angle);
    }
}

inline void OpalBeamline::updateOrientation(const Vector_t &angle, const Vector_t &centroid, const double &smin, const double &smax) {

    // Determine which sections are online.
    int j = 0;
    for(int i = 0; i < 5 * Ippl::getNodes(); ++ i) {
        online_sections_m[i] = 0;
    }

    for(unsigned int i = 0; i < sections_m.size(); ++ i) {
        if(sections_m[i].getStatus()) {
            if(i > 0 && sections_m[i - 1].getStatus() && sections_m[i - 1].is_glued_to(&sections_m[i])) {
                continue;
            } else {
                online_sections_m[j + 5 * Ippl::myNode()] = i + 1;
                ++ j;
            }
        }
        if(j == 5) {
            break;
        }
    }

    reduce(online_sections_m, online_sections_m + 5 * Ippl::getNodes(), online_sections_m, OpAddAssign());
kraus's avatar
kraus committed
221 222 223

    bool anyOnline = false;
    size_t lastSectionOnline = 0;
gsell's avatar
gsell committed
224
    for(int i = 0; i < 5 * Ippl::getNodes(); ++ i) {
kraus's avatar
kraus committed
225
        if(online_sections_m[i] > 0) {
gsell's avatar
gsell committed
226
            online_secs_m[online_sections_m[i] - 1] = true;
kraus's avatar
kraus committed
227 228 229
            anyOnline = true;
            lastSectionOnline = std::max(lastSectionOnline, (size_t) online_sections_m[i]);
        }
gsell's avatar
gsell committed
230 231
    }

kraus's avatar
kraus committed
232 233 234
    if (!anyOnline) return;
    lastSectionOnline = std::min(lastSectionOnline, sections_m.size());

gsell's avatar
gsell committed
235
    // Update orientation of online sections.
kraus's avatar
kraus committed
236 237 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
    for(unsigned int i = 0; i < lastSectionOnline; ++ i) {
        if (i > 0 && sections_m[i-1].is_glued_to(&sections_m[i])) continue;

        online_secs_m[i] = false;
        Vector_t oldAngle = sections_m[i].getOrientation();

        // Rotate about z axis before changing section's Euler angles.
        Vector_t newAngle;
        double cosc = cos(oldAngle(2));
        double sinc = -sin(oldAngle(2));

        newAngle(0) = oldAngle(0) + cosc * angle(0) - sinc * angle(1);
        newAngle(1) = oldAngle(1) + sinc * angle(0) + cosc * angle(1);
        newAngle(2) = oldAngle(2);

        sections_m[i].setOrientation(newAngle);

        if(smin < sections_m[i].getStart(0.0, 0.0)) {
            double dist = sections_m[i].getStart(0.0, 0.0) - centroid(2);
            double newdist = dist / (1. - tan(oldAngle(0)) * angle(0));  // * cos(oldAngle(0)) / cos(newAngle(0));
            sections_m[i].setStart(centroid(2) + newdist);
        } else if(smax > sections_m[i].getEnd(0.0, 0.0)) {
            double dist = sections_m[i].getEnd(0.0, 0.0) - centroid(2);
            double newdist = dist / (1. - tan(oldAngle(0)) * angle(0)); // * cos(oldAngle(0)) / cos(newAngle(0));
            sections_m[i].setEnd(centroid(2) + newdist);
            if(i + 1 < sections_m.size() && sections_m[i].is_glued_to(&sections_m[i + 1])) {
                sections_m[i + 1].setStart(centroid(2) + newdist);
gsell's avatar
gsell committed
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 const Vector_t &OpalBeamline::getOrientation(const Vector_t &pos) const {
    static const Vector_t dummy(0.0);
    long index = 0;
    getSectionIndexAt(pos, index);
    if(index > -1 && (unsigned long) index < sections_m.size()) {
        return sections_m[index].getOrientation();
    } else {
        return dummy;
    }
}

inline const Vector_t &OpalBeamline::getOrientation(const long &i) const {
    static const Vector_t dummy(0.0);
    if(i > -1 && (unsigned long) i < sections_m.size()) {
        return sections_m[i].getOrientation();
    } else {
        return dummy;
    }
}

inline bool OpalBeamline::section_is_glued_to(const long &i, const long &j) const {
    if(i > -1 && i < j && (unsigned long) j < sections_m.size()) {
        for(int k = i; k < j; ++ k) {
            if(!sections_m[k].is_glued_to(&sections_m[k + 1])) {
                return false;
            }
        }
        return true;
    }
    return false;
}

template<class T> inline
301
void OpalBeamline::visit(const T &element, BeamlineVisitor &, PartBunch *bunch) {
gsell's avatar
gsell committed
302
    Inform msg("OPAL ");
303
    std::shared_ptr<T> elptr(dynamic_cast<T *>(element.clone()->removeWrappers()));
gsell's avatar
gsell committed
304 305 306 307 308 309 310 311 312 313 314
    if(!elptr->hasAttribute("ELEMEDGE")) {
        msg << elptr->getType() << ": no position of the element given!" << endl;
        return;
    }
    //     if (elptr->hasWake()) {
    //         const Wake *wf = elptr->getWake();
    //     }

    double startField = elptr->getAttribute("ELEMEDGE");
    double endField;
    elptr->initialise(bunch, startField, endField, 1.0);
kraus's avatar
kraus committed
315
    elements_m.push_back(ClassicField(elptr, startField, endField));
gsell's avatar
gsell committed
316 317 318
}

template<> inline
319
void OpalBeamline::visit<AlignWrapper>(const AlignWrapper &wrap, BeamlineVisitor &visitor, PartBunch *) {
320
    if(wrap.getType() == ElementBase::BEAMLINE) {
gsell's avatar
gsell committed
321
        Beamline *bl = dynamic_cast<Beamline *>(wrap.getElement());
322
        bl->iterate(visitor, false);
gsell's avatar
gsell committed
323
    } else {
324
        wrap.getElement()->accept(visitor);
gsell's avatar
gsell committed
325 326 327
    }
}

adelmann's avatar
adelmann committed
328
/*
gsell's avatar
gsell committed
329
template<> inline
330
void OpalBeamline::visit<Corrector>(const Corrector &element, BeamlineVisitor &, PartBunch *bunch) {
adelmann's avatar
adelmann committed
331
    Inform msg("visit<Corrector ");
gsell's avatar
gsell committed
332

adelmann's avatar
adelmann committed
333 334 335 336 337 338 339 340 341 342 343 344
    Corrector *elptr = dynamic_cast<Corrector *>(element.clone()->removeWrappers());
    if(!elptr->hasAttribute("ELEMEDGE")) {
        msg << elptr->getType() << ": no position of the element given!" << endl;
        return;
    }
    //     if (elptr->hasWake()) {
    //         const Wake *wf = elptr->getWake();
    //     }

    double startField = elptr->getAttribute("ELEMEDGE");
    double endField;
    elptr->initialise(bunch, startField, endField, 1.0);
kraus's avatar
kraus committed
345
    elements_m.push_back(ClassicField(elptr, startField, endField));
adelmann's avatar
adelmann committed
346 347 348 349

    msg << element.getType() << " ELEMEDGE=" << startField << endl;
}
*/
gsell's avatar
gsell committed
350
template<> inline
351
void OpalBeamline::visit<BeamBeam>(const BeamBeam &element, BeamlineVisitor &, PartBunch *) {
kraus's avatar
kraus committed
352
    WARNMSG(element.getTypeString() << " not implemented yet!" << endl);
gsell's avatar
gsell committed
353 354 355
}

template<> inline
356
void OpalBeamline::visit<Diagnostic>(const Diagnostic &element, BeamlineVisitor &, PartBunch *) {
kraus's avatar
kraus committed
357
    WARNMSG(element.getTypeString() << " not implemented yet!" << endl);
gsell's avatar
gsell committed
358 359 360
}

template<> inline
361
void OpalBeamline::visit<Lambertson>(const Lambertson &element, BeamlineVisitor &, PartBunch *) {
kraus's avatar
kraus committed
362
    WARNMSG(element.getTypeString() << " not implemented yet!" << endl);
gsell's avatar
gsell committed
363 364 365
}

template<> inline
366
void OpalBeamline::visit<Marker>(const Marker &element, BeamlineVisitor &, PartBunch *) {
gsell's avatar
gsell committed
367 368 369
}

template<> inline
370
void OpalBeamline::visit<RFQuadrupole>(const RFQuadrupole &element, BeamlineVisitor &, PartBunch *) {
kraus's avatar
kraus committed
371
    WARNMSG(element.getTypeString() << " not implemented yet!" << endl);
gsell's avatar
gsell committed
372 373 374
}

template<> inline
375
void OpalBeamline::visit<Separator>(const Separator &element, BeamlineVisitor &, PartBunch *) {
kraus's avatar
kraus committed
376
    WARNMSG(element.getTypeString() << " not implemented yet!" << endl);
gsell's avatar
gsell committed
377 378 379
}

template<> inline
380
void OpalBeamline::visit<Septum>(const Septum &element, BeamlineVisitor &, PartBunch *) {
kraus's avatar
kraus committed
381
    WARNMSG(element.getTypeString() << " not implemented yet!" << endl);
gsell's avatar
gsell committed
382 383 384
}

inline
385
void OpalBeamline::removeElement(const std::string &ElName) {
gsell's avatar
gsell committed
386 387 388 389 390 391 392 393
    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());
        }
    }
}

394
#endif // OPAL_BEAMLINE_H