Probe.cpp 8.27 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// ------------------------------------------------------------------------
// $RCSfile: Probe.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.1.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: Probe
//   Defines the abstract interface for a septum magnet.
//
// ------------------------------------------------------------------------
// Class category: AbsBeamline
// ------------------------------------------------------------------------
//
// $Date: 2009/10/07 09:32:32 $
// $Author: Bi, Yang $
// 2012/03/01: fix bugs and change the algorithm in the checkProbe()
//
// ------------------------------------------------------------------------

#include "AbsBeamline/Probe.h"
23
#include "Algorithms/PartBunch.h"
gsell's avatar
gsell committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include "AbsBeamline/BeamlineVisitor.h"
#include "Physics/Physics.h"
#include "Structure/LossDataSink.h"
#include <iostream>
#include <fstream>
using Physics::pi;

extern Inform *gmsg;

// Class Probe
// ------------------------------------------------------------------------

Probe::Probe():
    Component(),
    filename_m(""),
    position_m(0.0),
    xstart_m(0.0),
    xend_m(0.0),
    ystart_m(0.0),
    yend_m(0.0),
    width_m(0.0),
45 46
    step_m(0){
    A_m = yend_m - ystart_m;
47
    B_m = xstart_m - xend_m;
48
    R_m = sqrt(A_m*A_m+B_m*B_m);
49
    C_m = ystart_m*xend_m - xstart_m*yend_m;
50
}
gsell's avatar
gsell committed
51 52 53 54 55 56 57 58 59 60 61


Probe::Probe(const Probe &right):
    Component(right),
    filename_m(right.filename_m),
    position_m(right.position_m),
    xstart_m(right.xstart_m),
    xend_m(right.xend_m),
    ystart_m(right.ystart_m),
    yend_m(right.yend_m),
    width_m(right.width_m),
62 63
    step_m(right.step_m){
    A_m = yend_m - ystart_m;
64
    B_m = xstart_m - xend_m;
65
    R_m = sqrt(A_m*A_m+B_m*B_m);
66
    C_m = ystart_m*xend_m - xstart_m*yend_m;
67
}
gsell's avatar
gsell committed
68 69


70
Probe::Probe(const std::string &name):
gsell's avatar
gsell committed
71 72 73 74 75 76 77 78
    Component(name),
    filename_m(""),
    position_m(0.0),
    xstart_m(0.0),
    xend_m(0.0),
    ystart_m(0.0),
    yend_m(0.0),
    width_m(0.0),
79 80
    step_m(0){
    A_m = yend_m - ystart_m;
81
    B_m = xstart_m - xend_m;
82
    R_m = sqrt(A_m*A_m+B_m*B_m);
83
    C_m = ystart_m*xend_m - xstart_m*yend_m;
84
}
gsell's avatar
gsell committed
85 86 87 88 89 90 91 92 93 94 95


Probe::~Probe() {
    idrec_m.clear();
}


void Probe::accept(BeamlineVisitor &visitor) const {
    visitor.visitProbe(*this);
}

96
bool Probe::apply(const size_t &i, const double &t, double E[], double B[]) {
gsell's avatar
gsell committed
97 98 99 100
    Vector_t Ev(0, 0, 0), Bv(0, 0, 0);
    return apply(i, t, Ev, Bv);
}

101
bool Probe::apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) {
gsell's avatar
gsell committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115
    return false;
}

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

void Probe::initialise(PartBunch *bunch, double &startField, double &endField, const double &scaleFactor) {
    position_m = startField;
    startField -= 0.005;
    endField = position_m + 0.005;
}

void Probe::initialise(PartBunch *bunch, const double &scaleFactor) {
adelmann's avatar
Cleanup  
adelmann committed
116
    *gmsg << "* Initialize probe" << endl;
adelmann's avatar
adelmann committed
117 118 119 120
    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));
gsell's avatar
gsell committed
121 122 123
}

void Probe::finalise() {
adelmann's avatar
adelmann committed
124
    lossDs_m->save();
adelmann's avatar
Cleanup  
adelmann committed
125
    *gmsg << "* Finalize probe" << endl;
gsell's avatar
gsell committed
126 127 128 129 130 131 132 133 134 135 136
}

