Septum.cpp 2.57 KB
Newer Older
gsell's avatar
gsell committed
1
#include "AbsBeamline/Septum.h"
2

gsell's avatar
gsell committed
3
#include "AbsBeamline/BeamlineVisitor.h"
4
#include "Algorithms/PartBunchBase.h"
gsell's avatar
gsell committed
5
#include "Physics/Physics.h"
6
#include "Structure/LossDataSink.h"
gsell's avatar
gsell committed
7 8 9 10 11 12

extern Inform *gmsg;

// Class Septum
// ------------------------------------------------------------------------

13 14
Septum::Septum():Septum("")
{}
gsell's avatar
gsell committed
15

16
Septum::Septum(const std::string &name):
17 18
    PluginElement(name),
    width_m(0.0) {
19
}
gsell's avatar
gsell committed
20

21 22 23 24
Septum::Septum(const Septum &right):
    PluginElement(right),
    width_m(right.width_m) {
    setGeom(width_m);
gsell's avatar
gsell committed
25 26
}

27
Septum::~Septum() {}
gsell's avatar
gsell committed
28 29 30 31 32

void Septum::accept(BeamlineVisitor &visitor) const {
    visitor.visitSeptum(*this);
}

33
void Septum::initialise(PartBunchBase<double, 3> *bunch, double &startField, double &endField) {
gsell's avatar
gsell committed
34 35 36
    position_m = startField;
    startField -= 0.005;
    endField = position_m + 0.005;
37
    initialise(bunch);
gsell's avatar
gsell committed
38 39
}

40
void Septum::doInitialise(PartBunchBase<double, 3>* /*bunch*/) {
gsell's avatar
gsell committed
41 42 43
    *gmsg << "Septum initialise" << endl;
}

44
double Septum::getWidth() const {
gsell's avatar
gsell committed
45
    return width_m;
46
}
gsell's avatar
gsell committed
47

48 49 50
void Septum::setWidth(double width) {
    width_m = width;
    setGeom(width_m);
gsell's avatar
gsell committed
51 52
}

53
bool Septum::doPreCheck(PartBunchBase<double, 3> *bunch) {
gsell's avatar
gsell committed
54 55
    Vector_t rmin;
    Vector_t rmax;
frey_m's avatar
frey_m committed
56
    bunch->get_bounds(rmin, rmax);
57 58 59 60 61 62
    // interested in absolute maximum
    double xmax = std::max(std::abs(rmin(0)), std::abs(rmax(0)));
    double ymax = std::max(std::abs(rmin(1)), std::abs(rmax(1)));
    double rbunch_max = std::hypot(xmax, ymax);

    if(rbunch_max > rstart_m - 100)  {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        return true;
    }
    return false;
}

bool Septum::doCheck(PartBunchBase<double, 3> *bunch, const int /*turnnumber*/, const double /*t*/, const double /*tstep*/) {

    bool flag = false;
    const double slope = (yend_m - ystart_m) / (xend_m - xstart_m);
    const double halfLength = width_m / 2.0 * std::hypot(slope, 1);
    const double intcept = ystart_m - slope * xstart_m;
    const double intcept1 = intcept - halfLength;
    const double intcept2 = intcept + halfLength;

    for(unsigned int i = 0; i < bunch->getLocalNum(); ++i) {
        const Vector_t& R = bunch->R[i];

        double line1 = fabs(slope * R(0) + intcept1);
        double line2 = fabs(slope * R(0) + intcept2);

        if(fabs(R(1)) > line2 &&
           fabs(R(1)) < line1 &&
           R(0) > xstart_m    &&
           R(0) < xend_m      &&
           R(1) > ystart_m    &&
           R(1) < yend_m) {

            bunch->lossDs_m->addParticle(R, bunch->P[i], bunch->ID[i]);
            bunch->Bin[i] = -1;
            flag = true;
gsell's avatar
gsell committed
93 94 95 96 97
        }
    }
    return flag;
}

98 99
ElementBase::ElementType Septum::getType() const {
    return SEPTUM;
100
}