FM1DDynamic.cpp 10.4 KB
Newer Older
gsell's avatar
gsell committed
1
#include <fstream>
2
#include <iostream>
gsell's avatar
gsell committed
3 4 5 6 7

#include "Fields/FM1DDynamic.hh"
#include "Fields/Fieldmap.icc"
#include "Physics/Physics.h"

8
#include "gsl/gsl_fft_real.h"
gsell's avatar
gsell committed
9

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

    Type = T1DDynamic;

15 16 17 18 19 20 21 22
    std::ifstream fieldFile(Filename_m.c_str());
    if(fieldFile.good()) {

        bool parsingPassed = readFileHeader(fieldFile);
        parsingPassed = checkFileData(fieldFile, parsingPassed);
        fieldFile.close();

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

        length_m = 2.0 * numberOfGridPoints_m * (zEnd_m - zBegin_m)
                   / (numberOfGridPoints_m - 1);

gsell's avatar
gsell committed
31 32
    } else {
        noFieldmapWarning();
33 34
        zBegin_m = 0.0;
        zEnd_m = -1e-3;
gsell's avatar
gsell committed
35 36 37 38 39 40 41 42
    }
}

FM1DDynamic::~FM1DDynamic() {
}

void FM1DDynamic::readMap() {

43
    if(fourierCoefs_m.empty()) {
gsell's avatar
gsell committed
44

45 46
        ifstream fieldFile(Filename_m.c_str());
        stripFileHeader(fieldFile);
gsell's avatar
gsell committed
47

48 49 50 51 52
        double *fieldData = new double[2 * numberOfGridPoints_m - 1];
        double maxEz = readFileData(fieldFile, fieldData);
        fieldFile.close();
        computeFourierCoefficients(maxEz, fieldData);
        delete [] fieldData;
gsell's avatar
gsell committed
53

54 55 56
        INFOMSG(typeset_msg("Read in field map '" + Filename_m + "'", "info") << endl);
    }
}
gsell's avatar
gsell committed
57

58
void FM1DDynamic::freeMap() {
gsell's avatar
gsell committed
59

60 61
    if(!fourierCoefs_m.empty()) {
        fourierCoefs_m.clear();
gsell's avatar
gsell committed
62

63 64 65 66 67 68 69
        INFOMSG(typeset_msg("Freed field map '" + Filename_m  + "'", "info")
                << endl);
    }
}

bool FM1DDynamic::getFieldstrength(const Vector_t &R, Vector_t &E,
                                   Vector_t &B) const {
gsell's avatar
gsell committed
70

71 72 73
    std::vector<double> fieldComponents;
    computeFieldOnAxis(R(2), fieldComponents);
    computeFieldOffAxis(R, E, B, fieldComponents);
gsell's avatar
gsell committed
74

75 76
    return false;
}
gsell's avatar
gsell committed
77

78 79 80 81
bool FM1DDynamic::getFieldDerivative(const Vector_t &R,
                                     Vector_t &E,
                                     Vector_t &B,
                                     const DiffDirection &dir) const {
gsell's avatar
gsell committed
82

83 84
    double kz = Physics::two_pi * R(2) / length_m + Physics::pi;
    double eZPrime = 0.0;
gsell's avatar
gsell committed
85

86 87 88 89 90 91 92
    int coefIndex = 1;
    for(int n = 1; n < accuracy_m; n++) {

        eZPrime += n * Physics::two_pi / length_m
                   * (-fourierCoefs_m.at(coefIndex) * sin(kz * n)
                      - fourierCoefs_m.at(coefIndex + 1) * cos(kz * n));
        coefIndex += 2;
gsell's avatar
gsell committed
93 94

    }
95 96 97 98

    E(2) +=  eZPrime;

    return false;
gsell's avatar
gsell committed
99 100
}

101 102 103 104 105 106 107
void FM1DDynamic::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
108

109 110 111 112
void FM1DDynamic::getFieldDimensions(double &xIni, double &xFinal,
                                     double &yIni, double &yFinal,
                                     double &zIni, double &zFinal) const {
}
gsell's avatar
gsell committed
113

