123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /**
- * @file kernel/pdevs/qss/Derivative.hpp
- * @author The ARTIS Development Team
- * See the AUTHORS or Authors.txt file
- */
- /*
- * ARTIS - the multimodeling and simulation environment
- * This file is a part of the ARTIS environment
- *
- * Copyright (C) 2013-2019 ULCO http://www.univ-littoral.fr
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef QSS_DERIVATIVE
- #define QSS_DERIVATIVE
- #include <artis-star/kernel/pdevs/Dynamics.hpp>
- #include <artis-star/kernel/pdevs/qss/Data.hpp>
- namespace artis {
- namespace pdevs {
- namespace qss {
- template<class Time, class Dyn, class Parameters = common::NoParameters>
- class Derivative : public artis::pdevs::Dynamics<Time, Dyn, Parameters> {
- public:
- enum inputs {
- IN = 1
- };
- enum outputs {
- OUT = 1
- };
- Derivative(const std::string& name, const Context<Time, Dyn, Parameters>& context)
- :
- artis::pdevs::Dynamics<Time, Dyn, Parameters>(name, context)
- {
- this->input_port({IN, "in"});
- this->output_port({OUT, "out"});
- }
- virtual ~Derivative() { }
- virtual double compute() const = 0;
- virtual void dconf(typename Time::type t, typename Time::type e,
- const common::Bag<Time>& bag)
- {
- dint(t);
- dext(t, e, bag);
- }
- virtual void dint(typename Time::type /* time */)
- {
- if (_state == RESPONSE) {
- _last_output = _output_value;
- }
- _state = WAIT;
- }
- virtual void dext(typename Time::type t, typename Time::type e,
- const common::Bag<Time>& bag)
- {
- std::for_each(bag.begin(), bag.end(),
- [this, t, e](const common::ExternalEvent<Time>& event) {
- IntegratorData data;
- event.data()(data);
- _inputs[event.port_index()] = data.value;
- switch (_state) {
- case INIT:
- if (_inputs.size() == _input_number) {
- _output_value = compute();
- _state = RESPONSE;
- }
- break;
- case WAIT:
- case RESPONSE:
- double value = compute();
- if (value != _last_output) {
- _output_value = value;
- _state = RESPONSE;
- } else {
- _state = WAIT;
- }
- }
- });
- }
- virtual typename Time::type
- start(typename Time::type /* time */)
- {
- _input_number = this->input_port_number();
- if (_input_number == 0) {
- _output_value = compute();
- _state = RESPONSE;
- return 0;
- } else {
- _state = INIT;
- return Time::infinity;
- }
- }
- virtual typename Time::type ta(typename Time::type /* time */)
- {
- switch (_state) {
- case INIT:
- return Time::infinity;
- case WAIT:
- return Time::infinity;
- case RESPONSE:
- return 0;
- }
- return Time::infinity;
- }
- virtual common::Bag<Time> lambda(typename Time::type /* time */) const
- {
- common::Bag<Time> msgs;
- switch (_state) {
- case INIT:
- break;
- case WAIT:
- break;
- case RESPONSE:
- const DerivativeData data = {_output_value};
- msgs.push_back(common::ExternalEvent<Time>(OUT, data));
- }
- return msgs;
- }
- virtual common::Value observe(const typename Time::type& /* t */,
- unsigned int /* index */) const
- {
- return common::Value();
- }
- double input(unsigned int index) const {
- return _inputs.at(index);
- }
- private:
- typedef enum {
- INIT, WAIT, RESPONSE
- } State;
- State _state;
- unsigned int _input_number;
- std::map<unsigned int, double> _inputs;
- double _output_value;
- double _last_output;
- };
- }
- }
- }
- #endif
|