AutophaseTracker.cpp 19 KB
Newer Older
kraus's avatar
kraus committed
1
#include "Algorithms/AutophaseTracker.h"
kraus's avatar
kraus committed
2
#include "Utilities/ClassicField.h"
kraus's avatar
kraus committed
3 4
#include "AbstractObjects/OpalData.h"
#include "Utilities/OpalOptions.h"
kraus's avatar
kraus committed
5
#include "Utilities/Timer.h"
kraus's avatar
kraus committed
6

7 8 9
#include <iostream>
#include <fstream>

kraus's avatar
kraus committed
10 11
#define DTFRACTION 2

kraus's avatar
kraus committed
12 13
extern Inform *gmsg;

kraus's avatar
kraus committed
14 15 16 17 18
AutophaseTracker::AutophaseTracker(const Beamline &beamline,
                                   const PartData& data,
                                   const double &T0,
                                   double initialR,
                                   double initialP):
kraus's avatar
kraus committed
19 20
    DefaultVisitor(beamline, false, false),
    itsBunch_m(&data),
kraus's avatar
kraus committed
21 22
    itsPusher_m(data),
    particleLayout_m(NULL)
kraus's avatar
kraus committed
23
{
kraus's avatar
kraus committed
24 25
    particleLayout_m = new Layout_t();
    itsBunch_m.initialize(particleLayout_m);
kraus's avatar
kraus committed
26
    itsBunch_m.setT(T0);
kraus's avatar
kraus committed
27

kraus's avatar
kraus committed
28 29 30 31 32 33
    if(Ippl::myNode() == 0) {
        itsBunch_m.create(1);
        itsBunch_m.R[0] = Vector_t(0.0, 0.0, initialR);
        itsBunch_m.P[0] = Vector_t(0.0, 0.0, initialP);
        itsBunch_m.Bin[0] = 0;
        itsBunch_m.Q[0] = itsBunch_m.getChargePerParticle();
kraus's avatar
kraus committed
34
        itsBunch_m.PType[0] = ParticleType::REGULAR;
kraus's avatar
kraus committed
35 36 37
        itsBunch_m.LastSection[0] = 0;
    }

kraus's avatar
kraus committed
38
    visitBeamline(beamline);
kraus's avatar
kraus committed
39
    itsOpalBeamline_m.prepareSections();
kraus's avatar
kraus committed
40 41 42 43 44 45 46 47 48
}

AutophaseTracker::~AutophaseTracker()
{ }

