TravelingWave.cpp 27.4 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: TravelingWave.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.1.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: TravelingWave
//   Defines the abstract interface for an accelerating structure.
//
// ------------------------------------------------------------------------
// Class category: AbsBeamline
// ------------------------------------------------------------------------
//
// $Date: 2000/03/27 09:32:31 $
// $Author: fci $
//
// ------------------------------------------------------------------------

#include "AbsBeamline/TravelingWave.h"
#include "AbsBeamline/BeamlineVisitor.h"
23
#include "Algorithms/PartBunch.h"
24
#include "Fields/Fieldmap.h"
gsell's avatar
gsell committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

#include "gsl/gsl_interp.h"
#include "gsl/gsl_spline.h"
#include <iostream>
#include <fstream>

extern Inform *gmsg;

using namespace std;

// Class TravelingWave
// ------------------------------------------------------------------------

TravelingWave::TravelingWave():
    Component(),
    NumCells_m(0),
    fast_m(false)
{}


TravelingWave::TravelingWave(const TravelingWave &right):
    Component(right),
    CoreFilename_m(right.CoreFilename_m),
    CoreFieldmap_m(right.CoreFieldmap_m),
    scale_m(right.scale_m),
    scaleCore_m(right.scaleCore_m),
    phase_m(right.phase_m),
    phaseCore1_m(right.phaseCore1_m),
    phaseCore2_m(right.phaseCore2_m),
    phaseExit_m(right.phaseExit_m),
    frequency_m(right.frequency_m),
    startField_m(right.startField_m),
    startCoreField_m(right.startCoreField_m),
    startExitField_m(right.startExitField_m),
    mappedStartExitField_m(right.mappedStartExitField_m),
    PeriodLength_m(right.PeriodLength_m),
    NumCells_m(right.NumCells_m),
    CellLength_m(right.CellLength_m),
    Mode_m(right.Mode_m),
    fast_m(right.fast_m),
    autophaseVeto_m(right.autophaseVeto_m)
{}


69
TravelingWave::TravelingWave(const std::string &name):
gsell's avatar
gsell committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    Component(name)
{}


TravelingWave::~TravelingWave() {
    // Fieldmap::deleteFieldmap(CoreFilename_m);
    //   Fieldmap::deleteFieldmap(EntryFilename_m);
    //   Fieldmap::deleteFieldmap(ExitFilename_m);
}


void TravelingWave::accept(BeamlineVisitor &visitor) const {
    visitor.visitTravelingWave(*this);
}

85
void TravelingWave::setFieldMapFN(std::string fn) {
gsell's avatar
gsell committed
86 87 88
    CoreFilename_m = fn;
}

89
// void TravelingWave::setEntryFieldMapFN(std::string fn)
gsell's avatar
gsell committed
90 91 92 93
// {
//   EntryFilename_m = fn;
// }

94
// void TravelingWave::setExitFieldMapFN(std::string fn)
gsell's avatar
gsell committed
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
// {
//   ExitFilename_m = fn;
// }

string TravelingWave::getFieldMapFN() const {
    return CoreFilename_m;
}

void TravelingWave::setAmplitudem(double vPeak) {
    scale_m = vPeak;
}

void TravelingWave::setFrequencym(double freq) {
    frequency_m = freq;
}


double TravelingWave::getFrequencym() const {
    return frequency_m;
}


void TravelingWave::setPhasem(double phase) {
    using Physics::pi;

    phase_m = phase;
    phaseCore1_m = phase_m + pi * Mode_m / 2.0;
    phaseCore2_m = phase_m + pi * Mode_m * 1.5;
    phaseExit_m = phase_m - 2.0 * pi * ((NumCells_m - 1) * Mode_m - floor((NumCells_m - 1) * Mode_m));
}

double TravelingWave::getPhasem() const {
    return phase_m;
}

void TravelingWave::setNumCells(int NumCells) {
    NumCells_m = NumCells;
}

void TravelingWave::setFast(bool fast) {
    fast_m = fast;
}


bool TravelingWave::getFast() const {
    return fast_m;
}

143
void TravelingWave::setAutophaseVeto(bool veto) {
gsell's avatar
gsell committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    autophaseVeto_m = veto;
}


bool TravelingWave::getAutophaseVeto() const {
    return autophaseVeto_m;
}


