Tracker.cpp 11.6 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// ------------------------------------------------------------------------
// $RCSfile: Tracker.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.3.2.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: Tracker
//   The visitor class for tracking a bunch of particles through a beamline
//   using a thin-lens approximation for all elements.
//
// ------------------------------------------------------------------------
// Class category: Algorithms
// ------------------------------------------------------------------------
//
// $Date: 2004/11/12 18:57:53 $
// $Author: adelmann $
//
// ------------------------------------------------------------------------

#include "Algorithms/Tracker.h"
#include "AbsBeamline/AlignWrapper.h"
#include "AbsBeamline/Patch.h"
#include "Algorithms/MapIntegrator.h"
#include "Fields/BMultipoleField.h"

frey_m's avatar
frey_m committed
28 29
//FIXME Remove headers and dynamic_cast in readOneBunchFromFile
#include "Algorithms/PartBunch.h"
30
#ifdef ENABLE_AMR
frey_m's avatar
frey_m committed
31 32
    #include "Algorithms/AmrPartBunch.h"
#endif
frey_m's avatar
frey_m committed
33

kraus's avatar
kraus committed
34 35
#include <cfloat>
#include <cmath>
36
#include <limits>
kraus's avatar
kraus committed
37

gsell's avatar
gsell committed
38 39 40 41 42 43 44 45 46 47 48
typedef FTps<double, 2> Series2;
typedef FTps<double, 6> Series;

// Class Tracker
// ------------------------------------------------------------------------


Tracker::Tracker(const Beamline &beamline, const PartData &reference,
                 bool backBeam, bool backTrack):
    AbstractTracker(beamline, reference, backBeam, backTrack),
    itsBeamline_m(beamline),
frey_m's avatar
frey_m committed
49
    itsBunch_m(nullptr)
50
{ }
gsell's avatar
gsell committed
51 52 53


Tracker::Tracker(const Beamline &beamline,
frey_m's avatar
frey_m committed
54
                 PartBunchBase<double, 3> *bunch,
gsell's avatar
gsell committed
55 56 57 58
                 const PartData &reference,
                 bool backBeam, bool backTrack):
    AbstractTracker(beamline, reference, backBeam, backTrack),
    itsBeamline_m(beamline),
frey_m's avatar
frey_m committed
59
    itsBunch_m(bunch)
gsell's avatar
gsell committed
60 61 62 63 64 65 66
{}


Tracker::~Tracker()
{}


frey_m's avatar
frey_m committed
67
const PartBunchBase<double, 3> *Tracker::getBunch() const {
68
    return itsBunch_m;
gsell's avatar
gsell committed
69 70 71
}


72
void Tracker::addToBunch(const OpalParticle &part) {
frey_m's avatar
frey_m committed
73
    itsBunch_m->push_back(part);
gsell's avatar
gsell committed
74 75 76
}


77
//~ void Tracker::setBunch(const PartBunch &bunch) {
frey_m's avatar
frey_m committed
78
    //~ itsBunch_m = &bunch;
79
//~ }
gsell's avatar
gsell committed
80 81 82


void Tracker::visitComponent(const Component &comp) {
83
    comp.trackBunch(itsBunch_m, itsReference, back_beam, back_track);
gsell's avatar
gsell committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}


void Tracker::visitPatch(const Patch &patch) {
    Euclid3D transform = patch.getPatch();
    if(back_path) transform = Inverse(transform);
    applyTransform(transform);
}


void Tracker::visitAlignWrapper(const AlignWrapper &wrap) {
    if(wrap.offset().isIdentity()) {
        wrap.getElement()->accept(*this);
    } else {
        Euclid3D e1 = wrap.getEntranceTransform();
        Euclid3D e2 = wrap.getExitTransform();

        if(back_path) {
            // Tracking right to left.
            applyTransform(Inverse(e2));
            wrap.getElement()->accept(*this);
            applyTransform(Inverse(e1));
        } else {
            // Tracking left to right.
            applyTransform(e1);
            wrap.getElement()->accept(*this);
            applyTransform(e2);
        }
    }
}


void Tracker::visitTrackIntegrator(const TrackIntegrator &i) {
117
    i.trackBunch(itsBunch_m, itsReference, back_beam, back_track);
gsell's avatar
gsell committed
118 119 120 121
}


void Tracker::visitMapIntegrator(const MapIntegrator &i) {
122
    i.trackBunch(itsBunch_m, itsReference, back_beam, back_track);
gsell's avatar
gsell committed
123 124 125 126 127 128 129
}


