ParallelCyclotronTracker.cpp 230 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// ------------------------------------------------------------------------
// $RCSfile: ParallelCyclotronTracker.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.1 $initialLocalNum_m
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: ParallelCyclotronTracker
//   The class for tracking particles with 3D space charge in Cyclotrons and FFAG's
//
// ------------------------------------------------------------------------
//
// $Date: 2007/10/17 04:00:08 $
15
// $Author: adelmann, yang, winklehner $
gsell's avatar
gsell committed
16 17
//
// ------------------------------------------------------------------------
kraus's avatar
kraus committed
18

19
#include <Algorithms/Ctunes.hpp>
kraus's avatar
kraus committed
20 21
#include "Algorithms/ParallelCyclotronTracker.h"

gsell's avatar
gsell committed
22 23 24 25
#include <cfloat>
#include <iostream>
#include <fstream>
#include <vector>
26
#include "AbstractObjects/OpalData.h"
gsell's avatar
gsell committed
27 28 29 30

#include "AbsBeamline/Collimator.h"
#include "AbsBeamline/Corrector.h"
#include "AbsBeamline/Cyclotron.h"
adelmann's avatar
adelmann committed
31
#include "AbsBeamline/Degrader.h"
gsell's avatar
gsell committed
32 33 34 35
#include "AbsBeamline/Diagnostic.h"
#include "AbsBeamline/Drift.h"
#include "AbsBeamline/ElementBase.h"
#include "AbsBeamline/Lambertson.h"
36
#include "AbsBeamline/Offset.h"
gsell's avatar
gsell committed
37 38 39 40 41 42 43 44
#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/RFQuadrupole.h"
#include "AbsBeamline/SBend.h"
45
#include "AbsBeamline/SBend3D.h"
gsell's avatar
gsell committed
46 47 48 49 50
#include "AbsBeamline/Separator.h"
#include "AbsBeamline/Septum.h"
#include "AbsBeamline/Solenoid.h"
#include "AbsBeamline/CyclotronValley.h"
#include "AbsBeamline/Stripper.h"
51
#include "AbsBeamline/VariableRFCavity.h"
52 53

#include "Elements/OpalBeamline.h"
kraus's avatar
kraus committed
54
#include "AbsBeamline/Ring.h"
gsell's avatar
gsell committed
55 56 57

#include "BeamlineGeometry/Euclid3D.h"
#include "BeamlineGeometry/PlanarArcGeometry.h"
Jianjun Yang's avatar
Jianjun Yang committed
58
#include "BeamlineGeometry/RBendGeometry.h"
gsell's avatar
gsell committed
59 60 61 62 63 64 65 66 67 68 69 70
#include "Beamlines/Beamline.h"

#include "Fields/BMultipoleField.h"
#include "FixedAlgebra/FTps.h"
#include "FixedAlgebra/FTpsMath.h"
#include "FixedAlgebra/FVps.h"

#include "Physics/Physics.h"

#include "Utilities/NumToStr.h"
#include "Utilities/OpalException.h"

71
#include "Structure/H5PartWrapperForPC.h"
72
#include "Structure/BoundaryGeometry.h"
kraus's avatar
kraus committed
73
#include "Utilities/OpalOptions.h"
gsell's avatar
gsell committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

#include "Ctunes.h"
#include <cassert>

#include <hdf5.h>
#include "H5hut.h"

class Beamline;
class PartData;
using Physics::c;
using Physics::m_p; // GeV
using Physics::PMASS;
using Physics::PCHARGE;
using Physics::pi;
using Physics::q_e;

const double c_mmtns = c * 1.0e-6; // m/s --> mm/ns
const double mass_coeff = 1.0e18 * q_e / c / c; // from GeV/c^2 to basic unit: GV*C*s^2/m^2
92
const double PIOVER180 = pi / 180.0;
93

94 95 96
Vector_t const ParallelCyclotronTracker::xaxis = Vector_t(1.0, 0.0, 0.0);
Vector_t const ParallelCyclotronTracker::yaxis = Vector_t(0.0, 1.0, 0.0);
Vector_t const ParallelCyclotronTracker::zaxis = Vector_t(0.0, 0.0, 1.0);
gsell's avatar
gsell committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

#define PSdim 6

extern Inform *gmsg;

// typedef FVector<double, PSdim> Vector;

/**
 * Constructor ParallelCyclotronTracker
 *
 * @param beamline
 * @param reference
 * @param revBeam
 * @param revTrack
 */
ParallelCyclotronTracker::ParallelCyclotronTracker(const Beamline &beamline,
        const PartData &reference,
        bool revBeam, bool revTrack):
    Tracker(beamline, reference, revBeam, revTrack),
116
    eta_m(0.01),
gsell's avatar
gsell committed
117 118
    myNode_m(Ippl::myNode()),
    initialLocalNum_m(0),
119
    initialTotalNum_m(0),
120 121
    opalRing_m(NULL),
    lastDumpedStep_m(0) {
gsell's avatar
gsell committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    itsBeamline = dynamic_cast<Beamline *>(beamline.clone());
}

/**
 * Constructor ParallelCyclotronTracker
 *
 * @param beamline
 * @param bunch
 * @param ds
 * @param reference
 * @param revBeam
 * @param revTrack
 * @param maxSTEPS
 * @param timeIntegrator
 */
ParallelCyclotronTracker::ParallelCyclotronTracker(const Beamline &beamline,
                                                   PartBunch &bunch,
                                                   DataSink &ds,
                                                   const PartData &reference,
                                                   bool revBeam, bool revTrack,
                                                   int maxSTEPS, int timeIntegrator):
    Tracker(beamline, reference, revBeam, revTrack),
    maxSteps_m(maxSTEPS),
    timeIntegrator_m(timeIntegrator),
