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

#include "AbsBeamline/Multipole.h"
22
#include "Algorithms/PartBunch.h"
gsell's avatar
gsell committed
23
#include "AbsBeamline/BeamlineVisitor.h"
24
#include "Fields/Fieldmap.h"
gsell's avatar
gsell committed
25 26 27 28 29 30 31 32 33
#include "Physics/Physics.h"

extern Inform *gmsg;

// Class Multipole
// ------------------------------------------------------------------------

Multipole::Multipole():
    Component(),
kraus's avatar
kraus committed
34 35 36 37
    NormalComponents(1, 0.0),
    SkewComponents(1, 0.0),
    max_SkewComponent_m(1),
    max_NormalComponent_m(1),
gsell's avatar
gsell committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    myFieldmap_m(NULL) {
    setElType(isMultipole);
}


Multipole::Multipole(const Multipole &right):
    Component(right),
    NormalComponents(right.NormalComponents),
    SkewComponents(right.SkewComponents),
    max_SkewComponent_m(right.max_SkewComponent_m),
    max_NormalComponent_m(right.max_NormalComponent_m),
    myFieldmap_m(right.myFieldmap_m) {
    setElType(isMultipole);
}


54
Multipole::Multipole(const std::string &name):
gsell's avatar
gsell committed
55
    Component(name),
kraus's avatar
kraus committed
56 57 58 59
    NormalComponents(1, 0.0),
    SkewComponents(1, 0.0),
    max_SkewComponent_m(1),
    max_NormalComponent_m(1),
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
    myFieldmap_m(NULL) {
    setElType(isMultipole);
}


Multipole::~Multipole()
{}


void Multipole::accept(BeamlineVisitor &visitor) const {
    visitor.visitMultipole(*this);
}


double Multipole::getNormalComponent(int n) const {
    return getField().getNormalComponent(n);
}


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


void Multipole::setNormalComponent(int n, double v) {
    //   getField().setNormalComponent(n, v);
kraus's avatar
kraus committed
86 87 88
    PAssert(n > 1);

    if(n - 1 >  max_NormalComponent_m) {
gsell's avatar
gsell committed
89
        max_NormalComponent_m = n - 1;
kraus's avatar
kraus committed
90 91 92
        NormalComponents.resize(max_NormalComponent_m, 0.0);
    }
    NormalComponents[n - 2] = v; //starting from the quad (= 2)
gsell's avatar
gsell committed
93 94 95 96
}

void Multipole::setSkewComponent(int n, double v) {
    //   getField().setSkewComponent(n, v);
kraus's avatar
kraus committed
97 98 99
    PAssert(n > 1);

    if(n - 1 > max_SkewComponent_m) {
gsell's avatar
gsell committed
100
        max_SkewComponent_m = n - 1;
kraus's avatar
kraus committed
101 102 103
        SkewComponents.resize(max_SkewComponent_m, 0.0);
    }
    SkewComponents[n - 2] = v;  //starting from the quad (= 2)
gsell's avatar
gsell committed
104 105
}

adelmann's avatar
adelmann committed
106
double Multipole::EngeFunc(double z) {
kraus's avatar
kraus committed
107 108 109 110 111
  const double a1 = 0.296417;
  const double a2 = 4.533;
  const double a3 = -2.27;
  const double a4 = 1.06;
  const double a5 = -0.03;
adelmann's avatar
adelmann committed
112
  const double a6 = 0.02;					\
adelmann's avatar
adelmann committed
113
  const double DD = 0.99;
adelmann's avatar
adelmann committed
114

kraus's avatar
kraus committed
115 116
  const double y = z - 1.0;
  return 1.0/(1 + std::exp(a1 + a2*(y/DD) + a3*std::pow(y/DD,2) + a4*std::pow(y/DD,3) + a5*std::pow(y/DD,4) + a6*std::pow(y/DD,5)));
adelmann's avatar
adelmann committed
117
}
118

