OpalBeamline.cpp 19.5 KB
Newer Older
gsell's avatar
gsell committed
1
#include "Elements/OpalBeamline.h"
2
#include "Utilities/OpalException.h"
gsell's avatar
gsell committed
3 4 5 6 7 8 9 10

using namespace std;

extern Inform *gmsg;

OpalBeamline::OpalBeamline():
    sections_m(),
    elements_m(),
11 12 13
    prepared_m(false),
    online_secs_m(NULL)
{
gsell's avatar
gsell committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    online_sections_m = new int[5 * Ippl::getNodes()];
    // This is needed to communicate across the nodes which sections are online. We should allocate memory to store
    // (# of nodes) * (max # of sections which are online) integers.
    // While we know the first number the second depends on many things. It is *ASSUMED* that the bunch isn't ever
    // in more than 5 sections at the same time. Though it might be that this assumption is wrong. Then the
    // the communication between the nodes has to be adjusted to a scheme which works always or by using any higher
    // number.
}

OpalBeamline::~OpalBeamline() {
    elements_m.clear();
    sections_m.clear();
    delete[] online_sections_m;
    if(online_secs_m)
        delete[] online_secs_m;
}

CompVec OpalBeamline::dummy_list_m = CompVec();
OpalSection OpalBeamline::dummy_section_m = OpalSection(dummy_list_m, 0., 0.);

kraus's avatar
kraus committed
34
CompVec &OpalBeamline::getPredecessors(std::shared_ptr<const Component> element) {
gsell's avatar
gsell committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    size_t index;
    bool found = false;
    for(index = 0; index < sections_m.size(); ++ index) {
        if(sections_m[index].find(element)) {
            found = true;
            break;
        }
    }

    if(found && index > 0) {
        return sections_m[index - 1].getElements();
    } else {
        if(dummy_list_m.size() != 0) {
            dummy_list_m.erase(dummy_list_m.begin(), dummy_list_m.end());
        }
        return dummy_list_m;
    }

}

kraus's avatar
kraus committed
55
CompVec &OpalBeamline::getSuccessors(std::shared_ptr<const Component> element) {
gsell's avatar
gsell committed
56 57 58
    size_t index;
    bool found = false;

59 60 61
    if (sections_m.size() == 0)
        return dummy_list_m;

kraus's avatar
kraus committed
62 63 64
    if (element == NULL)
        return sections_m[0].getElements();

gsell's avatar
gsell committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    for(index = 0; index < sections_m.size(); ++ index) {
        if(sections_m[index].find(element)) {
            found = true;
            break;
        }
    }

    if(found && index + 1 < sections_m.size()) {
        return sections_m[index + 1].getElements();
    } else {
        if(dummy_list_m.size() != 0) {
            dummy_list_m.erase(dummy_list_m.begin(), dummy_list_m.end());
        }
        return dummy_list_m;
    }
}

82 83
OpalSection &OpalBeamline::getSectionAt(const Vector_t &pos, long &sectionIndex) {
    if(sectionIndex >= (long) sections_m.size()) sectionIndex = static_cast<long>(sections_m.size()) - 1;
gsell's avatar
gsell committed
84 85 86 87

    if(prepared_m) {
        if(pos(2) < sections_m[0].getStart(pos(0), pos(1))) return dummy_section_m;
        if(pos(2) > sections_m.back().getEnd(pos(0), pos(1))) {
88
            sectionIndex = BEAMLINE_EOL;
gsell's avatar
gsell committed
89 90
            return dummy_section_m;
        }
91
        if(sectionIndex == -1) sectionIndex = sections_m.size() - 1;
gsell's avatar
gsell committed
92

93 94
        while(pos(2) < sections_m[sectionIndex].getStart(pos(0), pos(1))) --sectionIndex;
        while(pos(2) > sections_m[sectionIndex].getEnd(pos(0), pos(1))) ++sectionIndex;
gsell's avatar
gsell committed
95

96 97
        if(pos(2) >= sections_m[sectionIndex].getStart(pos(0), pos(1)) &&
           pos(2) <= sections_m[sectionIndex].getEnd(pos(0), pos(1))) return sections_m[sectionIndex];
gsell's avatar
gsell committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

        return dummy_section_m;
    }
    *gmsg << "** WARNING *************************************\n"
          << "in OpalBeamline::getSectionAt(): section list not prepared\n"
          << "************************************************" << endl;
    return dummy_section_m;
}

OpalSection &OpalBeamline::getSection(const unsigned int &index) {
    if(index < sections_m.size()) {
        return sections_m[index];
    }

    return dummy_section_m;
}

