FLAME  devel
 All Classes Functions Variables Typedefs Enumerations Pages
rf_cavity.h
1 #ifndef RF_CAVITY_H
2 #define RF_CAVITY_H
3 
4 #endif // RF_CAVITY_H
5 
6 #include <boost/numeric/ublas/matrix.hpp>
7 
8 #include "moment.h"
9 #include "util.h"
10 
11 // Phase space dimension; including vector for orbit/1st moment.
12 # define PS_Dim MomentState::maxsize // Set to 7; to include orbit.
13 
14 
15 class CavDataType {
16 // Cavity on-axis longitudinal electric field vs. s.
17 public:
18  std::vector<double> s, // s coordinate [m]
19  Elong; // Longitudinal Electric field [V/m].
20 
21  void RdData(std::istream &inf);
22  void show(std::ostream&, const int) const;
23  void show(std::ostream&) const;
24 };
25 
26 
27 class CavTLMLineType {
28 public:
29  std::vector<double> s; // Longitudinal position [m].
30  std::vector<std::string> Elem;
31  std::vector<double> E0,
32  T,
33  S,
34  Accel;
35 
36  void clear(void);
37  void set(const double, const std::string &, const double,
38  const double, const double, const double);
39  void show(const int) const;
40  void show() const;
41 };
42 
43 struct ElementRFCavity : public MomentElementBase
44 {
45  // Transport matrix for an RF Cavity.
46  typedef ElementRFCavity self_t;
47  typedef MomentElementBase base_t;
48  typedef typename base_t::state_t state_t;
49 
50  struct RawParams {
51  std::string name, type;
52  double length, aperature, E0;
53  };
54  std::vector<RawParams> lattice; // from axisData_*.txt
55 
56  numeric_table mlptable, // from CaviMlp_*.txt
57  CavData; // from thinlenlon_*.txt
58 
59  std::vector<CavTLMLineType> CavTLMLineTab; // from lattice, for each charge state
60  double fRF, // RF frequency [Hz]
61  IonFys, // Synchrotron phase [rad].
62  phi_ref;
63  int MpoleLevel;
64  int cavi;
65  bool forcettfcalc;
66 
67  unsigned EmitGrowth;
68 
69  ElementRFCavity(const Config& c);
70 
71  void GetCavMatParams(const int cavi,
72  const double beta_tab[], const double gamma_tab[], const double IonK[],
73  CavTLMLineType& lineref) const;
74 
75  void GetCavMat(const int cavi, const int cavilabel, const double Rm, Particle &real,
76  const double EfieldScl, const double IonFyi_s,
77  const double IonEk_s, state_t::matrix_t &M,
78  CavTLMLineType &linetab) const;
79 
80  void GenCavMat2(const int cavi, const double dis, const double EfieldScl, const double TTF_tab[],
81  const double beta_tab[], const double gamma_tab[], const double Lambda,
82  Particle &real, const double IonFys[], const double Rm, state_t::matrix_t &M,
83  const CavTLMLineType& linetab) const;
84 
85  void PropagateLongRFCav(Particle &ref, double &phi_ref) const;
86 
87  void calRFcaviEmitGrowth(const state_t::matrix_t &matIn, Particle &state, const int n,
88  const double betaf, const double gamaf,
89  const double aveX2i, const double cenX, const double aveY2i, const double cenY,
90  state_t::matrix_t &matOut);
91 
92  void InitRFCav(Particle &real, state_t::matrix_t &M, CavTLMLineType &linetab);
93 
94  void GetCavBoost(const numeric_table &CavData, Particle &state, const double IonFy0,
95  const double EfieldScl, double &IonFy) const;
96 
97  void TransFacts(const int cavilabel, double beta, const double CaviIonK, const int gaplabel, const double EfieldScl,
98  double &Ecen, double &T, double &Tp, double &S, double &Sp, double &V0) const;
99 
100  void TransitFacMultipole(const int cavi, const std::string &flabel, const double CaviIonK,
101  double &T, double &S) const;
102 
103  virtual ~ElementRFCavity() {}
104 
105  virtual void assign(const ElementVoid *other) {
106  base_t::assign(other);
107  const self_t* O=static_cast<const self_t*>(other);
108  // *all* member variables must be assigned here or reconfigure() will result in inconsistancy
109  lattice = O->lattice;
110  mlptable = O->mlptable;
111  CavData = O->CavData;
112  CavTLMLineTab = O->CavTLMLineTab;
113  fRF = O->fRF;
114  IonFys = O->IonFys;
115  phi_ref = O->phi_ref;
116  EmitGrowth = O->EmitGrowth;
117  MpoleLevel = O->MpoleLevel;
118  cavi = O->cavi;
119  forcettfcalc = O->forcettfcalc;
120  }
121 
122  virtual void advance(StateBase& s)
123  {
124  state_t& ST = static_cast<state_t&>(s);
125  using namespace boost::numeric::ublas;
126 
127  double x0[2], x2[2];
128 
129  // IonEk is Es + E_state; the latter is set by user.
130  ST.recalc();
131 
132  if(!check_cache(ST)) {
133  last_ref_in = ST.ref;
134  last_real_in = ST.real;
135  resize_cache(ST);
136  // need to re-calculate energy dependent terms
137 
138  recompute_matrix(ST); // updates transfer and last_Kenergy_out
139 
140  for(size_t i=0; i<last_real_in.size(); i++)
141  get_misalign(ST, ST.real[i], misalign[i], misalign_inv[i]);
142 
143  ST.recalc();
144 
145  last_ref_out = ST.ref;
146  last_real_out = ST.real;
147  } else {
148  ST.ref = last_ref_out;
149  assert(last_real_out.size()==ST.real.size()); // should be true if check_cache() -> true
150  std::copy(last_real_out.begin(),
151  last_real_out.end(),
152  ST.real.begin());
153  }
154  // note that calRFcaviEmitGrowth() assumes real[] isn't changed after this point
155 
156  ST.pos += length;
157 
158  for(size_t i=0; i<last_real_in.size(); i++) {
159  ST.moment0[i] = prod(misalign[i], ST.moment0[i]);
160 
161  // Inconsistency in TLM; orbit at entrace should be used to evaluate emittance growth.
162  x0[0] = ST.moment0[i][state_t::PS_X];
163  x0[1] = ST.moment0[i][state_t::PS_Y];
164  x2[0] = ST.moment1[i](0, 0);
165  x2[1] = ST.moment1[i](2, 2);
166 
167  ST.moment0[i] = prod(transfer[i], ST.moment0[i]);
168 
169  ST.moment0[i][state_t::PS_S] = ST.real[i].phis - ST.ref.phis;
170  ST.moment0[i][state_t::PS_PS] = (ST.real[i].IonEk-ST.ref.IonEk)/MeVtoeV;
171 
172  ST.moment0[i] = prod(misalign_inv[i], ST.moment0[i]);
173 
174  scratch = prod(misalign[i], ST.moment1[i]);
175  ST.moment1[i] = prod(scratch, trans(misalign[i]));
176 
177  scratch = prod(transfer[i], ST.moment1[i]);
178  ST.moment1[i] = prod(scratch, trans(transfer[i]));
179 
180  if (EmitGrowth) {
181  calRFcaviEmitGrowth(ST.moment1[i], ST.ref, i, ST.real[i].beta, ST.real[i].gamma, x2[0], x0[0], x2[1], x0[1], scratch);
182  ST.moment1[i] = scratch;
183  }
184 
185  scratch = prod(misalign_inv[i], ST.moment1[i]);
186  ST.moment1[i] = prod(scratch, trans(misalign_inv[i]));
187  }
188 
189  ST.calc_rms();
190  }
191 
192  virtual void recompute_matrix(state_t& ST)
193  {
194  // Re-initialize transport matrix. and update ST.ref and ST.real[]
195 
196  CavTLMLineTab.resize(last_real_in.size());
197 
198  PropagateLongRFCav(ST.ref, phi_ref);
199 
200  for(size_t i=0; i<last_real_in.size(); i++) {
201  // TODO: 'transfer' is overwritten in InitRFCav()?
202  transfer[i] = boost::numeric::ublas::identity_matrix<double>(state_t::maxsize);
203  transfer[i](state_t::PS_X, state_t::PS_PX) = length;
204  transfer[i](state_t::PS_Y, state_t::PS_PY) = length;
205 
206  // J.B. Bug in TLM.
207  double SampleIonK = ST.real[i].SampleIonK;
208 
209 
210  InitRFCav(ST.real[i], transfer[i], CavTLMLineTab[i]);
211 
212  // J.B. Bug in TLM.
213  ST.real[i].SampleIonK = SampleIonK;
214  }
215  }
216 
217  virtual const char* type_name() const {return "rfcavity";}
218 };
219 
Base class for all simulated elements.
Definition: base.h:166
double IonEk
Kinetic energy.
Definition: moment.h:24
virtual const char * type_name() const =0
The abstract base class for all simulation state objects.
Definition: base.h:28
virtual void advance(StateBase &s)
Propogate the given State through this Element.
Definition: moment.cpp:580
double phis
Absolute synchrotron phase [rad].
Definition: moment.h:24
Associative configuration container.
Definition: config.h:66
virtual void recompute_matrix(state_t &ST)
recalculate 'transfer' taking into consideration the provided input state
Definition: moment.cpp:644
double pos
absolute longitudinal position at end of Element
Definition: base.h:36
virtual void assign(const ElementVoid *other)=0
Definition: moment.cpp:511
std::vector< value_t > transfer
final transfer matricies
Definition: moment.h:160
An Element which propagates the statistical moments of a bunch.
Definition: moment.h:131