diff --git a/src/api/libcellml/interpreter.h b/src/api/libcellml/interpreter.h index 0ee2ec4ead..bed0b44fc2 100644 --- a/src/api/libcellml/interpreter.h +++ b/src/api/libcellml/interpreter.h @@ -65,6 +65,70 @@ 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 number of states in the model. + * + * Return the number of states in the model. If the model doesn't have any states then 0 is returned. + * + * @return The number of states in the model as a @c size_t. + */ + size_t stateCount(); + + /** + * @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 number of rates in the model. + * + * Return the number of rates in the model. If the model doesn't have any rates then 0 is returned. + * + * @return The number of rates in the model as a @c size_t. + */ + size_t rateCount(); + + /** + * @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 number of variables in the model. + * + * Return the number of variables in the model. If the model doesn't have any variables then 0 is returned. + * + * @return The number of variables in the model as a @c size_t. + */ + size_t variableCount(); + + /** + * @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..f85c9ec184 100644 --- a/src/bindings/interface/interpreter.i +++ b/src/bindings/interface/interpreter.i @@ -16,6 +16,27 @@ %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::stateCount +"Returns the number of states in the model."; + +%feature("docstring") libcellml::Interpreter::states +"Returns the model's states."; + +%feature("docstring") libcellml::Interpreter::rateCount +"Returns the number of rates in the model."; + +%feature("docstring") libcellml::Interpreter::rates +"Returns the model's rates."; + +%feature("docstring") libcellml::Interpreter::variableCount +"Returns the number of variables in the model."; + +%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..ab3ef817c2 100644 --- a/src/bindings/javascript/interpreter.cpp +++ b/src/bindings/javascript/interpreter.cpp @@ -26,6 +26,40 @@ 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("stateCount", &libcellml::Interpreter::stateCount) + .function("states", emscripten::optional_override([](libcellml::InterpreterPtr &interpreter) { + auto states = interpreter->states(); + auto stateCount = interpreter->stateCount(); + auto view = emscripten::typed_memory_view(stateCount, states); + auto res = emscripten::val::global("Uint8Array").new_(stateCount); + + res.call("set", view); + + return res; + })) + .function("rateCount", &libcellml::Interpreter::rateCount) + .function("rates", emscripten::optional_override([](libcellml::InterpreterPtr &interpreter) { + auto rates = interpreter->rates(); + auto rateCount = interpreter->rateCount(); + auto view = emscripten::typed_memory_view(rateCount, rates); + auto res = emscripten::val::global("Uint8Array").new_(rateCount); + + res.call("set", view); + + return res; + })) + .function("variableCount", &libcellml::Interpreter::variableCount) + .function("variables", emscripten::optional_override([](libcellml::InterpreterPtr &interpreter) { + auto variables = interpreter->variables(); + auto variableCount = interpreter->variableCount(); + auto view = emscripten::typed_memory_view(variableCount, variables); + auto res = emscripten::val::global("Uint8Array").new_(variableCount); + + 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..0d0ed91129 100644 --- a/src/interpreter.cpp +++ b/src/interpreter.cpp @@ -45,6 +45,41 @@ void Interpreter::setModel(const AnalyserModelPtr &model) mPimpl->mModel = model; } +double Interpreter::voi() +{ + return mPimpl->mVoi; +} + +size_t Interpreter::stateCount() +{ + return mPimpl->mStateCount; +} + +double *Interpreter::states() +{ + return mPimpl->mStates; +} + +size_t Interpreter::rateCount() +{ + return mPimpl->mRateCount; +} + +double *Interpreter::rates() +{ + return mPimpl->mRates; +} + +size_t Interpreter::variableCount() +{ + return mPimpl->mVariableCount; +} + +double *Interpreter::variables() +{ + return mPimpl->mVariables; +} + void Interpreter::initialiseVariables() { } diff --git a/src/interpreter_p.h b/src/interpreter_p.h index 0988fd471d..203ad15aeb 100644 --- a/src/interpreter_p.h +++ b/src/interpreter_p.h @@ -30,6 +30,17 @@ std::string generateDoubleCode(const std::string &value); struct Interpreter::InterpreterImpl { AnalyserModelPtr mModel; + + double mVoi = 0.0; + + size_t mStateCount = 0; + double *mStates = nullptr; + + size_t mRateCount = 0; + double *mRates = nullptr; + + size_t mVariableCount = 0; + double *mVariables = nullptr; }; } // namespace libcellml diff --git a/tests/bindings/python/test_interpreter.py b/tests/bindings/python/test_interpreter.py index 807caaf59f..5a3dd9962c 100644 --- a/tests/bindings/python/test_interpreter.py +++ b/tests/bindings/python/test_interpreter.py @@ -37,6 +37,17 @@ def test_algebraic_eqn_computed_var_on_rhs(self): self.assertIsNotNone(i.model()) + self.assertEqual(0.0, i.voi()) + + self.assertEqual(0, i.stateCount()) + self.assertIsNone(i.states()) + + self.assertEqual(0, i.rateCount()) + self.assertIsNone(i.rates()) + + self.assertEqual(0, i.variableCount()) + self.assertIsNone(i.variables()) + i.initialiseVariables() i.computeComputedConstants() i.computeRates() diff --git a/tests/generator/generator.cpp b/tests/generator/generator.cpp index 2494fa8dcf..e1e7cd86d2 100644 --- a/tests/generator/generator.cpp +++ b/tests/generator/generator.cpp @@ -38,6 +38,21 @@ 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(size_t(0), interpreter->stateCount()); + EXPECT_EQ(nullptr, interpreter->states()); + + EXPECT_EQ(size_t(0), interpreter->rateCount()); + EXPECT_EQ(nullptr, interpreter->rates()); + + EXPECT_EQ(size_t(0), interpreter->variableCount()); + EXPECT_EQ(nullptr, interpreter->variables()); } TEST(Generator, algebraicEqnComputedVarOnRhs)