void AutophaseTracker::execute(const std::queue<double> &dtAllTracks,
                               const std::queue<double> &maxZ,
                               const std::queue<unsigned long long> &maxTrackSteps) {

49 50 51 52
    if (Ippl::myNode() != 0) {
        receiveCavityPhases();
        return;
    }
kraus's avatar
kraus committed
53 54
    OPALTimer::Timer clock;
    *gmsg << level1 << "\n\nstart autophasing at " << clock.time() << "\n" << endl;
kraus's avatar
kraus committed
55

kraus's avatar
kraus committed
56 57 58 59 60 61
    Vector_t rmin, rmax;
    Vector_t &refR = itsBunch_m.R[0];
    Vector_t &refP = itsBunch_m.P[0];
    std::queue<double> dtAutoPhasing(dtAllTracks);
    std::queue<double> maxSPosAutoPhasing(maxZ);
    std::queue<unsigned long long> maxStepsAutoPhasing(maxTrackSteps);
kraus's avatar
kraus committed
62

kraus's avatar
kraus committed
63
    maxSPosAutoPhasing.back() = itsOpalBeamline_m.calcBeamlineLenght();
kraus's avatar
kraus committed
64

kraus's avatar
kraus committed
65
    size_t step = 0;
kraus's avatar
kraus committed
66 67 68
    const int dtfraction = DTFRACTION;
    double newDt = dtAutoPhasing.front() / dtfraction;
    double scaleFactor = newDt * Physics::c;
kraus's avatar
kraus committed
69

kraus's avatar
kraus committed
70 71
    itsBunch_m.LastSection[0] -= 1;
    itsOpalBeamline_m.getSectionIndexAt(refR, itsBunch_m.LastSection[0]);
kraus's avatar
kraus committed
72
    itsOpalBeamline_m.print(*gmsg);
kraus's avatar
kraus committed
73

kraus's avatar
kraus committed
74
    itsOpalBeamline_m.switchAllElements();
kraus's avatar
kraus committed
75

kraus's avatar
kraus committed
76
    std::shared_ptr<Component> cavity = NULL;
kraus's avatar
kraus committed
77
    while (true) {
kraus's avatar
kraus committed
78
        std::shared_ptr<Component> next = getNextCavity(cavity);
kraus's avatar
kraus committed
79 80
        if (next == NULL) break;

kraus's avatar
kraus committed
81 82
        double endNext = getEndCavity(next);
        if (refR(2) < endNext) break;
kraus's avatar
kraus committed
83 84 85

        cavity = next;
    }
kraus's avatar
kraus committed
86

kraus's avatar
kraus committed
87 88
    while (maxStepsAutoPhasing.size() > 0) {
        maxStepsAutoPhasing.front() = maxStepsAutoPhasing.front() * dtfraction + step;
kraus's avatar
kraus committed
89 90
        newDt = dtAutoPhasing.front() / dtfraction;
        scaleFactor = newDt * Physics::c;
kraus's avatar
kraus committed
91 92 93
        Vector_t vscaleFactor = Vector_t(scaleFactor);
        itsBunch_m.setdT(newDt);
        itsBunch_m.dt = newDt;
kraus's avatar
kraus committed
94

kraus's avatar
kraus committed
95 96
        INFOMSG(level2 << "\n"
                << "*************************************************\n"
kraus's avatar
kraus committed
97
                << "new set of dt, zstop and max number of time steps\n"
kraus's avatar
kraus committed
98
                << "*************************************************\n\n"
kraus's avatar
kraus committed
99
                << "start at t = " << itsBunch_m.getT() << " [s], "
kraus's avatar
kraus committed
100
                << "zstop at z = " << maxSPosAutoPhasing.front() << " [m],\n"
kraus's avatar
kraus committed
101 102 103
                << "dt = " << itsBunch_m.dt[0] << " [s], "
                << "step = " << step << ", "
                << "R =  " << refR << " [m]" << endl);
kraus's avatar
kraus committed
104

kraus's avatar
kraus committed
105
        for(; step < maxStepsAutoPhasing.front(); ++ step) {
kraus's avatar
kraus committed
106
            std::shared_ptr<Component> next = getNextCavity(cavity);
kraus's avatar
kraus committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120
            if (next == NULL) break;

            double beginNext, endNext;
            next->getDimensions(beginNext, endNext);
            double sStop = beginNext;
            bool reachesNextCavity = true;
            if (sStop > maxSPosAutoPhasing.front()) {
                sStop = maxSPosAutoPhasing.front();
                reachesNextCavity = false;
            }

            advanceTime(step, maxStepsAutoPhasing.front(), sStop);
            if (step >= maxStepsAutoPhasing.front() || !reachesNextCavity) break;

kraus's avatar
kraus committed
121 122
            INFOMSG(level2 << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
            INFOMSG(level2 << "\nFound " << next->getName()
kraus's avatar
kraus committed
123 124
                    << " at " << sStop << " [m], "
                    << "step  " << step << ", "
kraus's avatar
kraus committed
125 126
                    << "t = " << itsBunch_m.getT() << " [s], "
                    << "E = " << getEnergyMeV(refP) << " [MeV]\n\n"
127
                    << "start phase scan ... " << endl;);
kraus's avatar
kraus committed
128 129 130 131 132 133

            double guess = guessCavityPhase(next);
            optimizeCavityPhase(next,
                                guess,
                                step,
                                newDt);
kraus's avatar
kraus committed
134

135
            INFOMSG(level2 << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl);
kraus's avatar
kraus committed
136 137 138 139 140 141 142 143 144

            track(endNext,
                  step,
                  dtAutoPhasing,
                  maxSPosAutoPhasing,
                  maxStepsAutoPhasing);

            cavity = next;
        }
kraus's avatar
kraus committed
145

kraus's avatar
kraus committed
146
        if (dtAutoPhasing.size() == 0) break;
kraus's avatar
kraus committed
147

kraus's avatar
kraus committed
148 149 150 151
        dtAutoPhasing.pop();
        maxSPosAutoPhasing.pop();
        maxStepsAutoPhasing.pop();
    }
152

153
    printCavityPhases();
154 155
    sendCavityPhases();

156
    *gmsg << level1 << "\n\nfinished autophasing " << clock.time() << "\n\n"  << endl;
kraus's avatar
kraus committed
157 158 159 160 161 162 163 164 165 166 167
}

void AutophaseTracker::track(double uptoSPos,
                             size_t &step,
                             std::queue<double> &dtAutoPhasing,
                             std::queue<double> &maxSPosAutoPhasing,
                             std::queue<unsigned long long> &maxStepsAutoPhasing) {
    Vector_t &refR = itsBunch_m.R[0];
    Vector_t &refP = itsBunch_m.P[0];
    double t = itsBunch_m.getT();
    const int dtfraction = DTFRACTION;
kraus's avatar
kraus committed
168

kraus's avatar
kraus committed
169 170 171 172 173 174 175
    while (maxStepsAutoPhasing.size() > 0) {
        maxStepsAutoPhasing.front() = maxStepsAutoPhasing.front() * dtfraction + step;
        double newDt = dtAutoPhasing.front() / dtfraction;
        double scaleFactor = newDt * Physics::c;
        Vector_t vscaleFactor = Vector_t(scaleFactor);
        itsBunch_m.setdT(newDt);
        itsBunch_m.dt[0] = newDt;
kraus's avatar
kraus committed
176

kraus's avatar
kraus committed
177
        for(; step < maxStepsAutoPhasing.front(); ++ step) {
kraus's avatar
kraus committed
178
            IpplTimings::startTimer(timeIntegrationTimer_m);
kraus's avatar
kraus committed
179

kraus's avatar
kraus committed
180
            refR /= vscaleFactor;
kraus's avatar
kraus committed
181
            itsPusher_m.push(refR, refP, itsBunch_m.dt[0]);
kraus's avatar
kraus committed
182
            refR *= vscaleFactor;
kraus's avatar
kraus committed
183

kraus's avatar
kraus committed
184
            IpplTimings::stopTimer(timeIntegrationTimer_m);
kraus's avatar
kraus committed
185

kraus's avatar
kraus committed
186
            evaluateField();
kraus's avatar
kraus committed
187

kraus's avatar
kraus committed
188
            IpplTimings::startTimer(timeIntegrationTimer_m);
kraus's avatar
kraus committed
189

kraus's avatar
kraus committed
190
            itsPusher_m.kick(refR, refP, itsBunch_m.Ef[0], itsBunch_m.Bf[0], itsBunch_m.dt[0]);
kraus's avatar
kraus committed
191

kraus's avatar
kraus committed
192
            refR /= vscaleFactor;
kraus's avatar
kraus committed
193
            itsPusher_m.push(refR, refP, itsBunch_m.dt[0]);
kraus's avatar
kraus committed
194
            refR *= vscaleFactor;
kraus's avatar
kraus committed
195

kraus's avatar
kraus committed
196 197 198
            t += itsBunch_m.getdT();
            itsBunch_m.setT(t);
            IpplTimings::stopTimer(timeIntegrationTimer_m);
kraus's avatar
kraus committed
199

kraus's avatar
kraus committed
200
            if (refR(2) > uptoSPos) return;
kraus's avatar
kraus committed
201

kraus's avatar
kraus committed
202 203
            if(refR(2) > maxSPosAutoPhasing.front())
                maxStepsAutoPhasing.front() = step;
kraus's avatar
kraus committed
204

kraus's avatar
kraus committed
205
            if(step % 5000 == 0) {
206 207 208
                INFOMSG(level2 << "step = " << step << ", spos = " << refR(2) << " [m], "
                        << "t= " << itsBunch_m.getT() << " [s], " << "E = " << getEnergyMeV(refP) << " [MeV] "
                        << endl);
kraus's avatar
kraus committed
209 210
            }
        }
kraus's avatar
kraus committed
211

kraus's avatar
kraus committed
212 213 214 215
        dtAutoPhasing.pop();
        maxSPosAutoPhasing.pop();
        maxStepsAutoPhasing.pop();
    }
kraus's avatar
kraus committed
216 217
}

kraus's avatar
kraus committed
218 219
std::shared_ptr<Component> AutophaseTracker::getNextCavity(const std::shared_ptr<Component> &current) {
    auto cavities = itsOpalBeamline_m.getSuccessors(current);
kraus's avatar
kraus committed
220 221

    if (cavities.size() > 0) {
kraus's avatar
kraus committed
222
        return cavities.front();
kraus's avatar
kraus committed
223 224
    }

kraus's avatar
kraus committed
225 226
    std::shared_ptr<Component> noCavity;
    return noCavity;
kraus's avatar
kraus committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
}

void AutophaseTracker::advanceTime(size_t &step, const size_t maxSteps, const double beginNextCavity) {
    Vector_t &refR = itsBunch_m.R[0];
    Vector_t &refP = itsBunch_m.P[0];

    const double distance = beginNextCavity - refR(2);
    if (distance < 0.0) return;

    const double beta = refP(2) / sqrt(dot(refP, refP) + 1.0);
    size_t  numSteps = std::floor(distance / (beta * Physics::c * itsBunch_m.dt[0]));
    numSteps = std::min(numSteps, maxSteps - step);
    double timeDifference = numSteps * itsBunch_m.dt[0];

    itsBunch_m.setT(timeDifference + itsBunch_m.getT());
    step += numSteps;
kraus's avatar
kraus committed
243
    refR = Vector_t(0.0, 0.0, refR(2) + beta * Physics::c * timeDifference);
kraus's avatar
kraus committed
244 245 246
    refP = Vector_t(0.0, 0.0, sqrt(dot(refP, refP)));
}

kraus's avatar
kraus committed
247
double AutophaseTracker::guessCavityPhase(const std::shared_ptr<Component> &cavity) {
kraus's avatar
kraus committed
248 249 250 251 252 253
    Vector_t &refR = itsBunch_m.R[0];
    Vector_t &refP = itsBunch_m.P[0];

    double cavityStart = getBeginCavity(cavity);
    double orig_phi = 0.0, Phimax = 0.0;

kraus's avatar
kraus committed
254
    const double beta = std::max(refP(2) / sqrt(dot(refP, refP) + 1.0), 1e-6);
kraus's avatar
kraus committed
255 256 257
    const double tErr  = (cavityStart - refR(2)) / (Physics::c * beta);

    bool apVeto;
258
    if(cavity->getType() == ElementBase::TRAVELINGWAVE) {
kraus's avatar
kraus committed
259
        TravelingWave *element = static_cast<TravelingWave *>(cavity.get());
kraus's avatar
kraus committed
260 261 262
        orig_phi = element->getPhasem();
        apVeto = element->getAutophaseVeto();
        if(apVeto) {
263
            INFOMSG(level2 << " ----> APVETO -----> "
kraus's avatar
kraus committed
264 265 266
                    << element->getName() <<  endl);
            return orig_phi;
        }
267 268
        INFOMSG(level2 << cavity->getName() << ", "
                << "phase = " << orig_phi << " rad" << endl);
kraus's avatar
kraus committed
269 270 271 272 273 274

        Phimax = element->getAutoPhaseEstimate(getEnergyMeV(refP),
                                               itsBunch_m.getT() + tErr,
                                               itsBunch_m.getQ(),
                                               itsBunch_m.getM() * 1e-6);
    } else {
kraus's avatar
kraus committed
275
        RFCavity *element = static_cast<RFCavity *>(cavity.get());
kraus's avatar
kraus committed
276 277 278
        orig_phi = element->getPhasem();
        apVeto = element->getAutophaseVeto();
        if(apVeto) {
279
            INFOMSG(level2 << " ----> APVETO -----> "
kraus's avatar
kraus committed
280 281 282
                    << element->getName() << endl);
            return orig_phi;
        }
283 284
        INFOMSG(level2 << cavity->getName() << ", "
                << "phase = " << orig_phi << " rad" << endl);
kraus's avatar
kraus committed
285 286 287 288 289 290 291 292 293 294

        Phimax = element->getAutoPhaseEstimate(getEnergyMeV(refP),
                                               itsBunch_m.getT() + tErr,
                                               itsBunch_m.getQ(),
                                               itsBunch_m.getM() * 1e-6);
    }

    return Phimax;
}

kraus's avatar
kraus committed
295
double AutophaseTracker::optimizeCavityPhase(const std::shared_ptr<Component> &cavity,
kraus's avatar
kraus committed
296 297 298 299
                                             double initialPhase,
                                             size_t currentStep,
                                             double dt) {

300
    if(cavity->getType() == ElementBase::TRAVELINGWAVE) {
kraus's avatar
kraus committed
301
        if (static_cast<TravelingWave *>(cavity.get())->getAutophaseVeto()) return initialPhase;
kraus's avatar
kraus committed
302
    } else {
kraus's avatar
kraus committed
303
        if (static_cast<RFCavity *>(cavity.get())->getAutophaseVeto()) return initialPhase;
kraus's avatar
kraus committed
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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    }

    itsBunch_m.setdT(dt);
    itsBunch_m.dt[0] = dt;
    double Phimax = initialPhase;
    double phi = initialPhase;
    double originalPhase = 0.0, PhiAstra = 0.0;
    double dphi = Physics::pi / 360.0;
    double cavityStart = getBeginCavity(cavity);
    const int numRefinements = Options::autoPhase;

    int j = -1;

    double E = APtrack(cavity, cavityStart, phi);
    double Emax = E;
    do {
        j ++;
        Emax = E;
        initialPhase = phi;
        phi -= dphi;
        E = APtrack(cavity, cavityStart, phi);
    } while(E > Emax);

    if(j == 0) {
        phi = initialPhase;
        E = Emax;
        j = -1;
        do {
            j ++;
            Emax = E;
            initialPhase = phi;
            phi += dphi;
            E = APtrack(cavity, cavityStart, phi);
        } while(E > Emax);
    }

    for(int rl = 0; rl < numRefinements; ++ rl) {
        dphi /= 2.;
        phi = initialPhase - dphi;
        E = APtrack(cavity, cavityStart, phi);
        if(E > Emax) {
            initialPhase = phi;
            Emax = E;
        } else {
            phi = initialPhase + dphi;
            E = APtrack(cavity, cavityStart, phi);
            if(E > Emax) {
                initialPhase = phi;
                Emax = E;
            }
        }
    }
356
    Phimax = std::fmod(initialPhase, 2 * Physics::pi);
kraus's avatar
kraus committed
357

358
    if(cavity->getType() == ElementBase::TRAVELINGWAVE) {
kraus's avatar
kraus committed
359
        TravelingWave *element = static_cast<TravelingWave *>(cavity.get());
kraus's avatar
kraus committed
360
        originalPhase = element->getPhasem();
361 362
        double newPhase = std::fmod(originalPhase + Phimax, 2 * Physics::pi);
        element->updatePhasem(newPhase);
kraus's avatar
kraus committed
363
    } else {
kraus's avatar
kraus committed
364
        RFCavity *element = static_cast<RFCavity *>(cavity.get());
kraus's avatar
kraus committed
365
        originalPhase = element->getPhasem();
366 367
        double newPhase = std::fmod(originalPhase + Phimax, 2 * Physics::pi);
        element->updatePhasem(newPhase);
kraus's avatar
kraus committed
368 369
    }

370
    PhiAstra = std::fmod((Phimax * Physics::rad2deg) + 90.0, 360.0);
kraus's avatar
kraus committed
371 372 373

    INFOMSG(cavity->getName() << "_phi = "  << Phimax << " rad / "
            << Phimax *Physics::rad2deg <<  " deg, AstraPhi = " << PhiAstra << " deg,\n"
kraus's avatar
kraus committed
374
            << "E = " << Emax << " (MeV), " << "phi_nom = " << originalPhase *Physics::rad2deg << "\n");
kraus's avatar
kraus committed
375 376 377 378

    OpalData::getInstance()->setMaxPhase(cavity->getName(), Phimax);

    return Phimax + originalPhase;
kraus's avatar
kraus committed
379 380
}

kraus's avatar
kraus committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
void AutophaseTracker::evaluateField() {
    IpplTimings::startTimer(fieldEvaluationTimer_m);

    Vector_t &refR = itsBunch_m.R[0];

    itsBunch_m.Ef = Vector_t(0.0);
    itsBunch_m.Bf = Vector_t(0.0);

    long ls = itsBunch_m.LastSection[0];
    itsOpalBeamline_m.getSectionIndexAt(refR, ls);
    itsBunch_m.LastSection[0] = ls;

    Vector_t externalE, externalB;
    itsOpalBeamline_m.getFieldAt(0, refR, ls, itsBunch_m.getT() + itsBunch_m.dt[0] / 2., externalE, externalB);

    itsBunch_m.Ef[0] += externalE;
    itsBunch_m.Bf[0] += externalB;

    IpplTimings::stopTimer(fieldEvaluationTimer_m);
}

kraus's avatar
kraus committed
402
double AutophaseTracker::APtrack(const std::shared_ptr<Component> &cavity, double cavityStart, const double &phi) const {
kraus's avatar
kraus committed
403 404
    const Vector_t &refR = itsBunch_m.R[0];
    const Vector_t &refP = itsBunch_m.P[0];
kraus's avatar
kraus committed
405

kraus's avatar
kraus committed
406 407 408
    double beta = refP(2) / std::sqrt(dot(refP, refP) + 1.0);
    beta = std::max(beta, 0.0001);
    double tErr  = (cavityStart - refR(2)) / (Physics::c * beta);
kraus's avatar
kraus committed
409

kraus's avatar
kraus committed
410
    double initialPhase = 0.0;
kraus's avatar
kraus committed
411
    double finalMomentum = 0.0;
412
    if(cavity->getType() == ElementBase::TRAVELINGWAVE) {
kraus's avatar
kraus committed
413 414
        TravelingWave *tws = static_cast<TravelingWave *>(cavity.get());
        initialPhase = tws->getPhasem();
kraus's avatar
kraus committed
415
        tws->updatePhasem(phi);
kraus's avatar
kraus committed
416
        std::pair<double, double> pe = tws->trackOnAxisParticle(refP(2),
kraus's avatar
kraus committed
417 418 419 420 421
                                                                itsBunch_m.getT() + tErr,
                                                                itsBunch_m.dt[0],
                                                                itsBunch_m.getQ(),
                                                                itsBunch_m.getM() * 1e-6);
        finalMomentum = pe.first;
kraus's avatar
kraus committed
422
        tws->updatePhasem(initialPhase);
kraus's avatar
kraus committed
423
    } else {
kraus's avatar
kraus committed
424 425
        RFCavity *rfc = static_cast<RFCavity *>(cavity.get());
        initialPhase = rfc->getPhasem();
kraus's avatar
kraus committed
426 427
        rfc->updatePhasem(phi);

kraus's avatar
kraus committed
428
        std::pair<double, double> pe = rfc->trackOnAxisParticle(refP(2),
kraus's avatar
kraus committed
429 430 431 432 433
                                                                itsBunch_m.getT() + tErr,
                                                                itsBunch_m.dt[0],
                                                                itsBunch_m.getQ(),
                                                                itsBunch_m.getM() * 1e-6);
        finalMomentum = pe.first;
kraus's avatar
kraus committed
434
        rfc->updatePhasem(initialPhase);
kraus's avatar
kraus committed
435 436 437 438 439 440 441
    }
    double finalGamma = sqrt(1.0 + finalMomentum * finalMomentum);
    double finalKineticEnergy = (finalGamma - 1.0) * itsBunch_m.getM() * 1e-6;
    return finalKineticEnergy;
}


442 443 444 445 446 447
void AutophaseTracker::sendCavityPhases() {
    typedef std::vector<MaxPhasesT>::iterator iterator_t;

    int tag = 101;
    Message *mess = new Message();
    putMessage(*mess, OpalData::getInstance()->getNumberOfMaxPhases());
kraus's avatar
kraus committed
448

449 450 451 452 453 454 455
    iterator_t it = OpalData::getInstance()->getFirstMaxPhases();
    iterator_t end = OpalData::getInstance()->getLastMaxPhases();
    for(; it < end; ++ it) {
        putMessage(*mess, (*it).first);
        putMessage(*mess, (*it).second);
    }
    Ippl::Comm->broadcast_all(mess, tag);
kraus's avatar
kraus committed
456

457
    delete mess;
kraus's avatar
kraus committed
458 459
}

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
void AutophaseTracker::receiveCavityPhases() {
    typedef std::vector<MaxPhasesT>::iterator iterator_t;

    int parent = 0;
    int tag = 101;
    int nData = 0;
    Message *mess = Ippl::Comm->receive_block(parent, tag);
    getMessage(*mess, nData);
    for(int i = 0; i < nData; i++) {
        std::string elName;
        double maxPhi;
        getMessage(*mess, elName);
        getMessage(*mess, maxPhi);
        OpalData::getInstance()->setMaxPhase(elName, maxPhi);
    }

    delete mess;
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 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 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
}

void AutophaseTracker::printCavityPhases() {
    std::shared_ptr<Component> cavity = NULL;
    const double RADDEG = 180.0 / Physics::pi;
    const double globalTimeShift = OpalData::getInstance()->getGlobalPhaseShift();

    *gmsg << level1 << "\n-------------------------------------------------------------------------------------\n";
    while (true) {
        std::shared_ptr<Component> next = getNextCavity(cavity);
        if (next == NULL) break;

        std::string name = next->getName();
        double frequency;
        double phase;

        if (next->getType() == ElementBase::TRAVELINGWAVE) {
            phase = static_cast<TravelingWave *>(next.get())->getPhasem();
	    frequency = static_cast<TravelingWave *>(next.get())->getFrequencym();
        } else {
            phase = static_cast<RFCavity *>(next.get())->getPhasem();
	    frequency = static_cast<RFCavity *>(next.get())->getFrequencym();
        }

        phase -= globalTimeShift * frequency;

        *gmsg << (cavity == NULL? "": "\n")
            << name
            << ": phi = phi_nom + phi_maxE + global phase shift = " << phase * RADDEG << " degree, "
            << "(global phase shift = " << -globalTimeShift *frequency *RADDEG << " degree) \n";

        cavity = next;
    }
    *gmsg << "-------------------------------------------------------------------------------------\n"
          << endl;
}

void AutophaseTracker::save(const std::string &fname) {
    std::ofstream out(fname);

    std::shared_ptr<Component> cavity = NULL;
    const double globalTimeShift = OpalData::getInstance()->getGlobalPhaseShift();

    while (true) {
        std::shared_ptr<Component> next = getNextCavity(cavity);
        if (next == NULL) break;

        std::string name = next->getName();
        double frequency;
        double phase;

        if (next->getType() == ElementBase::TRAVELINGWAVE) {
            phase = static_cast<TravelingWave *>(next.get())->getPhasem();
	    frequency = static_cast<TravelingWave *>(next.get())->getFrequencym();
        } else {
            phase = static_cast<RFCavity *>(next.get())->getPhasem();
	    frequency = static_cast<RFCavity *>(next.get())->getFrequencym();
        }

        phase -= globalTimeShift * frequency;

        out << name << "_lag = " << phase << ";\n";

        cavity = next;
    }

    out.close();
kraus's avatar
kraus committed
544
}