RBend.cpp 48.8 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
// ------------------------------------------------------------------------
// $RCSfile: RBend.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.1.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: RBend
//   Defines the abstract interface for a rectangular bend magnet.
//
// ------------------------------------------------------------------------
// Class category: AbsBeamline
// ------------------------------------------------------------------------
//
// $Date: 2000/03/27 09:32:31 $
// $Author: fci $
//
// ------------------------------------------------------------------------

#include "AbsBeamline/RBend.h"
22
#include "Algorithms/PartBunch.h"
gsell's avatar
gsell committed
23 24
#include "AbsBeamline/BeamlineVisitor.h"
#include "Fields/Fieldmap.hh"
25 26
#include "Fields/FM1DProfile1.hh"
#include "Fields/FM1DProfile2.hh"
gsell's avatar
gsell committed
27 28 29 30 31 32 33 34 35 36
#include <iostream>
#include <fstream>

// Class RBend
// ------------------------------------------------------------------------

int RBend::RBend_counter_m = 0;

RBend::RBend():
    Component(),
37 38
    pusher_m(),
    fileName_m(""),
gsell's avatar
gsell committed
39 40 41
    fieldmap_m(NULL),
    fast_m(false),
    angle_m(0.0),
42 43 44 45
    aperture_m(0.0),
    designEnergy_m(0.0),
    designRadius_m(0.0),
    fieldAmplitude_m(0.0),
46 47
    bX_m(0.0),
    bY_m(0.0),
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
    entranceAngle_m(0.0),
    exitAngle_m(0.0),
    gradient_m(0.0),
    elementEdge_m(0.0),
    startField_m(0.0),
    endField_m(0.0),
    reinitialize_m(false),
    recalcRefTraj_m(false),
    length_m(0.0),
    gap_m(0.0),
    refTrajMapSize_m(0),
    refTrajMapStepSize_m(0.0),
    entranceParameter1_m(0.0),
    entranceParameter2_m(0.0),
    entranceParameter3_m(0.0),
    exitParameter1_m(0.0),
    exitParameter2_m(0.0),
    exitParameter3_m(0.0),
    xOriginEngeEntry_m(0.0),
    zOriginEngeEntry_m(0.0),
    deltaBeginEntry_m(0.0),
    deltaEndEntry_m(0.0),
    polyOrderEntry_m(0),
    xExit_m(0.0),
    zExit_m(0.0),
    xOriginEngeExit_m(0.0),
    zOriginEngeExit_m(0.0),
    deltaBeginExit_m(0.0),
    deltaEndExit_m(0.0),
    polyOrderExit_m(0),
    cosEntranceAngle_m(1.0),
    sinEntranceAngle_m(0.0),
    exitEdgeAngle_m(0.0),
    cosExitAngle_m(1.0),
    sinExitAngle_m(0.0) {

gsell's avatar
gsell committed
84
    setElType(isDipole);
85

gsell's avatar
gsell committed
86 87 88 89 90
}


RBend::RBend(const RBend &right):
    Component(right),
91 92
    pusher_m(right.pusher_m),
    fileName_m(right.fileName_m),
gsell's avatar
gsell committed
93 94 95
    fieldmap_m(right.fieldmap_m),
    fast_m(right.fast_m),
    angle_m(right.angle_m),
96 97 98 99
    aperture_m(right.aperture_m),
    designEnergy_m(right.designEnergy_m),
    designRadius_m(right.designRadius_m),
    fieldAmplitude_m(right.fieldAmplitude_m),
100 101
    bX_m(right.bX_m),
    bY_m(right.bY_m),
102 103 104 105 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 140
    entranceAngle_m(right.entranceAngle_m),
    exitAngle_m(right.exitAngle_m),
    gradient_m(right.gradient_m),
    elementEdge_m(right.elementEdge_m),
    startField_m(right.startField_m),
    endField_m(right.endField_m),
    reinitialize_m(right.reinitialize_m),
    recalcRefTraj_m(right.recalcRefTraj_m),
    length_m(right.length_m),
    gap_m(right.gap_m),
    refTrajMapX_m(right.refTrajMapX_m),
    refTrajMapY_m(right.refTrajMapY_m),
    refTrajMapZ_m(right.refTrajMapZ_m),
    refTrajMapSize_m(right.refTrajMapSize_m),
    refTrajMapStepSize_m(right.refTrajMapStepSize_m),
    entranceParameter1_m(right.entranceParameter1_m),
    entranceParameter2_m(right.entranceParameter2_m),
    entranceParameter3_m(right.entranceParameter3_m),
    exitParameter1_m(right.exitParameter1_m),
    exitParameter2_m(right.exitParameter2_m),
    exitParameter3_m(right.exitParameter3_m),
    xOriginEngeEntry_m(right.xOriginEngeEntry_m),
    zOriginEngeEntry_m(right.zOriginEngeEntry_m),
    deltaBeginEntry_m(right.deltaBeginEntry_m),
    deltaEndEntry_m(right.deltaEndEntry_m),
    polyOrderEntry_m(right.polyOrderEntry_m),
    xExit_m(right.xExit_m),
    zExit_m(right.zExit_m),
    xOriginEngeExit_m(right.xOriginEngeExit_m),
    zOriginEngeExit_m(right.zOriginEngeExit_m),
    deltaBeginExit_m(right.deltaBeginExit_m),
    deltaEndExit_m(right.deltaEndExit_m),
    polyOrderExit_m(right.polyOrderExit_m),
    cosEntranceAngle_m(right.cosEntranceAngle_m),
    sinEntranceAngle_m(right.sinEntranceAngle_m),
    exitEdgeAngle_m(right.exitEdgeAngle_m),
    cosExitAngle_m(right.cosExitAngle_m),
    sinExitAngle_m(right.sinExitAngle_m) {

gsell's avatar
gsell committed
141 142
    setElType(isDipole);

143
}
gsell's avatar
gsell committed
144

Steve Russell's avatar
Steve Russell committed
145
RBend::RBend(const std::string &name):
gsell's avatar
gsell committed
146
    Component(name),
147 148
    pusher_m(),
    fileName_m(""),
gsell's avatar
gsell committed
149 150 151
    fieldmap_m(NULL),
    fast_m(false),
    angle_m(0.0),
152 153 154 155
    aperture_m(0.0),
    designEnergy_m(0.0),
    designRadius_m(0.0),
    fieldAmplitude_m(0.0),
