FM2DDynamic.cpp 8.61 KB
Newer Older
1 2
#include "Fields/FM2DDynamic.h"
#include "Fields/Fieldmap.hpp"
gsell's avatar
gsell committed
3 4
#include "Physics/Physics.h"

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

8 9 10
extern Inform *gmsg;

// using namespace std;
gsell's avatar
gsell committed
11 12
using Physics::mu_0;

13
FM2DDynamic::FM2DDynamic(std::string aFilename)
gsell's avatar
gsell committed
14 15 16 17
    : Fieldmap(aFilename),
      FieldstrengthEz_m(NULL),
      FieldstrengthEr_m(NULL),
      FieldstrengthHt_m(NULL) {
18
    std::ifstream file;
19
    std::string tmpString;
gsell's avatar
gsell committed
20 21 22 23 24 25 26
    double tmpDouble;

    Type = T2DDynamic;

    // open field map, parse it and disable element on error
    file.open(Filename_m.c_str());
    if(file.good()) {
27
        bool parsing_passed = interpreteLine<std::string, std::string>(file, tmpString, tmpString);
gsell's avatar
gsell committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        if(tmpString == "ZX") {
            swap_m = true;
            parsing_passed = parsing_passed &&
                             interpreteLine<double, double, int>(file, rbegin_m, rend_m, num_gridpr_m);
            parsing_passed = parsing_passed &&
                             interpreteLine<double>(file, frequency_m);
            parsing_passed = parsing_passed &&
                             interpreteLine<double, double, int>(file, zbegin_m, zend_m, num_gridpz_m);
        } else if(tmpString == "XZ") {
            swap_m = false;
            parsing_passed = parsing_passed &&
                             interpreteLine<double, double, int>(file, zbegin_m, zend_m, num_gridpz_m);
            parsing_passed = parsing_passed &&
                             interpreteLine<double>(file, frequency_m);
            parsing_passed = parsing_passed &&
                             interpreteLine<double, double, int>(file, rbegin_m, rend_m, num_gridpr_m);
        } else {
45
            std::cerr << "unknown orientation of 2D dynamic fieldmap" << std::endl;
gsell's avatar
gsell committed
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
            parsing_passed = false;
            zbegin_m = 0.0;
            zend_m = -1e-3;
        }

        for(long i = 0; (i < (num_gridpz_m + 1) * (num_gridpr_m + 1)) && parsing_passed; ++ i) {
            parsing_passed = parsing_passed && interpreteLine<double, double, double, double>(file, tmpDouble, tmpDouble, tmpDouble, tmpDouble);
        }

        parsing_passed = parsing_passed &&
                         interpreteEOF(file);

        file.close();
        lines_read_m = 0;

        if(!parsing_passed) {
            disableFieldmapWarning();
            zend_m = zbegin_m - 1e-3;
        } else {
            // convert MHz to Hz and frequency to angular frequency
            frequency_m *= Physics::two_pi * 1e6;

            // convert cm to m
            rbegin_m /= 100.0;
            rend_m /= 100.0;
            zbegin_m /= 100.0;
            zend_m /= 100.0;

            hr_m = (rend_m - rbegin_m) / num_gridpr_m;
            hz_m = (zend_m - zbegin_m) / num_gridpz_m;

            // num spacings -> num grid points
            num_gridpr_m++;
            num_gridpz_m++;
        }
    } else {
        noFieldmapWarning();
        zbegin_m = 0.0;
        zend_m = -1e-3;
    }
}


FM2DDynamic::~FM2DDynamic() {
90
    freeMap();
gsell's avatar
gsell committed
91 92 93 94 95
}

void FM2DDynamic::readMap() {
    if(FieldstrengthEz_m == NULL) {
        // declare variables and allocate memory
96
        std::ifstream in;
gsell's avatar
gsell committed
97
        int tmpInt;
98
        std::string tmpString;
gsell's avatar
gsell committed
99 100 101 102 103 104 105 106
        double tmpDouble, Ezmax = 0.0;

        FieldstrengthEz_m = new double[num_gridpz_m * num_gridpr_m];
        FieldstrengthEr_m = new double[num_gridpz_m * num_gridpr_m];
        FieldstrengthHt_m = new double[num_gridpz_m * num_gridpr_m];

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

        if(swap_m) {
            for(int i = 0; i < num_gridpz_m; i++) {
                for(int j = 0; j < num_gridpr_m; j++) {
                    interpreteLine<double, double, double, double>(in,
                            FieldstrengthEr_m[i + j * num_gridpz_m],
                            FieldstrengthEz_m[i + j * num_gridpz_m],
                            FieldstrengthHt_m[i + j * num_gridpz_m],
                            tmpDouble);
                }
            }
        } else {
            for(int j = 0; j < num_gridpr_m; j++) {
                for(int i = 0; i < num_gridpz_m; i++) {
                    interpreteLine<double, double, double, double>(in,
                            FieldstrengthEz_m[i + j * num_gridpz_m],
                            FieldstrengthEr_m[i + j * num_gridpz_m],
                            tmpDouble,
                            FieldstrengthHt_m[i + j * num_gridpz_m]);
                }
            }
        }

        in.close();
135 136 137


        // find maximum field
138 139 140
        for(int i = 0; i < num_gridpz_m; ++ i) {
            if(std::abs(FieldstrengthEz_m[i]) > Ezmax) {
                Ezmax = std::abs(FieldstrengthEz_m[i]);
141
            }
gsell's avatar
gsell committed
142 143 144 145 146 147 148 149
        }

        for(int i = 0; i < num_gridpr_m * num_gridpz_m; i++) {
            FieldstrengthEz_m[i] *= 1.e6 / Ezmax; // conversion MV/m to V/m and normalization
            FieldstrengthEr_m[i] *= 1.e6 / Ezmax;
            FieldstrengthHt_m[i] *= mu_0 / Ezmax; // H -> B
        }

kraus's avatar
kraus committed
150
        INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m  + "'", "info") << "\n"
151
                << endl);
