GreenWakeFunction.hh 5.06 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6
#ifndef GREENWAKEFUNCTION_HH
#define GREENWAKEFUNCTION_HH

#include "Filters/Filter.h"
#include "Solvers/WakeFunction.hh"
#include "Physics/Physics.h"
7
#include "Utility/IpplInfo.h"
gsell's avatar
gsell committed
8 9 10

#include <vector>
#include <cassert>
11 12 13
#include <map>
#include <string>
#include <complex>
gsell's avatar
gsell committed
14

15 16 17
#ifdef WITH_UNIT_TESTS
#include <gtest/gtest_prod.h>
#endif
gsell's avatar
gsell committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

enum { TRANSVERSAL, LONGITUDINAL };
typedef std::map<std::string, int> FilterOptions;

class GreenWakeFunction: public WakeFunction {
public:
    ~GreenWakeFunction();
    //IFF: changed direction to int (was double)
    //IFF: changed acMode to int (was double)
    GreenWakeFunction(const std::string &name,
                      std::vector<Filter *> filters,
                      int NBIN,
                      double Z0,
                      double radius,
                      double sigma,
                      int acMode,
                      double tau,
                      int direction,
                      bool constLength,
                      std::string fname);

    std::pair<int, int> distrIndices(int vectLen);

frey_m's avatar
frey_m committed
41
    void apply(PartBunchBase<double, 3> *bunch);
gsell's avatar
gsell committed
42 43 44 45
    void setWakeFromFile(int NBin, double spacing);
    virtual const std::string getType() const;

private:
46 47 48 49
#ifdef WITH_UNIT_TESTS
    FRIEND_TEST(GreenWakeFunctionTest, TestApply);
#endif

gsell's avatar
gsell committed
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
    class Wake {

    public:

        Wake(double s, double Z0, double a, double sigma, int acMode, double tau, int direction)
            : Z0_(Z0), a_(a), sigma_(sigma), s_(s), acMode_(acMode), tau_(tau), direction_(direction)
        {}

        /**
         * @brief   Used to integrate the function
         *
         * @param[in]   k parameter
         *
         * @return  the function value at position k
         */
        double operator()(double k) {

            std::complex <double> i(0, 1);
            std::complex <double> Z(0, 0);
            double signK = (k > 0 ? 1 : -1);

            //1 == AC
            //2 == DC
            switch(acMode_) {
                case 1:
75
                    Z = (Z0_ / (2 * Physics::pi * a_)) * 1.0 / (sqrt(Z0_ * std::abs(k) / 2) * sqrt(sigma_ / (1.0 - i * Physics::c * k * tau_)) * (i + signK) / k - (i * k * a_) / 2.0);
gsell's avatar
gsell committed
76 77
                    break;
                case 2:
78
                    Z = (Z0_ / (2 * Physics::pi * a_)) * 1.0 / (sqrt(sigma_ * Z0_ * std::abs(k) / 2) * (i + signK) / k - (i * k * a_) / 2.0);
gsell's avatar
gsell committed
79 80 81 82
                    break;
            }
            switch(direction_) {
                case LONGITUDINAL:
83
                    return real(Z) * cos(k * s_) * 2.0 * Physics::c / Physics::pi;
gsell's avatar
gsell committed
84 85
                    break;
                case TRANSVERSAL:
86
                    return real(Z) * Physics::c / k * cos(k * s_) * 2.0 * Physics::c / Physics::pi;
gsell's avatar
gsell committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
                    break;
            }
            ERRORMSG("We should not be here: " << __FILE__ << " L" << __LINE__ << endl);

            return 0.0;
        }

    private:

        /// impedance
        double Z0_;
        /// radius
        double a_;
        /// material constant
        double sigma_;
        /// distance from the particle
        double s_;
        /// conductivity either 1="AC" or 2="DC"
        int acMode_;
        /// material constant
        double tau_;
        /// direction either 1="Longitudinal" 0= "Transversal"
        int direction_;

    };

    /**
     * @brief   Simpson-Integration from the function f from a to b with N steps
     *
     *
     * @param[in]   f the function to integrate
     * @param[in]   a integrate from a
     * @param[in]   b integrate to b
     * @param[in]   N Number of integration points
     * @return  function value of the integration
     *
     */
    template<class F> double simpson(F &f, double a, double b, unsigned int N) {
        assert(b > a);
        assert(N > 0);

        double result = 0;
        double h = (b - a) / N;

        // boundary values
        result += (f(a) + 4 * f(a + h / 2) + f(b)) / 2.0;

        // values between boundaries
        for(unsigned int i = 1; i < N; ++ i) {
            result += f(a + i * h) + 2 * f(a + (i + 0.5) * h);
        }

        result *= h / 3.0;

        return result;

    }
    /// save the line Density of the particle bunch
    std::vector<double> lineDensity_m;
    /// FFT of the zero padded wakefield
147
    std::vector<double>  FftWField_m;
gsell's avatar
gsell committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

    /// divides the particle bunch in NBin slices
    int NBin_m;
    /// impedance
    double Z0_m;
    /// radius
    double radius_m;
    /// material constant
    double sigma_m;
    /// conductivity either 1="AC" or 2="DC"
    int acMode_m;
    /// material constant
    double tau_m;
    /// direction either 1="Longitudinal" 2= "Transversal"
    int direction_m;
    /// true if the length of the particle bunch is considered as constant
    bool constLength_m;
    /// filename of the wakefield
    std::string filename_m;

    std::vector<Filter *> filters_m;

    void compEnergy(const double K, const double charge, const double *lambda, double *OutEnergy);
    void compEnergy(const double K, const double charge, std::vector<double> lambda, double *OutEnergy);
    void CalcWakeFFT(double spacing);
};
174
#endif //GREENWAKEFUNCTION_HH