28#include <boost/assign.hpp>
31#include "gsl/gsl_interp.h"
32#include "gsl/gsl_spline.h"
40 boost::assign::list_of<const boost::bimap<CavityType, std::string>::relation>
136 Vector_t tmpE(0.0, 0.0, 0.0), tmpB(0.0, 0.0, 0.0);
138 bool outOfBounds =
fieldmap_m->getFieldstrength(R, tmpE, tmpB);
155 Vector_t tmpE(0.0, 0.0, 0.0), tmpB(0.0, 0.0, 0.0);
157 bool outOfBounds =
fieldmap_m->getFieldstrength(R, tmpE, tmpB);
158 if (outOfBounds)
return true;
170 if (bunch ==
nullptr) {
178 std::stringstream errormsg;
185 "The length of the field map '" +
filename_m +
"' is zero or negative");
191 errormsg <<
"FREQUENCY IN INPUT FILE DIFFERENT THAN IN FIELD MAP '" <<
filename_m <<
"';\n"
197 std::ofstream omsg(
"errormsg.txt", std::ios_base::app);
198 omsg << errormsg_str << std::endl;
208 std::shared_ptr<AbstractTimeDependence> freq_atd,
209 std::shared_ptr<AbstractTimeDependence> ampl_atd,
210 std::shared_ptr<AbstractTimeDependence> phase_atd) {
230 "', please check the data format");
244 *
gmsg <<
"* Cavity voltage data read successfully!" <<
endl;
295 "RMIN must be positive");
304 "The attribute RMAX has to be higher than RMIN");
348 "The attribute \"FMAPFN\" isn't set "
349 "for the \"RFCAVITY\" element!");
350 }
else if (std::filesystem::exists(
filename_m)) {
355 "', please check if it exists");
376 const double dtCorrt,
378 const double restMass,
379 const int chargenumber) {
383 double momentum2 = momentum[0] * momentum[0] + momentum[1] * momentum[1] + momentum[2] * momentum[2];
384 double betgam = std::sqrt(momentum2);
386 double gamma = std::sqrt(1.0 + momentum2);
387 double beta = betgam / gamma;
391 double Ufactor = 1.0;
397 Ufactor = std::sin(transit_factor) / transit_factor;
403 double nphase = (frequency * (t + dtCorrt)) -
phi0_m;
404 double dgam = Voltage * std::cos(nphase) / (restMass);
407 if (tempdegree > 270.0) tempdegree -= 360.0;
411 double newmomentum2 = std::pow(gamma, 2) - 1.0;
414 double ptheta = std::sqrt(newmomentum2 - std::pow(pr, 2));
421 momentum[0] = std::cos(rotate) * px + std::sin(rotate) * py;
422 momentum[1] = -std::sin(rotate) * px + std::cos(rotate) * py;
426 m <<
"* Cavity " <<
getName() <<
" Phase= " << tempdegree <<
" [deg] transit time factor= " << Ufactor
439 "no support points!");
451 while ((ih - il) > 1) {
452 int i = (int)((il + ih) / 2.0);
475 double u = (z - x1) / dx;
478 double dy2 = -2.0 * dy;
479 double ya2 = y2a + 2.0 * y1a;
480 double dy3 = 3.0 * dy;
481 double ya3 = y2a + y1a;
482 double yb2 = dy2 + dx * ya3;
483 double yb4 = dy3 - dx * ya2;
484 splint = y1 + u * dx * y1a + u2 * yb4 + u3 * yb2;
485 *za = y1a + 2.0 * u / dx * yb4 + 3.0 * u2 / dx * yb2;
505 const double dt = 1e-13;
517 for (
unsigned int j = 0; j < 2; ++ j) {
518 for (
unsigned int i = 0; i < 36; ++ i, phi += dphi) {
535 const int prevPrecision =
Ippl::Info->precision(8);
537 <<
"estimated phase= " << phimax <<
" rad = "
539 <<
"Ekin= " << Emax <<
" MeV" << std::setprecision(prevPrecision) <<
"\n" <<
endl);
546 const double& q,
const double& mass) {
547 std::vector<double> t, E, t2, E2;
548 std::vector<double> F;
549 std::vector< std::pair< double, double > > G;
550 gsl_spline *onAxisInterpolants;
551 gsl_interp_accel *onAxisAccel;
554 double dz = 1.0, length = 0.0;
556 if (G.size() == 0)
return 0.0;
557 double begin = (G.front()).first;
558 double end = (G.back()).first;
559 std::unique_ptr<double[]> zvals(
new double[G.size()]);
560 std::unique_ptr<double[]> onAxisField(
new double[G.size()]);
562 for (
size_t j = 0; j < G.size(); ++ j) {
563 zvals[j] = G[j].first;
564 onAxisField[j] = G[j].second;
566 onAxisInterpolants = gsl_spline_alloc(gsl_interp_cspline, G.size());
567 onAxisAccel = gsl_interp_accel_alloc();
568 gsl_spline_init(onAxisInterpolants, zvals.get(), onAxisField.get(), G.size());
571 dz = length / G.size();
575 unsigned int N = (int)std::floor(length / dz + 1);
580 for (
size_t j = 0; j < N; ++ j, z += dz) {
581 F[j] = gsl_spline_eval(onAxisInterpolants, z, onAxisAccel);
583 gsl_spline_free(onAxisInterpolants);
584 gsl_interp_accel_free(onAxisAccel);
592 for (
unsigned int i = 1; i < N; ++ i, z += dz) {
593 E[i] = E[i - 1] + dz *
scale_m / mass;
597 for (
int iter = 0; iter < 10; ++ iter) {
600 for (
unsigned int i = 1; i < N; ++ i) {
601 t[i] = t[i - 1] +
getdT(i, E, dz, mass);
602 t2[i] = t2[i - 1] +
getdT(i, E2, dz, mass);
607 if (std::abs(B) > 0.0000001) {
608 tmp_phi = std::atan(A / B);
612 if (q * (A * std::sin(tmp_phi) + B * std::cos(tmp_phi)) < 0) {
616 if (std::abs (phi - tmp_phi) <
frequency_m * (t[N - 1] - t[0]) / (10 * N)) {
617 for (
unsigned int i = 1; i < N; ++ i) {
621 const int prevPrecision =
Ippl::Info->precision(8);
622 INFOMSG(
level2 <<
"estimated phase= " << tmp_phi <<
" rad = "
624 <<
"Ekin= " << E[N - 1] <<
" MeV" << std::setprecision(prevPrecision) <<
"\n" <<
endl);
630 for (
unsigned int i = 1; i < N; ++ i) {
635 double a = E[i], b = E2[i];
636 if (std::isnan(
a) || std::isnan(b)) {
639 t[i] = t[i - 1] +
getdT(i, E, dz, mass);
640 t2[i] = t2[i - 1] +
getdT(i, E2, dz, mass);
648 double cosine_part = 0.0, sine_part = 0.0;
653 double totalEz0 = std::cos(phi) * cosine_part - std::sin(phi) * sine_part;
655 if (p0 + q * totalEz0 * (t[1] - t[0]) *
Physics::c / mass < 0) {
657 tmp_phi = std::atan(cosine_part / sine_part);
666 const int prevPrecision =
Ippl::Info->precision(8);
668 <<
"estimated phase= " << tmp_phi <<
" rad = "
670 <<
"Ekin= " << E[N - 1] <<
" MeV" << std::setprecision(prevPrecision) <<
"\n" <<
endl);
680 std::ofstream *out) {
692 if (out) *out << std::setw(18) << z[2]
695 while (z(2) + dz < zend && z(2) + dz > zbegin) {
697 integrator.
push(z, p, dt);
702 if (z(2) >= zbegin && z(2) <= zend) {
705 integrator.
kick(z, p, Ef, Bf, dt);
707 dz = 0.5 * p(2) / std::sqrt(1.0 +
dot(p, p)) * cdt;
709 integrator.
push(z, p, dt);
713 if (out) *out << std::setw(18) << z[2]
718 const double beta = std::sqrt(1. - 1 / (
dot(p, p) + 1.));
719 const double tErr = (z(2) - zend) / (
Physics::c * beta);
721 return std::pair<double, double>(p(2), t - tErr);
734 if (length < 1e-10 &&
fieldmap_m !=
nullptr) {
737 length =
end - start;
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
Inform & level2(Inform &inf)
Inform & endl(Inform &inf)
constexpr double two_pi
The value of.
constexpr double c
The velocity of light in m/s.
constexpr double pi
The value of.
constexpr double MVpm2Vpm
double getKineticEnergy(Vector_t p, double mass)
double getBetaGamma(double Ekin, double mass)
double getGamma(Vector_t p)
virtual void visitRFCavity(const RFCavity &)=0
Apply the algorithm to a RF cavity.
Component(const std::string &name)
Constructor with given name.
PartBunchBase< double, 3 > * RefPartBunch_m
virtual const std::string & getName() const
Get element name.
bool getFlagDeleteOnTransverseExit() const
virtual double getElementLength() const
Get design length.
virtual void setElementLength(double length)
Set design length.
bool isInsideTransverse(const Vector_t &r) const
void setFrequencyModel(std::shared_ptr< AbstractTimeDependence > time_dep)
virtual bool isInside(const Vector_t &r) const override
virtual double getPhasem() const
virtual bool bends() const override
virtual double getRmax() const
void setPerpenDistance(double pdis)
void getMomentaKick(const double normalRadius, double momentum[], const double t, const double dtCorrt, const int PID, const double restMass, const int chargenumber)
used in OPAL-cycl
virtual double getAzimuth() const
virtual ElementType getType() const override
Get element type std::string.
double getdE(const int &i, const std::vector< double > &t, const double &dz, const double &phi, const double &frequency, const std::vector< double > &F) const
virtual void accept(BeamlineVisitor &) const override
Apply visitor to RFCavity.
double getdB(const int &i, const std::vector< double > &t, const double &dz, const double &frequency, const std::vector< double > &F) const
std::unique_ptr< double[]> DvDr_m
virtual void finalise() override
void setRmin(double rmin)
void setPhaseModel(std::shared_ptr< AbstractTimeDependence > time_dep)
virtual void getElementDimensions(double &begin, double &end) const override
virtual double getCosAzimuth() const
std::shared_ptr< AbstractTimeDependence > phaseTD_m
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
std::string frequencyName_m
virtual std::pair< double, double > trackOnAxisParticle(const double &p0, const double &t0, const double &dt, const double &q, const double &mass, std::ofstream *out=nullptr)
std::unique_ptr< double[]> RNormal_m
std::shared_ptr< AbstractTimeDependence > amplitudeTD_m
virtual void setPhasem(double phase)
RFCavity(const std::string &name)
Constructor with given name.
virtual double getSinAzimuth() const
void setPhi0(double phi0)
virtual bool applyToReferenceParticle(const Vector_t &R, const Vector_t &P, const double &t, Vector_t &E, Vector_t &B) override
double spline(double z, double *za)
std::shared_ptr< AbstractTimeDependence > frequencyTD_m
virtual std::string getFieldMapFN() const
void setAzimuth(double angle)
std::unique_ptr< double[]> VrNormal_m
virtual void goOnline(const double &kineticEnergy) override
void setCavityType(const std::string &type)
virtual double getCycFrequency() const
double getdA(const int &i, const std::vector< double > &t, const double &dz, const double &frequency, const std::vector< double > &F) const
virtual double getGapWidth() const
virtual double getAutoPhaseEstimate(const double &E0, const double &t0, const double &q, const double &m)
virtual void getDimensions(double &zBegin, double &zEnd) const override
static const boost::bimap< CavityType, std::string > bmCavityTypeString_s
virtual double getPerpenDistance() const
std::string amplitudeName_m
void setGapWidth(double gapwidth)
virtual double getElementLength() const override
Get design length.
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) override
virtual void goOffline() override
virtual double getPhi0() const
virtual double getAutoPhaseEstimateFallback(double E0, double t0, double q, double m)
std::string getCavityTypeString() const
void setRmax(double rmax)
void setAmplitudeModel(std::shared_ptr< AbstractTimeDependence > time_dep)
virtual double getRmin() const
double getdT(const int &i, const std::vector< double > &E, const double &dz, const double mass) const
static std::string typeset_msg(const std::string &msg, const std::string &title)
static Fieldmap getFieldmap(std::string Filename, bool fast=false)
void kick(const Vector_t &R, Vector_t &P, const Vector_t &Ef, const Vector_t &Bf, const double &dt) const
void push(Vector_t &R, const Vector_t &P, const double &dt) const
Vektor< double, 3 > Vector_t