FlexibleCollimator.cpp 7.56 KB
Newer Older
1 2
//
// Class FlexibleCollimator
3
//   Defines the abstract interface for a collimator.
gsell's avatar
gsell committed
4
//
5
// Copyright (c) 200x - 2021, Paul Scherrer Institut, Villigen PSI, Switzerland
6 7 8 9 10 11 12 13 14 15 16 17
// All rights reserved.
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL.  If not, see <https://www.gnu.org/licenses/>.
//
18
#include "AbsBeamline/FlexibleCollimator.h"
19 20

#include "AbsBeamline/BeamlineVisitor.h"
frey_m's avatar
frey_m committed
21
#include "AbstractObjects/OpalData.h"
22 23
#include "Algorithms/PartBunchBase.h"
#include "Fields/Fieldmap.h"
24 25
#include "Physics/Physics.h"
#include "Solvers/ParticleMatterInteractionHandler.h"
26 27 28 29 30 31 32 33 34
#include "Structure/LossDataSink.h"
#include "Utilities/Options.h"
#include "Utilities/Util.h"

#include <memory>

extern Inform *gmsg;

FlexibleCollimator::FlexibleCollimator():
35 36
    FlexibleCollimator("")
{}
37 38 39 40


FlexibleCollimator::FlexibleCollimator(const FlexibleCollimator &right):
    Component(right),
41
    description_m(right.description_m),
42
    bb_m(right.bb_m),
43
    tree_m(/*right.tree_m*/),
44 45 46
    informed_m(right.informed_m),
    losses_m(0),
    lossDs_m(nullptr),
47 48
    parmatint_m(NULL)
{
49
    for (const std::shared_ptr<mslang::Base>& obj: right.holes_m) {
50
        holes_m.emplace_back(obj->clone());
51
    }
52 53 54 55

    tree_m.bb_m = bb_m;
    tree_m.objects_m.insert(tree_m.objects_m.end(), holes_m.begin(), holes_m.end());
    tree_m.buildUp();
56 57 58 59 60
}


FlexibleCollimator::FlexibleCollimator(const std::string &name):
    Component(name),
61
    description_m(""),
62 63 64 65 66 67 68 69 70 71
    informed_m(false),
    losses_m(0),
    lossDs_m(nullptr),
    parmatint_m(NULL)
{}


FlexibleCollimator::~FlexibleCollimator() {
    if (online_m)
        goOffline();
72

73 74 75
    // for (mslang::Base *obj: holes_m) {
    //     delete obj;
    // }
76 77 78 79 80 81 82 83
}


void FlexibleCollimator::accept(BeamlineVisitor &visitor) const {
    visitor.visitFlexibleCollimator(*this);
}


84
bool FlexibleCollimator::isStopped(const Vector_t &R) {
85
    const double z = R(2);
86 87 88 89 90

    if ((z < 0.0) ||
        (z > getElementLength()) ||
        (!isInsideTransverse(R))) return false;

91
    if (!bb_m.isInside(R)) {
92
        return true;
93
    }
94

95 96
    if (!tree_m.isInside(R)) {
        return true;
97 98
    }

99
    return  false;
100 101
}

102
bool FlexibleCollimator::apply(const size_t &i, const double &t, Vector_t &/*E*/, Vector_t &/*B*/) {
103 104 105
    const Vector_t &R = RefPartBunch_m->R[i];
    const Vector_t &P = RefPartBunch_m->P[i];
    const double &dt = RefPartBunch_m->dt[i];
106
    const double recpgamma = Physics::c * dt / std::sqrt(1.0 + dot(P, P));
107
    bool pdead = isStopped(R);
108 109 110 111

    if (pdead) {
        if (lossDs_m) {
            double frac = -R(2) / P(2) * recpgamma;
kraus's avatar
kraus committed
112 113 114 115
            lossDs_m->addParticle(OpalParticle(RefPartBunch_m->ID[i],
                                               R, P,
                                               t + frac * dt,
                                               RefPartBunch_m->Q[i], RefPartBunch_m->M[i]));
116 117 118 119 120 121 122
        }
        ++ losses_m;
    }

    return pdead;
}

123 124 125 126 127 128
bool FlexibleCollimator::applyToReferenceParticle(
        const Vector_t &/*R*/,
        const Vector_t &/*P*/,
        const double &/*t*/,
        Vector_t &/*E*/,
        Vector_t &/*B*/) {
129 130 131
    return false;
}

snuverink_j's avatar
snuverink_j committed
132
// rectangle collimators in cyclotron cyclindral coordinates
133
// without particlematterinteraction, the particle hitting collimator is deleted directly
134 135 136 137 138
bool FlexibleCollimator::checkCollimator(
        PartBunchBase<double, 3> */*bunch*/,
        const int /*turnnumber*/,
        const double /*t*/,
        const double /*tstep*/) {
139 140 141 142 143 144 145 146 147
    return false;
}

