RFCavity.h 11.1 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
#ifndef CLASSIC_RFCavity_HH
#define CLASSIC_RFCavity_HH

// ------------------------------------------------------------------------
// $RCSfile: RFCavity.h,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.1.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: RFCavity
//   Defines the abstract interface for an accelerating structure.
//
// ------------------------------------------------------------------------
// Class category: AbsBeamline
// ------------------------------------------------------------------------
//
// $Date: 2000/03/27 09:32:31 $
// $Author: fci $
//
// ------------------------------------------------------------------------

24
#include "Algorithms/AbstractTimeDependence.h"
gsell's avatar
gsell committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#include "AbsBeamline/Component.h"
#include "Physics/Physics.h"

class Fieldmap;

// Class RFCavity
// ------------------------------------------------------------------------
/// Interface for RF cavity.
//  Class RFCavity defines the abstract interface for RF cavities.


class RFCavity: public Component {

public:

    enum CavityType { SW, SGSW };
    /// Constructor with given name.
    explicit RFCavity(const std::string &name);

    RFCavity();
    RFCavity(const RFCavity &);
    virtual ~RFCavity();

    /// Apply visitor to RFCavity.
    virtual void accept(BeamlineVisitor &) const;

    /// Get RF amplitude.
    virtual double getAmplitude() const = 0;

    /// Get RF frequencey.
    virtual double getFrequency() const = 0;

    /// Get RF phase.
    virtual double getPhase() const = 0;

    void dropFieldmaps();

    /// Set the name of the field map
    void setFieldMapFN(std::string fmapfn);

    std::string getFieldMapFN() const;

    void setAmplitudem(double vPeak);

    void setFrequencym(double freq);

    void setFrequency(double freq);

    double getFrequencym() const ;

    void setPhasem(double phase);

    void updatePhasem(double phase);

    double getPhasem() const;

    void setCavityType(std::string type);

    std::string getCavityType() const;

    void setFast(bool fast);

    bool getFast() const;

    void setAutophaseVeto(bool veto);

    bool getAutophaseVeto() const;

    double getAutoPhaseEstimate(const double & E0, const double & t0, const double & q, const double & m);

    std::pair<double, double> trackOnAxisParticle(const double & p0, 
                                                  const double & t0, 
                                                  const double & dt, 
                                                  const double & q, 
                                                  const double & mass);

    virtual void addKR(int i, double t, Vector_t &K);

    virtual void addKT(int i, double t, Vector_t &K);

105
    virtual bool apply(const size_t &i, const double &t, double E[], double B[]);
gsell's avatar
gsell committed
106

107
    virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B);
gsell's avatar
gsell committed
108 109 110 111 112

    virtual bool apply(const Vector_t &R, const Vector_t &centroid, const double &t, Vector_t &E, Vector_t &B);

    virtual void initialise(PartBunch *bunch, double &startField, double &endField, const double &scaleFactor);

113 114 115 116 117 118 119
//    virtual void initialise(PartBunch *bunch, const double &scaleFactor);

    virtual void initialise(PartBunch *bunch, const double &scaleFactor, std::shared_ptr<AbstractTimeDependence> freq_atd,
                            std::shared_ptr<AbstractTimeDependence> ampl_atd, std::shared_ptr<AbstractTimeDependence> phase_atd);



gsell's avatar
gsell committed
120 121 122 123 124

    virtual void finalise();

    virtual bool bends() const;

kraus's avatar
kraus committed
125
    virtual void goOnline(const double &kineticEnergy);
gsell's avatar
gsell committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

    virtual void goOffline();

    void setRmin(double rmin);

    void setRmax(double rmax);

    void setAzimuth(double angle);

    void setPerpenDistance(double pdis);

    void setGapWidth(double gapwidth);

    void setPhi0(double phi0);

    virtual double getRmin() const;

    virtual double getRmax() const;

    virtual double getAzimuth() const;

    virtual double getCosAzimuth() const;

    virtual double getSinAzimuth() const;

    virtual double getPerpenDistance() const;

    virtual double getGapWidth() const;

    virtual double getPhi0() const;

    virtual void setComponentType(std::string name);

    virtual std::string getComponentType()const;

    virtual double getCycFrequency()const;

