FM1DMagnetoStatic_fast.cpp 12.5 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4
#include "Fields/FM1DMagnetoStatic_fast.hh"
#include "Fields/Fieldmap.icc"
#include "Physics/Physics.h"

5
#include "gsl/gsl_fft_real.h"
gsell's avatar
gsell committed
6

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

10
FM1DMagnetoStatic_fast::FM1DMagnetoStatic_fast(std::string aFilename):
gsell's avatar
gsell committed
11 12
    Fieldmap(aFilename) {

13
    Type = T1DMagnetoStatic;
gsell's avatar
gsell committed
14 15
    onAxisField_m = NULL;

16 17
    std::ifstream fieldFile(Filename_m.c_str());
    if(fieldFile.good()) {
gsell's avatar
gsell committed
18

19 20 21
        bool parsingPassed = readFileHeader(fieldFile);
        parsingPassed = checkFileData(fieldFile, parsingPassed);
        fieldFile.close();
gsell's avatar
gsell committed
22

23 24 25 26 27
        if(!parsingPassed) {
            disableFieldmapWarning();
            zEnd_m = zBegin_m - 1.0e-3;
        } else
            convertHeaderData();
gsell's avatar
gsell committed
28

29 30
        deltaZ_m = (zEnd_m - zBegin_m) / (numberOfGridPoints_m - 1);
        length_m = 2.0 * numberOfGridPoints_m * deltaZ_m;
gsell's avatar
gsell committed
31 32 33

    } else {
        noFieldmapWarning();
34 35
        zBegin_m = 0.0;
        zEnd_m = -1.0e-3;
gsell's avatar
gsell committed
36 37 38 39 40
    }
}

FM1DMagnetoStatic_fast::~FM1DMagnetoStatic_fast() {
    if(onAxisField_m != NULL) {
41 42 43 44 45 46 47 48 49
        delete [] onAxisField_m;
        gsl_spline_free(onAxisFieldInterpolants_m);
        gsl_spline_free(onAxisFieldPInterpolants_m);
        gsl_spline_free(onAxisFieldPPInterpolants_m);
        gsl_spline_free(onAxisFieldPPPInterpolants_m);
        gsl_interp_accel_free(onAxisFieldAccel_m);
        gsl_interp_accel_free(onAxisFieldPAccel_m);
        gsl_interp_accel_free(onAxisFieldPPAccel_m);
        gsl_interp_accel_free(onAxisFieldPPPAccel_m);
gsell's avatar
gsell committed
50 51 52 53 54
    }
}

void FM1DMagnetoStatic_fast::readMap() {

55
    if(onAxisField_m == NULL) {
gsell's avatar
gsell committed
56

57
        std::ifstream fieldFile(Filename_m.c_str());
58
        int accuracy = stripFileHeader(fieldFile);
gsell's avatar
gsell committed
59

60 61 62
        onAxisField_m = new double[numberOfGridPoints_m];
        double maxBz = readFileData(fieldFile, onAxisField_m);
        fieldFile.close();
gsell's avatar
gsell committed
63

64 65 66 67
        std::vector<double> fourierCoefs
        = computeFourierCoefficients(accuracy,
                                     onAxisField_m);
        normalizeField(maxBz, fourierCoefs);
gsell's avatar
gsell committed
68

69 70 71 72 73 74 75
        double *onAxisFieldP = new double[numberOfGridPoints_m];
        double *onAxisFieldPP = new double[numberOfGridPoints_m];
        double *onAxisFieldPPP = new double[numberOfGridPoints_m];
        computeFieldDerivatives(accuracy, fourierCoefs, onAxisFieldP,
                                onAxisFieldPP, onAxisFieldPPP);
        computeInterpolationVectors(onAxisFieldP, onAxisFieldPP,
                                    onAxisFieldPPP);
gsell's avatar
gsell committed
76

77 78 79
        delete [] onAxisFieldP;
        delete [] onAxisFieldPP;
        delete [] onAxisFieldPPP;
gsell's avatar
gsell committed
80

81 82
        INFOMSG(typeset_msg("read in fieldmap '" + Filename_m  + "'", "info")
                << endl);
gsell's avatar
gsell committed
83 84 85 86 87
    }
}