/**
 * ENVELOPE COMPONENT for radial focussing of the beam
 * Calculates the transverse envelope component for the traveling wave
 * element and adds it to the K vector
*/
void TravelingWave::addKR(int i, double t, Vector_t &K) {
    Inform msg("TravelingWave::addK()");

    Vector_t tmpE(0.0, 0.0, 0.0), tmpB(0.0, 0.0, 0.0);
    Vector_t tmpE_diff(0.0, 0.0, 0.0), tmpB_diff(0.0, 0.0, 0.0);
adelmann's avatar
adelmann committed
163
    Vector_t tmpA0(RefPartBunch_m->getX(i) - dx_m, RefPartBunch_m->getY(i) - dy_m, RefPartBunch_m->getZ(i) - startField_m);
gsell's avatar
gsell committed
164 165 166 167 168 169 170 171 172 173 174

    double g = RefPartBunch_m->getGamma(i);
    double b = RefPartBunch_m->getBeta(i);
    DiffDirection zdir(DZ);
    double wtf = 0.0;
    double k = 0.0;

    if(tmpA0(2) < startCoreField_m) {

        wtf = frequency_m * t + phase_m;
        CoreFieldmap_m->getFieldstrength(tmpA0, tmpE, tmpB);
175
        CoreFieldmap_m->getFieldDerivative(tmpA0, tmpE_diff, tmpB_diff, zdir);
gsell's avatar
gsell committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        k = scale_m * (tmpE_diff(2) * cos(wtf) - b * frequency_m * tmpE(2) * sin(wtf) / Physics::c);

    } else if(tmpA0(2) < startExitField_m) {

        wtf = frequency_m * t + phaseCore1_m;
        tmpA0(2) -= startCoreField_m;
        const double z = tmpA0(2);
        tmpA0(2) = tmpA0(2) - PeriodLength_m * floor(tmpA0(2) / PeriodLength_m);
        tmpA0(2) += startCoreField_m;
        tmpE = 0.0;
        tmpB = 0.0;
        tmpE_diff = 0.0;
        tmpB_diff = 0.0;

        CoreFieldmap_m->getFieldstrength(tmpA0, tmpE, tmpB);
191
        CoreFieldmap_m->getFieldDerivative(tmpA0, tmpE_diff, tmpB_diff, zdir);
gsell's avatar
gsell committed
192 193 194 195 196 197 198 199 200 201 202 203
        k = scaleCore_m * (tmpE_diff(2) * cos(wtf) - b * frequency_m * tmpE(2) * sin(wtf) / Physics::c);

        wtf = frequency_m * t + phaseCore2_m;
        tmpA0(2) = z + CellLength_m;
        tmpA0(2) = tmpA0(2) - PeriodLength_m * floor(tmpA0(2) / PeriodLength_m);
        tmpA0(2) += startCoreField_m;
        tmpE = 0.0;
        tmpB = 0.0;
        tmpE_diff = 0.0;
        tmpB_diff = 0.0;

        CoreFieldmap_m->getFieldstrength(tmpA0, tmpE, tmpB);
204
        CoreFieldmap_m->getFieldDerivative(tmpA0, tmpE_diff, tmpB_diff, zdir);
gsell's avatar
gsell committed
205 206 207 208 209 210 211 212 213 214 215 216
        k += scaleCore_m * (tmpE_diff(2) * cos(wtf) - b * frequency_m * tmpE(2) * sin(wtf) / Physics::c);

    } else {

        wtf  = frequency_m * t + phaseExit_m;
        tmpA0(2) -= mappedStartExitField_m;
        tmpE = 0.0;
        tmpB = 0.0;
        tmpE_diff = 0.0;
        tmpB_diff = 0.0;

        CoreFieldmap_m->getFieldstrength(tmpA0, tmpE, tmpB);
217
        CoreFieldmap_m->getFieldDerivative(tmpA0, tmpE_diff, tmpB_diff, zdir);
gsell's avatar
gsell committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        k = scale_m * (tmpE_diff(2) * cos(wtf) - b * frequency_m * tmpE(2) * sin(wtf) / Physics::c);

    }

    //XXX: we have to compensate for bet behaviour by using -q_e
    k *= (-Physics::q_e / (2.0 * g * Physics::EMASS));
    K += Vector_t(k, k, 0.0);
}


void TravelingWave::addKT(int i, double t, Vector_t &K) {
    // get K from radial function, solves the lastk problem from BET
    Vector_t tempK(0.0, 0.0, 0.0);
    addKR(i, t, tempK);

    // x and y component are identical and equal to k
    double oldK = tempK(0);

    //get coordinates
    double dx = RefPartBunch_m->getX0(i) - dx_m;
    double dy = RefPartBunch_m->getY0(i) - dy_m;

    K += Vector_t(oldK * dx, oldK * dy, 0.0);
}

