MultiBunchHandler.h 4.59 KB
Newer Older
1 2 3 4
#ifndef OPAL_MULTI_BUNCH_HANDLER_H
#define OPAL_MULTI_BUNCH_HANDLER_H

#include "Algorithms/PartBunchBase.h"
frey_m's avatar
frey_m committed
5 6 7

#include <cassert>
#include <vector>
8 9 10 11 12 13 14

/* Helper class that stores bunch injection
 * information like azimuth, radius etc. of first
 * bunch in multi-bunch mode of ParallelCyclotronTracker.
 */
class MultiBunchHandler {
public:
frey_m's avatar
frey_m committed
15
    struct beaminfo_t {
frey_m's avatar
frey_m committed
16 17 18
        beaminfo_t(const double& t,
                   const double& lpath,
                   const double& theta)
frey_m's avatar
frey_m committed
19 20 21
            : time(t)
            , azimuth(theta)
            , prevAzimuth(-1.0)
frey_m's avatar
frey_m committed
22 23
            , pathlength(lpath)
        { };
frey_m's avatar
frey_m committed
24 25 26 27 28 29 30 31 32 33 34 35 36

        double time;
        double azimuth;
        double radius;
        double prevAzimuth;
        double pathlength;
        long unsigned int nParticles;
        double ekin;
        double dEkin;
        double rrms[3];
        double prms[3];
        double emit[3];
        double mean[3];
frey_m's avatar
frey_m committed
37
        double correlation[3];
frey_m's avatar
frey_m committed
38 39 40
        double halo[3];
    };

41 42
    // multi-bunch modes
    enum class MB_MODE {
43 44
        FORCE  = 0,
        AUTO   = 1
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    };

    // multi-bunch binning type
    enum class MB_BINNING {
        GAMMA = 0,
        BUNCH = 1
    };

    /*
     * @param numBunch > 1 --> multi bunch mode
     * @param eta binning value
     * @param para only for MB_MODE::AUTO
     * @param mode of multi-bunch
     * @param binning type of particle binning
     */
    MultiBunchHandler(PartBunchBase<double, 3> *beam,
                      const int& numBunch,
                      const double& eta,
                      const double& para,
                      const std::string& mode,
frey_m's avatar
frey_m committed
65
                      const std::string& binning);
66 67 68 69 70 71 72 73 74 75 76 77

    void saveBunch(PartBunchBase<double, 3> *beam,
                   const double& azimuth);

    bool readBunch(PartBunchBase<double, 3> *beam,
                   const PartData& ref);

    /* Returns:
     *     0 - if nothing happened
     *     1 - if bunch got saved
     *     2 - if bunch got injected
     */
frey_m's avatar
frey_m committed
78 79 80 81
    short injectBunch(PartBunchBase<double, 3> *beam,
                      const PartData& ref,
                      bool& flagTransition,
                      const double& azimuth);
82 83 84 85 86 87 88 89 90 91 92 93

    void updateParticleBins(PartBunchBase<double, 3> *beam);
    
    /// set the working sub-mode for multi-bunch mode: "FORCE" or "AUTO"
    void setMode(const std::string& mbmode);

    // set binning type
    void setBinning(std::string binning);

    void setRadiusTurns(const double& radius);

    /// set total number of tracked bunches
frey_m's avatar
frey_m committed
94
    void setTotalNumBunch(short n);
95 96

    /// get total number of tracked bunches
frey_m's avatar
frey_m committed
97
    short getTotalNumBunch() const;
98

frey_m's avatar
frey_m committed
99
    void setNumBunch(short n);
100

frey_m's avatar
frey_m committed
101
    short getNumBunch() const;
102 103 104

    bool isForceMode() const;

frey_m's avatar
frey_m committed
105
    bool calcBunchBeamParameters(PartBunchBase<double, 3>* beam, short bunchNr);
frey_m's avatar
frey_m committed
106

frey_m's avatar
frey_m committed
107
    beaminfo_t& getBunchInfo(short bunchNr);
frey_m's avatar
frey_m committed
108

frey_m's avatar
frey_m committed
109
    const beaminfo_t& getBunchInfo(short bunchNr) const;
frey_m's avatar
frey_m committed
110

111 112 113 114 115 116
private:
    // store the data of the beam which are required for injecting a
    // new bunch for multibunch filename
    std::string onebunch_m;

    /// The number of bunches specified in TURNS of RUN commond
frey_m's avatar
frey_m committed
117
    short numBunch_m;
118 119 120 121 122 123 124 125 126 127 128 129 130 131

    // parameter for reset bin in multi-bunch run
    double eta_m;

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

    // 0 for GAMMA (default),
    // 1 for BUNCH
    MB_BINNING binning_m; //binningType_m;

    // control parameter for AUTO multi-bunch mode
132
    double coeffDBunches_m;
133 134

    // used for automatic injection in multi-bunch mode
135 136
    double radiusLastTurn_m;
    double radiusThisTurn_m;
137 138

    // record how many bunches has already been injected.
frey_m's avatar
frey_m committed
139
    short bunchCount_m;
frey_m's avatar
frey_m committed
140 141 142

    // each list entry belongs to a bunch
    std::vector<beaminfo_t> binfo_m;
frey_m's avatar
frey_m committed
143 144 145 146 147

    // injection values
    double injTime_m;
    double injPathlength_m;
    double injAzimuth_m;
148 149 150 151
};


inline
frey_m's avatar
frey_m committed
152
void MultiBunchHandler::setTotalNumBunch(short n) {
153 154 155 156 157
    numBunch_m = n;
}


inline
frey_m's avatar
frey_m committed
158
short MultiBunchHandler::getTotalNumBunch() const {
159 160 161 162 163
    return numBunch_m;
}


inline
frey_m's avatar
frey_m committed
164
void MultiBunchHandler::setNumBunch(short n) {
165 166 167 168 169
    bunchCount_m = n;
}


inline
frey_m's avatar
frey_m committed
170
short MultiBunchHandler::getNumBunch() const {
171
    return bunchCount_m;
172 173
}

174

175 176 177 178 179
inline
bool MultiBunchHandler::isForceMode() const {
    return (mode_m == MB_MODE::FORCE);
}

frey_m's avatar
frey_m committed
180 181

inline
frey_m's avatar
frey_m committed
182 183 184
MultiBunchHandler::beaminfo_t& MultiBunchHandler::getBunchInfo(short bunchNr) {
    assert(bunchNr < 0 || bunchNr >= (short)binfo_m.size());
    return binfo_m[bunchNr];
frey_m's avatar
frey_m committed
185 186 187 188
}


inline
frey_m's avatar
frey_m committed
189 190 191
const MultiBunchHandler::beaminfo_t& MultiBunchHandler::getBunchInfo(short bunchNr) const {
    assert(bunchNr < 0 || bunchNr >= (short)binfo_m.size());
    return binfo_m[bunchNr];
frey_m's avatar
frey_m committed
192 193
}

194
#endif