49#include <h5core/h5_types.h>
67 const std::string& mode,
68 const std::string& binning)
69 : onebunch_m(
OpalData::getInstance()->getInputBasename() +
"-onebunch.h5")
70 , numBunch_m(numBunch)
72 , coeffDBunches_m(para)
73 , radiusLastTurn_m(0.0)
74 , radiusThisTurn_m(0.0)
92 *
gmsg <<
"***-------------------- MULTI-BUNCHES MULTI-ENERGY-BINS MODE --------------------*** " <<
endl;
100 const int BinCount = 1;
102 const int MaxBinNum = 1000;
105 size_t partInBin[MaxBinNum] = {0};
116 *
gmsg <<
"In this restart job, the multi-bunches mode is forcely set to AUTO mode." <<
endl;
119 *
gmsg <<
"In this restart job, the multi-bunches mode is forcely set to FORCE mode." <<
endl
120 <<
"If the existing bunch number is less than the specified number of TURN, "
121 <<
"readin the phase space of STEP#0 from h5 file consecutively" <<
endl;
133 *
gmsg <<
"* Store beam to H5 file for multibunch simulation ... ";
144 momentum.
create(localNum);
156 std::map<std::string, double> additionalAttributes = {
157 std::make_pair(
"REFPR", 0.0),
158 std::make_pair(
"REFPT", 0.0),
159 std::make_pair(
"REFPZ", 0.0),
160 std::make_pair(
"REFR", 0.0),
161 std::make_pair(
"REFTHETA", 0.0),
162 std::make_pair(
"REFZ", 0.0),
163 std::make_pair(
"AZIMUTH", 0.0),
164 std::make_pair(
"ELEVATION", 0.0),
165 std::make_pair(
"REFSOURCE", 0.0),
166 std::make_pair(
"B-ref_x", 0.0),
167 std::make_pair(
"B-ref_z", 0.0),
168 std::make_pair(
"B-ref_y", 0.0),
169 std::make_pair(
"E-ref_x", 0.0),
170 std::make_pair(
"E-ref_z", 0.0),
171 std::make_pair(
"E-ref_y", 0.0),
172 std::make_pair(
"B-head_x", 0.0),
173 std::make_pair(
"B-head_z", 0.0),
174 std::make_pair(
"B-head_y", 0.0),
175 std::make_pair(
"E-head_x", 0.0),
176 std::make_pair(
"E-head_z", 0.0),
177 std::make_pair(
"E-head_y", 0.0),
178 std::make_pair(
"B-tail_x", 0.0),
179 std::make_pair(
"B-tail_z", 0.0),
180 std::make_pair(
"B-tail_y", 0.0),
181 std::make_pair(
"E-tail_x", 0.0),
182 std::make_pair(
"E-tail_z", 0.0),
183 std::make_pair(
"E-tail_y", 0.0)
188 h5wrapper.
writeStep(beam, additionalAttributes);
201 *
gmsg <<
"* Read beam from H5 file for multibunch simulation ... ";
218 if ( numParticles == 0 ) {
225 size_t firstParticle = numParticlesPerNode *
Ippl::myNode();
226 size_t lastParticle = firstParticle + numParticlesPerNode - 1;
228 lastParticle = numParticles - 1;
232 numParticles = lastParticle - firstParticle + 1;
237 std::unique_ptr<PartBunchBase<double, 3> > tmpBunch =
nullptr;
240 if ( amrbunch_p !=
nullptr ) {
247 tmpBunch->
create(numParticles);
249 h5wrapper.
readStep(tmpBunch.get(), firstParticle, lastParticle);
252 beam->
create(numParticles);
254 for(
size_t ii = 0; ii < numParticles; ++ ii, ++ localNum) {
255 beam->
R[localNum] = tmpBunch->R[ii];
256 beam->
P[localNum] = tmpBunch->P[ii];
257 beam->
M[localNum] = tmpBunch->M[ii];
258 beam->
Q[localNum] = tmpBunch->Q[ii];
260 beam->
Bin[localNum] = bunchNum;
261 beam->
bunchNum[localNum] = bunchNum;
277 bool& flagTransition) {
291 *
gmsg <<
"* MBM: Checking for automatically injecting new bunch ..." <<
endl;
301 double XYrms = std::hypot(Rrms[0], Rrms[1]);
308 flagTransition =
true;
313 *
gmsg <<
"* MBM: XYrms = " << XYrms <<
" [m]" <<
endl;
328 *
gmsg <<
"* MBM: Injecting a new bunch ..." <<
endl;
344 "We shouldn't be here in single bunch mode.");
350 <<
" injected, total particle number = "
380 static const std::map<std::string, MultiBunchMode> stringMBMode_s = {
384 mode_m = stringMBMode_s.at(mbmode);
388 *
gmsg <<
"FORCE mode: The multi bunches will be injected consecutively" <<
endl
389 <<
" after each revolution, until get \"TURNS\" bunches." <<
endl;
392 *
gmsg <<
"AUTO mode: The multi bunches will be injected only when the" <<
endl
393 <<
" distance between two neighboring bunches is below" <<
endl
394 <<
" the limitation. The control parameter is set to "
399 "Unknown \"MBMODE\" multi-bunch mode for OPAL-cycl");
406 static const std::map<std::string, MultiBunchBinning> stringMBBinning_s = {
410 binning_m = stringMBBinning_s.at(binning);
414 *
gmsg <<
"\nUse 'BUNCH_BINNING' injection for binnning.\n" <<
endl;
417 *
gmsg <<
"\nUse 'GAMMA_BINNING' injection for binnning.\n" <<
endl;
421 "Unknown \"MB_BINNING\" type of energy binning in multi-bunch mode");
434 *
gmsg <<
"Radial position at restart position = ";
436 *
gmsg <<
"Initial radial position = ";
449 const unsigned long localNum = beam->
getLocalNum();
451 long int bunchTotalNum = 0;
452 unsigned long bunchLocalNum = 0;
462 std::vector<double> local(7 * dim + 1);
466 for(
unsigned long k = 0; k < localNum; ++k) {
467 if ( beam->
bunchNum[k] != bunchNr ) {
475 local[0] += std::sqrt(
dot(beam->
P[k], beam->
P[k]) + 1.0);
477 for (
unsigned int i = 0; i < dim; ++i) {
479 double r = beam->
R[k](i);
480 double p = beam->
P[k](i);
486 local[i + dim + 1] += p;
490 local[i + 2 * dim + 1] += r2;
493 local[i + 3 * dim + 1] += p * p;
496 local[i + 4 * dim + 1] += r * p;
499 local[i + 5 * dim + 1] += r2 * r;
502 local[i + 6 * dim + 1] += r2 * r2;
507 allreduce(bunchTotalNum, 1, std::plus<long int>());
511 throw OpalException(
"MultiBunchHandler::calcBunchBeamParameters()",
512 "Bunch number " + std::to_string(bunchNr) +
513 " exceeds bunch index " + std::to_string(beam->
getNumBunch() - 1));
518 if (bunchTotalNum == 0) {
524 local[0] -= bunchLocalNum;
527 allreduce(local.data(), local.size(), std::plus<double>());
529 double invN = 1.0 / double(bunchTotalNum);
530 binfo.
ekin = local[0] * invN;
535 for (
unsigned int i = 0; i < dim; ++i) {
537 double w = local[i + 1] * invN;
538 double pw = local[i + dim + 1] * invN;
539 double w2 = local[i + 2 * dim + 1] * invN;
540 double pw2 = local[i + 3 * dim + 1] * invN;
541 double wpw = local[i + 4 * dim + 1] * invN;
542 double w3 = local[i + 5 * dim + 1] * invN;
543 double w4 = local[i + 6 * dim + 1] * invN;
549 binfo.
prms[i] = pw2 - pw * pw;
550 if ( binfo.
prms[i] < 0 ) {
558 binfo.
rrms[i] = w2 - w * w;
561 binfo.
emit[i] = (binfo.
rrms[i] * binfo.
prms[i] - wpw * wpw);
562 binfo.
emit[i] = std::sqrt(std::max(binfo.
emit[i], 0.0));
568 - 3.0 * w * w * w * w;
569 binfo.
halo[i] = tmp / ( binfo.
rrms[i] * binfo.
rrms[i] );
572 binfo.
rrms[i] = std::sqrt(binfo.
rrms[i]);
575 binfo.
prms[i] = std::sqrt(binfo.
prms[i]);
578 double denom = 1.0 / (binfo.
rrms[i] * binfo.
prms[i]);
579 binfo.
correlation[i] = (std::isfinite(denom)) ? wpw * denom : 0.0;
582 double tmp = 1.0 / std::pow(binfo.
ekin / m0 + 1.0, 2.0);
583 binfo.
dEkin = binfo.
prms[1] * m0 * std::sqrt(1.0 - tmp);
599 binfo_m[b].pathlength += lpaths[b];
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
void allreduce(const T *input, T *output, int count, Op op)
Inform & endl(Inform &inf)
ParticleAttrib< int > Bin
bool resetPartBinID2(const double eta)
reset Bin[] for each particle according to the method given in paper PAST-AB(064402) by G....
void setNumBunch(short n)
size_t getLocalNum() const
ParticleAttrib< double > M
size_t getTotalNum() const
ParticleAttrib< Vector_t > P
void setLocalNumPerBunch(size_t numpart, short n)
ParticleAttrib< ParticleOrigin > POrigin
Vector_t get_rrms() const
ParticleAttrib< double > Q
void calcBeamParameters()
short getNumBunch() const
Vector_t get_centroid() const
ParticleAttrib< short > bunchNum
void setTotalNumPerBunch(size_t numpart, short n)
void setPBins(PartBins *pbin)
The global OPAL structure.
static OpalData * getInstance()
void saveBunch(PartBunchBase< double, 3 > *beam)
bool calcBunchBeamParameters(PartBunchBase< double, 3 > *beam, short bunchNr)
MultiBunchHandler(PartBunchBase< double, 3 > *beam, const int &numBunch, const double &eta, const double ¶, const std::string &mode, const std::string &binning)
MultiBunchBinning binning_m
beaminfo_t & getBunchInfo(short bunchNr)
void setMode(const std::string &mbmode)
void updateTime(const double &dt)
void setBinning(const std::string &binning)
void setRadiusTurns(const double &radius)
short numBunch_m
The number of bunches specified in TURNS of RUN command.
bool readBunch(PartBunchBase< double, 3 > *beam, const PartData &ref)
void updatePathLength(const std::vector< double > &lpaths)
short injectBunch(PartBunchBase< double, 3 > *beam, const PartData &ref, bool &flagTransition)
std::vector< beaminfo_t > binfo_m
void updateParticleBins(PartBunchBase< double, 3 > *beam)
long unsigned int nParticles
virtual void create(size_t)
pbase_t * getAmrParticleBase()
size_t getNumParticles() const
virtual void writeStep(PartBunchBase< double, 3 > *, const std::map< std::string, double > &additionalStepAttributes)
virtual void writeHeader()
virtual void readStep(PartBunchBase< double, 3 > *, h5_ssize_t firstParticle, h5_ssize_t lastParticle)
The base class for all OPAL exceptions.
static Communicate * Comm
Timing::TimerRef TimerRef
static TimerRef getTimer(const char *nm)
static void stopTimer(TimerRef t)
static void startTimer(TimerRef t)