ParallelCyclotronTracker.h 15.4 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#ifndef OPAL_ParallelCyclotronTracker_HH
#define OPAL_ParallelCyclotronTracker_HH

// ------------------------------------------------------------------------
// $RCSfile: ParallelCyclotronTracker.h,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.2.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: ParallelCyclotron
//
// ------------------------------------------------------------------------
//
// $Date: 2004/11/12 20:10:11 $
// $Author: adelmann $
//
// ------------------------------------------------------------------------

#include "Algorithms/Tracker.h"
#include "Structure/DataSink.h"
#include "Utilities/Options.h"
#include <vector>

class BMultipoleField;
class PartBunch;
class PlanarArcGeometry;
29 30
class OpalRing;
class SBend3D;
31 32
class VariableRFCavity;
class Offset;
gsell's avatar
gsell committed
33 34 35 36 37

// Class ParallelCyclotronTracker
// ------------------------------------------------------------------------
enum CyclOperationModeT {SINGLEP, MULTIP, TUNECALC};

38 39 40 41 42 43 44
struct CavityCrossData {
    RFCavity * cavity;
    double sinAzimuth;
    double cosAzimuth;
    double perpenDistance;
};

gsell's avatar
gsell committed
45 46 47 48
class ParallelCyclotronTracker: public Tracker {

public:

49 50
    typedef std::pair<double[8], Component *>      element_pair;
    typedef std::pair<string, element_pair>        type_pair;
gsell's avatar
gsell committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    typedef std::list<type_pair *>                 beamline_list;
    /// Constructor.
    //  The beam line to be tracked is "bl".
    //  The particle reference data are taken from "data".
    //  The particle bunch tracked is initially empty.
    //  If [b]revBeam[/b] is true, the beam runs from s = C to s = 0.
    //  If [b]revTrack[/b] is true, we track against the beam.
    explicit ParallelCyclotronTracker(const Beamline &bl, const PartData &data,
                                      bool revBeam, bool revTrack);

    /// Constructor.
    //  The beam line to be tracked is "bl".
    //  The particle reference data are taken from "data".
    //  The particle bunch tracked is taken from [b]bunch[/b].
    //  If [b]revBeam[/b] is true, the beam runs from s = C to s = 0.
    //  If [b]revTrack[/b] is true, we track against the beam.
    explicit ParallelCyclotronTracker(const Beamline &bl, PartBunch &bunch, DataSink &ds,
                                      const PartData &data, bool revBeam, bool revTrack, int maxSTEPS, int timeIntegrator);

    virtual ~ParallelCyclotronTracker();

72 73 74
    /// Apply the algorithm to an OpalRing
    virtual void visitOpalRing(const OpalRing &ring);

gsell's avatar
gsell committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    /// Apply the algorithm to a Cyclotorn
    virtual void visitCyclotron(const Cyclotron &cycl);

    /// Apply the algorithm to a RFCavity.
    virtual void visitRFCavity(const RFCavity &);

    /// Apply the algorithm to a BeamBeam.
    virtual void visitBeamBeam(const BeamBeam &);

    /// Apply the algorithm to a collimator.
    virtual void visitCollimator(const Collimator &);

    /// Apply the algorithm to a Corrector.
    virtual void visitCorrector(const Corrector &);

adelmann's avatar
adelmann committed
90 91 92
    /// Apply the algorithm to a Degrader
    virtual void visitDegrader(const Degrader &);

gsell's avatar
gsell committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    /// Apply the algorithm to a Diagnostic.
    virtual void visitDiagnostic(const Diagnostic &);

    /// Apply the algorithm to a Drift.
    virtual void visitDrift(const Drift &);

    /// Apply the algorithm to a Lambertson.
    virtual void visitLambertson(const Lambertson &);

    /// Apply the algorithm to a Marker.
    virtual void visitMarker(const Marker &);

    /// Apply the algorithm to a Monitor.
    virtual void visitMonitor(const Monitor &);

    /// Apply the algorithm to a Multipole.
    virtual void visitMultipole(const Multipole &);

111 112 113
    /// Apply the algorithm to a Offset.
    virtual void visitOffset(const Offset &);

gsell's avatar
gsell committed
114 115 116 117 118 119 120 121 122 123 124 125
    /// Apply the algorithm to a Probe.
    virtual void visitProbe(const Probe &);

    /// Apply the algorithm to a RBend.
    virtual void visitRBend(const RBend &);

    /// Apply the algorithm to a RFQuadrupole.
    virtual void visitRFQuadrupole(const RFQuadrupole &);