bool Probe::bends() const {
    return false;
}
void Probe::goOnline() {
    online_m = true;
}

void Probe::goOffline() {
    online_m = false;
adelmann's avatar
adelmann committed
137
    lossDs_m->save();
gsell's avatar
gsell committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
}

void  Probe::setXstart(double xstart) {
    xstart_m = xstart;
}

void  Probe::setXend(double xend) {
    xend_m = xend;
}

void  Probe::setYstart(double ystart) {
    ystart_m = ystart;
}


void  Probe::setYend(double yend) {
    yend_m = yend;
}
void  Probe::setWidth(double width) {
    width_m = width;
}


double  Probe::getXstart() const {
    return xstart_m;
}

double  Probe::getXend() const {
    return xend_m;
}

double  Probe::getYstart() const {
    return ystart_m;
}

double  Probe::getYend() const {
    return yend_m;
}
double  Probe::getWidth() const {
    return width_m;
}

void Probe::setGeom(const double dist) {

182 183
    double slope;
    if (xend_m == xstart_m)
184 185 186 187
      slope = 1.0e12;
    else
      slope = (yend_m - ystart_m) / (xend_m - xstart_m);

188 189 190 191 192
    double coeff2 = sqrt(1 + slope * slope);
    double coeff1 = slope / coeff2;
    double halfdist = dist / 2.0;
    geom_m[0].x = xstart_m - halfdist * coeff1;
    geom_m[0].y = ystart_m + halfdist / coeff2;
gsell's avatar
gsell committed
193

194 195
    geom_m[1].x = xstart_m + halfdist * coeff1;
    geom_m[1].y = ystart_m - halfdist / coeff2;
gsell's avatar
gsell committed
196

197 198
    geom_m[2].x = xend_m + halfdist * coeff1;
    geom_m[2].y = yend_m - halfdist  / coeff2;
gsell's avatar
gsell committed
199

200 201
    geom_m[3].x = xend_m - halfdist * coeff1;
    geom_m[3].y = yend_m + halfdist / coeff2;
gsell's avatar
gsell committed
202 203

    geom_m[4].x = geom_m[0].x;
204
    geom_m[4].y = geom_m[0].y;
gsell's avatar
gsell committed
205 206
}

