BeamBeam3D.cpp 15 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
// ------------------------------------------------------------------------
// $RCSfile: BeamBeam3D.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.1.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
// Description:
//
// Definitions for class: BeamBeam3D
//   Defines a concrete beam-beam interaction.
// ------------------------------------------------------------------------
//
// $Date: 2000/03/27 09:33:35 $
// $Author: Andreas Adelmann $
//
// ------------------------------------------------------------------------

#include "Algorithms/BeamBeam3D.h"
#include "AbsBeamline/BeamlineVisitor.h"
#include "AbsBeamline/ElementImage.h"
#include "Algorithms/PartBunch.h"
#include "BeamlineGeometry/Matrix3D.h"
#include "BeamlineGeometry/Vector3D.h"
#include "FixedAlgebra/FTpsMath.h"
#include "FixedAlgebra/FTps.h"
#include "FixedAlgebra/FVps.h"
#include "Physics/Physics.h"
#include "Utilities/ComplexErrorFun.h"
#include "Utilities/Gauss.h"
#include "Utilities/InverseGauss.h"
#include "Utilities/TpsWerrf.h"
#include <cmath>
#include <complex>

using std::complex;
using std::max;
using namespace Physics;


// Attribute access table.
// ------------------------------------------------------------------------

namespace {
    struct Entry {
        const char *name;
        double(BeamBeam3D::*get)() const;
        void (BeamBeam3D::*set)(double);
    };

    const Entry entries[] = {
        {
            "L",
            &BeamBeam3D::getElementLength,
            0
        },
        { 0, 0, 0 }
    };

    template <class T>
    inline T sqr(const T &x) { return x * x; }
}


// Class BeamBeam3D
// ------------------------------------------------------------------------

BeamBeam3D::BeamBeam3D():
    BeamBeam(), geometry(), errorFunction(Werrf), F(0.0), slices(1), nsli(1) {
    lf.betax = lf.betay = lf.emitx = lf.emity = 1;
}


BeamBeam3D::BeamBeam3D(const BeamBeam3D &rhs):
    BeamBeam(rhs), geometry(), errorFunction(Werrf), F(rhs.F),
    slices(rhs.slices), nsli(rhs.nsli) {
    lf.betax = rhs.lf.betax;
    lf.betay = rhs.lf.betay;
    lf.emitx = rhs.lf.emitx;
    lf.emity = rhs.lf.emity;
}


84
BeamBeam3D::BeamBeam3D(const std::string &name):
gsell's avatar
gsell committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    BeamBeam(name), geometry(), errorFunction(Werrf), F(0.0),
    slices(1), nsli(1) {
    lf.betax = lf.betay = lf.emitx = lf.emity = 1;
}


BeamBeam3D::~BeamBeam3D()
{}


void BeamBeam3D::accept(BeamlineVisitor &visitor) const {
    visitor.visitComponent(*this);
}


ElementBase *BeamBeam3D::clone() const {
    return new BeamBeam3D(*this);
}


105
Channel *BeamBeam3D::getChannel(const std::string &aKey, bool create) {
gsell's avatar
gsell committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    return ElementBase::getChannel(aKey, create);
}


NullField &BeamBeam3D::getField() {
    return field;
}


const NullField &BeamBeam3D::getField() const {
    return field;
}


NullGeometry &BeamBeam3D::getGeometry() {
    return geometry;
}

const NullGeometry &BeamBeam3D::getGeometry() const {
    return geometry;
}


ElementImage *BeamBeam3D::getImage() const {
    ElementImage *image = ElementBase::getImage();

    for(const Entry *entry = entries; entry->name != 0; ++entry) {
        image->setAttribute(entry->name, (this->*(entry->get))());
    }

    return image;
}


140 141
const std::string &BeamBeam3D::getType() const {
    static const std::string type("BeamBeam3D");
gsell's avatar
gsell committed
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 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
    return type;
}


double BeamBeam3D::getBunchCharge() const {
    return 0.0;
}


const Matrix3D &BeamBeam3D::getBunchMoment() const {
    static const Matrix3D dummy;
    return dummy;
}


const Vector3D &BeamBeam3D::getBunchDisplacement() const {
    static const Vector3D dummy;
    return dummy;
}


void BeamBeam3D::setErrorFunctionPointer
(complex<double> (*fun)(complex<double>)) {
    errorFunction = fun;
}


void BeamBeam3D::setCrossingAngle(double value) {
    phi = value;
    cphi = cos(phi);
    sphi = sin(phi);
    tphi = sphi / cphi;
    computeSlices();
}


void BeamBeam3D::setBeamBeamParameter(double value) {
    xiyn = value;
    computeF();
}


void BeamBeam3D::setBeamDescription(const Vector3D &disp, const Beta &beta) {
    displacement = disp;
    lf = beta;
    computeF();
    computeSlices();
}


