Probe.cpp 8.15 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 71 72 73 74 75 76 77 78


Probe::Probe(const string &name):
    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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121


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


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

bool Probe::apply(const int &i, const double &t, double E[], double B[]) {
    *gmsg << "septum1" << endl;
    Vector_t Ev(0, 0, 0), Bv(0, 0, 0);
    return apply(i, t, Ev, Bv);
}

bool Probe::apply(const int &i, const double &t, Vector_t &E, Vector_t &B) {
    *gmsg << "septum2" << endl;
    return false;
}

bool Probe::apply(const Vector_t &R, const Vector_t &centroid, const double &t, Vector_t &E, Vector_t &B) {
    *gmsg << "septum3" << endl;
    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) {
    // initialize DataSink with H5Part output enabled
    bool doH5 = false;
    lossDs_m = new LossDataSink(bunch->getTotalNum(), doH5);
122
    //    lossDs_m->openH5(getName());
gsell's avatar
gsell committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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 182 183
}

void Probe::finalise() {
    *gmsg << "Finalize probe" << endl;
    if(lossDs_m)
        delete lossDs_m;
}

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

void Probe::goOffline() {
    online_m = false;
}

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) {

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

190 191 192 193 194
    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
195

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

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

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

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

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

    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));
217
   
gsell's avatar
gsell committed
218 219 220 221
    if( r1 > r_start - 10.0 && r1 < r_end + 10.0 ){

        size_t tempnum = bunch.getLocalNum();
        int pflag = 0;
222

gsell's avatar
gsell committed
223 224 225 226 227 228 229 230 231 232
	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;
233
	if ( B_m == 0.0 ){
gsell's avatar
gsell committed
234
	  sk1 = meanP(1)/meanP(0);
235 236 237 238
	  if (sk1 == 0.0)
	    stangle = 1.0e12;
	  else
	    stangle = std::abs(1/sk1);
gsell's avatar
gsell committed
239
	}else if (meanP(0) == 0.0 ){
240 241 242 243 244
	  sk2 = - A_m/B_m;
	  if ( sk2 == 0.0 )
	    stangle = 1.0e12;
	  else
	    stangle = std::abs(1/sk2);
gsell's avatar
gsell committed
245 246
	}else {
	  sk1 = meanP(1)/meanP(0);
247
	  sk2 = - A_m/B_m;
248
	  stangle = std::abs(( sk1-sk2 )/(1 + sk1*sk2));
gsell's avatar
gsell committed
249 250
	}
	double lstep = (sqrt(1.0-1.0/(1.0+dot(meanP, meanP))) * Physics::c) * tstep*1.0e-6; // [mm]
251
	double Swidth = lstep / sqrt( 1 + 1/stangle/stangle );
gsell's avatar
gsell committed
252 253 254
	setGeom(Swidth);

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

280 281 282
	    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);
283
	    lossDs_m->addParticle_time(probepoint, bunch.P[i], bunch.ID[i], t+dt, turnnumber);
284
	    flagprobed = true;
285 286
	  }
	}
gsell's avatar
gsell committed
287
    }
288

289
    reduce(&flagprobed, &flagprobed + 1, &flagprobed, OpBitwiseOrAssign());
290
    if(flagprobed) lossDs_m->save_time(getName());
gsell's avatar
gsell committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    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;
}

const string &Probe::getType() const {
    static const string type("Probe");
    return type;
}