diff --git a/src/api/libcellml/interpreter.h b/src/api/libcellml/interpreter.h index 0ee2ec4ead..4edc5742f6 100644 --- a/src/api/libcellml/interpreter.h +++ b/src/api/libcellml/interpreter.h @@ -65,6 +65,43 @@ class LIBCELLML_EXPORT Interpreter */ void setModel(const AnalyserModelPtr &model); + /** + * @brief Get the value of the model's variable of integration. + * + * Return the value of the model's variable of integration. If no variable of integration is needed to compute the + * model then 0.0 is returned. + * + * @return The value of the variable of integration as a @c double. + */ + double voi(); + + /** + * @brief Get the model's states. + * + * Return the model's states. If the model doesn't have any states then @c nullptr is returned. + * + * @return The model's states as an array of @c double. + */ + double *states(); + + /** + * @brief Get the model's rates. + * + * Return the model's rates. If the model doesn't have any rates then @c nullptr is returned. + * + * @return The model's rates as an array of @c double. + */ + double *rates(); + + /** + * @brief Get the model's variables. + * + * Return the model's variables. + * + * @return The model's variables as an array of @c double. + */ + double *variables(); + /** * @brief Initialise the model's variables. * diff --git a/src/bindings/interface/interpreter.i b/src/bindings/interface/interpreter.i index 74297669b9..bb8d2082bc 100644 --- a/src/bindings/interface/interpreter.i +++ b/src/bindings/interface/interpreter.i @@ -16,6 +16,18 @@ %feature("docstring") libcellml::Interpreter::setModel "Sets the model to interpret."; +%feature("docstring") libcellml::Interpreter::voi +"Returns the value of the model's variable of integration."; + +%feature("docstring") libcellml::Interpreter::states +"Returns the model's states."; + +%feature("docstring") libcellml::Interpreter::rates +"Returns the model's rates."; + +%feature("docstring") libcellml::Interpreter::variables +"Returns the model's variables."; + %feature("docstring") libcellml::Interpreter::initialiseVariables "Initialises the model's variables."; diff --git a/src/bindings/javascript/interpreter.cpp b/src/bindings/javascript/interpreter.cpp index ab9a12926d..52b57c5538 100644 --- a/src/bindings/javascript/interpreter.cpp +++ b/src/bindings/javascript/interpreter.cpp @@ -26,6 +26,34 @@ EMSCRIPTEN_BINDINGS(libcellml_interpreter) .smart_ptr_constructor("Interpreter", &libcellml::Interpreter::create) .function("model", &libcellml::Interpreter::model) .function("setModel", &libcellml::Interpreter::setModel) + .function("voi", &libcellml::Interpreter::voi) + .function("states", emscripten::optional_override([](libcellml::InterpreterPtr &interpreter) { + auto states = interpreter->states(); + auto view = emscripten::typed_memory_view(states.size(), states.data()); + auto res = emscripten::val::global("Uint8Array").new_(states.size()); + + res.call("set", view); + + return res; + })) + .function("rates", emscripten::optional_override([](libcellml::InterpreterPtr &interpreter) { + auto rates = interpreter->rates(); + auto view = emscripten::typed_memory_view(rates.size(), rates.data()); + auto res = emscripten::val::global("Uint8Array").new_(rates.size()); + + res.call("set", view); + + return res; + })) + .function("variables", emscripten::optional_override([](libcellml::InterpreterPtr &interpreter) { + auto variables = interpreter->variables(); + auto view = emscripten::typed_memory_view(variables.size(), variables.data()); + auto res = emscripten::val::global("Uint8Array").new_(variables.size()); + + res.call("set", view); + + return res; + })) .function("initialiseVariables", &libcellml::Interpreter::initialiseVariables) .function("computeComputedConstants", &libcellml::Interpreter::computeComputedConstants) .function("computeRates", &libcellml::Interpreter::computeRates) diff --git a/src/interpreter.cpp b/src/interpreter.cpp index 7833357b33..a33e5153a7 100644 --- a/src/interpreter.cpp +++ b/src/interpreter.cpp @@ -45,6 +45,26 @@ void Interpreter::setModel(const AnalyserModelPtr &model) mPimpl->mModel = model; } +double Interpreter::voi() +{ + return mPimpl->mVoi; +} + +double *Interpreter::states() +{ + return mPimpl->mStates; +} + +double *Interpreter::rates() +{ + return mPimpl->mRates; +} + +double *Interpreter::variables() +{ + return mPimpl->mVariables; +} + void Interpreter::initialiseVariables() { } diff --git a/src/interpreter_p.h b/src/interpreter_p.h index 0988fd471d..96af7170ad 100644 --- a/src/interpreter_p.h +++ b/src/interpreter_p.h @@ -30,6 +30,11 @@ std::string generateDoubleCode(const std::string &value); struct Interpreter::InterpreterImpl { AnalyserModelPtr mModel; + + double mVoi = 0.0; + double *mStates = nullptr; + double *mRates = nullptr; + double *mVariables = nullptr; }; } // namespace libcellml diff --git a/tests/bindings/python/test_interpreter.py b/tests/bindings/python/test_interpreter.py index 807caaf59f..780454a892 100644 --- a/tests/bindings/python/test_interpreter.py +++ b/tests/bindings/python/test_interpreter.py @@ -37,6 +37,11 @@ def test_algebraic_eqn_computed_var_on_rhs(self): self.assertIsNotNone(i.model()) + self.assertEqual(0.0, i.voi()) + self.assertIsNone(i.states()) + self.assertIsNone(i.rates()) + self.assertIsNone(i.variables()) + i.initialiseVariables() i.computeComputedConstants() i.computeRates() diff --git a/tests/generator/generator.cpp b/tests/generator/generator.cpp index 2494fa8dcf..a0f3bca08f 100644 --- a/tests/generator/generator.cpp +++ b/tests/generator/generator.cpp @@ -38,6 +38,15 @@ TEST(Generator, emptyModel) EXPECT_EQ(EMPTY_STRING, generator->interfaceCode()); EXPECT_EQ(EMPTY_STRING, generator->implementationCode()); + + auto interpreter = libcellml::Interpreter::create(); + + interpreter->setModel(analyserModel); + + EXPECT_EQ(0.0, interpreter->voi()); + EXPECT_EQ(nullptr, interpreter->states()); + EXPECT_EQ(nullptr, interpreter->rates()); + EXPECT_EQ(nullptr, interpreter->variables()); } TEST(Generator, algebraicEqnComputedVarOnRhs)