void FlexibleCollimator::initialise(PartBunchBase<double, 3> *bunch, double &startField, double &endField) {
    RefPartBunch_m = bunch;
    endField = startField + getElementLength();

    parmatint_m = getParticleMatterInteraction();

148
    lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getOutputFN(), !Options::asciidump));
149 150 151 152 153 154 155 156 157

    goOnline(-1e6);
}

void FlexibleCollimator::initialise(PartBunchBase<double, 3> *bunch) {
    RefPartBunch_m = bunch;

    parmatint_m = getParticleMatterInteraction();

158
    lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getOutputFN(), !Options::asciidump));
159 160 161 162

    goOnline(-1e6);
}

163
void FlexibleCollimator::finalise() {
164 165
    if (online_m)
        goOffline();
snuverink_j's avatar
snuverink_j committed
166
    *gmsg << "* Finalize flexible collimator " << getName() << endl;
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
}

void FlexibleCollimator::goOnline(const double &) {
    print();

    online_m = true;
}

void FlexibleCollimator::print() {
    if (RefPartBunch_m == NULL) {
        if (!informed_m) {
            std::string errormsg = Fieldmap::typeset_msg("BUNCH SIZE NOT SET", "warning");
            ERRORMSG(errormsg << endl);
            if (Ippl::myNode() == 0) {
                std::ofstream omsg("errormsg.txt", std::ios_base::app);
                omsg << errormsg << std::endl;
                omsg.close();
            }
            informed_m = true;
        }
        return;
    }

    *gmsg << level3;
}

void FlexibleCollimator::goOffline() {
    if (online_m && lossDs_m)
        lossDs_m->save();
    lossDs_m.reset(0);
    online_m = false;
}

void FlexibleCollimator::getDimensions(double &zBegin, double &zEnd) const {
    zBegin = 0.0;
    zEnd = getElementLength();
}

ElementBase::ElementType FlexibleCollimator::getType() const {
206
    return FLEXIBLECOLLIMATOR;
207 208 209
}

void FlexibleCollimator::setDescription(const std::string &desc) {
210 211 212
    tree_m.reset();
    holes_m.clear();

213 214 215 216 217 218 219 220 221 222
    mslang::Function *fun;

    if (!mslang::parse(desc, fun))
        throw GeneralClassicException("FlexibleCollimator::setDescription",
                                      "Couldn't parse input file");

    fun->apply(holes_m);

    if (holes_m.size() == 0) return;

223
    for (std::shared_ptr<mslang::Base> & it: holes_m) {
224 225 226
        it->computeBoundingBox();
    }

227
    std::shared_ptr<mslang::Base> &first = holes_m.front();
228 229 230 231 232 233 234 235 236
    const mslang::BoundingBox &bb = first->bb_m;

    Vector_t llc(bb.center_m[0] - 0.5 * bb.width_m,
                 bb.center_m[1] - 0.5 * bb.height_m,
                 0.0);
    Vector_t urc(bb.center_m[0] + 0.5 * bb.width_m,
                 bb.center_m[1] + 0.5 * bb.height_m,
                 0.0);

237
    for (const std::shared_ptr<mslang::Base> & it: holes_m) {
238 239 240 241 242 243 244
        const mslang::BoundingBox &bb = it->bb_m;
        llc[0] = std::min(llc[0], bb.center_m[0] - 0.5 * bb.width_m);
        llc[1] = std::min(llc[1], bb.center_m[1] - 0.5 * bb.height_m);
        urc[0] = std::max(urc[0], bb.center_m[0] + 0.5 * bb.width_m);
        urc[1] = std::max(urc[1], bb.center_m[1] + 0.5 * bb.height_m);
    }

kraus's avatar
kraus committed
245 246 247 248 249 250 251 252
    double width = urc[0] - llc[0];
    double height = urc[1] - llc[1];

    llc[0] -= 1e-3 * width;
    urc[0] += 1e-3 * width;
    llc[1] -= 1e-3 * height;
    urc[1] += 1e-3 * height;

253 254
    bb_m = mslang::BoundingBox(llc, urc);

255 256 257 258
    tree_m.bb_m = bb_m;
    tree_m.objects_m.insert(tree_m.objects_m.end(), holes_m.begin(), holes_m.end());
    tree_m.buildUp();

259
    delete fun;
260 261 262 263
}

void FlexibleCollimator::writeHolesAndQuadtree(const std::string &baseFilename) const {
    if (Ippl::myNode() == 0) {
264 265 266 267 268 269
        std::string fname = Util::combineFilePath({
            OpalData::getInstance()->getAuxiliaryOutputDirectory(),
            baseFilename
        });

        std::ofstream out(fname + "_quadtree.gpl");
270 271 272
        tree_m.writeGnuplot(out);
        out.close();

273
        out.open(fname + "_holes.gpl");
274 275 276 277 278
        for (const std::shared_ptr<mslang::Base> &obj: holes_m) {
            obj->writeGnuplot(out);
        }
        out.close();
    }
279
}