Degrader.cpp 6.54 KB
Newer Older
adelmann's avatar
adelmann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// ------------------------------------------------------------------------
// $RCSfile: Degrader.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.1.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: Degrader
//   Defines the abstract interface for a beam Degrader.
//
// ------------------------------------------------------------------------
// Class category: AbsBeamline
// ------------------------------------------------------------------------
//
// $Date: 2000/03/27 09:32:31 $
// $Author: fci $
//
// ------------------------------------------------------------------------

#include "AbsBeamline/Degrader.h"
#include "Algorithms/PartBunch.h"
#include "AbsBeamline/BeamlineVisitor.h"
#include "Fields/Fieldmap.hh"
#include "Structure/LossDataSink.h"
kraus's avatar
kraus committed
26
#include "Physics/Physics.h"
adelmann's avatar
adelmann committed
27 28 29 30 31 32 33 34 35 36 37 38 39
#include <memory>

extern Inform *gmsg;

using namespace std;

// Class Degrader
// ------------------------------------------------------------------------

Degrader::Degrader():
    Component(),
    filename_m(""),
    position_m(0.0),
adelmann's avatar
adelmann committed
40
    deg_width_m(0.0),
adelmann's avatar
adelmann committed
41 42 43 44 45 46 47 48
    PosX_m(0),
    PosY_m(0),
    PosZ_m(0),
    MomentumX_m(0),
    MomentumY_m(0),
    MomentumZ_m(0),
    time_m(0),
    id_m(0),
adelmann's avatar
adelmann committed
49
    informed_m(false)
adelmann's avatar
adelmann committed
50 51 52 53 54 55
{}

Degrader::Degrader(const Degrader &right):
    Component(right),
    filename_m(right.filename_m),
    position_m(right.position_m),
adelmann's avatar
adelmann committed
56
    deg_width_m(right.deg_width_m),
adelmann's avatar
adelmann committed
57 58 59 60 61 62 63 64 65 66
    PosX_m(right.PosX_m),
    PosY_m(right.PosY_m),
    PosZ_m(right.PosZ_m),
    MomentumX_m(right.MomentumX_m),
    MomentumY_m(right.MomentumY_m),
    MomentumZ_m(right.MomentumZ_m),
    time_m(right.time_m),
    id_m(right.id_m),
    informed_m(right.informed_m),
    zstart_m(right.zstart_m),
adelmann's avatar
adelmann committed
67 68
    zend_m(right.zend_m)
{}
adelmann's avatar
adelmann committed
69

70
Degrader::Degrader(const std::string &name):
adelmann's avatar
adelmann committed
71 72 73
    Component(name),
    filename_m(""),
    position_m(0.0),
adelmann's avatar
adelmann committed
74
    deg_width_m(0.0),
adelmann's avatar
adelmann committed
75 76 77 78 79 80 81 82 83 84
    PosX_m(0),
    PosY_m(0),
    PosZ_m(0),
    MomentumX_m(0),
    MomentumY_m(0),
    MomentumZ_m(0),
    time_m(0),
    id_m(0),
    informed_m(false),
    zstart_m(0.0),
adelmann's avatar
adelmann committed
85
    zend_m(0.0)
adelmann's avatar
adelmann committed
86 87 88 89
{}


Degrader::~Degrader() {
adelmann's avatar
adelmann committed
90 91 92

  if(online_m)
    goOffline();
adelmann's avatar
adelmann committed
93 94 95 96 97 98 99 100
}


void Degrader::accept(BeamlineVisitor &visitor) const {
    visitor.visitDegrader(*this);
}


101
inline bool Degrader::isInMaterial(double z ) {
adelmann's avatar
adelmann committed
102
 /**
103
     check if the particle is in the degarder material
104

adelmann's avatar
adelmann committed
105
  */
adelmann's avatar
adelmann committed
106
    return ((z > position_m) && (z <= position_m + getZSize())); //getElementLength()));
adelmann's avatar
adelmann committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120
}

bool Degrader::apply(const size_t &i, const double &t, double E[], double B[]) {
    Vector_t Ev(0, 0, 0), Bv(0, 0, 0);
    return apply(i, t, Ev, Bv);
}