114 115 116 117 118 119 120 121
void FM1DDynamic::swap()
{ }

void FM1DDynamic::getInfo(Inform *msg) {
    (*msg) << Filename_m
           << " (1D dynamic); zini= "
           << zBegin_m << " m; zfinal= "
           << zEnd_m << " m;" << endl;
gsell's avatar
gsell committed
122 123
}

124 125 126
double FM1DDynamic::getFrequency() const {
    return frequency_m;
}
gsell's avatar
gsell committed
127

128 129 130
void FM1DDynamic::setFrequency(double frequency) {
    frequency_m = frequency;
}
gsell's avatar
gsell committed
131

132 133 134 135 136 137 138 139
void FM1DDynamic::getOnaxisEz(vector<pair<double, double> > &eZ) {

    eZ.resize(numberOfGridPoints_m);
    ifstream fieldFile(Filename_m.c_str());
    stripFileHeader(fieldFile);
    double maxEz = readFileData(fieldFile, eZ);
    fieldFile.close();
    scaleField(maxEz, eZ);
gsell's avatar
gsell committed
140 141 142

}

143
bool FM1DDynamic::checkFileData(std::ifstream &fieldFile, bool parsingPassed) {
gsell's avatar
gsell committed
144

145 146 147 148
    double tempDouble;
    for(int dataIndex = 0; dataIndex <= numberOfGridPoints_m; dataIndex++)
        parsingPassed = parsingPassed
                        && interpreteLine<double>(fieldFile, tempDouble);
gsell's avatar
gsell committed
149

150
    return parsingPassed && interpreteEOF(fieldFile);
gsell's avatar
gsell committed
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
void FM1DDynamic::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 transverseEFactor = fieldComponents.at(1)
                               * (0.5 - radiusSq * twoPiOverLambdaSq_m / 16.0)
                               - radiusSq * fieldComponents.at(3) / 16.0;
    double transverseBFactor = (fieldComponents.at(0)
                                * (0.5 - radiusSq * twoPiOverLambdaSq_m / 16.0)
                                - radiusSq * fieldComponents.at(2) / 16.0)
                               * twoPiOverLambdaSq_m / frequency_m;

    E(0) += - R(0) * transverseEFactor;
    E(1) += - R(1) * transverseEFactor;
    E(2) += fieldComponents.at(0)
            * (1.0 - radiusSq * twoPiOverLambdaSq_m / 4.0)
            - radiusSq * fieldComponents.at(2) / 4.0;

    B(0) += - R(1) * transverseBFactor;
    B(1) += R(0) * transverseBFactor;

gsell's avatar
gsell committed
177 178
}

