TrackRun.cpp 29.7 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: TrackRun.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.1.1.4.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: TrackRun
//   The class for the OPAL RUN command.
//
// ------------------------------------------------------------------------
//
// $Date: 2004/11/12 20:10:11 $
// $Author: adelmann $
//
// ------------------------------------------------------------------------

#include "Track/TrackRun.h"
#include "AbstractObjects/BeamSequence.h"
#include "AbstractObjects/OpalData.h"
#include "AbstractObjects/ObjectFunction.h"
23
#include "Algorithms/Tracker.h"
gsell's avatar
gsell committed
24 25 26
#include "Algorithms/ThinTracker.h"
#include "Algorithms/ThickTracker.h"

27 28
#include "Algorithms/bet/EnvelopeBunch.h"

gsell's avatar
gsell committed
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 122 123 124 125 126
#include "Algorithms/ParallelTTracker.h"
#include "Algorithms/ParallelSliceTracker.h"
#include "Algorithms/ParallelCyclotronTracker.h"

#include "Attributes/Attributes.h"
#include "Beamlines/TBeamline.h"

#include "BasicActions/Option.h"

#include "Elements/OpalBeamBeam3D.h"
#include "Track/Track.h"
#include "Utilities/OpalException.h"
#include "Utilities/Round.h"
#include "Structure/Beam.h"
#include "Structure/FieldSolver.h"
#include "Structure/DataSink.h"
#include "Distribution/Distribution.h"

#include <fstream>
#include <iomanip>

extern Inform *gmsg;

using namespace Options;
using namespace Physics;

// ------------------------------------------------------------------------

namespace {

    // The attributes of class TrackRun.
    enum {
        METHOD,       // Tracking method to use.
        FNAME,        // The name of file to be written.
        TURNS,        // The number of turns to be tracked.
        MBMODE,       // The working way for multi-bunch mode for OPAL-cycl: "FORCE" or "AUTO"
        PARAMB,       // The control parameter for "AUTO" mode of multi-bunch
        BEAM,         // The beam to track
        FIELDSOLVER,  // The field solver attached
        DISTRIBUTION, // The particle distribution
        DISTRIBUTIONS, // A list of  particle distributions
        MULTIPACTING, // MULTIPACTING flag
        // THE INTEGRATION TIMESTEP IN SEC
        SIZE
    };
}

TrackRun::TrackRun():
    Action(SIZE, "RUN",
           "The \"RUN\" sub-command tracks the defined particles through "
           "the given lattice.") {
    itsAttr[METHOD] = Attributes::makeString
                      ("METHOD", "Name of tracking algorithm to use:\n"
                       "\t\t\t\"THIN\" (default) or \"THICK,PARALLEL-T,PARALLEL-TA,PARALLEL-Z,PARALLEL-SLICE\".", "THIN");
    itsAttr[TURNS] = Attributes::makeReal
                     ("TURNS", "Number of turns to be tracked; Number of neighboring bunches to be tracked in cyclotron", 1.0);

    itsAttr[MBMODE] = Attributes::makeString
                      ("MBMODE", "The working way for multi-bunch mode for OPAL-cycl: FORCE or AUTO ", "FORCE");

    itsAttr[PARAMB] = Attributes::makeReal
                      ("PARAMB", " Control parameter to define when to start multi-bunch mode, only available in \"AUTO\" mode ", 5.0);

    itsAttr[FNAME] = Attributes::makeString
                     ("FILE", "Name of file to be written", "TRACK");

    itsAttr[BEAM] = Attributes::makeString
                    ("BEAM", "Name of beam ", "BEAM");
    itsAttr[FIELDSOLVER] = Attributes::makeString
                           ("FIELDSOLVER", "Field solver to be used ", "FIELDSOLVER");
    itsAttr[DISTRIBUTION] = Attributes::makeString
                            ("DISTRIBUTION", "Particle distribution to be used ", "DISTRIBUTION");
    itsAttr[DISTRIBUTIONS] = Attributes::makeStringArray
                             ("DISTRIBUTIONS", "List of particle distributions to be used ");
    itsAttr[MULTIPACTING] = Attributes::makeBool
                            ("MULTIPACTING", "Multipacting flag, default: false. Set true to initialize primary particles according to BoundaryGeometry", false);
    OPAL = OpalData::getInstance();
}