146
    eta_m(0.01),
gsell's avatar
gsell committed
147 148
    myNode_m(Ippl::myNode()),
    initialLocalNum_m(bunch.getLocalNum()),
149
    initialTotalNum_m(bunch.getTotalNum()),
150 151
    opalRing_m(NULL),
    lastDumpedStep_m(0) {
gsell's avatar
gsell committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    itsBeamline = dynamic_cast<Beamline *>(beamline.clone());
    itsBunch = &bunch;
    itsDataSink = &ds;
    //  scaleFactor_m = itsBunch->getdT() * c;
    scaleFactor_m = 1;
    multiBunchMode_m = 0;

    IntegrationTimer_m = IpplTimings::getTimer("Integration");
    TransformTimer_m   = IpplTimings::getTimer("Frametransform");
    DumpTimer_m        = IpplTimings::getTimer("Dump");
    BinRepartTimer_m   = IpplTimings::getTimer("Binaryrepart");
}

/**
 * Destructor ParallelCyclotronTracker
 *
 */
ParallelCyclotronTracker::~ParallelCyclotronTracker() {
170
    for(std::list<Component *>::iterator compindex = myElements.begin(); compindex != myElements.end(); compindex++) {
gsell's avatar
gsell committed
171 172 173 174 175 176
        delete(*compindex);
    }
    for(beamline_list::iterator fdindex = FieldDimensions.begin(); fdindex != FieldDimensions.end(); fdindex++) {
        delete(*fdindex);
    }
    delete itsBeamline;
177 178 179
    if (opalRing_m != NULL) {
        // delete opalRing_m;
    }
gsell's avatar
gsell committed
180 181
}

182 183 184 185 186 187
/**
 * AAA
 *
 * @param none
 */
void ParallelCyclotronTracker::initializeBoundaryGeometry() {
188
  for(std::list<Component *>::iterator compindex = myElements.begin(); compindex != myElements.end(); compindex++) {
189
    bgf_m = dynamic_cast<ElementBase *>(*compindex)->getBoundaryGeometry();
190
    if(!bgf_m)
191 192 193 194
      continue;
    else
      break;
  }
adelmann's avatar
adelmann committed
195
  if (bgf_m) {
196
    itsDataSink->writeGeomToVtk(*bgf_m, std::string("data/testGeometry-00000.vtk"));
adelmann's avatar
adelmann committed
197 198 199
    OpalData::getInstance()->setGlobalGeometry(bgf_m);
    *gmsg << "* Boundary geometry initialized " << endl;
  }
200
}
201

202 203 204 205 206 207 208 209 210
/**
 *
 *
 * @param fn Base file name
 */
void ParallelCyclotronTracker::bgf_main_collision_test() {
  if(!bgf_m) return;

  Inform msg("bgf_main_collision_test ");
211 212

  /**
213 214 215
   *Here we check if a particles is outside the domain, flag it for deletion
   */

gsell's avatar
gsell committed
216
  Vector_t intecoords = 0.0;
217 218 219 220 221

  // This has to match the dT in the rk4 pusher! -DW
  //double dtime = 0.5 * itsBunch->getdT();  // Old
  double dtime = itsBunch->getdT() * getHarmonicNumber();  // New

222
  int triId = 0;
223 224
  size_t Nimpact = 0;
  for(size_t i = 0; i < itsBunch->getLocalNum(); i++) {
225
    int res = bgf_m->PartInside(itsBunch->R[i]*1.0e-3, itsBunch->P[i], dtime, itsBunch->PType[i], itsBunch->Q[i], intecoords, triId);
226
    if(res >= 0) {
227 228
      itsBunch->Bin[i] = -1;
      Nimpact++;
229
    }
230 231 232 233
  }
}


gsell's avatar
gsell committed
234 235 236 237 238
/**
 *
 *
 * @param fn Base file name
 */
239
void ParallelCyclotronTracker::openFiles(std::string SfileName) {
gsell's avatar
gsell committed
240

241
    std::string  SfileName2 = SfileName + std::string("-Angle0.dat");
gsell's avatar
gsell committed
242 243

    outfTheta0_m.precision(8);
244
    outfTheta0_m.setf(std::ios::scientific, std::ios::floatfield);
gsell's avatar
gsell committed
245
    outfTheta0_m.open(SfileName2.c_str());
246
    outfTheta0_m << "#  r [mm]      beta_r*gamma       theta [mm]      beta_theta*gamma        z [mm]          beta_z*gamma" << std::endl;
gsell's avatar
gsell committed
247

248
    SfileName2 = SfileName + std::string("-Angle1.dat");
gsell's avatar
gsell committed
249
    outfTheta1_m.precision(8);
250
    outfTheta1_m.setf(std::ios::scientific, std::ios::floatfield);
gsell's avatar
gsell committed
251
    outfTheta1_m.open(SfileName2.c_str());
252
    outfTheta1_m << "#  r [mm]      beta_r*gamma       theta [mm]      beta_theta*gamma        z [mm]          beta_z*gamma"  << std::endl;
gsell's avatar
gsell committed
253

254
    SfileName2 = SfileName + std::string("-Angle2.dat");
gsell's avatar
gsell committed
255
    outfTheta2_m.precision(8);
256
    outfTheta2_m.setf(std::ios::scientific, std::ios::floatfield);
gsell's avatar
gsell committed
257
    outfTheta2_m.open(SfileName2.c_str());
258
    outfTheta2_m << "#  r [mm]      beta_r*gamma       theta [mm]      beta_theta*gamma        z [mm]          beta_z*gamma"  << std::endl;
gsell's avatar
gsell committed
259 260 261

    // for single Particle Mode, output after each turn, to define matched initial phase ellipse.

262
    SfileName2 = SfileName + std::string("-afterEachTurn.dat");
gsell's avatar
gsell committed
263 264

    outfThetaEachTurn_m.precision(8);
265
    outfThetaEachTurn_m.setf(std::ios::scientific, std::ios::floatfield);
gsell's avatar
gsell committed
266 267

    outfThetaEachTurn_m.open(SfileName2.c_str());
268
    outfTheta2_m << "#  r [mm]      beta_r*gamma       theta [mm]      beta_theta*gamma        z [mm]          beta_z*gamma"  << std::endl;
gsell's avatar
gsell committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
}

