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 // Default sampling frequency [Hz].
17 # define SampleFreqDefault 80.5e6
18 
21 struct Particle {
22  double IonZ,
23  IonQ,
24  IonEs,
25  IonW,
26  gamma,
27  beta,
28  bg,
29  SampleFreq,
30  SampleLambda,
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
41  = SampleIonK = IonEk
42  = std::numeric_limits<double>::quiet_NaN();
43  }
44 
47  void recalc() {
48  IonW = IonEs + IonEk;
49  gamma = (IonEs != 0e0)? IonW/IonEs : 1e0;
50  beta = sqrt(1e0-1e0/(gamma*gamma));
51  bg = (beta != 0e0)? beta*gamma : 1e0;
52  SampleLambda = C0/SampleFreq*MtoMM;
53  SampleIonK = 2e0*M_PI/(beta*SampleLambda);
54  }
55 
56  double Brho() const { return beta*IonW/(C0*IonZ); }
57 };
58 
59 std::ostream& operator<<(std::ostream&, const Particle&);
60 
61 inline bool operator==(const Particle& lhs, const Particle& rhs)
62 {
63  // compare only independent variables.
64  return lhs.IonEk==rhs.IonEk && lhs.IonEs==rhs.IonEs
65  && lhs.IonZ==rhs.IonZ && lhs.IonQ==rhs.IonQ
66  && lhs.phis==rhs.phis && lhs.SampleFreq==rhs.SampleFreq;
67 }
68 
69 inline bool operator!=(const Particle& lhs, const Particle& rhs)
70 {
71  return !(lhs==rhs);
72 }
73 
74 inline bool operator<=(const Particle& lhs, const Particle& rhs)
75 {
76  // compare only independent variables.
77  return lhs.IonEk==rhs.IonEk && lhs.IonEs==rhs.IonEs
78  && lhs.IonZ==rhs.IonZ && lhs.IonQ==rhs.IonQ
79  && lhs.SampleFreq==rhs.SampleFreq;
80 }
81 
88 struct MomentState : public StateBase
89 {
90  enum {maxsize=7};
91  enum param_t {
92  PS_X, PS_PX, PS_Y, PS_PY, PS_S, PS_PS,
93  PS_QQ // ???
94  };
95 
96  MomentState(const Config& c);
97  virtual ~MomentState();
98 
99  typedef boost::numeric::ublas::vector<double,
100  boost::numeric::ublas::bounded_array<double, maxsize>
101  > vector_t;
102 
103  typedef boost::numeric::ublas::matrix<double,
104  boost::numeric::ublas::row_major,
105  boost::numeric::ublas::bounded_array<double, maxsize*maxsize>
106  > matrix_t;
107 
108  virtual void assign(const StateBase& other);
109 
110  virtual void show(std::ostream& strm, int level) const;
111 
112  Particle ref;
113 
114  // all three must have the same length, which is the # of charge states
115  std::vector<Particle> real;
116  std::vector<vector_t> moment0;
117  std::vector<matrix_t> moment1;
118  std::vector<matrix_t> transmat;
119 
120  vector_t moment0_env, moment0_rms;
121  matrix_t moment1_env;
122 
123  double last_caviphi0;
124 
125  virtual bool getArray(unsigned idx, ArrayInfo& Info);
126 
127  virtual MomentState* clone() const {
128  return new MomentState(*this, clone_tag());
129  }
130 
131  void recalc() {
132  ref.recalc();
133  for(size_t i=0; i<real.size(); i++) real[i].recalc();
134  }
135 
136  void calc_rms();
137 
138  inline size_t size() const { return real.size(); }
139 
140 protected:
141  MomentState(const MomentState& o, clone_tag);
142 };
143 
147 {
148  typedef MomentState state_t;
149 
150  typedef state_t::matrix_t value_t;
151 
152  MomentElementBase(const Config& c);
153  virtual ~MomentElementBase();
154 
155  void get_misalign(const state_t& ST, const Particle& real, value_t& M, value_t& IM) const;
156 
157  unsigned get_flag(const Config& c, const std::string& name, const unsigned& def_value);
158 
159  virtual void advance(StateBase& s);
160 
164  virtual bool check_cache(const state_t& S) const;
165 
167  virtual bool check_backward(const state_t& S) const;
168 
171  void resize_cache(const state_t& ST);
172 
174  virtual void recompute_matrix(state_t& ST);
175 
176  virtual void show(std::ostream& strm, int level) const;
177 
178  Particle last_ref_in, last_ref_out;
179  std::vector<Particle> last_real_in, last_real_out;
181  std::vector<value_t> transfer;
182  std::vector<value_t> misalign, misalign_inv;
183 
185  double dx, dy, pitch, yaw, roll;
186 
188  bool skipcache;
189 
190  virtual void assign(const ElementVoid *other) =0;
191 
192 protected:
193  // scratch space to avoid temp. allocation in advance()
194  // An Element can't be shared between multiple threads
195  state_t::matrix_t scratch;
196 };
197 
198 #endif // FLAME_MOMENT_H
double gamma
Gamma for ion. (dependent)
Definition: moment.h:22
double IonQ
Ion Charge.
Definition: moment.h:22
virtual bool check_cache(const state_t &S) const
Definition: moment.cpp:728
virtual bool getArray(unsigned idx, ArrayInfo &Info)
Introspect named parameter of the derived class.
Definition: moment.cpp:306
Base class for all simulated elements.
Definition: base.h:165
double IonW
Total energy. (dependent)
Definition: moment.h:22
double IonEk
Kinetic energy.
Definition: moment.h:22
virtual void show(std::ostream &strm, int level) const
Definition: moment.cpp:582
double IonZ
Charge state.
Definition: moment.h:22
The abstract base class for all simulation state objects.
Definition: base.h:29
virtual void advance(StateBase &s)
Propogate the given State through this Element.
Definition: moment.cpp:658
double phis
Absolute synchrotron phase [rad].
Definition: moment.h:22
virtual bool check_backward(const state_t &S) const
Check input state for backward propagation.
Definition: moment.cpp:738
double IonEs
Rest energy.
Definition: moment.h:22
void recalc()
Definition: moment.h:47
double SampleFreq
Sampling frequency [Hz].
Definition: moment.h:22
Associative configuration container.
Definition: config.h:66
virtual void recompute_matrix(state_t &ST)
recalculate &#39;transfer&#39; taking into consideration the provided input state
Definition: moment.cpp:760
Used with StateBase::getArray() to describe a single parameter.
Definition: base.h:51
virtual void assign(const ElementVoid *other)=0
Definition: moment.cpp:563
virtual void assign(const StateBase &other)
Definition: moment.cpp:248
const std::string name
Name of this element (unique in its Machine)
Definition: base.h:192
double dx
constituents of misalign
Definition: moment.h:185
double Brho() const
Magnetic rigidity.
Definition: moment.h:56
bool skipcache
If set, check_cache() will always return false.
Definition: moment.h:188
size_t size() const
of charge states
Definition: moment.h:138
std::vector< value_t > transfer
final transfer matricies
Definition: moment.h:181
double beta
Beta for ion. (dependent)
Definition: moment.h:22
double bg
Beta*gamma. (dependent)
Definition: moment.h:22
virtual void show(std::ostream &strm, int level) const
Definition: moment.cpp:265
virtual MomentState * clone() const
Definition: moment.h:127
double SampleIonK
Sample rate; different RF Cavity due to RF frequenies. (dependent)
Definition: moment.h:22
void resize_cache(const state_t &ST)
Definition: moment.cpp:753
Used with clone ctor.
Definition: base.h:133
An Element which propagates the statistical moments of a bunch.
Definition: moment.h:146
double SampleLambda
Sampling distance [m].
Definition: moment.h:22