TrackRun::TrackRun(const string &name, TrackRun *parent):
    Action(name, parent) {
    OPAL = OpalData::getInstance();
}


TrackRun::~TrackRun()
{}


TrackRun *TrackRun::clone(const string &name) {
    return new TrackRun(name, this);
}


void TrackRun::execute() {

    // Get algorithm to use.
127
    std::string method = Attributes::getString(itsAttr[METHOD]);
gsell's avatar
gsell committed
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
    bool mpacflg = Attributes::getBool(itsAttr[MULTIPACTING]);
    if(method == "THIN") {
        //std::cerr << "  method == \"THIN\"" << std::endl;
        itsTracker = new ThinTracker(*Track::block->use->fetchLine(),
                                     *Track::block->bunch, Track::block->reference,
                                     false, false);
    } else if(method == "THICK") {
        //std::cerr << "  method == \"THICK\"" << std::endl;
        itsTracker = new ThickTracker(*Track::block->use->fetchLine(),
                                      *Track::block->bunch, Track::block->reference,
                                      false, false);
    } else if(method == "PARALLEL-SLICE") {

        if(!OPAL->hasSLBunchAllocated()) {
            *gmsg << "* ********************************************************************************** " << endl;
            *gmsg << "  Selected Tracking Method == PARALLEL-SLICE, NEW TRACK" << endl;
            *gmsg << "* ********************************************************************************** " << endl;
        } else if(OPAL->hasSLBunchAllocated() && !Options::scan) {
            *gmsg << "* ********************************************************************************** " << endl;
            *gmsg << "  Selected Tracking Method == PARALLEL-SLICE, FOLLOWUP TRACK" << endl;
            *gmsg << "* ********************************************************************************** " << endl;
        } else if(OPAL->hasSLBunchAllocated() && Options::scan) {
            *gmsg << "* ********************************************************************************** " << endl;
            *gmsg << "  Selected Tracking Method == PARALLEL-SLICE, SCAN TRACK" << endl;
            *gmsg << "* ********************************************************************************** " << endl;
        }

        Beam   *beam = Beam::find(Attributes::getString(itsAttr[BEAM]));
        dist = Distribution::find(Attributes::getString(itsAttr[DISTRIBUTION]));


        fs = FieldSolver::find(Attributes::getString(itsAttr[FIELDSOLVER]));
        fs->initCartesianFields();

        double charge = 0.0;

        std::vector<std::string> distr_str = Attributes::getStringArray(itsAttr[DISTRIBUTIONS]);
        if(distr_str.size() > 0) {
            *gmsg << "Found more than one distribution: ";
            for(std::vector<std::string>::const_iterator dit = distr_str.begin(); dit != distr_str.end(); ++ dit) {
                Distribution *d = Distribution::find(*dit);
                *gmsg << " " << *dit;
                distrs_m.push_back(d);
            }
            *gmsg << endl;
        }

        if(!OPAL->hasSLBunchAllocated()) {
            if(!OPAL->inRestartRun()) {

178 179
                dist->CreateOpalE(beam, distrs_m, Track::block->slbunch, 0.0, 0.0);
                OPAL->setGlobalPhaseShift(0.5 * dist->GetTEmission());
gsell's avatar
gsell committed
180 181 182 183 184 185

            } else {
                /***
                    reload slice distribution
                */

186
                dist->DoRestartOpalE(*Track::block->slbunch, beam->getNumberOfParticles(), OPAL->getRestartStep());
gsell's avatar
gsell committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
            }
        } else {
            charge = 1.0;
        }

        Track::block->slbunch->setdT(Track::block->dT);
        // set the total charge
        charge = beam->getCharge() * beam->getCurrent() / beam->getFrequency();
        Track::block->slbunch->setCharge(charge);
        // set coupling constant
        double coefE = 1.0 / (4 * pi * epsilon_0);
        Track::block->slbunch->setCouplingConstant(coefE);
        //Track::block->slbunch->calcBeamParameters();


        if(!OPAL->inRestartRun()) {
            if(!OPAL->hasDataSinkAllocated())
                OPAL->setDataSink(new DataSink());
        } else
            OPAL->setDataSink(new DataSink(OPAL->getRestartStep() + 1));

        ds = OPAL->getDataSink();

        if(!OPAL->hasBunchAllocated())
            *gmsg << *dist << endl;
        *gmsg << *beam << endl;
        *gmsg << *Track::block->slbunch  << endl;
        *gmsg << "Phase space dump frequency is set to " << Options::psDumpFreq
              << " Inputfile is " << OPAL->getInputFn() << endl;


        ParallelTTracker *mySlApTracker = setupForAutophase();
        itsTracker = new ParallelSliceTracker(*Track::block->use->fetchLine(),
                                              dynamic_cast<EnvelopeBunch &>(*Track::block->slbunch),
                                              *ds,
                                              Track::block->reference,
                                              false, false,
224
                                              Track::block->localTimeSteps,
gsell's avatar
gsell committed
225 226 227 228
                                              Track::block->zstop,
                                              *mySlApTracker);
    } else if(method == "PARALLEL-T") {

adelmann's avatar
adelmann committed
229 230
        bool isFollowupTrack = (OPAL->hasBunchAllocated() && !Options::scan);

gsell's avatar
gsell committed
231 232 233 234
        if(!OPAL->hasBunchAllocated() && !Options::scan) {
            *gmsg << "* ********************************************************************************** " << endl;
            *gmsg << "  Selected Tracking Method == PARALLEL-T, NEW TRACK" << endl;
            *gmsg << "* ********************************************************************************** " << endl;
adelmann's avatar
adelmann committed
235
        } else if(isFollowupTrack) {
gsell's avatar
gsell committed
236 237 238
            *gmsg << "* ********************************************************************************** " << endl;
            *gmsg << "  Selected Tracking Method == PARALLEL-T, FOLLOWUP TRACK" << endl;
            *gmsg << "* ********************************************************************************** " << endl;
239
            Track::block->bunch->setLocalTrackStep(0);
gsell's avatar
gsell committed
240 241
        } else if(OPAL->hasBunchAllocated() && Options::scan) {
            *gmsg << "* ********************************************************************************** " << endl;
242
            *gmsg << "  Selected Tracking Method == PARALLEL-T, FOLLOWUP TRACK in SCAN MODE" << endl;
gsell's avatar
gsell committed
243
            *gmsg << "* ********************************************************************************** " << endl;
244 245
            Track::block->bunch->setLocalTrackStep(0);
            Track::block->bunch->setGlobalTrackStep(0);
gsell's avatar
gsell committed
246 247 248 249 250 251 252 253 254 255 256 257 258
        } else if(!OPAL->hasBunchAllocated() && Options::scan) {
            *gmsg << "* ********************************************************************************** " << endl;
            *gmsg << "  Selected Tracking Method == PARALLEL-T, NEW TRACK in SCAN MODE" << endl;
            *gmsg << "* ********************************************************************************** " << endl;
        } else
            *gmsg << "  Selected Tracking Method is NOT implemented, good luck ..." << endl;

        Beam *beam = Beam::find(Attributes::getString(itsAttr[BEAM]));
        fs = FieldSolver::find(Attributes::getString(itsAttr[FIELDSOLVER]));
        fs->initCartesianFields();
        Track::block->bunch->setSolver(fs);
        Track::block->bunch->setBCAllOpen();

259
        double charge = SetDistributionParallelT(beam);
gsell's avatar
gsell committed
260 261

        Track::block->bunch->setdT(Track::block->dT);
262 263
        Track::block->bunch->dtScInit_m = Track::block->dtScInit;
        Track::block->bunch->deltaTau_m = Track::block->deltaTau;
adelmann's avatar
adelmann committed
264

265
        if (!isFollowupTrack && !OPAL->inRestartRun())
adelmann's avatar
adelmann committed
266 267
            Track::block->bunch->setT(Track::block->t0_m);

gsell's avatar
gsell committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 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
        if(!mpacflg) {
            Track::block->bunch->setCharge(charge);
        } else {
            Track::block->bunch->setChargeZeroPart(charge);// just set bunch->qi_m=charge, don't set bunch->Q[] as we have not initialized any particle yet.
        }
        if(!mpacflg) {
            // set coupling constant
            double coefE = 1.0 / (4 * pi * epsilon_0);
            Track::block->bunch->setCouplingConstant(coefE);


            // statistical data are calculated (rms, eps etc.)
            Track::block->bunch->calcBeamParameters();
        } else {
            Track::block->bunch->calcBeamParametersInitial();// we have not initialized any particle yet.
        }

        if(!OPAL->inRestartRun()) {
            if(!OPAL->hasDataSinkAllocated() && !Options::scan) {
                OPAL->setDataSink(new DataSink());
            } else if(Options::scan) {
                ds = OPAL->getDataSink();
                if(ds)
                    delete ds;
                OPAL->setDataSink(new DataSink());
            }
        } else {
            OPAL->setDataSink(new DataSink(OPAL->getRestartStep() + 1));
        }

        ds = OPAL->getDataSink();

        if(OPAL->hasBunchAllocated() && Options::scan)
            ds->reset();

        if(!OPAL->hasBunchAllocated() || Options::scan) {
            if(!mpacflg) {
                *gmsg << *dist << endl;
            } else {
                *gmsg << "* Multipacting flag is true. The particle distribution in the run command will be ignored " << endl;
            }
        }

        *gmsg << *beam << endl;
        *gmsg << *fs   << endl;
        *gmsg << *Track::block->bunch  << endl;

        *gmsg << "Phase space dump frequency " << Options::psDumpFreq << " and "
              << "statistics dump frequency " << Options::statDumpFreq << " w.r.t. the time step." << endl;

        itsTracker = new ParallelTTracker(*Track::block->use->fetchLine(),
                                          dynamic_cast<PartBunch &>(*Track::block->bunch), *ds,
320
                                          Track::block->reference, false, false, Track::block->localTimeSteps,
321
                                          Track::block->zstop, Track::block->timeIntegrator);
gsell's avatar
gsell committed
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
        itsTracker->setMpacflg(mpacflg); // set multipacting flag in ParallelTTracker

    } else if(method == "PARALLEL-Z") {
        *gmsg << "  method == \"PARALLEL-Z\"" << endl;

    } else if(method == "CYCLOTRON-T") {

        Beam *beam = Beam::find(Attributes::getString(itsAttr[BEAM]));

        fs = FieldSolver::find(Attributes::getString(itsAttr[FIELDSOLVER]));
        fs->initCartesianFields();
        Track::block->bunch->setSolver(fs);
        Track::block->bunch->setBCAllOpen();
        Track::block->bunch->PType = 0;

        dist = Distribution::find(Attributes::getString(itsAttr[DISTRIBUTION]));

        // set macromass and charge for simulation particles
        double macromass = 0.0;
        double macrocharge = 0.0;

        const int specifiedNumBunch = int(std::abs(Round(Attributes::getReal(itsAttr[TURNS]))));

        if(beam->getNumberOfParticles() < 3 || beam->getCurrent() == 0.0) {

            macrocharge = beam->getCharge() * q_e;
            macromass = beam->getMass();
349
            dist->CreateOpalCycl(*Track::block->bunch, beam->getNumberOfParticles(), Options::scan);
gsell's avatar
gsell committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363

        } else {

            /**
                   getFrequency() gets RF frequency [MHz], NOT isochronous  revolution frequency of particle!
                   getCurrent() gets beamcurrent [A]

                */
            macrocharge = beam->getCurrent() / (beam->getFrequency() * 1.0e6); // [MHz]-->[Hz]

            if(!OPAL->hasBunchAllocated()) {
                if(!OPAL->inRestartRun()) {
                    macrocharge /= beam->getNumberOfParticles();
                    macromass = beam->getMass() * macrocharge / (beam->getCharge() * q_e);
364
                    dist->CreateOpalCycl(*Track::block->bunch, beam->getNumberOfParticles(), Options::scan);
gsell's avatar
gsell committed
365 366

                } else {
367
                    dist->DoRestartOpalCycl(*Track::block->bunch, beam->getNumberOfParticles(),
gsell's avatar
gsell committed
368 369 370 371 372 373 374
                                         OPAL->getRestartStep(), specifiedNumBunch);
                    macrocharge /= beam->getNumberOfParticles();
                    macromass = beam->getMass() * macrocharge / (beam->getCharge() * q_e);
                }
            } else if(OPAL->hasBunchAllocated() && Options::scan) {
                macrocharge /= beam->getNumberOfParticles();
                macromass = beam->getMass() * macrocharge / (beam->getCharge() * q_e);
375
                dist->CreateOpalCycl(*Track::block->bunch, beam->getNumberOfParticles(), Options::scan);
gsell's avatar
gsell committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
            }
        }
        Track::block->bunch->setMass(macromass); // set the Mass per macro-particle, [GeV/c^2]
        Track::block->bunch->setCharge(macrocharge);  // set the charge per macro-particle, [C]

        *gmsg << "Mass of simulation particle= " << macromass << "GeV/c^2" << endl;
        *gmsg << "Charge of simulation particle= " << macrocharge << "[C]" << endl;


        Track::block->bunch->setdT(1.0 / (Track::block->stepsPerTurn * beam->getFrequency() * 1.0e6));
        Track::block->bunch->setStepsPerTurn(Track::block->stepsPerTurn);

        // set coupling constant
        double coefE = 1.0 / (4 * pi * epsilon_0);
        Track::block->bunch->setCouplingConstant(coefE);

        // statistical data are calculated (rms, eps etc.)
        Track::block->bunch->calcBeamParameters_cycl();

        if(!OPAL->inRestartRun())
            if(!OPAL->hasDataSinkAllocated()) {
                ds = new DataSink();
                OPAL->setDataSink(ds);
            } else
                ds = OPAL->getDataSink();
        else {
            ds = new DataSink(OPAL->getRestartStep() + 1);
            OPAL->setDataSink(ds);
        }
405

gsell's avatar
gsell committed
406 407 408
        if(OPAL->hasBunchAllocated() && Options::scan)
            ds->reset();

409
        //        ds->setOPALcycl();
adelmann's avatar
adelmann committed
410

gsell's avatar
gsell committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
        *gmsg << *dist << endl;
        *gmsg << *beam << endl;
        *gmsg << *fs   << endl;
        *gmsg << *Track::block->bunch  << endl;

        if(!OPAL->hasBunchAllocated() && !Options::scan) {
            *gmsg << "* ********************************************************************************** " << endl;
            *gmsg << "  Selected Tracking Method == CYCLOTRON-T, NEW TRACK" << endl;

            *gmsg << "* ********************************************************************************** " << endl;
        } else if(OPAL->hasBunchAllocated() && !Options::scan) {
            *gmsg << "* ********************************************************************************** " << endl;
            *gmsg << "  Selected Tracking Method == CYCLOTRON-T, FOLLOWUP TRACK" << endl;
            *gmsg << "* ********************************************************************************** " << endl;
        } else if(OPAL->hasBunchAllocated() && Options::scan) {
            *gmsg << "* ********************************************************************************** " << endl;
            *gmsg << "  Selected Tracking Method == CYCLOTRON-T, SCAN TRACK" << endl;
            *gmsg << "* ********************************************************************************** " << endl;
        }
        *gmsg << "* Number of neighbour bunches= " << specifiedNumBunch << endl;
        *gmsg << "* DT                         = " << Track::block->dT << endl;
432
        *gmsg << "* MAXSTEPS                   = " << Track::block->localTimeSteps << endl;
gsell's avatar
gsell committed
433 434 435 436 437 438
        *gmsg << "* Phase space dump frequency = " << Options::psDumpFreq << endl;
        *gmsg << "* Statistics dump frequency  = " << Options::statDumpFreq << " w.r.t. the time step." << endl;
        *gmsg << "* ********************************************************************************** " << endl;

        itsTracker = new ParallelCyclotronTracker(*Track::block->use->fetchLine(),
                dynamic_cast<PartBunch &>(*Track::block->bunch), *ds, Track::block->reference,
439
                false, false, Track::block->localTimeSteps, Track::block->timeIntegrator);
gsell's avatar
gsell committed
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 480 481 482 483 484 485 486 487 488 489 490

        itsTracker->setNumBunch(specifiedNumBunch);

        if(specifiedNumBunch > 1) {

            // only for regular  run of multi bunches, instantiate the  PartBins class
            // note that for restart run of multi bunches, PartBins class is instantiated in function doRestart_cycl()
            if(!OPAL->inRestartRun()) {


                // already exist bins number initially
                const int BinCount = 1;
                //allowed maximal bin
                const int MaxBinNum = 1000;

                // initialize particles number for each bin (both existed and not yet emmitted)
                size_t partInBin[MaxBinNum];
                for(int ii = 0; ii < MaxBinNum; ii++) partInBin[ii] = 0;
                partInBin[0] =  beam->getNumberOfParticles();

                Track::block->bunch->setPBins(new PartBinsCyc(MaxBinNum, BinCount, partInBin));
                // the allowed maximal bin number is set to 100
                //Track::block->bunch->setPBins(new PartBins(100));

            }

            // mode of generating new bunches:
            // "FORCE" means generating one bunch after each revolution, until get "TURNS" bunches.
            // "AUTO" means only when the distance between two neighbor bunches is bellow the limitation,
            //        then starts to generate new bunches after each revolution,until get "TURNS" bunches;
            //        otherwise, run single bunch track

            *gmsg << "***---------------------------- MULTI-BUNCHES MULTI-ENERGY-BINS MODE------ ----------------------------*** " << endl;

            double paraMb = Attributes::getReal(itsAttr[PARAMB]);
            itsTracker->setParaAutoMode(paraMb);

            if(OPAL->inRestartRun()) {

                itsTracker->setLastDumpedStep(OPAL->getRestartStep());

                if(Track::block->bunch->pbin_m->getLastemittedBin() < 2) {
                    itsTracker->setMultiBunchMode(2);
                    *gmsg << "In this restart job, the multi-bunches mode is forcely set to AUTO mode." << endl;
                } else {
                    itsTracker->setMultiBunchMode(1);
                    *gmsg << "In this restart job, the multi-bunches mode is forcely set to FORCE mode." << endl
                          << "If the existing bunch number is less than the specified number of TURN, readin the phase space of STEP#0 from h5 file consecutively" << endl;
                }
            } else {
                //////
491
                if((Attributes::getString(itsAttr[MBMODE])) == std::string("FORCE")) {
gsell's avatar
gsell committed
492 493 494 495 496 497
                    itsTracker->setMultiBunchMode(1);
                    *gmsg << "FORCE mode: The multi bunches will be injected consecutively after each revolution, until get \"TURNS\" bunches." << endl;


                }
                //////
498
                else if((Attributes::getString(itsAttr[MBMODE])) == std::string("AUTO")) {
gsell's avatar
gsell committed
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 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562


                    itsTracker->setMultiBunchMode(2);

                    *gmsg << "AUTO mode: The multi bunches will be injected only when the distance between two neighborring bunches " << endl
                          << "is bellow the limitation. The control parameter is set to " << paraMb << endl;
                }
                //////
                else
                    throw OpalException("TrackRun::execute()",
                                        "MBMODE name \"" + Attributes::getString(itsAttr[MBMODE]) + "\" unknown.");
            }

        }

    } else {
        throw OpalException("TrackRun::execute()",
                            "Method name \"" + method + "\" unknown.");
    }

    if((method != "PARALLEL-Z") && (method != "PARALLEL-T") && (method != "CYCLOTRON-T") && (method != "PARALLEL-SLICE")) {
        /*
          OLD SERIAL STUFF
        */
        // Open output file.
        string file = Attributes::getString(itsAttr[FNAME]);
        std::ofstream os(file.c_str());
        if(os.bad()) {
            throw OpalException("TrackRun::execute()",
                                "Unable to open output file \"" + file + "\".");
        }

        // Print initial conditions.
        os << "\nInitial particle positions:\n"
           << itsTracker->getBunch() << std::endl;

        int turns = int(Round(Attributes::getReal(itsAttr[TURNS])));
        // Track for the all but last turn.
        for(int turn = 1; turn < turns; ++turn) {
            itsTracker->execute();
            os << "\nParticle positions after turn " << turn << ":\n"
               << itsTracker->getBunch() << std::endl;
        }
        // Track last turn, with statistics.
        itsTracker->execute();

        // Print final conditions.
        os << "Particle positions after turn " << turns << ":\n"
           << itsTracker->getBunch() << std::endl;
        //    Track::block->bunch = itsTracker->getBunch();
    } else {
        itsTracker->execute();
        OPAL->setRestartRun(false);
    }
    //  delete [] fs;
    //  delete [] dist;

    OPAL->bunchIsAllocated();
    if(method == "PARALLEL-SLICE")
        OPAL->slbunchIsAllocated();

    delete itsTracker;

}
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 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 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 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

