Skip to content

Commit 9d955ff

Browse files
committed
Interpreter: added methods to retrieve the VOI, states, rates, and variables.
1 parent d6f5b87 commit 9d955ff

File tree

8 files changed

+202
-0
lines changed

8 files changed

+202
-0
lines changed

src/api/libcellml/interpreter.h

+64
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,70 @@ class LIBCELLML_EXPORT Interpreter
6565
*/
6666
void setModel(const AnalyserModelPtr &model);
6767

68+
/**
69+
* @brief Get the value of the model's variable of integration.
70+
*
71+
* Return the value of the model's variable of integration. If no variable of integration is needed to compute the
72+
* model then 0.0 is returned.
73+
*
74+
* @return The value of the variable of integration as a @c double.
75+
*/
76+
double voi();
77+
78+
/**
79+
* @brief Get the number of states in the model.
80+
*
81+
* Return the number of states in the model. If the model doesn't have any states then 0 is returned.
82+
*
83+
* @return The number of states in the model as a @c size_t.
84+
*/
85+
size_t stateCount();
86+
87+
/**
88+
* @brief Get the model's states.
89+
*
90+
* Return the model's states. If the model doesn't have any states then @c nullptr is returned.
91+
*
92+
* @return The model's states as an array of @c double.
93+
*/
94+
double *states();
95+
96+
/**
97+
* @brief Get the number of rates in the model.
98+
*
99+
* Return the number of rates in the model. If the model doesn't have any rates then 0 is returned.
100+
*
101+
* @return The number of rates in the model as a @c size_t.
102+
*/
103+
size_t rateCount();
104+
105+
/**
106+
* @brief Get the model's rates.
107+
*
108+
* Return the model's rates. If the model doesn't have any rates then @c nullptr is returned.
109+
*
110+
* @return The model's rates as an array of @c double.
111+
*/
112+
double *rates();
113+
114+
/**
115+
* @brief Get the number of variables in the model.
116+
*
117+
* Return the number of variables in the model. If the model doesn't have any variables then 0 is returned.
118+
*
119+
* @return The number of variables in the model as a @c size_t.
120+
*/
121+
size_t variableCount();
122+
123+
/**
124+
* @brief Get the model's variables.
125+
*
126+
* Return the model's variables.
127+
*
128+
* @return The model's variables as an array of @c double.
129+
*/
130+
double *variables();
131+
68132
/**
69133
* @brief Initialise the model's variables.
70134
*

src/bindings/interface/interpreter.i

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@
1616
%feature("docstring") libcellml::Interpreter::setModel
1717
"Sets the model to interpret.";
1818

19+
%feature("docstring") libcellml::Interpreter::voi
20+
"Returns the value of the model's variable of integration.";
21+
22+
%feature("docstring") libcellml::Interpreter::stateCount
23+
"Returns the number of states in the model.";
24+
25+
%feature("docstring") libcellml::Interpreter::states
26+
"Returns the model's states.";
27+
28+
%feature("docstring") libcellml::Interpreter::rateCount
29+
"Returns the number of rates in the model.";
30+
31+
%feature("docstring") libcellml::Interpreter::rates
32+
"Returns the model's rates.";
33+
34+
%feature("docstring") libcellml::Interpreter::variableCount
35+
"Returns the number of variables in the model.";
36+
37+
%feature("docstring") libcellml::Interpreter::variables
38+
"Returns the model's variables.";
39+
1940
%feature("docstring") libcellml::Interpreter::initialiseVariables
2041
"Initialises the model's variables.";
2142

src/bindings/javascript/interpreter.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,40 @@ EMSCRIPTEN_BINDINGS(libcellml_interpreter)
2626
.smart_ptr_constructor("Interpreter", &libcellml::Interpreter::create)
2727
.function("model", &libcellml::Interpreter::model)
2828
.function("setModel", &libcellml::Interpreter::setModel)
29+
.function("voi", &libcellml::Interpreter::voi)
30+
.function("stateCount", &libcellml::Interpreter::stateCount)
31+
.function("states", emscripten::optional_override([](libcellml::InterpreterPtr &interpreter) {
32+
auto states = interpreter->states();
33+
auto stateCount = interpreter->stateCount();
34+
auto view = emscripten::typed_memory_view(stateCount, states);
35+
auto res = emscripten::val::global("Uint8Array").new_(stateCount);
36+
37+
res.call<void>("set", view);
38+
39+
return res;
40+
}))
41+
.function("rateCount", &libcellml::Interpreter::rateCount)
42+
.function("rates", emscripten::optional_override([](libcellml::InterpreterPtr &interpreter) {
43+
auto rates = interpreter->rates();
44+
auto rateCount = interpreter->rateCount();
45+
auto view = emscripten::typed_memory_view(rateCount, rates);
46+
auto res = emscripten::val::global("Uint8Array").new_(rateCount);
47+
48+
res.call<void>("set", view);
49+
50+
return res;
51+
}))
52+
.function("variableCount", &libcellml::Interpreter::variableCount)
53+
.function("variables", emscripten::optional_override([](libcellml::InterpreterPtr &interpreter) {
54+
auto variables = interpreter->variables();
55+
auto variableCount = interpreter->variableCount();
56+
auto view = emscripten::typed_memory_view(variableCount, variables);
57+
auto res = emscripten::val::global("Uint8Array").new_(variableCount);
58+
59+
res.call<void>("set", view);
60+
61+
return res;
62+
}))
2963
.function("initialiseVariables", &libcellml::Interpreter::initialiseVariables)
3064
.function("computeComputedConstants", &libcellml::Interpreter::computeComputedConstants)
3165
.function("computeRates", &libcellml::Interpreter::computeRates)

src/interpreter.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,41 @@ void Interpreter::setModel(const AnalyserModelPtr &model)
4545
mPimpl->mModel = model;
4646
}
4747

48+
double Interpreter::voi()
49+
{
50+
return mPimpl->mVoi;
51+
}
52+
53+
size_t Interpreter::stateCount()
54+
{
55+
return mPimpl->mStateCount;
56+
}
57+
58+
double *Interpreter::states()
59+
{
60+
return mPimpl->mStates;
61+
}
62+
63+
size_t Interpreter::rateCount()
64+
{
65+
return mPimpl->mRateCount;
66+
}
67+
68+
double *Interpreter::rates()
69+
{
70+
return mPimpl->mRates;
71+
}
72+
73+
size_t Interpreter::variableCount()
74+
{
75+
return mPimpl->mVariableCount;
76+
}
77+
78+
double *Interpreter::variables()
79+
{
80+
return mPimpl->mVariables;
81+
}
82+
4883
void Interpreter::initialiseVariables()
4984
{
5085
}

src/interpreter_p.h

+11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ std::string generateDoubleCode(const std::string &value);
3030
struct Interpreter::InterpreterImpl
3131
{
3232
AnalyserModelPtr mModel;
33+
34+
double mVoi = 0.0;
35+
36+
size_t mStateCount = 0;
37+
double *mStates = nullptr;
38+
39+
size_t mRateCount = 0;
40+
double *mRates = nullptr;
41+
42+
size_t mVariableCount = 0;
43+
double *mVariables = nullptr;
3344
};
3445

3546
} // namespace libcellml

tests/bindings/javascript/interpreter.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ describe("Interpreter tests", () => {
3838

3939
expect(i.model()).toBeDefined()
4040

41+
expect(i.voi()).toBe(0.0)
42+
43+
expect(i.stateCount()).toBe(0)
44+
expect(i.states()).toBe(null)
45+
46+
expect(i.rateCount()).toBe(0)
47+
expect(i.rates()).toBe(null)
48+
49+
expect(i.variableCount()).toBe(0)
50+
expect(i.variables()).toBe(null)
51+
4152
i.initialiseVariables()
4253
i.computeComputedConstants()
4354
i.computeRates()

tests/bindings/python/test_interpreter.py

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ def test_algebraic_eqn_computed_var_on_rhs(self):
3737

3838
self.assertIsNotNone(i.model())
3939

40+
self.assertEqual(0.0, i.voi())
41+
42+
self.assertEqual(0, i.stateCount())
43+
self.assertIsNone(i.states())
44+
45+
self.assertEqual(0, i.rateCount())
46+
self.assertIsNone(i.rates())
47+
48+
self.assertEqual(0, i.variableCount())
49+
self.assertIsNone(i.variables())
50+
4051
i.initialiseVariables()
4152
i.computeComputedConstants()
4253
i.computeRates()

tests/generator/generator.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ TEST(Generator, emptyModel)
3838

3939
EXPECT_EQ(EMPTY_STRING, generator->interfaceCode());
4040
EXPECT_EQ(EMPTY_STRING, generator->implementationCode());
41+
42+
auto interpreter = libcellml::Interpreter::create();
43+
44+
interpreter->setModel(analyserModel);
45+
46+
EXPECT_EQ(0.0, interpreter->voi());
47+
48+
EXPECT_EQ(size_t(0), interpreter->stateCount());
49+
EXPECT_EQ(nullptr, interpreter->states());
50+
51+
EXPECT_EQ(size_t(0), interpreter->rateCount());
52+
EXPECT_EQ(nullptr, interpreter->rates());
53+
54+
EXPECT_EQ(size_t(0), interpreter->variableCount());
55+
EXPECT_EQ(nullptr, interpreter->variables());
4156
}
4257

4358
TEST(Generator, algebraicEqnComputedVarOnRhs)

0 commit comments

Comments
 (0)