FM3DDynamic.cpp 15.6 KB
Newer Older
1 2
#include "Fields/FM3DDynamic.h"
#include "Fields/Fieldmap.hpp"
gsell's avatar
gsell committed
3 4 5

#include "Physics/Physics.h"

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

9 10
extern Inform *gmsg;

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

14
FM3DDynamic::FM3DDynamic(std::string aFilename):
gsell's avatar
gsell committed
15 16 17 18 19 20 21
    Fieldmap(aFilename),
    FieldstrengthEz_m(NULL),
    FieldstrengthEx_m(NULL),
    FieldstrengthEy_m(NULL),
    FieldstrengthHz_m(NULL),
    FieldstrengthHx_m(NULL),
    FieldstrengthHy_m(NULL) {
kraus's avatar
kraus committed
22

23
    std::string tmpString;
gsell's avatar
gsell committed
24 25 26 27 28 29
    double tmpDouble;

    Type = T3DDynamic;
    ifstream file(Filename_m.c_str());

    if(file.good()) {
30
        bool parsing_passed = interpreteLine<std::string, std::string>(file, tmpString, tmpString);
gsell's avatar
gsell committed
31 32 33
        if(tmpString == "XYZ") {
            swap_m = false;
            parsing_passed = parsing_passed &&
34
                             interpreteLine<double>(file, frequency_m);
gsell's avatar
gsell committed
35
            parsing_passed = parsing_passed &&
36
                             interpreteLine<double, double, unsigned int>(file, xbegin_m, xend_m, num_gridpx_m);
gsell's avatar
gsell committed
37
            parsing_passed = parsing_passed &&
38
                             interpreteLine<double, double, unsigned int>(file, ybegin_m, yend_m, num_gridpy_m);
gsell's avatar
gsell committed
39
            parsing_passed = parsing_passed &&
40
                             interpreteLine<double, double, unsigned int>(file, zbegin_m, zend_m, num_gridpz_m);
gsell's avatar
gsell committed
41 42 43 44 45
        } else {
            cerr << "at the moment only the XYZ orientation is supported!\n"
                 << "unknown orientation of 3D dynamic fieldmap" << endl;
            parsing_passed = false;
        }
46
        for(unsigned long i = 0; (i < (num_gridpz_m + 1) * (num_gridpx_m + 1) * (num_gridpy_m + 1)) && parsing_passed; ++ i) {
gsell's avatar
gsell committed
47
            parsing_passed = parsing_passed &&
48 49 50 51 52 53 54
                             interpreteLine<double>(file,
                                                    tmpDouble,
                                                    tmpDouble,
                                                    tmpDouble,
                                                    tmpDouble,
                                                    tmpDouble,
                                                    tmpDouble);
gsell's avatar
gsell committed
55
        }
56

gsell's avatar
gsell committed
57
        parsing_passed = parsing_passed &&
58 59
                         interpreteEOF(file);

gsell's avatar
gsell committed
60
        file.close();
61

gsell's avatar
gsell committed
62 63 64 65 66
        if(!parsing_passed) {
            disableFieldmapWarning();
            zend_m = zbegin_m - 1e-3;
        } else {
            frequency_m *= Physics::two_pi * 1e6;
67

gsell's avatar
gsell committed
68 69 70 71 72 73
            xbegin_m /= 100.0;
            xend_m /= 100.0;
            ybegin_m /= 100.0;
            yend_m /= 100.0;
            zbegin_m /= 100.0;
            zend_m /= 100.0;
74

gsell's avatar
gsell committed
75 76 77 78 79 80 81
            hx_m = (xend_m - xbegin_m) / num_gridpx_m;
            hy_m = (yend_m - ybegin_m) / num_gridpy_m;
            hz_m = (zend_m - zbegin_m) / num_gridpz_m;

            num_gridpx_m++;
            num_gridpy_m++;
            num_gridpz_m++;
82

gsell's avatar
gsell committed
83 84 85 86 87 88 89 90 91 92
        }
    } else {
        noFieldmapWarning();
        zbegin_m = 0.0;
        zend_m = -1e-3;
    }
}


