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 d6f5b87 commit b67ea7d
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/api/libcellml/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
21 changes: 21 additions & 0 deletions src/bindings/interface/interpreter.i
Original file line number Diff line number Diff line change
Expand Up @@ -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.";

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

return res;
}))
.function("initialiseVariables", &libcellml::Interpreter::initialiseVariables)
.function("computeComputedConstants", &libcellml::Interpreter::computeComputedConstants)
.function("computeRates", &libcellml::Interpreter::computeRates)
Expand Down
35 changes: 35 additions & 0 deletions src/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
Expand Down
11 changes: 11 additions & 0 deletions src/interpreter_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions tests/bindings/javascript/interpreter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ describe("Interpreter tests", () => {

expect(i.model()).toBeDefined()

expect(i.voi()).toBe(0.0)

expect(i.stateCount()).toBe(0)
expect(i.states()).toHaveLength(0)

expect(i.rateCount()).toBe(0)
expect(i.rates()).toHaveLength(0)

expect(i.variableCount()).toBe(0)
expect(i.variables()).toHaveLength(0)

i.initialiseVariables()
i.computeComputedConstants()
i.computeRates()
Expand Down
11 changes: 11 additions & 0 deletions tests/bindings/python/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions tests/generator/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b67ea7d

Please sign in to comment.