156 157
    bX_m(0.0),
    bY_m(0.0),
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
    entranceAngle_m(0.0),
    exitAngle_m(0.0),
    gradient_m(0.0),
    elementEdge_m(0.0),
    startField_m(0.0),
    endField_m(0.0),
    reinitialize_m(false),
    recalcRefTraj_m(false),
    length_m(0.0),
    gap_m(0.0),
    refTrajMapSize_m(0),
    refTrajMapStepSize_m(0.0),
    entranceParameter1_m(0.0),
    entranceParameter2_m(0.0),
    entranceParameter3_m(0.0),
    exitParameter1_m(0.0),
    exitParameter2_m(0.0),
    exitParameter3_m(0.0),
    xOriginEngeEntry_m(0.0),
    zOriginEngeEntry_m(0.0),
    deltaBeginEntry_m(0.0),
    deltaEndEntry_m(0.0),
    polyOrderEntry_m(0),
    xExit_m(0.0),
    zExit_m(0.0),
    xOriginEngeExit_m(0.0),
    zOriginEngeExit_m(0.0),
    deltaBeginExit_m(0.0),
    deltaEndExit_m(0.0),
    polyOrderExit_m(0),
    cosEntranceAngle_m(1.0),
    sinEntranceAngle_m(0.0),
    exitEdgeAngle_m(0.0),
    cosExitAngle_m(1.0),
    sinExitAngle_m(0.0) {

gsell's avatar
gsell committed
194 195
    setElType(isDipole);

196
}
gsell's avatar
gsell committed
197 198 199 200 201 202 203 204

RBend::~RBend() {
}

void RBend::accept(BeamlineVisitor &visitor) const {
    visitor.visitRBend(*this);
}

205 206 207 208
/*
 * OPAL-MAP methods
 * ================
 */
gsell's avatar
gsell committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
double RBend::getNormalComponent(int n) const {
    return getField().getNormalComponent(n);
}

double RBend::getSkewComponent(int n) const {
    return getField().getSkewComponent(n);
}

void RBend::setNormalComponent(int n, double v) {
    getField().setNormalComponent(n, v);
}

void RBend::setSkewComponent(int n, double v) {
    getField().setSkewComponent(n, v);
}

225 226 227
/*
 * BET methods.
 */
gsell's avatar
gsell committed
228 229 230 231 232 233 234 235 236 237 238
void RBend::addKR(int i, double t, Vector_t &K) {
    Inform msg("RBend::addK()");

    Vector_t tmpE(0.0, 0.0, 0.0);
    Vector_t tmpB(0.0, 0.0, 0.0);
    Vector_t tmpE_diff(0.0, 0.0, 0.0);
    Vector_t tmpB_diff(0.0, 0.0, 0.0);
    double pz = RefPartBunch_m->getZ(i) - startField_m - ds_m;
    const Vector_t tmpA(RefPartBunch_m->getX(i) - dx_m, RefPartBunch_m->getY(i) - dy_m, pz);

    DiffDirection zdir(DZ);
239 240
    fieldmap_m->getFieldstrength(tmpA, tmpE, tmpB);
    fieldmap_m->getFieldDerivative(tmpA, tmpE_diff, tmpB_diff, zdir);
gsell's avatar
gsell committed
241 242 243

    double g = RefPartBunch_m->getGamma(i);

244
    if(fabs(fieldAmplitude_m * tmpB_diff(2)) > 0.1) {
gsell's avatar
gsell committed
245
        double cf = Physics::q_e * tmpB(2) / (g * Physics::EMASS);
246
        K += Vector_t(-pow(cf * fieldAmplitude_m * tmpB(0), 2) / 3.0, -pow(cf * fieldAmplitude_m * tmpB(1), 2) / 3.0, 0.0);
gsell's avatar
gsell committed
247 248 249 250 251 252 253 254 255 256
    }
}

void RBend::addKT(int i, double t, Vector_t &K) {
    Inform msg("RBend::addK()");

    Vector_t tmpE(0.0, 0.0, 0.0);
    Vector_t tmpB(0.0, 0.0, 0.0);
    double pz = RefPartBunch_m->getZ(i) - startField_m - ds_m;
    const Vector_t tmpA(RefPartBunch_m->getX(i) - dx_m, RefPartBunch_m->getY(i) - dy_m, pz);
257
    fieldmap_m->getFieldstrength(tmpA, tmpE, tmpB);
gsell's avatar
gsell committed
258 259 260 261

    double b = RefPartBunch_m->getBeta(i);
    double g = 1 / sqrt(1 - b * b);

262
    double cf = -Physics::q_e * Physics::c * b * tmpB(2) * fieldAmplitude_m / (g * Physics::EMASS);
gsell's avatar
gsell committed
263 264 265 266 267
    Vector_t temp(cf * tmpB(1), cf * tmpB(0), 0.0);

    //FIXME: K += ??
}

268 269 270 271 272 273 274 275 276 277

/*
 * OPAL-T Methods.
 * ===============
 */

/*
 *  This function merely repackages the field arrays as type Vector_t and calls
 *  the equivalent method but with the Vector_t data types.
 */
278
bool RBend::apply(const size_t &i, const double &t, double E[], double B[]) {
279 280 281 282 283

    Vector_t Ev(0.0, 0.0, 0.0);
    Vector_t Bv(0.0, 0.0, 0.0);
    if(apply(RefPartBunch_m->R[i], RefPartBunch_m->get_rmean(), t, Ev, Bv))
        return true;
gsell's avatar
gsell committed
284 285 286 287 288 289 290 291 292 293 294

    E[0] = Ev(0);
    E[1] = Ev(1);
    E[2] = Ev(2);
    B[0] = Bv(0);
    B[1] = Bv(1);
    B[2] = Bv(2);

    return false;
}

295
bool RBend::apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) {
gsell's avatar
gsell committed
296

297 298 299 300 301 302
    if(designRadius_m > 0.0) {

        // Check if we need to reinitialize the bend field amplitude.
        if(reinitialize_m) {
            reinitialize_m = Reinitialize();
            recalcRefTraj_m = false;
Steve Russell's avatar
Steve Russell committed
303 304
        }

305 306 307 308 309 310 311 312 313 314 315 316 317
        /*
         * Always recalculate the reference trajectory on first call even
         * if we do not reinitialize the bend. The reference trajectory
         * has to be calculated at the same energy as the actual beam or
         * we do not get accurate values for the magnetic field in the output
         * file.
         */
        if(recalcRefTraj_m) {
            double angleX = 0.0;
            double angleY = 0.0;
            CalculateRefTrajectory(angleX, angleY);
            recalcRefTraj_m = false;
        }
Steve Russell's avatar
Steve Russell committed
318

319 320 321
        // Shift position to magnet frame.
        Vector_t X = RefPartBunch_m->X[i];
        X(2) += startField_m - elementEdge_m;
gsell's avatar
gsell committed
322

323 324 325 326 327 328
        /*
         * Add in transverse bend displacements. (ds is already
         * accounted for.)
         */
        X(0) -= dx_m;
        X(1) -= dy_m;
gsell's avatar
gsell committed
329

330 331 332 333 334
        // Get field from field map.
        Vector_t eField(0.0, 0.0, 0.0);
        Vector_t bField(0.0, 0.0, 0.0);
        CalculateMapField(X, eField, bField);
        bField *= fieldAmplitude_m;
gsell's avatar
gsell committed
335

336 337 338 339 340
        B(0) += bField(0);
        B(1) += bField(1);
        B(2) += bField(2);

    }
gsell's avatar
gsell committed
341 342 343 344

    return false;
}