bool Degrader::apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) {

    const Vector_t &R = RefPartBunch_m->R[i] - Vector_t(dx_m, dy_m, ds_m); // including the missaligment
    const Vector_t &P = RefPartBunch_m->P[i];
    const double recpgamma = Physics::c * RefPartBunch_m->getdT() / sqrt(1.0  + dot(P, P));

    bool pdead = false;
121
    pdead = isInMaterial(R(2));
adelmann's avatar
adelmann committed
122 123 124 125 126 127 128 129 130 131 132 133

    if(pdead) {
      RefPartBunch_m->Bin[i] = -1;
      double frac = (R(2) - position_m) / P(2) * recpgamma;
      PosX_m.push_back(R(0));
      PosY_m.push_back(R(1));
      PosZ_m.push_back(R(2));
      MomentumX_m.push_back(P(0));
      MomentumY_m.push_back(P(1));
      MomentumZ_m.push_back(P(2));
      time_m.push_back(t + frac * RefPartBunch_m->getdT());
      id_m.push_back(i);
134
    }
135
    return false;
adelmann's avatar
adelmann committed
136 137 138 139 140 141 142 143 144 145 146
}

bool Degrader::apply(const Vector_t &R, const Vector_t &centroid, const double &t, Vector_t &E, Vector_t &B) {
    return false;
}


void Degrader::initialise(PartBunch *bunch, double &startField, double &endField, const double &scaleFactor) {
    RefPartBunch_m = bunch;
    position_m = startField;
    endField = position_m + getElementLength();
adelmann's avatar
adelmann committed
147 148 149 150 151

    if (filename_m == std::string(""))
        lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getName(), !Options::asciidump));
    else
        lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(filename_m.substr(0, filename_m.rfind(".")), !Options::asciidump));
adelmann's avatar
adelmann committed
152 153 154 155
}

void Degrader::initialise(PartBunch *bunch, const double &scaleFactor) {
    RefPartBunch_m = bunch;
adelmann's avatar
adelmann committed
156 157 158 159
    if (filename_m == std::string(""))
        lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getName(), !Options::asciidump));
    else
        lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(filename_m.substr(0, filename_m.rfind(".")), !Options::asciidump));
adelmann's avatar
adelmann committed
160 161 162 163 164
}


void Degrader::finalise()
{
adelmann's avatar
Cleanup  
adelmann committed
165
  *gmsg << "* Finalize Degrader" << endl;
adelmann's avatar
adelmann committed
166 167 168
}

void Degrader::goOnline() {
adelmann's avatar
adelmann committed
169
 Inform msg("Degrader::goOnline ");
adelmann's avatar
adelmann committed
170
   if(RefPartBunch_m == NULL) {
adelmann's avatar
adelmann committed
171
        if(!informed_m) {
172
            std::string errormsg = Fieldmap::typeset_msg("BUNCH SIZE NOT SET", "warning");
adelmann's avatar
adelmann committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
            msg << errormsg << "\n"
                << endl;
            if(Ippl::myNode() == 0) {
                ofstream omsg("errormsg.txt", ios_base::app);
                omsg << errormsg << endl;
                omsg.close();
            }
            informed_m = true;
        }
        return;
    }
    PosX_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    PosY_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    PosZ_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    MomentumX_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    MomentumY_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    MomentumZ_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    time_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    id_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    online_m = true;
}

void Degrader::goOffline() {
    Inform msg("Degrader::goOffline ");
adelmann's avatar
adelmann committed
197 198
    online_m = false;
    lossDs_m->save();
adelmann's avatar
adelmann committed
199
    msg << " done..." << endl;
adelmann's avatar
adelmann committed
200 201 202 203 204 205
}

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

206
void Degrader::setOutputFN(std::string fn) {
adelmann's avatar
adelmann committed
207 208 209 210
    filename_m = fn;
}

string Degrader::getOutputFN() {
adelmann's avatar
adelmann committed
211 212
    if (filename_m == std::string(""))
        return getName();
213
    else
adelmann's avatar
adelmann committed
214
        return filename_m.substr(0, filename_m.rfind("."));
adelmann's avatar
adelmann committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
}

double Degrader::getZStart() {
    return zstart_m;
}

double Degrader::getZEnd() {
    return zend_m;
}

void Degrader::getDimensions(double &zBegin, double &zEnd) const {
    zBegin = position_m;
    zEnd = position_m + getElementLength();

}

231 232
const std::string &Degrader::getType() const {
    static const std::string type("DEGRADER");
adelmann's avatar
adelmann committed
233 234 235 236 237 238 239
    return type;
}

string Degrader::getDegraderShape() {
    return "DEGRADER";

}
adelmann's avatar
adelmann committed
240 241 242 243 244 245 246 247

void Degrader::setZSize(double z) {
    deg_width_m = z;
}

double Degrader::getZSize() {
    return deg_width_m;
}