ParallelTTracker.cpp 108 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
// ------------------------------------------------------------------------
// $RCSfile: ParallelTTracker.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.2.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: ParallelTTracker
//   The visitor class for tracking particles with time as independent
//   variable.
//
// ------------------------------------------------------------------------
//
// $Date: 2004/11/12 20:10:11 $
// $Author: adelmann $
//
// ------------------------------------------------------------------------

kraus's avatar
kraus committed
20 21
#include "Algorithms/ParallelTTracker.h"

gsell's avatar
gsell committed
22 23 24 25 26 27
#include <cfloat>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
28
#include <limits>
adelmann's avatar
adelmann committed
29
#include <cmath>
gsell's avatar
gsell committed
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
#include "Algorithms/PartPusher.h"
#include "AbsBeamline/AlignWrapper.h"
#include "AbsBeamline/BeamBeam.h"
#include "AbsBeamline/Collimator.h"
#include "AbsBeamline/Corrector.h"
#include "AbsBeamline/Diagnostic.h"
#include "AbsBeamline/Drift.h"
#include "AbsBeamline/ElementBase.h"
#include "AbsBeamline/Lambertson.h"
#include "AbsBeamline/Marker.h"
#include "AbsBeamline/Monitor.h"
#include "AbsBeamline/Multipole.h"
#include "AbsBeamline/Probe.h"
#include "AbsBeamline/RBend.h"
#include "AbsBeamline/RFCavity.h"
#include "AbsBeamline/TravelingWave.h"
#include "AbsBeamline/RFQuadrupole.h"
#include "AbsBeamline/SBend.h"
#include "AbsBeamline/Separator.h"
#include "AbsBeamline/Septum.h"
#include "AbsBeamline/Solenoid.h"
#include "AbsBeamline/ParallelPlate.h"
#include "AbsBeamline/CyclotronValley.h"
#include "Beamlines/Beamline.h"
#include "Lines/Sequence.h"
56 57
//--------- Added by Xiaoying Pang 04/22/2014 ---------------
#include "Solvers/CSRWakeFunction.hh"
gsell's avatar
gsell committed
58 59 60 61

#include "AbstractObjects/OpalData.h"

#include "BasicActions/Option.h"
62
#include "Utilities/Options.h"
kraus's avatar
kraus committed
63
#include "Utilities/Options.h"
gsell's avatar
gsell committed
64 65

#include "Distribution/Distribution.h"
66
#include "ValueDefinitions/RealVariable.h"
gsell's avatar
gsell committed
67 68
#include "Utilities/Timer.h"
#include "Utilities/OpalException.h"
69
#include "Solvers/SurfacePhysicsHandler.hh"
gsell's avatar
gsell committed
70
#include "Structure/BoundaryGeometry.h"
cwang's avatar
cwang committed
71
#define EPS 10e-10
gsell's avatar
gsell committed
72 73 74 75 76 77 78
class PartData;

using namespace std;

ParallelTTracker::ParallelTTracker(const Beamline &beamline,
                                   const PartData &reference,
                                   bool revBeam,
adelmann's avatar
adelmann committed
79 80
                                   bool revTrack,
				   size_t N):
81 82 83 84 85 86 87 88 89 90 91 92
Tracker(beamline, reference, revBeam, revTrack),
itsBunch(NULL),
itsDataSink_m(NULL),
bgf_m(NULL),
itsOpalBeamline_m(),
lineDensity_m(),
RefPartR_zxy_m(0.0),
RefPartP_zxy_m(0.0),
RefPartR_suv_m(0.0),
RefPartP_suv_m(0.0),
globalEOL_m(false),
wakeStatus_m(false),
93 94
//--------- Added by Xiaoying Pang 04/22/2014 ---------------
wakeFunction_m(NULL),
95 96 97 98
surfaceStatus_m(false),
secondaryFlg_m(false),
mpacflg_m(true),
nEmissionMode_m(false),
99
zStop_m(),
100 101 102 103
scaleFactor_m(1.0),
vscaleFactor_m(scaleFactor_m),
recpGamma_m(1.0),
rescale_coeff_m(1.0),
104 105
dtCurrentTrack_m(0.0),
dtAllTracks_m(),
106
surfaceEmissionStop_m(-1),
107
specifiedNPart_m(N),
108 109 110 111 112 113 114
minStepforReBin_m(-1),
minBinEmitted_m(std::numeric_limits<size_t>::max()),
repartFreq_m(-1),
lastVisited_m(-1),
numRefs_m(-1),
gunSubTimeSteps_m(-1),
emissionSteps_m(std::numeric_limits<unsigned int>::max()),
115
localTrackSteps_m(),
116 117 118 119
maxNparts_m(0),
numberOfFieldEmittedParticles_m(std::numeric_limits<size_t>::max()),
bends_m(0),
numParticlesInSimulation_m(0),
120
totalParticlesInSimulation_m(0),
kraus's avatar
kraus committed
121
space_orientation_m(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0),
122 123 124 125 126 127
timeIntegrationTimer1_m(IpplTimings::getTimer("TIntegration1")),
timeIntegrationTimer2_m(IpplTimings::getTimer("TIntegration2")),
timeFieldEvaluation_m(IpplTimings::getTimer("Fieldeval")),
BinRepartTimer_m(IpplTimings::getTimer("Binaryrepart")),
WakeFieldTimer_m(IpplTimings::getTimer("WakeField")),
Nimpact_m(0),
128
SeyNum_m(0.0),
129 130
timeIntegrationTimer1Push_m(IpplTimings::getTimer("TIntegration1Push")),
timeIntegrationTimer2Push_m(IpplTimings::getTimer("TIntegration2Push"))
131
{
gsell's avatar
gsell committed
132 133 134 135 136 137 138 139
}