345 346 347 348 349
bool RBend::apply(const Vector_t &R,
                  const Vector_t &centroid,
                  const double &t,
                  Vector_t &E,
                  Vector_t &B) {
gsell's avatar
gsell committed
350

351
    if(designRadius_m > 0.0) {
gsell's avatar
gsell committed
352

353 354
        int index = static_cast<int>
                    (std::floor((R(2) - startField_m) / refTrajMapStepSize_m));
gsell's avatar
gsell committed
355

356
        if(index > 0 && index + 1 < refTrajMapSize_m) {
gsell's avatar
gsell committed
357

358 359 360 361 362 363 364 365
            // Find indices for position in pre-computed central trajectory map.
            double lever = (R(2) - startField_m) / refTrajMapStepSize_m - index;
            double x = (1.0 - lever) * refTrajMapX_m.at(index)
                       + lever * refTrajMapX_m.at(index + 1);
            double y = (1.0 - lever) * refTrajMapY_m.at(index)
                       + lever * refTrajMapY_m.at(index + 1);
            double z = (1.0 - lever) * refTrajMapZ_m.at(index)
                       + lever * refTrajMapZ_m.at(index + 1);
gsell's avatar
gsell committed
366

367 368 369 370 371
            // Adjust position relative to pre-computed central trajectory map.
            Vector_t X(0.0, 0.0, 0.0);
            X(0) = R(0) + x;
            X(1) = R(1) + y;
            X(2) = z;
gsell's avatar
gsell committed
372

373 374 375
            Vector_t tempE(0.0, 0.0, 0.0);
            Vector_t tempB(0.0, 0.0, 0.0);
            Vector_t XInBendFrame = RotateToBendFrame(X);
gsell's avatar
gsell committed
376

377 378 379 380 381 382
            /*
             * Add in transverse bend displacements. (ds is already
             * accounted for.)
             */
            XInBendFrame(0) -= dx_m;
            XInBendFrame(1) -= dy_m;
gsell's avatar
gsell committed
383

384 385
            CalculateMapField(XInBendFrame, tempE, tempB);
            tempB = fieldAmplitude_m * RotateOutOfBendFrame(tempB);
gsell's avatar
gsell committed
386

387 388 389
            B(0) += tempB(0);
            B(1) += tempB(1);
            B(2) += tempB(2);
gsell's avatar
gsell committed
390

391 392
        }
    }
Steve Russell's avatar
Steve Russell committed
393

394
    return false;
gsell's avatar
gsell committed
395

396
}
gsell's avatar
gsell committed
397

398 399 400
bool RBend::bends() const {
    return true;
}
gsell's avatar
gsell committed
401

402 403 404
void RBend::finalise() {
    online_m = false;
}
gsell's avatar
gsell committed
405

406 407 408 409
void RBend::getDimensions(double &sBegin, double &sEnd) const {
    sBegin = startField_m;
    sEnd = endField_m;
}
gsell's avatar
gsell committed
410

411 412 413 414
const std::string &RBend::getType() const {
    static const std::string type("RBend");
    return type;
}
gsell's avatar
gsell committed
415

416
void RBend::initialise(PartBunch *bunch, double &startField, double &endField, const double &scaleFactor) {
gsell's avatar
gsell committed
417

418
    Inform msg("RBend ");
gsell's avatar
gsell committed
419

420
    if(InitializeFieldMap(msg)) {
gsell's avatar
gsell committed
421

422 423 424 425 426 427 428 429
        SetupPusher(bunch);
        ReadFieldMap(msg);
        SetupBendGeometry(msg, startField, endField);
        double bendAngleX = 0.0;
        double bendAngleY = 0.0;
        CalculateRefTrajectory(bendAngleX, bendAngleY);
        recalcRefTraj_m = true;
        Print(msg, bendAngleX, bendAngleY);
gsell's avatar
gsell committed
430

431
        // Pass start and end of field to calling function.
Steve Russell's avatar
Steve Russell committed
432 433
        startField = startField_m;
        endField = endField_m;
gsell's avatar
gsell committed
434 435

    } else {
436 437 438 439
        msg << " There is something wrong with your field map \""
            << fileName_m
            << "\"";
        endField = startField - 1.0e-3;
gsell's avatar
gsell committed
440 441 442
    }
}

443 444
double RBend::GetBendAngle() const {
    return angle_m;
gsell's avatar
gsell committed
445 446
}

447 448
double RBend::GetBendRadius() const {
    return designRadius_m;
gsell's avatar
gsell committed
449 450
}

451 452
double RBend::GetEffectiveCenter() const {
    return elementEdge_m + designRadius_m * angle_m / 2.0;
Steve Russell's avatar
Steve Russell committed
453 454
}

455 456
double RBend::GetEffectiveLength() const {
    return designRadius_m * angle_m;
gsell's avatar
gsell committed
457 458
}

459 460
std::string RBend::GetFieldMapFN() const {
    return fileName_m;
Steve Russell's avatar
Steve Russell committed
461 462
}

463 464
double RBend::GetStartElement() const {
    return elementEdge_m;
Steve Russell's avatar
Steve Russell committed
465 466
}

467 468
void RBend::SetAperture(double aperture) {
    aperture_m = std::abs(aperture);
gsell's avatar
gsell committed
469 470
}

471 472
void RBend::SetBendAngle(double angle) {
    angle_m = angle;
gsell's avatar
gsell committed
473 474
}

475 476
void RBend::SetBeta(double beta) {
    Orientation_m(1) = beta;
gsell's avatar
gsell committed
477 478
}

479 480
void RBend::SetDesignEnergy(double energy) {
    designEnergy_m = std::abs(energy);
gsell's avatar
gsell committed
481 482
}