double TrackRun::SetDistributionParallelT(Beam *beam) {

    // If multipacting flag is not set, get distribution(s).
    if (!Attributes::getBool(itsAttr[MULTIPACTING])) {
        /*
         * Distribution(s) can be set via a single distribution or a list
         * (array) of distributions. If an array is defined the first in the
         * list is treated as the primary distribution. All others are added to
         * it to create the full distribution.
         */
        std::vector<std::string> distributionArray
            = Attributes::getStringArray(itsAttr[DISTRIBUTIONS]);

        if (distributionArray.size() > 0) {
            *gmsg << endl
                  << "---------------------------------" << endl
                  << "Found more than one distribution:" << endl << endl;

            for (std::vector<std::string>::const_iterator distIterator
                 = distributionArray.begin();
                 distIterator != distributionArray.end(); ++distIterator) {

                if (distIterator == distributionArray.begin()) {
                    dist = Distribution::find(*distIterator);
                    *gmsg << "Main Distribution" << endl
                          << "-----------------" << endl
                          << *distIterator << endl << endl
                          << "Secondary Distribution(s)" << endl
                          << "-------------------------" << endl;
                } else {
                    Distribution *distribution = Distribution::find(*distIterator);
                    *gmsg << *distIterator << endl;
                    distrs_m.push_back(distribution);
                }
            }
            *gmsg << endl
                  << "---------------------------------" << endl << endl;
        } else
            dist = Distribution::find(Attributes::getString(itsAttr[DISTRIBUTION]));
    }


    /*
     * Initialize distributions.
     */
    size_t numberOfParticles = beam->getNumberOfParticles();
    if (!OPAL->hasBunchAllocated()) {
        if (!OPAL->inRestartRun()) {
            if (!Attributes::getBool(itsAttr[MULTIPACTING])) {
                /*
                 * Here we are not doing a restart or doing a mulitpactor run
                 * and we do not have a bunch already allocated.
                 */
                Track::block->bunch->setDistribution(dist,
                                                     distrs_m,
                                                     numberOfParticles,
                                                     Options::scan);

                /*
                 * If this is an injected beam (rather than an emitted beam), we
                 * make sure it doesn't have any particles at z < 0.
                 */
                Vector_t rMin;
                Vector_t rMax;
                Track::block->bunch->get_bounds(rMin, rMax);

                OPAL->setGlobalPhaseShift(0.5 * dist->GetTEmission());
            }
        } else {
            /*
             * Read in beam from restart file.
             */
            dist->DoRestartOpalT(*Track::block->bunch, numberOfParticles, OPAL->getRestartStep());
        }
    } else if (OPAL->hasBunchAllocated() && Options::scan) {
        /*
         * We are in scan mode and have already allocated a bunch. So, we need to
         * do a couple more things.
         */
        Track::block->bunch->setDistribution(dist,
                                             distrs_m,
                                             numberOfParticles,
                                             Options::scan);
        Track::block->bunch->resetIfScan();
        Track::block->bunch->LastSection = 1;

        OPAL->setGlobalPhaseShift(0.5 * dist->GetTEmission());
    }

    // Return charge per macroparticle.
    return beam->getCharge() * beam->getCurrent() / beam->getFrequency() / numberOfParticles;

}