void OpalBeamline::getSectionIndexAt(const Vector_t &pos, long &initial_guess) const {
    if(initial_guess > -1 && (size_t) initial_guess >= sections_m.size()) initial_guess = static_cast<long>(sections_m.size()) - 1;

    if(prepared_m) {
119 120
        if (sections_m.size() == 0) return;

gsell's avatar
gsell committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 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
        if(initial_guess == -1 && pos(2) > sections_m[0].getStart(pos(0), pos(1)))
            initial_guess = 0;

        if(initial_guess > -1) {
            if(pos(2) < sections_m[initial_guess].getStart(pos(0), pos(1))) {
                do {
                    -- initial_guess;
                } while(initial_guess > -1 && pos(2) < sections_m[initial_guess].getEnd(pos(0), pos(1)));
                ++ initial_guess;
            } else {
                while(initial_guess < (long) sections_m.size() && pos(2) > sections_m[initial_guess].getEnd(pos(0), pos(1))) {
                    ++ initial_guess;
                }
                if(pos(2) > sections_m.back().getEnd(pos(0), pos(1))) {
                    initial_guess = BEAMLINE_EOL;
                } else {
                    if(pos(2) < sections_m[initial_guess].getStart(pos(0), pos(1))) {
                        -- initial_guess;
                    }
                }
            }
        }
        return;
    }
    *gmsg << "** WARNING *************************************\n"
          << "in OpalBeamline::getSectionIndexAt(): section list not prepared\n"
          << "************************************************" << endl;
}

void OpalBeamline::getKFactors(const unsigned int &index, const Vector_t &pos, const long &sindex, const double &t, Vector_t &KR, Vector_t &KT) {

    if(sindex > -1 && (size_t) sindex < sections_m.size()) {
        OpalSection &section = getSection(sindex);
        setStatus(sindex, true);
        if(pos(2) >= section.getStart(pos(0), pos(1)) && pos(2) < section.getEnd(pos(0), pos(1))) {
            const CompVec &elements = section.getElements();
            for(CompVec::const_iterator elit = elements.begin(); elit != elements.end(); elit++) {
                (*elit)->addKR(index, t, KR);
                (*elit)->addKT(index, t, KT);
            }
        }
    }
}

unsigned long OpalBeamline::getFieldAt(const unsigned int &index, const Vector_t &pos, const long &sindex, const double &t, Vector_t &E, Vector_t &B) {

    /*
      If one uses static rtv has
      after the first particles enters the Collimator
      always 16384

      static unsigned int rtv = 0x00;
     */

    if(sindex >= (long) sections_m.size()) return BEAMLINE_EOL;
    if(sindex < 0) return 0x00;

    unsigned long rtv = 0x00;

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

    OpalSection &section = getSection(sindex);
    setStatus(sindex, true);
    if(pos(2) < section.getStart(pos(0), pos(1)) || pos(2) > section.getEnd(pos(0), pos(1))) return 0x00;
186

gsell's avatar
gsell committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    const CompVec &elements = section.getElements();
    for(CompVec::const_iterator elit = elements.begin(); elit != elements.end(); ++ elit) {
        if((*elit)->apply(index, t, E, B)) {
            return BEAMLINE_OOB;
        }
    }

    const Vector_t &ori = section.getOrientation();
    if(fabs(ori(0)) > 1.e-10 || fabs(ori(1)) > 1.e-10 || fabs(ori(2)) > 1.e-10) {

        // Rotate field out of the element's local coordinate system and back to lab frame.
        //
        // 1) Rotate about the x axis by angle -ori(1).
        // 2) Rotate about the y axis by angle ori(0).
        // 3) Rotate about the z axis by angle ori(3).

        // Tenzor<double, 3> rota(cos(ori(0)), 0, sin(ori(0)), 0, 1, 0, -sin(ori(0)), 0, cos(ori(0)));
        // Tenzor<double, 3> rotb(1, 0, 0, 0, cos(ori(1)), sin(ori(1)), 0, -sin(ori(1)), cos(ori(1)));
        // Tenzor<double, 3> rotc(cos(ori(2)), -sin(ori(2)), 0, sin(ori(2)), cos(ori(2)), 0, 0, 0, 1);
206

gsell's avatar
gsell committed
207 208 209 210 211
        // Tenzor<double, 3> orientation = dot(rotc, dot(rota, rotb));
        // E = dot(orientation, E);
        // B = dot(orientation, B);

        const  double sina = sin(ori(0)),
212 213 214 215 216
                      cosa = cos(ori(0)),
                      sinb = sin(ori(1)),
                      cosb = cos(ori(1)),
                      sinc = sin(ori(2)),
                      cosc = cos(ori(2));
gsell's avatar
gsell committed
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

        Vector_t temp = E;

        E(0) =  cosa * cosc * temp(0) + (-sina * sinb * cosc - cosb * sinc) * temp(1) + (sina * cosb * cosc - sinb * sinc) * temp(2);
        E(1) =  cosa * sinc * temp(0) + (-sina * sinb * sinc + cosb * cosc) * temp(1) + (sina * cosb * sinc + sinb * cosc) * temp(2);
        E(2) = -sina *        temp(0) + (-cosa * sinb) * temp(1) + (cosa * cosb) * temp(2);
        temp = B;
        B(0) =  cosa * cosc * temp(0) + (-sina * sinb * cosc - cosb * sinc) * temp(1) + (sina * cosb * cosc - sinb * sinc) * temp(2);
        B(1) =  cosa * sinc * temp(0) + (-sina * sinb * sinc + cosb * cosc) * temp(1) + (sina * cosb * sinc + sinb * cosc) * temp(2);
        B(2) = -sina *        temp(0) + (-cosa * sinb) * temp(1) + (cosa * cosb) * temp(2);

    }

    if(section.doesBend()) {
        rtv |= BEAMLINE_BEND;
    }
    if(section.hasWake()) {
        rtv |= BEAMLINE_WAKE;
    }
    if(section.hasSurfacePhysics()) {
        rtv |= BEAMLINE_SURFACEPHYSICS;
    }

    return rtv;
}

