CSRWakeFunction.cpp 12.4 KB
Newer Older
gsell's avatar
gsell committed
1
#include "Solvers/CSRWakeFunction.hh"
2
#include "Algorithms/PartBunch.h"
3
#include "Filters/Filter.h"
gsell's avatar
gsell committed
4 5 6
#include "Physics/Physics.h"
#include "AbsBeamline/RBend.h"
#include "AbsBeamline/SBend.h"
7
#include "Utilities/Options.h"
8

kraus's avatar
kraus committed
9 10
#include <iostream>
#include <fstream>
11
// using namespace std;
12

13
CSRWakeFunction::CSRWakeFunction(const std::string &name, ElementBase *element, std::vector<Filter *> filters, const unsigned int &N):
gsell's avatar
gsell committed
14 15 16 17 18
    WakeFunction(name, element),
    filters_m(filters.begin(), filters.end()),
    lineDensity_m(),
    dlineDensitydz_m(),
    d2lineDensitydz2_m(),
19
    bendRadius_m(0.0),
20
    totalBendAngle_m(0.0)
gsell's avatar
gsell committed
21 22 23
{ }

void CSRWakeFunction::apply(PartBunch &bunch) {
24
    Inform msg("CSRWake ");
gsell's avatar
gsell committed
25

26 27
    const double &meshSpacing = bunch.getMesh().get_meshSpacing(2);
    const Vector_t &meshOrigin = bunch.getMesh().get_origin();
gsell's avatar
gsell committed
28

29
    calculateLineDensity(bunch, meshSpacing);
gsell's avatar
gsell committed
30

31 32 33 34
    if(Ez_m.size() < lineDensity_m.size()) {
        Ez_m.resize(lineDensity_m.size(), 0.0);
        Psi_m.resize(lineDensity_m.size(), 0.0);
    }
gsell's avatar
gsell committed
35 36

    Vector_t smin, smax;
37 38 39 40
    bunch.get_bounds(smin, smax);
    double minPathLength = smin(2) - FieldBegin_m;
    Ez_m[0] = 0.0;
    // calculate wake field of bunch
41
    for(unsigned int i = 1; i < lineDensity_m.size(); ++i) {
42 43 44 45
        Ez_m[i] = 0.0;

        double angleOfSlice = 0.0;
        double pathLengthOfSlice = minPathLength + i * meshSpacing;
46
        if(pathLengthOfSlice > 0.0)
47 48 49 50 51 52 53 54 55 56
            angleOfSlice = pathLengthOfSlice / bendRadius_m;

        calculateContributionInside(i, angleOfSlice, meshSpacing);
        calculateContributionAfter(i, angleOfSlice, meshSpacing);

        Ez_m[i] /= (4. * Physics::pi * Physics::epsilon_0);
    }

    // calculate the wake field seen by the particles
    for(unsigned int i = 0; i < bunch.getLocalNum(); ++i) {
57
        const Vector_t &R = bunch.R[i];
58 59 60 61 62
        int indexz = (int)floor((R(2) - meshOrigin(2)) / meshSpacing);
        double leverz = (R(2) - meshOrigin(2)) / meshSpacing - indexz;
        bunch.Ef[i](2) += (1. - leverz) * Ez_m[indexz] + leverz * Ez_m[indexz + 1];
    }

63

64 65

    if(Options::csrDump) {
66
        static std::string oldBendName;
67 68 69 70 71
        static unsigned long counter = 0;

        if(oldBendName != bendName_m) counter = 0;

        const int every = 1;
72
        bool print_criterion = (counter + 1) % every == 0;
73 74 75
        if(print_criterion) {
            static unsigned int file_number = 0;
            if(counter == 0) file_number = 0;
76 77 78 79 80 81 82
	    double spos = bunch.get_sPos();
	    if (Ippl::myNode() == 0) {
	      std::stringstream filename_str;
	      filename_str << "data/" << bendName_m << "-CSRWake" << file_number << ".txt";
	      std::ofstream csr(filename_str.str().c_str());
	      csr << spos << std::endl;
	      for(unsigned int i = 0; i < lineDensity_m.size(); ++ i) {
83 84 85 86
                csr << i *meshSpacing << "\t"
                    << Ez_m[i] << "\t"
                    << lineDensity_m[i] << "\t"
                    << dlineDensitydz_m[i] << std::endl;
87 88 89 90
	      }
	      csr.close();
	      msg << "** wrote " << filename_str.str() << endl;
	    }
91
            ++ file_number;
92
        }
93 94
        ++ counter;
        oldBendName = bendName_m;
95 96
    }
}
gsell's avatar
gsell committed
97