163
    void getMomentaKick(const double normalRadius, double momentum[], const double t, const double dtCorrt, const int PID, const double restMass,const int chargenumber);
gsell's avatar
gsell committed
164 165 166

    double spline(double z, double *za);

167
    virtual ElementBase::ElementType getType() const;
gsell's avatar
gsell committed
168 169 170

    virtual void getDimensions(double &zBegin, double &zEnd) const;

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

    virtual std::shared_ptr<AbstractTimeDependence> getAmplitudeModel() const;
    virtual std::shared_ptr<AbstractTimeDependence> getPhaseModel() const;
    virtual std::shared_ptr<AbstractTimeDependence> getFrequencyModel() const;

    virtual void setAmplitudeModel(std::shared_ptr<AbstractTimeDependence> time_dep);
    void setAmplitudeModelName(std::string name) {amplitude_name_m=name;}
    std::string getAmplitudeModelName() { return amplitude_name_m;}

    virtual void setPhaseModel(std::shared_ptr<AbstractTimeDependence> time_dep);
    void setPhaseModelName(std::string name) {phase_name_m=name;}
    std::string getPhaseModelName() { return phase_name_m;}

    virtual void setFrequencyModel(std::shared_ptr<AbstractTimeDependence> time_dep);
    virtual void setFrequencyModelName(std::string name);
    std::string getFrequencyModelName() { return frequency_name_m;}

 protected:
    std::shared_ptr<AbstractTimeDependence> phase_td_m;
    std::string phase_name_m;
    std::shared_ptr<AbstractTimeDependence> amplitude_td_m;
    std::string amplitude_name_m;
    std::shared_ptr<AbstractTimeDependence> frequency_td_m;
    std::string frequency_name_m;
gsell's avatar
gsell committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
private:
    std::string filename_m;             /**< The name of the inputfile*/
    std::vector<std::string> multiFilenames_m;
    std::vector<Fieldmap*> multiFieldmaps_m;
    double scale_m;              /**< scale multiplier*/
    std::vector<double> multiScales_m;
    double phase_m;              /**< phase shift of time varying field(degrees)*/
    std::vector<double> multiPhases_m;
    double frequency_m;          /**< Read in frequency of time varying field(MHz)*/
    std::vector<double> multiFrequencies_m;
    double ElementEdge_m;
    double startField_m;         /**< starting point of field(m)*/
    double endField_m;
    std::vector<std::pair<double, double> > multi_start_end_field_m;

    CavityType type_m;

    bool fast_m;
    bool autophaseVeto_m;

    double rmin_m;
    double rmax_m;
    double angle_m;
    double sinAngle_m;
    double cosAngle_m;
    double pdis_m;
    double gapwidth_m;
    double phi0_m;

224 225 226
    std::unique_ptr<double[]> RNormal_m;
    std::unique_ptr<double[]> VrNormal_m;
    std::unique_ptr<double[]> DvDr_m;
gsell's avatar
gsell committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 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 329 330
    int num_points_m;

    inline size_t numFieldmaps() const {
        return multiFilenames_m.size();
    }

    
    double getdE(const int & i, 
                 const std::vector<double> & t, 
                 const double & dz,
                 const double & phi,
                 const double & frequency,
                 const std::vector<double> & F) const;

    double getdT(const int & i, 
                 const std::vector<double> & E,
                 const double & dz,
                 const double mass) const;
    
    double getdA(const int & i,
                 const std::vector<double> & t, 
                 const double & dz,
                 const double & phi,
                 const double & frequency,
                 const std::vector<double> & F) const;

    double getdB(const int & i,
                 const std::vector<double> & t, 
                 const double & dz,
                 const double & phi,
                 const double & frequency,
                 const std::vector<double> & F) const;

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