void BeamBeam3D::setSlices(int slices) {
    nsli = slices;
    computeF();
    computeSlices();
}


void BeamBeam3D::trackBunch
(PartBunch &bunch, const PartData &, bool revBeam, bool revTrack) const {
    boost(bunch);
    synchroBeamCollision(bunch);
    boosti(bunch);
    double ax, ay;
    bunch.maximumAmplitudes(D, ax, ay);
    axmax = max(axmax, ax);
    aymax = max(aymax, ay);
}


void BeamBeam3D::trackMap
(FVps<double, 6> &map, const PartData &, bool revBeam, bool revTrack) const {
    boost(map);
    synchroBeamCollision(map);
    boosti(map);
}


void BeamBeam3D::boost(PartBunch &bunch) const {

    for(unsigned int i = 0; i < bunch.getLocalNum(); i++) {
        Particle part = bunch.get_part(i);

        double a = (sqr(part.px()) + sqr(part.py())) / sqr(1.0 + part.pt());
        double sqr1a = sqrt(1.0 - a);
        double h = (part.pt() + 1.0) * a / (1.0 + sqr1a);
        part.px() = (part.px() - tphi * h) / cphi;
        part.py() = part.py() / cphi;
        part.pt() = part.pt() - sphi * part.px();
        double a1 = (sqr(part.px()) + sqr(part.py())) / sqr(1.0 + part.pt());
        sqr1a = sqrt(1.0 - a1);
        double hd1 = (1.0 + part.pt()) * sqr1a;
        double h1x = part.px() / hd1;
        double h1y = part.py() / hd1;
        double h1z = a1 / (1.0 + sqr1a) / sqr1a;

        double x1 = tphi * part.t() + (1.0 + sphi * h1x) * part.x();
        part.y() = part.y() + sphi * h1y * part.x();
        part.t() = part.t() / cphi - sphi * h1z * part.x();
        part.x() = x1;
        bunch.set_part(part, i);
    }
}


void BeamBeam3D::boost(FVps<double, 6> &map) const {
    Series a = (sqr(map[1]) + sqr(map[3])) / sqr(1.0 + map[5]);
    Series sqr1a = sqrt(1.0 - a);
    Series h = (map[5] + 1.0) * a / (1.0 + sqr1a);
    map[1] = (map[1] - tphi * h) / cphi;
    map[3] = map[3] / cphi;
    map[5] = map[5] - sphi * map[1];
    Series a1 = (sqr(map[1]) + sqr(map[3])) / sqr(1.0 + map[5]);
    sqr1a = sqrt(1.0 - a1);
    Series hd1 = (1.0 + map[5]) * sqr1a;
    Series h1x = map[1] / hd1;
    Series h1y = map[3] / hd1;
    Series h1z = a1 / (1.0 + sqr1a) / sqr1a;

    Series x1 = tphi * map[4] + (1.0 + sphi * h1x) * map[0];
    map[2] = map[2] + sphi * h1y * map[0];
    map[4] = map[4] / cphi - sphi * h1z * map[0];
    map[0] = x1;
}


void BeamBeam3D::boosti(PartBunch &bunch) const {
    for(unsigned int i = 0; i < bunch.getLocalNum(); i++) {
        Particle part = bunch.get_part(i);

        double a1 = (sqr(part.px()) + sqr(part.py())) / sqr(1.0 + part.pt());
        double sqr1a = sqrt(1.0 - a1);
        double h1d = (1.0 + part.pt()) * sqr1a;
        double h1 = (part.pt() + 1.0) * a1 / (1.0 + sqr1a);
        double h1x = part.px() / h1d;
        double h1y = part.py() / h1d;
        double h1z = a1 / (1.0 + sqr1a) / sqr1a;
        double det = 1.0 + sphi * (h1x - sphi * h1z);
        part.x() = (part.x() - sphi * part.t()) / det;
        part.t() = cphi * (part.t() + h1z * sphi * part.x());
        part.y() = part.y() - h1y * sphi * part.x();
        part.pt() = part.pt() + sphi * part.px();
        part.px() = (part.px() + sphi * h1) * cphi;
        part.py() = part.py() * cphi;
        bunch.set_part(part, i);
    }
}


void BeamBeam3D::boosti(FVps<double, 6> &map) const {
    Series a1 = (sqr(map[1]) + sqr(map[3])) / sqr(1.0 + map[5]);
    Series sqr1a = sqrt(1.0 - a1);
    Series h1d = (1.0 + map[5]) * sqr1a;
    Series h1 = (map[5] + 1.0) * a1 / (1.0 + sqr1a);
    Series h1x = map[1] / h1d;
    Series h1y = map[3] / h1d;
    Series h1z = a1 / (1.0 + sqr1a) / sqr1a;
    Series det = 1.0 + sphi * (h1x - sphi * h1z);
    map[0] = (map[0] - sphi * map[4]) / det;
    map[4] = cphi * (map[4] + h1z * sphi * map[0]);
    map[2] = map[2] - h1y * sphi * map[0];
    map[5] = map[5] + sphi * map[1];
    map[1] = (map[1] + sphi * h1) * cphi;
    map[3] = map[3] * cphi;
}