FM3DDynamic::~FM3DDynamic() {
93
    freeMap();
gsell's avatar
gsell committed
94 95 96 97
}

void FM3DDynamic::readMap() {
    if(FieldstrengthEz_m == NULL) {
kraus's avatar
kraus committed
98

gsell's avatar
gsell committed
99 100
        ifstream in(Filename_m.c_str());
        int tmpInt;
101
        std::string tmpString;
gsell's avatar
gsell committed
102 103
        double tmpDouble, Ezmax = 0.0;

104
        interpreteLine<std::string, std::string>(in, tmpString, tmpString);
gsell's avatar
gsell committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        interpreteLine<double>(in, tmpDouble);
        interpreteLine<double, double, int>(in, tmpDouble, tmpDouble, tmpInt);
        interpreteLine<double, double, int>(in, tmpDouble, tmpDouble, tmpInt);
        interpreteLine<double, double, int>(in, tmpDouble, tmpDouble, tmpInt);

        FieldstrengthEz_m = new double[num_gridpz_m * num_gridpx_m * num_gridpy_m];
        FieldstrengthEx_m = new double[num_gridpz_m * num_gridpx_m * num_gridpy_m];
        FieldstrengthEy_m = new double[num_gridpz_m * num_gridpx_m * num_gridpy_m];
        FieldstrengthHz_m = new double[num_gridpz_m * num_gridpx_m * num_gridpy_m];
        FieldstrengthHx_m = new double[num_gridpz_m * num_gridpx_m * num_gridpy_m];
        FieldstrengthHy_m = new double[num_gridpz_m * num_gridpx_m * num_gridpy_m];

        //       if (swap_m)
        //         for (int i = 0; i < num_gridpz_m; i++)
        //           {
        //             for (int j = 0; j < num_gridpr_m; j++)
        //               in >> FieldstrengthEr_m[i + j * num_gridpz_m] >> FieldstrengthEz_m[i + j * num_gridpz_m] >> FieldstrengthHt_m[i + j * num_gridpz_m] >> tmpDouble;
        //             if (fabs(FieldstrengthEz_m[i]) > Ezmax) Ezmax = fabs(FieldstrengthEz_m[i]);
        //           }
        //       else
        //         {
        long ii = 0;
127 128 129
        for(unsigned int i = 0; i < num_gridpx_m; ++ i) {
            for(unsigned int j = 0; j < num_gridpy_m; ++ j) {
                for(unsigned int k = 0; k < num_gridpz_m; ++ k) {
gsell's avatar
gsell committed
130 131
                    interpreteLine<double>(in,
                                           FieldstrengthEz_m[ii],
132 133 134
                                           FieldstrengthEy_m[ii],
                                           FieldstrengthEx_m[ii],
                                           FieldstrengthHz_m[ii],
gsell's avatar
gsell committed
135
                                           FieldstrengthHy_m[ii],
136
                                           FieldstrengthHx_m[ii]);
gsell's avatar
gsell committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
                    ++ ii;
                }
            }
        }
        in.close();

        int index_x = static_cast<int>(ceil(-xbegin_m / hx_m));
        double lever_x = index_x * hx_m + xbegin_m;
        if(lever_x > 0.5) {
            -- index_x;
        }

        int index_y = static_cast<int>(ceil(-ybegin_m / hy_m));
        double lever_y = index_y * hy_m + ybegin_m;
        if(lever_y > 0.5) {
            -- index_y;
        }

155 156 157 158
        ii = (index_y + index_x * num_gridpy_m) * num_gridpz_m;
        for(unsigned int i = 0; i < num_gridpz_m; i++) {
            if(std::abs(FieldstrengthEz_m[ii]) > Ezmax) {
                Ezmax = std::abs(FieldstrengthEz_m[ii]);
gsell's avatar
gsell committed
159
            }
160
            ++ ii;
gsell's avatar
gsell committed
161 162
        }

163
        for(unsigned long i = 0; i < num_gridpx_m * num_gridpy_m * num_gridpz_m; i++) {
gsell's avatar
gsell committed
164

165 166 167 168 169 170
            FieldstrengthEz_m[i] *= 1e6 / Ezmax;
            FieldstrengthEx_m[i] *= 1e6 / Ezmax;
            FieldstrengthEy_m[i] *= 1e6 / Ezmax;
            FieldstrengthHz_m[i] *= Physics::mu_0 / Ezmax;
            FieldstrengthHx_m[i] *= Physics::mu_0 / Ezmax;
            FieldstrengthHy_m[i] *= Physics::mu_0 / Ezmax;
gsell's avatar
gsell committed
171 172
        }

kraus's avatar
kraus committed
173
        INFOMSG(level3 << typeset_msg("read in fieldmap '" + Filename_m  + "'", "info") << "\n"
174
                << endl);
gsell's avatar
gsell committed
175 176 177 178 179 180 181 182 183 184 185
    }
}