/*
    Function to obtain z-k-component, copied from BET:
   Field Kloc;

   if (isActive(z)){
       double k,kx,ky;

       if (kLast == MAXFLOAT) {
           Kloc = getK(g,b,z,t,m);  // dummy call to set kLast
       }
       k   = kLast;
       kx  = 0.0; ky = 0.0;
       if (Cxy < 1.0) {
           Field Eloc = getE(z,t);
           double
               cf = -e0/(g*m);

           kx = cf*Eloc.x;
           ky = cf*Eloc.y;
       }
       Kloc = Field(k*Cxy*(x0-x) + kx,
               k*Cxy*(y0-y) + ky,
               0.0);
       if (Ndamp > 0) {
           Kloc *= fDamp(z - z0);
       }
   }

   return Kloc;
*/


275
bool TravelingWave::apply(const size_t &i, const double &t, double E[], double B[]) {
gsell's avatar
gsell committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289
    Vector_t Ev(0, 0, 0), Bv(0, 0, 0);
    Vector_t Rt(RefPartBunch_m->getX(i), RefPartBunch_m->getY(i), RefPartBunch_m->getZ(i));
    if(apply(Rt, Vector_t(0.0), t, Ev, Bv)) return true;

    E[0] = Ev(0);
    E[1] = Ev(1);
    E[2] = Ev(2);
    B[0] = Bv(0);
    B[1] = Bv(1);
    B[2] = Bv(2);

    return false;
}

290
bool TravelingWave::apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) {
gsell's avatar
gsell committed
291 292
    double tmpcos, tmpsin;

adelmann's avatar
adelmann committed
293
    Vector_t tmpR(RefPartBunch_m->getX(i) - dx_m, RefPartBunch_m->getY(i) - dy_m , RefPartBunch_m->getZ(i) - startField_m);
gsell's avatar
gsell committed
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    Vector_t tmpE(0.0, 0.0, 0.0), tmpB(0.0, 0.0, 0.0);
    bool out_of_bounds = false;


    if(tmpR(2) < startCoreField_m) {
        tmpcos =  scale_m * cos(frequency_m * t + phase_m);
        tmpsin = -scale_m * sin(frequency_m * t + phase_m);

    } else if(tmpR(2) < startExitField_m) {
        Vector_t tmpE2(0.0, 0.0, 0.0), tmpB2(0.0, 0.0, 0.0);
        tmpR(2) -= startCoreField_m;
        const double z = tmpR(2);
        tmpR(2) = tmpR(2) - PeriodLength_m * floor(tmpR(2) / PeriodLength_m);
        tmpR(2) += startCoreField_m;

        tmpcos =  scaleCore_m * cos(frequency_m * t + phaseCore1_m);
        tmpsin = -scaleCore_m * sin(frequency_m * t + phaseCore1_m);
        out_of_bounds = CoreFieldmap_m->getFieldstrength(tmpR, tmpE, tmpB);
        E += tmpcos * tmpE;
        B += tmpsin * tmpB;

        tmpE = 0.0;
        tmpB = 0.0;

        tmpR(2) = z + CellLength_m;
        tmpR(2) = tmpR(2) - PeriodLength_m * floor(tmpR(2) / PeriodLength_m);
        tmpR(2) += startCoreField_m;

        tmpcos =  scaleCore_m * cos(frequency_m * t + phaseCore2_m);
        tmpsin = -scaleCore_m * sin(frequency_m * t + phaseCore2_m);

    } else {
        tmpcos =  scale_m * cos(frequency_m * t + phaseExit_m);
        tmpsin = -scale_m * sin(frequency_m * t + phaseExit_m);
        tmpR(2) -= mappedStartExitField_m;

    }

    out_of_bounds = out_of_bounds || CoreFieldmap_m->getFieldstrength(tmpR, tmpE, tmpB);
    E += tmpcos * tmpE;
    B += tmpsin * tmpB;

    return out_of_bounds;
}

