38 std::shared_ptr<Component> cavity):
42 double zbegin = 0.0, zend = 0.0;
43 cavity->getDimensions(zbegin, zend);
57 throw OpalException(
"CavityAutophaser::getPhaseAtMaxEnergy()",
58 "given element is not a cavity");
66 double originalPhase = element->
getPhasem();
68 double optimizedPhase = 0.0;
69 double finalEnergy = 0.0;
70 double newPhase = 0.0;
77 element->
setPhasem(optimizedPhase + originalPhase);
80 originalPhase += optimizedPhase;
89 ss << std::setw(2) << std::left <<
c;
92 << std::left << std::setw(68) << std::setfill(
'*') << ss.str()
93 << std::setfill(
' ') <<
endl);
96 double AstraPhase = 0.0;
99 if (amplitude < 0.0) {
100 amplitude = -amplitude;
106 if (amplitude == 0.0 && designEnergy <= 0.0) {
107 throw OpalException(
"CavityAutophaser::getPhaseAtMaxEnergy()",
108 "neither amplitude or design energy given to cavity " + element->
getName());
111 if (designEnergy > 0.0) {
112 const double length =
itsCavity_m->getElementLength();
114 throw OpalException(
"CavityAutophaser::getPhaseAtMaxEnergy()",
115 "length of cavity " + element->
getName() +
" is zero");
123 while (count < 1000) {
127 optimizedPhase = status.first;
128 finalEnergy = status.second;
130 if (
std::abs(designEnergy - finalEnergy) < 1
e-7)
break;
132 amplitude *=
std::abs(designEnergy / finalEnergy);
134 initialPhase = optimizedPhase;
141 optimizedPhase = status.first;
142 finalEnergy = status.second;
151 opal->setMaxPhase(
itsCavity_m->getName(), newPhase);
155 if (!opal->isOptimizerRun()) {
157 opal->getAuxiliaryOutputDirectory(),
160 std::ofstream out(fname);
161 track(t + tErr, dt, newPhase, &out);
164 track(t + tErr, dt, newPhase, NULL);
170 <<
"E = " << finalEnergy <<
" [MeV], " <<
"phi_nom = " << originalPhase *
Physics::rad2deg <<
" [deg]\n"
171 <<
"Ez_0 = " << amplitude <<
" [MV/m]" <<
"\n"
172 <<
"time = " << (t + tErr) * 1e9 <<
" [ns], dt = " << dt * 1e12 <<
" [ps]" <<
endl);
177 finalEnergy = status.second;
188 <<
"E = " << finalEnergy <<
" [MeV], " <<
"phi_nom = " << originalPhase *
Physics::rad2deg <<
" [deg]\n"
189 <<
"Ez_0 = " << amplitude <<
" [MV/m]" <<
"\n"
190 <<
"time = " << (t + tErr) * 1e9 <<
" [ns], dt = " << dt * 1e12 <<
" [ps]" <<
endl);
195 optimizedPhase = originalPhase;
197 INFOMSG(
level1 <<
"* " << std::right << std::setw(83) << std::setfill(
'*') <<
"*\n"
198 << std::setfill(
' ') <<
endl);
200 return optimizedPhase;
227 double originalPhase = element->
getPhasem();
232 double E =
track(t, dt, phase);
233 std::pair<double, double> status(originalPhase, E);
237 double Phimax = initialPhase;
238 double phi = initialPhase;
243 double E =
track(t, dt, phi);
251 E =
track(t, dt, phi);
263 E =
track(t, dt, phi);
267 for(
int rl = 0; rl < numRefinements; ++ rl) {
269 phi = initialPhase - dphi;
270 E =
track(t, dt, phi);
275 phi = initialPhase + dphi;
276 E =
track(t, dt, phi);
285 E =
track(t, dt, Phimax + originalPhase);
286 std::pair<double, double> status(Phimax, E);
294 std::ofstream *out)
const {
311 return finalKineticEnergy;
Tps< T > sqrt(const Tps< T > &x)
Square root.
T euclidean_norm(const Vector< T > &)
Euclidean norm.
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
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 & level1(Inform &inf)
Inform & endl(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.
constexpr double rad2deg
The conversion factor from radians to degrees.
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()
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
double track(double t, const double dt, const double phase, std::ofstream *out=NULL) const
CavityAutophaser(const PartData &ref, std::shared_ptr< Component > cavity)
virtual const std::string & getName() const
Get element name.
virtual double getPhasem() const
virtual std::pair< double, double > trackOnAxisParticle(const double &p0, const double &t0, const double &dt, const double &q, const double &mass, std::ofstream *out=NULL)
virtual double getAmplitudem() const
virtual bool getAutophaseVeto() const
virtual void setAmplitudem(double vPeak)
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