/**
 * Close all files related to
 * special output in the Cyclotron
 * mode.
 */
void ParallelCyclotronTracker::closeFiles() {

    outfTheta0_m.close();
    outfTheta1_m.close();
    outfTheta2_m.close();
    outfThetaEachTurn_m.close();
}

284
/**
285 286 287
 *
 * @param ring
 */
kraus's avatar
kraus committed
288
void ParallelCyclotronTracker::visitRing(const Ring &ring) {
Daniel Winklehner's avatar
Daniel Winklehner committed
289

290
    *gmsg << "* ----------------------------- Adding Ring ------------------------------ *" << endl;
Daniel Winklehner's avatar
Daniel Winklehner committed
291 292

    if (opalRing_m != NULL)
293
        delete opalRing_m;
Daniel Winklehner's avatar
Daniel Winklehner committed
294

kraus's avatar
kraus committed
295
    opalRing_m = dynamic_cast<Ring*>(ring.clone());
Daniel Winklehner's avatar
Daniel Winklehner committed
296

297
    myElements.push_back(opalRing_m);
Daniel Winklehner's avatar
Daniel Winklehner committed
298

299 300 301 302 303
    opalRing_m->initialise(itsBunch);

    referenceR = opalRing_m->getBeamRInit();
    referencePr = opalRing_m->getBeamPRInit();
    referenceTheta = opalRing_m->getBeamPhiInit();
Daniel Winklehner's avatar
Daniel Winklehner committed
304

305
    if(referenceTheta <= -180.0 || referenceTheta > 180.0) {
kraus's avatar
kraus committed
306
        throw OpalException("Error in ParallelCyclotronTracker::visitRing",
307 308
                            "PHIINIT is out of [-180, 180)!");
    }
Daniel Winklehner's avatar
Daniel Winklehner committed
309 310

    referenceZ = 0.0;
311
    referencePz = 0.0;
Daniel Winklehner's avatar
Daniel Winklehner committed
312 313 314 315

    referencePtot = itsReference.getGamma() * itsReference.getBeta();
    referencePt = sqrt(referencePtot * referencePtot - referencePr * referencePr);

316 317
    if(referencePtot < 0.0)
        referencePt *= -1.0;
Daniel Winklehner's avatar
Daniel Winklehner committed
318

319 320
    sinRefTheta_m = sin(referenceTheta * PIOVER180);
    cosRefTheta_m = cos(referenceTheta * PIOVER180);
gsell's avatar
gsell committed
321

322
    double BcParameter[8];
Daniel Winklehner's avatar
Daniel Winklehner committed
323

324
    for(int i = 0; i < 8; i++)
Daniel Winklehner's avatar
Daniel Winklehner committed
325 326
        BcParameter[i] = 0.0;

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    buildupFieldList(BcParameter, "OPALRING", opalRing_m);

    // Finally print some diagnostic
    *gmsg << "* Initial beam radius = " << referenceR << " [mm] " << endl;
    *gmsg << "* Initial gamma = " << itsReference.getGamma() << endl;
    *gmsg << "* Initial beta = " << itsReference.getBeta() << endl;
    *gmsg << "* Total reference momentum   = " << referencePtot * 1000.0
          << " [MCU]" << endl;
    *gmsg << "* Reference azimuthal momentum  = " << referencePt * 1000.0
          << " [MCU]" << endl;
    *gmsg << "* Reference radial momentum     = " << referencePr * 1000.0
          << " [MCU]" << endl;
    *gmsg << "* " << opalRing_m->getSymmetry() << " fold field symmetry "
          << endl;
    *gmsg << "* Harmonic number h= " << opalRing_m->getHarmonicNumber() << " "
          << endl;
}
gsell's avatar
gsell committed
344 345 346 347 348 349 350 351

/**
 *
 *
 * @param cycl
 */
