25#include "gsl/gsl_interp.h"
26#include "gsl/gsl_spline.h"
40 scaleCore_m(right.scaleCore_m),
41 scaleCoreError_m(right.scaleCoreError_m),
42 phaseCore1_m(right.phaseCore1_m),
43 phaseCore2_m(right.phaseCore2_m),
44 phaseExit_m(right.phaseExit_m),
45 startCoreField_m(right.startCoreField_m),
46 startExitField_m(right.startExitField_m),
47 mappedStartExitField_m(right.mappedStartExitField_m),
48 periodLength_m(right.periodLength_m),
49 numCells_m(right.numCells_m),
50 cellLength_m(right.cellLength_m),
58 scaleCoreError_m(0.0),
62 startCoreField_m(0.0),
63 startExitField_m(0.0),
64 mappedStartExitField_m(0.0),
92 double tmpcos, tmpsin;
93 Vector_t tmpE(0.0, 0.0, 0.0), tmpB(0.0, 0.0, 0.0);
102 Vector_t tmpE2(0.0, 0.0, 0.0), tmpB2(0.0, 0.0, 0.0);
104 const double z = tmpR(2);
148 double tmpcos, tmpsin;
149 Vector_t tmpE(0.0, 0.0, 0.0), tmpB(0.0, 0.0, 0.0);
157 Vector_t tmpE2(0.0, 0.0, 0.0), tmpB2(0.0, 0.0, 0.0);
159 const double z = tmpR(2);
197 if (bunch ==
nullptr) {
206 double zBegin = 0.0, zEnd = 0.0;
210 "The field map of a traveling wave structure has to begin at 0.0");
266 const double& mass) {
267 std::vector<double> t, E, t2, E2;
268 std::vector<std::pair<double, double> > F;
275 if (F.size() == 0)
return 0.0;
277 int N1 =
static_cast<int>(
std::floor(F.size() / 4.)) + 1;
278 int N2 = F.size() - 2 * N1 + 1;
280 int N4 =
static_cast<int>(std::round(N2 *
mode_m));
281 double Dz = F[N1 + N2].first - F[N1].first;
287 for (
int i = 1; i < N1; ++ i) {
288 E[i] = E0 + (F[i].first + F[i - 1].first) / 2. *
scale_m / mass;
291 for (
int i = N1; i < N3 - N1 + 1; ++ i) {
292 int I = (i - N1) % N2 + N1;
293 double Z = (F[I].first + F[I - 1].first) / 2. +
std::floor((i - N1) / N2) * Dz;
297 for (
int i = N3 - N1 + 1; i < N3; ++ i) {
298 int I = i - N3 - 1 + 2 * N1 + N2;
299 double Z = (F[I].first + F[I - 1].first) / 2. + ((
numCells_m - 1) *
mode_m - 1) * Dz;
300 E[i] = E0 + Z *
scale_m / mass;
304 for (
int iter = 0; iter < 10; ++ iter) {
307 for (
int i = 1; i < N1; ++ i) {
308 t[i] = t[i - 1] +
getdT(i, i, E, F, mass);
309 t2[i] = t2[i - 1] +
getdT(i, i, E2, F, mass);
313 for (
int i = N1; i < N3 - N1 + 1; ++ i) {
314 int I = (i - N1) % N2 + N1;
315 int J = (i - N1 + N4) % N2 + N1;
316 t[i] = t[i - 1] +
getdT(i, I, E, F, mass);
317 t2[i] = t2[i - 1] +
getdT(i, I, E2, F, mass);
321 for (
int i = N3 - N1 + 1; i < N3; ++ i) {
322 int I = i - N3 - 1 + 2 * N1 + N2;
323 t[i] = t[i - 1] +
getdT(i, I, E, F, mass);
324 t2[i] = t2[i - 1] +
getdT(i, I, E2, F, mass);
339 for (
int i = 1; i < N1; ++ i) {
342 for (
int i = N1; i < N3 - N1 + 1; ++ i) {
343 int I = (i - N1) % N2 + N1;
344 int J = (i - N1 + N4) % N2 + N1;
345 E[i] = E[i - 1] + q *
scaleCore_m * (
getdE(i, I, t, phi + phaseC1, F) +
getdE(i, J, t, phi + phaseC2, F));
347 for (
int i = N3 - N1 + 1; i < N3; ++ i) {
348 int I = i - N3 - 1 + 2 * N1 + N2;
349 E[i] = E[i - 1] + q *
scale_m *
getdE(i, I, t, phi + phaseE, F);
353 INFOMSG(
level2 <<
"estimated phase= " << tmp_phi <<
" rad = "
355 <<
"Ekin= " << E[N3 - 1] <<
" MeV" << std::setprecision(prevPrecision) <<
"\n" <<
endl);
361 for (
int i = 1; i < N1; ++ i) {
363 E2[i] = E2[i - 1] + q *
scale_m *
getdE(i, i, t, phi + dphi, F);
364 t[i] = t[i - 1] +
getdT(i, i, E, F, mass);
365 t2[i] = t2[i - 1] +
getdT(i, i, E2, F, mass);
367 E2[i] = E2[i - 1] + q *
scale_m *
getdE(i, i, t2, phi + dphi, F);
369 for (
int i = N1; i < N3 - N1 + 1; ++ i) {
370 int I = (i - N1) % N2 + N1;
371 int J = (i - N1 + N4) % N2 + N1;
372 E[i] = E[i - 1] + q *
scaleCore_m * (
getdE(i, I, t, phi + phaseC1, F) +
getdE(i, J, t, phi + phaseC2, F));
373 E2[i] = E2[i - 1] + q *
scaleCore_m * (
getdE(i, I, t, phi + phaseC1 + dphi, F) +
getdE(i, J, t, phi + phaseC2 + dphi, F));
374 t[i] = t[i - 1] +
getdT(i, I, E, F, mass);
375 t2[i] = t2[i - 1] +
getdT(i, I, E2, F, mass);
376 E[i] = E[i - 1] + q *
scaleCore_m * (
getdE(i, I, t, phi + phaseC1, F) +
getdE(i, J, t, phi + phaseC2, F));
377 E2[i] = E2[i - 1] + q *
scaleCore_m * (
getdE(i, I, t2, phi + phaseC1 + dphi, F) +
getdE(i, J, t2, phi + phaseC2 + dphi, F));
379 for (
int i = N3 - N1 + 1; i < N3; ++ i) {
380 int I = i - N3 - 1 + 2 * N1 + N2;
381 E[i] = E[i - 1] + q *
scale_m *
getdE(i, I, t, phi + phaseE, F);
382 E2[i] = E2[i - 1] + q *
scale_m *
getdE(i, I, t, phi + phaseE + dphi, F);
383 t[i] = t[i - 1] +
getdT(i, I, E, F, mass);
384 t2[i] = t2[i - 1] +
getdT(i, I, E2, F, mass);
385 E[i] = E[i - 1] + q *
scale_m *
getdE(i, I, t, phi + phaseE, F);
386 E2[i] = E2[i - 1] + q *
scale_m *
getdE(i, I, t2, phi + phaseE + dphi, F);
393 INFOMSG(
level2 <<
"estimated phase= " << tmp_phi <<
" rad = "
395 <<
"Ekin= " << E[N3 - 1] <<
" MeV" << std::setprecision(prevPrecision) <<
"\n" <<
endl);
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
Tps< T > cos(const Tps< T > &x)
Cosine.
Tps< T > sin(const Tps< T > &x)
Sine.
PETE_TUTree< FnArcTan, typename T::PETE_Expr_t > atan(const PETE_Expr< T > &l)
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Inform & level2(Inform &inf)
Inform & endl(Inform &inf)
constexpr double two_pi
The value of.
constexpr double pi
The value of.
ParticleAttrib< Vector_t > P
virtual void visitTravelingWave(const TravelingWave &)=0
Apply the algorithm to a traveling wave.
PartBunchBase< double, 3 > * RefPartBunch_m
bool getFlagDeleteOnTransverseExit() const
virtual void setElementLength(double length)
Set design length.
bool isInsideTransverse(const Vector_t &r) const
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
virtual double getElementLength() const override
Get design length.
double mappedStartExitField_m
double getdA(const int &i, const int &I, const std::vector< double > &t, const double &phi, const std::vector< std::pair< double, double > > &F) const
virtual ElementType getType() const override
Get element type std::string.
virtual bool bends() const override
virtual void accept(BeamlineVisitor &) const override
Apply visitor to TravelingWave.
virtual void getDimensions(double &zBegin, double &zEnd) const override
double getdB(const int &i, const int &I, const std::vector< double > &t, const double &phi, const std::vector< std::pair< double, double > > &F) const
virtual double getAutoPhaseEstimate(const double &E0, const double &t0, const double &q, const double &m) override
virtual bool isInside(const Vector_t &r) const override
virtual void goOffline() override
double getdE(const int &i, const int &I, const std::vector< double > &t, const double &phi, const std::vector< std::pair< double, double > > &F) const
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) override
virtual void finalise() override
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
virtual void getElementDimensions(double &begin, double &end) const override
virtual bool applyToReferenceParticle(const Vector_t &R, const Vector_t &P, const double &t, Vector_t &E, Vector_t &B) override
double getdT(const int &i, const int &I, const std::vector< double > &E, const std::vector< std::pair< double, double > > &F, const double mass) const
virtual void goOnline(const double &kineticEnergy) override
virtual bool isInside(const Vector_t &) const
virtual void getOnaxisEz(std::vector< std::pair< double, double > > &onaxis)
virtual bool getFieldstrength(const Vector_t &R, Vector_t &E, Vector_t &B) const =0
Vektor< double, 3 > Vector_t