void FM1DMagnetoStatic_fast::freeMap() {
    if(onAxisField_m != NULL) {
88 89 90 91 92 93 94 95 96 97 98 99
        delete [] onAxisField_m;
        gsl_spline_free(onAxisFieldInterpolants_m);
        gsl_spline_free(onAxisFieldPInterpolants_m);
        gsl_spline_free(onAxisFieldPPInterpolants_m);
        gsl_spline_free(onAxisFieldPPPInterpolants_m);
        gsl_interp_accel_free(onAxisFieldAccel_m);
        gsl_interp_accel_free(onAxisFieldPAccel_m);
        gsl_interp_accel_free(onAxisFieldPPAccel_m);
        gsl_interp_accel_free(onAxisFieldPPPAccel_m);

        INFOMSG(typeset_msg("freed fieldmap '" + Filename_m  + "'", "info")
                << endl);
gsell's avatar
gsell committed
100 101 102
    }
}

103 104
bool FM1DMagnetoStatic_fast::getFieldstrength(const Vector_t &R, Vector_t &E,
        Vector_t &B) const {
gsell's avatar
gsell committed
105

106 107 108
    std::vector<double> fieldComponents;
    computeFieldOnAxis(R(2), fieldComponents);
    computeFieldOffAxis(R, E, B, fieldComponents);
gsell's avatar
gsell committed
109 110

    return false;
111

gsell's avatar
gsell committed
112 113
}

114 115 116 117 118 119 120
bool FM1DMagnetoStatic_fast::getFieldDerivative(const Vector_t &R,
        Vector_t &E,
        Vector_t &B,
        const DiffDirection &dir) const {

    B(2) += gsl_spline_eval(onAxisFieldPInterpolants_m, R(2),
                            onAxisFieldPAccel_m);
gsell's avatar
gsell committed
121 122

    return false;
123

gsell's avatar
gsell committed
124 125
}

126 127 128 129 130 131
void FM1DMagnetoStatic_fast::getFieldDimensions(double &zBegin, double &zEnd,
        double &rBegin, double &rEnd) const {
    zBegin = zBegin_m;
    zEnd = zEnd_m;
    rBegin = rBegin_m;
    rEnd = rEnd_m;
gsell's avatar
gsell committed
132 133
}

134 135 136
void FM1DMagnetoStatic_fast::getFieldDimensions(double &xIni, double &xFinal,
        double &yIni, double &yFinal,
        double &zIni, double &zFinal) const {}
137

gsell's avatar
gsell committed
138 139 140 141
void FM1DMagnetoStatic_fast::swap()
{ }

void FM1DMagnetoStatic_fast::getInfo(Inform *msg) {
142 143 144 145
    (*msg) << Filename_m
           << " (1D magnetostatic); zini= "
           << zBegin_m << " m; zfinal= "
           << zEnd_m << " m;" << endl;
gsell's avatar
gsell committed
146 147 148 149 150 151 152 153
}

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

void FM1DMagnetoStatic_fast::setFrequency(double freq)
{ }
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 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 214 215 216 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 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

bool FM1DMagnetoStatic_fast::checkFileData(std::ifstream &fieldFile,
        bool parsingPassed) {

    double tempDouble;
    for(int dataIndex = 0; dataIndex <= numberOfGridPoints_m; dataIndex++)
        parsingPassed = parsingPassed
                        && interpreteLine<double>(fieldFile, tempDouble);

    return parsingPassed && interpreteEOF(fieldFile);

}