void ParallelCyclotronTracker::visitCyclotron(const Cyclotron &cycl) {

352
    *gmsg << "* -------------------------- Adding Cyclotron ---------------------------- *" << endl;
gsell's avatar
gsell committed
353

354 355
    Cyclotron *elptr = dynamic_cast<Cyclotron *>(cycl.clone());
    myElements.push_back(elptr);
356

357
    // Fresh run (no restart):
358
    if(!OpalData::getInstance()->inRestartRun()) {
359

360 361 362 363 364
        // Get reference values from cyclotron element
        referenceR     = elptr->getRinit();
        referenceTheta = elptr->getPHIinit();
        referenceZ     = elptr->getZinit();
        referencePr    = elptr->getPRinit();
365 366
        referencePz    = elptr->getPZinit();

367
        if(referenceTheta <= -180.0 || referenceTheta > 180.0) {
368 369
            throw OpalException("Error in ParallelCyclotronTracker::visitCyclotron",
                                "PHIINIT is out of [-180, 180)!");
370 371 372 373 374
        }

        referencePtot =  itsReference.getGamma() * itsReference.getBeta();

        // Calculate reference azimuthal (tangential) momentum from total-, z- and radial momentum:
375 376
        float insqrt = referencePtot * referencePtot - \
                       referencePr * referencePr - referencePz * referencePz;
377 378 379 380 381 382 383 384 385

        if(insqrt < 0) {

            if(insqrt > -1.0e-10) {

	        referencePt = 0.0;

            } else {

386
	        throw OpalException("Error in ParallelCyclotronTracker::visitCyclotron",
387
                                    "Pt imaginary!");
388 389 390 391 392 393 394
            }

        } else {

            referencePt = sqrt(insqrt);
        }

395
        if(referencePtot < 0.0)
Daniel Winklehner's avatar
Daniel Winklehner committed
396
            referencePt *= -1.0;
397 398 399 400 401
        // End calculate referencePt

    // Restart a run:
    } else {

402
        // If the user wants to save the restarted run in local frame,
403 404
        // make sure the previous h5 file was local too
        if (Options::psDumpLocalFrame) {
405

406
	    if (!previousH5Local) {
407

408 409
                throw OpalException("Error in ParallelCyclotronTracker::visitCyclotron",
                                    "You are trying a local restart from a global h5 file!");
410 411
	    }
	// Else, if the user wants to save the restarted run in global frame,
412 413
        // make sure the previous h5 file was global too
        } else {
414

415
	    if (previousH5Local) {
416

417 418
                throw OpalException("Error in ParallelCyclotronTracker::visitCyclotron",
                                    "You are trying a global restart from a local h5 file!");
419
            }
420
        }
421

422 423 424 425
        // Adjust some of the reference variables from the h5 file
        referencePhi *= PIOVER180;
        referencePsi *= PIOVER180;
        referencePtot = bega;
426 427 428

        if(referenceTheta <= -180.0 || referenceTheta > 180.0) {

429
            throw OpalException("Error in ParallelCyclotronTracker::visitCyclotron",
430 431
                                "PHIINIT is out of [-180, 180)!");
        }
432 433
    }

434
    sinRefTheta_m = sin(referenceTheta * PIOVER180);
435 436
    cosRefTheta_m = cos(referenceTheta * PIOVER180);

437
    *gmsg << endl;
adelmann's avatar
adelmann committed
438
    *gmsg << "* Bunch global starting position:" << endl;
439 440
    *gmsg << "* RINIT = " << referenceR  << " [mm]" << endl;
    *gmsg << "* PHIINIT = " << referenceTheta << " [deg]" << endl;
441
    *gmsg << "* ZINIT = " << referenceZ << " [mm]" << endl;
442
    *gmsg << endl;
adelmann's avatar
adelmann committed
443
    *gmsg << "* Bunch global starting momenta:" << endl;
gsell's avatar
gsell committed
444 445
    *gmsg << "* Initial gamma = " << itsReference.getGamma() << endl;
    *gmsg << "* Initial beta = " << itsReference.getBeta() << endl;
446
    *gmsg << "* Reference total momentum (beta * gamma) = " << referencePtot * 1000.0 << " [MCU]" << endl;
447 448 449
    *gmsg << "* Reference azimuthal momentum (Pt) = " << referencePt * 1000.0 << " [MCU]" << endl;
    *gmsg << "* Reference radial momentum (Pr) = " << referencePr * 1000.0 << " [MCU]" << endl;
    *gmsg << "* Reference axial momentum (Pz) = " << referencePz * 1000.0 << " [MCU]" << endl;
450
    *gmsg << endl;
adelmann's avatar
adelmann committed
451

gsell's avatar
gsell committed
452
    double sym = elptr->getSymmetry();
453
    *gmsg << "* " << sym << "-fold field symmerty " << endl;
gsell's avatar
gsell committed
454

455 456 457
    // ckr: this just returned the default value as defined in Component.h
    // double rff = elptr->getRfFrequ();
    // *gmsg << "* Rf frequency= " << rff << " [MHz]" << endl;
gsell's avatar
gsell committed
458

459
    std::string fmfn = elptr->getFieldMapFN();
460
    *gmsg << "* Field map file name = " << fmfn << " " << endl;
gsell's avatar
gsell committed
461

462
    std::string type = elptr->getCyclotronType();
463
    *gmsg << "* Type of cyclotron = " << type << " " << endl;
464

465 466
    double rmin = elptr->getMinR();
    double rmax = elptr->getMaxR();
467
    *gmsg << "* Radial aperture = " << rmin << " ... " << rmax<<" [mm] "<< endl;
468 469 470

    double zmin = elptr->getMinZ();
    double zmax = elptr->getMaxZ();
471
    *gmsg << "* Vertical aperture = " << zmin << " ... " << zmax<<" [mm]"<< endl;
gsell's avatar
gsell committed
472

473
    /**
474
    bool Sflag = elptr->getSuperpose();
475
    std::string flagsuperposed;
476 477 478 479
    if (Sflag)
      flagsuperposed="yes";
    else
      flagsuperposed="no";
480
    *gmsg << "* Electric field maps are superposed? " << flagsuperposed << " " << endl;
481
    */
482

gsell's avatar
gsell committed
483
    double h = elptr->getCyclHarm();
484
    *gmsg << "* Harmonic number h = " << h << " " << endl;
gsell's avatar
gsell committed
485

486
    /**
adelmann's avatar
adelmann committed
487
    if (elptr->getSuperpose())
488
        *gmsg << "* Fields are superposed " << endl;
489
    */
adelmann's avatar
adelmann committed
490

gsell's avatar
gsell committed
491 492 493 494 495 496 497 498 499 500 501
    /**
     * To ease the initialise() function, set a integral parameter fieldflag internally.
     * Its value is  by the option "TYPE" of the element  "CYCLOTRON"
     * fieldflag = 1, readin PSI format measured field file (default)
     * fieldflag = 2, readin carbon cyclotron field file created by Jianjun Yang, TYPE=CARBONCYCL
     * fieldflag = 3, readin ANSYS format file for CYCIAE-100 created by Jianjun Yang, TYPE=CYCIAE
     * fieldflag = 4, readin AVFEQ format file for Riken cyclotrons
     * fieldflag = 5, readin FFAG format file for MSU/FNAL FFAG
     * fieldflag = 6, readin both median plane B field map and 3D E field map of RF cavity for compact cyclotron
     */
    int  fieldflag;
502
    if(type == std::string("CARBONCYCL")) {
gsell's avatar
gsell committed
503
        fieldflag = 2;
504
    } else if(type == std::string("CYCIAE")) {
gsell's avatar
gsell committed
505
        fieldflag = 3;
506
    } else if(type == std::string("AVFEQ")) {
gsell's avatar
gsell committed
507
        fieldflag = 4;
508
    } else if(type == std::string("FFAG")) {
gsell's avatar
gsell committed
509
        fieldflag = 5;
510
    } else if(type == std::string("BANDRF")) {
gsell's avatar
gsell committed
511
        fieldflag = 6;
512
    } else //(type == "RING")
gsell's avatar
gsell committed
513 514 515 516
        fieldflag = 1;

    // read field map on the  middle plane of cyclotron.
    // currently scalefactor is set to 1.0
517
    // TEMP changed 1.0 to getBScale() to test if we can scale the midplane field -DW
518
    elptr->initialise(itsBunch, fieldflag, elptr->getBScale());
gsell's avatar
gsell committed
519 520

    double BcParameter[8];
521

522
    for(int i = 0; i < 8; i++)
adelmann's avatar
adelmann committed
523
      BcParameter[i] = 0.0;
524

gsell's avatar
gsell committed
525 526 527
    BcParameter[0] = elptr->getRmin();
    BcParameter[1] = elptr->getRmax();

528 529
    // Store inner radius and outer radius of cyclotron field map in the list
    buildupFieldList(BcParameter, "CYCLOTRON", elptr);
gsell's avatar
gsell committed
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
}