ParallelTTracker::ParallelTTracker(const Beamline &beamline,
                                   PartBunch &bunch,
                                   DataSink &ds,
                                   const PartData &reference,
                                   bool revBeam,
                                   bool revTrack,
140
                                   const std::vector<unsigned long long> &maxSteps,
141
                                   const std::vector<double> &zstop,
adelmann's avatar
adelmann committed
142
                                   int timeIntegrator,
143
                                   const std::vector<double> &dt,
adelmann's avatar
adelmann committed
144
				   size_t N):
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
Tracker(beamline, reference, revBeam, revTrack),
itsBunch(&bunch),
itsDataSink_m(&ds),
bgf_m(NULL),
itsOpalBeamline_m(),
lineDensity_m(),
RefPartR_zxy_m(0.0),
RefPartP_zxy_m(0.0),
RefPartR_suv_m(0.0),
RefPartP_suv_m(0.0),
globalEOL_m(false),
wakeStatus_m(false),
surfaceStatus_m(false),
secondaryFlg_m(false),
mpacflg_m(true),
nEmissionMode_m(false),
scaleFactor_m(itsBunch->getdT() * Physics::c),
vscaleFactor_m(scaleFactor_m),
recpGamma_m(1.0),
rescale_coeff_m(1.0),
165
dtCurrentTrack_m(0.0),
166
surfaceEmissionStop_m(-1),
167
specifiedNPart_m(N),
168 169 170 171 172 173 174 175 176 177 178
minStepforReBin_m(-1),
minBinEmitted_m(std::numeric_limits<size_t>::max()),
repartFreq_m(-1),
lastVisited_m(-1),
numRefs_m(-1),
gunSubTimeSteps_m(-1),
emissionSteps_m(numeric_limits<unsigned int>::max()),
maxNparts_m(0),
numberOfFieldEmittedParticles_m(numeric_limits<size_t>::max()),
bends_m(0),
numParticlesInSimulation_m(0),
179
totalParticlesInSimulation_m(0),
kraus's avatar
kraus committed
180
space_orientation_m(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0),
181 182 183 184 185 186 187
timeIntegrationTimer1_m(IpplTimings::getTimer("TIntegration1")),
timeIntegrationTimer2_m(IpplTimings::getTimer("TIntegration2")),
timeFieldEvaluation_m(IpplTimings::getTimer("Fieldeval")),
BinRepartTimer_m(IpplTimings::getTimer("Binaryrepart")),
WakeFieldTimer_m(IpplTimings::getTimer("WakeField")),
timeIntegrator_m(timeIntegrator),
Nimpact_m(0),
188
SeyNum_m(0.0),
189 190
timeIntegrationTimer1Push_m(IpplTimings::getTimer("TIntegration1Push")),
timeIntegrationTimer2Push_m(IpplTimings::getTimer("TIntegration2Push"))
191
{
192

193 194 195 196 197 198 199 200 201 202
    for (std::vector<unsigned long long>::const_iterator it = maxSteps.begin(); it != maxSteps.end(); ++ it) {
        localTrackSteps_m.push(*it);
    }
    for (std::vector<double>::const_iterator it = dt.begin(); it != dt.end(); ++ it) {
        dtAllTracks_m.push(*it);
    }
    for (std::vector<double>::const_iterator it = zstop.begin(); it != zstop.end(); ++ it) {
        zStop_m.push(*it);
    }

203
    //    itsBeamline = dynamic_cast<Beamline*>(beamline.clone());
204

205
#ifdef OPAL_DKS
206
    if (IpplInfo::DKSEnabled) {
207 208 209 210
        dksbase.setAPI("Cuda", 4);
        dksbase.setDevice("-gpu", 4);
        dksbase.initDevice();
    }
211 212
#endif

gsell's avatar
gsell committed
213 214 215 216
}


ParallelTTracker::~ParallelTTracker() {
217

gsell's avatar
gsell committed
218 219
}

220 221 222
void ParallelTTracker::applyEntranceFringe(double angle, double curve,
                                           const BMultipoleField &field, double scale) {
}
223 224


225 226 227
void ParallelTTracker::applyExitFringe(double angle, double curve,
                                       const BMultipoleField &field, double scale) {
}
228

229
void ParallelTTracker::updateRFElement(std::string elName, double maxPhase) {
230 231 232 233 234
    /**
     The maximum phase is added to the nominal phase of
     the element. This is done on all nodes except node 0 where
     the Autophase took place.
     */
235 236 237 238 239
    double phase = 0.0;
    double frequency = 0.0;
    double globalTimeShift = OpalData::getInstance()->getGlobalPhaseShift();
    for (FieldList::iterator fit = cavities_m.begin(); fit != cavities_m.end(); ++fit) {
        if ((*fit).getElement()->getName() == elName) {
240
            if ((*fit).getElement()->getType() == ElementBase::TRAVELINGWAVE) {
241 242 243 244 245
                phase  =  static_cast<TravelingWave *>((*fit).getElement().get())->getPhasem();
                frequency = static_cast<TravelingWave *>((*fit).getElement().get())->getFrequencym();
                maxPhase -= frequency * globalTimeShift;

                static_cast<TravelingWave *>((*fit).getElement().get())->updatePhasem(phase + maxPhase);
246
            } else {
247 248 249 250 251
                phase  = static_cast<RFCavity *>((*fit).getElement().get())->getPhasem();
                frequency = static_cast<RFCavity *>((*fit).getElement().get())->getFrequencym();
                maxPhase -= frequency * globalTimeShift;

                static_cast<RFCavity *>((*fit).getElement().get())->updatePhasem(phase + maxPhase);
252
            }
253 254

            break;
255
        }
gsell's avatar
gsell committed
256 257 258
    }
}