gsell's avatar
gsell committed
152 153 154 155 156 157 158

    }
}

void FM2DDynamic::freeMap() {
    if(FieldstrengthEz_m != NULL) {
        delete[] FieldstrengthEz_m;
159
        FieldstrengthEz_m = NULL;
gsell's avatar
gsell committed
160
        delete[] FieldstrengthEr_m;
161
        FieldstrengthEr_m = NULL;
gsell's avatar
gsell committed
162
        delete[] FieldstrengthHt_m;
163
        FieldstrengthHt_m = NULL;
gsell's avatar
gsell committed
164

kraus's avatar
kraus committed
165
        INFOMSG(level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << "\n"
166
                << endl);
gsell's avatar
gsell committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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
    }
}

bool FM2DDynamic::getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const {
    // do bi-linear interpolation
    const double RR = sqrt(R(0) * R(0) + R(1) * R(1));

    const int indexr = (int)floor(RR / hr_m);
    const double leverr = RR / hr_m - indexr;

    const int indexz = (int)floor(R(2) / hz_m);
    const double leverz = R(2) / hz_m - indexz;

    if((indexz < 0) || (indexz + 2 > num_gridpz_m))
        return false;
    if(indexr + 2 > num_gridpr_m)
        return true;

    const int index1 = indexz + indexr * num_gridpz_m;
    const int index2 = index1 + num_gridpz_m;

    double EfieldR = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEr_m[index1]
                     + leverz         * (1.0 - leverr) * FieldstrengthEr_m[index1 + 1]
                     + (1.0 - leverz) * leverr         * FieldstrengthEr_m[index2]
                     + leverz         * leverr         * FieldstrengthEr_m[index2 + 1];

    double EfieldZ = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthEz_m[index1]
                     + leverz         * (1.0 - leverr) * FieldstrengthEz_m[index1 + 1]
                     + (1.0 - leverz) * leverr         * FieldstrengthEz_m[index2]
                     + leverz         * leverr         * FieldstrengthEz_m[index2 + 1];

    double HfieldT = (1.0 - leverz) * (1.0 - leverr) * FieldstrengthHt_m[index1]
                     + leverz         * (1.0 - leverr) * FieldstrengthHt_m[index1 + 1]
                     + (1.0 - leverz) * leverr         * FieldstrengthHt_m[index2]
                     + leverz         * leverr         * FieldstrengthHt_m[index2 + 1];

    if(RR > 1e-10) {
        E(0) += EfieldR * R(0) / RR;
        E(1) += EfieldR * R(1) / RR;
        B(0) -= HfieldT * R(1) / RR;
        B(1) += HfieldT * R(0) / RR;
    }
    E(2) += EfieldZ;

    return false;
}

214
bool FM2DDynamic::getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const {
gsell's avatar
gsell committed
215 216 217 218 219 220 221 222 223
    return false;
}

void FM2DDynamic::getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const {
    zBegin = zbegin_m;
    zEnd = zend_m;
    rBegin = rbegin_m;
    rEnd = rend_m;
}
224
void FM2DDynamic::getFieldDimensions(double &xIni, double &xFinal, double &yIni, double &yFinal, double &zIni, double &zFinal) const {}
gsell's avatar
gsell committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

void FM2DDynamic::swap() {
    if(swap_m) swap_m = false;
    else swap_m = true;
}

void FM2DDynamic::getInfo(Inform *msg) {
    (*msg) << Filename_m << " (2D dynamic); zini= " << zbegin_m << " m; zfinal= " << zend_m << " m;" << endl;
}

double FM2DDynamic::getFrequency() const {
    return frequency_m;
}

void FM2DDynamic::setFrequency(double freq) {
    frequency_m = freq;
}

243
void FM2DDynamic::getOnaxisEz(std::vector<std::pair<double, double> > & F) {
gsell's avatar
gsell committed
244 245 246 247 248 249 250 251
    double dz = (zend_m - zbegin_m) / (num_gridpz_m - 1);
    F.resize(num_gridpz_m);

    for(int i = 0; i < num_gridpz_m; ++ i) {
        F[i].first = dz * i;
        F[i].second = FieldstrengthEz_m[i] / 1e6;

    }
252
}