void FM1DMagnetoStatic_fast::computeFieldDerivatives(int accuracy,
        std::vector<double> fourierCoefs,
        double onAxisFieldP[],
        double onAxisFieldPP[],
        double onAxisFieldPPP[]) {

    for(int zStepIndex = 0; zStepIndex < numberOfGridPoints_m; zStepIndex++) {

        double z = deltaZ_m * zStepIndex;
        double kz = Physics::two_pi * z / length_m + Physics::pi;
        onAxisFieldP[zStepIndex] = 0.0;
        onAxisFieldPP[zStepIndex] = 0.0;
        onAxisFieldPPP[zStepIndex] = 0.0;

        int coefIndex = 1;
        for(int n = 1; n < accuracy; n++) {

            double kn = n * Physics::two_pi / length_m;
            double coskzn = cos(kz * n);
            double sinkzn = sin(kz * n);

            onAxisFieldP[zStepIndex] += kn * (-fourierCoefs.at(coefIndex) * sinkzn
                                              - fourierCoefs.at(coefIndex + 1) * coskzn);

            double derivCoeff = pow(kn, 2.0);
            onAxisFieldPP[zStepIndex] += derivCoeff * (-fourierCoefs.at(coefIndex) * coskzn
                                         + fourierCoefs.at(coefIndex + 1) * sinkzn);
            derivCoeff *= kn;
            onAxisFieldPPP[zStepIndex] += derivCoeff * (fourierCoefs.at(coefIndex) * sinkzn
                                          + fourierCoefs.at(coefIndex + 1) * coskzn);

            coefIndex += 2;
        }
    }
}

void FM1DMagnetoStatic_fast::computeFieldOffAxis(const Vector_t &R,
        Vector_t &E,
        Vector_t &B,
        std::vector<double> fieldComponents) const {

    double radiusSq = pow(R(0), 2.0) + pow(R(1), 2.0);
    double transverseBFactor = -fieldComponents.at(1) / 2.0
                               + radiusSq * fieldComponents.at(3) / 16.0;

    B(0) += R(0) * transverseBFactor;
    B(1) += R(1) * transverseBFactor;
    B(2) += fieldComponents.at(0) - fieldComponents.at(2) * radiusSq / 4.0;

}

void FM1DMagnetoStatic_fast::computeFieldOnAxis(double z,
        std::vector<double> &fieldComponents)
const {

    fieldComponents.push_back(gsl_spline_eval(onAxisFieldInterpolants_m,
                              z, onAxisFieldAccel_m));
    fieldComponents.push_back(gsl_spline_eval(onAxisFieldPInterpolants_m,
                              z, onAxisFieldPAccel_m));
    fieldComponents.push_back(gsl_spline_eval(onAxisFieldPPInterpolants_m,
                              z, onAxisFieldPPAccel_m));
    fieldComponents.push_back(gsl_spline_eval(onAxisFieldPPPInterpolants_m,
                              z, onAxisFieldPPPAccel_m));
}

std::vector<double> FM1DMagnetoStatic_fast::computeFourierCoefficients(int accuracy,
        double fieldData[]) {

    gsl_fft_real_wavetable *waveTable = gsl_fft_real_wavetable_alloc(2
                                        * numberOfGridPoints_m - 1);
    gsl_fft_real_workspace *workSpace = gsl_fft_real_workspace_alloc(2
                                        * numberOfGridPoints_m - 1);

    // Reflect field about minimum z value to ensure that it is periodic.
    double *fieldDataReflected = new double[2 * numberOfGridPoints_m - 1];
    for(int dataIndex = 0; dataIndex < numberOfGridPoints_m; dataIndex++) {
        fieldDataReflected[numberOfGridPoints_m - 1 + dataIndex]
        = fieldData[dataIndex];
        if(dataIndex != 0)
            fieldDataReflected[numberOfGridPoints_m - 1 - dataIndex]
            = fieldData[dataIndex];
    }

    gsl_fft_real_transform(fieldDataReflected, 1, 2 * numberOfGridPoints_m - 1,
                           waveTable, workSpace);

    std::vector<double> fourierCoefs;
    fourierCoefs.push_back(fieldDataReflected[0]
                           / (2.0 * numberOfGridPoints_m - 1));
    for(int coefIndex = 1; coefIndex < 2 * accuracy - 1; coefIndex++)
        fourierCoefs.push_back(2.0 * fieldDataReflected[coefIndex]
                               / (2.0 * numberOfGridPoints_m - 1));

    delete [] fieldDataReflected;
    gsl_fft_real_workspace_free(workSpace);
    gsl_fft_real_wavetable_free(waveTable);

    return fourierCoefs;

}

