Astra1DElectroStatic.cpp 7.05 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7
#include "Fields/Astra1DElectroStatic.hh"
#include "Fields/Fieldmap.icc"
#include "Physics/Physics.h"
#include "gsl/gsl_interp.h"
#include "gsl/gsl_spline.h"
#include "gsl/gsl_fft_real.h"

kraus's avatar
kraus committed
8 9 10
#include <fstream>
#include <ios>

gsell's avatar
gsell committed
11 12 13 14 15
using namespace std;
using Physics::mu_0;
using Physics::c;
using Physics::two_pi;

16
Astra1DElectroStatic::Astra1DElectroStatic(std::string aFilename)
gsell's avatar
gsell committed
17 18 19 20 21
    : Fieldmap(aFilename),
      FourCoefs_m(NULL) {

    ifstream file;
    int skippedValues = 0;
22
    std::string tmpString;
gsell's avatar
gsell committed
23 24 25 26 27 28 29 30
    double tmpDouble;
    double tmpDouble2;

    Type = TAstraElectroStatic;

    // open field map, parse it and disable element on error
    file.open(Filename_m.c_str());
    if(file.good()) {
31
        bool parsing_passed = interpreteLine<std::string, int>(file, tmpString, accuracy_m);
gsell's avatar
gsell committed
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
        parsing_passed = parsing_passed &&
                         interpreteLine<double, double>(file, zbegin_m, tmpDouble);

        tmpDouble2 = zbegin_m;
        while(!file.eof() && parsing_passed) {
            parsing_passed = interpreteLine<double, double>(file, zend_m, tmpDouble, false);
            if(zend_m - tmpDouble2 > 1e-10) {
                tmpDouble2 = zend_m;
            } else if(parsing_passed) {
                ++ skippedValues;
            }
        }

        num_gridpz_m = lines_read_m - 2 - skippedValues;
        lines_read_m = 0;

        if(!parsing_passed && !file.eof()) {
            disableFieldmapWarning();
            zend_m = zbegin_m - 1e-3;
        }
        length_m = 2.0 * num_gridpz_m * (zend_m - zbegin_m) / (num_gridpz_m - 1);
        file.close();
    } else {
        noFieldmapWarning();
        zbegin_m = 0.0;
        zend_m = -1e-3;
    }
}

Astra1DElectroStatic::~Astra1DElectroStatic() {
    if(FourCoefs_m != NULL) {
        delete[] FourCoefs_m;
    }
}

void Astra1DElectroStatic::readMap() {
    if(FourCoefs_m == NULL) {
        // declare variables and allocate memory

        ifstream in;

        bool parsing_passed = true;

75
        std::string tmpString;
gsell's avatar
gsell committed
76 77 78 79 80

        double Ez_max = 0.0;
        double dz = (zend_m - zbegin_m) / (num_gridpz_m - 1);
        double tmpDouble = zbegin_m - dz;

81
        double *RealValues = new double[2 * num_gridpz_m];
gsell's avatar
gsell committed
82 83 84 85 86 87 88 89
        double *zvals = new double[num_gridpz_m];

        gsl_spline *Ez_interpolant = gsl_spline_alloc(gsl_interp_cspline, num_gridpz_m);
        gsl_interp_accel *Ez_accel = gsl_interp_accel_alloc();

        gsl_fft_real_wavetable *real = gsl_fft_real_wavetable_alloc(2 * num_gridpz_m);
        gsl_fft_real_workspace *work = gsl_fft_real_workspace_alloc(2 * num_gridpz_m);

90
        FourCoefs_m = new double[2 * accuracy_m - 1];
gsell's avatar
gsell committed
91 92 93

        // read in and parse field map
        in.open(Filename_m.c_str());
94
        interpreteLine<std::string, int>(in, tmpString, accuracy_m);
gsell's avatar
gsell committed
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 147 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 174 175 176 177 178 179

        for(int i = 0; i < num_gridpz_m && parsing_passed; /* skip increment of i here */) {
            parsing_passed = interpreteLine<double, double>(in, zvals[i], RealValues[i]);
            // the sequence of z-position should be strictly increasing
            // drop sampling points that don't comply to this
            if(zvals[i] - tmpDouble > 1e-10) {
                if(fabs(RealValues[i]) > Ez_max) {
                    Ez_max = fabs(RealValues[i]);
                }
                tmpDouble = zvals[i];
                ++ i; // increment i only if sampling point is accepted
            }
        }
        in.close();

        gsl_spline_init(Ez_interpolant, zvals, RealValues, num_gridpz_m);

        // get equidistant sampling from the, possibly, non-equidistant sampling
        // using cubic spline for this
        int ii = num_gridpz_m;
        for(int i = 0; i < num_gridpz_m - 1; ++ i, ++ ii) {
            double z = zbegin_m + dz * i;
            RealValues[ii] = gsl_spline_eval(Ez_interpolant, z, Ez_accel);
        }
        RealValues[ii ++] = RealValues[num_gridpz_m - 1];
        // prepend mirror sampling points such that field values are periodic for sure
        -- ii; // ii == 2*num_gridpz_m at the moment
        for(int i = 0; i < num_gridpz_m; ++ i, -- ii) {
            RealValues[i] = RealValues[ii];
        }


        num_gridpz_m *= 2; // we doubled the sampling points

        gsl_fft_real_transform(RealValues, 1, num_gridpz_m, real, work);

        // normalize to Ez_max = 1 MV/m
        FourCoefs_m[0] = 1e6 * RealValues[0] / (Ez_max * num_gridpz_m); // factor 1e6 due to conversion MV/m to V/m
        for(int i = 1; i < 2 * accuracy_m - 1; i++) {
            FourCoefs_m[i] = 1e6 * 2.* RealValues[i] / (Ez_max * num_gridpz_m);
        }

        gsl_fft_real_workspace_free(work);
        gsl_fft_real_wavetable_free(real);

        gsl_spline_free(Ez_interpolant);
        gsl_interp_accel_free(Ez_accel);

        delete[] zvals;
        delete[] RealValues;

        INFOMSG(typeset_msg("read in fieldmap '" + Filename_m  + "'", "info") << endl);

    }
}