    /// Apply the algorithm to a SBend.
    virtual void visitSBend(const SBend &);

126 127 128
    /// Apply the algorithm to a SBend3D.
    virtual void visitSBend3D(const SBend3D &);

gsell's avatar
gsell committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    /// Apply the algorithm to a Separator.
    virtual void visitSeparator(const Separator &);

    /// Apply the algorithm to a Septum.
    virtual void visitSeptum(const Septum &);

    /// Apply the algorithm to a Solenoid.
    virtual void visitSolenoid(const Solenoid &);

    /// Apply the algorithm to a charge stripper.
    virtual void visitStripper(const Stripper &);

    /// Apply the algorithm to a ParallelPlate, it is empty for cyclotrontracker .
    virtual void visitParallelPlate(const ParallelPlate &);

    /// Apply the algorithm to a CyclotronValley.it is empty for cyclotrontracker .
    virtual void visitCyclotronValley(const CyclotronValley &);

147 148 149
    /// Apply the algorithm to a VariabelRFCavity.
    virtual void visitVariableRFCavity(const VariableRFCavity &cav);

gsell's avatar
gsell committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    /// Apply the algorithm to the top-level beamline.
    //  overwrite the execute-methode from DefaultVisitor
    virtual void execute();

    /// Apply the algorithm to a beam line.
    //  overwrite the execute-methode from DefaultVisitor
    virtual void visitBeamline(const Beamline &);

    /// set total number of tracked bunches
    inline void setNumBunch(int n) { numBunch_m = n; }

    /// get total number of tracked bunches
    inline int  getNumBunch() { return numBunch_m; }

    /// set the working sub-mode for multi-bunch mode: "FORCE" or "AUTO"
    inline void  setMultiBunchMode(const int flag) {multiBunchMode_m = flag; }

    /// set last dumped step
    inline void setLastDumpedStep(const int para) {lastDumpedStep_m = para ; }

    /// set the control parameter for "AUTO" sub-mode
    inline void  setParaAutoMode(const double para) {CoeffDBunches_m = para; }
  
173
    inline void setPr(double x) {referencePr = x;}
174 175
    inline void setPt(double x) {referencePt = x;}
    inline void setPz(double x) {referencePz = x;}
176 177
    inline void setR(double x) {referenceR = x;}
    inline void setTheta(double x) {referenceTheta = x;}
178
    inline void setZ(double x) {referenceZ = x;}
179
    inline void setBeGa(double x) {bega = x;}
180 181 182

    void bgf_main_collision_test();
    void initializeBoundaryGeometry();
183
    
gsell's avatar
gsell committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
private:

    // Not implemented.
    ParallelCyclotronTracker();
    ParallelCyclotronTracker(const ParallelCyclotronTracker &);
    void operator=(const ParallelCyclotronTracker &);

    beamline_list FieldDimensions;
    std::list<Component *> myElements;
    int LastVisited;
    Beamline *itsBeamline;

    PartBunch *itsBunch;

    DataSink *itsDataSink;

200 201
    BoundaryGeometry *bgf_m;

gsell's avatar
gsell committed
202 203 204
    /// The maximal number of steps the system is integrated
    int maxSteps_m;

205
    /// The positive axes unit vectors
206 207 208
    static Vector_t const xaxis;
    static Vector_t const yaxis;
    static Vector_t const zaxis;
209

gsell's avatar
gsell committed
210 211
    /// The scale factor for dimensionless variables
    double scaleFactor_m;
Daniel Winklehner's avatar
Daniel Winklehner committed
212 213

    /// The reference variables
214
    double bega;
gsell's avatar
gsell committed
215 216
    double referenceR;
    double referenceTheta;
Daniel Winklehner's avatar
Daniel Winklehner committed
217
    double referenceZ = 0.0;
gsell's avatar
gsell committed
218 219 220

    double referencePr;
    double referencePt;
Daniel Winklehner's avatar
Daniel Winklehner committed
221
    double referencePz = 0.0;
gsell's avatar
gsell committed
222 223
    double referencePtot;

224 225
    Vector_t PreviousMeanP;

gsell's avatar
gsell committed
226 227
    double sinRefTheta_m;
    double cosRefTheta_m;
Daniel Winklehner's avatar
Daniel Winklehner committed
228
    
gsell's avatar
gsell committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    /// The number of bunches specified in TURNS of RUN commond
    int numBunch_m;

    // 0 for single bunch (default),
    // 1 for FORCE,
    // 2 for AUTO
    int multiBunchMode_m;

    // control parameter for AUTO multi-bunch mode
    double CoeffDBunches_m;

    int lastDumpedStep_m;

    double PathLength_m;