void BeamBeam3D::computeF() {
    double sigxxn = lf.emitx * lf.betax;
    double sigyyn = lf.emity * lf.betay;
    F = (xiyn * two_pi * sqrt(sigyyn) * (sqrt(sigyyn) + sqrt(sigxxn))) /
        (lf.betay * double(nsli));
}


void BeamBeam3D::computeSlices() {
    static const double border = 8.0;
    double bord1 = - border;
    double bord2;
    double sigz = lf.sigt / cphi;

    for(int i = 1; i <= nsli; ++i) {
        if(i != nsli) {
            bord2 = InverseGauss(double(i) / double(nsli));
        } else {
            bord2 = + border;
        }

        BeamBeam3D::Slice slice;
        slice.zstar = (exp(-sqr(bord2) / 2.0) - exp(-sqr(bord1) / 2.0)) /
                      sqrt(two_pi) * double(nsli);
        bord1 = bord2;

        slice.zstar = displacement.getZ() + slice.zstar * sigz;
        slice.xstar = displacement.getX() + slice.zstar * sphi;
        slice.ystar = displacement.getY();
        slice.sigx  =  lf.emitx * lf.betax + sqr(lf.etax * lf.sige);
        slice.sigpx = (lf.emitx / lf.betax + sqr(lf.etapx * lf.sige)) / sqr(cphi);
        slice.sigy  =  lf.emity * lf.betay + sqr(lf.etay * lf.sige);
        slice.sigpy = (lf.emity / lf.betay + sqr(lf.etapy * lf.sige)) / sqr(cphi);
        slices.push_back(slice);
    }
}


void BeamBeam3D::synchroBeamCollision(PartBunch &bunch) const {
    std::vector<Slice>::const_iterator last_slice = slices.end();
    std::vector<Slice>::const_iterator slice = slices.begin();


    for(; slice != last_slice; ++slice) {

        for(unsigned int i = 0; i < bunch.getLocalNum(); i++) {
            Particle part = bunch.get_part(i);
            double s  = (part.t() - slice->zstar) / 2.0;
            double sx = slice->sigx + slice->sigpx * s * s;
            double sy = slice->sigy + slice->sigpy * s * s;

            double sepx = part.x() + part.px() * s - slice->xstar;
            double sepy = part.y() + part.py() * s - slice->ystar;
361
            double bbfx = 0, bbfy = 0, bbgx = 0, bbgy = 0;
gsell's avatar
gsell committed
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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506

            if(std::abs(sx - sy) < 1.0e-6 * std::abs(sx + sy)) {
                double x = sepx * sepx + sepy * sepy;
                if(x != 0.0) {
                    double factor = x / (sx + sy);
                    double expfac = exp(-factor);
                    bbfx = 2.0 * sepx * (1.0 - expfac) / x;
                    bbfy = 2.0 * sepy * (1.0 - expfac) / x;
                    double comfac = -sepx * bbfx + sepy * bbfy;
                    bbgx = (+comfac + 4.0 * sepx * sepx * factor * expfac / x) / (2.0 * x);
                    bbgy = (-comfac + 4.0 * sepy * sepy * factor * expfac / x) / (2.0 * x);
                }
            } else if(sx > sy) {
                bbf(sepx, sepy, sx, sy, bbfx, bbfy, bbgx, bbgy);
            } else {
                bbf(sepy, sepx, sy, sx, bbfy, bbfx, bbgy, bbgx);
            }

            bbfx *= F;
            bbfy *= F;
            bbgx *= F;
            bbgy *= F;

            part.x()  += s * bbfx;
            part.px() -= bbfx;
            part.y()  += s * bbfy;
            part.py() -= bbfy;
            part.pt() -= s * (slice->sigpx * bbgx + slice->sigpy * bbgy) +
                         (bbfx * (part.px() - bbfx / 2.0) + bbfy * (part.py() - bbfy / 2.0)) / 2.0;
            bunch.set_part(part, i);
        }
    }
}