unsigned long OpalBeamline::getFieldAt(const Vector_t &pos, const Vector_t &centroid, const double &t, Vector_t &E, Vector_t &B) {
    unsigned long rtv = 0x00;

246
    long initial_guess = -1;
gsell's avatar
gsell committed
247 248 249 250 251 252 253
    B = Vector_t(0.0);
    E = Vector_t(0.0);

    OpalSection &section = getSectionAt(pos, initial_guess);
    if(!(initial_guess & BEAMLINE_EOL || initial_guess < 0)) {
        unsigned int ini_guess = initial_guess;
        setStatus(ini_guess, true);
254

gsell's avatar
gsell committed
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
        if(pos(2) >= section.getStart(pos(0), pos(1)) && pos(2) <= section.getEnd(pos(0), pos(1))) {
            const CompVec &elements = section.getElements();
            for(CompVec::const_iterator elit = elements.begin(); elit != elements.end(); ++ elit) {
                if((*elit)->apply(pos, centroid, t, E, B)) {

                    rtv |= BEAMLINE_OOB;
                }
            }
            if(! rtv & BEAMLINE_OOB) {

                const Vector_t &ori = section.getOrientation();
                if(fabs(ori(0)) > 1.e-10 || fabs(ori(1)) > 1.e-10 || fabs(ori(2)) > 1.e-10) {

                    // Rotate field out of the element's local coordinate system and back to lab frame.
                    //
                    // 1) Rotate about the x axis by angle ori(1).
                    // 2) Rotate about the y axis by angle ori(0).
                    // 3) Rotate about the z axis by angle negative ori(3).

                    const  double sina = sin(ori(0)),
                                  cosa = cos(ori(0)),
                                  sinb = sin(ori(1)),
                                  cosb = cos(ori(1)),
                                  sinc = sin(ori(2)),
                                  cosc = cos(ori(2));

                    Vector_t temp = E;

                    E(0) =  cosa * cosc * temp(0) + (-sina * sinb * cosc - cosb * sinc) * temp(1) + (sina * cosb * cosc - sinb * sinc) * temp(2);
                    E(1) =  cosa * sinc * temp(0) + (-sina * sinb * sinc + cosb * cosc) * temp(1) + (sina * cosb * sinc + sinb * cosc) * temp(2);
                    E(2) = -sina *        temp(0) + (-cosa * sinb) * temp(1) + (cosa * cosb) * temp(2);
                    temp = B;
                    B(0) =  cosa * cosc * temp(0) + (-sina * sinb * cosc - cosb * sinc) * temp(1) + (sina * cosb * cosc - sinb * sinc) * temp(2);
                    B(1) =  cosa * sinc * temp(0) + (-sina * sinb * sinc + cosb * cosc) * temp(1) + (sina * cosb * sinc + sinb * cosc) * temp(2);
                    B(2) = -sina *        temp(0) + (-cosa * sinb) * temp(1) + (cosa * cosb) * temp(2);
                }
            }
            if(section.doesBend()) {
                rtv |= BEAMLINE_BEND;
            }
            if(section.hasWake()) {
                rtv |= BEAMLINE_WAKE;
            }
            if(section.hasSurfacePhysics()) {
                rtv |= BEAMLINE_SURFACEPHYSICS;
            }

            return rtv;
        }
        return 0x00;
    } else {
        if(initial_guess > -1) {
            return BEAMLINE_EOL;
        }
        return 0x00;
    }
}