    // the name of time integrator
    // The ID of time integrator
    // 0 --- RK-4(default)
    // 1 --- LF-2
248
    // 2 --- MTS
gsell's avatar
gsell committed
249 250 251 252
    int  timeIntegrator_m;

    void Tracker_LF();
    void Tracker_RK4();
253
    void Tracker_MTS();
gsell's avatar
gsell committed
254 255 256 257 258 259 260 261 262 263

    /*
     Local Variables both used by the integration methods
    */

    Vector_t rold_m, pold_m, rnew_m, ptmp_m;

    long long step_m;
    long long restartStep0_m;

264 265 266 267
    int turnnumber_m;

    double const eta_m; // parameter for reset bin in multi-bunch run, todo: readin from inputfile

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 320 321 322 323 324 325 326 327 328
    // temporal 6 phase space varibles of particle [x,y,z,px,py,pz]. Unit: mm & dimensionless
    double variable_m[6];
    // temporal 3 real space varibles of particle ID=0 [x,y,z]. for tune with SC.  Unit: mm
    Vector_t variable_tune0_m;
    // temporal 3 real space varibles of particle ID=1 [x,y,z]. for tune with SC.  Unit: mm
    Vector_t variable_tune1_m;

    // vector of [angle, x, y] of SEO read in from external file for tune with SC. Unit : rad, mm
    std::vector<Vector_t> variable_SEO_m;

    // save initial phase space distribution (in global Cartesian frame ) for multi-bunch simultion. FixMe: not used
    Vector_t *initialR_m, *initialP_m;

    // record how many bunches has already been injected. ONLY FOR MPM
    int BunchCount_m;

    // decide how many energy bins. ONLY FOR MPM
    // For the time being, we set bin number equal to bunch number.
    int BinCount_m;

    // used for automatic injection in multi-bunch mode
    double RLastTurn_m , RThisTurn_m;

    // start time to record tune data
    double StartTime_m;

    // external field arrays for dumping
    Vector_t FDext_m[2], extE_m, extB_m;

    // mark the dumpstep to inject new bunch from here for AUTO mode of restart run of multibunch
    int backupDumpStep_m;

    const int myNode_m;
    const size_t initialLocalNum_m;
    const size_t initialTotalNum_m;

    std::ofstream outfTheta0_m;
    std::ofstream outfTheta1_m;
    std::ofstream outfTheta2_m;
    std::ofstream outfThetaEachTurn_m;


    //store the data of the beam which are required for injecting a new bunch for multibunch
    Ppos_t r_mb, p_mb;

    ParticleAttrib<double> q_mb;
    ParticleAttrib<double> m_mb;
    ParticleAttrib<short> ptype_mb;
    size_t npart_mb;

    void openFiles(string fn);
    void closeFiles();

    // Fringe fields for entrance and exit of magnetic elements.
    void applyEntranceFringe(double edge, double curve,
                             const BMultipoleField &field, double scale);
    void applyExitFringe(double edge, double curve,
                         const BMultipoleField &field, double scale);

    void buildupFieldList(double BcParameter[], string ElementType, Component *elptr);

329
    bool derivate(double *y, const double &t, double *yp, const size_t &Pindex); 
gsell's avatar
gsell committed
330

331
    bool rk4(double x[], const double &t, const double &tau, const size_t &Pindex);
gsell's avatar
gsell committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

    // angle range [0~2PI) degree
    double calculateAngle(double x, double y);
    // angle range [-PI~PI) degree
    double calculateAngle2(double x, double y);

    bool readOneBunchFromFile(const size_t BeamCount);
    bool readOneBunch(const size_t BeamCount);
    void saveOneBunch();

    bool checkGapCross(Vector_t Rold, Vector_t Rnew, RFCavity * rfcavity, double &DistOld);
    bool RFkick(RFCavity * rfcavity, const double t, const double dt, const int Pindex);

    bool getTunes(std::vector<double> &t,  std::vector<double> &r,  std::vector<double> &z, int lastTurn, double &nur, double &nuz);

    IpplTimings::TimerRef IntegrationTimer_m;
    IpplTimings::TimerRef DumpTimer_m ;
    IpplTimings::TimerRef TransformTimer_m;
    IpplTimings::TimerRef BinRepartTimer_m;

    Vector_t calcMeanR() const;
    
    Vector_t calcMeanP() const;
    
    void repartition(); // Do repartition between nodes if step_m is multiple of Options::repartFreq
    
    // Transform the x- and y-parts of a particle attribute (position, momentum, fields) from the 
    // global reference frame to the local reference frame.
360

gsell's avatar
gsell committed
361 362 363 364 365 366 367
    // phi is the angle of the bunch measured counter-clockwise from the positive x-axis.
    void globalToLocal(ParticleAttrib<Vector_t> & vectorArray, double phi, Vector_t const translationToGlobal = 0);
    