483 484
void RBend::SetEntranceAngle(double entranceAngle) {
    entranceAngle_m = entranceAngle;
gsell's avatar
gsell committed
485 486
}

487 488 489
void RBend::SetFieldAmplitude(double k0, double k0s) {
    bY_m = k0;
    bX_m = k0s;
gsell's avatar
gsell committed
490 491
}

492 493
void RBend::SetFieldMapFN(std::string fileName) {
    fileName_m = fileName;
gsell's avatar
gsell committed
494 495
}

496 497
void RBend::SetFullGap(double gap) {
    gap_m = gap;
gsell's avatar
gsell committed
498 499
}

500 501
void RBend::SetK1(double k1) {
    gradient_m = k1;
gsell's avatar
gsell committed
502 503
}

504 505 506
void RBend::SetLength(double length) {
    length_m = length;
}
Steve Russell's avatar
Steve Russell committed
507

508 509 510
void RBend::SetRotationAboutZ(double rotation) {
    Orientation_m(2) = rotation;
}
Steve Russell's avatar
Steve Russell committed
511

512
void RBend::AdjustFringeFields(double ratio) {
Steve Russell's avatar
Steve Russell committed
513

514 515
    double delta = std::abs(entranceParameter1_m - entranceParameter2_m);
    entranceParameter1_m = entranceParameter2_m - delta * ratio;
Steve Russell's avatar
Steve Russell committed
516

517 518
    delta = std::abs(entranceParameter2_m - entranceParameter3_m);
    entranceParameter3_m = entranceParameter2_m + delta * ratio;
Steve Russell's avatar
Steve Russell committed
519

520 521
    delta = std::abs(exitParameter1_m - exitParameter2_m);
    exitParameter1_m = exitParameter2_m - delta * ratio;
Steve Russell's avatar
Steve Russell committed
522

523 524
    delta = std::abs(exitParameter2_m - exitParameter3_m);
    exitParameter3_m = exitParameter2_m + delta * ratio;
Steve Russell's avatar
Steve Russell committed
525 526 527

}

528
double RBend::CalculateBendAngle() {
Steve Russell's avatar
Steve Russell committed
529 530

    const double mass = RefPartBunch_m->getM();
531
    const double gamma = designEnergy_m / mass + 1.0;
Steve Russell's avatar
Steve Russell committed
532
    const double betaGamma = sqrt(pow(gamma, 2.0) - 1.0);
533
    const double beta = betaGamma / gamma;
Steve Russell's avatar
Steve Russell committed
534 535 536
    const double deltaT = RefPartBunch_m->getdT();

    // Integrate through field for initial angle.
537 538 539 540
    Vector_t X(0.0, 0.0, startField_m - elementEdge_m);
    Vector_t P(0.0, 0.0, betaGamma);
    double deltaS = 0.0;
    double bendLength = endField_m - startField_m;
Steve Russell's avatar
Steve Russell committed
541

542
    while(deltaS < bendLength) {
Steve Russell's avatar
Steve Russell committed
543 544 545 546 547

        X /= Vector_t(Physics::c * deltaT);
        pusher_m.push(X, P, deltaT);
        X *= Vector_t(Physics::c * deltaT);

548 549 550 551
        Vector_t eField(0.0, 0.0, 0.0);
        Vector_t bField(0.0, 0.0, 0.0);
        CalculateMapField(X, eField, bField);
        bField = fieldAmplitude_m * bField;
Steve Russell's avatar
Steve Russell committed
552 553

        X /= Vector_t(Physics::c * deltaT);
554
        pusher_m.kick(X, P, eField, bField, deltaT);
Steve Russell's avatar
Steve Russell committed
555 556 557 558

        pusher_m.push(X, P, deltaT);
        X *= Vector_t(Physics::c * deltaT);

559 560
        deltaS += deltaT * beta * Physics::c;

Steve Russell's avatar
Steve Russell committed
561 562
    }

563
    double angle =  -atan2(P(0), P(2));
Steve Russell's avatar
Steve Russell committed
564 565

    return angle;
566

Steve Russell's avatar
Steve Russell committed
567 568
}

569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
void RBend::CalcCentralField(Vector_t R,
                             double deltaX,
                             double angle,
                             Vector_t &B) {

    B(0) = -gradient_m * R(1) * cos(angle) / designRadius_m;
    B(1) = 1.0 - gradient_m * deltaX / designRadius_m;
    B(2) = -gradient_m * R(1) * sin(angle) / designRadius_m;

}

void RBend::CalcEngeFunction(double zNormalized,
                             std::vector<double> engeCoeff,
                             int polyOrder,
                             double &engeFunc,
                             double &engeFuncDeriv,
                             double &engeFuncSecDeriv) {

    double expSum = 0.0;
    double expSumDeriv = 0.0;
    double expSumSecDeriv = 0.0;

    if(polyOrder >= 2) {

        expSum = engeCoeff.at(0)
                 + engeCoeff.at(1) * zNormalized;
        expSumDeriv = engeCoeff.at(1);

        for(int index = 2; index <= polyOrder; index++) {
            expSum += engeCoeff.at(index) * pow(zNormalized, index);
            expSumDeriv += index * engeCoeff.at(index)
                           * pow(zNormalized, index - 1);
            expSumSecDeriv += index * (index - 1) * engeCoeff.at(index)
                              * pow(zNormalized, index - 2);
        }

    } else if(polyOrder == 1) {

        expSum = engeCoeff.at(0)
                 + engeCoeff.at(1) * zNormalized;
        expSumDeriv = engeCoeff.at(1);

    } else
        expSum = engeCoeff.at(0);

    expSumDeriv /= gap_m;
    expSumSecDeriv /= pow(gap_m, 2.0);

    double engeExp = exp(expSum);
    double engeExpDeriv = expSumDeriv * engeExp;
    double engeExpSecDeriv = (expSumSecDeriv + pow(expSumDeriv, 2.0)) * engeExp;

    engeFunc = 1.0 / (1.0 + engeExp);
    if(engeFunc > 1.0e-30) {
        engeFuncDeriv = -engeExpDeriv * pow(engeFunc, 2.0);
        engeFuncSecDeriv = -engeExpSecDeriv * pow(engeFunc, 2.0)
                           + 2.0 * pow(engeExpDeriv, 2.0) * pow(engeFunc, 3.0);
    } else {
        engeFunc = 0.0;
        engeFuncDeriv = 0.0;
        engeFuncSecDeriv = 0.0;
    }

}

