Degrader.cpp 6.55 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
// ------------------------------------------------------------------------
// $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"
24
#include "Fields/Fieldmap.h"
adelmann's avatar
adelmann committed
25
#include "Structure/LossDataSink.h"
kraus's avatar
kraus committed
26
#include "Utilities/Options.h"
kraus's avatar
kraus committed
27
#include "Physics/Physics.h"
adelmann's avatar
adelmann committed
28 29 30 31 32 33 34 35 36 37 38 39 40
#include <memory>

extern Inform *gmsg;

using namespace std;

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

Degrader::Degrader():
    Component(),
    filename_m(""),
    position_m(0.0),
adelmann's avatar
adelmann committed
41
    deg_width_m(0.0),
adelmann's avatar
adelmann committed
42 43 44 45 46 47 48 49
    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
50
    informed_m(false)
adelmann's avatar
adelmann committed
51 52 53 54 55 56
{}

Degrader::Degrader(const Degrader &right):
    Component(right),
    filename_m(right.filename_m),
    position_m(right.position_m),
adelmann's avatar
adelmann committed
57
    deg_width_m(right.deg_width_m),
adelmann's avatar
adelmann committed
58 59 60 61 62 63 64 65 66 67
    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
68 69
    zend_m(right.zend_m)
{}
adelmann's avatar
adelmann committed
70

71
Degrader::Degrader(const std::string &name):
adelmann's avatar
adelmann committed
72 73 74
    Component(name),
    filename_m(""),
    position_m(0.0),
adelmann's avatar
adelmann committed
75
    deg_width_m(0.0),
adelmann's avatar
adelmann committed
76 77 78 79 80 81 82 83 84 85
    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
86
    zend_m(0.0)
adelmann's avatar
adelmann committed
87 88 89 90
{}


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

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


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


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

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

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;
122
    pdead = isInMaterial(R(2));
adelmann's avatar
adelmann committed
123 124 125 126 127 128 129 130 131 132 133 134

    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);
135
    }
136
    return false;
adelmann's avatar
adelmann committed
137 138 139 140 141 142 143 144 145 146 147
}

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
148 149 150 151 152

    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
153 154 155 156
}

void Degrader::initialise(PartBunch *bunch, const double &scaleFactor) {
    RefPartBunch_m = bunch;
adelmann's avatar
adelmann committed
157 158 159 160
    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
161 162 163 164 165
}


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

kraus's avatar
kraus committed
169
void Degrader::goOnline(const double &) {
adelmann's avatar
adelmann committed
170
 Inform msg("Degrader::goOnline ");
adelmann's avatar
adelmann committed
171
   if(RefPartBunch_m == NULL) {
adelmann's avatar
adelmann committed
172
        if(!informed_m) {
173
            std::string errormsg = Fieldmap::typeset_msg("BUNCH SIZE NOT SET", "warning");
adelmann's avatar
adelmann committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
            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
198 199
    online_m = false;
    lossDs_m->save();
adelmann's avatar
adelmann committed
200
    msg << " done..." << endl;
adelmann's avatar
adelmann committed
201 202 203 204 205 206
}

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

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

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

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

}

232 233
ElementBase::ElementType Degrader::getType() const {
    return DEGRADER;
adelmann's avatar
adelmann committed
234 235 236 237 238 239
}

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

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

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

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