/**
 * Not implemented and most probable never used
 *
 */
void ParallelCyclotronTracker::visitBeamBeam(const BeamBeam &) {
    *gmsg << "In BeamBeam tracker is missing " << endl;
}

/**
 *
 *
 * @param coll
 */
void ParallelCyclotronTracker::visitCollimator(const Collimator &coll) {

547
    *gmsg << "* --------- Collimator -----------------------------" << endl;
gsell's avatar
gsell committed
548

549 550
    Collimator* elptr = dynamic_cast<Collimator *>(coll.clone());
    myElements.push_back(elptr);
gsell's avatar
gsell committed
551

552
    double xstart = elptr->getXStart();
adelmann's avatar
adelmann committed
553
    *gmsg << "* Xstart= " << xstart << " [mm]" << endl;
gsell's avatar
gsell committed
554

555
    double xend = elptr->getXEnd();
adelmann's avatar
adelmann committed
556
    *gmsg << "* Xend= " << xend << " [mm]" << endl;
gsell's avatar
gsell committed
557

558
    double ystart = elptr->getYStart();
adelmann's avatar
adelmann committed
559
    *gmsg << "* Ystart= " << ystart << " [mm]" << endl;
gsell's avatar
gsell committed
560

561
    double yend = elptr->getYEnd();
562
    *gmsg << "* Yend= " << yend << " [mm]" << endl;
gsell's avatar
gsell committed
563

564
    double zstart = elptr->getZStart();
adelmann's avatar
adelmann committed
565
    *gmsg << "* Zstart= " << zstart << " [mm]" << endl;
566 567

    double zend = elptr->getZEnd();
568
    *gmsg << "* Zend= " << zend << " [mm]" << endl;
569

570
    double width = elptr->getWidth();
adelmann's avatar
adelmann committed
571
    *gmsg << "* Width= " << width << " [mm]" << endl;
gsell's avatar
gsell committed
572 573 574 575 576 577

    elptr->initialise(itsBunch, 1.0);

    double BcParameter[8];
    for(int i = 0; i < 8; i++)
        BcParameter[i] = 0.0;
578
    std::string ElementType = "CCOLLIMATOR";
579 580 581 582
    BcParameter[0] = xstart ;
    BcParameter[1] = xend;
    BcParameter[2] = ystart ;
    BcParameter[3] = yend;
gsell's avatar
gsell committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596
    BcParameter[4] = width ;
    buildupFieldList(BcParameter, ElementType, elptr);
}

/**
 *
 *
 * @param corr
 */
void ParallelCyclotronTracker::visitCorrector(const Corrector &corr) {
    *gmsg << "In Corrector; L= " << corr.getElementLength() << endl;
    myElements.push_back(dynamic_cast<Corrector *>(corr.clone()));
}

adelmann's avatar
adelmann committed
597 598 599 600 601 602 603 604 605 606 607 608
/**
 *
 *
 * @param degrader
 */
void ParallelCyclotronTracker::visitDegrader(const Degrader &deg) {
    *gmsg << "In Degrader; L= " << deg.getElementLength() << endl;
    myElements.push_back(dynamic_cast<Degrader *>(deg.clone()));

}


gsell's avatar
gsell committed
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
/**
 *
 *
 * @param diag
 */
void ParallelCyclotronTracker::visitDiagnostic(const Diagnostic &diag) {
    *gmsg << "In Diagnostic; L= " << diag.getElementLength() << endl;
    myElements.push_back(dynamic_cast<Diagnostic *>(diag.clone()));
}

/**
 *
 *
 * @param drift
 */