98
void CSRWakeFunction::initialize(const ElementBase *ref) {
99 100 101
    double End;
    if(dynamic_cast<const RBend *>(ref)) {
        const RBend *bend = dynamic_cast<const RBend *>(ref);
102
        bendRadius_m = bend->GetBendRadius();
103
        bend->getDimensions(Begin_m, End);
104
        Length_m = bend->GetEffectiveLength();
105 106
        //xpang 09-19-2014 : removed "Begin_m +" from FieldBegin_m
        FieldBegin_m = bend->GetEffectiveCenter() - Length_m / 2.0;
107
        totalBendAngle_m = std::abs(bend->GetBendAngle());
108
        bendName_m = bend->getName();
109
    } else if(dynamic_cast<const SBend *>(ref)) {
110
        const SBend *bend = dynamic_cast<const SBend *>(ref);
111
        bendRadius_m = bend->GetBendRadius();
112
        bend->getDimensions(Begin_m, End);
113
        Length_m = bend->GetEffectiveLength();
114
        //xpang 09-19-2014 : removed "Begin_m +" from FieldBegin_m
115
        FieldBegin_m = bend->GetEffectiveCenter() - Length_m / 2.0;
116
        totalBendAngle_m = bend->GetBendAngle();
117
        bendName_m = bend->getName();
gsell's avatar
gsell committed
118
    }
119 120
}

121
void CSRWakeFunction::calculateLineDensity(PartBunch &bunch, double meshSpacing) {
122 123
    bunch.calcLineDensity();
    bunch.getLineDensity(lineDensity_m);
gsell's avatar
gsell committed
124

125 126
    std::vector<Filter *>::const_iterator fit;
    for(fit = filters_m.begin(); fit != filters_m.end(); ++ fit) {
gsell's avatar
gsell committed
127 128 129
        (*fit)->apply(lineDensity_m);
    }
    dlineDensitydz_m.assign(lineDensity_m.begin(), lineDensity_m.end());
130 131
    filters_m.back()->calc_derivative(dlineDensitydz_m, meshSpacing);
}
gsell's avatar
gsell committed
132