259
void ParallelTTracker::handleAutoPhasing() {
260
    typedef std::vector<MaxPhasesT>::iterator iterator_t;
261

262
    if(Options::autoPhase == 0) return;
263

264
    if(!OpalData::getInstance()->inRestartRun()) {
265 266
        itsDataSink_m->storeCavityInformation();
    }
267

268 269 270 271
    iterator_t it = OpalData::getInstance()->getFirstMaxPhases();
    iterator_t end = OpalData::getInstance()->getLastMaxPhases();
    for(; it < end; ++ it) {
        updateRFElement((*it).first, (*it).second);
gsell's avatar
gsell committed
272 273 274
    }
}

275 276 277 278 279
void ParallelTTracker::execute() {
    if(timeIntegrator_m == 3) {
        executeAMTSTracker();
    } else {
        executeDefaultTracker();
gsell's avatar
gsell committed
280 281 282
    }
}

283
void ParallelTTracker::executeDefaultTracker() {
kraus's avatar
kraus committed
284
    Inform msg("ParallelTTracker ", *gmsg);
285 286 287 288
    const Vector_t vscaleFactor_m = Vector_t(scaleFactor_m);
    BorisPusher pusher(itsReference);
    secondaryFlg_m = false;
    dtCurrentTrack_m = itsBunch->getdT();
gsell's avatar
gsell committed
289

290 291 292 293
    // upper limit of particle number when we do field emission and secondary emission
    // simulation. Could be reset to another value in input file with MAXPARTSNUM.
    maxNparts_m = 100000000;
    nEmissionMode_m = true;
294

295
    prepareSections();
296

297 298 299 300 301 302 303
    if (OpalData::getInstance()->hasBunchAllocated()) {
        // delete last entry of sdds file and load balance file
        // if we are in a follow-up track
        itsDataSink_m->rewindLinesSDDS(1);
        itsDataSink_m->rewindLinesLBal(1);
    }

304
    handleAutoPhasing();
305

306
    numParticlesInSimulation_m = itsBunch->getTotalNum();
307
    totalParticlesInSimulation_m = itsBunch->getTotalNum();
308

309
    OPALTimer::Timer myt1;
310

311
    setTime();
312

313
    double t = itsBunch->getT();
314

315
    unsigned long long step = itsBunch->getLocalTrackStep();
316

kraus's avatar
kraus committed
317
    *gmsg << "Track start at: " << myt1.time() << ", t= " << t << "; zstop at: " << zStop_m.front() << " [m]" << endl;
318

319 320
    gunSubTimeSteps_m = 10;
    prepareEmission();
321

322
    doSchottyRenormalization();
323

kraus's avatar
kraus committed
324 325 326 327
    *gmsg << level1
          << "Executing ParallelTTracker, initial DT " << itsBunch->getdT() << " [s];\n"
          << "max integration steps " << localTrackSteps_m.front() << ", next step= " << step << "\n";
    *gmsg << "Using default (Boris-Buneman) integrator" << endl;
328

kraus's avatar
kraus committed
329
    itsOpalBeamline_m.print(*gmsg);
330

331
    setupSUV(!(OpalData::getInstance()->inRestartRun() || (OpalData::getInstance()->hasBunchAllocated() && !Options::scan)));
332

333 334 335
    // increase margin from 3.*c*dt to 10.*c*dt to prevent that fieldmaps are accessed
    // before they are allocated when increasing the timestep in the gun.
    switchElements(10.0);
336

337
    initializeBoundaryGeometry();
338

339
    setOptionalVariables();
gsell's avatar
gsell committed
340

341 342 343
    // there is no point to do repartitioning with one node
    if(Ippl::getNodes() == 1)
        repartFreq_m = 1000000;
gsell's avatar
gsell committed
344

345 346
    wakeStatus_m = false;
    surfaceStatus_m = false;
347

348
#ifdef OPAL_DKS
349
    if (IpplInfo::DKSEnabled) {
350 351
        //get number of elements in the bunch
        numDeviceElements = itsBunch->getLocalNum();
352

353 354 355 356
        //allocate memory on device
        r_ptr = dksbase.allocateMemory<Vector_t>(numDeviceElements, ierr);
        p_ptr = dksbase.allocateMemory<Vector_t>(numDeviceElements, ierr);
        x_ptr = dksbase.allocateMemory<Vector_t>(numDeviceElements, ierr);
357

358 359
        lastSec_ptr = dksbase.allocateMemory<long>(numDeviceElements, ierr);
        dt_ptr = dksbase.allocateMemory<double>(numDeviceElements, ierr);
360

361
        orient_ptr = dksbase.allocateMemory<Vector_t>(itsOpalBeamline_m.sections_m.size(), ierr);
362

363 364 365 366 367 368 369 370 371
        //get all the section orientations
        int nsec = itsOpalBeamline_m.sections_m.size();
        Vector_t *orientation = new Vector_t[nsec];
        for (long i = 0; i < nsec; i++) {
            orientation[i] = itsOpalBeamline_m.getOrientation(i);
        }
        
        //write orientations to device
        dksbase.writeData<Vector_t>(orient_ptr, orientation, nsec);
kraus's avatar
kraus committed
372

373 374
        //free local orientation memory
        delete[] orientation;
kraus's avatar
kraus committed
375

376 377
        //allocate memory on device for particle
        allocateDeviceMemory();
378

379 380 381 382 383 384 385 386 387 388 389 390
        //page lock itsBunch->X, itsBunch->R, itsBunch-P
        registerHostMemory();
        
        //write R, P and X data to device
        dksbase.writeDataAsync<Vector_t>(r_ptr, &itsBunch->R[0], itsBunch->getLocalNum());
        dksbase.writeDataAsync<Vector_t>(p_ptr, &itsBunch->P[0], itsBunch->getLocalNum());
        dksbase.writeDataAsync<Vector_t>(x_ptr, &itsBunch->X[0], itsBunch->getLocalNum());
        
        //create two new streams
        dksbase.createStream(stream1);
        dksbase.createStream(stream2);
    }
391 392
#endif

393 394 395 396
    while (localTrackSteps_m.size() > 0) {
        localTrackSteps_m.front() += step;
        dtCurrentTrack_m = dtAllTracks_m.front();
        changeDT();
397

398 399 400
        for(; step < localTrackSteps_m.front(); ++step) {
            bends_m = 0;
            numberOfFieldEmittedParticles_m = 0;
cwang's avatar
cwang committed
401
            
402
            itsOpalBeamline_m.resetStatus();
403

404 405
            // we dump later, after one step.
            // dumpStats(step, true, true);
406 407


408 409
            timeIntegration1(pusher);
            timeIntegration1_bgf(pusher);
410

411
            itsBunch->calcBeamParameters();
412

413 414 415
            // reset E and B to Vector_t(0.0) for every step
            itsBunch->Ef = Vector_t(0.0);
            itsBunch->Bf = Vector_t(0.0);
416

417 418 419
            if(step % repartFreq_m == 0 && step != 0) {
                doBinaryRepartition();
            }
420

421
            computeSpaceChargeFields();
422

kraus's avatar
kraus committed
423 424
            switchElements(10.0);

425 426 427
            selectDT();
            emitParticles(step);
            selectDT();
428

429
            computeExternalFields();
430

431 432
            timeIntegration2(pusher);
            timeIntegration2_bgf(pusher);
433

434
            bgf_main_collision_test();
435

436 437 438
            //t after a full global timestep with dT "synchronization point" for simulation time
            t += itsBunch->getdT();
            itsBunch->setT(t);
439

440 441
            bool const psDump = itsBunch->getGlobalTrackStep() % Options::psDumpFreq == 0;
            bool const statDump = itsBunch->getGlobalTrackStep() % Options::statDumpFreq == 0;
442
            dumpStats(step, psDump, statDump);
443

444
            if(hasEndOfLineReached()) break;
445

446
            itsBunch->incTrackSteps();
447

448
        }
449

450 451 452
        dtAllTracks_m.pop();
        localTrackSteps_m.pop();
        zStop_m.pop();
453 454
    }

455 456 457
    if(numParticlesInSimulation_m > minBinEmitted_m) {
        itsBunch->boundp();
        numParticlesInSimulation_m = itsBunch->getTotalNum();
458
    }
459 460

    bool const psDump = (itsBunch->getGlobalTrackStep() - 1) % Options::psDumpFreq != 0;
461
    bool const statDump = (itsBunch->getGlobalTrackStep() - 1) % Options::statDumpFreq != 0;
462
    writePhaseSpace((step + 1), itsBunch->get_sPos(), psDump, statDump);
kraus's avatar
kraus committed
463
    msg << level2 << "Dump phase space of last step" << endl;
464 465
    OPALTimer::Timer myt3;
    itsOpalBeamline_m.switchElementsOff();
kraus's avatar
kraus committed
466
    *gmsg << "done executing ParallelTTracker at " << myt3.time() << endl;
467 468

#ifdef OPAL_DKS
469
    if (IpplInfo::DKSEnabled) {
470 471 472 473 474 475
        //free device memory
        freeDeviceMemory();
        dksbase.freeMemory<Vector_t>(orient_ptr, itsOpalBeamline_m.sections_m.size());
        //unregister page lock itsBunch->X, itsBunch->R, itsBunch-P
        unregisterHostMemory();
    }
476
#endif
477 478
}