inline
double RFCavity::getdE(const int & i,
                       const std::vector<double> & t, 
                       const double & dz,
                       const double & phi,
                       const double & frequency,
                       const std::vector<double> & F) const {
    return dz / (frequency * frequency * (t[i] - t[i-1]) * (t[i] - t[i-1])) * 
        (frequency * (t[i] - t[i-1]) * (F[i] * sin(frequency * t[i] + phi) - F[i-1] * sin(frequency * t[i-1] + phi)) +
         (F[i] - F[i-1]) * (cos(frequency * t[i] + phi) - cos(frequency * t[i-1] + phi)));
}

inline
double RFCavity::getdT(const int & i, 
                       const std::vector<double> & E,
                       const double & dz,
                       const double mass) const {
    double gamma1  = 1. + (19. * E[i-1] + 1. * E[i]) / (20. * mass);
    double gamma2  = 1. + (17. * E[i-1] + 3. * E[i]) / (20. * mass);
    double gamma3  = 1. + (15. * E[i-1] + 5. * E[i]) / (20. * mass);
    double gamma4  = 1. + (13. * E[i-1] + 7. * E[i]) / (20. * mass);
    double gamma5  = 1. + (11. * E[i-1] + 9. * E[i]) / (20. * mass);
    double gamma6  = 1. + (9. * E[i-1] + 11. * E[i]) / (20. * mass);
    double gamma7  = 1. + (7. * E[i-1] + 13. * E[i]) / (20. * mass);
    double gamma8  = 1. + (5. * E[i-1] + 15. * E[i]) / (20. * mass);
    double gamma9  = 1. + (3. * E[i-1] + 17. * E[i]) / (20. * mass);
    double gamma10 = 1. + (1. * E[i-1] + 19. * E[i]) / (20. * mass);
    return dz *
        (1. / sqrt(1. - 1. / (gamma1 * gamma1)) +
         1. / sqrt(1. - 1. / (gamma2 * gamma2)) +
         1. / sqrt(1. - 1. / (gamma3 * gamma3)) +
         1. / sqrt(1. - 1. / (gamma4 * gamma4)) +
         1. / sqrt(1. - 1. / (gamma5 * gamma5)) +
         1. / sqrt(1. - 1. / (gamma6 * gamma6)) +
         1. / sqrt(1. - 1. / (gamma7 * gamma7)) +
         1. / sqrt(1. - 1. / (gamma8 * gamma8)) +
         1. / sqrt(1. - 1. / (gamma9 * gamma9)) +
         1. / sqrt(1. - 1. / (gamma10 * gamma10))) / (10. * Physics::c);
}

inline
double RFCavity::getdA(const int & i,
                       const std::vector<double> & t, 
                       const double & dz,
                       const double & phi,
                       const double & frequency,
                       const std::vector<double> & F) const {
    double dt = t[i] - t[i-1];
    return dz / (frequency * frequency * dt * dt) *
        (frequency * dt * (F[i] * cos(frequency * t[i] + phi) - F[i-1] * cos(frequency * t[i-1] + phi)) -
         (F[i] - F[i-1]) * (sin(frequency * t[i] + phi) - sin(frequency * t[i-1] + phi)));
}

inline
double RFCavity::getdB(const int & i,
                       const std::vector<double> & t, 
                       const double & dz,
                       const double & phi,
                       const double & frequency,
                       const std::vector<double> & F) const {
    double dt = t[i] - t[i-1];
    return dz / (frequency * frequency * dt * dt) * 
        (frequency * dt * (F[i] * sin(frequency * t[i] + phi) - F[i-1] * sin(frequency * t[i-1] + phi)) +
         (F[i] - F[i-1]) * (cos(frequency * t[i] + phi) - cos(frequency * t[i-1] + phi)));
}

#endif // CLASSIC_RFCavity_HH