void Astra1DElectroStatic::freeMap() {
    if(FourCoefs_m != NULL) {

        delete[] FourCoefs_m;

        INFOMSG(typeset_msg("freed fieldmap '" + Filename_m  + "'", "info") << endl);
    }
}

bool Astra1DElectroStatic::getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const {
    // do fourier interpolation in z-direction
    const double RR2 = R(0) * R(0) + R(1) * R(1);

    const double kz = two_pi * (R(2) - length_m) / length_m;

    double ez = FourCoefs_m[0];
    double ezp = 0.0;
    double ezpp = 0.0;
    double ezppp = 0.0;
    double somefactor_base, somefactor;
    double coskzl;
    double sinkzl;

    int n = 1;
    for(int l = 1; l < accuracy_m ; l++, n += 2) {
        somefactor_base =  two_pi / length_m * l;  // = \frac{d(kz*l)}{dz}
        somefactor = 1.0;
        coskzl = cos(kz * l);
        sinkzl = sin(kz * l);
180
        ez    += (FourCoefs_m[n] * coskzl - FourCoefs_m[n + 1] * sinkzl);
gsell's avatar
gsell committed
181
        somefactor *= somefactor_base;
182
        ezp   += somefactor * (-FourCoefs_m[n] * sinkzl - FourCoefs_m[n + 1] * coskzl);
gsell's avatar
gsell committed
183
        somefactor *= somefactor_base;
184
        ezpp  += somefactor * (-FourCoefs_m[n] * coskzl + FourCoefs_m[n + 1] * sinkzl);
gsell's avatar
gsell committed
185
        somefactor *= somefactor_base;
186
        ezppp += somefactor * (FourCoefs_m[n] * sinkzl + FourCoefs_m[n + 1] * coskzl);
gsell's avatar
gsell committed
187 188 189 190 191 192 193 194 195 196 197
    }

    // expand the field to off-axis
    const double EfieldR = -ezp / 2. + ezppp / 16. * RR2;

    E(0) += EfieldR * R(0);
    E(1) += EfieldR * R(1);
    E(2) += ez - ezpp * RR2 / 4.;
    return false;
}

198
bool Astra1DElectroStatic::getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const {
gsell's avatar
gsell committed
199 200 201 202 203 204 205 206
    return false;
}

void Astra1DElectroStatic::getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const {
    zBegin = zbegin_m;
    zEnd = zend_m;
}

207 208
void Astra1DElectroStatic::getFieldDimensions(double &xIni, double &xFinal, double &yIni, double &yFinal, double &zIni, double &zFinal) const {}

gsell's avatar
gsell committed
209 210 211 212 213 214 215 216 217 218 219 220 221
void Astra1DElectroStatic::swap()
{ }

void Astra1DElectroStatic::getInfo(Inform *msg) {
    (*msg) << Filename_m << " (1D electrostatic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
}

double Astra1DElectroStatic::getFrequency() const {
    return 0.0;
}

void Astra1DElectroStatic::setFrequency(double freq)
{ }