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

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

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
        if(!parsingPassed) {
gsell's avatar
gsell committed
24
            disableFieldmapWarning();
25 26 27 28 29 30 31
            zEnd_m = zBegin_m - 1.0e-3;
        } else
            convertHeaderData();

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

gsell's avatar
gsell committed
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
    }
}

FM1DElectroStatic_fast::~FM1DElectroStatic_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 55
    }
}

void FM1DElectroStatic_fast::readMap() {
    if(onAxisField_m == NULL) {

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

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

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

68 69 70 71 72 73 74
        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
75

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

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

void FM1DElectroStatic_fast::freeMap() {
86 87 88 89 90 91 92 93 94 95 96 97 98
    if(onAxisField_m != NULL) {
        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
99 100 101
    }
}

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

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

    return false;
110

gsell's avatar
gsell committed
111 112
}

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

    E(2) += gsl_spline_eval(onAxisFieldPInterpolants_m, R(2),
                            onAxisFieldPAccel_m);

gsell's avatar
gsell committed
121
    return false;
122

gsell's avatar
gsell committed
123 124
}

125 126 127 128 129 130
void FM1DElectroStatic_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
131
}
132 133 134
void FM1DElectroStatic_fast::getFieldDimensions(double &xIni, double &xFinal,
        double &yIni, double &yFinal,
        double &zIni, double &zFinal) const {}
gsell's avatar
gsell committed
135 136 137 138 139

void FM1DElectroStatic_fast::swap()
{ }

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

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

void FM1DElectroStatic_fast::setFrequency(double freq)
{ }
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 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

bool FM1DElectroStatic_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 FM1DElectroStatic_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 FM1DElectroStatic_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 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_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> FM1DElectroStatic_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 FM1DElectroStatic_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 FM1DElectroStatic_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 FM1DElectroStatic_fast::normalizeField(double maxEz, std::vector<double> &fourierCoefs) {

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

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

}

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

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

    return maxEz;
}

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

    std::string tempString;
    int tempInt;

339
    bool parsingPassed = interpreteLine<std::string, int>(fieldFile, tempString,
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
                         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 FM1DElectroStatic_fast::stripFileHeader(std::ifstream &fieldFile) {

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

359
    interpreteLine<std::string, int>(fieldFile, tempString, accuracy);
360 361 362 363 364 365 366 367 368 369
    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;
}