OpalBeamline.cpp 18.3 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 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

using namespace std;

extern Inform *gmsg;

OpalBeamline::OpalBeamline():
    sections_m(),
    elements_m(),
    prepared_m(false) {
    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
32
CompVec &OpalBeamline::getPredecessors(std::shared_ptr<const Component> element) {
gsell's avatar
gsell committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    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
53
CompVec &OpalBeamline::getSuccessors(std::shared_ptr<const Component> element) {
gsell's avatar
gsell committed
54 55 56
    size_t index;
    bool found = false;

kraus's avatar
kraus committed
57 58 59
    if (element == NULL)
        return sections_m[0].getElements();

gsell's avatar
gsell committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    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;
    }
}

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

    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))) {
            initial_guess = BEAMLINE_EOL;
            return dummy_section_m;
        }
        if(initial_guess == -1) initial_guess = 0;

        while(pos(2) > sections_m[initial_guess].getEnd(pos(0), pos(1))) ++initial_guess;
        while(pos(2) < sections_m[initial_guess].getStart(pos(0), pos(1))) --initial_guess;

91 92
        if(pos(2) >= sections_m[initial_guess].getStart(pos(0), pos(1)) &&
           pos(2) <= sections_m[initial_guess].getEnd(pos(0), pos(1))) return sections_m[initial_guess];
gsell's avatar
gsell committed
93 94 95 96 97 98 99 100 101 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 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

        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) {
        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;
179

gsell's avatar
gsell committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    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);
199

gsell's avatar
gsell committed
200 201 202 203 204
        // Tenzor<double, 3> orientation = dot(rotc, dot(rota, rotb));
        // E = dot(orientation, E);
        // B = dot(orientation, B);

        const  double sina = sin(ori(0)),
205 206 207 208 209
                      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
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

        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;

    long initial_guess = 0;
    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);
248

gsell's avatar
gsell committed
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
        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
307
void OpalBeamline::switchElements(const double &min, const double &max, const double &kineticEnergy, const bool &nomonitors) {
gsell's avatar
gsell committed
308 309 310 311 312 313 314
    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)
        if(flit->getElement()->getType() == "Monitor") {
            double spos = (max + min) / 2.;
            if(!nomonitors && spos < (*flit).getStart()) {
                if(!(*flit).isOn() && max > (*flit).getStart()) {
kraus's avatar
kraus committed
315
                    (*flit).setOn(kineticEnergy);
gsell's avatar
gsell committed
316 317 318 319 320
                }
            }

        } else {
            if(!(*flit).isOn() && max > (*flit).getStart() && min < (*flit).getEnd()) {
kraus's avatar
kraus committed
321
                (*flit).setOn(kineticEnergy);
gsell's avatar
gsell committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
            }
        }
        /////////////////////////
        // 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
338
            (*flit).setOn(-1.0);
gsell's avatar
gsell committed
339 340 341 342 343
        }
    }

}

344
void OpalBeamline::switchElementsOff(const double &min, const std::string &eltype) {
gsell's avatar
gsell committed
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
    if(eltype == "All") {
        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;
    const double tolerance = 1.e-4;

    /* 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
376
    elements_m.sort(ClassicField::SortAsc);
gsell's avatar
gsell committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    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) {
392 393
        throw OpalException("OpalBeamline::prepareSections",
                            "no valid elements found");
gsell's avatar
gsell committed
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
    }

    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
                }
            }
        }
    }


    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;

    msg << "\n--- BEGIN FIELD LIST ---------------------------------------------------------------\n" << endl;
    for(sec_it = sections_m.begin(); sec_it != sections_m.end(); ++ sec_it) {
        (*sec_it).print(msg);
    }
    msg << "\n--- END FIELD LIST -----------------------------------------------------------------\n" << endl;
}


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;
}


FieldList OpalBeamline::getElementByType(const std::string type) {
    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;
503
}