207
bool  Probe::checkProbe(PartBunch &bunch, const int turnnumber, const double t, const double tstep) {
gsell's avatar
gsell committed
208 209 210 211 212 213 214

    bool flagprobed = false;
    Vector_t rmin, rmax, probepoint;
    bunch.get_bounds(rmin, rmax);
    double r_start = sqrt(xstart_m * xstart_m + ystart_m * ystart_m);
    double r_end = sqrt(xend_m * xend_m + yend_m * yend_m);
    double r1 = sqrt(rmax(0) * rmax(0) + rmax(1) * rmax(1));
215
    double r2 = sqrt(rmin(0) * rmin(0) + rmin(1) * rmin(1));
216

217
    if( r1 > r_start - 10.0 && r2 < r_end + 10.0 ){
gsell's avatar
gsell committed
218 219
        size_t tempnum = bunch.getLocalNum();
        int pflag = 0;
220

gsell's avatar
gsell committed
221 222 223 224 225 226 227 228 229 230
	Vector_t meanP(0.0, 0.0, 0.0);
	for(unsigned int i = 0; i < bunch.getLocalNum(); ++i) {
	  for(int d = 0; d < 3; ++d) {
            meanP(d) += bunch.P[i](d);
	  }
	}
	reduce(meanP, meanP, OpAddAssign());
	meanP = meanP / Vector_t(bunch.getTotalNum());

	double sk1, sk2, stangle = 0.0;
231
	if ( B_m == 0.0 ){
gsell's avatar
gsell committed
232
	  sk1 = meanP(1)/meanP(0);
233 234 235 236
	  if (sk1 == 0.0)
	    stangle = 1.0e12;
	  else
	    stangle = std::abs(1/sk1);
gsell's avatar
gsell committed
237
	}else if (meanP(0) == 0.0 ){
238 239 240 241 242
	  sk2 = - A_m/B_m;
	  if ( sk2 == 0.0 )
	    stangle = 1.0e12;
	  else
	    stangle = std::abs(1/sk2);
gsell's avatar
gsell committed
243 244
	}else {
	  sk1 = meanP(1)/meanP(0);
245
	  sk2 = - A_m/B_m;
246
	  stangle = std::abs(( sk1-sk2 )/(1 + sk1*sk2));
gsell's avatar
gsell committed
247 248
	}
	double lstep = (sqrt(1.0-1.0/(1.0+dot(meanP, meanP))) * Physics::c) * tstep*1.0e-6; // [mm]
249
	double Swidth = lstep / sqrt( 1 + 1/stangle/stangle );
gsell's avatar
gsell committed
250 251
	setGeom(Swidth);

252
    for(unsigned int i = 0; i < tempnum; ++i) {
253 254
	  pflag = checkPoint(bunch.R[i](0), bunch.R[i](1));
	  if(pflag != 0) {
255
	     // dist1 > 0, right hand, dt > 0; dist1 < 0, left hand, dt < 0
256
		  double dist1 = (A_m*bunch.R[i](0)+B_m*bunch.R[i](1)+C_m)/R_m/1000.0;
257 258 259 260 261 262
		  double k1, k2, tangle = 0.0;
		  if ( B_m == 0.0 ){
		    k1 = bunch.P[i](1)/bunch.P[i](0);
		    if (k1 == 0.0)
		      tangle = 1.0e12;
		    else
263
                      tangle = std::abs(1/k1);
264 265 266 267 268
		  }else if (bunch.P[i](0) == 0.0 ){
		    k2 = -A_m/B_m;
		    if (k2 == 0.0)
		      tangle = 1.0e12;
		    else
269
                      tangle = std::abs(1/k2);
270 271 272
		  }else {
		    k1 = bunch.P[i](1)/bunch.P[i](0);
		    k2 = -A_m/B_m;
273
		    tangle = std::abs(( k1-k2 )/(1 + k1*k2));
274 275 276 277
		  }
		  double dist2 = dist1 * sqrt( 1+1/tangle/tangle );
		  double dt = dist2/(sqrt(1.0-1.0/(1.0 + dot(bunch.P[i], bunch.P[i]))) * Physics::c)*1.0e9;

278 279 280
	    probepoint(0) = (B_m*B_m*bunch.R[i](0) - A_m*B_m*bunch.R[i](1)-A_m*C_m)/(R_m*R_m);
	    probepoint(1) = (A_m*A_m*bunch.R[i](1) - A_m*B_m*bunch.R[i](0)-B_m*C_m)/(R_m*R_m);
	    probepoint(2) = bunch.R[i](2);
adelmann's avatar
adelmann committed
281
	    lossDs_m->addParticle(probepoint, bunch.P[i], bunch.ID[i], t+dt, turnnumber);
282
	    flagprobed = true;
283 284
	  }
	}
gsell's avatar
gsell committed
285
    }
286

287
    reduce(&flagprobed, &flagprobed + 1, &flagprobed, OpBitwiseOrAssign());
gsell's avatar
gsell committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    return flagprobed;
}

int Probe::checkPoint(const double &x, const double &y) {
    int    cn = 0;

    for(int i = 0; i < 4; i++) {
        if(((geom_m[i].y <= y) && (geom_m[i+1].y > y))
           || ((geom_m[i].y > y) && (geom_m[i+1].y <= y))) {

            float vt = (float)(y - geom_m[i].y) / (geom_m[i+1].y - geom_m[i].y);
            if(x < geom_m[i].x + vt * (geom_m[i+1].x - geom_m[i].x))
                ++cn;
        }
    }
    return (cn & 1);  // 0 if even (out), and 1 if odd (in)

}

void Probe::getDimensions(double &zBegin, double &zEnd) const {
    zBegin = position_m - 0.005;
    zEnd = position_m + 0.005;
}

312 313
const std::string &Probe::getType() const {
    static const std::string type("Probe");
gsell's avatar
gsell committed
314 315
    return type;
}