adelmann's avatar
adelmann committed
119
double Multipole::EngeFact(double z) {
kraus's avatar
kraus committed
120
  // Normalize
adelmann's avatar
adelmann committed
121 122 123
  const double lFringe = std::abs(endField_m-startField_m);
  const double zn = (z - startField_m) / lFringe;

adelmann's avatar
adelmann committed
124 125
  const double scale = 1.0; // 1.4289638937448055; // to make integrated field strenght like the hard edge approximation

adelmann's avatar
adelmann committed
126 127
  if(zn > 0.5) {
    return scale*EngeFunc((zn-0.5)*10 - 3.) ;
adelmann's avatar
adelmann committed
128 129
  }
  else
adelmann's avatar
adelmann committed
130
    return scale*EngeFunc((0.5-zn)*10 - 3.) ;
131 132 133
}


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
//ff
// radial focussing term
void Multipole::addKR(int i, double t, Vector_t &K) {
    Inform msg("Multipole::addK()");

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

    // calculate the average of all normal components, to obtain the gradient
    double l = NormalComponents.size();
    double temp_n = 0;
    for(int j = 0; j < l; j++)
        temp_n += NormalComponents.at(j);

    double Grad = temp_n / l;
    double k = -Physics::q_e * b * Physics::c * Grad / (g * Physics::EMASS);
    //FIXME: factor? k *= 5?

    //FIXME: sign?
    Vector_t temp(k, -k, 0.0);

    K += temp;
}

