Source.cpp 2.15 KB
Newer Older
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
1 2

#include "AbsBeamline/Source.h"
3
#include "Algorithms/PartBunchBase.h"
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "AbsBeamline/BeamlineVisitor.h"
#include "Fields/Fieldmap.h"
#include "Physics/Physics.h"
#include "Elements/OpalBeamline.h"

#include <iostream>
#include <fstream>

extern Inform *gmsg;

// Class Source
// ------------------------------------------------------------------------

Source::Source():
18 19
    Source("")
{}
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
20 21 22 23 24 25


Source::Source(const Source &right):
    Component(right),
    startField_m(right.startField_m),
    endField_m(right.endField_m)
snuverink_j's avatar
snuverink_j committed
26
{}
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
27 28 29 30 31 32


Source::Source(const std::string &name):
    Component(name),
    startField_m(0.0),
    endField_m(0.0)
snuverink_j's avatar
snuverink_j committed
33
{}
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
34 35 36 37 38 39 40 41 42

Source::~Source() {
}


void Source::accept(BeamlineVisitor &visitor) const {
    visitor.visitSource(*this);
}

43
bool Source::apply(const size_t &i, const double &t, Vector_t &/*E*/, Vector_t &/*B*/) {
44 45 46 47
    const Vector_t &R = RefPartBunch_m->R[i];
    const Vector_t &P = RefPartBunch_m->P[i];
    const double &dt = RefPartBunch_m->dt[i];
    const double recpgamma = Physics::c * dt / Util::getGamma(P);
48 49
    if (online_m && R(2) <= 0.0 && P(2) < 0.0) {
        double frac = -R(2) / (P(2) * recpgamma);
50

51 52
        lossDs_m->addParticle(R + frac * recpgamma * P,
                              P, RefPartBunch_m->ID[i], t + frac * dt, 0);
53

54
        return true;
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
55 56 57 58 59
    }

    return false;
}

60
void Source::initialise(PartBunchBase<double, 3> *bunch, double &startField, double &endField) {
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
61
    RefPartBunch_m = bunch;
62 63 64 65 66 67 68 69
    endField = startField;
    startField -= getElementLength();

    std::string filename = getName();
    lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(filename,
                                                              !Options::asciidump,
                                                              getType()));

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
}

void Source::finalise()
{}

bool Source::bends() const {
    return false;
}


void Source::goOnline(const double &) {
    online_m = true;
}

void Source::goOffline() {
    online_m = false;
86
    lossDs_m->save();
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
87 88 89 90 91 92 93 94 95 96
}

void Source::getDimensions(double &zBegin, double &zEnd) const {
    zBegin = startField_m;
    zEnd = endField_m;
}


ElementBase::ElementType Source::getType() const {
    return SOURCE;
97
}