OpalBeamline.h 12.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 21 22 23
#include "AbsBeamline/Diagnostic.h"
#include "AbsBeamline/Lambertson.h"
#include "AbsBeamline/Marker.h"
#include "AbsBeamline/RFQuadrupole.h"
#include "AbsBeamline/Separator.h"
#include "AbsBeamline/Septum.h"

#include "Utilities/OpalSection.h"
#include "Utilities/OpalField.h"

24
class Tracker;
25 26
class PartBunch;
class SurfacePhysicsHandler;
gsell's avatar
gsell committed
27
class BoundaryGeometry;
28
class WakeFunction;
gsell's avatar
gsell committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

#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();

    CompVec &getPredecessors(const Component *);
    CompVec &getSuccessors(const Component *);
    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;

    void switchElements(const double &, const double &, const bool &nomonitors = false);
    void switchAllElements();

    void switchElementsOff(const double &, const string &eltype = "All");
    void switchElementsOff();

    WakeFunction *getWakeFunction(const unsigned int &);
73
    const ElementBase *getWakeFunctionOwner(const unsigned int &);
gsell's avatar
gsell committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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

    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>
    void visit(const T &, Tracker &, PartBunch *);

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

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

    FieldList getElementByType(const std::string type);

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

    void removeElement(const string &ElName);

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();
    }
133 134 135 136 137 138 139 140
    return NULL;
}

inline const ElementBase *OpalBeamline::getWakeFunctionOwner(const unsigned int &index) {
    if(index < sections_m.size()) {
        return sections_m[index].getWakeFunctionOwner();
    }
    return NULL;
gsell's avatar
gsell committed
141 142 143 144 145 146
}

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

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 {
158
    if(index > -1 && (unsigned long) index < sections_m.size())
gsell's avatar
gsell committed
159 160 161 162 163 164
        return sections_m[index].getStart(0., 0.);

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

inline double OpalBeamline::getSectionEnd(const unsigned int &index) const {
165
    if(index < sections_m.size())
gsell's avatar
gsell committed
166 167 168 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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 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 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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
        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());
    for(int i = 0; i < 5 * Ippl::getNodes(); ++ i) {
        if(online_sections_m[i] > 0)
            online_secs_m[online_sections_m[i] - 1] = true;
    }

    // Update orientation of online sections.
    for(unsigned int i = 0; i < sections_m.size(); ++ i) {
        if(online_secs_m[i]) {
            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);
                }
            }
        }
    }
}

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
void OpalBeamline::visit(const T &element, Tracker &, PartBunch *bunch) {
    Inform msg("OPAL ");
    T *elptr = dynamic_cast<T *>(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);
    elements_m.push_back(OpalField(elptr, startField, endField));
}

template<> inline
void OpalBeamline::visit<AlignWrapper>(const AlignWrapper &wrap, Tracker &aTracker, PartBunch *) {
    if(wrap.getType() == "beamline") {
        Beamline *bl = dynamic_cast<Beamline *>(wrap.getElement());
        bl->iterate(aTracker, false);
    } else {
        wrap.getElement()->accept(aTracker);
    }
}

template<> inline
void OpalBeamline::visit<Corrector>(const Corrector &element, Tracker &, PartBunch *) {
    Inform msg("OPAL ");
    msg << element.getType() << " not implemented yet!" << endl;
}

template<> inline
void OpalBeamline::visit<BeamBeam>(const BeamBeam &element, Tracker &, PartBunch *) {
    Inform msg("OPAL ");
    msg << element.getType() << " not implemented yet!" << endl;
}

template<> inline
void OpalBeamline::visit<Diagnostic>(const Diagnostic &element, Tracker &, PartBunch *) {
    Inform msg("OPAL ");
    msg << element.getType() << " not implemented yet!" << endl;
}

template<> inline
void OpalBeamline::visit<Lambertson>(const Lambertson &element, Tracker &, PartBunch *) {
    Inform msg("OPAL ");
    msg << element.getType() << " not implemented yet!" << endl;
}

template<> inline
void OpalBeamline::visit<Marker>(const Marker &element, Tracker &, PartBunch *) {
    WARNMSG(element.getType() << " not implemented yet!" << endl);
}

template<> inline
void OpalBeamline::visit<RFQuadrupole>(const RFQuadrupole &element, Tracker &, PartBunch *) {
    Inform msg("OPAL ");
    msg << element.getType() << " not implemented yet!" << endl;
}

template<> inline
void OpalBeamline::visit<Separator>(const Separator &element, Tracker &, PartBunch *) {
    Inform msg("OPAL ");
    msg << element.getType() << " not implemented yet!" << endl;
}

template<> inline
void OpalBeamline::visit<Septum>(const Septum &element, Tracker &, PartBunch *) {
    Inform msg("OPAL ");
    msg << element.getType() << " not implemented yet!" << endl;
}

inline
void OpalBeamline::removeElement(const string &ElName) {
    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());
        }
    }
}

#endif // OPAL_BEAMLINE_H