//ff
//transverse kick
void Multipole::addKT(int i, double t, Vector_t &K) {
    Inform msg("Multipole::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 b = RefPartBunch_m->getBeta(i);
    double g = RefPartBunch_m->getGamma(i);

    // calculate the average of all normal components, to obtain the gradient

    double l = NormalComponents.size();
    double temp_n = 0;
    for(int j = 0; j < l; j++)
        temp_n += NormalComponents.at(j);

    double G = temp_n / l;
    double cf = -Physics::q_e * b * Physics::c * G / (g * Physics::EMASS);
    double dx = RefPartBunch_m->getX0(i) - dx_m;
    double dy = RefPartBunch_m->getY0(i) - dy_m;

    K += Vector_t(cf * dx, -cf * dy, 0.0);
}

186
bool Multipole::apply(const size_t &i, const double &t, double E[], double B[]) {
gsell's avatar
gsell committed
187
    Vector_t Ev(0, 0, 0), Bv(0, 0, 0);
kraus's avatar
kraus committed
188

adelmann's avatar
adelmann committed
189
    const Vector_t Rt(RefPartBunch_m->getX(i) - dx_m, RefPartBunch_m->getY(i) - dy_m , RefPartBunch_m->getZ(i) - ds_m);
190 191
    // before misalignment    Vector_t Rt(RefPartBunch_m->getX(i), RefPartBunch_m->getY(i), RefPartBunch_m->getZ(i));

gsell's avatar
gsell committed
192 193 194 195 196 197 198 199 200 201 202 203
    if(apply(Rt, Vector_t(0.0), t, Ev, Bv)) return true;

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

204
bool Multipole::apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) {
adelmann's avatar
adelmann committed
205 206

    const Vector_t temp(RefPartBunch_m->getX(i) - dx_m, RefPartBunch_m->getY(i) - dy_m , RefPartBunch_m->getZ(i) - ds_m);
207 208
    // before misalignment Vector_t temp(RefPartBunch_m->getX(i), RefPartBunch_m->getY(i), RefPartBunch_m->getZ(i));

gsell's avatar
gsell committed
209 210 211 212
    const Vector_t &R(temp);
    //const Vector_t &R = RefPartBunch_m->R[i];

    Vector_t FieldFactor(1.0, 0.0, 0.0);
213

gsell's avatar
gsell committed
214 215 216 217 218 219 220 221 222 223
    if(myFieldmap_m) {
        if(myFieldmap_m->getType() == T3DMagnetoStatic) {
            return false;
        } else if(myFieldmap_m->getType() == T1DProfile1 || myFieldmap_m->getType() == T1DProfile2) {
            Vector_t tmpE(0, 0, 0);
            myFieldmap_m->getFieldstrength(R, tmpE, FieldFactor);
        }
    }

    if(R(2) > startField_m && R(2) <= endField_m) {
224

adelmann's avatar
adelmann committed
225 226
      FieldFactor(0) = 1.0; // FixMe: need to define a way to incooperate fringe fields EngeFact(R(2));

gsell's avatar
gsell committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
        if(max_NormalComponent_m > 0) {
            B(0) += NormalComponents[0] * (FieldFactor(0) * R(1) - FieldFactor(2) * R(1) * R(1) * R(1) / 6.);
            B(1) += NormalComponents[0] * (FieldFactor(0) * R(0) - FieldFactor(2) * R(0) * R(0) / 2.);
            B(2) += NormalComponents[0] * FieldFactor(1) * R(0) * R(1);

            if(max_NormalComponent_m > 1) {
                const double R02 = R(0) * R(0);
                const double R12 = R(1) * R(1);
                B(0) += NormalComponents[1] * R(0) * R(1);
                B(1) += NormalComponents[1] * (R02 - R12) / 2.;

                if(max_NormalComponent_m > 2) {
                    B(0) += NormalComponents[2] * (3. * R02 * R(1) - R12 * R(1)) / 6.;
                    B(1) += NormalComponents[2] * (R02 * R(0) - 3. * R(0) * R12) / 6.;

                    if(max_NormalComponent_m > 3) {
                        B(0) += NormalComponents[3] * (R02 * R(0) * R(1) - R(0) * R12) / 6.;
                        B(1) += NormalComponents[3] * (R02 * R02 - 6. * R02 * R12 + R12 * R12) / 24.;

                        if(max_NormalComponent_m > 4) {
                            Inform msg("Multipole ");
                            msg << Fieldmap::typeset_msg("HIGHER MULTIPOLES THAN DECAPOLE NOT IMPLEMENTED!", "warning") << "\n"
                                << endl;
                        }
                    }
                }
            }
        }

        if(max_SkewComponent_m > 0) {
adelmann's avatar
adelmann committed
257 258
	  B(0) += -SkewComponents[0] * R(0);
	  B(1) += SkewComponents[0] * R(1);
gsell's avatar
gsell committed
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

            if(max_SkewComponent_m > 1) {
                const double R02 = R(0) * R(0);
                const double R12 = R(1) * R(1);

                B(0) += -SkewComponents[1] * (R02 - R12) / 2.;
                B(1) += SkewComponents[1] * R(0) * R(1);

                if(max_SkewComponent_m > 2) {
                    B(0) += -SkewComponents[2] * (R02 * R(0) - 3. * R(0) * R12) / 6.;
                    B(1) += -SkewComponents[2] * (3. * R02 * R(1) - R12 * R(1)) / 6.;

                    if(max_SkewComponent_m > 3) {
                        B(0) += -SkewComponents[3] * (R02 * R02 - 6. * R02 * R12 + R12 * R12) / 24.;
                        B(1) += SkewComponents[3] * (R02 * R(0) * R(1) - R(0) * R12 * R(1)) / 6.;

                        if(max_SkewComponent_m > 4) {
                            Inform msg("Multipole ");
                            msg << Fieldmap::typeset_msg("HIGHER MULTIPOLES THAN DECAPOLE NOT IMPLEMENTED!", "warning") << "\n"
                                << endl;
                        }
                    }
                }
            }
        }
    }

    return false;
}

adelmann's avatar
adelmann committed
289 290
bool Multipole::apply(const Vector_t &R0, const Vector_t &centroid, const double &t, Vector_t &E, Vector_t &B) {

adelmann's avatar
adelmann committed
291
   const Vector_t R = R0 - Vector_t(dx_m,dy_m,ds_m);
adelmann's avatar
adelmann committed
292

adelmann's avatar
adelmann committed
293
   const Vector_t tmpR(R(0) - dx_m, R(1) - dy_m, R(2) - ds_m);
294 295

    if(tmpR(2) > startField_m && tmpR(2) <= endField_m) {
gsell's avatar
gsell committed
296
        if(max_NormalComponent_m > 0) {
297 298
            B(0) += NormalComponents[0] * tmpR(1);
            B(1) += NormalComponents[0] * tmpR(0);
gsell's avatar
gsell committed
299 300

            if(max_NormalComponent_m > 1) {
301 302 303
                const double R02 = tmpR(0) * tmpR(0);
                const double R12 = tmpR(1) * tmpR(1);
                B(0) += NormalComponents[1] * tmpR(0) * tmpR(1);
gsell's avatar
gsell committed
304 305 306
                B(1) += NormalComponents[1] * (R02 - R12) / 2.;

                if(max_NormalComponent_m > 2) {
307 308
                    B(0) += NormalComponents[2] * (3. * R02 * tmpR(1) - R12 * tmpR(1)) / 6.;
                    B(1) += NormalComponents[2] * (R02 * tmpR(0) - 3. * tmpR(0) * R12) / 6.;
gsell's avatar
gsell committed
309 310

                    if(max_NormalComponent_m > 3) {
311
                        B(0) += NormalComponents[3] * (R02 * tmpR(0) * tmpR(1) - tmpR(0) * R12) / 6.;
gsell's avatar
gsell committed
312 313 314 315 316 317 318 319 320 321
                        B(1) += NormalComponents[3] * (R02 * R02 - 6. * R02 * R12 + R12 * R12) / 24.;

                        if(max_NormalComponent_m > 4) {
                            Inform msg("Multipole ");
                            msg << Fieldmap::typeset_msg("HIGHER MULTIPOLES THAN DECAPOLE NOT IMPLEMENTED!", "warning") << "\n"
                                << endl;
                        }
                    }
                }
            }
adelmann's avatar
adelmann committed
322

gsell's avatar
gsell committed
323 324 325
        }

        if(max_SkewComponent_m > 0) {
adelmann's avatar
adelmann committed
326

327 328
            B(0) += -SkewComponents[0] * tmpR(0);
            B(1) += SkewComponents[0] * tmpR(1);
gsell's avatar
gsell committed
329 330

            if(max_SkewComponent_m > 1) {
331 332
                const double R02 = tmpR(0) * tmpR(0);
                const double R12 = tmpR(1) * tmpR(1);
gsell's avatar
gsell committed
333 334

                B(0) += -SkewComponents[1] * (R02 - R12) / 2.;
335
                B(1) += SkewComponents[1] * tmpR(0) * tmpR(1);
gsell's avatar
gsell committed
336 337

                if(max_SkewComponent_m > 2) {
338 339
                    B(0) += -SkewComponents[2] * (R02 * tmpR(0) - 3. * tmpR(0) * R12) / 6.;
                    B(1) += -SkewComponents[2] * (3. * R02 * tmpR(1) - R12 * tmpR(1)) / 6.;
gsell's avatar
gsell committed
340 341 342

                    if(max_SkewComponent_m > 3) {
                        B(0) += -SkewComponents[3] * (R02 * R02 - 6. * R02 * R12 + R12 * R12) / 24.;
343
                        B(1) += SkewComponents[3] * (R02 * tmpR(0) * tmpR(1) - tmpR(0) * R12 * tmpR(1)) / 6.;
gsell's avatar
gsell committed
344 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 376 377 378 379 380

                        if(max_SkewComponent_m > 4) {
                            Inform msg("Multipole ");
                            msg << Fieldmap::typeset_msg("HIGHER MULTIPOLES THAN DECAPOLE NOT IMPLEMENTED!", "warning") << "\n"
                                << endl;
                        }
                    }
                }
            }
        }
    }
    return false;
}
void Multipole::initialise(PartBunch *bunch, double &startField, double &endField, const double &scaleFactor) {
    RefPartBunch_m = bunch;
    endField = startField + getElementLength();
    startField_m = startField;
    endField_m = endField;
    online_m = true;
}


void Multipole::finalise() {
    online_m = false;
}

bool Multipole::bends() const {
    return false;
}


void Multipole::getDimensions(double &zBegin, double &zEnd) const {
    zBegin = startField_m;
    zEnd = endField_m;
}


381 382
ElementBase::ElementType Multipole::getType() const {
    return MULTIPOLE;
gsell's avatar
gsell committed
383
}