bool TravelingWave::apply(const Vector_t &R, const Vector_t &centroid, const double &t, Vector_t &E, Vector_t &B) {
    double tmpcos, tmpsin;
adelmann's avatar
adelmann committed
341
    Vector_t tmpR(R(0) - dx_m, R(1) - dy_m , R(2) - startField_m);
gsell's avatar
gsell committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    Vector_t tmpE(0.0, 0.0, 0.0), tmpB(0.0, 0.0, 0.0);
    bool out_of_bounds = false;


    if(tmpR(2) < startCoreField_m) {
        tmpcos =  scale_m * cos(frequency_m * t + phase_m);
        tmpsin = -scale_m * sin(frequency_m * t + phase_m);
    } else if(tmpR(2) < startExitField_m) {
        Vector_t tmpE2(0.0, 0.0, 0.0), tmpB2(0.0, 0.0, 0.0);
        tmpR(2) -= startCoreField_m;
        const double z = tmpR(2);
        tmpR(2) = tmpR(2) - PeriodLength_m * floor(tmpR(2) / PeriodLength_m);
        tmpR(2) += startCoreField_m;

        tmpcos =  scaleCore_m * cos(frequency_m * t + phaseCore1_m);
        tmpsin = -scaleCore_m * sin(frequency_m * t + phaseCore1_m);
        out_of_bounds = CoreFieldmap_m->getFieldstrength(tmpR, tmpE, tmpB);
        E += tmpcos * tmpE;
        B += tmpsin * tmpB;

        tmpE = 0.0;
        tmpB = 0.0;

        tmpR(2) = z + CellLength_m;
        tmpR(2) = tmpR(2) - PeriodLength_m * floor(tmpR(2) / PeriodLength_m);
        tmpR(2) += startCoreField_m;

        tmpcos =  scaleCore_m * cos(frequency_m * t + phaseCore2_m);
        tmpsin = -scaleCore_m * sin(frequency_m * t + phaseCore2_m);

    } else {
        tmpcos =  scale_m * cos(frequency_m * t + phaseExit_m);
        tmpsin = -scale_m * sin(frequency_m * t + phaseExit_m);
        tmpR(2) -= mappedStartExitField_m;
    }

    out_of_bounds = out_of_bounds || CoreFieldmap_m->getFieldstrength(tmpR, tmpE, tmpB);
    E += tmpcos * tmpE;
    B += tmpsin * tmpB;

    return out_of_bounds;

}

void TravelingWave::initialise(PartBunch *bunch, double &startField, double &endField, const double &scaleFactor) {
    using Physics::pi;
    using Physics::two_pi;

    Inform msg("TravelingWave ");
391
    std::stringstream errormsg;
gsell's avatar
gsell committed
392 393 394 395 396 397 398 399 400

    RefPartBunch_m = bunch;

    CoreFieldmap_m = Fieldmap::getFieldmap(CoreFilename_m, fast_m);
    if(CoreFieldmap_m != NULL) {
        double zBegin = 0.0, zEnd = 0.0, rBegin = 0.0, rEnd = 0.0;
        CoreFieldmap_m->getFieldDimensions(zBegin, zEnd, rBegin, rEnd);

        if(zEnd > zBegin) {
401 402
            *Ippl::Info << getName() << " using file ";
            CoreFieldmap_m->getInfo(Ippl::Info);
gsell's avatar
gsell committed
403 404 405 406
            if(fabs((frequency_m - CoreFieldmap_m->getFrequency()) / frequency_m) > 0.01) {
                errormsg << "FREQUENCY IN INPUT FILE DIFFERENT THAN IN FIELD MAP '" <<  CoreFilename_m + "';\n"
                         << frequency_m / two_pi * 1e-6 << " MHz <> "
                         << CoreFieldmap_m->getFrequency() / two_pi * 1e-6 << " MHz; TAKE ON THE LATTER\n";
407
                std::string errormsg_str = Fieldmap::typeset_msg(errormsg.str(), "warning");
gsell's avatar
gsell committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
                msg << errormsg_str << "\n"
                    << endl;
                if(Ippl::myNode() == 0) {
                    ofstream omsg("errormsg.txt", ios_base::app);
                    omsg << errormsg_str << endl;
                    omsg.close();
                }
                frequency_m = CoreFieldmap_m->getFrequency();
            }


            /**
               NOT needed anymore in the new AUTOPHASE procedure
               const double dPhi = OpalData::getInstance()->getGlobalPhaseShift() * frequency_m;

               msg << "Global Phase-Shift is " << dPhi / two_pi * 360.0 << " (deg)"
               << " old phase = " << phase_m << " (rad) ";
               phase_m -= dPhi;
               msg << "new phase = " << phase_m << " (rad) " << endl;
            */

adelmann's avatar
adelmann committed
429 430
            if(dx_m > 1e-10 || dy_m > 1e-10)
                msg << "misaligned by dx = " << dx_m << ", dy = " << dy_m << endl;
gsell's avatar
gsell committed
431 432 433 434 435 436 437

            if(hasAttribute("MODE")) {
                Mode_m = getAttribute("MODE");
            } else {
                Mode_m = 1. / 3.;
                errormsg.str("");
                errormsg  << "NO MODE GIVEN; 2\\pi/3 MODE ASSUMED.";
438
                std::string errormsg_str = Fieldmap::typeset_msg(errormsg.str(), "warning");
gsell's avatar
gsell committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
                msg << errormsg_str << "\n"
                    << endl;
                if(Ippl::myNode() == 0) {
                    ofstream omsg("errormsg.txt", ios_base::app);
                    omsg << errormsg_str << endl;
                    omsg.close();
                }
            }

            PeriodLength_m = (zEnd - zBegin) / 2.0;
            CellLength_m = PeriodLength_m * Mode_m;

            startCoreField_m = PeriodLength_m / 2.0;
            startExitField_m = startCoreField_m + (NumCells_m - 1) * CellLength_m;
            mappedStartExitField_m = startExitField_m - 3.0 * PeriodLength_m / 2.0;

            startField -= PeriodLength_m / 2.0;
            endField = startField + startExitField_m + PeriodLength_m / 2.0;
            startField_m = startField;

            scaleCore_m = scale_m / sin(2.0 * pi * Mode_m);
            phaseCore1_m = phase_m + pi * Mode_m / 2.0;
            phaseCore2_m = phase_m + pi * Mode_m * 1.5;
            phaseExit_m = phase_m - 2.0 * pi * ((NumCells_m - 1) * Mode_m - floor((NumCells_m - 1) * Mode_m));

        } else {
            endField = startField - 1e-3;
        }
    } else {
        endField = startField - 1e-3;
    }
}