void BeamBeam3D::synchroBeamCollision(FVps<double, 6> &map) const {
    std::vector<Slice>::const_iterator last_slice = slices.end();
    std::vector<Slice>::const_iterator slice = slices.begin();

    for(; slice != last_slice; ++slice) {
        Series s  = (map[4] - slice->zstar) / 2.0;
        Series sx = slice->sigx + slice->sigpx * s * s;
        Series sy = slice->sigy + slice->sigpy * s * s;

        Series sepx = map[0] + map[1] * s - slice->xstar;
        Series sepy = map[2] + map[3] * s - slice->ystar;
        Series bbfx, bbfy, bbgx, bbgy;

        if(std::abs(sx[0] - sy[0]) < 1.0e-6 * std::abs(sx[0] + sy[0])) {
            Series x = sepx * sepx + sepy * sepy;
            if(x[0] != 0.0) {
                Series factor = x / (sx + sy);
                Series expfac = exp(-factor);
                bbfx = 2.0 * sepx * (1.0 - expfac) / x;
                bbfy = 2.0 * sepy * (1.0 - expfac) / x;
                Series comfac = -sepx * bbfx + sepy * bbfy;
                bbgx = (+comfac + 4.0 * sepx * sepx * factor * expfac / x) / (2.0 * x);
                bbgy = (-comfac + 4.0 * sepy * sepy * factor * expfac / x) / (2.0 * x);
            }
        } else if(sx[0] > sy[0]) {
            bbf(sepx, sepy, sx, sy, bbfx, bbfy, bbgx, bbgy);
        } else {
            bbf(sepy, sepx, sy, sx, bbfy, bbfx, bbgy, bbgx);
        }

        bbfx *= F;
        bbfy *= F;
        bbgx *= F;
        bbgy *= F;

        map[0] += s * bbfx;
        map[1] -= bbfx;
        map[2] += s * bbfy;
        map[3] -= bbfy;
        map[5] -= s * (slice->sigpx * bbgx + slice->sigpy * bbgy) +
                  (bbfx * (map[1] - bbfx / 2.0) + bbfy * (map[3] - bbfy / 2.0)) / 2.0;
    }
}


void BeamBeam3D::bbf(double sepx,  double sepy,  double sigxx, double sigyy,
                     double &bbfx, double &bbfy, double &bbgx, double &bbgy)
const {
    static const double sqrpi2 = 2.0 * sqrt(pi);

    double x = sepx * sepx / sigxx + sepy * sepy / sigyy;
    double fac2 = 2.0 * std::abs(sigxx - sigyy);
    double fac  = sqrt(fac2);
    double sigxy = sqrt(sigxx / sigyy);
    double expfac = 0.0;
    complex<double> w1 =
        (*errorFunction)(complex<double>(std::abs(sepx), std::abs(sepy)) / fac);

    if(x < 100.0) {
        expfac = exp(- x * 0.5);
        w1 -= expfac * (*errorFunction)
              (complex<double>(std::abs(sepx) / sigxy, std::abs(sepy) * sigxy) / fac);
    }

    complex<double> bbf = (sqrpi2 / fac) * w1;
    bbfx = std::imag(bbf);
    bbfy = std::real(bbf);
    if(sepx < 0) bbfx = - bbfx;
    if(sepy < 0) bbfy = - bbfy;
    double comfac = sepx * bbfx + sepy * bbfy;
    bbgx = - (comfac + 2.0 * (expfac / sigxy - 1.0)) / fac2;
    bbgy = (comfac + 2.0 * (expfac * sigxy - 1.0)) / fac2;
}


void BeamBeam3D::bbf(const Series &sepx, const Series &sepy,
                     const Series &sigxx, const Series &sigyy,
                     Series &bbfx, Series &bbfy, Series &bbgx, Series &bbgy)
const {
    static const double sqrpi2 = 2.0 * sqrt(pi);

    Series x = sepx * sepx / sigxx + sepy * sepy / sigyy;
    Series fac2 = 2.0 * ((sigxx[0] > sigyy[0]) ? sigxx - sigyy : sigyy - sigxx);
    Series fac  = sqrt(fac2);
    Series sigxy = sqrt(sigxx / sigyy);
    Series expfac = 0.0;
    Series argx = (sepx[0] < 0.0) ? -sepx : sepx;
    Series argy = (sepy[0] < 0.0) ? -sepy : sepy;
    Series wx;
    Series wy;
    TpsWerrf(argx / fac, argy / fac, wx, wy);

    if(x[0] < 100.0) {
        expfac = exp(- x * 0.5);
        Series wx2;
        Series wy2;
        TpsWerrf(argx / (sigxy * fac), (argy * sigxy) / fac, wx2, wy2);
        wx -= expfac * wx2;
        wy -= expfac * wy2;
    }

    bbfx = (sqrpi2 / fac) * wy;
    bbfy = (sqrpi2 / fac) * wx;

    if(sepx[0] < 0) bbfx = - bbfx;
    if(sepy[0] < 0) bbfy = - bbfy;
    Series comfac = sepx * bbfx + sepy * bbfy;
    bbgx = - (comfac + 2.0 * (expfac / sigxy - 1.0)) / fac2;
    bbgy = (comfac + 2.0 * (expfac * sigxy - 1.0)) / fac2;
}