void FM1DMagnetoStatic_fast::computeInterpolationVectors(double onAxisFieldP[],
        double onAxisFieldPP[],
        double onAxisFieldPPP[]) {

    onAxisFieldInterpolants_m = gsl_spline_alloc(gsl_interp_cspline,
                                numberOfGridPoints_m);
    onAxisFieldPInterpolants_m = gsl_spline_alloc(gsl_interp_cspline,
                                 numberOfGridPoints_m);
    onAxisFieldPPInterpolants_m = gsl_spline_alloc(gsl_interp_cspline,
                                  numberOfGridPoints_m);
    onAxisFieldPPPInterpolants_m = gsl_spline_alloc(gsl_interp_cspline,
                                   numberOfGridPoints_m);

    double *z = new double[numberOfGridPoints_m];
    for(int zStepIndex = 0; zStepIndex < numberOfGridPoints_m; zStepIndex++)
        z[zStepIndex] = deltaZ_m * zStepIndex;
    gsl_spline_init(onAxisFieldInterpolants_m, z,
                    onAxisField_m, numberOfGridPoints_m);
    gsl_spline_init(onAxisFieldPInterpolants_m, z,
                    onAxisFieldP, numberOfGridPoints_m);
    gsl_spline_init(onAxisFieldPPInterpolants_m, z,
                    onAxisFieldPP, numberOfGridPoints_m);
    gsl_spline_init(onAxisFieldPPPInterpolants_m, z,
                    onAxisFieldPPP, numberOfGridPoints_m);

    onAxisFieldAccel_m = gsl_interp_accel_alloc();
    onAxisFieldPAccel_m = gsl_interp_accel_alloc();
    onAxisFieldPPAccel_m = gsl_interp_accel_alloc();
    onAxisFieldPPPAccel_m = gsl_interp_accel_alloc();

    delete [] z;
}

void FM1DMagnetoStatic_fast::convertHeaderData() {

    // Convert to m.
    rBegin_m /= 100.0;
    rEnd_m /= 100.0;
    zBegin_m /= 100.0;
    zEnd_m /= 100.0;

    // Convert number of grid spacings to number of grid points.
    numberOfGridPoints_m++;
}

void FM1DMagnetoStatic_fast::normalizeField(double maxBz, std::vector<double> &fourierCoefs) {

    for(int dataIndex = 0; dataIndex < numberOfGridPoints_m; dataIndex++)
        onAxisField_m[dataIndex] /= maxBz;

    for(std::vector<double>::iterator fourierIterator = fourierCoefs.begin();
        fourierIterator < fourierCoefs.end(); fourierIterator++)
        *fourierIterator /= maxBz;

}

double FM1DMagnetoStatic_fast::readFileData(std::ifstream &fieldFile,
        double fieldData[]) {

    double maxBz = 0.0;
    for(int dataIndex = 0; dataIndex < numberOfGridPoints_m; dataIndex++) {
        interpreteLine<double>(fieldFile, fieldData[dataIndex]);
        if(std::abs(fieldData[dataIndex]) > maxBz)
            maxBz = std::abs(fieldData[dataIndex]);
    }

    return maxBz;
}

bool FM1DMagnetoStatic_fast::readFileHeader(std::ifstream &fieldFile) {

    std::string tempString;
    int tempInt;

342
    bool parsingPassed = interpreteLine<std::string, int>(fieldFile,
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
                         tempString, tempInt);
    parsingPassed = parsingPassed &&
                    interpreteLine<double, double, int>(fieldFile,
                            zBegin_m,
                            zEnd_m,
                            numberOfGridPoints_m);
    parsingPassed = parsingPassed &&
                    interpreteLine<double, double, int>(fieldFile,
                            rBegin_m,
                            rEnd_m, tempInt);
    return parsingPassed;
}

int FM1DMagnetoStatic_fast::stripFileHeader(std::ifstream &fieldFile) {

    std::string tempString;
    int tempInt;
    int accuracy;
    double tempDouble;

363
    interpreteLine<std::string, int>(fieldFile, tempString, accuracy);
364 365 366 367 368 369 370 371 372 373
    interpreteLine<double, double, int>(fieldFile, tempDouble,
                                        tempDouble, tempInt);
    interpreteLine<double, double, int>(fieldFile, tempDouble,
                                        tempDouble, tempInt);

    if(accuracy > numberOfGridPoints_m)
        accuracy = numberOfGridPoints_m;

    return accuracy;
}