179 180
void FM1DDynamic::computeFieldOnAxis(double z,
                                     std::vector<double> &fieldComponents) const {
181

182 183 184 185 186
    double kz = Physics::two_pi * z / length_m + Physics::pi;
    fieldComponents.push_back(fourierCoefs_m.at(0));
    fieldComponents.push_back(0.0);
    fieldComponents.push_back(0.0);
    fieldComponents.push_back(0.0);
gsell's avatar
gsell committed
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    int coefIndex = 1;
    for(int n = 1; n < accuracy_m; n++) {

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

        fieldComponents.at(0) += fourierCoefs_m.at(coefIndex) * coskzn
                                 - fourierCoefs_m.at(coefIndex + 1) * sinkzn;

        fieldComponents.at(1) += kn * (-fourierCoefs_m.at(coefIndex) * sinkzn
                                       - fourierCoefs_m.at(coefIndex + 1) * coskzn);

        double derivCoeff = pow(kn, 2.0);
        fieldComponents.at(2) += derivCoeff * (-fourierCoefs_m.at(coefIndex) * coskzn
                                               + fourierCoefs_m.at(coefIndex + 1) * sinkzn);
        derivCoeff *= kn;
        fieldComponents.at(3) += derivCoeff * (fourierCoefs_m.at(coefIndex) * sinkzn
                                               + fourierCoefs_m.at(coefIndex + 1) * coskzn);

        coefIndex += 2;
    }
gsell's avatar
gsell committed
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
void FM1DDynamic::computeFourierCoefficients(double maxEz, 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);
    gsl_fft_real_transform(fieldData, 1, 2 * numberOfGridPoints_m - 1,
                           waveTable, workSpace);

    /*
     * Normalize the Fourier coefficients such that the max field value
     * is 1 V/m.
     */

    fourierCoefs_m.push_back(1.0e6 * fieldData[0]
                             / (2.0 * (2 * numberOfGridPoints_m - 1) * maxEz));
    for(int coefIndex = 1; coefIndex < 2 * accuracy_m - 1; coefIndex++)
        fourierCoefs_m.push_back(1.0e6 * fieldData[coefIndex] * 2.0
                                 / ((2 * numberOfGridPoints_m - 1) * maxEz));

    gsl_fft_real_workspace_free(workSpace);
    gsl_fft_real_wavetable_free(waveTable);

gsell's avatar
gsell committed
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
void FM1DDynamic::convertHeaderData() {

    // Convert to angular frequency in Hz.
    frequency_m *= Physics::two_pi * 1.0e6;

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

    twoPiOverLambdaSq_m = pow(frequency_m / Physics::c, 2.0);

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

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

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

        /*
         * Mirror the field map about minimum z value to ensure that it
         * is periodic.
         */
        if(dataIndex != 0)
            fieldData[numberOfGridPoints_m - 1 - dataIndex]
            = fieldData[numberOfGridPoints_m + dataIndex];
gsell's avatar
gsell committed
270 271
    }

272 273 274 275 276 277 278 279 280 281 282 283 284
    return maxEz;
}

double FM1DDynamic::readFileData(std::ifstream &fieldFile,
                                 std::vector<std::pair<double, double>> &eZ) {

    double maxEz = 0.0;
    double deltaZ = (zEnd_m - zBegin_m) / (numberOfGridPoints_m - 1);
    for(int dataIndex = 0; dataIndex < numberOfGridPoints_m; dataIndex++) {
        eZ.at(dataIndex).first = deltaZ * dataIndex;
        interpreteLine<double>(fieldFile, eZ.at(dataIndex).second);
        if(std::abs(eZ.at(dataIndex).second) > maxEz)
            maxEz = std::abs(eZ.at(dataIndex).second);
gsell's avatar
gsell committed
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
    return maxEz;

}

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

    std::string tempString;
    int tempInt;

    bool parsingPassed = interpreteLine<string, int>(fieldFile, tempString,
                         accuracy_m);
    parsingPassed = parsingPassed &&
                    interpreteLine<double, double, int>(fieldFile, zBegin_m,
                            zEnd_m, numberOfGridPoints_m);
    parsingPassed = parsingPassed &&
                    interpreteLine<double>(fieldFile, frequency_m);
    parsingPassed = parsingPassed &&
                    interpreteLine<double, double, int>(fieldFile, rBegin_m,
                            rEnd_m, tempInt);

    if(accuracy_m > numberOfGridPoints_m)
        accuracy_m = numberOfGridPoints_m;

    return parsingPassed;
}

void FM1DDynamic::scaleField(double maxEz, std::vector<std::pair<double, double>> &eZ) {
    for(int dataIndex = 0; dataIndex < numberOfGridPoints_m; dataIndex++)
        eZ.at(dataIndex).second /= maxEz;
}

void FM1DDynamic::stripFileHeader(std::ifstream &fieldFile) {

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

    interpreteLine<string, int>(fieldFile, tempString, tempInt);
    interpreteLine<double, double, int>(fieldFile, tempDouble, tempDouble,
                                        tempInt);
    interpreteLine<double>(fieldFile, tempDouble);
    interpreteLine<double, double, int>(fieldFile, tempDouble, tempDouble,
                                        tempInt);

gsell's avatar
gsell committed
330
}