void Tracker::applyDrift(double length) {
    double kin = itsReference.getM() / itsReference.getP();
    double refTime = length / itsReference.getBeta();

frey_m's avatar
frey_m committed
130
    for(unsigned int i = 0; i < itsBunch_m->getLocalNum(); i++) {
131
        OpalParticle part = itsBunch_m->get_part(i);
132
        if(part.x() != std::numeric_limits<double>::max()) {
gsell's avatar
gsell committed
133 134 135 136 137 138 139 140
            double px = part.px();
            double py = part.py();
            double pt = part.pt() + 1.0;
            double lByPz = length / sqrt(pt * pt - px * px - py * py);
            part.x() += px * lByPz;
            part.y() += py * lByPz;
            part.t() += pt * (refTime / sqrt(pt * pt + kin * kin) - lByPz);
        }
frey_m's avatar
frey_m committed
141
        itsBunch_m->set_part(part, i);
gsell's avatar
gsell committed
142 143 144 145 146 147 148 149 150
    }
}


void Tracker::applyThinMultipole
(const BMultipoleField &field, double scale) {
    int order = field.order();

    if(order > 0) {
frey_m's avatar
frey_m committed
151
        for(unsigned int i = 0; i < itsBunch_m->getLocalNum(); i++) {
152
            OpalParticle part = itsBunch_m->get_part(i);
153
            if(part.x() != std::numeric_limits<double>::max()) {
gsell's avatar
gsell committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
                double x = part.x();
                double y = part.y();
                double kx = + field.normal(order);
                double ky = - field.skew(order);

                int ord = order;
                while(--ord > 0) {
                    double kxt = x * kx - y * ky;
                    double kyt = x * ky + y * kx;
                    kx = kxt + field.normal(ord);
                    ky = kyt - field.skew(ord);
                }
                part.px() -= kx * scale;
                part.py() += ky * scale;
            }
frey_m's avatar
frey_m committed
169
            itsBunch_m->set_part(part, i);
gsell's avatar
gsell committed
170 171 172 173 174 175 176 177 178 179 180 181 182
        }
    }
}


void Tracker::applyThinSBend
(const BMultipoleField &field, double scale, double h) {
    Series2 As = buildSBendVectorPotential2D(field, h) * scale;
    Series2 Fx = As.derivative(0);
    Series2 Fy = As.derivative(1);

    // These substitutions work because As depends on x and y only,
    // and not on px or py.
frey_m's avatar
frey_m committed
183
    for(unsigned int i = 0; i < itsBunch_m->getLocalNum(); i++) {
184
        OpalParticle part = itsBunch_m->get_part(i);
gsell's avatar
gsell committed
185 186 187 188 189
        FVector<double, 2> z;
        z[0] = part.x();
        z[1] = part.y();
        part.px() -= Fx.evaluate(z);
        part.py() -= Fy.evaluate(z);
frey_m's avatar
frey_m committed
190
        itsBunch_m->set_part(part, i);
gsell's avatar
gsell committed
191 192 193 194 195 196 197 198 199
    }
}


void Tracker::applyTransform(const Euclid3D &euclid, double refLength) {
    if(! euclid.isIdentity()) {
        double kin = itsReference.getM() / itsReference.getP();
        double refTime = refLength / itsReference.getBeta();

frey_m's avatar
frey_m committed
200
        for(unsigned int i = 0; i < itsBunch_m->getLocalNum(); i++) {
201
            OpalParticle part = itsBunch_m->get_part(i);
gsell's avatar
gsell committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
            double px = part.px();
            double py = part.py();
            double pt = part.pt() + 1.0;
            double pz = sqrt(pt * pt - px * px - py * py);

            part.px() = euclid.M(0, 0) * px + euclid.M(1, 0) * py + euclid.M(2, 0) * pz;
            part.py() = euclid.M(0, 1) * px + euclid.M(1, 1) * py + euclid.M(2, 1) * pz;
            pz = euclid.M(0, 2) * px + euclid.M(1, 2) * py + euclid.M(2, 2) * pz;

            double x = part.x() - euclid.getX();
            double y = part.y() - euclid.getY();
            double x2 =
                euclid.M(0, 0) * x + euclid.M(1, 0) * y - euclid.M(2, 0) * euclid.getZ();
            double y2 =
                euclid.M(0, 1) * x + euclid.M(1, 1) * y - euclid.M(2, 1) * euclid.getZ();
            double s2 =
                euclid.M(0, 2) * x + euclid.M(1, 2) * y - euclid.M(2, 2) * euclid.getZ();
            double sByPz = s2 / pz;

            double E = sqrt(pt * pt + kin * kin);
            part.x() = x2 - sByPz * part.px();
            part.y() = y2 - sByPz * part.py();
            part.t() += pt * (refTime / E + sByPz);
frey_m's avatar
frey_m committed
225
            itsBunch_m->set_part(part, i);
gsell's avatar
gsell committed
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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
        }
    }
}


