39 std::shared_ptr<Component> cavity):
43 double zbegin = 0.0, zend = 0.0;
44 cavity->getDimensions(zbegin, zend);
58 throw OpalException(
"CavityAutophaser::getPhaseAtMaxEnergy()",
59 "given element is not a cavity");
67 double originalPhase = element->
getPhasem();
69 double optimizedPhase = 0.0;
70 double finalEnergy = 0.0;
71 double newPhase = 0.0;
78 element->
setPhasem(optimizedPhase + originalPhase);
81 originalPhase += optimizedPhase;
90 ss << std::setw(2) << std::left <<
c;
93 << std::left << std::setw(68) << std::setfill(
'*') << ss.str()
94 << std::setfill(
' ') <<
endl);
97 double AstraPhase = 0.0;
100 if (amplitude < 0.0) {
101 amplitude = -amplitude;
107 if (amplitude == 0.0 && designEnergy <= 0.0) {
108 throw OpalException(
"CavityAutophaser::getPhaseAtMaxEnergy()",
109 "neither amplitude or design energy given to cavity " + element->
getName());
112 if (designEnergy > 0.0) {
113 const double length =
itsCavity_m->getElementLength();
115 throw OpalException(
"CavityAutophaser::getPhaseAtMaxEnergy()",
116 "length of cavity " + element->
getName() +
" is zero");
124 while (count < 1000) {
128 optimizedPhase = status.first;
129 finalEnergy = status.second;
131 if (
std::abs(designEnergy - finalEnergy) < 1
e-7)
break;
133 amplitude *=
std::abs(designEnergy / finalEnergy);
135 initialPhase = optimizedPhase;
142 optimizedPhase = status.first;
143 finalEnergy = status.second;
152 opal->setMaxPhase(
itsCavity_m->getName(), newPhase);
156 if (!opal->isOptimizerRun()) {
158 opal->getAuxiliaryOutputDirectory(),
161 std::ofstream out(fname);
162 track(t + tErr, dt, newPhase, &out);
165 track(t + tErr, dt, newPhase,
nullptr);
170 <<
"corresp. in Astra = " << AstraPhase *
Units::rad2deg <<
" [deg],\n"
171 <<
"E = " << finalEnergy <<
" [MeV], " <<
"phi_nom = " << originalPhase *
Units::rad2deg <<
" [deg]\n"
172 <<
"Ez_0 = " << amplitude <<
" [MV/m]" <<
"\n"
178 finalEnergy = status.second;
188 <<
"corresp. in Astra = " << AstraPhase *
Units::rad2deg <<
" [deg],\n"
189 <<
"E = " << finalEnergy <<
" [MeV], " <<
"phi_nom = " << originalPhase *
Units::rad2deg <<
" [deg]\n"
190 <<
"Ez_0 = " << amplitude <<
" [MV/m]" <<
"\n"
196 optimizedPhase = originalPhase;
198 INFOMSG(
level1 <<
"* " << std::right << std::setw(83) << std::setfill(
'*') <<
"*\n"
199 << std::setfill(
' ') <<
endl);
201 return optimizedPhase;
228 double originalPhase = element->
getPhasem();
233 double E =
track(t, dt, phase);
234 std::pair<double, double> status(originalPhase, E);
238 double Phimax = initialPhase;
239 double phi = initialPhase;
244 double E =
track(t, dt, phi);
252 E =
track(t, dt, phi);
264 E =
track(t, dt, phi);
268 for(
int rl = 0; rl < numRefinements; ++ rl) {
270 phi = initialPhase - dphi;
271 E =
track(t, dt, phi);
276 phi = initialPhase + dphi;
277 E =
track(t, dt, phi);
286 E =
track(t, dt, Phimax + originalPhase);
287 std::pair<double, double> status(Phimax, E);
295 std::ofstream *out)
const {
312 return finalKineticEnergy;
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Tps< T > sqrt(const Tps< T > &x)
Square root.
T euclidean_norm(const Vector< T > &)
Euclidean norm.
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
PETE_TBTree< FnFmod, PETE_Scalar< Vektor< T1, Dim > >, typename T2::PETE_Expr_t > fmod(const Vektor< T1, Dim > &l, const PETE_Expr< T2 > &r)
Inform & endl(Inform &inf)
Inform & level1(Inform &inf)
constexpr double two_pi
The value of.
constexpr double e
The value of.
constexpr double c
The velocity of light in m/s.
constexpr double pi
The value of.
std::string combineFilePath(std::initializer_list< std::string > ilist)
double getKineticEnergy(Vector_t p, double mass)
void setMaxPhase(std::string elName, double phi)
static OpalData * getInstance()
double track(double t, const double dt, const double phase, std::ofstream *out=nullptr) const
const PartData & itsReference_m
double guessCavityPhase(double t)
std::pair< double, double > optimizeCavityPhase(double initialGuess, double t, double dt)
double getPhaseAtMaxEnergy(const Vector_t &R, const Vector_t &P, double t, double dt)
std::shared_ptr< Component > itsCavity_m
CavityAutophaser(const PartData &ref, std::shared_ptr< Component > cavity)
virtual const std::string & getName() const
Get element name.
virtual double getPhasem() const
virtual double getAmplitudem() const
virtual bool getAutophaseVeto() const
virtual void setAmplitudem(double vPeak)
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)
virtual void setPhasem(double phase)
virtual double getFrequencym() const
virtual void setAutophaseVeto(bool veto=true)
virtual double getAutoPhaseEstimate(const double &E0, const double &t0, const double &q, const double &m)
virtual double getDesignEnergy() const override
double getQ() const
The constant charge per particle.
double getM() const
The constant mass per particle.
The base class for all OPAL exceptions.
Vektor< double, 3 > Vector_t