void FM3DDynamic::freeMap() {
    if(FieldstrengthEz_m != NULL) {
        delete[] FieldstrengthEz_m;
        delete[] FieldstrengthEx_m;
        delete[] FieldstrengthEy_m;
        delete[] FieldstrengthHz_m;
        delete[] FieldstrengthHx_m;
        delete[] FieldstrengthHy_m;
186 187 188 189 190 191 192 193

        FieldstrengthEz_m = NULL;
        FieldstrengthEx_m = NULL;
        FieldstrengthEy_m = NULL;
        FieldstrengthHz_m = NULL;
        FieldstrengthHx_m = NULL;
        FieldstrengthHy_m = NULL;

kraus's avatar
kraus committed
194
        INFOMSG(level3 << typeset_msg("freed fieldmap '" + Filename_m + "'", "info") << "\n"
195
                << endl);
gsell's avatar
gsell committed
196 197 198 199
    }
}

bool FM3DDynamic::getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const {
200
    const unsigned int index_x = static_cast<int>(floor((R(0) - xbegin_m) / hx_m));
gsell's avatar
gsell committed
201 202
    const double lever_x = (R(0) - xbegin_m) / hx_m - index_x;

203
    const unsigned int index_y = static_cast<int>(floor((R(1) - ybegin_m) / hy_m));
gsell's avatar
gsell committed
204 205
    const double lever_y = (R(1) - ybegin_m) / hy_m - index_y;

206
    const unsigned int index_z = (int)floor((R(2) - zbegin_m) / hz_m);
gsell's avatar
gsell committed
207 208
    const double lever_z = (R(2) - zbegin_m) / hz_m - index_z;

209
    if(index_z >= num_gridpz_m - 2) {
gsell's avatar
gsell committed
210 211 212
        return false;
    }

213
    if(index_x >= num_gridpx_m - 2|| index_y >= num_gridpy_m - 2) {
gsell's avatar
gsell committed
214 215 216
        return true;
    }

217 218 219 220 221 222 223 224 225 226 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
    static unsigned int deltaX = num_gridpy_m * num_gridpz_m;
    static unsigned int deltaY = num_gridpz_m;
    static unsigned int deltaZ = 1;

    const unsigned long index1 = index_x * deltaX + index_y * deltaY + index_z * deltaZ;

    E(0) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEx_m[index1                           ]
        + lever_x           * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEx_m[index1 + deltaX                  ]
        + (1.0 - lever_x)   * lever_y         * (1.0 - lever_z) * FieldstrengthEx_m[index1 +          deltaY         ]
        + lever_x           * lever_y         * (1.0 - lever_z) * FieldstrengthEx_m[index1 + deltaX + deltaY         ]
        + (1.0 - lever_x)   * (1.0 - lever_y) * lever_z         * FieldstrengthEx_m[index1 +                   deltaZ]
        + lever_x           * (1.0 - lever_y) * lever_z         * FieldstrengthEx_m[index1 + deltaX +          deltaZ]
        + (1.0 - lever_x)   * lever_y         * lever_z         * FieldstrengthEx_m[index1 +          deltaY + deltaZ]
        + lever_x           * lever_y         * lever_z         * FieldstrengthEx_m[index1 + deltaX + deltaY + deltaZ];

    E(1) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEy_m[index1                           ]
        + lever_x           * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEy_m[index1 + deltaX                  ]
        + (1.0 - lever_x)   * lever_y         * (1.0 - lever_z) * FieldstrengthEy_m[index1 +          deltaY         ]
        + lever_x           * lever_y         * (1.0 - lever_z) * FieldstrengthEy_m[index1 + deltaX + deltaY         ]
        + (1.0 - lever_x)   * (1.0 - lever_y) * lever_z         * FieldstrengthEy_m[index1 +                   deltaZ]
        + lever_x           * (1.0 - lever_y) * lever_z         * FieldstrengthEy_m[index1 + deltaX +          deltaZ]
        + (1.0 - lever_x)   * lever_y         * lever_z         * FieldstrengthEy_m[index1 +          deltaY + deltaZ]
        + lever_x           * lever_y         * lever_z         * FieldstrengthEy_m[index1 + deltaX + deltaY + deltaZ];

    E(2) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEz_m[index1                           ]
        + lever_x           * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthEz_m[index1 + deltaX                  ]
        + (1.0 - lever_x)   * lever_y         * (1.0 - lever_z) * FieldstrengthEz_m[index1 +          deltaY         ]
        + lever_x           * lever_y         * (1.0 - lever_z) * FieldstrengthEz_m[index1 + deltaX + deltaY         ]
        + (1.0 - lever_x)   * (1.0 - lever_y) * lever_z         * FieldstrengthEz_m[index1 +                   deltaZ]
        + lever_x           * (1.0 - lever_y) * lever_z         * FieldstrengthEz_m[index1 + deltaX +          deltaZ]
        + (1.0 - lever_x)   * lever_y         * lever_z         * FieldstrengthEz_m[index1 +          deltaY + deltaZ]
        + lever_x           * lever_y         * lever_z         * FieldstrengthEz_m[index1 + deltaX + deltaY + deltaZ];

    B(0) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHx_m[index1                           ]
        + lever_x           * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHx_m[index1 + deltaX                  ]
        + (1.0 - lever_x)   * lever_y         * (1.0 - lever_z) * FieldstrengthHx_m[index1 +          deltaY         ]
        + lever_x           * lever_y         * (1.0 - lever_z) * FieldstrengthHx_m[index1 + deltaX + deltaY         ]
        + (1.0 - lever_x)   * (1.0 - lever_y) * lever_z         * FieldstrengthHx_m[index1 +                   deltaZ]
        + lever_x           * (1.0 - lever_y) * lever_z         * FieldstrengthHx_m[index1 + deltaX +          deltaZ]
        + (1.0 - lever_x)   * lever_y         * lever_z         * FieldstrengthHx_m[index1 +          deltaY + deltaZ]
        + lever_x           * lever_y         * lever_z         * FieldstrengthHx_m[index1 + deltaX + deltaY + deltaZ];

    B(1) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHy_m[index1                           ]
        + lever_x           * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHy_m[index1 + deltaX                  ]
        + (1.0 - lever_x)   * lever_y         * (1.0 - lever_z) * FieldstrengthHy_m[index1 +          deltaY         ]
        + lever_x           * lever_y         * (1.0 - lever_z) * FieldstrengthHy_m[index1 + deltaX + deltaY         ]
        + (1.0 - lever_x)   * (1.0 - lever_y) * lever_z         * FieldstrengthHy_m[index1 +                   deltaZ]
        + lever_x           * (1.0 - lever_y) * lever_z         * FieldstrengthHy_m[index1 + deltaX +          deltaZ]
        + (1.0 - lever_x)   * lever_y         * lever_z         * FieldstrengthHy_m[index1 +          deltaY + deltaZ]
        + lever_x           * lever_y         * lever_z         * FieldstrengthHy_m[index1 + deltaX + deltaY + deltaZ];

    B(2) += (1.0 - lever_x) * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHz_m[index1                           ]
        + lever_x           * (1.0 - lever_y) * (1.0 - lever_z) * FieldstrengthHz_m[index1 + deltaX                  ]
        + (1.0 - lever_x)   * lever_y         * (1.0 - lever_z) * FieldstrengthHz_m[index1 +          deltaY         ]
        + lever_x           * lever_y         * (1.0 - lever_z) * FieldstrengthHz_m[index1 + deltaX + deltaY         ]
        + (1.0 - lever_x)   * (1.0 - lever_y) * lever_z         * FieldstrengthHz_m[index1 +                   deltaZ]
        + lever_x           * (1.0 - lever_y) * lever_z         * FieldstrengthHz_m[index1 + deltaX +          deltaZ]
        + (1.0 - lever_x)   * lever_y         * lever_z         * FieldstrengthHz_m[index1 +          deltaY + deltaZ]
        + lever_x           * lever_y         * lever_z         * FieldstrengthHz_m[index1 + deltaX + deltaY + deltaZ];
gsell's avatar
gsell committed
276 277 278 279

    return false;
}

