diff options
author | Jan Huwald <jh@sotun.de> | 2012-05-07 20:01:51 (GMT) |
---|---|---|
committer | Jan Huwald <jh@sotun.de> | 2012-05-07 20:01:51 (GMT) |
commit | 420d2ef464d4a741028e132e662d5626806a41f5 (patch) | |
tree | 1aca6eb512e4ed0fb5f3c10c528cb998b6ffd695 /core/pla_evolve.hpp |
Diffstat (limited to 'core/pla_evolve.hpp')
-rw-r--r-- | core/pla_evolve.hpp | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/core/pla_evolve.hpp b/core/pla_evolve.hpp new file mode 100644 index 0000000..032d9d4 --- /dev/null +++ b/core/pla_evolve.hpp @@ -0,0 +1,93 @@ +#ifndef FznMZXtH9pg7npfVO7D248udzM8 +#define FznMZXtH9pg7npfVO7D248udzM8 + +#include <assert.h> + +#include <boost/type_traits/is_same.hpp> + +#include "context.hpp" +#include "continuous_property.hpp" +#include "template_helpers.hpp" +#include "pla_set.hpp" +#include "time.hpp" + +// local state +// general case: no action (and zero storage) +template<typename Prop, typename Quant1, typename Quant2> +struct PLA_Evolve_LocalState { + template <typename PropComp, typename Context, typename LocalData> + inline void evolve(PropComp &pc, Context context, LocalData &data, Time newTime) { + DO_NOT_CALL; + } + + template <typename PropComp, typename Context> + inline void commit(PropComp &pc, Context context, Time time) { + DO_NOT_CALL; + } +}; + +// matching case: get/set the value to local storage +template<typename Prop, typename Quant> +struct PLA_Evolve_LocalState<Prop, Quant, Quant> { + typename Prop::type val; + + template <typename PropComp, typename Context, typename LocalData> + inline void evolve(PropComp &pc, Context context, LocalData &data, Time newTime) { + // calc time difference (= time amount we evolve) + Time oldTime = data.data.getTime(newTime, context.template getptr<Quant>()()); + Time dt = newTime - oldTime; + assert(dt >= 0); + + // calc new val and store locally + val = ContinuousProperty_Evolve<PropComp, Prop, Quant, Context>:: + eval(pc, context, oldTime, dt); + } + + template <typename PropComp, typename Context> + inline void commit(PropComp &pc, Context context, Time time) { + Ptr<Quant> dst(context.template getptr<Quant>()); + PLA_Set<Prop> pla_set(time, dst, val); + pc.call(pla_set); + } +}; + +// evolve action +template<typename Quant, typename Context = ContinuousContext<Quant> > +struct PLA_Evolve { + // action types & consts + typedef Void result_t; + template<typename ContextProp, bool _doWriteResults> + struct local_state_t + : PLA_Evolve_LocalState<ContextProp, Quant, typename ContextProp::quant> + { + typedef ContextProp prop; + }; + + // action state & constructor + typedef typename Quant::instance_ptr_t instance_ptr_t; + Context context; + Time newTime; + result_t result; + + PLA_Evolve(Context context, Time newTime) : context(context), newTime(newTime) {} + + // action methods + template<typename PropComp, typename LocalData, typename LocalState> + inline void pre(PropComp &pc, LocalData &data, LocalState &state) { + // if we have the correct quantity ... + if (boost::is_same<Quant, typename LocalState::prop::quant>::value) + state.evolve(pc, context, data, newTime); // ... evolve it + } + + template<typename _PropComp, typename _LocalData, typename LocalState> + inline void post(_PropComp &pc, _LocalData &data, LocalState &state) { + // if we have the correct quantity ... + if (boost::is_same<Quant, typename LocalState::prop::quant>::value) + state.commit(pc, context, newTime); // ... store new value and time + } + +private: + PLA_Evolve(); +}; + +#endif // FznMZXtH9pg7npfVO7D248udzM8 |