FLAME  devel
 All Classes Functions Variables Typedefs Enumerations Pages
moment.h
1 #ifndef FLAME_MOMENT_H
2 #define FLAME_MOMENT_H
3 
4 #include <ostream>
5 #include <limits>
6 #include <iomanip>
7 #include <math.h>
8 
9 #include <boost/numeric/ublas/matrix.hpp>
10 #include <boost/numeric/ublas/io.hpp>
11 
12 #include "base.h"
13 
14 #include "constants.h"
15 
16 // Long. sampling frequency [Hz]; must be set to RF Cavity frequency.
17 # define SampleFreq 80.5e6
18 // Sampling distance [m].
19 # define SampleLambda (C0/SampleFreq*MtoMM)
20 
23 struct Particle {
24  double IonZ,
25  IonQ,
26  IonEs,
27  IonW,
28  gamma,
29  beta,
30  bg,
31  SampleIonK,
32  phis,
33  IonEk;
34 
35  Particle() {
36  phis = 0.0;
37  // initially spoil
38  IonZ = IonQ = IonEs = IonW
39  = gamma = beta = bg
40  = SampleIonK = IonEk
41  = std::numeric_limits<double>::quiet_NaN();
42  }
43 
46  void recalc() {
47  IonW = IonEs + IonEk;
48  gamma = (IonEs != 0e0)? IonW/IonEs : 1e0;
49  beta = sqrt(1e0-1e0/(gamma*gamma));
50  bg = (beta != 0e0)? beta*gamma : 1e0;
51  SampleIonK = 2e0*M_PI/(beta*SampleLambda);
52  }
53 };
54 
55 std::ostream& operator<<(std::ostream&, const Particle&);
56 
57 inline bool operator==(const Particle& lhs, const Particle& rhs)
58 {
59  // compare only independent variables.
60  return lhs.IonEk==rhs.IonEk && lhs.IonEs==rhs.IonEs
61  && lhs.IonZ==rhs.IonZ && lhs.IonQ==rhs.IonQ
62  && lhs.phis==rhs.phis;
63 }
64 
65 inline bool operator!=(const Particle& lhs, const Particle& rhs)
66 {
67  return !(lhs==rhs);
68 }
69 
76 struct MomentState : public StateBase
77 {
78  enum {maxsize=7};
79  enum param_t {
80  PS_X, PS_PX, PS_Y, PS_PY, PS_S, PS_PS,
81  PS_QQ // ???
82  };
83 
84  MomentState(const Config& c);
85  virtual ~MomentState();
86 
87  typedef boost::numeric::ublas::vector<double,
88  boost::numeric::ublas::bounded_array<double, maxsize>
89  > vector_t;
90 
91  typedef boost::numeric::ublas::matrix<double,
92  boost::numeric::ublas::row_major,
93  boost::numeric::ublas::bounded_array<double, maxsize*maxsize>
94  > matrix_t;
95 
96  virtual void assign(const StateBase& other);
97 
98  virtual void show(std::ostream& strm, int level) const;
99 
100  Particle ref;
101 
102  // all three must have the same length, which is the # of charge states
103  std::vector<Particle> real;
104  std::vector<vector_t> moment0;
105  std::vector<matrix_t> moment1;
106 
107  vector_t moment0_env, moment0_rms;
108  matrix_t moment1_env;
109 
110  virtual bool getArray(unsigned idx, ArrayInfo& Info);
111 
112  virtual MomentState* clone() const {
113  return new MomentState(*this, clone_tag());
114  }
115 
116  void recalc() {
117  ref.recalc();
118  for(size_t i=0; i<real.size(); i++) real[i].recalc();
119  }
120 
121  void calc_rms();
122 
123  inline size_t size() const { return real.size(); }
124 
125 protected:
126  MomentState(const MomentState& o, clone_tag);
127 };
128 
132 {
133  typedef MomentState state_t;
134 
135  typedef state_t::matrix_t value_t;
136 
137  MomentElementBase(const Config& c);
138  virtual ~MomentElementBase();
139 
140  void get_misalign(const state_t& ST, const Particle& real, value_t& M, value_t& IM) const;
141 
142  virtual void advance(StateBase& s);
143 
147  virtual bool check_cache(const state_t& S) const;
150  void resize_cache(const state_t& ST);
151 
153  virtual void recompute_matrix(state_t& ST);
154 
155  virtual void show(std::ostream& strm, int level) const;
156 
157  Particle last_ref_in, last_ref_out;
158  std::vector<Particle> last_real_in, last_real_out;
160  std::vector<value_t> transfer;
161  std::vector<value_t> misalign, misalign_inv;
162 
164  double dx, dy, pitch, yaw, roll;
165 
167  bool skipcache;
168 
169  virtual void assign(const ElementVoid *other) =0;
170 
171 protected:
172  // scratch space to avoid temp. allocation in advance()
173  // An Element can't be shared between multiple threads
174  state_t::matrix_t scratch;
175 };
176 
177 #endif // FLAME_MOMENT_H
double gamma
Gamma for ion. (dependent)
Definition: moment.h:24
double IonQ
Ion Charge.
Definition: moment.h:24
virtual bool check_cache(const state_t &S) const
Definition: moment.cpp:627
virtual bool getArray(unsigned idx, ArrayInfo &Info)
Introspect named parameter of the derived class.
Definition: moment.cpp:289
Base class for all simulated elements.
Definition: base.h:166
double IonW
Total energy. (dependent)
Definition: moment.h:24
double IonEk
Kinetic energy.
Definition: moment.h:24
virtual void show(std::ostream &strm, int level) const
Definition: moment.cpp:530
double IonZ
Charge state.
Definition: moment.h:24
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
double IonEs
Rest energy.
Definition: moment.h:24
void recalc()
Definition: moment.h:46
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
Used with StateBase::getArray() to describe a single parameter.
Definition: base.h:48
virtual void assign(const ElementVoid *other)=0
Definition: moment.cpp:511
virtual void assign(const StateBase &other)
Definition: moment.cpp:233
double dx
constituents of misalign
Definition: moment.h:164
bool skipcache
If set, check_cache() will always return false.
Definition: moment.h:167
size_t size() const
of charge states
Definition: moment.h:123
std::vector< value_t > transfer
final transfer matricies
Definition: moment.h:160
double beta
Beta for ion. (dependent)
Definition: moment.h:24
double bg
Beta*gamma. (dependent)
Definition: moment.h:24
virtual void show(std::ostream &strm, int level) const
Definition: moment.cpp:248
virtual MomentState * clone() const
Definition: moment.h:112
double SampleIonK
Sample rate; different RF Cavity due to RF frequenies. (dependent)
Definition: moment.h:24
void resize_cache(const state_t &ST)
Definition: moment.cpp:637
Used with clone ctor.
Definition: base.h:134
An Element which propagates the statistical moments of a bunch.
Definition: moment.h:131