Astra1DMagnetoStatic_fast.cpp 8.64 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5
#include "Fields/Astra1DMagnetoStatic_fast.hh"
#include "Fields/Fieldmap.icc"
#include "Physics/Physics.h"
#include "gsl/gsl_fft_real.h"

kraus's avatar
kraus committed
6 7 8
#include <fstream>
#include <ios>

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

14
Astra1DMagnetoStatic_fast::Astra1DMagnetoStatic_fast(std::string aFilename):
15
    Fieldmap(aFilename) {
gsell's avatar
gsell committed
16 17 18
    ifstream file;
    int tmpInt;
    int skippedValues = 0;
19
    std::string tmpString;
gsell's avatar
gsell committed
20 21 22 23 24 25 26 27 28 29
    double tmpDouble;
    double tmpDouble2;

    onAxisField_m = NULL;

    Type = TAstraMagnetoStatic;

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

        file.close();
        num_gridpz_m = lines_read_m - 2 - skippedValues;
        lines_read_m = 0;

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

Astra1DMagnetoStatic_fast::~Astra1DMagnetoStatic_fast() {
    if(onAxisField_m != NULL) {
        for(int i = 0; i < 4; ++i) {
            gsl_spline_free(onAxisInterpolants_m[i]);
            gsl_interp_accel_free(onAxisAccel_m[i]);
        }
        delete[] onAxisField_m;
    }
}

void Astra1DMagnetoStatic_fast::readMap() {
    if(onAxisField_m == NULL) {
        // declare variables and allocate memory
        ifstream in;

        bool parsing_passed = true;
        int accuracy;
        int nsp;
        int ii;

81
        std::string tmpString;
gsell's avatar
gsell committed
82 83 84 85 86 87 88 89 90

        double Bz_max = 0.0;
        double *higherDerivatives[3];
        double *zvals[2];
        double tmpDouble = zbegin_m - hz_m;
        // double z = 0.0;
        double interior_derivative, base;
        double coskzl, sinkzl;

91
        double *RealValues = new double[2 * num_gridpz_m];
gsell's avatar
gsell committed
92 93 94 95 96
        onAxisField_m = new double[num_gridpz_m];
        zvals[0] = new double[num_gridpz_m];

        // read in and parse field map
        in.open(Filename_m.c_str());
97
        interpreteLine<std::string, int>(in, tmpString, accuracy);
gsell's avatar
gsell committed
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

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

        higherDerivatives[0] = new double[num_gridpz_m];
        higherDerivatives[1] = new double[num_gridpz_m];
        higherDerivatives[2] = new double[num_gridpz_m];
        zvals[1] = new double[num_gridpz_m];

        for(int i = 0; i < 4; ++i) {
            onAxisInterpolants_m[i] = gsl_spline_alloc(gsl_interp_cspline, num_gridpz_m);
            onAxisAccel_m[i] = 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);

        ii = num_gridpz_m - 1;
        for(int i = 0; i < num_gridpz_m; ++ i, -- ii) {
            onAxisField_m[i] = RealValues[i] / Bz_max;
            zvals[0][ii] -= zvals[0][0];
        }
        gsl_spline_init(onAxisInterpolants_m[0], zvals[0], onAxisField_m, num_gridpz_m);

        // get equidistant sampling from the, possibly, non-equidistant sampling
        // using cubic spline for this
        ii = num_gridpz_m;
        double z = 0.0;

        for(int i = 0; i < num_gridpz_m - 1; ++ i, ++ ii, z += hz_m) {
            zvals[1][i] = z;
            RealValues[ii] = gsl_spline_eval(onAxisInterpolants_m[0], z, onAxisAccel_m[0]);
        }
        zvals[1][num_gridpz_m - 1] = zvals[0][num_gridpz_m - 1];
        RealValues[ii ++] = onAxisField_m[num_gridpz_m - 1];
        // prepend mirror sampling points such that field values are periodic for sure
        // ii == 2*num_gridpz_m at the moment
148
        -- ii;
gsell's avatar
gsell committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        for(int i = 0; i < num_gridpz_m; ++ i, -- ii) {
            RealValues[i] = RealValues[ii];
        }

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

        // normalize to Bz_max = 1 A/m
        RealValues[0] /= (2. * num_gridpz_m);
        for(int i = 1; i < 2 * accuracy - 1; i++) {
            RealValues[i] /= num_gridpz_m;
        }

        for(int i = 0; i < num_gridpz_m; ++ i) {
            const double kz = two_pi * (zvals[1][i] / length_m + 0.5);
            higherDerivatives[0][i] = 0.0;
            higherDerivatives[1][i] = 0.0;
            higherDerivatives[2][i] = 0.0;
            int n = 1;
            for(int l = 1; l < accuracy; ++l, n += 2) {
                base = two_pi / length_m * l;
                interior_derivative = base;
                coskzl = cos(kz * l);
                sinkzl = sin(kz * l);
172 173

                higherDerivatives[0][i] += interior_derivative * (-RealValues[n] * sinkzl - RealValues[n + 1] * coskzl);
gsell's avatar
gsell committed
174
                interior_derivative *= base;
175
                higherDerivatives[1][i] += interior_derivative * (-RealValues[n] * coskzl + RealValues[n + 1] * sinkzl);
gsell's avatar
gsell committed
176
                interior_derivative *= base;
177
                higherDerivatives[2][i] += interior_derivative * (RealValues[n] * sinkzl + RealValues[n + 1] * coskzl);
gsell's avatar
gsell committed
178 179 180 181 182 183 184
            }
        }

        gsl_fft_real_workspace_free(work);
        gsl_fft_real_wavetable_free(real);

        for(int i = 1; i < 4; ++i) {
185
            gsl_spline_init(onAxisInterpolants_m[i], zvals[1], higherDerivatives[i - 1], num_gridpz_m);
gsell's avatar
gsell committed
186 187 188 189 190 191 192 193 194 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 224 225 226 227 228
        }

        delete[] RealValues;
        delete[] higherDerivatives[0];
        delete[] higherDerivatives[1];
        delete[] higherDerivatives[2];
        delete[] zvals[0];
        delete[] zvals[1];

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

void Astra1DMagnetoStatic_fast::freeMap() {
    if(onAxisField_m != NULL) {
        for(int i = 0; i < 4; ++i) {
            gsl_spline_free(onAxisInterpolants_m[i]);
            gsl_interp_accel_free(onAxisAccel_m[i]);
        }
        delete[] onAxisField_m;

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

bool Astra1DMagnetoStatic_fast::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);

    double bz = gsl_spline_eval(onAxisInterpolants_m[0], R(2), onAxisAccel_m[0]);
    double bzp = gsl_spline_eval(onAxisInterpolants_m[1], R(2), onAxisAccel_m[1]);
    double bzpp = gsl_spline_eval(onAxisInterpolants_m[2], R(2), onAxisAccel_m[2]);
    double bzppp = gsl_spline_eval(onAxisInterpolants_m[3], R(2), onAxisAccel_m[3]);

    // expand to off-axis
    const double BfieldR = -bzp / 2. + bzppp / 16. * RR2;

    B(0) += BfieldR * R(0);
    B(1) += BfieldR * R(1);
    B(2) += bz - bzpp * RR2 / 4.;
    return false;
}

229
bool Astra1DMagnetoStatic_fast::getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const {
gsell's avatar
gsell committed
230 231 232 233 234 235 236 237
    return false;
}

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

238 239
void Astra1DMagnetoStatic_fast::getFieldDimensions(double &xIni, double &xFinal, double &yIni, double &yFinal, double &zIni, double &zFinal) const {}

gsell's avatar
gsell committed
240 241 242 243 244 245 246 247 248 249 250 251 252
void Astra1DMagnetoStatic_fast::swap()
{ }

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

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

void Astra1DMagnetoStatic_fast::setFrequency(double freq)
{ }