void RBend::CalcEntranceFringeField(Vector_t REntrance,
                                    double deltaX,
                                    Vector_t &B) {

    double zNormalized = -REntrance(2) / gap_m;
    double engeFunc = 0.0;
    double engeFuncDeriv = 0.0;
    double engeFuncSecDeriv = 0.0;

    CalcEngeFunction(zNormalized,
                     engeCoeffsEntry_m,
                     polyOrderEntry_m,
                     engeFunc,
                     engeFuncDeriv,
                     engeFuncSecDeriv);

    double bXEntrance = -(engeFunc - (engeFuncSecDeriv / 2.0) * pow(REntrance(1), 2.0))
                        * gradient_m * REntrance(1) / designRadius_m;
    double bYEntrance = engeFunc - (engeFuncSecDeriv / 2.0) * pow(REntrance(1), 2.0)
                        * (1.0 - gradient_m * deltaX / designRadius_m);
    double bZEntrance = -engeFuncDeriv * REntrance(1);

    B(0) = bXEntrance * cosEntranceAngle_m - bZEntrance * sinEntranceAngle_m;
    B(1) = bYEntrance;
    B(2) = bXEntrance * sinEntranceAngle_m + bZEntrance * cosEntranceAngle_m;

}

void RBend::CalcExitFringeField(Vector_t RExit, double deltaX, Vector_t &B) {

    double zNormalized = RExit(2) / gap_m;
    double engeFunc = 0.0;
    double engeFuncDeriv = 0.0;
    double engeFuncSecDeriv = 0.0;
    CalcEngeFunction(zNormalized,
                     engeCoeffsExit_m,
                     polyOrderExit_m,
                     engeFunc,
                     engeFuncDeriv,
                     engeFuncSecDeriv);

    double bXExit = -(engeFunc - (engeFuncSecDeriv / 2.0) * pow(RExit(1), 2.0))
                    * gradient_m * RExit(1) / designRadius_m;
    double bYExit = engeFunc - (engeFuncSecDeriv / 2.0) * pow(RExit(1), 2.0)
                    * (1.0 - gradient_m * deltaX / designRadius_m);
    double bZExit = engeFuncDeriv * RExit(1);

    B(0) = bXExit * cosExitAngle_m - bZExit * sinExitAngle_m;
    B(1) = bYExit;
    B(2) = bXExit * sinExitAngle_m + bZExit * cosExitAngle_m;

}

void RBend::CalculateMapField(Vector_t R, Vector_t &E, Vector_t &B) {

    E = Vector_t(0.0);
    B = Vector_t(0.0);

    //    Vector_t REntrance(0.0, 0.0, 0.0);
    //    Vector_t RExit(0.0, 0.0, 0.0);
    //    if (IsPositionInEntranceField(R, REntrance)) {
    //        CalcEntranceFringeField(REntrance, 0.0, B);
    //    } else if (IsPositionInExitField(R, RExit)) {
    //        CalcExitFringeField(RExit, 0.0, B);
    //    } else {
    //        CalcCentralField(R, 0.0, 0.0, B);
    //    }

    double deltaXEntrance = 0.0;
    double deltaXExit = 0.0;
    bool inEntranceRegion = InMagnetEntranceRegion(R, deltaXEntrance);
    bool inExitRegion = InMagnetExitRegion(R, deltaXExit);

    if(!inEntranceRegion && !inExitRegion) {

        double deltaX = 0.0;
        double angle = 0.0;
        if(InMagnetCentralRegion(R, deltaX, angle)) {
            Vector_t REntrance(0.0, 0.0, 0.0);
            Vector_t RExit(0.0, 0.0, 0.0);
            if(IsPositionInEntranceField(R, REntrance))
                CalcEntranceFringeField(REntrance, deltaX, B);
            else if(IsPositionInExitField(R, RExit))
                CalcExitFringeField(RExit, deltaX, B);
            else
                CalcCentralField(R, deltaX, angle, B);

        }

    } else if(inEntranceRegion && !inExitRegion) {

        Vector_t REntrance(0.0, 0.0, 0.0);
        if(IsPositionInEntranceField(R, REntrance)) {
            CalcEntranceFringeField(REntrance, deltaXEntrance, B);
        } else if(REntrance(2) > 0.0)
            CalcCentralField(R, deltaXEntrance, 0.0, B);

    } else if(!inEntranceRegion && inExitRegion) {

        Vector_t RExit(0.0, 0.0, 0.0);
        if(IsPositionInExitField(R, RExit)) {
            CalcExitFringeField(RExit, deltaXExit, B);
        } else if(RExit(2) < 0.0)
            CalcCentralField(R, deltaXExit, angle_m, B);

    } else if(inEntranceRegion && inExitRegion) {

        /*
         * This is an unusual condition and should only happen with
         * a sector magnet that bends more than 180 degrees. Here, we
         * have the possibility that the particle sees both the
         * entrance and exit fringe fields.
         */
        Vector_t BEntrance(0.0, 0.0, 0.0);
        Vector_t REntrance(0.0, 0.0, 0.0);
        if(IsPositionInEntranceField(R, REntrance))
            CalcEntranceFringeField(REntrance, deltaXEntrance, BEntrance);

        Vector_t BExit(0.0, 0.0, 0.0);
        Vector_t RExit(0.0, 0.0, 0.0);
        if(IsPositionInExitField(R, RExit))
            CalcExitFringeField(RExit, deltaXExit, BExit);

        B(0) = BEntrance(0) + BExit(0);
        B(1) = BEntrance(1) + BExit(1);
        B(2) = BEntrance(2) + BExit(2);

    }
}