280
bool FM3DDynamic::getFieldDerivative(const Vector_t &R, Vector_t &E, Vector_t &B, const DiffDirection &dir) const {
gsell's avatar
gsell committed
281 282 283 284 285 286 287 288 289
    return false;
}

void FM3DDynamic::getFieldDimensions(double &zBegin, double &zEnd, double &rBegin, double &rEnd) const {
    zBegin = zbegin_m;
    zEnd = zend_m;
    rBegin = xbegin_m;
    rEnd = xend_m;
}
290
void FM3DDynamic::getFieldDimensions(double &xIni, double &xFinal, double &yIni, double &yFinal, double &zIni, double &zFinal) const {}
gsell's avatar
gsell committed
291 292 293 294

void FM3DDynamic::swap() {}

void FM3DDynamic::getInfo(Inform *msg) {
295
    (*msg) << Filename_m << " (3D dynamic) "
gsell's avatar
gsell committed
296 297 298 299 300 301 302 303 304 305 306
           << " xini= " << xbegin_m << " xfinal= " << xend_m
           << " yini= " << ybegin_m << " yfinal= " << yend_m
           << " zini= " << zbegin_m << " zfinal= " << zend_m << " (m) " << endl;
}

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

void FM3DDynamic::setFrequency(double freq) {
    frequency_m = freq;
kraus's avatar
kraus committed
307
}
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