void ParallelCyclotronTracker::visitDrift(const Drift &drift) {
    *gmsg << "In drift L= " << drift.getElementLength() << endl;
    myElements.push_back(dynamic_cast<Drift *>(drift.clone()));
}

/**
 *
 *
 * @param lamb
 */
void ParallelCyclotronTracker::visitLambertson(const Lambertson &lamb) {
    *gmsg << "In Lambertson; L= " << lamb.getElementLength() << endl;
    myElements.push_back(dynamic_cast<Lambertson *>(lamb.clone()));
}

639 640 641 642
void ParallelCyclotronTracker::visitOffset(const Offset & off) {
    if (opalRing_m == NULL)
        throw OpalException(
                        "ParallelCylcotronTracker::visitOffset",
kraus's avatar
kraus committed
643
                        "Attempt to place an offset when Ring not defined");
644 645 646 647 648 649
    Offset* offNonConst = const_cast<Offset*>(&off);
    offNonConst->updateGeometry(opalRing_m->getNextPosition(),
                       opalRing_m->getNextNormal());
    opalRing_m->appendElement(off);
}

gsell's avatar
gsell committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
/**
 *
 *
 * @param marker
 */
void ParallelCyclotronTracker::visitMarker(const Marker &marker) {
    //   *gmsg << "In Marker; L= " << marker.getElementLength() << endl;
    myElements.push_back(dynamic_cast<Marker *>(marker.clone()));
    // Do nothing.
}

/**
 *
 *
 * @param corr
 */
void ParallelCyclotronTracker::visitMonitor(const Monitor &corr) {
    //   *gmsg << "In Monitor; L= " << corr.getElementLength() << endl;
    myElements.push_back(dynamic_cast<Monitor *>(corr.clone()));
    //   applyDrift(flip_s * corr.getElementLength());
}

672

gsell's avatar
gsell committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
/**
 *
 *
 * @param mult
 */
void ParallelCyclotronTracker::visitMultipole(const Multipole &mult) {
    *gmsg << "In Multipole; L= " << mult.getElementLength() << " however the element is missing " << endl;
    myElements.push_back(dynamic_cast<Multipole *>(mult.clone()));
}

/**
 *
 *
 * @param prob
 */
void ParallelCyclotronTracker::visitProbe(const Probe &prob) {
689
    *gmsg << "* -----------  Probe -------------------------------" << endl;
690 691
    Probe *elptr = dynamic_cast<Probe *>(prob.clone());
    myElements.push_back(elptr);
gsell's avatar
gsell committed
692

693
    double xstart = elptr->getXstart();
694
    *gmsg << "XStart= " << xstart << " [mm]" << endl;
gsell's avatar
gsell committed
695

696
    double xend = elptr->getXend();
697
    *gmsg << "XEnd= " << xend << " [mm]" << endl;
gsell's avatar
gsell committed
698

699
    double ystart = elptr->getYstart();
700
    *gmsg << "YStart= " << ystart << " [mm]" << endl;
gsell's avatar
gsell committed
701

702
    double yend = elptr->getYend();
703
    *gmsg << "YEnd= " << yend << " [mm]" << endl;
gsell's avatar
gsell committed
704

705
    double width = elptr->getWidth();
706
    *gmsg << "Width= " << width << " [mm]" << endl;
gsell's avatar
gsell committed
707 708 709 710 711 712 713 714


    // initialise, do nothing
    elptr->initialise(itsBunch, 1.0);

    double BcParameter[8];
    for(int i = 0; i < 8; i++)
        BcParameter[i] = 0.0;
715
    std::string ElementType = "PROBE";
gsell's avatar
gsell committed
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
    BcParameter[0] = xstart ;
    BcParameter[1] = xend;
    BcParameter[2] = ystart ;
    BcParameter[3] = yend;
    BcParameter[4] = width ;

    // store probe parameters in the list
    buildupFieldList(BcParameter, ElementType, elptr);
}

/**
 *
 *
 * @param bend
 */
void ParallelCyclotronTracker::visitRBend(const RBend &bend) {
    *gmsg << "In RBend; L= " << bend.getElementLength() << " however the element is missing " << endl;
    myElements.push_back(dynamic_cast<RBend *>(bend.clone()));
}

736 737 738 739 740 741 742 743 744
void ParallelCyclotronTracker::visitSBend3D(const SBend3D &bend) {
    *gmsg << "Adding SBend3D" << endl;
    if (opalRing_m != NULL)
        opalRing_m->appendElement(bend);
    else
        throw OpalException("ParallelCyclotronTracker::visitSBend3D",
                      "Need to define a RINGDEFINITION to use SBend3D element");
}

745 746 747 748 749 750 751 752 753
void ParallelCyclotronTracker::visitVariableRFCavity(const VariableRFCavity &cav) {
    *gmsg << "Adding Variable RF Cavity" << endl;
    if (opalRing_m != NULL)
        opalRing_m->appendElement(cav);
    else
        throw OpalException("ParallelCyclotronTracker::visitVariableRFCavity",
            "Need to define a RINGDEFINITION to use VariableRFCavity element");
}

gsell's avatar
gsell committed
754 755 756 757 758 759 760 761
/**
 *
 *
 * @param as
 */