void RBend::CalculateRefTrajectory(double &angleX, double &angleY) {
Steve Russell's avatar
Steve Russell committed
765 766

    const double mass = RefPartBunch_m->getM();
767 768
    const double gamma = designEnergy_m / mass + 1.;
    const double betaGamma = sqrt(gamma * gamma - 1.);
Steve Russell's avatar
Steve Russell committed
769 770
    const double dt = RefPartBunch_m->getdT();

771 772
    Vector_t X(0.0, 0.0, startField_m - elementEdge_m);
    Vector_t P(0.0, 0.0, betaGamma);
Steve Russell's avatar
Steve Russell committed
773

774 775 776 777 778 779
    if(!refTrajMapX_m.empty())
        refTrajMapX_m.clear();
    if(!refTrajMapY_m.empty())
        refTrajMapY_m.clear();
    if(!refTrajMapZ_m.empty())
        refTrajMapZ_m.clear();
Steve Russell's avatar
Steve Russell committed
780

781 782 783
    refTrajMapX_m.push_back(X(0));
    refTrajMapY_m.push_back(X(1));
    refTrajMapZ_m.push_back(X(2));
Steve Russell's avatar
Steve Russell committed
784

785 786 787 788 789
    refTrajMapStepSize_m = betaGamma / gamma * Physics::c * dt;
    double deltaS = 0.0;
    double bendLength = endField_m - startField_m;

    while(deltaS < bendLength) {
Steve Russell's avatar
Steve Russell committed
790 791 792 793 794

        X /= Vector_t(Physics::c * dt);
        pusher_m.push(X, P, dt);
        X *= Vector_t(Physics::c * dt);

795 796 797
        Vector_t eField(0.0, 0.0, 0.0);
        Vector_t bField(0.0, 0.0, 0.0);
        Vector_t XInBendFrame = RotateToBendFrame(X);
Steve Russell's avatar
Steve Russell committed
798

799 800 801 802 803 804 805 806 807
        /*
         * Add in transverse bend displacements. (ds is already
         * accounted for.)
         */
        XInBendFrame(0) -= dx_m;
        XInBendFrame(1) -= dy_m;

        CalculateMapField(XInBendFrame, eField, bField);
        bField = fieldAmplitude_m * RotateOutOfBendFrame(bField);
Steve Russell's avatar
Steve Russell committed
808 809

        X /= Vector_t(Physics::c * dt);
810 811
        pusher_m.kick(X, P, eField, bField, dt);

Steve Russell's avatar
Steve Russell committed
812 813 814
        pusher_m.push(X, P, dt);
        X *= Vector_t(Physics::c * dt);

815 816 817 818 819
        refTrajMapX_m.push_back(X(0));
        refTrajMapY_m.push_back(X(1));
        refTrajMapZ_m.push_back(X(2));

        deltaS += refTrajMapStepSize_m;
Steve Russell's avatar
Steve Russell committed
820 821 822

    }

823
    refTrajMapSize_m = refTrajMapX_m.size();
Steve Russell's avatar
Steve Russell committed
824

825 826 827 828 829
    if(Orientation_m(2) == Physics::pi / 2.0
       || Orientation_m(2) == 3.0 * Physics::pi / 2.0)
        angleX = 0.0;
    else
        angleX = -atan2(P(0), P(2));
Steve Russell's avatar
Steve Russell committed
830

831 832 833 834 835
    if(Orientation_m(2) == 0.0
       || Orientation_m(2) == Physics::pi)
        angleY = 0.0;
    else
        angleY = atan2(P(1), P(2));
Steve Russell's avatar
Steve Russell committed
836 837

}
gsell's avatar
gsell committed
838

839 840 841 842 843 844 845 846 847 848 849 850 851
double RBend::EstimateFieldAdjustmentStep(double actualBendAngle,
        double mass,
        double betaGamma) {

    double amplitude1 = fieldAmplitude_m;
    double bendAngle1 = actualBendAngle;

    // Estimate field adjustment step.
    double effectiveLength = angle_m * designRadius_m;
    double fieldStep = (angle_m - bendAngle1) * betaGamma * mass / (2.0 * effectiveLength * Physics::c);
    if(pow(fieldAmplitude_m * effectiveLength * Physics::c / (betaGamma * mass), 2.0) < 1.0)
        fieldStep = (angle_m - bendAngle1) * betaGamma * mass / (2.0 * effectiveLength * Physics::c)
                    * std::sqrt(1.0 - pow(fieldAmplitude_m * effectiveLength * Physics::c / (betaGamma * mass), 2.0));
gsell's avatar
gsell committed
852

853
    fieldStep *= amplitude1 / std::abs(amplitude1);
gsell's avatar
gsell committed
854

855
    return fieldStep;
gsell's avatar
gsell committed
856

857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
}

void RBend::FindBendEffectiveLength(double startField, double endField) {

    /*
     * Use an iterative procedure to set the width of the
     * default field map for the defined field amplitude
     * and bend angle.
     */
    SetEngeOriginDelta(0.0);
    SetFieldCalcParam(false);
    SetFieldBoundaries(startField, endField);

    double actualBendAngle = CalculateBendAngle();
    double error = std::abs(actualBendAngle - angle_m);
    if(error > 1.0e-6) {

        double deltaStep = 0.0;
        if(std::abs(actualBendAngle) < std::abs(angle_m))
            deltaStep = -gap_m / 2.0;
        else
            deltaStep = gap_m / 2.0;

        double delta1 = 0.0;
        double bendAngle1 = actualBendAngle;

        double delta2 = deltaStep;
        SetEngeOriginDelta(delta2);
        SetFieldCalcParam(false);
        SetFieldBoundaries(startField, endField);
        double bendAngle2 = CalculateBendAngle();

        if(std::abs(bendAngle1) > std::abs(angle_m)) {
            while(std::abs(bendAngle2) > std::abs(angle_m)) {
                delta2 += deltaStep;
                SetEngeOriginDelta(delta2);
                SetFieldCalcParam(false);
                SetFieldBoundaries(startField, endField);
                bendAngle2 = CalculateBendAngle();
            }
        } else {
            while(std::abs(bendAngle2) < std::abs(angle_m)) {
                delta2 += deltaStep;
                SetEngeOriginDelta(delta2);
                SetFieldCalcParam(false);
                SetFieldBoundaries(startField, endField);
                bendAngle2 = CalculateBendAngle();
            }
        }
gsell's avatar
gsell committed
906

907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
        // Now we should have the proper field map width bracketed.
        unsigned int iterations = 1;
        double delta = 0.0;
        error = std::abs(actualBendAngle - angle_m);
        while(error > 1.0e-6 && iterations < 100) {

            delta = (delta1 + delta2) / 2.0;
            SetEngeOriginDelta(delta);
            SetFieldCalcParam(false);
            SetFieldBoundaries(startField, endField);
            double newBendAngle = CalculateBendAngle();

            error = std::abs(newBendAngle - angle_m);

            if(error > 1.0e-6) {

                if(bendAngle1 - angle_m < 0.0) {

                    if(newBendAngle - angle_m < 0.0) {
                        bendAngle1 = newBendAngle;
                        delta1 = delta;
                    } else {
                        bendAngle2 = newBendAngle;
                        delta2 = delta;
                    }

                } else {

                    if(newBendAngle - angle_m < 0.0) {
                        bendAngle2 = newBendAngle;
                        delta2 = delta;
                    } else {
                        bendAngle1 = newBendAngle;
                        delta1 = delta;
                    }
                }
            }
            iterations++;
        }
    }
}
gsell's avatar
gsell committed
948