void FM3DDynamic::getOnaxisEz(std::vector<std::pair<double, double> > & F) {
    F.resize(num_gridpz_m);

    int index_x = static_cast<int>(ceil(-xbegin_m / hx_m));
    double lever_x = index_x * hx_m + xbegin_m;
    if(lever_x > 0.5) {
        -- index_x;
    }

    int index_y = static_cast<int>(ceil(-ybegin_m / hy_m));
    double lever_y = index_y * hy_m + ybegin_m;
    if(lever_y > 0.5) {
        -- index_y;
    }

    std::ofstream out("data/" + Filename_m);
    unsigned int ii = (index_y + index_x * num_gridpy_m) * num_gridpz_m;
    for(unsigned int i = 0; i < num_gridpz_m; ++ i) {
        F[i].first = hz_m * i;
        F[i].second = FieldstrengthEz_m[ii ++] / 1e6;


        Vector_t R(0,0,zbegin_m + F[i].first), B(0.0), E(0.0);
        getFieldstrength(R, E, B);
        out << std::setw(16) << std::setprecision(8) << F[i].first
            << std::setw(16) << std::setprecision(8) << E(0)
            << std::setw(16) << std::setprecision(8) << E(1)
            << std::setw(16) << std::setprecision(8) << E(2)
            << std::setw(16) << std::setprecision(8) << B(0)
            << std::setw(16) << std::setprecision(8) << B(1)
            << std::setw(16) << std::setprecision(8) << B(2) << "\n";
    }
    out.close();
}