479
void ParallelTTracker::executeAMTSTracker() {
kraus's avatar
kraus committed
480
    Inform msg("ParallelTTracker ", *gmsg);
481
    const Vector_t vscaleFactor_m = Vector_t(scaleFactor_m);
482
    dtCurrentTrack_m = itsBunch->getdT();
483 484 485 486 487 488 489

    // upper limit of particle number when we do field emission and secondary emission
    // simulation. Could be reset to another value in input file with MAXPARTSNUM.
    maxNparts_m = 100000000;

    prepareSections();

490
    handleAutoPhasing();
491 492 493 494

    numParticlesInSimulation_m = itsBunch->getTotalNum();
    setTime();
    unsigned long long step = itsBunch->getLocalTrackStep();
495 496 497
    msg << "Track start at: " << OPALTimer::Timer().time() << ", t = " << itsBunch->getT() << "; zstop at: " << zStop_m.front() << " [m]" << endl;
    msg << "Executing ParallelTTracker, next step = " << step << endl;
    msg << "Using AMTS (adaptive multiple-time-stepping) integrator" << endl;
498 499 500
    itsOpalBeamline_m.print(msg);
    setupSUV();

501
    itsOpalBeamline_m.switchAllElements();
502 503 504 505 506 507 508 509 510 511

    setOptionalVariables();

    // there is no point to do repartitioning with one node
    if(Ippl::getNodes() == 1)
        repartFreq_m = 1000000;

    wakeStatus_m = false;
    surfaceStatus_m = false;

512 513 514 515 516
    // Count inner steps
    int totalInnerSteps = 0;

    itsBunch->boundp();
    itsBunch->calcBeamParameters();
517 518
    itsBunch->Ef = Vector_t(0.0);
    itsBunch->Bf = Vector_t(0.0);
519 520 521 522 523
    computeSpaceChargeFields();
    if(itsBunch->WeHaveEnergyBins()) {
        itsBunch->Rebin();
        itsBunch->resetInterpolationCache(true);
    }
524

525 526 527 528 529 530 531 532 533 534 535 536 537 538
    // AMTS step size initialization
    double const dt_inner_target = itsBunch->getdT();
    msg << "AMTS initialization: dt_inner_target = " << dt_inner_target << endl;
    double dt_outer, deltaTau;
    if(itsBunch->deltaTau_m != -1.0) {
        // DTAU is set in the inputfile, calc initial outer time step from that
        deltaTau = itsBunch->deltaTau_m;
        dt_outer = calcG() * deltaTau;
    } else {
        // Otherwise use DTSCINIT
        dt_outer = itsBunch->dtScInit_m;
        deltaTau = dt_outer / calcG();
    }
    msg << "AMTS initialization: dt_outer = " << dt_outer << " deltaTau = " << deltaTau << endl;
539

540 541 542 543 544 545 546 547 548 549 550
    // AMTS calculation of stopping times
    double const tEnd = itsBunch->getT() + double(localTrackSteps_m.front() - step) * dt_inner_target;
    double const psDumpInterval = double(Options::psDumpFreq) * dt_inner_target;
    double const statDumpInterval = double(Options::statDumpFreq) * dt_inner_target;
    double const repartInterval = double(repartFreq_m) * dt_inner_target;
    double const tTrackStart = itsBunch->getT() - double(step) * dt_inner_target; // we could be in a restarted simulation!
    double tNextPsDump = tTrackStart + psDumpInterval;
    while(tNextPsDump < itsBunch->getT()) tNextPsDump += psDumpInterval;
    double tNextStatDump = tTrackStart + statDumpInterval;
    while(tNextStatDump < itsBunch->getT()) tNextStatDump += statDumpInterval;
    double tDoNotRepartBefore = itsBunch->getT() + repartInterval;
551

552
    IpplTimings::startTimer(IpplTimings::getTimer("AMTS"));
553

554 555 556
    bool finished = false;
    for(; !finished; ++step) {
        itsOpalBeamline_m.resetStatus();
557

558 559 560 561 562 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
        // AMTS choose new timestep
        IpplTimings::startTimer(IpplTimings::getTimer("AMTS-TimestepSelection"));
        dt_outer = calcG() * deltaTau;
        double tAfterStep = itsBunch->getT() + dt_outer;
        double const tNextStop = std::min(std::min(tEnd, tNextPsDump), tNextStatDump);
        bool psDump = false, statDump = false;
        if(tAfterStep > tNextStop) {
            dt_outer = tNextStop - itsBunch->getT();
            tAfterStep = tNextStop;
        }
        double const eps = 1e-14; // To test approx. equality of times
        if(std::fabs(tAfterStep - tEnd) < eps) {
            finished = true;
        }
        if(std::fabs(tAfterStep - tNextPsDump) < eps) {
            psDump = true;
            tNextPsDump += psDumpInterval;
        }
        if(std::fabs(tAfterStep - tNextStatDump) < eps) {
            statDump = true;
            tNextStatDump += statDumpInterval;
        }
        msg << "AMTS: dt_outer = " << dt_outer;
        double numSubsteps = std::max(round(dt_outer / dt_inner_target), 1.0);
        msg << " numSubsteps = " << numSubsteps;
        double dt_inner = dt_outer / numSubsteps;
        msg << " dt_inner = " << dt_inner << endl;
        IpplTimings::stopTimer(IpplTimings::getTimer("AMTS-TimestepSelection"));

        IpplTimings::startTimer(IpplTimings::getTimer("AMTS-Kick"));
        if(itsBunch->hasFieldSolver()) {
            kick(0.5 * dt_outer);
        }
        IpplTimings::stopTimer(IpplTimings::getTimer("AMTS-Kick"));

        for(int n = 0; n < numSubsteps; ++n) {
            bool const isFirstSubstep = (n == 0);
            bool const isLastSubstep = (n == numSubsteps - 1);
            borisExternalFields(dt_inner, isFirstSubstep, isLastSubstep);
            ++totalInnerSteps;
        }

        IpplTimings::startTimer(IpplTimings::getTimer("AMTS-SpaceCharge"));
        if(itsBunch->hasFieldSolver()) {
            itsBunch->boundp();
            itsBunch->Ef = Vector_t(0.0);
            itsBunch->Bf = Vector_t(0.0);
            if(itsBunch->getT() >= tDoNotRepartBefore) {
            	doBinaryRepartition();
            	tDoNotRepartBefore = itsBunch->getT() + repartInterval;
            }
            computeSpaceChargeFields();
            if(itsBunch->WeHaveEnergyBins()) {
                itsBunch->rebin();
                itsBunch->resetInterpolationCache(true);
            }
        }
        IpplTimings::stopTimer(IpplTimings::getTimer("AMTS-SpaceCharge"));

        IpplTimings::startTimer(IpplTimings::getTimer("AMTS-Kick"));
        if(itsBunch->hasFieldSolver()) {
            kick(0.5 * dt_outer);
        }
        IpplTimings::stopTimer(IpplTimings::getTimer("AMTS-Kick"));

        IpplTimings::startTimer(IpplTimings::getTimer("AMTS-Dump"));
        itsBunch->RefPart_R = RefPartR_zxy_m;
        itsBunch->RefPart_P = RefPartP_zxy_m;
        itsBunch->calcBeamParameters();
        dumpStats(step, psDump, statDump);
        IpplTimings::stopTimer(IpplTimings::getTimer("AMTS-Dump"));

        if(hasEndOfLineReached()) break;
        itsBunch->incTrackSteps();
    }

    IpplTimings::stopTimer(IpplTimings::getTimer("AMTS"));

    msg << "totalInnerSteps = " << totalInnerSteps << endl;

    itsBunch->boundp();
    numParticlesInSimulation_m = itsBunch->getTotalNum();
    writePhaseSpace((step + 1), itsBunch->get_sPos(), true, true);
    msg << "Dump phase space of last step" << endl;
    itsOpalBeamline_m.switchElementsOff();
    msg << "done executing ParallelTTracker at " << OPALTimer::Timer().time() << endl;
}

