42 : itsReference_m(ref), itsCavity_m(cavity) {
43 double zbegin = 0.0, zend = 0.0;
44 cavity->getDimensions(zbegin, zend);
56 "CavityAutophaser::getPhaseAtMaxEnergy()",
"given element is not a cavity");
64 double originalPhase = element->
getPhasem();
66 double optimizedPhase = 0.0;
67 double finalEnergy = 0.0;
68 double newPhase = 0.0;
75 element->
setPhasem(optimizedPhase + originalPhase);
78 originalPhase += optimizedPhase;
87 ss << std::setw(2) << std::left <<
c;
89 *
ippl::Info <<
level1 <<
"\n* ************* " << std::left << std::setw(68) << std::setfill(
'*')
90 << ss.str() << std::setfill(
' ') <<
endl;
93 double AstraPhase = 0.0;
96 if (amplitude < 0.0) {
97 amplitude = -amplitude;
103 if (amplitude == 0.0 && designEnergy <= 0.0) {
105 "CavityAutophaser::getPhaseAtMaxEnergy()",
106 "neither amplitude or design energy given to cavity " + element->
getName());
109 if (designEnergy > 0.0) {
110 const double length =
itsCavity_m->getElementLength();
113 "CavityAutophaser::getPhaseAtMaxEnergy()",
114 "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)
133 amplitude *= std::abs(designEnergy / finalEnergy);
135 initialPhase = optimizedPhase;
143 optimizedPhase = status.first;
144 finalEnergy = status.second;
153 opal->setMaxPhase(
itsCavity_m->getName(), newPhase);
157 if (!opal->isOptimizerRun()) {
159 {opal->getAuxiliaryOutputDirectory(),
itsCavity_m->getName() +
"_AP.dat"});
160 std::ofstream out(fname);
161 track(t + tErr, dt, newPhase, &out);
164 track(t + tErr, dt, newPhase,
nullptr);
169 <<
"corresp. in Astra = " << AstraPhase *
Units::rad2deg <<
" [deg],\n"
170 <<
"E = " << finalEnergy <<
" [MeV], "
172 <<
"Ez_0 = " << amplitude <<
" [MV/m]"
180 finalEnergy = status.second;
191 <<
"corresp. in Astra = " << AstraPhase *
Units::rad2deg <<
" [deg],\n"
192 <<
"E = " << finalEnergy <<
" [MeV], "
194 <<
"Ez_0 = " << amplitude <<
" [MV/m]"
202 optimizedPhase = originalPhase;
204 *
ippl::Info <<
level1 <<
"* " << std::right << std::setw(83) << std::setfill(
'*') <<
"*\n"
205 << std::setfill(
' ') <<
endl;
207 return optimizedPhase;
229 double initialPhase,
double t,
double dt) {
231 double originalPhase = element->
getPhasem();
236 double E =
track(t, dt, phase);
237 std::pair<double, double> status(originalPhase, E);
241 double Phimax = initialPhase;
242 double phi = initialPhase;
247 double E =
track(t, dt, phi);
255 E =
track(t, dt, phi);
267 E =
track(t, dt, phi);
271 for (
int rl = 0; rl < numRefinements; ++rl) {
273 phi = initialPhase - dphi;
274 E =
track(t, dt, phi);
279 phi = initialPhase + dphi;
280 E =
track(t, dt, phi);
288 E =
track(t, dt, Phimax + originalPhase);
289 std::pair<double, double> status(Phimax, E);
295 double t,
const double dt,
const double phase, std::ofstream* out)
const {
308 return finalKineticEnergy;
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
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(ippl::Vector< double, 3 > p, double mass)
std::unique_ptr< Inform > Info
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
void setMaxPhase(std::string elName, double phi)
static OpalData * getInstance()
Vector_t< double, 3 > initialR_m
double getPhaseAtMaxEnergy(const Vector_t< double, 3 > &R, const Vector_t< double, 3 > &P, double t, double dt)
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)
Vector_t< double, 3 > initialP_m
std::shared_ptr< Component > itsCavity_m
CavityAutophaser(const PartData &ref, std::shared_ptr< Component > cavity)
KOKKOS_INLINE_FUNCTION double getM() const
The constant mass per particle.
KOKKOS_INLINE_FUNCTION double getQ() const
The constant charge per particle.
The base class for all OPAL exceptions.