13 #include <boost/noncopyable.hpp>
14 #include <boost/any.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <boost/call_traits.hpp>
21 #define FLAME_API_VERSION 0
48 virtual void show(std::ostream&,
int level =0)
const {}
76 for (
int i=
ndim-1; i>=0; --i) {
82 void *raw(
size_t* d) {
83 char *ret = (
char*)
ptr;
84 for (
int i=
ndim-1; i>=0; --i) {
92 inline E*
get(
size_t *d) {
117 virtual bool getArray(
unsigned index, ArrayInfo& Info);
139 std::ostream& operator<<(std::ostream& strm,
const StateBase& s)
184 virtual const char*
type_name()
const =0;
207 virtual void show(std::ostream&,
int level)
const;
248 int max=INT_MAX)
const;
286 inline const std::string&
simtype()
const {
return p_simtype;}
289 inline std::ostream*
trace()
const {
return p_trace;}
300 typedef std::vector<ElementVoid*> p_elements_t;
305 LookupKey(
const std::string& n,
size_t i) :name(n), index(i) {}
306 bool operator<(
const LookupKey& o)
const {
307 int ord = name.compare(o.name);
308 if(ord<0)
return true;
309 else if(ord>0)
return false;
310 else return index<o.index;
314 typedef std::map<LookupKey, ElementVoid*> p_lookup_t;
318 inline size_t size()
const {
return p_elements.size(); }
349 p_lookup_t::const_iterator low (p_lookup.lower_bound(LookupKey(name, 0))),
350 high(p_lookup.upper_bound(LookupKey(name, (
size_t)-1)));
352 for(;low!=high;++low,++i) {
362 std::pair<lookup_iterator, lookup_iterator> all_range() {
369 std::pair<lookup_iterator, lookup_iterator>
equal_range(
const std::string& name) {
370 p_lookup_t::iterator low (p_lookup.lower_bound(LookupKey(name, 0))),
371 high(p_lookup.upper_bound(LookupKey(name, (
size_t)-1)));
379 p_lookup_t::iterator low (p_lookup_type.lower_bound(LookupKey(name, 0))),
380 high(p_lookup_type.upper_bound(LookupKey(name, (
size_t)-1)));
387 p_elements_t p_elements;
389 p_lookup_t p_lookup_type;
390 std::string p_simtype;
391 std::ostream* p_trace;
395 template<
typename State>
396 struct state_builder_impl {
398 {
return new State(c); }
400 struct element_builder_t {
401 virtual ~element_builder_t() {}
405 template<
typename Element>
406 struct element_builder_impl :
public element_builder_t {
407 virtual ~element_builder_impl() {}
409 {
return new Element(c); }
412 std::unique_ptr<ElementVoid> N(build(c));
413 Element *m =
dynamic_cast<Element*
>(o);
415 throw std::runtime_error(
"reconfigure() can't change element type");
423 state_builder_t builder;
424 typedef std::map<std::string, element_builder_t*> elements_t;
430 typedef std::map<std::string, state_info> p_state_infos_t;
431 static p_state_infos_t p_state_infos;
433 static void p_registerState(
const char *name, state_builder_t b);
435 static void p_registerElement(
const std::string& sname,
const char *ename, element_builder_t* b);
456 template<
typename State>
459 p_registerState(name, &state_builder_impl<State>::build);
473 template<
typename Element>
476 p_registerElement(sname, ename,
new element_builder_impl<Element>);
491 friend std::ostream& operator<<(std::ostream&,
const Machine& m);
496 unsigned short level;
497 std::ostringstream strm;
498 LogRecord(
const char *fname,
unsigned short lnum,
unsigned short lvl)
499 :fname(fname), lnum(lnum), level(lvl) {}
502 LogRecord& operator<<(T v)
503 { strm<<v;
return *
this; }
505 LogRecord(
const LogRecord&);
506 LogRecord& operator=(
const LogRecord&);
511 virtual void log(
const LogRecord& r)=0;
514 static int log_detail;
516 static inline bool detail(
int lvl) {
return log_detail<=lvl; }
518 static void set_logger(
const boost::shared_ptr<Logger>& p);
520 static boost::shared_ptr<Logger> p_logger;
523 #define FLAME_ERROR 40
524 #define FLAME_WARN 30
525 #define FLAME_INFO 20
526 #define FLAME_DEBUG 10
530 #define FLAME_LOG_ALWAYS(LVL) Machine::LogRecord(__FILE__, __LINE__, FLAME_##LVL)
532 #ifndef FLAME_DISABLE_LOG
533 #define FLAME_LOG_CHECK(LVL) UNLIKELY(Machine::detail(FLAME_##LVL))
535 #define FLAME_LOG_CHECK(LVL) (false)
539 #define FLAME_LOG(LVL) if(FLAME_LOG_CHECK(LVL)) FLAME_LOG_ALWAYS(LVL)
541 std::ostream& operator<<(std::ostream&,
const Machine& m);
544 void registerLinear();
546 void registerMoment();
548 #endif // FLAME_BASE_H
StateBase(const Config &c)
ElementVoid(const Config &conf)
Construct this element using the provided Config.
p_elements_t::iterator iterator
Beamline element iterator.
bool retreat
retreat (backward) simulation flag
virtual void show(std::ostream &, int level=0) const
Base class for all simulated elements.
double length
Longitudual length of this element (added to StateBase::pos)
virtual void view(const ElementVoid *elem, const StateBase *state)=0
Called from within Machine::propagate()
Machine(const Config &c)
Construct a new Machine.
bool inbounds(size_t *d) const
is the given index valid?
const std::string & simtype() const
Return the sim_type string found during construction.
static void registerState(const char *name)
Register a new State with the simulation framework.
static void registerElement(const char *sname, const char *ename)
Register a new Element type with the simulation framework.
The core simulate Machine engine.
virtual StateBase * clone() const =0
virtual const char * type_name() const =0
virtual void advance(StateBase &s)=0
Propogate the given State through this Element.
Type
The parameter type Double (double) or Sizet (size_t)
The abstract base class for all simulation state objects.
std::pair< lookup_iterator, lookup_iterator > equal_range_type(const std::string &name)
iterator begin()
Points to the first element.
virtual void assign(const ElementVoid *other)=0
void propagate(StateBase *S, size_t start=0, int max=INT_MAX) const
Pass the given bunch State through this Machine.
ElementVoid * find(const std::string &name, size_t nth=0)
Associative configuration container.
Used with StateBase::getArray() to describe a single parameter.
virtual void assign(const StateBase &other)=0
size_t index
Index of this element (unique in its Machine)
static void registeryCleanup()
Discard all registered State and Element type information.
double pos
absolute longitudinal position at end of Element
const ElementVoid * at(size_t i) const
Access a beamline element.
const std::string name
Name of this element (unique in its Machine)
size_t dim[maxdims]
Array dimensions in elements.
const Config & conf() const
The Config used to construct this element.
Allow inspection of intermediate State.
const Config & conf() const
Fetch Config used to construct this Machine.
value_proxy_iterator< p_lookup_t::iterator > lookup_iterator
iterator for use with equal_range() and equal_range_type()
void set_trace(std::ostream *v)
Assign new tracing stream.
ElementVoid * operator[](size_t i)
Access a beamline element.
iterator end()
Points just after the last element.
ElementVoid * at(size_t i)
Access a beamline element.
void reconfigure(size_t idx, const Config &c)
Change the configuration of a single element.
size_t stride[maxdims]
Array strides in bytes.
Observer * observer() const
The current observer, or NULL.
virtual bool getArray(unsigned index, ArrayInfo &Info)
Introspect named parameter of the derived class.
std::ostream * trace() const
The current tracing stream, or NULL.
std::pair< lookup_iterator, lookup_iterator > equal_range(const std::string &name)
const_iterator begin() const
Points to the first element.
const ElementVoid * operator[](size_t i) const
Access a beamline element.
const_iterator end() const
Points just after the last element.
void set_observer(Observer *o)
virtual void show(std::ostream &, int level) const
StateBase * allocState() const
const char * name
The parameter name.
p_elements_t::const_iterator const_iterator
Beamline element iterator (const version)