646
void ParallelTTracker::doSchottyRenormalization() {
kraus's avatar
kraus committed
647
    Inform msg("ParallelTTracker ", *gmsg);
648 649 650
    double init_erg = itsBunch->getEkin();
    double tol_iter = 1e-5;
    rescale_coeff_m = 1 / init_erg / init_erg;
651

652 653 654 655 656 657
    if(Options::schottkyRennormalization > 0) {
        rescale_coeff_m = Options:: schottkyRennormalization;
        msg << "Set schottky scale coefficient to  " << rescale_coeff_m << endl;
    } else if(Options::schottkyCorrection) {
        while(true) {
            double real_charge = schottkyLoop(rescale_coeff_m);
658

659 660 661 662 663 664 665 666 667 668 669
            double total_charge = itsBunch->getTotalNum() * itsBunch->getChargePerParticle();
            msg << "Schottky scale coefficient " << rescale_coeff_m << ", actual emitted charge " << real_charge << " (Cb)" << endl;
            itsBunch->cleanUpParticles();
            itsBunch->setT(0);
            double scale_error = total_charge / real_charge - 1;
            // TODO : send scale_error to all nodes
            rescale_coeff_m *= (1.3 * scale_error + 1);
            if(fabs(scale_error) < tol_iter)
                break;
        }
        msg << "Schottky scan, final scale coefficient " << rescale_coeff_m << " ()" << endl;
670 671
    }
}
672