Series2 Tracker::
buildMultipoleVectorPotential2D(const BMultipoleField &field) {
    int order = field.order();

    if(order > 0) {
        static const Series2 x = Series2::makeVariable(0);
        static const Series2 y = Series2::makeVariable(1);
        Series2 kx = + field.normal(order) / double(order);
        Series2 ky = - field.skew(order)   / double(order);

        while(order > 1) {
            Series2 kxt = x * kx - y * ky;
            Series2 kyt = x * ky + y * kx;
            order--;
            kx = kxt + field.normal(order) / double(order);
            ky = kyt - field.skew(order)   / double(order);
        }

        Series2 As = x * kx - y * ky;
        As.setTruncOrder(As.getMaxOrder());
        return As;
    } else {
        return Series2(0.0);
    }
}


Series Tracker::
buildMultipoleVectorPotential(const BMultipoleField &field) {
    int order = field.order();

    if(order > 0) {
        static const Series x = Series::makeVariable(X);
        static const Series y = Series::makeVariable(Y);
        Series kx = + field.normal(order) / double(order);
        Series ky = - field.skew(order)   / double(order);

        while(order > 1) {
            Series kxt = x * kx - y * ky;
            Series kyt = x * ky + y * kx;
            order--;
            kx = kxt + field.normal(order) / double(order);
            ky = kyt - field.skew(order)   / double(order);
        }

        Series As = x * kx - y * ky;
        As.setTruncOrder(As.getMaxOrder());
        return As;
    } else {
        return Series(0.0);
    }
}


Series2
Tracker::buildSBendVectorPotential2D(const BMultipoleField &field, double h) {
    int order = field.order();
    Series2 As;

    if(order > 0) {
        static const Series2 x = Series2::makeVariable(0);
        static const Series2 y = Series2::makeVariable(1);

        // Construct terms constant and linear in y.
        Series2 Ae = + field.normal(order); // Term even in y.
        Series2 Ao = - field.skew(order);   // Term odd  in y.

        for(int i = order; --i >= 1;) {
            Ae = Ae * x + field.normal(i);
            Ao = Ao * x - field.skew(i);
        }
        Ae.setTruncOrder(Ae.getMaxOrder());
        Ao.setTruncOrder(Ao.getMaxOrder());

        Series2 hx1 = 1. + h * x; // normalized radius
        Ae = + (Ae * hx1).integral(X);
        Ao = - (Ao * hx1);
        // Add terms up to maximum order.
        As = Ae + y * Ao;

        int k = 2;
        if(k <= order) {
            Series2 yp = y * y / 2.0;

            while(true) {
                // Terms even in y.
                Ae = Ae.derivative(0);
                Ae = h * Ae / hx1 - Ae.derivative(0);
                As += Ae * yp;
                if(++k > order) break;
                yp *= y / double(k);

                // Terms odd in y.
                Ao = Ao.derivative(0);
                Ao = h * Ao / hx1 - Ao.derivative(0);
                As += Ao * yp;
                if(++k > order) break;
                yp *= y / double(k);
            }
        }
    }

    return As;
}


Series
Tracker::buildSBendVectorPotential(const BMultipoleField &field, double h) {
    int order = field.order();
    Series As;

    if(order > 0) {
        static const Series x = Series::makeVariable(X);
        static const Series y = Series::makeVariable(Y);

        // Construct terms constant and linear in y.
        Series Ae = + field.normal(order); // Term even in y.
        Series Ao = - field.skew(order);   // Term odd  in y.

        for(int i = order; --i >= 1;) {
            Ae = Ae * x + field.normal(i);
            Ao = Ao * x - field.skew(i);
        }
        Ae.setTruncOrder(Ae.getMaxOrder());
        Ao.setTruncOrder(Ao.getMaxOrder());

        Series hx1 = 1. + h * x; // normalized radius
        Ae = + (Ae * hx1).integral(X);
        Ao = - (Ao * hx1);
        // Add terms up to maximum order.
        As = Ae + y * Ao;

        int k = 2;
        if(k <= order) {
            Series yp = y * y / 2.0;

            while(true) {
                // Terms even in y.
                Ae = Ae.derivative(X);
                Ae = h * Ae / hx1 - Ae.derivative(X);
                As += Ae * yp;
                if(++k > order) break;
                yp *= y / double(k);

                // Terms odd in y.
                Ao = Ao.derivative(X);
                Ao = h * Ao / hx1 - Ao.derivative(X);
                As += Ao * yp;
                if(++k > order) break;
                yp *= y / double(k);
            }
        }
    }

    return As;
}