49#define CHECK_CYC_FSCANF_EOF(arg) if (arg == EOF)\
50throw GeneralClassicException("Cyclotron::getFieldFromFile",\
51 "fscanf returned EOF at " #arg);
62 fieldType_m(right.fieldType_m),
63 fmapfn_m(right.fmapfn_m),
64 rffrequ_m(right.rffrequ_m),
65 rfphi_m(right.rfphi_m),
66 escale_m(right.escale_m),
67 superpose_m(right.superpose_m),
68 symmetry_m(right.symmetry_m),
69 rinit_m(right.rinit_m),
70 prinit_m(right.prinit_m),
71 phiinit_m(right.phiinit_m),
72 zinit_m(right.zinit_m),
73 pzinit_m(right.pzinit_m),
74 spiralFlag_m(right.spiralFlag_m),
75 trimCoilThreshold_m(right.trimCoilThreshold_m),
76 typeName_m(right.typeName_m),
78 bscale_m(right.bscale_m),
79 trimcoils_m(right.trimcoils_m),
84 fmLowE_m(right.fmLowE_m),
85 fmHighE_m(right.fmHighE_m),
86 RFfilename_m(right.RFfilename_m),
87 RFFCoeff_fn_m(right.RFFCoeff_fn_m),
88 RFVCoeff_fn_m(right.RFVCoeff_fn_m) {
100 const double tet_rad,
101 double* br,
double* bz) {
103 trimcoil->applyField(r, z, tet_rad, br, bz);
108 const double tet_rad,
109 double& br,
double& bz) {
123 std::fill(
t,
t +
size, std::numeric_limits<double>::quiet_NaN());
129 double& cosPhase,
double& sinPhase) {
132 if (cache.
t[k] == t) {
142 cosPhase = std::cos(phase);
143 sinPhase = std::sin(phase);
144 cache.
t[cache.
next] = t;
158 const double rad = std::hypot(R[0], R[1]);
160 return rad > 0.0 && std::isfinite(R[2]) && ir >= 0 && ir <=
Bfield_m.
nrad_m - 3;
234 "Cyclotron::getFieldMapFN",
235 "The attribute FMAPFN isn't set for the CYCLOTRON element");
236 }
else if (std::filesystem::exists(
fmapfn_m)) {
240 "Failed to open file '" +
fmapfn_m +
241 "', please check if it exists");
266 "RFPHI not defined for CYCLOTRON");
279 "RFFREQ not defined for CYCLOTRON");
292 "SUPERPOSE not defined for CYCLOTRON");
340 "ESCALE not defined for CYCLOTRON");
373 "MINR must be positive");
382 "The attribute MAXR has to be higher than MINR");
403 "The attribute MAXZ has to be higher than MINZ");
428 static const std::map<std::string, BFieldType> typeStringToBFieldType_s = {
448 if ( (refZ < minz_m || refZ >
maxz_m) ||
449 (refR < minr_m || refR >
maxr_m) ) {
451 "Cyclotron::checkInitialReferenceParticle",
452 "The initial position of the reference particle (RINIT, ZINIT) "
453 "must be in the range of (MINR, MAXR) and (MINZ, MAXZ)");
458 "PHIINIT is out of [-180, 180)");
464 bool flagNeedUpdate =
false;
473 const bool outsideAperture = (zpos >
maxz_m || zpos < minz_m || rpos >
maxr_m || rpos <
minr_m);
475 if (outsideAperture) {
476 flagNeedUpdate =
true;
481 if (flagNeedUpdate && !flaggedBefore) {
483 << (outsideAperture ?
" out of the global aperture of cyclotron!"
484 :
" out of the field map boundary!") <<
endl;
498 return flagNeedUpdate;
512 if (std::abs(R[0]) < 1.0E-10) {
518 }
else if (R[0] < 0.0) {
522 tet = std::atan(R[1] / R[0]);
532 double brint = 0.0, btint = 0.0, bzint = 0.0;
534 const double rad = std::hypot(R[0],R[1]);
535 if ( this->
interpolate(rad, tet, brint, btint, bzint) ) {
538 double br = - brint * R[2];
541 double bt = - btint / rad * R[2];
549 B[0] = br * std::cos(tet) - bt * std::sin(tet);
550 B[1] = br * std::sin(tet) + bt * std::cos(tet);
562 std::vector<Fieldmap>::const_iterator fi =
RFfields_m.begin();
563 std::vector<double>::const_iterator rffi =
rffrequ_m.begin();
564 std::vector<double>::const_iterator rfphii =
rfphi_m.begin();
565 std::vector<double>::const_iterator escali =
escale_m.begin();
566 std::vector<bool>::const_iterator superposei =
superpose_m.begin();
567 std::vector<std::vector<double>>::const_iterator rffci =
rffc_m.begin();
568 std::vector<std::vector<double>>::const_iterator rfvci =
rfvc_m.begin();
570 double xBegin(0), xEnd(0), yBegin(0), yEnd(0), zBegin(0), zEnd(0);
573 for (; fi !=
RFfields_m.end(); ++fi, ++rffi, ++rfphii, ++escali, ++superposei) {
574 (*fi)->getFieldDimensions(xBegin, xEnd, yBegin, yEnd, zBegin, zEnd);
575 if (fcount > 0 && *superposei ==
false)
continue;
581 if (temp_R(0) < xBegin || temp_R(0) > xEnd ||
582 temp_R(1) < yBegin || temp_R(1) > yEnd ||
583 temp_R(2) < zBegin || temp_R(2) > zEnd)
continue;
585 Vector_t tmpE({0.0, 0.0, 0.0}), tmpB({0.0, 0.0, 0.0});
587 if ((*fi)->getFieldstrength(temp_R, tmpE, tmpB))
continue;
591 double frequency = (*rffi);
592 double ebscale = (*escali);
596 for (
const double fcoef : (*rffci)) {
598 frequency += fcoef * powert;
601 for (
const double vcoef : (*rfvci)) {
603 ebscale += vcoef * powert;
607 double phase = 0.0, cosPhase = 0.0, sinPhase = 0.0;
608 const std::size_t map = fi -
RFfields_m.begin();
611 cosPhase = std::cos(phase);
612 sinPhase = std::sin(phase);
614 rfPhase(map, t, frequency, *rfphii, cosPhase, sinPhase);
617 E += ebscale * cosPhase * tmpE;
618 B -= ebscale * sinPhase * tmpB;
626 phase_print = std::fmod(phase_print, 360) - 360.0;
628 *
gmsg <<
endl <<
"Gap 1 phase = " << phase_print <<
" Deg" <<
endl;
629 *
gmsg <<
"Gap 1 E-Field = (" << E[0] <<
"/" << E[1] <<
"/" << E[2] <<
")" <<
endl;
630 *
gmsg <<
"Gap 1 B-Field = (" << B[0] <<
"/" << B[1] <<
"/" << B[2] <<
")" <<
endl;
631 *
gmsg <<
"RF Frequency = " << frequency <<
" MHz" <<
endl;
635 phase_print = std::fmod(phase_print, 360) - 360.0;
637 *
gmsg <<
endl <<
"Gap 2 phase = " << phase_print <<
" Deg" <<
endl;
638 *
gmsg <<
"Gap 2 E-Field = (" << E[0] <<
"/" << E[1] <<
"/" << E[2] <<
")" <<
endl;
639 *
gmsg <<
"Gap 2 B-Field = (" << B[0] <<
"/" << B[1] <<
"/" << B[2] <<
")" <<
endl;
640 *
gmsg <<
"RF Frequency = " << frequency <<
" MHz" <<
endl;
651 const double& tet_rad,
double& br,
652 double& bt,
double& bz) {
669 const int krl,
const int lpr) {
671 double C[5][5][3], FAC[3];
772 for (j = 0; j < 5; j++) {
773 result += C[j][krl][kor] * *(f + j * lpr);
776 return result / (FAC[kor] * std::pow(dx, (kor + 1)));
781 const double& tet_rad,
789 const int ir = (int)xir;
792 const double wr1 = xir - (double)ir;
794 const double wr2 = 1.0 - wr1;
803 const double wt1 = xit - (double)it;
804 const double wt2 = 1.0 - wt1;
811 int r1t1, r2t1, r1t2, r2t2;
821 r1t1 = it + ntetS * ir - 1;
832 r2t1 =
idx(ir + 1, it);
833 r1t2 =
idx(ir , it + 1);
834 r2t2 =
idx(ir + 1, it + 1);
866 "The attribute TYPE isn't set for the CYCLOTRON element");
870 *
gmsg <<
"* Read field data from PSI format field map file" <<
endl;
875 *
gmsg <<
"* Read data from 450MeV Carbon cyclotron field file" <<
endl;
880 *
gmsg <<
"* Read data from 100MeV H- cyclotron CYCIAE-100 field file" <<
endl;
885 *
gmsg <<
"* Read AVFEQ data (Riken)" <<
endl;
890 *
gmsg <<
"* Read FFA data MSU/FNAL" <<
endl;
895 *
gmsg <<
"* Read both median plane B field map and 3D E field map of RF cavity for compact cyclotron" <<
endl;
900 *
gmsg <<
"* Read midplane B-field, 3D RF fieldmaps, and text files with RF frequency/Voltage coefficients for Synchrocyclotron" <<
endl;
906 "Unknown TYPE for the CYCLOTRON element");
924 auto allZero = [](
const std::vector<double>& v) {
925 return std::all_of(v.begin(), v.end(), [](
double x) { return x == 0.0; });
930 std::isfinite(sectorSteps) && sectorSteps < 2.0 * ntetS - 2.0 &&
934 *
gmsg <<
"* The midplane field map is zero everywhere: it adds no field and only "
935 <<
"limits the radius to r < "
965 kEdge = std::max(k - 2, 0);
968 int dkFromEdge = k - kEdge;
969 int index =
idx(i, k);
970 int indexkEdge =
idx(i, kEdge);
984 int iredg = std::max(i - 2, 0);
986 int irtak = i - iredg;
987 int index =
idx(i, k);
988 int indexredg =
idx(iredg, k);
1017 int istart =
idx(i, 0);
1055 for (
int i = 0; i < nrad; i++) {
1070 this->
read(scaleFactor);
1082 *
gmsg <<
"* ----------------------------------------------" <<
endl;
1083 *
gmsg <<
"* READ IN RING FIELD MAP " <<
endl;
1084 *
gmsg <<
"* (The first data block is useless) " <<
endl;
1085 *
gmsg <<
"* ----------------------------------------------" <<
endl;
1089 f = std::fopen(
fmapfn_m.c_str(),
"r");
1092 *
gmsg <<
"* Minimal radius of measured field map: " <<
BP_m.
rmin_m <<
" [mm]" <<
endl;
1109 for (
int i = 0; i < 13; i++) {
1122 for (
int i = 0; i < 5; i++) {
1128 for (
int i = 0; i < 4; i++) {
1132 for (
int i = 0; i < lpar; i++) {
1135 for (
int i = 0; i < 6; i++) {
1139 for (
int i = 0; i < 10000; i++) {
1141 if (std::strcmp(fout,
"LREC=") == 0)
break;
1144 for (
int i = 0; i < 5; i++) {
1155 *
gmsg <<
"* Read-in loop one block per radius" <<
endl;
1159 for (
int dummy = 0; dummy < 6; dummy++) {
1182 *
gmsg <<
"* Field Map read successfully!" <<
endl <<
endl;
1207 std::vector<double> rv;
1208 std::vector<double> thv;
1209 std::vector<double> xv;
1210 std::vector<double> yv;
1211 std::vector<double> bzv;
1212 std::vector<double>::iterator vit;
1214 *
gmsg <<
"* ----------------------------------------------" <<
endl;
1215 *
gmsg <<
"* READ IN FFA FIELD MAP " <<
endl;
1216 *
gmsg <<
"* ----------------------------------------------" <<
endl;
1220 std::ifstream file_to_read(
fmapfn_m.c_str());
1221 const int max_num_of_char_in_a_line = 128;
1222 const int num_of_header_lines = 1;
1225 for (
int i = 0; i < num_of_header_lines; ++i)
1226 file_to_read.ignore(max_num_of_char_in_a_line,
'\n');
1228 while(!file_to_read.eof()) {
1229 double r, th, x, y, bz;
1230 file_to_read >> r >> th >> x >> y >> bz;
1231 if ((
int)th != 360) {
1240 double maxtheta = 360.0;
1243 double rmax = rv.back();
1246 for (vit = rv.begin(); *vit <=
BP_m.
rmin_m; ++vit) {}
1255 *
gmsg <<
"* Maximal radius of measured field map: " <<
Units::m2mm * rmax <<
" [mm]" <<
endl;
1258 *
gmsg <<
"* Maximal angle of measured field map: " << maxtheta <<
" [deg]" <<
endl;
1287 *
gmsg <<
"* Number of elements read: " << count <<
endl;
1288 *
gmsg <<
"* Field Map read successfully!" <<
endl <<
endl;
1295 *
gmsg <<
"* ----------------------------------------------" <<
endl;
1296 *
gmsg <<
"* READ IN AVFEQ CYCLOTRON FIELD MAP " <<
endl;
1297 *
gmsg <<
"* ----------------------------------------------" <<
endl;
1315 f = std::fopen(
fmapfn_m.c_str(),
"r");
1318 *
gmsg <<
"* Minimal radius of measured field map: " <<
BP_m.
rmin_m <<
" [mm]" <<
endl;
1323 *
gmsg <<
"* Maximal radius of measured field map: " << rmax <<
" [mm]" <<
endl;
1335 *
gmsg <<
"* Maximal angle of measured field map: " << tetmax <<
" [deg]" <<
endl;
1351 *
gmsg <<
"* Total stored grid point number ( ntetS * nrad ): "
1377 *
gmsg <<
"* Number of elements read: " << count <<
endl;
1378 *
gmsg <<
"* Field Map read successfully!" <<
endl <<
endl;
1385 *
gmsg <<
"* ----------------------------------------------" <<
endl;
1386 *
gmsg <<
"* READ IN CARBON CYCLOTRON FIELD MAP " <<
endl;
1387 *
gmsg <<
"* ----------------------------------------------" <<
endl;
1391 f = std::fopen(
fmapfn_m.c_str(),
"r");
1394 *
gmsg <<
"* Minimal radius of measured field map: " <<
BP_m.
rmin_m <<
" [mm]" <<
endl;
1424 *
gmsg <<
"* Adding a guard cell along azimuth" <<
endl;
1445 *
gmsg <<
"* Field Map read successfully!" <<
endl <<
endl;
1455 *
gmsg <<
"* ----------------------------------------------" <<
endl;
1456 *
gmsg <<
"* READ IN CYCIAE-100 CYCLOTRON FIELD MAP " <<
endl;
1457 *
gmsg <<
"* ----------------------------------------------" <<
endl;
1461 f = std::fopen(
fmapfn_m.c_str(),
"r");
1464 *
gmsg <<
"* Minimal radius of measured field map: " <<
BP_m.
rmin_m <<
" [mm]" <<
endl;
1501 for (
int ii = 0; ii < 13; ii++) {
1504 for (
int k = 0; k < nHalfPoints; k++) {
1518 *
gmsg <<
"* Field Map read successfully!" <<
endl <<
endl;
1527 *
gmsg <<
"* Reading '" << fm <<
"'" <<
endl;
1539 std::vector<std::string>::const_iterator fm =
RFfilename_m.begin();
1540 std::vector<std::string>::const_iterator rffcfni =
RFFCoeff_fn_m.begin();
1541 std::vector<std::string>::const_iterator rfvcfni =
RFVCoeff_fn_m.begin();
1543 FILE *rffcf =
nullptr;
1544 FILE *rfvcf =
nullptr;
1547 *
gmsg <<
"* ------------------------------------------------------------" <<
endl;
1548 *
gmsg <<
"* READ IN 3D RF Fields and Frequency Coefficients " <<
endl;
1549 *
gmsg <<
"* ------------------------------------------------------------" <<
endl;
1551 for (; fm !=
RFfilename_m.end(); ++fm, ++rffcfni, ++rfvcfni) {
1558 *
gmsg <<
"RF Frequency Coefficient Filename: " << (*rffcfni) <<
endl;
1560 rffcf = std::fopen((*rffcfni).c_str(),
"r");
1562 if (rffcf ==
nullptr) {
1564 "Cyclotron::getFieldFromFile_Synchrocyclotron",
1565 "failed to open file '" + *rffcfni +
"', please check if it exists");
1568 std::vector<double> fcoeff;
1573 *
gmsg <<
"* Number of coefficients in file: " << nc <<
endl;
1574 for (
int k = 0; k < nc; k++) {
1576 fcoeff.push_back(value);
1579 rffc_m.push_back(fcoeff);
1584 *
gmsg <<
"RF Voltage Coefficient Filename: " << (*rfvcfni) <<
endl;
1586 rfvcf = std::fopen((*rfvcfni).c_str(),
"r");
1587 if (rfvcf ==
nullptr) {
1589 "Cyclotron::getFieldFromFile_Synchrocyclotron",
1590 "failed to open file '" + *rfvcfni +
"', please check if it exists");
1593 std::vector<double> vcoeff;
1596 *
gmsg <<
"* Number of coefficients in file: " << nc <<
endl;
1597 for (
int k = 0; k < nc; k++) {
1599 vcoeff.push_back(value);
1602 rfvc_m.push_back(vcoeff);
1621 fp1.open(fname, std::ios::out);
1639 fp2.open(fname, std::ios::out);
1643 Vector_t tmpE({0.0, 0.0, 0.0}), tmpB({0.0, 0.0, 0.0});
1645 Vector_t E({0.0, 0.0, 0.0}), B({0.0, 0.0, 0.0});
1646 if (!fi->getFieldstrength(tmpR, tmpE, tmpB)) {
1651 fp2 << tmpR <<
" \t E= " << tmpE <<
"\t B= " << tmpB << std::endl;
1654 *
gmsg <<
"\n* Writing 'gnu.out' and 'eb.out' files of cyclotron field maps\n" <<
endl;
1657 *
gmsg <<
"\n* Writing 'gnu.out' file of cyclotron field map\n" <<
endl;
1661#undef CHECK_CYC_FSCANF_EOF
#define CHECK_CYC_FSCANF_EOF(arg)
std::shared_ptr< _Fieldmap > Fieldmap
Inform & level4(Inform &inf)
Inform & endl(Inform &inf)
void fp2(BareField< T, 2U > &field, bool docomm=true)
void fp1(BareField< T, 1U > &field, bool docomm=true)
constexpr double two_pi
The value of.
constexpr double pi
The value of.
std::string combineFilePath(std::initializer_list< std::string > ilist)
bool angleBetweenAngles(const double angle, const double min, const double max)
check if angle (in rad and in range [0,2pi]) is within [min, max]
ParticleAttrib< int > Bin
ParticleAttrib< double > M
ParticleAttrib< Vector_t > P
ParticleAttrib< double > Q
ParticleAttrib< short > bunchNum
static OpalData * getInstance()
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
virtual void visitCyclotron(const Cyclotron &)=0
Apply the algorithm to a cyclotron.
Interface for a single beam element.
PartBunchBase< double, 3 > * RefPartBunch_m
std::vector< double > dbrrt_m
std::vector< double > g3_m
std::vector< double > dbtt_m
std::vector< double > dbrtt_m
std::vector< double > f3_m
std::vector< double > dbrr_m
std::vector< double > dbrrr_m
std::vector< double > dbttt_m
std::vector< double > dbrt_m
std::vector< double > dbr_m
std::vector< double > dbt_m
std::vector< double > bfld_m
std::vector< double > f2_m
std::vector< double > rarr_m
virtual std::vector< double > getEScale() const
void setTrimCoilThreshold(double)
void setZinit(double zinit)
virtual double getCyclHarm() const
virtual void getDimensions(double &zBegin, double &zEnd) const
std::vector< std::string > RFfilename_m
virtual bool getSpiralFlag() const
virtual bool bends() const
void getFieldFromFile_CYCIAE(const double &scaleFactor)
void setSpiralFlag(bool spiral_flag)
virtual ElementType getType() const
Get element type std::string.
virtual double getPRinit() const
void getFieldFromFile_BandRF(const double &scaleFactor)
void setSuperpose(std::vector< bool > flag)
virtual double getTrimCoilThreshold() const
void checkInitialReferenceParticle(double refR, double refTheta, double refZ)
void setRfPhi(std::vector< double > f)
void setRFVCoeffFN(std::vector< std::string > rfv_coeff_fn)
void rfPhase(std::size_t map, double t, double frequency, double phi, double &cosPhase, double &sinPhase)
void setPZinit(double zinit)
void setCyclotronType(const std::string &type)
virtual void accept(BeamlineVisitor &) const
Apply visitor to Cyclotron.
virtual double getPZinit() const
virtual std::string getFieldMapFN() const
virtual double getRmin() const
void read(const double &scaleFactor)
int idx(int irad, int ktet)
virtual double getMaxR() const
void setEScale(std::vector< double > bs)
void setSymmetry(double symmetry)
std::vector< bool > superpose_m
virtual std::vector< double > getRfFrequ() const
std::vector< double > escale_m
std::vector< std::string > RFVCoeff_fn_m
bool midplaneMapInterior(const Vector_t &R) const
std::vector< double > rffrequ_m
void getFieldFromFile_AVFEQ(const double &scaleFactor)
virtual double getRmax() const
virtual double getFMHighE() const
double gutdf5d(double *f, double dx, const int kor, const int krl, const int lpr)
void setFieldMapFN(const std::string &fmapfn)
double trimCoilThreshold_m
virtual double getMaxZ() const
virtual double getBScale() const
virtual double getZinit() const
void getFieldFromFile_Carbon(const double &scaleFactor)
void writeOutputFieldFiles()
virtual double getPHIinit() const
std::vector< TrimCoil * > trimcoils_m
void setTurnNumber(int turn)
Turn number written into the loss records of apply(); the tracker sets it every step.
std::vector< std::string > RFFCoeff_fn_m
void setTrimCoils(const std::vector< TrimCoil * > &trimcoils)
virtual double getSymmetry() const
void setCyclHarm(double h)
virtual double getMinZ() const
void getFieldFromFile_Synchrocyclotron(const double &scaleFactor)
virtual std::vector< double > getRfPhi() const
void applyTrimCoil_m(const double r, const double z, const double tet_rad, double *br, double *bz)
Apply trim coils (calculate field contributions)
BFieldType getBFieldType() const
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField)
void getFieldFromFile_FFA(const double &scaleFactor)
void initR(double rmin, double dr, int nrad)
void setPHIinit(double phiinit)
void setRfFrequ(std::vector< double > f)
void setPRinit(double prinit)
std::vector< RFPhaseCache > rfPhaseCache_m
void applyTrimCoil(const double r, const double z, const double tet_rad, double &br, double &bz)
Apply trim coils (calculate field contributions) with smooth field transition.
virtual bool apply(const size_t &id, const double &t, Vector_t &E, Vector_t &B)
void setRfFieldMapFN(std::vector< std::string > rffmapfn)
bool interpolate(const double &rad, const double &tet_rad, double &br, double &bt, double &bz)
void setFMHighE(double e)
std::vector< double > rfphi_m
void setRinit(double rinit)
virtual std::vector< bool > getSuperpose() const
std::vector< std::vector< double > > rfvc_m
std::vector< std::vector< double > > rffc_m
std::vector< Fieldmap > RFfields_m
virtual double getFMLowE() const
std::unique_ptr< LossDataSink > lossDs_m
unsigned int getNumberOfTrimcoils() const
virtual double getRinit() const
void getFieldFromFile_Ring(const double &scaleFactor)
virtual double getMinR() const
const std::string & getCyclotronType() const
void setRFFCoeffFN(std::vector< std::string > rff_coeff_fn)
void setBScale(double bs)
static constexpr int size
virtual const std::string & getName() const
Get element name.
std::string getOutputFN() const
Get output filename.
static Fieldmap getFieldmap(std::string Filename, bool fast=false)
Vektor< double, 3 > Vector_t