949 950 951 952 953 954 955 956 957 958 959 960 961
bool RBend::FindBendLength(Inform &msg,
                           double &bendLength,
                           bool &bendLengthFromMap) {

    /*
     * Find bend length. If this was not set by the user using the
     * L (length) attribute, infer it from the field map.
     */
    bendLength = length_m;
    if(bendLength > 0.0) {
        bendLengthFromMap = false;
        return true;
    } else {
gsell's avatar
gsell committed
962

963 964
        if(bendLength == 0.0)
            bendLength = exitParameter2_m - entranceParameter2_m;
gsell's avatar
gsell committed
965 966


967 968 969 970 971 972 973
        if(bendLength <= 0.0) {
            msg << "Magnet length inferred from field map is less than or equal"
                " to zero. Check your bend magnet input."
                << endl;
            return false;
        } else
            return true;
gsell's avatar
gsell committed
974 975 976 977

    }
}

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
void RBend::FindBendStrength(double mass,
                             double gamma,
                             double betaGamma,
                             double charge) {

    /*
     * Use an iterative procedure to set the magnet field amplitude
     * for the defined bend angle.
     */
    double actualBendAngle = CalculateBendAngle();
    double fieldStep = EstimateFieldAdjustmentStep(actualBendAngle,
                       mass,
                       betaGamma);
    double amplitude1 = fieldAmplitude_m;
    double bendAngle1 = actualBendAngle;

    double amplitude2 = fieldAmplitude_m + fieldStep;
    fieldAmplitude_m = amplitude2;
    double bendAngle2 = CalculateBendAngle();

    if(std::abs(bendAngle1) > std::abs(angle_m)) {
        while(std::abs(bendAngle2) > std::abs(angle_m)) {
            amplitude2 += fieldStep;
            fieldAmplitude_m = amplitude2;
            bendAngle2 = CalculateBendAngle();
        }
    } else {
        while(std::abs(bendAngle2) < std::abs(angle_m)) {
            amplitude2 += fieldStep;
            fieldAmplitude_m = amplitude2;
            bendAngle2 = CalculateBendAngle();
        }
    }
gsell's avatar
gsell committed
1011

1012 1013 1014 1015
    // Now we should have the proper field amplitude bracketed.
    unsigned int iterations = 1;
    double error = std::abs(actualBendAngle - angle_m);
    while(error > 1.0e-6 && iterations < 100) {
gsell's avatar
gsell committed
1016

1017 1018
        fieldAmplitude_m = (amplitude1 + amplitude2) / 2.0;
        double newBendAngle = CalculateBendAngle();
gsell's avatar
gsell committed
1019

1020
        error = std::abs(newBendAngle - angle_m);
gsell's avatar
gsell committed
1021

1022
        if(error > 1.0e-6) {
gsell's avatar
gsell committed
1023

1024
            if(bendAngle1 - angle_m < 0.0) {
gsell's avatar
gsell committed
1025

1026 1027 1028 1029 1030 1031 1032
                if(newBendAngle - angle_m < 0.0) {
                    bendAngle1 = newBendAngle;
                    amplitude1 = fieldAmplitude_m;
                } else {
                    bendAngle2 = newBendAngle;
                    amplitude2 = fieldAmplitude_m;
                }
gsell's avatar
gsell committed
1033

1034
            } else {
gsell's avatar
gsell committed
1035

1036 1037 1038 1039 1040 1041 1042 1043 1044
                if(newBendAngle - angle_m < 0.0) {
                    bendAngle2 = newBendAngle;
                    amplitude2 = fieldAmplitude_m;
                } else {
                    bendAngle1 = newBendAngle;
                    amplitude1 = fieldAmplitude_m;
                }
            }
        }
Steve Russell's avatar
Steve Russell committed
1045
        iterations++;
1046 1047
    }
}
gsell's avatar
gsell committed
1048

1049
bool RBend::FindIdealBendParameters(double bendLength) {
gsell's avatar
gsell committed
1050

1051 1052 1053 1054
    double refMass = RefPartBunch_m->getM();
    double refGamma = designEnergy_m / refMass + 1.0;
    double refBetaGamma = sqrt(pow(refGamma, 2.0) - 1.0);
    double refCharge = RefPartBunch_m->getQ();
gsell's avatar
gsell committed
1055

1056
    if(angle_m != 0.0) {
gsell's avatar
gsell committed
1057

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
        if(angle_m < 0.0) {
            // Negative angle is a positive bend rotated 180 degrees.
            angle_m = std::abs(angle_m);
            gradient_m *= -1.0;
            Orientation_m(2) += Physics::pi;
        }
        designRadius_m = bendLength
                         / (sin(angle_m - entranceAngle_m) + sin(entranceAngle_m));
        fieldAmplitude_m = (refCharge / std::abs(refCharge))
                           * refBetaGamma * refMass
                           / (Physics::c * designRadius_m);
        exitAngle_m = angle_m - entranceAngle_m;

        return true;

1073
    } else if(bX_m == 0.0) {
1074 1075

        // Negative angle is a positive bend rotated 180 degrees.
1076 1077
        if((refCharge > 0.0 && bY_m < 0.0)
           || (refCharge < 0.0 && bY_m > 0.0)) {
1078 1079 1080 1081
            gradient_m *= -1.0;
            Orientation_m(2) += Physics::pi;
        }

1082
        fieldAmplitude_m = refCharge * std::abs(bY_m / refCharge);
1083 1084 1085 1086 1087 1088 1089
        designRadius_m = std::abs(refBetaGamma * refMass / (Physics::c * fieldAmplitude_m));
        double angle = asin(bendLength / designRadius_m - sin(entranceAngle_m));
        angle_m = angle + entranceAngle_m;
        exitAngle_m = angle_m - entranceAngle_m;

        return false;

1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    } else {

        Orientation_m(2) += atan2(bX_m, bY_m);
        if(refCharge < 0.0) {
            gradient_m *= -1.0;
            Orientation_m(2) -= Physics::pi;
        }

        fieldAmplitude_m = refCharge
                           * std::abs(sqrt(pow(bY_m, 2.0) + pow(bX_m, 2.0))
                                      / refCharge);
        designRadius_m = std::abs(refBetaGamma * refMass / (Physics::c * fieldAmplitude_m));
        double angle = asin(bendLength / designRadius_m - sin(entranceAngle_m));
        angle_m = angle + entranceAngle_m;
        exitAngle_m = angle_m - entranceAngle_m;

        return false;
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
    }
}