ParallelTTracker *TrackRun::setupForAutophase() {

    Inform m("setupForAutophase() ");

    Beam *beam = Beam::find(Attributes::getString(itsAttr[BEAM]));

    DataSink *ds = NULL;


    fs = FieldSolver::find(Attributes::getString(itsAttr[FIELDSOLVER]));

    fs->initCartesianFields();

    Track::block->bunch->setSolver(fs);

    dist = Distribution::find(Attributes::getString(itsAttr[DISTRIBUTION]));

    double charge = beam->getCharge() * beam->getCurrent() / beam->getFrequency();

    charge /= beam->getNumberOfParticles();

    Track::block->bunch->setdT(Track::block->dT);

    Track::block->bunch->resetIfScan();

    Track::block->bunch->LastSection = 1;

    Track::block->bunch->setCharge(charge);

    Track::block->bunch->setChargeZeroPart(charge);// just set bunch->qi_m=charge, don't set bunch->Q[] as we have not initialized any particle yet.

    double coefE = 1.0 / (4 * pi * epsilon_0);
    Track::block->bunch->setCouplingConstant(coefE);

    return new ParallelTTracker(*Track::block->use->fetchLine(),
                                dynamic_cast<PartBunch &>(*Track::block->bunch), *ds,
                                Track::block->reference, false, false, Track::block->localTimeSteps,
                                Track::block->zstop, Track::block->timeIntegrator);
}