ErrorField.cpp 5.71 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// ------------------------------------------------------------------------
// $RCSfile: ErrorField.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.1.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: ErrorField
//   Class for the OPAL EFIELD error command.
//
// ------------------------------------------------------------------------
//
// $Date: 2000/03/27 09:33:41 $
// $Author: Andreas Adelmann $
//
// ------------------------------------------------------------------------

#include "Errors/ErrorField.h"
#include "AbstractObjects/BeamSequence.h"
#include "Attributes/Attributes.h"
#include "Errors/Error.h"
#include "Errors/MPHandler.h"
#include "Fields/BMultipoleField.h"
25
#include "Utilities/Options.h"
gsell's avatar
gsell committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include "Utilities/Options.h"
#include "Utilities/Round.h"
#include <cmath>
#include <iostream>
#include <vector>

using std::cerr;
using std::endl;
using std::max;
using std::vector;


// Class ErrorField
// ------------------------------------------------------------------------

// The attributes of class ErrorField.
namespace {
    enum {
        RADIUS, // The normalising radius in m.
        ORDER,  // The order of component used for normalisation.
        DKR,    // The relative magnitude of error components.
        DK,     // The absolute magnitude of error components in m^(-n).
        ROT,    // The rotation angles in rad.
        SIZE
    };
}


ErrorField::ErrorField():
    Action(SIZE, "EFIELD",
           "The \"EFIELD\" sub-command assigns field errors by magnitude and "
           "rotation angle.") {
    itsAttr[RADIUS] = Attributes::makeReal
                      ("RADIUS", "Normalising radius in m", 1.0);

    itsAttr[ORDER] = Attributes::makeReal
                     ("ORDER", "Order of component used for normalisation");

    itsAttr[DKR] = Attributes::makeRealArray
                   ("DKR", "Relative magnitude of error components (no dimension)");
    itsAttr[DKR].setDeferred(true);

    itsAttr[DK] = Attributes::makeRealArray
                  ("DK", "Absolute magnitude of error components in m^(-n)");
    itsAttr[DK].setDeferred(true);

    itsAttr[ROT] = Attributes::makeRealArray
                   ("ROT", "Rotation angles in rad");
    itsAttr[ROT].setDeferred(true);
}


78
ErrorField::ErrorField(const std::string &name, ErrorField *parent):
gsell's avatar
gsell committed
79 80 81 82 83 84 85 86
    Action(name, parent)
{}


ErrorField::~ErrorField()
{}


87
ErrorField *ErrorField::clone(const std::string &name) {
gsell's avatar
gsell committed
88 89 90 91 92 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
    return new ErrorField(name, this);
}


void ErrorField::execute() {
    // Warn about redundant itsAttr.
    if(itsAttr[DKR]  &&  itsAttr[DK]) {
        if(Options::warn) {
            cerr << "\n### Warning ### " << *this
                 << "\nBoth relative and absolute errors given; "
                 << "error definitions will use their sum.\n" << endl;
        }
    }

    // Generate the multipole errors.
    oldError = newError = 0;
    MPHandler inserter(*Error::block->itsLine, *this, false);
    inserter.execute();

    if(Options::info) {
        cerr << '\n';

        if(oldError + newError == 0) {
            cerr << "No field errors assigned.\n";
        } else {
            if(oldError != 0) {
                cerr << "Field errors "
                     << (Error::block->addError ? "superposed on " : "replaced on ")
                     << oldError << " element";
                if(oldError > 1) cerr << "s";
                cerr << ".\n";
            }

            if(newError != 0) {
                cerr << "Field errors assigned to " << newError << " element";
                if(newError > 1) cerr << "s";
                cerr << ".\n";
            }
        }

        cerr << endl;
    }
}


133
void ErrorField::fieldError(const std::string &, int,
gsell's avatar
gsell committed
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 186 187 188 189
                            const BMultipoleField &designField,
                            BMultipoleField &errorField) {
    // Use of relative error requires radius to be given.
    double rad = Attributes::getReal(itsAttr[RADIUS]);
    if(rad == 0.0) rad = 1.0;
    int ord = int(Round(Attributes::getReal(itsAttr[ORDER])));
    double base = sqrt(pow(designField.getNormalComponent(ord + 1), 2) +
                       pow(designField.getSkewComponent(ord + 1), 2));
    if(base == 0.0) base = 1.0;

    // Generate the field error.
    vector<double> relative = Attributes::getRealArray(itsAttr[DKR]);
    vector<double> absolute = Attributes::getRealArray(itsAttr[DK]);
    vector<double> rotation = Attributes::getRealArray(itsAttr[ROT]);
    vector<double>::size_type top =
        max(relative.size(), absolute.size());

    BMultipoleField tempField;
    for(vector<double>::size_type comp = 0; comp < top; comp++) {
        // Compute error amplitude.
        double normal = 0.0;
        double skewed = 0.0;

        if(comp < absolute.size()) {
            normal  = absolute[comp] * absFactor(comp);
        }

        if(comp < relative.size()) {
            normal += relative[comp] * relFactor(comp, ord, rad) * base;
        }

        // Convert to components.
        if(normal != 0.0) {
            if(comp < rotation.size()) {
                double angle = - rotation[comp] * double(comp + 1);
                skewed = normal * sin(angle);
                normal = normal * cos(angle);
            }

            tempField.setNormalComponent(comp + 1, normal);
            tempField.setSkewComponent(comp + 1, skewed);
        }
    }

    if(errorField.order() != 0) {
        oldError++;
        if(Error::block->addError) {
            errorField.addField(tempField);
        } else {
            errorField = tempField;
        }
    } else {
        newError++;
        errorField = tempField;
    }
}