673
double ParallelTTracker::schottkyLoop(double rescale_coeff) {
674

kraus's avatar
kraus committed
675
    Inform msg("ParallelTTracker ", *gmsg);
676

677 678 679 680
    double recpgamma;
    double t = 0.0;
    double dt = itsBunch->getdT();
    Vector_t vscaleFactor = Vector_t(scaleFactor_m);
681

682 683
    unsigned long long step = 0;
    unsigned int emissionSteps = 0;
684

685 686 687 688
    Vector_t um, a, s;
    Vector_t externalE, externalB;
    BorisPusher pusher(itsReference);
    Vector_t rmin, rmax;
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 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
    bool global_EOL;

    bool hasSwitchedToTEmission = false;
    bool hasSwitchedBackToTTrack = false;

    size_t totalParticles_i = itsBunch->getTotalNum();

    msg << "*****************************************************************" << endl;
    msg << " Estimate Schottky correction                                    " << endl;
    msg << "*****************************************************************" << endl;

    double margin = 0.0;
    if(!mpacflg_m) {
        for(unsigned int i = 0; i < itsBunch->getLocalNum(); ++i) {
            long &l = itsBunch->LastSection[i];
            l = -1;
            itsOpalBeamline_m.getSectionIndexAt(itsBunch->R[i], l);
            itsBunch->ResetLocalCoordinateSystem(i, itsOpalBeamline_m.getOrientation(l), itsOpalBeamline_m.getSectionStart(l));
        }

        if(!(itsBunch->WeHaveEnergyBins())) {
            IpplTimings::startTimer(BinRepartTimer_m);
            itsBunch->do_binaryRepart();
            IpplTimings::stopTimer(BinRepartTimer_m);
            Ippl::Comm->barrier();
        }

        // Check if there are any particles in simulation. If there are,
        // as in a restart, use the usual function to calculate beam
        // parameters. If not, calculate beam parameters of the initial
        // beam distribution.
        if(totalParticles_i == 0) {// fixme: maybe cause nonsense output if initialized momenta=0; Q: by Chuan.
            itsBunch->calcBeamParametersInitial();
        } else {
            itsBunch->calcBeamParameters();
        }

        RefPartR_suv_m = RefPartR_zxy_m = itsBunch->get_rmean();
        RefPartP_suv_m = RefPartP_zxy_m = itsBunch->get_pmean();

        if(!OpalData::getInstance()->hasBunchAllocated()) {
            updateSpaceOrientation(false);  // vec{p} = (0,0,p_z), vec{r} = (0,0,z)
        }

        RefPartR_suv_m = itsBunch->get_rmean();
        RefPartP_suv_m = itsBunch->get_pmean();
        /* Activate all elements which influence the particles when the simulation starts;
         *  mark all elements which are already past.
         */

        /*
         increase margin from 3.*c*dt to 10.*c*dt to prevent that fieldmaps are accessed
         before they are allocated when increasing the timestep in the gun.
         */
        itsBunch->get_bounds(rmin, rmax);
        margin = 10. * RefPartP_suv_m(2) * scaleFactor_m / sqrt(1.0 + pSqr(RefPartP_suv_m, RefPartP_suv_m));
        margin = 0.01 > margin ? 0.01 : margin;
kraus's avatar
kraus committed
747
        itsOpalBeamline_m.switchElements(rmin(2) - margin, rmax(2) + margin, getEnergyMeV(RefPartP_suv_m));
748 749 750 751 752 753 754 755
    }

    double minBinEmitted  = 10.0;
    RealVariable *ar = dynamic_cast<RealVariable *>(OpalData::getInstance()->find("MINBINEMITTED"));
    if(ar) {
        minBinEmitted = ar->getReal();  // the space charge solver crashes if we use less than ~10 particles.
        // This variable controls the number of particles to be emitted before we use
        // the space charge solver.
756

kraus's avatar
kraus committed
757
        msg << level3 << "MINBINEMITTED " << minBinEmitted << endl;
758 759 760 761 762 763 764 765
    }


    double minStepforReBin  = 10000.0;
    RealVariable *br = dynamic_cast<RealVariable *>(OpalData::getInstance()->find("MINSTEPFORREBIN"));
    if(br) {
        minStepforReBin = br->getReal();  // this variable controls the minimal number of steps of emission (using bins)
        // before we can merge the bins
kraus's avatar
kraus committed
766
        msg << level3 << "MINSTEPFORREBIN " << minStepforReBin << endl;
767 768 769 770 771 772
    }

    int repartFreq = 1000;
    RealVariable *rep = dynamic_cast<RealVariable *>(OpalData::getInstance()->find("REPARTFREQ"));
    if(rep) {
        repartFreq = static_cast<int>(rep->getReal());  // this variable controls the minimal number of steps until we repartition the particles
kraus's avatar
kraus committed
773
        msg << level3 << "REPARTFREQ " << repartFreq << endl;
774
    }
775

776 777
    // there is no point to do repartitioning with one node
    if(Ippl::getNodes() == 1)
778
        repartFreq = 1000000;
779

780
    size_t totalParticles_f = 0;
781

782
    for(; step < localTrackSteps_m.front(); ++step) {
783
        global_EOL = true;  // check if any particle hasn't reached the end of the field from the last element
784

785
        itsOpalBeamline_m.resetStatus();
786

787 788 789
        IpplTimings::startTimer(timeIntegrationTimer1_m);

        // reset E and B to Vector_t(0.0) for every step
790

791 792
        itsBunch->Ef = Vector_t(0.0);
        itsBunch->Bf = Vector_t(0.0);
793

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
        Nimpact_m = 0; // Initial parallel plate benchmark variable.
        SeyNum_m = 0; // Initial parallel plate benchmark variable.

        for(unsigned int i = 0; i < itsBunch->getLocalNum(); ++i) {
            //scale each particle with c*dt
            itsBunch->R[i] /= vscaleFactor;
            pusher.push(itsBunch->R[i], itsBunch->P[i], itsBunch->dt[i]);
            // update local coordinate system of particleInform &PartBunc
            itsBunch->X[i] /= vscaleFactor;
            pusher.push(itsBunch->X[i], TransformTo(itsBunch->P[i], itsOpalBeamline_m.getOrientation(itsBunch->LastSection[i])),
                        itsBunch->getdT());
            itsBunch->X[i] *= vscaleFactor;
        }

        if(totalParticles_i > minBinEmitted) {
            itsBunch->boundp();
        }

        IpplTimings::stopTimer(timeIntegrationTimer1_m);
813

814
        itsBunch->calcBeamParameters();
815 816


817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
        /** \f[ Space Charge  \f]
         */
        if(itsBunch->hasFieldSolver() && totalParticles_i > minBinEmitted && fabs(itsBunch->getChargePerParticle()) > 0.0) {
            // Do repartition if we have enough particles.
            if(totalParticles_i > 1000 && (((step + 1) % repartFreq) == 0)) {
                INFOMSG("*****************************************************************" << endl);
                INFOMSG("do repartition because of repartFreq" << endl);
                INFOMSG("*****************************************************************" << endl);
                IpplTimings::startTimer(BinRepartTimer_m);
                itsBunch->do_binaryRepart();
                IpplTimings::stopTimer(BinRepartTimer_m);
                Ippl::Comm->barrier();
                INFOMSG("*****************************************************************" << endl);
                INFOMSG("do repartition done" << endl);
                INFOMSG("*****************************************************************" << endl);
            }

            // Calculate space charge.
            if(itsBunch->WeHaveEnergyBins()) {
                // When we have energy bins.
                itsBunch->calcGammas();
                ParticleAttrib<double> Q_back = itsBunch->Q;
                itsBunch->resetInterpolationCache();
                for(int binNumber = 0; binNumber <= itsBunch->getLastemittedBin() && binNumber < itsBunch->getNumBins(); ++binNumber) {
                    itsBunch->setBinCharge(binNumber);
                    itsBunch->computeSelfFields(binNumber);
                    itsBunch->Q = Q_back;
                }
            } else {
                // When we don't.
                itsBunch->computeSelfFields();
                /**
                 Need this maybe for the adaptive time integration scheme
                 pair<Vector_t,Vector_t> eExtrema = itsBunch->getEExtrema();
                 INFOMSG("maxE= " << eExtrema.first << " minE= " << eExtrema.second << endl);
                 */
            }
854
        }
855

856
        IpplTimings::startTimer(timeIntegrationTimer2_m);
857 858


859 860 861 862 863
        /*
         transport and emit particles
         that passed the cathode in the first
         half-step or that would pass it in the
         second half-step.
864

865 866
         to make IPPL and the field solver happy
         make sure that at least 10 particles are emitted
867

868 869
         also remember that node 0 has
         all the particles to be emitted
870

871 872 873
         this has to be done *after* the calculation of the
         space charges! thereby we neglect space charge effects
         in the very first step of a new-born particle.
874

875
         */
876

877
        if((itsBunch->WeHaveEnergyBins())) {
878

879 880 881 882 883 884 885 886 887
            // switch to TEmission
            if(!hasSwitchedToTEmission) {
                dt = itsBunch->GetEmissionDeltaT();
                itsBunch->setdT(dt);
                scaleFactor_m = dt * Physics::c;
                vscaleFactor = Vector_t(scaleFactor_m);
                msg << "Changing emission time step to: " << dt << endl;
                hasSwitchedToTEmission = true;
            }
888

889 890 891 892 893 894 895 896 897
            int ne = 0;
            Vector_t externalE = Vector_t(0.0);
            Vector_t externalB = Vector_t(0.0);
            itsOpalBeamline_m.getFieldAt(Vector_t(0.0),
                                         Vector_t(0.0),
                                         itsBunch->getT() + 0.5 * itsBunch->getdT(),
                                         externalE,
                                         externalB);
            ne += itsBunch->EmitParticles(externalE[2]);
898

899 900
            if(Options::schottkyCorrection && !hasSwitchedBackToTTrack)
                applySchottkyCorrection(*itsBunch, ne, t, rescale_coeff);
901

902 903
            reduce(ne, ne, OpAddAssign());
            totalParticles_i += ne;
gsell's avatar
gsell committed
904

905 906 907 908 909 910 911 912 913 914 915
            //emission has finished, reset to TTrack
            if(itsBunch->getNumBins() == itsBunch->getLastemittedBin() &&
               !hasSwitchedBackToTTrack) {
                //dt = dtTrack;
                itsBunch->setdT(dt);
                scaleFactor_m = dt * Physics::c;
                vscaleFactor = Vector_t(scaleFactor_m);
                msg << "Emission done. Switching back to track timestep: " << dt << endl;
                hasSwitchedBackToTTrack = true;
                break;
            }
916

917 918 919 920 921 922 923 924 925 926
        } else {
            //emission has finished, reset to TTrack
            if(!hasSwitchedBackToTTrack) {
                //dt = dtTrack;
                itsBunch->setdT(dt);
                scaleFactor_m = dt * Physics::c;
                vscaleFactor = Vector_t(scaleFactor_m);
                msg << "Emission done. Switching back to track timestep: " << dt << endl;
                hasSwitchedBackToTTrack = true;
            }
927

928
        }
929

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
        // push the reference particle by a half step
        recpgamma = 1.0 / sqrt(1.0 + dot(RefPartP_suv_m, RefPartP_suv_m));
        RefPartR_zxy_m += RefPartP_zxy_m * recpgamma / 2. * scaleFactor_m;

        //
        // get external fields for all particles
        //
        IpplTimings::startTimer(timeFieldEvaluation_m);
        for(unsigned int i = 0; i < itsBunch->getLocalNum(); ++i) {
            //FIXME: rethink scaling!
            itsBunch->R[i] *= Vector_t(Physics::c * itsBunch->dt[i], Physics::c * itsBunch->dt[i], Physics::c * itsBunch->dt[i]);

            long ls = itsBunch->LastSection[i];
            itsOpalBeamline_m.getSectionIndexAt(itsBunch->R[i], ls);
            if(ls != itsBunch->LastSection[i]) {
                if(!itsOpalBeamline_m.section_is_glued_to(itsBunch->LastSection[i], ls)) {
                    itsBunch->ResetLocalCoordinateSystem(i, itsOpalBeamline_m.getOrientation(ls), itsOpalBeamline_m.getSectionStart(ls));
                }
                itsBunch->LastSection[i] = ls;
            }
            const unsigned long rtv = itsOpalBeamline_m.getFieldAt(i, itsBunch->R[i], ls, t + itsBunch->dt[i] / 2., externalE, externalB);

            global_EOL = global_EOL && (rtv & BEAMLINE_EOL);

            // skip rest of the particle push if the
            // particle is out of bounds i.e. does not see
            // a E or B field
            if(rtv & BEAMLINE_OOB)
                itsBunch->Bin[i] = -1;


            itsBunch->Ef[i] += externalE;
            itsBunch->Bf[i] += externalB;

            itsBunch->R[i] /= Vector_t(Physics::c * itsBunch->dt[i], Physics::c * itsBunch->dt[i], Physics::c * itsBunch->dt[i]);

            // in case a particle is pushed behind the emission surface, delete the particle

            if(itsBunch->R[i](2) < 0)
                itsBunch->Bin[i] = -1;
gsell's avatar
gsell committed
970 971 972

        }

973
        IpplTimings::stopTimer(timeFieldEvaluation_m);
974

975
        // if(itsBunch->getLocalNum() == 0)
976
        //    global_EOL = false;
977

978 979 980
        /**
         Delete marked particles.
         */
gsell's avatar
gsell committed
981

982 983 984 985 986
        bool globPartOutOfBounds = (min(itsBunch->Bin) < 0);
        size_t ne = 0;
        if(globPartOutOfBounds) {
            ne = itsBunch->boundp_destroyT();
        }
gsell's avatar
gsell committed
987

988 989
        totalParticles_f = totalParticles_i - ne;
        if(ne > 0)
990 991
            msg << "* Deleted in Shotky " << ne << " particles, remaining "
                << totalParticles_f << " particles" << endl; //benchmark output
992

993
        kickParticles(pusher);
994

995 996 997 998
        if(totalParticles_f > 0) {
            // none of the particles is in a bending element
            updateReferenceParticle();
        }
999

1000 1001
        itsBunch->RefPart_R = RefPartR_zxy_m;
        itsBunch->RefPart_P = RefPartP_zxy_m;
1002

1003 1004 1005 1006 1007
        /*
          calculate the dimensions of the bunch and add a small margin to them;
          then decide which elements have to be triggered when an element is
          triggered memory is allocated and the field map is read in
        */
1008
        itsBunch->get_bounds(rmin, rmax);
1009

1010 1011 1012
        // trigger the elements
        margin = 3. * RefPartP_suv_m(2) * recpgamma;
        margin = 0.01 > margin ? 0.01 : margin;
1013 1014 1015 1016
        itsOpalBeamline_m.switchElements(
            (rmin(2) - margin)*scaleFactor_m,
            (rmax(2) + margin)*scaleFactor_m,
            getEnergyMeV(RefPartP_suv_m));
1017

1018 1019 1020 1021
        // start normal particle loop part 2 for simulation without boundary geometry.
        for(unsigned int i = 0; i < itsBunch->getLocalNum(); ++i) {
            pusher.push(itsBunch->R[i], itsBunch->P[i], itsBunch->dt[i]);
            //and scale back to dimensions
1022 1023 1024 1025
            itsBunch->R[i] *= Vector_t (
                Physics::c * itsBunch->dt[i],
                Physics::c * itsBunch->dt[i],
                Physics::c * itsBunch->dt[i]);