OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
Cyclotron.cpp
Go to the documentation of this file.
1//
2// Class Cyclotron
3// Defines the abstract interface for a cyclotron.
4//
5// Copyright (c) 2007 - 2012, Jianjun Yang and Andreas Adelmann, Paul Scherrer Institut, Villigen PSI, Switzerland
6// Copyright (c) 2013 - 2021, Paul Scherrer Institut, Villigen PSI, Switzerland
7// All rights reserved
8//
9// Implemented as part of the PhD thesis
10// "Beam dynamics in high intensity cyclotrons including neighboring bunch effects"
11// and the paper
12// "Beam dynamics in high intensity cyclotrons including neighboring bunch effects:
13// Model, implementation, and application"
14// (https://journals.aps.org/prab/pdf/10.1103/PhysRevSTAB.13.064201)
15//
16// This file is part of OPAL.
17//
18// OPAL is free software: you can redistribute it and/or modify
19// it under the terms of the GNU General Public License as published by
20// the Free Software Foundation, either version 3 of the License, or
21// (at your option) any later version.
22//
23// You should have received a copy of the GNU General Public License
24// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
25//
27
31#include "Fields/Fieldmap.h"
32#include "Physics/Physics.h"
33#include "Physics/Units.h"
35#include "TrimCoils/TrimCoil.h"
37#include "Utilities/Options.h"
38#include "Utilities/Util.h"
39
40#include <algorithm>
41#include <cmath>
42#include <cstdio>
43#include <cstring>
44#include <filesystem>
45#include <fstream>
46#include <limits>
47#include <map>
48
49#define CHECK_CYC_FSCANF_EOF(arg) if (arg == EOF)\
50throw GeneralClassicException("Cyclotron::getFieldFromFile",\
51 "fscanf returned EOF at " #arg);
52
53extern Inform* gmsg;
54extern Inform *gmsgALL;
55
59
61 Component(right),
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),
77 harm_m(right.harm_m),
78 bscale_m(right.bscale_m),
79 trimcoils_m(right.trimcoils_m),
80 minr_m(right.minr_m),
81 maxr_m(right.maxr_m),
82 minz_m(right.minz_m),
83 maxz_m(right.maxz_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) {
89}
90
91Cyclotron::Cyclotron(const std::string& name):
93}
94
97
98
99void Cyclotron::applyTrimCoil_m(const double r, const double z,
100 const double tet_rad,
101 double* br, double* bz) {
102 for (auto trimcoil : trimcoils_m) {
103 trimcoil->applyField(r, z, tet_rad, br, bz);
104 }
105}
106
107void Cyclotron::applyTrimCoil(const double r, const double z,
108 const double tet_rad,
109 double& br, double& bz) {
110 //Changed from > to >= to include case where bz == 0 and trimCoilThreshold_m == 0 -DW
111 if (std::abs(bz) >= trimCoilThreshold_m) {
112 applyTrimCoil_m(r, z, tet_rad, &br, &bz);
113 } else {
114 // make sure to have a smooth transition
115 double tmp_bz = 0.0;
116 applyTrimCoil_m(r, z, tet_rad, &br, &tmp_bz);
117 bz += tmp_bz * std::abs(bz) / trimCoilThreshold_m;
118 }
119}
120
122 // NaN never compares equal, so an unused slot can never be hit.
123 std::fill(t, t + size, std::numeric_limits<double>::quiet_NaN());
124 std::fill(cosPhase, cosPhase + size, 0.0);
125 std::fill(sinPhase, sinPhase + size, 0.0);
126}
127
128void Cyclotron::rfPhase(std::size_t map, double t, double frequency, double phi,
129 double& cosPhase, double& sinPhase) {
130 RFPhaseCache& cache = rfPhaseCache_m[map];
131 for (int k = 0; k < RFPhaseCache::size; ++k) {
132 if (cache.t[k] == t) {
133 cosPhase = cache.cosPhase[k];
134 sinPhase = cache.sinPhase[k];
135 return;
136 }
137 }
138 // The same expression apply() used before this cache existed. Keep cos and sin of the
139 // phase together in one function: GCC fuses them into one sincos call, as it did in the
140 // old code, and glibc's sincos can differ from a separate sin or cos by one ulp.
141 const double phase = Physics::two_pi * Units::MHz2Hz * frequency * t + phi;
142 cosPhase = std::cos(phase);
143 sinPhase = std::sin(phase);
144 cache.t[cache.next] = t;
145 cache.cosPhase[cache.next] = cosPhase;
146 cache.sinPhase[cache.next] = sinPhase;
147 cache.next = (cache.next + 1) % RFPhaseCache::size;
148}
149
151 if (!midplaneMapIsZero_m || !trimcoils_m.empty()) {
152 return false;
153 }
154 // The radial cell index exactly as interpolate() computes it. For 0 <= ir <= nrad-3
155 // interpolate() succeeds at every angle (see read()), so the angle is not needed.
156 // On the axis and for a NaN z the original path divides by r or multiplies by z and
157 // returns NaN, so those inputs keep taking it.
158 const double rad = std::hypot(R[0], R[1]);
159 const int ir = (int)((rad - BP_m.rmin_m) / BP_m.delr_m);
160 return rad > 0.0 && std::isfinite(R[2]) && ir >= 0 && ir <= Bfield_m.nrad_m - 3;
161}
162
163void Cyclotron::accept(BeamlineVisitor &visitor) const {
164 visitor.visitCyclotron(*this);
165}
166
167void Cyclotron::setRinit(double rinit) {
168 rinit_m = rinit;
169}
170
171double Cyclotron::getRinit() const {
172 return rinit_m;
173}
174
175void Cyclotron::setPRinit(double prinit) {
176 prinit_m = prinit;
177}
178
179double Cyclotron::getPRinit() const {
180 return prinit_m;
181}
182
183void Cyclotron::setPHIinit(double phiinit) {
184 phiinit_m = phiinit;
185}
186
187double Cyclotron::getPHIinit() const {
188 return phiinit_m;
189}
190
191void Cyclotron::setZinit(double zinit){
192 zinit_m = zinit;
193}
194
195double Cyclotron::getZinit() const {
196 return zinit_m;
197}
198
199void Cyclotron::setPZinit(double pzinit){
200 pzinit_m = pzinit;
201}
202
203double Cyclotron::getPZinit() const {
204 return pzinit_m;
205}
206
207void Cyclotron::setTrimCoilThreshold(double trimCoilThreshold) {
208 trimCoilThreshold_m = trimCoilThreshold;
209}
210
212 return trimCoilThreshold_m;
213}
214
215void Cyclotron::setSpiralFlag(bool spiral_flag) {
216 spiralFlag_m = spiral_flag;
217}
218
220 return spiralFlag_m;
221}
222
224 turnNumber_m = turn;
225}
226
227void Cyclotron::setFieldMapFN(const std::string& f) {
228 fmapfn_m = f;
229}
230
231std::string Cyclotron::getFieldMapFN() const {
232 if (fmapfn_m.empty()) {
234 "Cyclotron::getFieldMapFN",
235 "The attribute FMAPFN isn't set for the CYCLOTRON element");
236 } else if (std::filesystem::exists(fmapfn_m)) {
237 return fmapfn_m;
238 } else {
239 throw GeneralClassicException("Cyclotron::getFieldMapFN",
240 "Failed to open file '" + fmapfn_m +
241 "', please check if it exists");
242 }
243}
244
245void Cyclotron::setRfFieldMapFN(std::vector<std::string> f) {
246 RFfilename_m = f;
247}
248
249void Cyclotron::setRFFCoeffFN(std::vector<std::string> f) {
250 RFFCoeff_fn_m = f;
251}
252
253void Cyclotron::setRFVCoeffFN(std::vector<std::string> f) {
254 RFVCoeff_fn_m = f;
255}
256
257void Cyclotron::setRfPhi(std::vector<double> f) {
258 rfphi_m = f;
259}
260
261std::vector<double> Cyclotron::getRfPhi() const {
262 if (!rfphi_m.empty()) {
263 return rfphi_m;
264 } else {
265 throw GeneralClassicException("Cyclotron::getRfPhi",
266 "RFPHI not defined for CYCLOTRON");
267 }
268}
269
270void Cyclotron::setRfFrequ(std::vector<double> f) {
271 rffrequ_m = f;
272}
273
274std::vector<double> Cyclotron::getRfFrequ() const {
275 if (!rffrequ_m.empty()) {
276 return rffrequ_m;
277 } else {
278 throw GeneralClassicException("Cyclotron::getRfFrequ",
279 "RFFREQ not defined for CYCLOTRON");
280 }
281}
282
283void Cyclotron::setSuperpose(std::vector<bool> flag) {
284 superpose_m = flag;
285}
286
287std::vector<bool> Cyclotron::getSuperpose() const {
288 if (!superpose_m.empty()) {
289 return superpose_m;
290 } else {
291 throw GeneralClassicException("Cyclotron::getSuperpose",
292 "SUPERPOSE not defined for CYCLOTRON");
293 }
294}
295
297 symmetry_m = s;
298}
299
301 return symmetry_m;
302}
303
304void Cyclotron::setCyclotronType(const std::string& type) {
305 typeName_m = type;
306 if (!typeName_m.empty()) {
308 }
309}
310
311const std::string& Cyclotron::getCyclotronType() const {
312 return typeName_m;
313}
314
318
320 harm_m = h;
321}
322
323void Cyclotron::setBScale(double s) {
324 bscale_m = s;
325}
326
327double Cyclotron::getBScale() const {
328 return bscale_m;
329}
330
331void Cyclotron::setEScale(std::vector<double> s) {
332 escale_m = s;
333}
334
335std::vector<double> Cyclotron::getEScale() const {
336 if (!escale_m.empty()) {
337 return escale_m;
338 } else {
339 throw GeneralClassicException("Cyclotron::getEScale",
340 "ESCALE not defined for CYCLOTRON");
341 }
342}
343
345 return trimcoils_m.size();
346}
347
349 return harm_m;
350}
351
352double Cyclotron::getRmin() const {
353 return BP_m.rmin_m;
354}
355
356double Cyclotron::getRmax() const {
357 return BP_m.rmin_m + (Bfield_m.nrad_m - 1) * BP_m.delr_m;
358}
359
360void Cyclotron::setMinR(double r) {
361 minr_m = r;
362}
363
364void Cyclotron::setMaxR(double r) {
365 maxr_m = r;
366}
367
368double Cyclotron::getMinR() const {
369 if (minr_m >= 0.0) {
370 return minr_m;
371 } else {
372 throw GeneralClassicException("Cyclotron::getMinR",
373 "MINR must be positive");
374 }
375}
376
377double Cyclotron::getMaxR() const {
378 if (maxr_m > minr_m) {
379 return maxr_m;
380 } else {
381 throw GeneralClassicException("Cyclotron::getMaxR",
382 "The attribute MAXR has to be higher than MINR");
383 }
384}
385
386void Cyclotron::setMinZ(double z) {
387 minz_m = z;
388}
389
390double Cyclotron::getMinZ() const {
391 return minz_m;
392}
393
394void Cyclotron::setMaxZ(double z) {
395 maxz_m = z;
396}
397
398double Cyclotron::getMaxZ() const {
399 if (maxz_m > minz_m) {
400 return maxz_m;
401 } else {
402 throw GeneralClassicException("Cyclotron::getMaxZ",
403 "The attribute MAXZ has to be higher than MINZ");
404 }
405}
406
407void Cyclotron::setTrimCoils(const std::vector<TrimCoil*>& trimcoils) {
408 trimcoils_m = trimcoils;
409}
410
411void Cyclotron::setFMLowE(double e) {
412 fmLowE_m = e;
413}
414
415double Cyclotron::getFMLowE() const {
416 return fmLowE_m;
417}
418
419void Cyclotron::setFMHighE(double e) {
420 fmHighE_m = e;
421}
422
423double Cyclotron::getFMHighE() const {
424 return fmHighE_m;
425}
426
428 static const std::map<std::string, BFieldType> typeStringToBFieldType_s = {
429 {"RING", BFieldType::PSIBF},
430 {"CARBONCYCL", BFieldType::CARBONBF},
431 {"CYCIAE", BFieldType::ANSYSBF},
432 {"AVFEQ", BFieldType::AVFEQBF},
433 {"FFA", BFieldType::FFABF},
434 {"BANDRF", BFieldType::BANDRF},
435 {"SYNCHROCYCLOTRON", BFieldType::SYNCHRO}
436 };
437
438 fieldType_m = typeStringToBFieldType_s.at(typeName_m);
439}
440
444
446 double refTheta,
447 double refZ) {
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)");
454 }
455
457 throw GeneralClassicException("Cyclotron::checkInitialReferenceParticle",
458 "PHIINIT is out of [-180, 180)");
459 }
460}
461
462bool Cyclotron::apply(const size_t& id, const double& t, Vector_t& E, Vector_t& B) {
463
464 bool flagNeedUpdate = false;
465
466 const double rpos = std::hypot(RefPartBunch_m->R[id](0), RefPartBunch_m->R[id](1));
467 const double zpos = RefPartBunch_m->R[id](2);
468
469 // A particle that is already flagged lost (Bin < 0, by this element, a collimator or
470 // the boundary geometry) stays in the bunch until the tracker deletes it, and keeps
471 // failing here on every step: report and record only its first loss.
472 const bool flaggedBefore = (RefPartBunch_m->Bin[id] < 0);
473 const bool outsideAperture = (zpos > maxz_m || zpos < minz_m || rpos > maxr_m || rpos < minr_m);
474
475 if (outsideAperture) {
476 flagNeedUpdate = true;
477 } else {
478 flagNeedUpdate = apply(RefPartBunch_m->R[id], RefPartBunch_m->P[id], t, E, B);
479 }
480
481 if (flagNeedUpdate && !flaggedBefore) {
482 *gmsgALL << level4 << getName() << ": Particle " << RefPartBunch_m->ID[id]
483 << (outsideAperture ? " out of the global aperture of cyclotron!"
484 : " out of the field map boundary!") << endl;
485 *gmsgALL << level4 << getName()
486 << ": Coords: "<< RefPartBunch_m->R[id] << " m" << endl;
487
488 // Called by the integrator: R and t are those of the field evaluation (with RK-4 the
489 // stage point and time); P is only updated when the step completes, so it is the
490 // momentum at the start of the step.
491 lossDs_m->addParticle(OpalParticle(RefPartBunch_m->ID[id],
492 RefPartBunch_m->R[id], RefPartBunch_m->P[id],
493 t, RefPartBunch_m->Q[id], RefPartBunch_m->M[id]),
494 std::make_pair(turnNumber_m, RefPartBunch_m->bunchNum[id]));
495 RefPartBunch_m->Bin[id] = -1;
496 }
497
498 return flagNeedUpdate;
499}
500
501bool Cyclotron::apply(const Vector_t& R, const Vector_t& /*P*/,
502 const double& t, Vector_t& E, Vector_t& B) {
503
504 double tet = 0.0;
505 if (midplaneMapInterior(R)) {
506 // A midplane map of zeros, away from its radial edge: the interpolation below would
507 // succeed and give a zero field, so skip the angle, the lookup and the rotation. It
508 // would give signed zeros; a zero of either sign cannot change a nonzero sum, so only
509 // the sign of an exactly-zero result can differ.
510 B = Vector_t(0.0);
511 } else {
512 if (std::abs(R[0]) < 1.0E-10) {
513 if (R[1] >= 0.0) {
514 tet = Physics::pi / 2.0;
515 } else {
516 tet = 1.5 * Physics::pi;
517 }
518 } else if (R[0] < 0.0) {
519 tet = Physics::pi + std::atan(R[1] / R[0]);
520 } else {
521 if (R[1] > 0.0) { // R[0] > 0.0 && R[1] > 0.0
522 tet = std::atan(R[1] / R[0]);
523 } else { // R[0] > 0.0 && R[1] <= 0.0
524 tet = Physics::two_pi + std::atan(R[1] / R[0]);
525 }
526 }
527
528 // Necessary for gap phase output -DW
529 if ( 0 <= tet && tet <= (Physics::pi / 4) ) waitingGap_m = 1;
530
531 // dB_{z}/dr, dB_{z}/dtheta, B_{z}
532 double brint = 0.0, btint = 0.0, bzint = 0.0;
533
534 const double rad = std::hypot(R[0],R[1]);
535 if ( this->interpolate(rad, tet, brint, btint, bzint) ) {
536
537 /* Br */
538 double br = - brint * R[2];
539
540 /* Btheta */
541 double bt = - btint / rad * R[2];
542
543 /* Bz */
544 double bz = - bzint;
545
546 this->applyTrimCoil(rad, R[2], tet, br, bz);
547
548 /* Br Btheta -> Bx By */
549 B[0] = br * std::cos(tet) - bt * std::sin(tet);
550 B[1] = br * std::sin(tet) + bt * std::cos(tet);
551 B[2] = bz;
552 } else {
553 return true;
554 }
555 }
556
558 return false;
559 }
560
561 //The RF field is supposed to be sampled on a cartesian grid
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();
569
570 double xBegin(0), xEnd(0), yBegin(0), yEnd(0), zBegin(0), zEnd(0);
571 int fcount = 0;
572
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;
576
577 // Ok, this is a total patch job, but now that the internal cyclotron units are in m, we have to
578 // change stuff here to match with the input units of mm in the fieldmaps. -DW
579 const Vector_t temp_R = R * Vector_t(Units::m2mm); //Keep this until we have transitioned fully to m -DW
580
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;
584
585 Vector_t tmpE({0.0, 0.0, 0.0}), tmpB({0.0, 0.0, 0.0});
586 // out of bounds?
587 if ((*fi)->getFieldstrength(temp_R, tmpE, tmpB)) continue;
588
589 ++fcount;
590
591 double frequency = (*rffi); // frequency in [MHz]
592 double ebscale = (*escali); // E and B field scaling
593
595 double powert = 1;
596 for (const double fcoef : (*rffci)) {
597 powert *= t;
598 frequency += fcoef * powert; // Add frequency ramp [MHz/s^n]
599 }
600 powert = 1;
601 for (const double vcoef : (*rfvci)) {
602 powert *= t;
603 ebscale += vcoef * powert; // Add frequency ramp [MHz/s^n]
604 }
605 }
606
607 double phase = 0.0, cosPhase = 0.0, sinPhase = 0.0;
608 const std::size_t map = fi - RFfields_m.begin();
609 if (fieldType_m == BFieldType::SYNCHRO || map >= rfPhaseCache_m.size()) {
610 phase = Physics::two_pi * Units::MHz2Hz * frequency * t + (*rfphii);
611 cosPhase = std::cos(phase);
612 sinPhase = std::sin(phase);
613 } else {
614 rfPhase(map, t, frequency, *rfphii, cosPhase, sinPhase);
615 }
616
617 E += ebscale * cosPhase * tmpE;
618 B -= ebscale * sinPhase * tmpB;
619
621 continue;
622
623 // Some phase output -DW
624 double phase_print = phase * Units::rad2deg;
625 if (tet >= 90.0 && waitingGap_m == 1) {
626 phase_print = std::fmod(phase_print, 360) - 360.0;
627
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;
632
633 waitingGap_m = 2;
634 } else if (tet >= 270.0 && waitingGap_m == 2) {
635 phase_print = std::fmod(phase_print, 360) - 360.0;
636
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;
641 waitingGap_m = 0;
642 }
644 ++rffci, ++rfvci;
645 }
646 }
647 return false;
648}
649
650void Cyclotron::apply(const double& rad, const double& z,
651 const double& tet_rad, double& br,
652 double& bt, double& bz) {
653 this->interpolate(rad, tet_rad, br, bt, bz);
654 this->applyTrimCoil(rad, z, tet_rad, br, bz);
655}
656
658 online_m = false;
659 lossDs_m->save();
660 *gmsg << "* Finalize cyclotron " << getName() << endl;
661}
662
663bool Cyclotron::bends() const {
664 return true;
665}
666
667// calculate derivatives with 5-point lagrange's formula.
668double Cyclotron::gutdf5d(double* f, double dx, const int kor,
669 const int krl, const int lpr) {
670
671 double C[5][5][3], FAC[3];
672 double result;
673 int j;
674 /* CALCULATE DERIVATIVES WITH 5-POINT LAGRANGE FORMULA
675 * PARAMETERS:
676 * F STARTADDRESS FOR THE 5 SUPPORT POINTS
677 * DX STEPWIDTH FOR ARGUMENT
678 * KOR ORDER OF DERIVATIVE (KOR=1,2,3).
679 * KRL NUMBER OF SUPPORT POINT, WHERE THE DERIVATIVE IS TO BE CALCULATED
680 * (USUALLY 3, USE FOR BOUNDARY 1 ,2, RESP. 4, 5)
681 * LPR DISTANCE OF THE 5 STORAGE POSITIONS (=1 IF THEY ARE NEIGHBORS OR LENGTH
682 * OF COLUMNLENGTH OF A MATRIX, IF THE SUPPORT POINTS ARE ON A LINE).
683 * ATTENTION! THE INDICES ARE NOW IN C-FORMAT AND NOT IN FORTRAN-FORMAT.*/
684
685 /* COEFFICIENTS FOR THE 1ST DERIVATIVE: */
686 C[0][0][0] = -50.0;
687 C[1][0][0] = 96.0;
688 C[2][0][0] = -72.0;
689 C[3][0][0] = 32.0;
690 C[4][0][0] = -6.0;
691 C[0][1][0] = -6.0;
692 C[1][1][0] = -20.0;
693 C[2][1][0] = 36.0;
694 C[3][1][0] = -12.0;
695 C[4][1][0] = 2.0;
696 C[0][2][0] = 2.0;
697 C[1][2][0] = -16.0;
698 C[2][2][0] = 0.0;
699 C[3][2][0] = 16.0;
700 C[4][2][0] = -2.0;
701 C[0][3][0] = -2.0;
702 C[1][3][0] = 12.0;
703 C[2][3][0] = -36.0;
704 C[3][3][0] = 20.0;
705 C[4][3][0] = 6.0;
706 C[0][4][0] = 6.0;
707 C[1][4][0] = -32.0;
708 C[2][4][0] = 72.0;
709 C[3][4][0] = -96.0;
710 C[4][4][0] = 50.0;
711
712 /* COEFFICIENTS FOR THE 2ND DERIVATIVE: */
713 C[0][0][1] = 35.0;
714 C[1][0][1] = -104;
715 C[2][0][1] = 114.0;
716 C[3][0][1] = -56.0;
717 C[4][0][1] = 11.0;
718 C[0][1][1] = 11.0;
719 C[1][1][1] = -20.0;
720 C[2][1][1] = 6.0;
721 C[3][1][1] = 4.0;
722 C[4][1][1] = -1.0;
723 C[0][2][1] = -1.0;
724 C[1][2][1] = 16.0;
725 C[2][2][1] = -30.0;
726 C[3][2][1] = 16.0;
727 C[4][2][1] = -1.0;
728 C[0][3][1] = -1.0;
729 C[1][3][1] = 4.0;
730 C[2][3][1] = 6.0;
731 C[3][3][1] = -20.0;
732 C[4][3][1] = 11.0;
733 C[0][4][1] = 11.0;
734 C[1][4][1] = -56.0;
735 C[2][4][1] = 114.0;
736 C[3][4][1] = -104;
737 C[4][4][1] = 35.0;
738
739 /* COEFFICIENTS FOR THE 3RD DERIVATIVE: */
740 C[0][0][2] = -10.0;
741 C[1][0][2] = 36.0;
742 C[2][0][2] = -48.0;
743 C[3][0][2] = 28.0;
744 C[4][0][2] = -6.0;
745 C[0][1][2] = -6.0;
746 C[1][1][2] = 20.0;
747 C[2][1][2] = -24.0;
748 C[3][1][2] = 12.0;
749 C[4][1][2] = -2.0;
750 C[0][2][2] = -2.0;
751 C[1][2][2] = 4.0;
752 C[2][2][2] = 0.0;
753 C[3][2][2] = -4.0;
754 C[4][2][2] = 2.0;
755 C[0][3][2] = 2.0;
756 C[1][3][2] = -12.0;
757 C[2][3][2] = 24.0;
758 C[3][3][2] = -20.0;
759 C[4][3][2] = 6.0;
760 C[0][4][2] = 6.0;
761 C[1][4][2] = -28.0;
762 C[2][4][2] = 48.0;
763 C[3][4][2] = -36.0;
764 C[4][4][2] = 10.0;
765
766 /* FACTOR: */
767 FAC[0] = 24.0;
768 FAC[1] = 12.0;
769 FAC[2] = 4.0;
770
771 result = 0.0;
772 for (j = 0; j < 5; j++) {
773 result += C[j][krl][kor] * *(f + j * lpr);
774 }
775
776 return result / (FAC[kor] * std::pow(dx, (kor + 1)));
777}
778
779
780bool Cyclotron::interpolate(const double& rad,
781 const double& tet_rad,
782 double& brint,
783 double& btint,
784 double& bzint) {
785
786 const double xir = (rad - BP_m.rmin_m) / BP_m.delr_m;
787
788 // ir : the number of path whose radius is less than the 4 points of cell which surround the particle.
789 const int ir = (int)xir;
790
791 // wr1 : the relative distance to the inner path radius
792 const double wr1 = xir - (double)ir;
793 // wr2 : the relative distance to the outer path radius
794 const double wr2 = 1.0 - wr1;
795
796 // the corresponding angle on the field map
797 // Note: this does not work if the start point of field map does not equal zero.
798 double tet_map = std::fmod(tet_rad * Units::rad2deg, 360.0 / symmetry_m);
799
800 double xit = tet_map / BP_m.dtet_m;
801 int it = (int) xit;
802
803 const double wt1 = xit - (double)it;
804 const double wt2 = 1.0 - wt1;
805
806 // it : the number of point on the inner path whose angle is less than
807 // the particle' corresponding angle.
808 // include zero degree point
809 it++;
810
811 int r1t1, r2t1, r1t2, r2t2;
812 int ntetS = Bfield_m.ntet_m + 1;
813
814 // r1t1 : the index of the "min angle, min radius" point in the 2D field array.
815 // considering the array start with index of zero, minus 1.
816
818 /*
819 For FFA this does not work
820 */
821 r1t1 = it + ntetS * ir - 1;
822 r1t2 = r1t1 + 1;
823 r2t1 = r1t1 + ntetS;
824 r2t2 = r2t1 + 1 ;
825
826 } else {
827 /*
828 With this we have B-field AND this is far more
829 intuitive for me ....
830 */
831 r1t1 = idx(ir , it);
832 r2t1 = idx(ir + 1, it);
833 r1t2 = idx(ir , it + 1);
834 r2t2 = idx(ir + 1, it + 1);
835 }
836
837 if ((r1t1 >= 0) && (r2t2 < Bfield_m.ntot_m)) {
838 // B_{z}
839 double bzf = Bfield_m.bfld_m[r1t1] * wr2 * wt2 +
840 Bfield_m.bfld_m[r2t1] * wr1 * wt2 +
841 Bfield_m.bfld_m[r1t2] * wr2 * wt1 +
842 Bfield_m.bfld_m[r2t2] * wr1 * wt1;
843 bzint = /*- */bzf ;
844
845 // dB_{z}/dr
846 brint = Bfield_m.dbr_m[r1t1] * wr2 * wt2 +
847 Bfield_m.dbr_m[r2t1] * wr1 * wt2 +
848 Bfield_m.dbr_m[r1t2] * wr2 * wt1 +
849 Bfield_m.dbr_m[r2t2] * wr1 * wt1;
850
851 // dB_{z}/dtheta
852 btint = Bfield_m.dbt_m[r1t1] * wr2 * wt2 +
853 Bfield_m.dbt_m[r2t1] * wr1 * wt2 +
854 Bfield_m.dbt_m[r1t2] * wr2 * wt1 +
855 Bfield_m.dbt_m[r2t2] * wr1 * wt1;
856
857 return true;
858 }
859 return false;
860}
861
862
863void Cyclotron::read(const double& scaleFactor) {
864 if (typeName_m.empty()) {
865 throw GeneralClassicException("Cyclotron::read",
866 "The attribute TYPE isn't set for the CYCLOTRON element");
867 }
868 switch (fieldType_m) {
869 case BFieldType::PSIBF: {
870 *gmsg << "* Read field data from PSI format field map file" << endl;
871 getFieldFromFile_Ring(scaleFactor);
872 break;
873 }
875 *gmsg << "* Read data from 450MeV Carbon cyclotron field file" << endl;
876 getFieldFromFile_Carbon(scaleFactor);
877 break;
878 }
879 case BFieldType::ANSYSBF: {
880 *gmsg << "* Read data from 100MeV H- cyclotron CYCIAE-100 field file" << endl;
881 getFieldFromFile_CYCIAE(scaleFactor);
882 break;
883 }
884 case BFieldType::AVFEQBF: {
885 *gmsg << "* Read AVFEQ data (Riken)" << endl;
886 getFieldFromFile_AVFEQ(scaleFactor);
887 break;
888 }
889 case BFieldType::FFABF: {
890 *gmsg << "* Read FFA data MSU/FNAL" << endl;
891 getFieldFromFile_FFA(scaleFactor);
892 break;
893 }
894 case BFieldType::BANDRF: {
895 *gmsg << "* Read both median plane B field map and 3D E field map of RF cavity for compact cyclotron" << endl;
896 getFieldFromFile_BandRF(scaleFactor);
897 break;
898 }
899 case BFieldType::SYNCHRO: {
900 *gmsg << "* Read midplane B-field, 3D RF fieldmaps, and text files with RF frequency/Voltage coefficients for Synchrocyclotron" << endl;
902 break;
903 }
904 default: {
905 throw GeneralClassicException("Cyclotron::read",
906 "Unknown TYPE for the CYCLOTRON element");
907 }
908 }
909
910 // calculate the radii of initial grid.
912
913 // calculate the remaining derivatives
914 getdiffs();
915
916 // A BANDRF deck may bring its whole magnetic field in through RFMAPFN and give FMAPFN
917 // a midplane map of zeros. That map then only acts through its radial extent, as an
918 // aperture, and apply() can skip the interpolation away from the edge. The skip is exact
919 // for 0 <= ir <= nrad-3: interpolate() reads up to index it + ntetS*(ir+1), where
920 // it <= 360/(symmetry*dtet) + 1, and that stays below nrad*ntetS as long as
921 // 360/(symmetry*dtet) + 2 < 2*ntetS, i.e. for any map that covers its sector.
922 midplaneMapIsZero_m = false;
923 if (fieldType_m == BFieldType::BANDRF && trimcoils_m.empty() && Bfield_m.nrad_m >= 3) {
924 auto allZero = [](const std::vector<double>& v) {
925 return std::all_of(v.begin(), v.end(), [](double x) { return x == 0.0; });
926 };
927 const double ntetS = Bfield_m.ntet_m + 1;
928 const double sectorSteps = 360.0 / symmetry_m / BP_m.dtet_m;
929 midplaneMapIsZero_m = symmetry_m > 0.0 && BP_m.dtet_m > 0.0 &&
930 std::isfinite(sectorSteps) && sectorSteps < 2.0 * ntetS - 2.0 &&
931 allZero(Bfield_m.bfld_m) && allZero(Bfield_m.dbr_m) &&
932 allZero(Bfield_m.dbt_m);
934 *gmsg << "* The midplane field map is zero everywhere: it adds no field and only "
935 << "limits the radius to r < "
936 << BP_m.rmin_m + (Bfield_m.nrad_m - 1) * BP_m.delr_m << " m" << endl;
937 }
938 }
939
940 rfPhaseCache_m.assign(RFfields_m.size(), RFPhaseCache());
941}
942
943// evaluate other derivative of magnetic field.
945
949
953
957
958 for (int i = 0; i < Bfield_m.nrad_m; i++) {
959 for (int k = 0; k < Bfield_m.ntet_m; k++) {
960
961 double dtheta = Units::deg2rad * BP_m.dtet_m;
962
963 int kEdge;
964
965 kEdge = std::max(k - 2, 0);
966 kEdge = std::min(kEdge, Bfield_m.ntet_m - 5);
967
968 int dkFromEdge = k - kEdge;
969 int index = idx(i, k);
970 int indexkEdge = idx(i, kEdge);
971
972 Bfield_m.dbt_m[index] = gutdf5d(&Bfield_m.bfld_m[indexkEdge], dtheta, 0, dkFromEdge, 1);
973 Bfield_m.dbtt_m[index] = gutdf5d(&Bfield_m.bfld_m[indexkEdge], dtheta, 1, dkFromEdge, 1);
974 Bfield_m.dbttt_m[index] = gutdf5d(&Bfield_m.bfld_m[indexkEdge], dtheta, 2, dkFromEdge, 1);
975 }
976 }
977
978 for (int k = 0; k < Bfield_m.ntet_m; k++) {
979 // inner loop varies R
980 for (int i = 0; i < Bfield_m.nrad_m; i++) {
981 double rac = BP_m.rarr_m[i];
982 // define iredg, the reference index for radial interpolation
983 // standard: i-2 minimal: 0 (not negative!) maximal: nrad-4
984 int iredg = std::max(i - 2, 0);
985 iredg = std::min(iredg, Bfield_m.nrad_m - 5);
986 int irtak = i - iredg;
987 int index = idx(i, k);
988 int indexredg = idx(iredg, k);
989
990 Bfield_m.dbr_m[index] = gutdf5d(&Bfield_m.bfld_m[indexredg], BP_m.delr_m, 0, irtak, Bfield_m.ntetS_m);
991 Bfield_m.dbrr_m[index] = gutdf5d(&Bfield_m.bfld_m[indexredg], BP_m.delr_m, 1, irtak, Bfield_m.ntetS_m);
992 Bfield_m.dbrrr_m[index] = gutdf5d(&Bfield_m.bfld_m[indexredg], BP_m.delr_m, 2, irtak, Bfield_m.ntetS_m);
993
994 Bfield_m.dbrt_m[index] = gutdf5d(&Bfield_m.dbt_m[indexredg], BP_m.delr_m, 0, irtak, Bfield_m.ntetS_m);
995 Bfield_m.dbrrt_m[index] = gutdf5d(&Bfield_m.dbt_m[indexredg], BP_m.delr_m, 1, irtak, Bfield_m.ntetS_m);
996 Bfield_m.dbrtt_m[index] = gutdf5d(&Bfield_m.dbtt_m[indexredg], BP_m.delr_m, 0, irtak, Bfield_m.ntetS_m);
997
998 // fehlt noch!! f2,f3,g3,
999 Bfield_m.f2_m[index] = (Bfield_m.dbrr_m[index]
1000 + Bfield_m.dbr_m[index] / rac
1001 + Bfield_m.dbtt_m[index] / rac / rac) / 2.0;
1002
1003 Bfield_m.f3_m[index] = (Bfield_m.dbrrr_m[index]
1004 + Bfield_m.dbrr_m[index] / rac
1005 + (Bfield_m.dbrtt_m[index] - Bfield_m.dbr_m[index]) / rac / rac
1006 - 2.0 * Bfield_m.dbtt_m[index] / rac / rac / rac) / 6.0;
1007
1008 Bfield_m.g3_m[index] = (Bfield_m.dbrrt_m[index]
1009 + Bfield_m.dbrt_m[index] / rac
1010 + Bfield_m.dbttt_m[index] / rac / rac) / 6.0;
1011 } // Radius Loop
1012 } // Azimuth loop
1013
1014 // copy 1st azimuth to last + 1 to always yield an interval
1015 for (int i = 0; i < Bfield_m.nrad_m; i++) {
1016 int iend = idx(i, Bfield_m.ntet_m);
1017 int istart = idx(i, 0);
1018
1019 Bfield_m.bfld_m[iend] = Bfield_m.bfld_m[istart];
1020 Bfield_m.dbt_m[iend] = Bfield_m.dbt_m[istart];
1021 Bfield_m.dbtt_m[iend] = Bfield_m.dbtt_m[istart];
1022 Bfield_m.dbttt_m[iend] = Bfield_m.dbttt_m[istart];
1023
1024 Bfield_m.dbr_m[iend] = Bfield_m.dbr_m[istart];
1025 Bfield_m.dbrr_m[iend] = Bfield_m.dbrr_m[istart];
1026 Bfield_m.dbrrr_m[iend] = Bfield_m.dbrrr_m[istart];
1027
1028 Bfield_m.dbrt_m[iend] = Bfield_m.dbrt_m[istart];
1029 Bfield_m.dbrtt_m[iend] = Bfield_m.dbrtt_m[istart];
1030 Bfield_m.dbrrt_m[iend] = Bfield_m.dbrrt_m[istart];
1031
1032 Bfield_m.f2_m[iend] = Bfield_m.f2_m[istart];
1033 Bfield_m.f3_m[iend] = Bfield_m.f3_m[istart];
1034 Bfield_m.g3_m[iend] = Bfield_m.g3_m[istart];
1035 }
1036
1037 /* debug
1038
1039 for (int i = 0; i< Bfield_m.nrad_m; i++){
1040 for (int j = 0; j< Bfield_m.ntetS_m; j++){
1041 int index = idx(i,j);
1042 double x = (BP_m.rmin_m+i*BP_m.delr_m) * std::sin(j*BP_m.dtet_m*pi/180.0);
1043 double y = (BP_m.rmin_m+i*BP_m.delr_m) * std::cos(j*BP_m.dtet_m*pi/180.0);
1044 *gmsg<<"x= "<<x<<" y= "<<y<<" B= "<<Bfield_m.bfld_m[index]<<endl;
1045 }
1046 }
1047 */
1048}
1049
1050
1051// Calculates Radii of initial grid.
1052// dimensions in [m]!
1053void Cyclotron::initR(double rmin, double dr, int nrad) {
1054 BP_m.rarr_m.resize(nrad);
1055 for (int i = 0; i < nrad; i++) {
1056 BP_m.rarr_m[i] = rmin + i * dr;
1057 }
1058 BP_m.delr_m = dr;
1059}
1060
1061void Cyclotron::initialise(PartBunchBase<double, 3>* bunch, double& /*startField*/, double& /*endField*/) {
1062 RefPartBunch_m = bunch;
1063 online_m = true;
1064}
1065
1066void Cyclotron::initialise(PartBunchBase<double, 3>* bunch, const double& scaleFactor) {
1067 RefPartBunch_m = bunch;
1068 lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getOutputFN(), !Options::asciidump));
1069
1070 this->read(scaleFactor);
1071}
1072
1073
1074// Read field map from external file.
1075void Cyclotron::getFieldFromFile_Ring(const double& scaleFactor) {
1076
1077 FILE *f = nullptr;
1078 int lpar;
1079 char fout[100];
1080 double dtmp;
1081
1082 *gmsg << "* ----------------------------------------------" << endl;
1083 *gmsg << "* READ IN RING FIELD MAP " << endl;
1084 *gmsg << "* (The first data block is useless) " << endl;
1085 *gmsg << "* ----------------------------------------------" << endl;
1086
1087 BP_m.Bfact_m = scaleFactor;
1088
1089 f = std::fopen(fmapfn_m.c_str(), "r");
1090
1091 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.rmin_m));
1092 *gmsg << "* Minimal radius of measured field map: " << BP_m.rmin_m << " [mm]" << endl;
1094
1095 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.delr_m));
1096 //if the value is negative, the actual value is its reciprocal.
1097 if (BP_m.delr_m < 0.0) BP_m.delr_m = 1.0 / (-BP_m.delr_m);
1098 *gmsg << "* Stepsize in radial direction: " << BP_m.delr_m << " [mm]" << endl;
1100
1101 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.tetmin_m));
1102 *gmsg << "* Minimal angle of measured field map: " << BP_m.tetmin_m << " [deg]" << endl;
1103
1104 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.dtet_m));
1105 //if the value is negative, the actual value is its reciprocal.
1106 if (BP_m.dtet_m < 0.0) BP_m.dtet_m = 1.0 / (-BP_m.dtet_m);
1107 *gmsg << "* Stepsize in azimuth direction: " << BP_m.dtet_m << " [deg]" << endl;
1108
1109 for (int i = 0; i < 13; i++) {
1110 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%s", fout));
1111 }
1112
1113 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%d", &Bfield_m.nrad_m));
1114 *gmsg << "* Index in radial direction: " << Bfield_m.nrad_m << endl;
1115
1116 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%d", &Bfield_m.ntet_m));
1117 *gmsg << "* Index in azimuthal direction: " << Bfield_m.ntet_m << endl;
1118
1120 *gmsg << "* Accordingly, total grid point along azimuth: " << Bfield_m.ntetS_m << endl;
1121
1122 for (int i = 0; i < 5; i++) {
1123 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%s", fout));
1124 }
1125 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%d", &lpar));
1126 // msg<< "READ"<<lpar<<" DATA ENTRIES"<<endl;
1127
1128 for (int i = 0; i < 4; i++) {
1129 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%s", fout));
1130 }
1131
1132 for (int i = 0; i < lpar; i++) {
1133 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%16lE", &dtmp));
1134 }
1135 for (int i = 0; i < 6; i++) {
1136 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%s", fout));
1137 }
1138 //*gmsg << "* READ FILE DESCRIPTION..." <<endl;
1139 for (int i = 0; i < 10000; i++) {
1140 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%s", fout));
1141 if (std::strcmp(fout, "LREC=") == 0)break;
1142 }
1143
1144 for (int i = 0; i < 5; i++) {
1145 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%s", fout));
1146 }
1148 *gmsg << "* Total stored grid point number ( ntetS * nrad ): " << Bfield_m.ntot_m << endl;
1149
1151 Bfield_m.dbt_m.resize(Bfield_m.ntot_m);
1154
1155 *gmsg << "* Read-in loop one block per radius" << endl;
1156 *gmsg << "* Rescaling of the magnetic fields with factor: " << BP_m.Bfact_m << endl;
1157 for (int i = 0; i < Bfield_m.nrad_m; i++) {
1158 if (i > 0) {
1159 for (int dummy = 0; dummy < 6; dummy++) {
1160 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%s", fout)); // INFO-LINE
1161 }
1162 }
1163 for (int k = 0; k < Bfield_m.ntet_m; k++) {
1164 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%16lE", &(Bfield_m.bfld_m[idx(i, k)])));
1165 Bfield_m.bfld_m[idx(i, k)] *= BP_m.Bfact_m;
1166 }
1167 for (int k = 0; k < Bfield_m.ntet_m; k++) {
1168 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%16lE", &(Bfield_m.dbt_m[idx(i, k)])));
1169 Bfield_m.dbt_m[idx(i, k)] *= BP_m.Bfact_m;
1170 }
1171 for (int k = 0; k < Bfield_m.ntet_m; k++) {
1172 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%16lE", &(Bfield_m.dbtt_m[idx(i, k)])));
1173 Bfield_m.dbtt_m[idx(i, k)] *= BP_m.Bfact_m;
1174 }
1175 for (int k = 0; k < Bfield_m.ntet_m; k++) {
1176 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%16lE", &(Bfield_m.dbttt_m[idx(i, k)])));
1177 Bfield_m.dbttt_m[idx(i, k)] *= BP_m.Bfact_m;
1178 }
1179 }
1180 std::fclose(f);
1181
1182 *gmsg << "* Field Map read successfully!" << endl << endl;
1183}
1184
1185
1186void Cyclotron::getFieldFromFile_FFA(const double& /*scaleFactor*/) {
1187
1188 /*
1189 Field is read in from ascii file (COSY output) in the order:
1190 R(m) theta(Deg) x(m) y(m) Bz(T).
1191
1192 Theta is the fast varying variable
1193
1194 2.0000 0.0 2.0000 0.0000 0.0000000000000000
1195 2.0000 1.0 1.9997 0.0349 0.0000000000000000
1196 2.0000 2.0 1.9988 0.0698 0.0000000000000000
1197 2.0000 3.0 1.9973 0.1047 0.0000000000000000
1198
1199 ......
1200 <blank line>
1201
1202 2.1000 0.0 2.1000 0.0000 0.0000000000000000
1203 2.1000 1.0 2.0997 0.0367 0.0000000000000000
1204 2.1000 2.0 2.0987 0.0733 0.0000000000000000
1205 */
1206
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;
1213
1214 *gmsg << "* ----------------------------------------------" << endl;
1215 *gmsg << "* READ IN FFA FIELD MAP " << endl;
1216 *gmsg << "* ----------------------------------------------" << endl;
1217
1218 BP_m.Bfact_m = Units::T2kG * -1; // T->kG and H- for the current FNAL FFA
1219
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;
1223
1224 // STEP2: SKIP ALL THE HEADER LINES
1225 for (int i = 0; i < num_of_header_lines; ++i)
1226 file_to_read.ignore(max_num_of_char_in_a_line, '\n');
1227
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) {
1232 rv.push_back(r);
1233 thv.push_back(th);
1234 xv.push_back(x);
1235 yv.push_back(y);
1236 bzv.push_back(bz);
1237 }
1238 }
1239
1240 double maxtheta = 360.0;
1241 BP_m.dtet_m = thv[1] - thv[0];
1242 BP_m.rmin_m = *(rv.begin());
1243 double rmax = rv.back();
1244
1245 // find out dR
1246 for (vit = rv.begin(); *vit <= BP_m.rmin_m; ++vit) {}
1247 BP_m.delr_m = *vit - BP_m.rmin_m;
1248
1249 BP_m.tetmin_m = thv[0];
1250
1251 Bfield_m.ntet_m = (int)((maxtheta - thv[0]) / BP_m.dtet_m);
1252 Bfield_m.nrad_m = (int)(rmax - BP_m.rmin_m) / BP_m.delr_m + 1;
1254 *gmsg << "* Minimal radius of measured field map: " << Units::m2mm * BP_m.rmin_m << " [mm]" << endl;
1255 *gmsg << "* Maximal radius of measured field map: " << Units::m2mm * rmax << " [mm]" << endl;
1256 *gmsg << "* Stepsize in radial direction: " << Units::m2mm * BP_m.delr_m << " [mm]" << endl;
1257 *gmsg << "* Minimal angle of measured field map: " << BP_m.tetmin_m << " [deg]" << endl;
1258 *gmsg << "* Maximal angle of measured field map: " << maxtheta << " [deg]" << endl;
1259
1260 //if the value is negtive, the actual value is its reciprocal.
1261 if (BP_m.dtet_m < 0.0) BP_m.dtet_m = 1.0 / (-BP_m.dtet_m);
1262 *gmsg << "* Stepsize in azimuth direction: " << BP_m.dtet_m << " [deg]" << endl;
1263 *gmsg << "* Total grid point along azimuth: " << Bfield_m.ntetS_m << endl;
1264 *gmsg << "* Total grid point along radius: " << Bfield_m.nrad_m << endl;
1265
1267 *gmsg << "* Total stored grid point number ( ntetS * nrad ): " << Bfield_m.ntot_m << endl;
1268
1270 Bfield_m.dbt_m.resize(Bfield_m.ntot_m);
1273
1274 *gmsg << "* Rescaling of the magnetic fields with factor: " << BP_m.Bfact_m << endl;
1275
1276 int count = 0;
1277 for (int r = 0; r < Bfield_m.nrad_m; r++) {
1278 for (int k = 0; k < Bfield_m.ntet_m; k++) {
1279 Bfield_m.bfld_m[idx(r, k)] = bzv[count] * BP_m.Bfact_m;
1280 count++;
1281 }
1282 }
1283
1284 if ((Ippl::getNodes()) == 1 && Options::info) {
1286 }
1287 *gmsg << "* Number of elements read: " << count << endl;
1288 *gmsg << "* Field Map read successfully!" << endl << endl;
1289}
1290
1291
1292void Cyclotron::getFieldFromFile_AVFEQ(const double& scaleFactor) {
1293
1294 FILE *f = nullptr;
1295 *gmsg << "* ----------------------------------------------" << endl;
1296 *gmsg << "* READ IN AVFEQ CYCLOTRON FIELD MAP " << endl;
1297 *gmsg << "* ----------------------------------------------" << endl;
1298
1299 /* From Hiroki-san
1300 The first line tells r minimum (500mm),
1301 r maximum(4150mm),
1302 r step(50mm),
1303 theta minimum(0deg),
1304 theta maximum(90deg)
1305 theta step(0.5deg).
1306
1307 From the next line data repeat the block for a given r which the
1308 first line of the block tells. Each block consists of the data Bz
1309 from theta minimum (0deg) to theta maximum(90deg) with theta
1310 step(0.5deg).
1311 */
1312
1313 BP_m.Bfact_m = scaleFactor / 1000.;
1314
1315 f = std::fopen(fmapfn_m.c_str(), "r");
1316
1317 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.rmin_m));
1318 *gmsg << "* Minimal radius of measured field map: " << BP_m.rmin_m << " [mm]" << endl;
1320
1321 double rmax;
1322 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &rmax));
1323 *gmsg << "* Maximal radius of measured field map: " << rmax << " [mm]" << endl;
1324 rmax *= Units::mm2m;
1325
1326 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.delr_m));
1327 *gmsg << "* Stepsize in radial direction: " << BP_m.delr_m << " [mm]" << endl;
1329
1330 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.tetmin_m));
1331 *gmsg << "* Minimal angle of measured field map: " << BP_m.tetmin_m << " [deg]" << endl;
1332
1333 double tetmax;
1334 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &tetmax));
1335 *gmsg << "* Maximal angle of measured field map: " << tetmax << " [deg]" << endl;
1336
1337 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.dtet_m));
1338 //if the value is nagtive, the actual value is its reciprocal.
1339
1340 if (BP_m.dtet_m < 0.0) BP_m.dtet_m = 1.0 / (-BP_m.dtet_m);
1341 *gmsg << "* Stepsize in azimuth direction: " << BP_m.dtet_m << " [deg]" << endl;
1342
1343 Bfield_m.ntetS_m = (int)((tetmax - BP_m.tetmin_m) / BP_m.dtet_m + 1);
1344 *gmsg << "* Total grid point along azimuth: " << Bfield_m.ntetS_m << endl;
1345
1346 Bfield_m.nrad_m = (int)(rmax - BP_m.rmin_m) / BP_m.delr_m;
1347
1348 int ntotidx = idx(Bfield_m.nrad_m, Bfield_m.ntetS_m) + 1;
1349
1351 *gmsg << "* Total stored grid point number ( ntetS * nrad ): "
1352 << Bfield_m.ntot_m << " ntot-idx= " << ntotidx << endl;
1353
1355 Bfield_m.dbt_m.resize(Bfield_m.ntot_m);
1358
1359 *gmsg << "* Rescaling of the magnetic fields with factor: " << BP_m.Bfact_m << endl;
1360
1361 double tmp;
1362 int count = 0;
1363 for (int r = 0; r < Bfield_m.nrad_m; r++) {
1364 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%16lE", &tmp)); // over read
1365 for (int k = 0; k < Bfield_m.ntetS_m; k++) {
1366 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%16lE", &(Bfield_m.bfld_m[idx(r, k)])));
1367 Bfield_m.bfld_m[idx(r, k)] *= BP_m.Bfact_m;
1368 count++;
1369 }
1370 }
1371
1372 if ((Ippl::getNodes()) == 1 && Options::info) {
1374 }
1375
1376 std::fclose(f);
1377 *gmsg << "* Number of elements read: " << count << endl;
1378 *gmsg << "* Field Map read successfully!" << endl << endl;
1379}
1380
1381
1382void Cyclotron::getFieldFromFile_Carbon(const double& scaleFactor) {
1383
1384 FILE *f = nullptr;
1385 *gmsg << "* ----------------------------------------------" << endl;
1386 *gmsg << "* READ IN CARBON CYCLOTRON FIELD MAP " << endl;
1387 *gmsg << "* ----------------------------------------------" << endl;
1388
1389 BP_m.Bfact_m = scaleFactor;
1390
1391 f = std::fopen(fmapfn_m.c_str(), "r");
1392
1393 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.rmin_m));
1394 *gmsg << "* Minimal radius of measured field map: " << BP_m.rmin_m << " [mm]" << endl;
1396
1397 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.delr_m));
1398 //if the value is negative, the actual value is its reciprocal.
1399 if (BP_m.delr_m < 0.0) BP_m.delr_m = 1.0 / (-BP_m.delr_m);
1400 *gmsg << "* Stepsize in radial direction: " << BP_m.delr_m << " [mm]" << endl;
1402
1403 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.tetmin_m));
1404 *gmsg << "* Minimal angle of measured field map: " << BP_m.tetmin_m << " [deg]" << endl;
1405
1406 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.dtet_m));
1407 //if the value is negative, the actual value is its reciprocal.
1408 if (BP_m.dtet_m < 0.0) BP_m.dtet_m = 1.0 / (-BP_m.dtet_m);
1409 *gmsg << "* Stepsize in azimuthal direction: " << BP_m.dtet_m << " [deg]" << endl;
1410
1411 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%d", &Bfield_m.ntet_m));
1412 *gmsg << "* Grid points along azimuth (ntet): " << Bfield_m.ntet_m << endl;
1413
1414 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%d", &Bfield_m.nrad_m));
1415 *gmsg << "* Grid points along radius (nrad): " << Bfield_m.nrad_m << endl;
1416
1417 //Bfield_m.ntetS = Bfield_m.ntet;
1419 //*gmsg << "* Accordingly, total grid point along azimuth: " << Bfield_m.ntetS << endl;
1420
1421 //Bfield_m.ntot = idx(Bfield_m.nrad - 1, Bfield_m.ntet) + 1;
1423
1424 *gmsg << "* Adding a guard cell along azimuth" << endl;
1425 *gmsg << "* Total stored grid point number ((ntet+1) * nrad): " << Bfield_m.ntot_m << endl;
1427 Bfield_m.dbt_m.resize(Bfield_m.ntot_m);
1430
1431 *gmsg << "* Rescaling of the magnetic fields with factor: " << BP_m.Bfact_m << endl;
1432
1433 for (int i = 0; i < Bfield_m.nrad_m; i++) {
1434 for (int k = 0; k < Bfield_m.ntet_m; k++) {
1435 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%16lE", &(Bfield_m.bfld_m[idx(i, k)])));
1436 Bfield_m.bfld_m[idx(i, k)] *= BP_m.Bfact_m;
1437 }
1438 }
1439
1440 if ((Ippl::getNodes()) == 1 && Options::info) {
1442 }
1443
1444 std::fclose(f);
1445 *gmsg << "* Field Map read successfully!" << endl << endl;
1446}
1447
1448
1449void Cyclotron::getFieldFromFile_CYCIAE(const double& scaleFactor) {
1450
1451 FILE *f = nullptr;
1452 char fout[100];
1453 int dtmp;
1454
1455 *gmsg << "* ----------------------------------------------" << endl;
1456 *gmsg << "* READ IN CYCIAE-100 CYCLOTRON FIELD MAP " << endl;
1457 *gmsg << "* ----------------------------------------------" << endl;
1458
1459 BP_m.Bfact_m = scaleFactor;
1460
1461 f = std::fopen(fmapfn_m.c_str(), "r");
1462
1463 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.rmin_m));
1464 *gmsg << "* Minimal radius of measured field map: " << BP_m.rmin_m << " [mm]" << endl;
1466
1467 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.delr_m));
1468 *gmsg << "* Stepsize in radial direction: " << BP_m.delr_m << " [mm]" << endl;
1470
1471 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.tetmin_m));
1472 *gmsg << "* Minimal angle of measured field map: " << BP_m.tetmin_m << " [deg]" << endl;
1473
1474 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &BP_m.dtet_m));
1475 //if the value is nagtive, the actual value is its reciprocal.
1476 if (BP_m.dtet_m < 0.0) BP_m.dtet_m = 1.0 / (-BP_m.dtet_m);
1477 *gmsg << "* Stepsize in azimuth direction: " << BP_m.dtet_m << " [deg]" << endl;
1478
1479 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%d", &Bfield_m.ntet_m));
1480 *gmsg << "* Index in azimuthal direction: " << Bfield_m.ntet_m << endl;
1481
1482 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%d", &Bfield_m.nrad_m));
1483 *gmsg << "* Index in radial direction: " << Bfield_m.nrad_m << endl;
1484
1486 *gmsg << "* Accordingly, total grid point along azimuth: " << Bfield_m.ntetS_m << endl;
1487
1489
1490 *gmsg << "* Total stored grid point number ( ntetS * nrad ): " << Bfield_m.ntot_m << endl;
1492 Bfield_m.dbt_m.resize(Bfield_m.ntot_m);
1495
1496 *gmsg << "* Rescaling of the magnetic fields with factor: " << BP_m.Bfact_m << endl;
1497
1498 int nHalfPoints = Bfield_m.ntet_m / 2.0 + 1;
1499
1500 for (int i = 0; i < Bfield_m.nrad_m; i++) {
1501 for (int ii = 0; ii < 13; ii++) {
1502 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%s", fout));
1503 }
1504 for (int k = 0; k < nHalfPoints; k++) {
1505 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%d", &dtmp));
1506 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%d", &dtmp));
1507 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%d", &dtmp));
1508 CHECK_CYC_FSCANF_EOF(std::fscanf(f, "%lf", &(Bfield_m.bfld_m[idx(i, k)])));
1509 // T --> kGs, minus for minus hydrogen
1510 Bfield_m.bfld_m[idx(i, k)] = Bfield_m.bfld_m[idx(i, k)] * ( Units::T2kG * -1);
1511 }
1512 for (int k = nHalfPoints; k < Bfield_m.ntet_m; k++) {
1514 }
1515 }
1516
1517 std::fclose(f);
1518 *gmsg << "* Field Map read successfully!" << endl << endl;
1519}
1520
1521
1522void Cyclotron::getFieldFromFile_BandRF(const double& scaleFactor) {
1523 // read 3D E&B field data file
1524 // loop over all field maps and superpose fields
1525 for (auto& fm: RFfilename_m) {
1526 Fieldmap f = _Fieldmap::getFieldmap(fm, false);
1527 *gmsg << "* Reading '" << fm << "'" << endl;
1528 f->readMap();
1529 RFfields_m.push_back(f);
1530 }
1531 // read CARBON type B field
1532 getFieldFromFile_Carbon(scaleFactor);
1533}
1534
1535
1536void Cyclotron::getFieldFromFile_Synchrocyclotron(const double& scaleFactor) {
1537
1538 // read 3D E&B field data file
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();
1542 // loop over all field maps and superpose fields
1543 FILE *rffcf = nullptr;
1544 FILE *rfvcf = nullptr;
1545
1546 *gmsg << endl;
1547 *gmsg << "* ------------------------------------------------------------" << endl;
1548 *gmsg << "* READ IN 3D RF Fields and Frequency Coefficients " << endl;
1549 *gmsg << "* ------------------------------------------------------------" << endl;
1550
1551 for (; fm != RFfilename_m.end(); ++fm, ++rffcfni, ++rfvcfni) {
1552 Fieldmap f = _Fieldmap::getFieldmap(*fm, false);
1553 f->readMap();
1554 // if (IPPL::Comm->getOutputLevel() != 0) f->getInfo(gmsg);
1555 RFfields_m.push_back(f);
1556
1557 // Read RF Frequency Coefficients from file
1558 *gmsg << "RF Frequency Coefficient Filename: " << (*rffcfni) << endl;
1559
1560 rffcf = std::fopen((*rffcfni).c_str(), "r");
1561
1562 if (rffcf == nullptr) {
1564 "Cyclotron::getFieldFromFile_Synchrocyclotron",
1565 "failed to open file '" + *rffcfni + "', please check if it exists");
1566 }
1567
1568 std::vector<double> fcoeff;
1569 int nc; //Number of coefficients
1570 double value;
1571
1572 CHECK_CYC_FSCANF_EOF(std::fscanf(rffcf, "%d", &nc));
1573 *gmsg << "* Number of coefficients in file: " << nc << endl;
1574 for (int k = 0; k < nc; k++) {
1575 CHECK_CYC_FSCANF_EOF(std::fscanf(rffcf, "%16lE", &value));
1576 fcoeff.push_back(value);
1577 //*gmsg << "* Coefficient " << k << ": " << value << endl;
1578 }
1579 rffc_m.push_back(fcoeff);
1580
1581 std::fclose(rffcf);
1582
1583 // Read RF Voltage Coefficients from file
1584 *gmsg << "RF Voltage Coefficient Filename: " << (*rfvcfni) << endl;
1585
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");
1591 }
1592
1593 std::vector<double> vcoeff;
1594
1595 CHECK_CYC_FSCANF_EOF(std::fscanf(rfvcf, "%d", &nc));
1596 *gmsg << "* Number of coefficients in file: " << nc << endl;
1597 for (int k = 0; k < nc; k++) {
1598 CHECK_CYC_FSCANF_EOF(std::fscanf(rfvcf, "%16lE", &value));
1599 vcoeff.push_back(value);
1600 //*gmsg << "* Coefficient " << k << ": " << value << endl;
1601 }
1602 rfvc_m.push_back(vcoeff);
1603
1604 std::fclose(rfvcf);
1605 }
1606
1607 // read CARBON type B field for mid-plane field
1608 getFieldFromFile_Carbon(scaleFactor);
1609}
1610
1611void Cyclotron::getDimensions(double& /*zBegin*/, double& /*zEnd*/) const
1612{ }
1613
1614
1616 std::fstream fp1;
1617 std::string fname = Util::combineFilePath({
1619 "gnu.out"
1620 });
1621 fp1.open(fname, std::ios::out);
1622 for (int i = 0; i < Bfield_m.nrad_m; i++) {
1623 for (int k = 0; k < Bfield_m.ntet_m; k++) {
1624 fp1 << BP_m.rmin_m + (i * BP_m.delr_m) << " \t "
1625 << k * (BP_m.tetmin_m + BP_m.dtet_m) << " \t "
1626 << Bfield_m.bfld_m[idx(i, k)] << std::endl;
1627 }
1628 }
1629 fp1.close();
1630
1634 std::fstream fp2;
1635 fname = Util::combineFilePath({
1637 "eb.out"
1638 });
1639 fp2.open(fname, std::ios::out);
1640 for (int i = 0; i < Bfield_m.nrad_m; i++) {
1641 for (int k = 0; k < Bfield_m.ntet_m; k++) {
1642 Vector_t tmpR = Vector_t ({BP_m.rmin_m + (i * BP_m.delr_m), 0.0, k * (BP_m.tetmin_m + BP_m.dtet_m)});
1643 Vector_t tmpE({0.0, 0.0, 0.0}), tmpB({0.0, 0.0, 0.0});
1644 for (auto& fi: RFfields_m) {
1645 Vector_t E({0.0, 0.0, 0.0}), B({0.0, 0.0, 0.0});
1646 if (!fi->getFieldstrength(tmpR, tmpE, tmpB)) {
1647 tmpE += E;
1648 tmpB -= B;
1649 }
1650 }
1651 fp2 << tmpR << " \t E= " << tmpE << "\t B= " << tmpB << std::endl;
1652 }
1653 }
1654 *gmsg << "\n* Writing 'gnu.out' and 'eb.out' files of cyclotron field maps\n" << endl;
1655 fp2.close();
1656 } else {
1657 *gmsg << "\n* Writing 'gnu.out' file of cyclotron field map\n" << endl;
1658 }
1659}
1660
1661#undef CHECK_CYC_FSCANF_EOF
Inform * gmsg
Definition Main.cpp:70
#define CHECK_CYC_FSCANF_EOF(arg)
Definition Cyclotron.cpp:49
Inform * gmsgALL
Definition Main.cpp:71
Inform * gmsg
Definition Main.cpp:70
ElementType
Definition ElementBase.h:89
std::shared_ptr< _Fieldmap > Fieldmap
Definition Definitions.h:24
Inform * gmsgALL
Definition Main.cpp:71
Inform & level4(Inform &inf)
Definition Inform.cpp:48
Inform & endl(Inform &inf)
Definition Inform.cpp:42
void fp2(BareField< T, 2U > &field, bool docomm=true)
void fp1(BareField< T, 1U > &field, bool docomm=true)
const std::string name
constexpr double two_pi
The value of.
Definition Physics.h:33
constexpr double pi
The value of.
Definition Physics.h:30
constexpr double mm2m
Definition Units.h:29
constexpr double m2mm
Definition Units.h:26
constexpr double MHz2Hz
Definition Units.h:113
constexpr double T2kG
Definition Units.h:56
constexpr double rad2deg
Definition Units.h:146
constexpr double deg2rad
Definition Units.h:143
bool asciidump
Definition Options.cpp:83
bool info
Info flag.
Definition Options.cpp:26
std::string combineFilePath(std::initializer_list< std::string > ilist)
Definition Util.cpp:204
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]
Definition Util.h:209
ParticlePos_t & R
ParticleAttrib< int > Bin
ParticleAttrib< double > M
ParticleAttrib< Vector_t > P
ParticleAttrib< double > Q
ParticleAttrib< short > bunchNum
ParticleIndex_t & ID
static OpalData * getInstance()
Definition OpalData.cpp:196
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
Definition OpalData.cpp:666
virtual void visitCyclotron(const Cyclotron &)=0
Apply the algorithm to a cyclotron.
Interface for a single beam element.
Definition Component.h:50
bool online_m
Definition Component.h:192
PartBunchBase< double, 3 > * RefPartBunch_m
Definition Component.h:191
std::vector< double > dbrrt_m
Definition Cyclotron.h:55
std::vector< double > g3_m
Definition Cyclotron.h:61
std::vector< double > dbtt_m
Definition Cyclotron.h:46
std::vector< double > dbrtt_m
Definition Cyclotron.h:56
std::vector< double > f3_m
Definition Cyclotron.h:60
std::vector< double > dbrr_m
Definition Cyclotron.h:51
std::vector< double > dbrrr_m
Definition Cyclotron.h:52
std::vector< double > dbttt_m
Definition Cyclotron.h:47
std::vector< double > dbrt_m
Definition Cyclotron.h:54
std::vector< double > dbr_m
Definition Cyclotron.h:50
std::vector< double > dbt_m
Definition Cyclotron.h:45
int ntetS_m
Definition Cyclotron.h:65
std::vector< double > bfld_m
Definition Cyclotron.h:44
std::vector< double > f2_m
Definition Cyclotron.h:59
double delr_m
Definition Cyclotron.h:74
double rmin_m
Definition Cyclotron.h:74
double dtet_m
Definition Cyclotron.h:75
double Bfact_m
Definition Cyclotron.h:81
std::vector< double > rarr_m
Definition Cyclotron.h:78
double tetmin_m
Definition Cyclotron.h:75
virtual std::vector< double > getEScale() const
void setTrimCoilThreshold(double)
void setZinit(double zinit)
double minr_m
Definition Cyclotron.h:284
virtual double getCyclHarm() const
double rinit_m
Definition Cyclotron.h:267
void setBFieldType()
virtual void getDimensions(double &zBegin, double &zEnd) const
std::vector< std::string > RFfilename_m
Definition Cyclotron.h:298
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)
double harm_m
Definition Cyclotron.h:278
void setSuperpose(std::vector< bool > flag)
double fmHighE_m
Definition Cyclotron.h:290
void setMinR(double r)
virtual double getTrimCoilThreshold() const
double prinit_m
Definition Cyclotron.h:268
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)
bool spiralFlag_m
Definition Cyclotron.h:273
void setPZinit(double zinit)
void setCyclotronType(const std::string &type)
virtual void accept(BeamlineVisitor &) const
Apply visitor to Cyclotron.
virtual double getPZinit() const
double symmetry_m
Definition Cyclotron.h:265
std::string typeName_m
Definition Cyclotron.h:276
virtual std::string getFieldMapFN() const
virtual double getRmin() const
void read(const double &scaleFactor)
int idx(int irad, int ktet)
Definition Cyclotron.h:250
double phiinit_m
Definition Cyclotron.h:269
virtual double getMaxR() const
void setEScale(std::vector< double > bs)
double zinit_m
Definition Cyclotron.h:270
void setSymmetry(double symmetry)
std::vector< bool > superpose_m
Definition Cyclotron.h:263
virtual void finalise()
virtual std::vector< double > getRfFrequ() const
std::vector< double > escale_m
Definition Cyclotron.h:262
std::vector< std::string > RFVCoeff_fn_m
Definition Cyclotron.h:300
bool midplaneMapInterior(const Vector_t &R) const
std::vector< double > rffrequ_m
Definition Cyclotron.h:257
double maxz_m
Definition Cyclotron.h:287
void getFieldFromFile_AVFEQ(const double &scaleFactor)
void setMaxR(double r)
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
Definition Cyclotron.h:274
virtual double getMaxZ() const
void getdiffs()
void setMaxZ(double z)
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
Definition Cyclotron.h:282
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
Definition Cyclotron.h:299
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)
bool midplaneMapIsZero_m
Definition Cyclotron.h:312
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)
Definition Cyclotron.cpp:99
virtual ~Cyclotron()
Definition Cyclotron.cpp:95
BFieldType getBFieldType() const
double minz_m
Definition Cyclotron.h:286
virtual void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField)
double fmLowE_m
Definition Cyclotron.h:289
void getFieldFromFile_FFA(const double &scaleFactor)
std::string fmapfn_m
Definition Cyclotron.h:256
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
Definition Cyclotron.h:331
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.
BFieldType fieldType_m
Definition Cyclotron.h:254
double bscale_m
Definition Cyclotron.h:280
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
Definition Cyclotron.h:261
void setMinZ(double z)
void setRinit(double rinit)
int waitingGap_m
Definition Cyclotron.h:307
double pzinit_m
Definition Cyclotron.h:271
int turnNumber_m
Definition Cyclotron.h:304
virtual std::vector< bool > getSuperpose() const
double maxr_m
Definition Cyclotron.h:285
BfieldData Bfield_m
Definition Cyclotron.h:338
std::vector< std::vector< double > > rfvc_m
Definition Cyclotron.h:260
std::vector< std::vector< double > > rffc_m
Definition Cyclotron.h:258
BPositions BP_m
Definition Cyclotron.h:341
std::vector< Fieldmap > RFfields_m
Definition Cyclotron.h:297
virtual double getFMLowE() const
std::unique_ptr< LossDataSink > lossDs_m
Definition Cyclotron.h:302
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 setFMLowE(double e)
void setBScale(double bs)
double sinPhase[size]
Definition Cyclotron.h:327
static constexpr int size
Definition Cyclotron.h:324
double cosPhase[size]
Definition Cyclotron.h:326
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)
Definition Fieldmap.cpp:48
static int getNodes()
Definition IpplInfo.cpp:689
Vektor< double, 3 > Vector_t
Definition Vektor.h:6