kraus's avatar
kraus committed
313
void OpalBeamline::switchElements(const double &min, const double &max, const double &kineticEnergy, const bool &nomonitors) {
gsell's avatar
gsell committed
314 315 316
    for(FieldList::iterator flit = elements_m.begin(); flit != elements_m.end(); ++ flit) {
        // don't set online monitors if the centroid of the bunch is allready inside monitor
        // or if explicitly not desired (eg during auto phasing)
317
        if(flit->getElement()->getType() == ElementBase::MONITOR) {
gsell's avatar
gsell committed
318 319 320
            double spos = (max + min) / 2.;
            if(!nomonitors && spos < (*flit).getStart()) {
                if(!(*flit).isOn() && max > (*flit).getStart()) {
kraus's avatar
kraus committed
321
                    (*flit).setOn(kineticEnergy);
gsell's avatar
gsell committed
322 323 324 325 326
                }
            }

        } else {
            if(!(*flit).isOn() && max > (*flit).getStart() && min < (*flit).getEnd()) {
kraus's avatar
kraus committed
327
                (*flit).setOn(kineticEnergy);
gsell's avatar
gsell committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
            }
        }
        /////////////////////////
        // does not work like that
        //         if (min > (*flit).getEnd()) {
        //             (*flit).setOff();
        //         }
        /////////////////////////

    }
}


void OpalBeamline::switchAllElements() {
    for(FieldList::iterator flit = elements_m.begin(); flit != elements_m.end(); ++ flit) {
        if(!(*flit).isOn()) {
kraus's avatar
kraus committed
344
            (*flit).setOn(-1.0);
gsell's avatar
gsell committed
345 346 347 348 349
        }
    }

}

350 351
void OpalBeamline::switchElementsOff(const double &min, ElementBase::ElementType eltype) {
    if(eltype == ElementBase::ANY) {
gsell's avatar
gsell committed
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
        for(FieldList::iterator flit = elements_m.begin(); flit != elements_m.end(); ++ flit) {
            if((*flit).isOn() && min >= (*flit).getEnd()) {
                (*flit).setOff();
            }

        }
    } else {
        for(FieldList::iterator flit = elements_m.begin(); flit != elements_m.end(); ++ flit) {
            if((*flit).isOn() && min >= (*flit).getEnd() && (*flit).getElement()->getType() == eltype) {
                (*flit).setOff();
            }
        }
    }
}

void OpalBeamline::switchElementsOff() {
    for(FieldList::iterator flit = elements_m.begin(); flit != elements_m.end(); ++ flit)
        (*flit).setOff();
}