    // Transform the x- and y-parts of a particle attribute (position, momentum, fields) from the 
    // local reference frame to the global reference frame.
    void localToGlobal(ParticleAttrib<Vector_t> & vectorArray, double phi, Vector_t const translationToGlobal = 0);
    
368
    // Overloaded version of globalToLocal using a quaternion for 3D rotation
369
    inline void globalToLocal(ParticleAttrib<Vector_t> & vectorArray, Quaternion_t const quaternion, Vector_t const meanR = Vector_t(0.0));
370 371
    
    // Overloaded version of localToGlobal using a quaternion for 3D rotation
372
    inline void localToGlobal(ParticleAttrib<Vector_t> & vectorArray, Quaternion_t const quaternion, Vector_t const meanR = Vector_t(0.0));
373 374
    
    // Overloaded version of globalToLocal using phi and theta for pseudo 3D rotation
375
    inline void globalToLocal(ParticleAttrib<Vector_t> & particleVectors, double const phi, double const psi, Vector_t const meanR = Vector_t(0.0));
376 377

    // Overloaded version of localToGlobal using phi and theta for pseudo 3D rotation
378
    inline void localToGlobal(ParticleAttrib<Vector_t> & particleVectors, double const phi, double const psi, Vector_t const meanR = Vector_t(0.0));
379 380

    // Rotate the particles by an angle and axis defined in the quaternion (4-vector w,x,y,z)
adelmann's avatar
adelmann committed
381
    inline void rotateWithQuaternion(ParticleAttrib<Vector_t> & vectorArray, Quaternion_t const quaternion);
382 383

    // Returns the quaternion of the rotation from vector u to vector v
adelmann's avatar
adelmann committed
384
    inline void getQuaternionTwoVectors(Vector_t u, Vector_t v, Quaternion_t & quaternion);
385 386

    // Normalization of a quaternion
adelmann's avatar
adelmann committed
387
    inline void normalizeQuaternion(Quaternion_t & quaternion);
388 389 390 391 392 393 394 395
    
    // Normalization of a quaternion
    inline void normalizeVector(Vector_t & vector); 

    // Some rotations
    inline void rotateAroundZ(ParticleAttrib<Vector_t> & particleVectors, double const phi);
    inline void rotateAroundX(ParticleAttrib<Vector_t> & particleVectors, double const psi);
    
396
    // Push particles for time h.
gsell's avatar
gsell committed
397 398 399 400 401
    // Apply effects of RF Gap Crossings.
    // Update time and path length.
    // Unit assumptions: [itsBunch->R] = m, [itsBunch->P] = 1, [h] = s, [c] = m/s, [itsBunch->getT()] = s
    void push(double h);

402 403 404 405 406 407 408
    // Kick particles for time h
    // The fields itsBunch->Bf, itsBunch->Ef are used to calculate the forces
    void kick(double h);

    // Apply the trilogy half push - kick - half push,
    // considering only external fields
    void borisExternalFields(double h);
409 410
    
    // apply the plugin elements: probe, collimator, stripper, septum 
411 412 413 414 415
    void applyPluginElements(const double dt);
    
    // destroy particles if they are marked as Bin=-1 in the plugin elements or out of global apeture
    bool deleteParticle();
    
416 417 418 419 420 421
    std::ofstream outfTrackOrbit_m;

    void initTrackOrbitFile();

    void singleParticleDump();

422 423
    void bunchDumpPhaseSpaceStatData();

424 425
    void evaluateSpaceChargeField();

426 427
    void initDistInGlobalFrame();

Matthias Toggweiler's avatar
Matthias Toggweiler committed
428 429
    void checkNumPart(std::string s);

430 431 432 433 434 435 436
    // we store a pointer explicitly to the OpalRing
    OpalRing* opalRing_m;

    // If OpalRing is defined take the harmonic number from OpalRing; else use
    // cyclotron
    double getHarmonicNumber() const;

gsell's avatar
gsell committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
};

/**
 *
 *
 * @param x
 * @param y
 *
 * @return angle range [0~2PI) degree
 */
inline
double ParallelCyclotronTracker::calculateAngle(double x, double y) {
    double thetaXY = atan2(y, x);

    if (thetaXY < 0) return thetaXY + Physics::two_pi;
    return thetaXY;
}

/**
 *
 *
 * @param x
 * @param y
 *
 * @return angle range [-PI~PI) degree
 */
inline
double ParallelCyclotronTracker::calculateAngle2(double x, double y) 
{ return atan2(y,x); }

#endif // OPAL_ParallelCyclotronTracker_HH