void TravelingWave::finalise()
{}

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


kraus's avatar
kraus committed
480
void TravelingWave::goOnline(const double &) {
gsell's avatar
gsell committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494
    Fieldmap::readMap(CoreFilename_m);
    online_m = true;
}

void TravelingWave::goOffline() {
    Fieldmap::freeMap(CoreFilename_m);
}

void TravelingWave::getDimensions(double &zBegin, double &zEnd) const {
    zBegin = startField_m;
    zEnd = startField_m + (NumCells_m - 1) * CellLength_m + PeriodLength_m;
}


495 496
ElementBase::ElementType TravelingWave::getType() const {
    return TRAVELINGWAVE;
gsell's avatar
gsell committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
}

double TravelingWave::getAutoPhaseEstimate(const double &E0, const double &t0, const double &q, const double &mass) {
    vector<double> t, E, t2, E2;
    vector<pair<double, double> > F;
    double Dz;
    int N1, N2, N3, N4;
    double A, B;
    double phi = 0.0, tmp_phi, dphi = 0.5 * Physics::pi / 180.;
    double phaseC1 = phaseCore1_m - phase_m;
    double phaseC2 = phaseCore2_m - phase_m;
    double phaseE = phaseExit_m - phase_m;

    CoreFieldmap_m->getOnaxisEz(F);
    if(F.size() > 0) {
        N1 = static_cast<int>(floor(F.size() / 4.)) + 1;
        N2 = F.size() - 2 * N1 + 1;
        N3 = 2 * N1 + static_cast<int>(floor((NumCells_m - 1) * N2 * Mode_m)) - 1;
        N4 = static_cast<int>(floor(0.5 + N2 * Mode_m));
        Dz = F[N1 + N2].first - F[N1].first;

        t.resize(N3, t0);
        t2.resize(N3, t0);
        E.resize(N3, E0);
        E2.resize(N3, E0);
        for(int i = 1; i < N1; ++ i) {
523
            E[i] = E0 + (F[i].first + F[i - 1].first) / 2. * scale_m / mass;
gsell's avatar
gsell committed
524 525 526 527
            E2[i] = E[i];
        }
        for(int i = N1; i < N3 - N1 + 1; ++ i) {
            int I = (i - N1) % N2 + N1;
528
            double Z = (F[I].first + F[I - 1].first) / 2. + floor((i - N1) / N2) * Dz;
gsell's avatar
gsell committed
529 530 531 532 533
            E[i] = E0 + Z * scaleCore_m / mass;
            E2[i] = E[i];
        }
        for(int i = N3 - N1 + 1; i < N3; ++ i) {
            int I = i - N3 - 1 + 2 * N1 + N2;
534
            double Z = (F[I].first + F[I - 1].first) / 2. + ((NumCells_m - 1) * Mode_m - 1) * Dz;
gsell's avatar
gsell committed
535 536 537 538 539 540 541
            E[i] = E0 + Z * scale_m / mass;
            E2[i] = E[i];
        }

        for(int iter = 0; iter < 10; ++ iter) {
            A = B = 0.0;
            for(int i = 1; i < N1; ++ i) {
542 543
                t[i] = t[i - 1] + getdT(i, i, E, F, mass);
                t2[i] = t2[i - 1] + getdT(i, i, E2, F, mass);
gsell's avatar
gsell committed
544 545 546 547 548 549
                A += scale_m * (1. + frequency_m * (t2[i] - t[i]) / dphi) * getdA(i, i, t, 0.0, F);
                B += scale_m * (1. + frequency_m * (t2[i] - t[i]) / dphi) * getdB(i, i, t, 0.0, F);
            }
            for(int i = N1; i < N3 - N1 + 1; ++ i) {
                int I = (i - N1) % N2 + N1;
                int J = (i - N1 + N4) % N2 + N1;
550 551
                t[i] = t[i - 1] + getdT(i, I, E, F, mass);
                t2[i] = t2[i - 1] + getdT(i, I, E2, F, mass);
gsell's avatar
gsell committed
552 553 554 555 556
                A += scaleCore_m * (1. + frequency_m * (t2[i] - t[i]) / dphi) * (getdA(i, I, t, phaseC1, F) + getdA(i, J, t, phaseC2, F));
                B += scaleCore_m * (1. + frequency_m * (t2[i] - t[i]) / dphi) * (getdB(i, I, t, phaseC1, F) + getdB(i, J, t, phaseC2, F));
            }
            for(int i = N3 - N1 + 1; i < N3; ++ i) {
                int I = i - N3 - 1 + 2 * N1 + N2;
557 558
                t[i] = t[i - 1] + getdT(i, I, E, F, mass);
                t2[i] = t2[i - 1] + getdT(i, I, E2, F, mass);
gsell's avatar
gsell committed
559 560 561 562 563 564 565 566 567 568 569 570 571
                A += scale_m * (1. + frequency_m * (t2[i] - t[i]) / dphi) * getdA(i, I, t, phaseE, F);
                B += scale_m * (1. + frequency_m * (t2[i] - t[i]) / dphi) * getdB(i, I, t, phaseE, F);
            }

            if(fabs(B) > 0.0000001) {
                tmp_phi = atan(A / B);
            } else {
                tmp_phi = Physics::pi / 2;
            }
            if(q * (A * sin(tmp_phi) + B * cos(tmp_phi)) < 0) {
                tmp_phi += Physics::pi;
            }

572
            if(fabs(phi - tmp_phi) < frequency_m * (t[N3 - 1] - t[0]) / N3) {
gsell's avatar
gsell committed
573
                for(int i = 1; i < N1; ++ i) {
574
                    E[i] = E[i - 1] + q * scale_m * getdE(i, i, t, phi, F);
gsell's avatar
gsell committed
575 576 577 578
                }
                for(int i = N1; i < N3 - N1 + 1; ++ i) {
                    int I = (i - N1) % N2 + N1;
                    int J = (i - N1 + N4) % N2 + N1;
579
                    E[i] = E[i - 1] + q * scaleCore_m * (getdE(i, I, t, phi + phaseC1, F) + getdE(i, J, t, phi + phaseC2, F));
gsell's avatar
gsell committed
580 581 582
                }
                for(int i = N3 - N1 + 1; i < N3; ++ i) {
                    int I = i - N3 - 1 + 2 * N1 + N2;
583
                    E[i] = E[i - 1] + q * scale_m * getdE(i, I, t, phi + phaseE, F);
gsell's avatar
gsell committed
584
                }
kraus's avatar
kraus committed
585 586 587 588

                const int prevPrecision = Ippl::Info->precision(8);
                INFOMSG("estimated phi= " << tmp_phi << " rad, "
                        << "Ekin= " << E[N3 - 1] << " MeV" << setprecision(prevPrecision) << endl);
589
                //                 std::stringstream fn;
gsell's avatar
gsell committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
                //                 fn << getName() << ".dat";
                //                 ofstream ckrtest(fn.str().c_str());
                //                 ckrtest << setprecision(12);
                //                 for (int i = 0; i < N1; ++ i) {
                //                     ckrtest << startField_m + F[i].first << "\t"
                //                             << t[i] << "\t"
                //                             << scale_m * cos(frequency_m * t[i] + phi) * F[i].second << "\t"
                //                             << E[i] << "\t"
                //                             << cos(frequency_m * t[i] + phi) << "\t"
                //                             << i << endl;
                //                 }
                //                 for (int i = N1; i < N3 - N1 + 1; ++ i) {
                //                     int I = (i - N1) % N2 + N1;
                //                     int J = (i - N1 + N4) % N2 + N1;
                //                     double Z = startField_m + F[I].first + floor((i - N1) / N2) * Dz;
                //                     ckrtest << Z << "\t"
                //                             << t[i] << "\t"
                //                             << scaleCore_m * (cos(frequency_m * t[i] + phaseC1 + phi) * F[I].second +
                //                                               cos(frequency_m * t[i] + phaseC2 + phi) * F[J].second) << "\t"
                //                             << E[i] << "\t"
                //                             << frequency_m * t[i] + phi << "\t"
                //                             << I << endl;
                //                 }
                //                 for (int i = N3 - N1 + 1; i < N3; ++ i) {
                //                     int I = i - N3 - 1 + 2 * N1 + N2;
                //                     double Z = startField_m + F[I].first + ((NumCells_m - 1) * Mode_m - 1) * Dz;
                //                     ckrtest << Z << "\t"
                //                             << t[i] << "\t"
                //                             << scale_m * cos(frequency_m * t[i] + phaseE + phi) * F[I].second << "\t"
                //                             << E[i] << "\t"
                //                             << frequency_m * t[i] + phi << "\t"
                //                             << I << endl;
                //                 }
                //                 ckrtest.close();
                return tmp_phi;
            }
            phi = tmp_phi - floor(tmp_phi / Physics::two_pi + 0.5) * Physics::two_pi;


            for(int i = 1; i < N1; ++ i) {
630 631 632 633 634 635
                E[i] = E[i - 1] + q * scale_m * getdE(i, i, t, phi, F);
                E2[i] = E2[i - 1] + q * scale_m * getdE(i, i, t, phi + dphi, F); // should I use here t or t2?
                t[i] = t[i - 1] + getdT(i, i, E, F, mass);
                t2[i] = t2[i - 1] + getdT(i, i, E2, F, mass);
                E[i] = E[i - 1] + q * scale_m * getdE(i, i, t, phi, F);
                E2[i] = E2[i - 1] + q * scale_m * getdE(i, i, t2, phi + dphi, F);
gsell's avatar
gsell committed
636 637 638 639
            }
            for(int i = N1; i < N3 - N1 + 1; ++ i) {
                int I = (i - N1) % N2 + N1;
                int J = (i - N1 + N4) % N2 + N1;
640 641 642 643 644 645
                E[i] = E[i - 1] + q * scaleCore_m * (getdE(i, I, t, phi + phaseC1, F) + getdE(i, J, t, phi + phaseC2, F));
                E2[i] = E2[i - 1] + q * scaleCore_m * (getdE(i, I, t, phi + phaseC1 + dphi, F) + getdE(i, J, t, phi + phaseC2 + dphi, F)); //concerning t: see above
                t[i] = t[i - 1] + getdT(i, I, E, F, mass);
                t2[i] = t2[i - 1] + getdT(i, I, E2, F, mass);
                E[i] = E[i - 1] + q * scaleCore_m * (getdE(i, I, t, phi + phaseC1, F) + getdE(i, J, t, phi + phaseC2, F));
                E2[i] = E2[i - 1] + q * scaleCore_m * (getdE(i, I, t2, phi + phaseC1 + dphi, F) + getdE(i, J, t2, phi + phaseC2 + dphi, F));
gsell's avatar
gsell committed
646 647 648
            }
            for(int i = N3 - N1 + 1; i < N3; ++ i) {
                int I = i - N3 - 1 + 2 * N1 + N2;
649 650 651 652 653 654
                E[i] = E[i - 1] + q * scale_m * getdE(i, I, t, phi + phaseE, F);
                E2[i] = E2[i - 1] + q * scale_m * getdE(i, I, t, phi + phaseE + dphi, F); //concerning t: see above
                t[i] = t[i - 1] + getdT(i, I, E, F, mass);
                t2[i] = t2[i - 1] + getdT(i, I, E2, F, mass);
                E[i] = E[i - 1] + q * scale_m * getdE(i, I, t, phi + phaseE, F);
                E2[i] = E2[i - 1] + q * scale_m * getdE(i, I, t2, phi + phaseE + dphi, F);
gsell's avatar
gsell committed
655 656 657 658 659
            }
            //             msg << ", Ekin= " << E[N3-1] << " MeV" << endl;
        }


kraus's avatar
kraus committed
660 661 662 663
        const int prevPrecision = Ippl::Info->precision(8);
        INFOMSG("estimated phi= " << tmp_phi << " rad, "
                << "Ekin= " << E[N3 - 1] << " MeV" << setprecision(prevPrecision) << endl);

gsell's avatar
gsell committed
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
        return phi;
    } else {
        return 0.0;
    }
}