void OpalBeamline::prepareSections() {
    list<double> start_end;
    CompVec tmp;
    FieldList::iterator flit;
    list<double>::iterator pos_it, next_it, last_it;
kraus's avatar
kraus committed
377
    const double tolerance = 1.e-10;
gsell's avatar
gsell committed
378

379 380 381 382 383
    if (elements_m.size() == 0) {
        prepared_m = true;
        return;
    }

gsell's avatar
gsell committed
384 385 386
    /* there might be elements with length zero or extremely short ones.
       we delete them such that they don't appear in the simulation
     */
kraus's avatar
kraus committed
387
    elements_m.sort(ClassicField::SortAsc);
gsell's avatar
gsell committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    for(flit = elements_m.begin();  flit != elements_m.end(); ++ flit) {
        while(flit != elements_m.end() && (*flit).getLength() < tolerance) {
            FieldList::iterator temp = flit;
            ++temp;
            elements_m.erase(flit);
            flit = temp;
        }
        if(flit != elements_m.end()) {
            start_end.push_back((*flit).getStart());
            start_end.push_back((*flit).getEnd());
        }
    }
    start_end.sort();

    if(start_end.size() == 0) {
403 404
        throw OpalException("OpalBeamline::prepareSections",
                            "no valid elements found");
gsell's avatar
gsell committed
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
    }

    next_it = start_end.begin();
    ++next_it;
    for(pos_it = start_end.begin(); next_it != start_end.end(); ++ pos_it, ++ next_it) {
        if(*next_it - *pos_it < tolerance) {
            *next_it = *pos_it;
        }
    }

    start_end.unique();  // remove duplicate entries

    next_it = start_end.begin();
    ++next_it;
    for(pos_it = start_end.begin(); next_it != start_end.end(); ++ pos_it, ++ next_it) {
        tmp.clear();
        for(flit = elements_m.begin(); flit != elements_m.end(); ++ flit) {
            if((*flit).getStart() <= *pos_it  + tolerance && (*flit).getEnd()   >= *next_it - tolerance) {
                tmp.push_back((*flit).getElement());
            } else {
                if((*flit).getStart() >= *next_it) {
                    break;
                }
            }
        }
        if(tmp.size() > 0) {
            sections_m.push_back(OpalSection(tmp, *pos_it, *next_it));
        }
    }

    for(int i = 0; i < -1 + static_cast<long>(sections_m.size()); ++ i) {
        if(sections_m[i].doesBend()) {
            const CompVec &els = sections_m[i].getElements();
            int num_bending_elements = 0;
            for(CompVec::const_iterator elit = els.begin(); elit != els.end(); ++ elit) {
                if((*elit)->bends()) {
                    ++ num_bending_elements;
                }
            }
            if(num_bending_elements > 1) {
                if(els.size() == 2) {
                    // TODO:
                    // set end of section i - 1 to (End_{i-1} + End_{i})/2 and
                    // set start of section i + 1 to (Start_{i+1} + Start_{i})/2
                    //
                    // mark section i for deletion
                }
            }
        }
454 455 456
        if (sections_m[i].doDipoleFieldsOverlap()) {
            const CompVec &elements = sections_m[i].getElements();
            auto it = elements.begin();
457 458 459 460 461 462 463 464
            std::stringstream elementNamesStream;
            std::string elementNames;
            for (; it != elements.end(); ++ it) {
                double start, end;
                (*it)->getDimensions(start, end);
                elementNamesStream << (*it)->getName() << ": "
                                   << "start= " << start << " m, "
                                   << "end= " << end << " m,\n";
465
            }
466 467
            elementNames = elementNamesStream.str();
            elementNames.erase(elementNames.length() - 2, 1);
468
            throw OpalException("OpalBeamline::prepareSections",
469 470
                                "Fields overlap with dipole fields; not supported yet;\n*** affected elements:\n" + elementNames +
                                "***");
471
        }
gsell's avatar
gsell committed
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
    }


    for(int i = 0; i < static_cast<long>(sections_m.size()) - 1; ++ i) {
        if(sections_m[i].doesBend() && sections_m[i + 1].doesBend()) {

            if(fabs(sections_m[i].getEnd(0.0, 0.0) - sections_m[i + 1].getStart(0.0, 0.0)) < 1.e-12) {
                // TODO:
                // check that elements realy overlap
                const CompVec &els = sections_m[i].getElements();
                for(CompVec::const_iterator elit = els.begin(); elit != els.end(); ++ elit) {
                    if((*elit)->bends()) {
                        if(sections_m[i + 1].find(*elit)) {
                            sections_m[i].glue_to(&sections_m[i + 1]);
                            sections_m[i + 1].previous_is_glued();
                        }
                    }
                }
            }
        }
    }

    online_secs_m = new bool[sections_m.size()];
    for(size_t i = 0; i < sections_m.size(); ++ i) {
        online_secs_m[i] = false;
    }
    prepared_m = true;
}

void OpalBeamline::print(Inform &msg) const {
    SectionList::const_iterator sec_it;

kraus's avatar
kraus committed
504
    msg << level1 << "\n--- BEGIN FIELD LIST ---------------------------------------------------------------\n\n";
gsell's avatar
gsell committed
505 506 507
    for(sec_it = sections_m.begin(); sec_it != sections_m.end(); ++ sec_it) {
        (*sec_it).print(msg);
    }
kraus's avatar
kraus committed
508
    msg << level1 << "\n--- END FIELD LIST -----------------------------------------------------------------\n" << endl;
gsell's avatar
gsell committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
}


double OpalBeamline::calcBeamlineLenght() {
    SectionList::const_iterator sec_it;
    double l = -1000000000.0;
    for(sec_it = sections_m.begin(); sec_it != sections_m.end(); ++ sec_it) {
        double tmp = (*sec_it).getEnd();
        if(tmp > l)
            l = tmp;
    }
    return l;
}


524
FieldList OpalBeamline::getElementByType(ElementBase::ElementType type) {
gsell's avatar
gsell committed
525 526 527 528 529 530 531
    FieldList elements_of_requested_type;
    for(FieldList::iterator fit = elements_m.begin(); fit != elements_m.end(); ++ fit) {
        if((*fit).getElement()->getType() == type) {
            elements_of_requested_type.push_back((*fit));
        }
    }
    return elements_of_requested_type;
kraus's avatar
kraus committed
532
}