FM1DElectroStatic.cpp 8.5 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4
#include "Fields/FM1DElectroStatic.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
FM1DElectroStatic::FM1DElectroStatic(std::string aFilename):
11
    Fieldmap(aFilename) {
gsell's avatar
gsell committed
12 13 14

    Type = T1DElectroStatic;

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
            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
30 31
    } else {
        noFieldmapWarning();
32 33
        zBegin_m = 0.0;
        zEnd_m = -1.0e-3;
gsell's avatar
gsell committed
34 35 36 37 38 39 40 41
    }
}

FM1DElectroStatic::~FM1DElectroStatic() {
}

void FM1DElectroStatic::readMap() {

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

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

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

53 54 55 56
        INFOMSG(typeset_msg("read in fieldmap '" + Filename_m  + "'", "info")
                << endl);
    }
}
gsell's avatar
gsell committed
57

58
void FM1DElectroStatic::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
        INFOMSG(typeset_msg("freed fieldmap '" + Filename_m  + "'", "info")
                << endl);
    }
}
gsell's avatar
gsell committed
67

68 69
bool FM1DElectroStatic::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
    return false;
gsell's avatar
gsell committed
76 77
}

78 79 80 81
bool FM1DElectroStatic::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
    int coefIndex = 1;
    for(int n = 1; n < accuracy_m; n++) {
gsell's avatar
gsell committed
88

89 90 91 92
        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
    E(2) +=  eZPrime;
gsell's avatar
gsell committed
97 98 99 100

    return false;
}

101 102 103 104 105 106
void FM1DElectroStatic::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
107
}
108 109 110
void FM1DElectroStatic::getFieldDimensions(double &xIni, double &xFinal,
        double &yIni, double &yFinal,
        double &zIni, double &zFinal) const {}
gsell's avatar
gsell committed
111 112 113 114 115

void FM1DElectroStatic::swap()
{ }

void FM1DElectroStatic::getInfo(Inform *msg) {
116 117 118 119
    (*msg) << Filename_m
           << " (1D electrostatic); zini= "
           << zBegin_m << " m; zfinal= "
           << zEnd_m << " m;" << endl;
gsell's avatar
gsell committed
120 121 122 123 124 125 126 127
}

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

void FM1DElectroStatic::setFrequency(double freq)
{ }
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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 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

bool FM1DElectroStatic::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 FM1DElectroStatic::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) / 2.0
                               + radiusSq * fieldComponents.at(3) / 16.0;

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

}

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

    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);

    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;
    }
}

void FM1DElectroStatic::computeFourierCoefficients(double maxBz,
        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) * maxBz));
    for(int coefIndex = 1; coefIndex < 2 * accuracy_m - 1; coefIndex++)
        fourierCoefs_m.push_back(1.0e6 * fieldData[coefIndex] * 2.0
                                 / ((2 * numberOfGridPoints_m - 1) * maxBz));

    gsl_fft_real_workspace_free(workSpace);
    gsl_fft_real_wavetable_free(waveTable);

}

void FM1DElectroStatic::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++;
}

double FM1DElectroStatic::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];
    }

    return maxEz;
}

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

    std::string tempString;
    int tempInt;

253
    bool parsingPassed = interpreteLine<std::string, int>(fieldFile,
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
                         tempString, accuracy_m);
    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);

    if(accuracy_m > numberOfGridPoints_m)
        accuracy_m = numberOfGridPoints_m;

    return parsingPassed;
}

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

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

276
    interpreteLine<std::string, int>(fieldFile, tempString, tempInt);
277 278 279 280 281 282
    interpreteLine<double, double, int>(fieldFile, tempDouble,
                                        tempDouble, tempInt);
    interpreteLine<double, double, int>(fieldFile, tempDouble,
                                        tempDouble, tempInt);

}