pair<double, double> TravelingWave::trackOnAxisParticle(const double &p0,
        const double &t0,
        const double &dt,
        const double &q,
        const double &mass) {
    double phase = frequency_m * t0 + phase_m;
    double p = p0;
    double t = t0;
    double cdt = Physics::c * dt;
    double dphi = frequency_m * dt;
    vector<pair<double, double> > F;
    CoreFieldmap_m->getOnaxisEz(F);

    double *zvals = new double[F.size()];
    double *onAxisField = new double[F.size()];
    double Ezmax = 0.0;
    for(unsigned int i = 0; i < F.size(); ++i) {
        zvals[i] = F[i].first;
        onAxisField[i] = F[i].second;
        if(fabs(onAxisField[i]) > Ezmax) Ezmax = fabs(onAxisField[i]);
    }
    //    Ezmax /= 1e6;
    double z = zvals[0];
    double zbegin = z;

    gsl_spline *onAxisInterpolants = gsl_spline_alloc(gsl_interp_cspline, F.size());
    gsl_interp_accel *onAxisAccel = gsl_interp_accel_alloc();
    gsl_spline_init(onAxisInterpolants, zvals, onAxisField, F.size());

    delete[] zvals;
    delete[] onAxisField;

    double dz = 0.5 * p / sqrt(1.0 + p * p) * cdt;
    while(z + dz < startCoreField_m + zbegin) {
        z += dz;
        double ez = scale_m / Ezmax * cos(phase) * gsl_spline_eval(onAxisInterpolants, z, onAxisAccel);
        p += ez * q * cdt / mass;
        dz = 0.5 * p / sqrt(1.0 + p * p) * cdt;
        z += 0.5 * p / sqrt(1.0 + p * p) * cdt;
        phase += dphi;
        t += dt;
    }
    double phase2 = phase - phase_m + phaseCore2_m;
    phase = phase - phase_m + phaseCore1_m;
    while(z + dz < startExitField_m + zbegin) {
        z += dz;

        double tmpz = z - (startCoreField_m + zbegin);
        tmpz -= PeriodLength_m * floor(tmpz / PeriodLength_m);
        tmpz += startCoreField_m + zbegin;

        double ez = scaleCore_m / Ezmax * cos(phase) * gsl_spline_eval(onAxisInterpolants, tmpz, onAxisAccel);

        tmpz = z - (startCoreField_m + zbegin) + CellLength_m;
        tmpz -= PeriodLength_m * floor(tmpz / PeriodLength_m);
        tmpz += startCoreField_m + zbegin;

        ez += scaleCore_m / Ezmax * cos(phase2) * gsl_spline_eval(onAxisInterpolants, tmpz, onAxisAccel);

        p += ez * q * cdt / mass;
730
        dz = 0.5 * p / sqrt(1.0 + p * p) * cdt;
gsell's avatar
gsell committed
731 732 733 734 735 736 737 738 739 740 741 742 743
        z += dz;
        phase += dphi;
        phase2 += dphi;
        t += dt;
    }
    phase = phase - phaseCore1_m + phaseExit_m;
    while(z + dz < startExitField_m + 0.5 * PeriodLength_m + zbegin) {
        z += dz;
        double tmpz = z - (startExitField_m + zbegin);
        tmpz -= PeriodLength_m * floor(tmpz / PeriodLength_m);
        tmpz += 3.0 * PeriodLength_m / 2.0;
        double ez = scale_m / Ezmax * cos(phase) * gsl_spline_eval(onAxisInterpolants, tmpz, onAxisAccel);
        p += ez * q * cdt / mass;
744
        dz = 0.5 * p / sqrt(1.0 + p * p) * cdt;
gsell's avatar
gsell committed
745 746 747 748 749 750 751 752 753 754 755 756
        z += dz;
        phase += dphi;
        t += dt;
    }

    gsl_spline_free(onAxisInterpolants);
    gsl_interp_accel_free(onAxisAccel);

    const double beta = sqrt(1. - 1 / (p * p + 1.));
    const double tErr  = (z - (startExitField_m + 0.5 * PeriodLength_m + zbegin)) / (Physics::c * beta);

    return pair<double, double>(p, t - tErr);
757
}