Skip to content

Commit

Permalink
Interpreter: added methods to retrieve the VOI, states, rates, and va…
Browse files Browse the repository at this point in the history
…riables.
  • Loading branch information
agarny committed Apr 8, 2024
1 parent b84e782 commit 1f42143
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/api/libcellml/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 12 additions & 0 deletions src/bindings/interface/interpreter.i
Original file line number Diff line number Diff line change
Expand Up @@ -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.";

Expand Down
28 changes: 28 additions & 0 deletions src/bindings/javascript/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>("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<void>("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<void>("set", view);

return res;
}))
.function("initialiseVariables", &libcellml::Interpreter::initialiseVariables)
.function("computeComputedConstants", &libcellml::Interpreter::computeComputedConstants)
.function("computeRates", &libcellml::Interpreter::computeRates)
Expand Down
20 changes: 20 additions & 0 deletions src/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
Expand Down
5 changes: 5 additions & 0 deletions src/interpreter_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions tests/bindings/python/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions tests/generator/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1f42143

Please sign in to comment.