5 #include "flame/linear.h"
6 #include "flame/state/vector.h"
7 #include "flame/state/matrix.h"
10 #define sqr(x) ((x)*(x))
11 #define cube(x) ((x)*(x)*(x))
22 MatrixState::MatrixState(
const Config& c)
24 ,state(boost::numeric::ublas::identity_matrix<double>(maxsize))
27 const std::vector<double>& I = c.
get<std::vector<double> >(
"initial");
28 if(I.size()>state.data().size())
29 throw std::invalid_argument(
"Initial state size too big");
30 std::copy(I.begin(), I.end(), state.data().begin());
33 }
catch(boost::bad_any_cast&){
34 throw std::invalid_argument(
"'initial' has wrong type (must be vector)");
38 MatrixState::~MatrixState() {}
40 MatrixState::MatrixState(
const MatrixState& o, clone_tag t)
49 throw std::invalid_argument(
"Can't assign State: incompatible types");
56 strm<<
"State: "<<state<<
"\n";
62 Info.
ptr = &state(0,0);
63 Info.type = ArrayInfo::Double;
65 Info.
dim[0] = state.size1();
66 Info.
dim[1] = state.size2();
67 Info.
stride[0] =
sizeof(double)*state.size1();
68 Info.
stride[1] =
sizeof(double);
74 VectorState::VectorState(
const Config& c)
79 const std::vector<double>& I = c.
get<std::vector<double> >(
"initial");
80 if(I.size()>state.size())
81 throw std::invalid_argument(
"Initial state size too big");
82 std::copy(I.begin(), I.end(), state.begin());
84 }
catch(boost::bad_any_cast&){
88 VectorState::~VectorState() {}
90 VectorState::VectorState(
const VectorState& o, clone_tag t)
99 throw std::invalid_argument(
"Can't assign State: incompatible types");
104 void VectorState::show(std::ostream& strm)
const
106 strm<<
"State: "<<state<<
"\n";
112 Info.
ptr = &state(0);
113 Info.type = ArrayInfo::Double;
115 Info.
dim[0] = state.size();
116 Info.
stride[0] =
sizeof(double);
124 template<
typename Base>
125 void Get2by2Matrix(
const double L,
const double K,
const unsigned ind,
typename Base::value_t &M)
140 M(ind, ind) = M(ind+1, ind+1) = cs;
142 M(ind, ind+1) = sn/sqrtK;
146 M(ind+1, ind) = -sqrtK*sn;
156 M(ind, ind) = M(ind+1, ind+1) = cs;
158 M(ind, ind+1) = sn/sqrtK;
162 M(ind+1, ind) = sqrtK*sn;
168 template<
typename Base>
169 struct ElementSource :
public Base
172 typedef typename base_t::state_t
state_t;
173 ElementSource(
const Config& c)
174 :base_t(c), istate(c)
179 state_t& ST =
static_cast<state_t&
>(s);
184 virtual void show(std::ostream& strm,
int level)
const
187 strm<<
"Initial: "<<istate.state<<
"\n";
193 virtual ~ElementSource() {}
195 virtual const char* type_name()
const {
return "source";}
198 template<
typename Base>
199 struct ElementMark :
public Base
203 typedef typename base_t::state_t state_t;
204 ElementMark(
const Config& c)
211 virtual ~ElementMark() {}
213 virtual const char* type_name()
const {
return "marker";}
216 template<
typename Base>
217 struct ElementDrift :
public Base
221 typedef typename base_t::state_t state_t;
222 ElementDrift(
const Config& c)
225 double L = this->length*MtoMM;
227 this->transfer(state_t::PS_X, state_t::PS_PX) = L;
228 this->transfer(state_t::PS_Y, state_t::PS_PY) = L;
232 virtual ~ElementDrift() {}
234 virtual const char* type_name()
const {
return "drift";}
237 template<
typename Base>
238 struct ElementSBend :
public Base
242 typedef typename base_t::state_t state_t;
243 ElementSBend(
const Config& c)
246 double L = this->length*MtoMM,
247 phi = c.
get<
double>(
"phi"),
249 K = c.
get<
double>(
"K", 0e0)/sqr(MtoMM),
250 Kx = K + 1e0/sqr(rho),
254 Get2by2Matrix<Base>(L, Kx, (unsigned)state_t::PS_X, this->transfer);
256 Get2by2Matrix<Base>(L, Ky, (unsigned)state_t::PS_Y, this->transfer);
260 virtual ~ElementSBend() {}
262 virtual const char* type_name()
const {
return "sbend";}
265 template<
typename Base>
266 struct ElementQuad :
public Base
270 typedef typename base_t::state_t
state_t;
271 ElementQuad(
const Config& c)
274 double L = this->length*MtoMM,
276 K = c.
get<
double>(
"K", 0e0)/sqr(MtoMM);
279 Get2by2Matrix<Base>(L, K, (unsigned)state_t::PS_X, this->transfer);
281 Get2by2Matrix<Base>(L, -K, (unsigned)state_t::PS_Y, this->transfer);
286 virtual ~ElementQuad() {}
288 virtual const char* type_name()
const {
return "quadrupole";}
291 template<
typename Base>
292 struct ElementSolenoid :
public Base
296 typedef typename base_t::state_t
state_t;
297 ElementSolenoid(
const Config& c)
300 double L = this->length*MtoMM,
302 K = c.
get<
double>(
"K", 0e0)/MtoMM,
306 this->transfer(state_t::PS_X, state_t::PS_X)
307 = this->transfer(state_t::PS_PX, state_t::PS_PX)
308 = this->transfer(state_t::PS_Y, state_t::PS_Y)
309 = this->transfer(state_t::PS_PY, state_t::PS_PY)
313 this->transfer(state_t::PS_X, state_t::PS_PX) = S*C/K;
315 this->transfer(state_t::PS_X, state_t::PS_PX) = L;
316 this->transfer(state_t::PS_X, state_t::PS_Y) = S*C;
318 this->transfer(state_t::PS_X, state_t::PS_PY) = sqr(S)/K;
320 this->transfer(state_t::PS_X, state_t::PS_PY) = 0e0;
322 this->transfer(state_t::PS_PX, state_t::PS_X) = -K*S*C;
323 this->transfer(state_t::PS_PX, state_t::PS_Y) = -K*sqr(S);
324 this->transfer(state_t::PS_PX, state_t::PS_PY) = S*C;
326 this->transfer(state_t::PS_Y, state_t::PS_X) = -S*C;
328 this->transfer(state_t::PS_Y, state_t::PS_PX) = -sqr(S)/K;
330 this->transfer(state_t::PS_Y, state_t::PS_PX) = 0e0;
332 this->transfer(state_t::PS_Y, state_t::PS_PY) = S*C/K;
334 this->transfer(state_t::PS_Y, state_t::PS_PY) = L;
336 this->transfer(state_t::PS_PY, state_t::PS_X) = K*sqr(S);
337 this->transfer(state_t::PS_PY, state_t::PS_PX) = -S*C;
338 this->transfer(state_t::PS_PY, state_t::PS_Y) = -K*S*C;
344 virtual ~ElementSolenoid() {}
346 virtual const char* type_name()
const {
return "solenoid";}
349 template<
typename Base>
350 struct ElementGeneric :
public Base
353 typedef typename base_t::state_t state_t;
354 ElementGeneric(
const Config& c)
357 std::vector<double> I = c.
get<std::vector<double> >(
"transfer");
358 if(I.size()>this->transfer.data().size())
359 throw std::invalid_argument(
"Initial transfer size too big");
360 std::copy(I.begin(), I.end(), this->transfer.data().begin());
362 virtual ~ElementGeneric() {}
364 virtual const char* type_name()
const {
return "generic";}
369 void registerLinear()
371 Machine::registerState<VectorState>(
"Vector");
372 Machine::registerState<MatrixState>(
"TransferMatrix");
374 Machine::registerElement<ElementSource<LinearElementBase<VectorState> > >(
"Vector",
"source");
375 Machine::registerElement<ElementSource<LinearElementBase<MatrixState> > >(
"TransferMatrix",
"source");
377 Machine::registerElement<ElementMark<LinearElementBase<VectorState> > >(
"Vector",
"marker");
378 Machine::registerElement<ElementMark<LinearElementBase<MatrixState> > >(
"TransferMatrix",
"marker");
380 Machine::registerElement<ElementDrift<LinearElementBase<VectorState> > >(
"Vector",
"drift");
381 Machine::registerElement<ElementDrift<LinearElementBase<MatrixState> > >(
"TransferMatrix",
"drift");
383 Machine::registerElement<ElementSBend<LinearElementBase<VectorState> > >(
"Vector",
"sbend");
384 Machine::registerElement<ElementSBend<LinearElementBase<MatrixState> > >(
"TransferMatrix",
"sbend");
386 Machine::registerElement<ElementQuad<LinearElementBase<VectorState> > >(
"Vector",
"quadrupole");
387 Machine::registerElement<ElementQuad<LinearElementBase<MatrixState> > >(
"TransferMatrix",
"quadrupole");
389 Machine::registerElement<ElementSolenoid<LinearElementBase<VectorState> > >(
"Vector",
"solenoid");
390 Machine::registerElement<ElementSolenoid<LinearElementBase<MatrixState> > >(
"TransferMatrix",
"solenoid");
392 Machine::registerElement<ElementGeneric<LinearElementBase<VectorState> > >(
"Vector",
"generic");
393 Machine::registerElement<ElementGeneric<LinearElementBase<MatrixState> > >(
"TransferMatrix",
"generic");
virtual void assign(const StateBase &other)
Simulation state which include only a matrix.
void assign(const StateBase &other)
Simulation state which include only a vector.
virtual void show(std::ostream &strm, int level) const
The abstract base class for all simulation state objects.
Associative configuration container.
Used with StateBase::getArray() to describe a single parameter.
virtual void assign(const StateBase &other)=0
virtual void assign(const StateBase &other)
size_t dim[maxdims]
Array dimensions in elements.
size_t stride[maxdims]
Array strides in bytes.
virtual bool getArray(unsigned index, ArrayInfo &Info)
Introspect named parameter of the derived class.
detail::RT< T >::type get(const std::string &name) const
virtual void show(std::ostream &, int level) const
virtual bool getArray(unsigned idx, ArrayInfo &Info)
Introspect named parameter of the derived class.
virtual bool getArray(unsigned idx, ArrayInfo &Info)
Introspect named parameter of the derived class.
const char * name
The parameter name.