133
void CSRWakeFunction::calculateContributionInside(size_t sliceNumber, double angleOfSlice, double meshSpacing) {
134
    if(angleOfSlice > totalBendAngle_m || angleOfSlice < 0.0) return;
135 136 137 138

    const double meshSpacingsup = pow(meshSpacing, -1. / 3.);
    double SlippageLength = pow(angleOfSlice, 3) * bendRadius_m / 24.;
    double relativeSlippageLength = SlippageLength / meshSpacing;
139
    if(relativeSlippageLength > sliceNumber) {
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

        /*
        Break integral into sum of integrals between grid points, then
        use linear interpolation between each grid point.
        */

        double dx1 = pow(sliceNumber, 2. / 3.);
        double dx2 = pow(sliceNumber, 5. / 3.);
        double dx3 = pow(sliceNumber - 1., 5. / 3.);
        Ez_m[sliceNumber] += 0.3 * meshSpacingsup * dlineDensitydz_m[0] * (5. * dx1 - 3. * dx2 + 3. * dx3);
        for(unsigned int j = 1; j < sliceNumber; ++ j) {
            dx1 = dx2;
            dx2 = dx3;
            dx3 = pow(sliceNumber - j - 1., 5. / 3.);
            Ez_m[sliceNumber] += 0.9 * meshSpacingsup * dlineDensitydz_m[j] * (dx1 - 2.* dx2 + dx3);
        }
        Ez_m[sliceNumber] += 0.9 * meshSpacingsup * dlineDensitydz_m[sliceNumber];
gsell's avatar
gsell committed
157

158
    } else if(relativeSlippageLength < 1) {
gsell's avatar
gsell committed
159

160
        // First do transient term.
161
        if(4.0 * relativeSlippageLength <= 1) {
162 163 164 165

            Ez_m[sliceNumber] += 3.0 * pow(SlippageLength, 2.0 / 3.0) * (lineDensity_m[sliceNumber] - lineDensity_m[sliceNumber - 1]) / meshSpacing;
        } else {

166
            if(4.0 * relativeSlippageLength < sliceNumber) {
167 168 169 170

                int j = sliceNumber - static_cast<int>(floor(4.0 * relativeSlippageLength));
                double frac = 4.0 * relativeSlippageLength - (sliceNumber - j);
                Ez_m[sliceNumber] -= (frac * lineDensity_m[j - 1] + (1. - frac) * lineDensity_m[j]) / pow(SlippageLength, 1. / 3.);
gsell's avatar
gsell committed
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

            Ez_m[sliceNumber] += (relativeSlippageLength * lineDensity_m[sliceNumber - 1] + (1. - relativeSlippageLength) * lineDensity_m[sliceNumber]) / pow(SlippageLength, 1. / 3.);

        }

        // Now do steady state term.
        Ez_m[sliceNumber] += (0.3 / meshSpacing) * pow(SlippageLength, 2. / 3.) * (5. * dlineDensitydz_m[sliceNumber] - 2. * relativeSlippageLength * (dlineDensitydz_m[sliceNumber] - dlineDensitydz_m[sliceNumber - 1]));

    } else {

        if(4. * relativeSlippageLength < sliceNumber) {

            int j = sliceNumber - static_cast<int>(floor(4. * relativeSlippageLength));
            double frac = 4. * relativeSlippageLength - (sliceNumber - j);
            Ez_m[sliceNumber] -= (frac * lineDensity_m[j - 1] + (1. - frac) * lineDensity_m[j]) / pow(SlippageLength, 1. / 3.);

        }

        int j = sliceNumber - static_cast<int>(floor(SlippageLength / meshSpacing));
        double frac = relativeSlippageLength - (sliceNumber - j);
        Ez_m[sliceNumber] += (frac * lineDensity_m[j - 1] + (1. - frac) * lineDensity_m[j]) / pow(SlippageLength, 1. / 3.);

        double dx1 = pow(sliceNumber - j + frac, 2. / 3.);
        double dx2 = pow(sliceNumber - j, 2. / 3.);
        double dx3 = pow(sliceNumber - j + frac, 5. / 3.);
        double dx4 = pow(sliceNumber - j, 5. / 3.);

        Ez_m[sliceNumber] += 1.5 * meshSpacingsup * dlineDensitydz_m[j - 1] * (dx1 - dx2);
        Ez_m[sliceNumber] += 0.3 * meshSpacingsup * (dlineDensitydz_m[j] - dlineDensitydz_m[j - 1]) * (5.*(dx1 - dx2) + 3.*(dx3 - dx4));

        dx1 = dx2;
        dx2 = dx4;
        dx3 = pow(sliceNumber - j - 1., 5. / 3.);
        Ez_m[sliceNumber] += 0.3 * meshSpacingsup * dlineDensitydz_m[j] * (5.*dx1 - 3.*dx2 + 3.*dx3);
        for(unsigned int k = j + 1; k < sliceNumber; ++ k) {
            dx1 = dx2;
            dx2 = dx3;
            dx3 = pow(sliceNumber - k - 1., 5. / 3.);
            Ez_m[sliceNumber] += 0.9 * meshSpacingsup * dlineDensitydz_m[k] * (dx1 - 2.*dx2 + dx3);
gsell's avatar
gsell committed
212
        }
213
        Ez_m[sliceNumber] += 0.9 * meshSpacingsup * dlineDensitydz_m[sliceNumber];
gsell's avatar
gsell committed
214
    }
215 216 217
    double prefactor = -2. / pow(3. * bendRadius_m * bendRadius_m, 1. / 3.);
    Ez_m[sliceNumber] *= prefactor;
}
gsell's avatar
gsell committed
218

219
void CSRWakeFunction::calculateContributionAfter(size_t sliceNumber, double angleOfSlice, double meshSpacing) {
220
    if(angleOfSlice <= totalBendAngle_m) return;
221 222 223 224 225 226 227

    double Ds_max = bendRadius_m * pow(totalBendAngle_m, 3) / 24. * (4. - 3.* totalBendAngle_m / angleOfSlice);

    // First do contribution from particles whose retarded position is
    // prior to the bend.
    double Ds_max2 = bendRadius_m * pow(totalBendAngle_m, 2) / 6. * (3. * angleOfSlice - 2. * totalBendAngle_m);
    int j = 0;
228
    double frac = 0.0;
229 230 231 232
    if(Ds_max2 / meshSpacing < sliceNumber) {
        j = sliceNumber - static_cast<int>(floor(Ds_max2 / meshSpacing));
        frac = Ds_max2 / meshSpacing - (sliceNumber - j);
        Ez_m[sliceNumber] -= (frac * lineDensity_m[j - 1] + (1. - frac) * lineDensity_m[j]) / (2. * angleOfSlice - totalBendAngle_m);
gsell's avatar
gsell committed
233 234
    }

235 236
    // Now do delta function contribution for particles whose retarded position
    // is in the bend.
237
    if(Ds_max / meshSpacing < sliceNumber) {
238 239 240 241
        j = sliceNumber - static_cast<int>(floor(Ds_max / meshSpacing));
        frac = Ds_max / meshSpacing - (sliceNumber - j);
        Ez_m[sliceNumber] += (frac * lineDensity_m[j - 1] + (1.0 - frac) * lineDensity_m[j]) / (2. * angleOfSlice - totalBendAngle_m);
    }
gsell's avatar
gsell committed
242

243 244 245 246 247
    // Now do integral contribution for particles whose retarded position is in
    // the bend.

    double angleOverlap = angleOfSlice - totalBendAngle_m;
    int k = sliceNumber;
248
    if(Ds_max / meshSpacing < sliceNumber) {
249 250
        k = j;
        Psi_m[k] = calcPsi(Psi_m[k], angleOverlap, meshSpacing * (k + frac));
251
        if(Psi_m[k] > 0 && Psi_m[k] < totalBendAngle_m)
252 253 254
            Ez_m[sliceNumber] += 0.5 * (frac * dlineDensitydz_m[sliceNumber - k - 1] + (1.0 - frac) * dlineDensitydz_m[sliceNumber - k]) / (Psi_m[k] + 2.0 * angleOverlap);
    } else {
        Psi_m[0] = calcPsi(Psi_m[0], angleOverlap, meshSpacing * sliceNumber);
255
        if(Psi_m[0] > 0 && Psi_m[0] < totalBendAngle_m)
256
            Ez_m[sliceNumber] += 0.5 * dlineDensitydz_m[0] / (Psi_m[0] + 2.0 * angleOverlap);
gsell's avatar
gsell committed
257
    }
258 259 260 261

    // Do rest of integral.
    for(unsigned int l = sliceNumber - k + 1; l < sliceNumber; ++ l) {
        Psi_m[l] = calcPsi(Psi_m[l], angleOverlap, meshSpacing * (sliceNumber - l));
262
        if(Psi_m[l] > 0 && Psi_m[l] < totalBendAngle_m)
263
            Ez_m[sliceNumber] += dlineDensitydz_m[l] / (Psi_m[l] + 2.0 * angleOverlap);
gsell's avatar
gsell committed
264 265
    }

266 267 268
    // We don't go right to the end as there is a singularity in the numerical integral that we don't quite know
    // how to deal with properly yet. This introduces a very slight error in the calculation (fractions of a percent).
    Psi_m[sliceNumber] = calcPsi(Psi_m[sliceNumber], angleOverlap, meshSpacing / 4.0);
269
    if(Psi_m[sliceNumber] > 0 && Psi_m[sliceNumber] < totalBendAngle_m)
270 271 272 273
        Ez_m[sliceNumber] += 0.5 * dlineDensitydz_m[sliceNumber] / (Psi_m[sliceNumber] + 2.0 * angleOverlap);

    double prefactor = -4 / bendRadius_m;
    Ez_m[sliceNumber] *= prefactor;
gsell's avatar
gsell committed
274 275 276 277 278 279 280 281 282 283 284 285 286
}

double CSRWakeFunction::calcPsi(const double &psiInitial, const double &x, const double &Ds) const {
    /** solve the equation
     *  \f[
     *  \Delta s = \frac{R \Psi^3}{24} \frac{\Psi + 4x}{\Psi + x}
     *  \f]
     *  for \f$\Psi\f$ using Newtons method.
     */

    const int Nmax = 100;
    const double eps = 1e-10;
    double residual = 0.0;
287
    double psi = pow(24. * Ds / bendRadius_m, 1. / 3.);
288
    if(psiInitial != 0.0) psi = psiInitial;
gsell's avatar
gsell committed
289 290

    for(int i = 0; i < Nmax; ++i) {
291
        residual = bendRadius_m * psi * psi * psi * (psi + 4. * x) - 24. * Ds * psi - 24. * Ds * x;
gsell's avatar
gsell committed
292 293
        if(std::abs(residual) < eps)
            return psi;
294
        psi -= residual / (4. * bendRadius_m * psi * psi * psi + 12. * x * bendRadius_m * psi * psi - 24. * Ds);
gsell's avatar
gsell committed
295
    }
296
    ERRORMSG("In CSRWakeFunction::calcPsi(): exceed maximum number of iterations!" << endl);
gsell's avatar
gsell committed
297 298 299
    return psi;
}

300
const std::string CSRWakeFunction::getType() const {
gsell's avatar
gsell committed
301 302
    return "CSRWakeFunction";
}