void ParallelCyclotronTracker::visitRFCavity(const RFCavity &as) {

    *gmsg << "* --------- RFCavity ------------------------------" << endl;
762

763 764
    RFCavity *elptr = dynamic_cast<RFCavity *>(as.clone());
    myElements.push_back(elptr);
gsell's avatar
gsell committed
765 766 767 768 769 770 771

    if((elptr->getComponentType() != "SINGLEGAP") && (elptr->getComponentType() != "DOUBLEGAP")) {
        *gmsg << (elptr->getComponentType()) << endl;
        throw OpalException("ParallelCyclotronTracker::visitRFCavity",
                            "The ParallelCyclotronTracker can only play with cyclotron type RF system currently ...");
    }

772
    double rmin = elptr->getRmin();
gsell's avatar
gsell committed
773 774
    *gmsg << "* Minimal radius of cavity= " << rmin << " [mm]" << endl;

775
    double rmax = elptr->getRmax();
gsell's avatar
gsell committed
776 777
    *gmsg << "* Maximal radius of cavity= " << rmax << " [mm]" << endl;

778
    double rff = elptr->getCycFrequency();
gsell's avatar
gsell committed
779 780
    *gmsg << "* RF frequency (2*pi*f)= " << rff << " [rad/s]" << endl;

781
    std::string fmfn = elptr->getFieldMapFN();
gsell's avatar
gsell committed
782
    *gmsg << "* RF Field map file name= " << fmfn << endl;
783
    double angle = elptr->getAzimuth();
gsell's avatar
gsell committed
784 785
    *gmsg << "* Cavity azimuth position= " << angle << " [deg] " << endl;

786
    double gap = elptr->getGapWidth();
gsell's avatar
gsell committed
787 788
    *gmsg << "* Cavity gap width= " << gap << " [mm] " << endl;

789
    double pdis = elptr->getPerpenDistance();
gsell's avatar
gsell committed
790 791 792
    *gmsg << "* Cavity Shift distance= " << pdis << " [mm] " << endl;


793
    double phi0 = elptr->getPhi0();
gsell's avatar
gsell committed
794 795 796 797 798 799 800 801
    *gmsg << "* Initial RF phase (t=0)= " << phi0 << " [deg] " << endl;

    // read cavity voltage profile data from file.
    elptr->initialise(itsBunch, 1.0);

    double BcParameter[8];
    for(int i = 0; i < 8; i++)
        BcParameter[i] = 0.0;
802
    std::string ElementType = "CAVITY";
gsell's avatar
gsell committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816
    BcParameter[0] = rmin;
    BcParameter[1] = rmax;
    BcParameter[2] = pdis;
    BcParameter[3] = angle;

    buildupFieldList(BcParameter, ElementType, elptr);
}

/**
 *
 *
 * @param rfq
 */
void ParallelCyclotronTracker::visitRFQuadrupole(const RFQuadrupole &rfq) {
817
    *gmsg << "In RFQuadrupole; L = " << rfq.getElementLength() << " however the element is missing " << endl;
gsell's avatar
gsell committed
818 819 820 821 822 823 824 825 826
    myElements.push_back(dynamic_cast<RFQuadrupole *>(rfq.clone()));
}

/**
 *
 *
 * @param bend
 */
void ParallelCyclotronTracker::visitSBend(const SBend &bend) {
827
    *gmsg << "In SBend; L = " << bend.getElementLength() << " however the element is missing " << endl;
gsell's avatar
gsell committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
    myElements.push_back(dynamic_cast<SBend *>(bend.clone()));
}

/**
 *
 *
 * @param sep
 */
void ParallelCyclotronTracker::visitSeparator(const Separator &sep) {
    *gmsg << "In Seapator L= " << sep.getElementLength() << " however the element is missing " << endl;
    myElements.push_back(dynamic_cast<Separator *>(sep.clone()));
}

/**
 *
 *
 * @param sept
 */
void ParallelCyclotronTracker::visitSeptum(const Septum &sept) {
847

848
    *gmsg << endl << "* -----------------------------  Septum ------------------------------- *" << endl;
gsell's avatar
gsell committed
849

850 851
    Septum *elptr = dynamic_cast<Septum *>(sept.clone());
    myElements.push_back(elptr);
gsell's avatar
gsell committed
852

853
    double xstart = elptr->getXstart();
854
    *gmsg << "XStart = " << xstart << " [mm]" << endl;
gsell's avatar
gsell committed
855

856
    double xend = elptr->getXend();
857
    *gmsg << "XEnd = " << xend << " [mm]" << endl;
gsell's avatar
gsell committed
858

859
    double ystart = elptr->getYstart();
860
    *gmsg << "YStart = " << ystart << " [mm]" << endl;
gsell's avatar
gsell committed
861

862
    double yend = elptr->getYend();
863
    *gmsg << "YEnd = " << yend << " [mm]" << endl;
gsell's avatar
gsell committed
864

865
    double width = elptr->getWidth();
866
    *gmsg << "Width = " << width << " [mm]" << endl;
gsell's avatar
gsell committed
867 868 869 870 871 872 873 874


    // initialise, do nothing
    elptr->initialise(itsBunch, 1.0);

    double BcParameter[8];
    for(int i = 0; i < 8; i++)
        BcParameter[i] = 0.0;
875
    std::string ElementType = "SEPTUM";
gsell's avatar
gsell committed
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
    BcParameter[0] = xstart ;
    BcParameter[1] = xend;
    BcParameter[2] = ystart ;
    BcParameter[3] = yend;
    BcParameter[4] = width ;

    // store septum parameters in the list
    buildupFieldList(BcParameter, ElementType, elptr);
}

/**
 *
 *
 * @param solenoid
 */
void ParallelCyclotronTracker::visitSolenoid(const Solenoid &solenoid) {
    myElements.push_back(dynamic_cast<Solenoid *>(solenoid.clone()));
    Component *elptr = *(--myElements.end());
    if(!elptr->hasAttribute("ELEMEDGE")) {
        *gmsg << "Solenoid: no position of the element given!" << endl;
        return;
    }
}

/**
 *
 *
 * @param pplate
 */
void ParallelCyclotronTracker::visitParallelPlate(const ParallelPlate &pplate) {//do nothing

    //*gmsg << "ParallelPlate: not in use in ParallelCyclotronTracker!" << endl;

    //buildupFieldList(startField, endField, elptr);

}

