Explorar el Código

New examples for QSS

Eric Ramat hace 5 años
padre
commit
c446bff074

+ 2 - 1
src/tests/CMakeLists.txt

@@ -2,4 +2,5 @@ ADD_SUBDIRECTORY(dtss)
 ADD_SUBDIRECTORY(mixed)
 ADD_SUBDIRECTORY(mpi)
 ADD_SUBDIRECTORY(multithreading)
-ADD_SUBDIRECTORY(pdevs)
+ADD_SUBDIRECTORY(pdevs)
+ADD_SUBDIRECTORY(qss)

+ 1 - 1
src/tests/multithreading/lifegame/models.hpp

@@ -90,7 +90,7 @@ namespace artis {
                     {
                         std::for_each(bag.begin(), bag.end(),
                                 [this](const common::ExternalEvent<common::DoubleTime>& e) {
-                                    if (e.get_port_index() == IN) {
+                                    if (e.on_port(IN)) {
                                         bool data;
 
                                         e.data()(data);

+ 12 - 0
src/tests/qss/CMakeLists.txt

@@ -0,0 +1,12 @@
+INCLUDE_DIRECTORIES(
+        ${CMAKE_SOURCE_DIR}/src
+        ${ARTIS_INCLUDE_DIRS}
+        ${Boost_INCLUDE_DIRS})
+
+LINK_DIRECTORIES()
+
+ADD_EXECUTABLE(qss-main graph_manager.hpp models.hpp main.cpp)
+
+TARGET_LINK_LIBRARIES(qss-main
+        ${Boost_SYSTEM_LIBRARY}
+        ${Boost_TIMER_LIBRARY})

+ 191 - 0
src/tests/qss/graph_manager.hpp

@@ -0,0 +1,191 @@
+/**
+ * @file tests/qss/graph_manager.cpp
+ * @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 TESTS_QSS_GRAPH_MANAGER_HPP
+#define TESTS_QSS_GRAPH_MANAGER_HPP 1
+
+#include <tests/qss/models.hpp>
+
+#include <artis-star/kernel/pdevs/Coordinator.hpp>
+#include <artis-star/kernel/pdevs/qss/GraphManager.hpp>
+
+namespace artis {
+    namespace tests {
+        namespace qss {
+
+            class ConstantGraphManager :
+                    public artis::pdevs::qss::GraphManager<common::DoubleTime, Constant> {
+            public:
+                ConstantGraphManager(common::Coordinator<common::DoubleTime>* coordinator,
+                        const artis::pdevs::qss::QSSParameters<artis::common::NoParameters>& parameters,
+                        const artis::common::NoParameters& graph_parameters)
+                        :
+                        artis::pdevs::qss::GraphManager<common::DoubleTime, Constant>(coordinator,
+                                parameters, graph_parameters) { }
+
+                ~ConstantGraphManager() override = default;
+            };
+
+            class OnlyOneGraphManager :
+                    public artis::pdevs::qss::GraphManager<common::DoubleTime, Constant> {
+            public:
+                enum submodels {
+                    A
+                };
+
+                OnlyOneGraphManager(common::Coordinator<common::DoubleTime>* coordinator,
+                        const artis::pdevs::qss::QSSParameters<artis::common::NoParameters>& parameters,
+                        const artis::common::NoParameters& graph_parameters)
+                        :
+                        artis::pdevs::qss::GraphManager<common::DoubleTime, Constant>(coordinator,
+                                parameters, graph_parameters),
+                        S("a", parameters, graph_parameters)
+                {
+                    add_child(A, &S);
+                }
+
+                ~OnlyOneGraphManager() override = default;
+
+            private:
+                artis::pdevs::Coordinator<common::DoubleTime, ConstantGraphManager, artis::pdevs::qss::QSSParameters<artis::common::NoParameters>> S;
+            };
+
+            class ParabolaGraphManager :
+                    public artis::pdevs::qss::GraphManager<common::DoubleTime, Parabola, ParabolaParameters> {
+            public:
+                ParabolaGraphManager(common::Coordinator<common::DoubleTime>* coordinator,
+                        const artis::pdevs::qss::QSSParameters<ParabolaParameters>& parameters,
+                        const artis::common::NoParameters& graph_parameters)
+                        :
+                        artis::pdevs::qss::GraphManager<common::DoubleTime, Parabola, ParabolaParameters>(
+                                coordinator, parameters, graph_parameters) { }
+
+                ~ParabolaGraphManager() override = default;
+            };
+
+            class OnlyOneParabolaGraphManager :
+                    public artis::pdevs::GraphManager<common::DoubleTime, artis::pdevs::qss::QSSParameters<ParabolaParameters>> {
+            public:
+                enum submodels {
+                    A
+                };
+
+                OnlyOneParabolaGraphManager(common::Coordinator<common::DoubleTime>* coordinator,
+                        const artis::pdevs::qss::QSSParameters<ParabolaParameters>& parameters,
+                        const artis::common::NoParameters& graph_parameters)
+                        :
+                        artis::pdevs::GraphManager<common::DoubleTime, artis::pdevs::qss::QSSParameters<ParabolaParameters>>(
+                                coordinator, parameters, graph_parameters),
+                        S("a", parameters, graph_parameters)
+                {
+                    add_child(A, &S);
+                }
+
+                ~OnlyOneParabolaGraphManager() override = default;
+
+            private:
+                artis::pdevs::Coordinator<common::DoubleTime, ParabolaGraphManager, artis::pdevs::qss::QSSParameters<ParabolaParameters>> S;
+            };
+
+            class PredatorGraphManager :
+                    public artis::pdevs::qss::GraphManager<common::DoubleTime, Predator, PreyPredatorParameters> {
+            public:
+                enum inputs {
+                    IN_X
+                };
+
+                PredatorGraphManager(common::Coordinator<common::DoubleTime>* coordinator,
+                        const artis::pdevs::qss::QSSParameters<PreyPredatorParameters>& parameters,
+                        const artis::common::NoParameters& graph_parameters)
+                        :
+                        artis::pdevs::qss::GraphManager<common::DoubleTime, Predator, PreyPredatorParameters>(
+                                coordinator, parameters, graph_parameters) {
+                    coordinator->input_port({IN_X, "in_x"});
+                    in({coordinator, IN_X}) >> in({derivative(), Predator::IN_X});
+                }
+
+                ~PredatorGraphManager() override = default;
+            };
+
+            class PreyGraphManager :
+                    public artis::pdevs::qss::GraphManager<common::DoubleTime, Prey, PreyPredatorParameters> {
+            public:
+                enum inputs {
+                    IN_Y
+                };
+
+                PreyGraphManager(common::Coordinator<common::DoubleTime>* coordinator,
+                        const artis::pdevs::qss::QSSParameters<PreyPredatorParameters>& parameters,
+                        const artis::common::NoParameters& graph_parameters)
+                        :
+                        artis::pdevs::qss::GraphManager<common::DoubleTime, Prey, PreyPredatorParameters>(
+                                coordinator, parameters, graph_parameters) {
+                    coordinator->input_port({IN_Y, "in_y"});
+                    in({coordinator, IN_Y}) >> in({derivative(), Prey::IN_Y});
+                }
+
+                ~PreyGraphManager() override = default;
+            };
+
+            struct PreyPredatorGraphManagerParameters {
+                artis::pdevs::qss::QSSParameters<PreyPredatorParameters> _predator;
+                artis::pdevs::qss::QSSParameters<PreyPredatorParameters> _prey;
+            };
+
+            class PreyPredatorGraphManager :
+                    public artis::pdevs::GraphManager<common::DoubleTime, PreyPredatorGraphManagerParameters> {
+            public:
+                enum submodels {
+                    PREDATOR, PREY
+                };
+
+                PreyPredatorGraphManager(common::Coordinator<common::DoubleTime>* coordinator,
+                        const PreyPredatorGraphManagerParameters& parameters,
+                        const artis::common::NoParameters& graph_parameters)
+                        :
+                        artis::pdevs::GraphManager<common::DoubleTime, PreyPredatorGraphManagerParameters>(
+                                coordinator, parameters, graph_parameters),
+                        _predator("predator", parameters._predator, graph_parameters),
+                        _prey("prey", parameters._prey, graph_parameters)
+                {
+                    add_child(PREDATOR, &_predator);
+                    add_child(PREY, &_prey);
+                    out({&_predator, PredatorGraphManager::OUT}) >> in({&_prey, PreyGraphManager::IN_Y});
+                    out({&_prey, PreyGraphManager::OUT}) >> in({&_predator, PredatorGraphManager::IN_X});
+                }
+
+                ~PreyPredatorGraphManager() override = default;
+
+            private:
+                artis::pdevs::Coordinator<common::DoubleTime, PredatorGraphManager, artis::pdevs::qss::QSSParameters<PreyPredatorParameters>> _predator;
+                artis::pdevs::Coordinator<common::DoubleTime, PreyGraphManager, artis::pdevs::qss::QSSParameters<PreyPredatorParameters>> _prey;
+            };
+
+        }
+    }
+} // namespace artis tests qss
+
+#endif

+ 133 - 0
src/tests/qss/main.cpp

@@ -0,0 +1,133 @@
+/**
+ * @file pdevs/tests.cpp
+ * @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/>.
+ */
+
+#include <tests/qss/graph_manager.hpp>
+
+#include <artis-star/common/RootCoordinator.hpp>
+#include <artis-star/common/observer/Iterator.hpp>
+
+using namespace artis::tests::qss;
+
+void test_parabola()
+{
+    artis::pdevs::qss::QSSParameters<ParabolaParameters> parameters = {{0.5},
+                                                                       {true, true, 0.001, 3},
+                                                                       {0.2}};
+
+    class View : public artis::observer::View<artis::common::DoubleTime> {
+    public:
+        View()
+        {
+            selector("Value", {OnlyOneParabolaGraphManager::A, ParabolaGraphManager::S_Integrator,
+                               artis::pdevs::qss::Integrator<artis::common::DoubleTime>::VALUE});
+        }
+    };
+
+    artis::common::RootCoordinator<
+            artis::common::DoubleTime, artis::pdevs::Coordinator<
+                    artis::common::DoubleTime,
+                    OnlyOneParabolaGraphManager,
+                    artis::pdevs::qss::QSSParameters<ParabolaParameters>>
+    > rc(0, 5, "root", parameters, artis::common::NoParameters());
+
+    rc.attachView("Value", new View());
+
+    rc.run();
+
+    const View::Values& values = rc.observer().view("Value").get("Value");
+
+    for (const auto& value: values) {
+        double v;
+
+        value.second(v);
+        std::cout << value.first << ": " << v << std::endl;
+    }
+}
+
+void test_predator_prey()
+{
+    PreyPredatorGraphManagerParameters parameters = {{{45.},
+                                                             {true, true, 0.1, 3},
+                                                             {0.5, 0.01, 0.01, 0.2}},
+                                                     {{5000.},
+                                                             {true, true, 1, 3},
+                                                             {0.5, 0.01, 0.01, 0.2}}};
+
+    class View : public artis::observer::View<artis::common::DoubleTime> {
+    public:
+        View()
+        {
+            selector("PredatorView",
+                    {PreyPredatorGraphManager::PREDATOR, PredatorGraphManager::S_Integrator,
+                     artis::pdevs::qss::Integrator<artis::common::DoubleTime>::VALUE});
+            selector("PreyView",
+                    {PreyPredatorGraphManager::PREY, PreyGraphManager::S_Integrator,
+                     artis::pdevs::qss::Integrator<artis::common::DoubleTime>::VALUE});
+        }
+    };
+
+    artis::common::RootCoordinator<
+            artis::common::DoubleTime, artis::pdevs::Coordinator<
+                    artis::common::DoubleTime,
+                    PreyPredatorGraphManager,
+                    PreyPredatorGraphManagerParameters>
+    > rc(0, 100, "root", parameters, artis::common::NoParameters());
+
+    rc.attachView("Value", new View());
+
+    rc.run();
+
+    {
+        artis::observer::DiscreteTimeIterator<artis::common::DoubleTime> it(
+                rc.observer().view("Value").get("PredatorView"), 0, 0.1);
+
+        while (it.has_next()) {
+            double v;
+
+            (*it).second(v);
+            std::cout << (*it).first << ";" << v << std::endl;
+            ++it;
+        }
+    }
+    {
+        artis::observer::DiscreteTimeIterator<artis::common::DoubleTime> it(
+                rc.observer().view("Value").get("PreyView"), 0, 0.1);
+
+        while (it.has_next()) {
+            double v;
+
+            (*it).second(v);
+            std::cout << (*it).first << ";" << v << std::endl;
+            ++it;
+        }
+    }
+}
+
+int main()
+{
+//    test_parabola();
+    test_predator_prey();
+}

+ 130 - 0
src/tests/qss/models.hpp

@@ -0,0 +1,130 @@
+/**
+ * @file tests/qss/models.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 TESTS_QSS_MODELS_HPP
+#define TESTS_QSS_MODELS_HPP 1
+
+#include <artis-star/common/time/DoubleTime.hpp>
+#include <artis-star/common/utils/Trace.hpp>
+
+#include <artis-star/kernel/pdevs/qss/Derivative.hpp>
+
+namespace artis {
+    namespace tests {
+        namespace qss {
+
+            class Constant : public artis::pdevs::qss::Derivative<common::DoubleTime, Constant> {
+            public:
+                Constant(const std::string& name,
+                        const artis::pdevs::Context<common::DoubleTime, Constant, artis::common::NoParameters>& context)
+                        :
+                        artis::pdevs::qss::Derivative<common::DoubleTime, Constant>(name,
+                                context) { }
+
+                ~Constant() override = default;
+
+                double compute() const override { return 0.5; }
+            };
+
+            struct ParabolaParameters {
+                double alpha;
+            };
+
+            class Parabola : public artis::pdevs::qss::Derivative<common::DoubleTime, Parabola, ParabolaParameters> {
+            public:
+                Parabola(const std::string& name,
+                        const artis::pdevs::Context<common::DoubleTime, Parabola, ParabolaParameters>& context)
+                        :
+                        artis::pdevs::qss::Derivative<common::DoubleTime, Parabola, ParabolaParameters>(name,
+                                context), _alpha(context.parameters().alpha) { }
+
+                ~Parabola() override = default;
+
+                double compute() const override { return _alpha * input(IN); }
+
+            private:
+                double _alpha;
+            };
+
+            struct PreyPredatorParameters {
+                double a;
+                double b;
+                double d;
+                double e;
+            };
+
+            class Predator : public artis::pdevs::qss::Derivative<common::DoubleTime, Predator, PreyPredatorParameters> {
+            public:
+                enum inputs {
+                    IN_X = IN + 1
+                };
+
+                Predator(const std::string& name,
+                        const artis::pdevs::Context<common::DoubleTime, Predator, PreyPredatorParameters>& context)
+                        :
+                        artis::pdevs::qss::Derivative<common::DoubleTime, Predator, PreyPredatorParameters>(name,
+                                context), _b(context.parameters().b), _d(context.parameters().d), _e(context.parameters().e) {
+                    input_port({IN_X, "in_x"});
+                }
+
+                ~Predator() override = default;
+
+                double compute() const override { return _b * _d * input(IN_X) * input(IN) - _e * input(IN); }
+
+            private:
+                double _b;
+                double _d;
+                double _e;
+            };
+
+            class Prey : public artis::pdevs::qss::Derivative<common::DoubleTime, Prey, PreyPredatorParameters> {
+            public:
+                enum inputs {
+                    IN_Y = IN + 1
+                };
+
+                Prey(const std::string& name,
+                        const artis::pdevs::Context<common::DoubleTime, Prey, PreyPredatorParameters>& context)
+                        :
+                        artis::pdevs::qss::Derivative<common::DoubleTime, Prey, PreyPredatorParameters>(name,
+                                context), _a(context.parameters().a), _b(context.parameters().b) {
+                    input_port({IN_Y, "in_y"});
+                }
+
+                ~Prey() override = default;
+
+                double compute() const override { return _a * input(IN) - _b * input(IN_Y) * input(IN); }
+
+            private:
+                double _a;
+                double _b;
+            };
+
+        }
+    }
+} // namespace artis tests qss
+
+#endif