void RBend::FindReferenceExitOrigin(double &x, double &z) {

    /*
      * Find x,z coordinates of reference trajectory as it passes exit edge
      * of the bend magnet. This assumes an entrance position of (x,z) = (0,0).
      */
    if(angle_m <= Physics::pi / 2.0) {
        x = - designRadius_m * (1.0 - std::cos(angle_m));
        z = designRadius_m * std::sin(angle_m);
    } else if(angle_m <= Physics::pi) {
        x = -designRadius_m * (1.0 + std::sin(angle_m - Physics::pi / 2.0));
        z = designRadius_m * std::cos(angle_m - Physics::pi / 2.0);
    } else if(angle_m <= 3.0 * Physics::pi / 2.0) {
        x = -designRadius_m * (2.0 - std::cos(angle_m - Physics::pi));
        z = -designRadius_m * std::sin(angle_m - Physics::pi);
    } else {
        x = -designRadius_m * (1.0 - std::cos(angle_m - 3.0 * Physics::pi / 2.0));
        z = -designRadius_m * std::sin(angle_m - 3.0 * Physics::pi / 2.0);
gsell's avatar
gsell committed
1128
    }
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
}

bool RBend::InitializeFieldMap(Inform &msg) {

    fieldmap_m = Fieldmap::getFieldmap(fileName_m, fast_m);

    if(fieldmap_m != NULL) {
        if(fileName_m != "1DPROFILE1-DEFAULT")
            return true;
        else
            return SetupDefaultFieldMap(msg);

    } else
        return false;

}

bool RBend::InMagnetCentralRegion(Vector_t R, double &deltaX, double &angle) {

    deltaX = sqrt(pow(R(2), 2.0) + pow(R(0) + designRadius_m, 2.0)) - designRadius_m;
    if(std::abs(deltaX) <= aperture_m / 2.0) {

        angle = atan2(R(2), R(0) + designRadius_m);
        return true;

    } else
        return false;

}

bool RBend::InMagnetEntranceRegion(Vector_t R, double &deltaX) {

1161
    if(std::abs(R(0)) <= aperture_m / 2.0) {
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247

        Vector_t RTransformed(0.0, R(1), 0.0);
        RTransformed(0) = (R(0) - xOriginEngeEntry_m) * cosEntranceAngle_m
                          + (R(2) - zOriginEngeEntry_m) * sinEntranceAngle_m;
        RTransformed(2) = -(R(0) - xOriginEngeEntry_m) * sinEntranceAngle_m
                          + (R(2) - zOriginEngeEntry_m) * cosEntranceAngle_m;

        if(RTransformed(2) <= 0.0) {
            deltaX = R(0);
            return true;
        } else
            return false;

    } else
        return false;

}

bool RBend::InMagnetExitRegion(Vector_t R, double &deltaX) {

    Vector_t RTransformed(0.0, R(1), 0.0);
    RTransformed(0) = (R(0) - xExit_m) * cosExitAngle_m
                      + (R(2) - zExit_m) * sinExitAngle_m;
    RTransformed(2) = -(R(0) - xExit_m) * sinExitAngle_m
                      + (R(2) - zExit_m) * cosExitAngle_m;

    if(RTransformed(2) >= 0.0) {

        deltaX = (R(0) - xExit_m) * cos(angle_m)
                 + (R(2) - zExit_m) * sin(angle_m);
        if(std::abs(deltaX) <= aperture_m / 2.0)
            return true;
        else
            return false;

    } else
        return false;
}

bool RBend::IsPositionInEntranceField(Vector_t R, Vector_t &REntrance) {

    REntrance(1) = R(1);

    REntrance(0) = (R(0) - xOriginEngeEntry_m) * cosEntranceAngle_m
                   + (R(2) - zOriginEngeEntry_m) * sinEntranceAngle_m;
    REntrance(2) = -(R(0) - xOriginEngeEntry_m) * sinEntranceAngle_m
                   + (R(2) - zOriginEngeEntry_m) * cosEntranceAngle_m;

    if(REntrance(2) >= -deltaBeginEntry_m && REntrance(2) <= deltaEndEntry_m)
        return true;
    else
        return false;
}

bool RBend::IsPositionInExitField(Vector_t R, Vector_t &RExit) {

    RExit(1) = R(1);

    RExit(0) = (R(0) - xOriginEngeExit_m) * cosExitAngle_m
               + (R(2) - zOriginEngeExit_m) * sinExitAngle_m;
    RExit(2) = -(R(0) - xOriginEngeExit_m) * sinExitAngle_m
               + (R(2) - zOriginEngeExit_m) * cosExitAngle_m;

    if(RExit(2) >= -deltaBeginExit_m && RExit(2) <= deltaEndExit_m)
        return true;
    else
        return false;

}

void RBend::Print(Inform &msg, double bendAngleX, double bendAngleY) {

    msg << endl
        << "Start of field map:      "
        << startField_m
        << " m (in s coordinates)"
        << endl;
    msg << "End of field map:        "
        << endField_m
        << " m (in s coordinates)"
        << endl;
    msg << "Entrance edge of magnet: "
        << elementEdge_m
        << " m (in s coordinates)"
        << endl;
    msg << endl
1248
        << "Reference Trajectory Properties"
1249
        << endl
1250
        << "==============================="
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
        << endl << endl;
    msg << "Bend angle magnitude:    "
        << angle_m
        << " rad ("
        << angle_m * 180.0 / Physics::pi
        << " degrees)"
        << endl;
    msg << "Entrance edge angle:     "
        << entranceAngle_m
        << " rad ("
        << entranceAngle_m * 180.0 / Physics::pi
        << " degrees)"
        << endl;
    msg << "Exit edge angle:         "
        << exitAngle_m
        << " rad ("
        << exitAngle_m * 180.0 / Physics::pi
        << " degrees)"
        << endl;
    msg << "Bend design radius:      "
        << designRadius_m
        << " m"
        << endl;
1274 1275 1276 1277
    msg << "Bend design energy:      "
        << designEnergy_m
        << " eV"
        << endl;
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
    msg << endl
        << "Bend Field and Rotation Properties"
        << endl
        << "=================================="
        << endl << endl;
    msg << "Field amplitude:         "
        << fieldAmplitude_m
        << " T"
        << endl;
    msg << "Field index (gradient):  "
        << gradient_m
        << " m^-1"
        << endl;
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
    msg << "Rotation about x axis:   "
        << Orientation_m(1)
        << " rad ("
        << Orientation_m(1) * 180.0 / Physics::pi
        << " degrees)"