/**
 *
 *
 * @param cv
 */
void ParallelCyclotronTracker::visitCyclotronValley(const CyclotronValley &cv) {
    // Do nothing here.
}
/**
 * not used
 *
 * @param angle
 * @param curve
 * @param field
 * @param scale
 */
void ParallelCyclotronTracker::applyEntranceFringe(double angle, double curve,
        const BMultipoleField &field, double scale) {

}

/**
 *
 *
 * @param stripper
 */

void ParallelCyclotronTracker::visitStripper(const Stripper &stripper) {

    *gmsg << "* ---------Stripper------------------------------" << endl;

944 945 946 947
    Stripper *elptr = dynamic_cast<Stripper *>(stripper.clone());
    myElements.push_back(elptr);

    double xstart = elptr->getXstart();
gsell's avatar
gsell committed
948 949
    *gmsg << "XStart= " << xstart << " [mm]" << endl;

950
    double xend = elptr->getXend();
gsell's avatar
gsell committed
951 952
    *gmsg << "XEnd= " << xend << " [mm]" << endl;

953
    double ystart = elptr->getYstart();
gsell's avatar
gsell committed
954 955
    *gmsg << "YStart= " << ystart << " [mm]" << endl;

956
    double yend = elptr->getYend();
gsell's avatar
gsell committed
957 958
    *gmsg << "YEnd= " << yend << " [mm]" << endl;

959
    double width = elptr->getWidth();
gsell's avatar
gsell committed
960 961
    *gmsg << "Width= " << width << " [mm]" << endl;

962
    double opcharge = elptr->getOPCharge();
963
    *gmsg << "Charge of outcoming particle = +e * " << opcharge << endl;
gsell's avatar
gsell committed
964

965
    double opmass = elptr->getOPMass();
966
    *gmsg << "* Mass of the outcoming particle = " << opmass << " [GeV/c^2]" << endl;
gsell's avatar
gsell committed
967 968 969 970 971 972

    elptr->initialise(itsBunch, 1.0);

    double BcParameter[8];
    for(int i = 0; i < 8; i++)
        BcParameter[i] = 0.0;
973
    std::string ElementType = "STRIPPER";
gsell's avatar
gsell committed
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
    BcParameter[0] = xstart ;
    BcParameter[1] = xend;
    BcParameter[2] = ystart ;
    BcParameter[3] = yend;
    BcParameter[4] = width ;
    BcParameter[5] = opcharge;
    BcParameter[6] = opmass;

    buildupFieldList(BcParameter, ElementType, elptr);
}


void ParallelCyclotronTracker::applyExitFringe(double angle, double curve,
        const BMultipoleField &field, double scale) {

}


/**
 *
 *
 * @param BcParameter
 * @param ElementType
 * @param elptr
 */
999
void ParallelCyclotronTracker::buildupFieldList(double BcParameter[], std::string ElementType, Component *elptr) {
gsell's avatar
gsell committed
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
    beamline_list::iterator sindex;

    type_pair *localpair = new type_pair();
    localpair->first = ElementType;

    for(int i = 0; i < 8; i++)
        *(((localpair->second).first) + i) = *(BcParameter + i);

    (localpair->second).second = elptr;

    // always put cyclotron as the first element in the list.
1011
    if(ElementType == "OPALRING" || ElementType == "CYCLOTRON") {
gsell's avatar
gsell committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
        sindex = FieldDimensions.begin();
    } else {
        sindex = FieldDimensions.end();
    }
    FieldDimensions.insert(sindex, localpair);

}

/**
 *
 *
 * @param bl
 */
void ParallelCyclotronTracker::visitBeamline(const Beamline &bl) {
    itsBeamline->iterate(*dynamic_cast<BeamlineVisitor *>(this), false);
}

Matthias Toggweiler's avatar
Matthias Toggweiler committed
1029 1030 1031 1032
void ParallelCyclotronTracker::checkNumPart(std::string s) {
    int nlp = itsBunch->getLocalNum();
    int minnlp = 0;
    int maxnlp = 111111;
gsell's avatar
gsell committed
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    reduce(nlp, minnlp, OpMinAssign());
    reduce(nlp, maxnlp, OpMaxAssign());
    *gmsg << s << " min local particle number " << minnlp << " max local particle number: " << maxnlp << endl;
}

/**
 *
 *
 */
void ParallelCyclotronTracker::execute() {

    /*
      Initialize common variables and structures
      for the integrators
    */

    step_m = 0;
    restartStep0_m = 0;
Daniel Winklehner's avatar
Daniel Winklehner committed
1051 1052

    // Record how many bunches have already been injected. ONLY FOR MPM
gsell's avatar
gsell committed
1053 1054 1055 1056 1057 1058
    BunchCount_m = itsBunch->getNumBunch();

    // For the time being, we set bin number equal to bunch number. FixMe: not used
    BinCount_m = BunchCount_m;

    itsBeamline->accept(*this);
1059 1060
    if (opalRing_m != NULL)
        opalRing_m->lockRing();
gsell's avatar
gsell committed
1061

Daniel Winklehner's avatar
Daniel Winklehner committed
1062 1063
    // Display the selected elements
    *gmsg << "* -------------------------------------" << endl;
adelmann's avatar
Cleanup  
adelmann committed
1064
    *gmsg << "* The selected Beam line elements are :" << endl;
1065

gsell's avatar
gsell committed
1066
    for(beamline_list::iterator sindex = FieldDimensions.begin(); sindex != FieldDimensions.end(); sindex++)
1067 1068
        *gmsg << "* -> " <<  ((*sindex)->first) << endl;