Skip to content

Commit 6195eff

Browse files
committed
Interpreter: allocate the different arrays.
1 parent b67ea7d commit 6195eff

File tree

9 files changed

+59
-132
lines changed

9 files changed

+59
-132
lines changed

src/api/libcellml/interpreter.h

+8-33
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ limitations under the License.
1919
#include "libcellml/exportdefinitions.h"
2020
#include "libcellml/types.h"
2121

22+
#include <vector>
23+
2224
namespace libcellml {
2325

2426
/**
@@ -75,59 +77,32 @@ class LIBCELLML_EXPORT Interpreter
7577
*/
7678
double voi();
7779

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-
8780
/**
8881
* @brief Get the model's states.
8982
*
9083
* Return the model's states. If the model doesn't have any states then @c nullptr is returned.
9184
*
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.
85+
* @return The model's states as a @c std::vector of @c double.
10286
*/
103-
size_t rateCount();
87+
std::vector<double> states();
10488

10589
/**
10690
* @brief Get the model's rates.
10791
*
10892
* Return the model's rates. If the model doesn't have any rates then @c nullptr is returned.
10993
*
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.
94+
* @return The model's rates as a @c std::vector of @c double.
12095
*/
121-
size_t variableCount();
96+
std::vector<double> rates();
12297

12398
/**
12499
* @brief Get the model's variables.
125100
*
126101
* Return the model's variables.
127102
*
128-
* @return The model's variables as an array of @c double.
103+
* @return The model's variables as a @c std::vector of @c double.
129104
*/
130-
double *variables();
105+
std::vector<double> variables();
131106

132107
/**
133108
* @brief Initialise the model's variables.

src/bindings/interface/interpreter.i

+3-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define LIBCELLML_EXPORT
44

55
%include <std_string.i>
6+
%include <std_vector.i>
67

78
%import "analysermodel.i"
89
%import "createconstructor.i"
@@ -19,21 +20,12 @@
1920
%feature("docstring") libcellml::Interpreter::voi
2021
"Returns the value of the model's variable of integration.";
2122

22-
%feature("docstring") libcellml::Interpreter::stateCount
23-
"Returns the number of states in the model.";
24-
2523
%feature("docstring") libcellml::Interpreter::states
2624
"Returns the model's states.";
2725

28-
%feature("docstring") libcellml::Interpreter::rateCount
29-
"Returns the number of rates in the model.";
30-
3126
%feature("docstring") libcellml::Interpreter::rates
3227
"Returns the model's rates.";
3328

34-
%feature("docstring") libcellml::Interpreter::variableCount
35-
"Returns the number of variables in the model.";
36-
3729
%feature("docstring") libcellml::Interpreter::variables
3830
"Returns the model's variables.";
3931

@@ -53,6 +45,8 @@
5345
#include "libcellml/interpreter.h"
5446
%}
5547

48+
%template(DoubleVector) std::vector<double>;
49+
5650
%pythoncode %{
5751
# libCellML generated wrapper code starts here.
5852
%}

src/bindings/javascript/interpreter.cpp

+3-33
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,9 @@ EMSCRIPTEN_BINDINGS(libcellml_interpreter)
2727
.function("model", &libcellml::Interpreter::model)
2828
.function("setModel", &libcellml::Interpreter::setModel)
2929
.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-
}))
30+
.function("states", &libcellml::Interpreter::states)
31+
.function("rates", &libcellml::Interpreter::rates)
32+
.function("variables", &libcellml::Interpreter::variables)
6333
.function("initialiseVariables", &libcellml::Interpreter::initialiseVariables)
6434
.function("computeComputedConstants", &libcellml::Interpreter::computeComputedConstants)
6535
.function("computeRates", &libcellml::Interpreter::computeRates)

src/bindings/javascript/types.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ using namespace emscripten;
2222

2323
EMSCRIPTEN_BINDINGS(libcellml_types)
2424
{
25+
register_vector<double>("VectorDouble");
2526
register_vector<std::string>("VectorString");
2627
register_vector<libcellml::AnyCellmlElementPtr>("VectorAnyCellmlElementPtr");
2728
register_vector<libcellml::VariablePtr>("VectorVariablePtr");

src/interpreter.cpp

+25-19
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,31 @@ limitations under the License.
1616

1717
#include "libcellml/interpreter.h"
1818

19+
#include "libcellml/analysermodel.h"
20+
1921
#include "interpreter_p.h"
2022

2123
namespace libcellml {
2224

25+
void Interpreter::InterpreterImpl::setModel(const AnalyserModelPtr &model)
26+
{
27+
mModel = model;
28+
29+
if (mModel != nullptr) {
30+
mStates.resize(mModel->stateCount());
31+
mRates.resize(mModel->stateCount());
32+
mVariables.resize(mModel->variableCount());
33+
34+
mVoi = 0.0;
35+
36+
static const auto NaN = std::numeric_limits<double>::quiet_NaN();
37+
38+
std::fill(mStates.begin(), mStates.end(), NaN);
39+
std::fill(mRates.begin(), mRates.end(), NaN);
40+
std::fill(mVariables.begin(), mVariables.end(), NaN);
41+
}
42+
}
43+
2344
Interpreter::Interpreter()
2445
: mPimpl(new InterpreterImpl())
2546
{
@@ -42,40 +63,25 @@ AnalyserModelPtr Interpreter::model()
4263

4364
void Interpreter::setModel(const AnalyserModelPtr &model)
4465
{
45-
mPimpl->mModel = model;
66+
mPimpl->setModel(model);
4667
}
4768

4869
double Interpreter::voi()
4970
{
5071
return mPimpl->mVoi;
5172
}
5273

53-
size_t Interpreter::stateCount()
54-
{
55-
return mPimpl->mStateCount;
56-
}
57-
58-
double *Interpreter::states()
74+
std::vector<double> Interpreter::states()
5975
{
6076
return mPimpl->mStates;
6177
}
6278

63-
size_t Interpreter::rateCount()
64-
{
65-
return mPimpl->mRateCount;
66-
}
67-
68-
double *Interpreter::rates()
79+
std::vector<double> Interpreter::rates()
6980
{
7081
return mPimpl->mRates;
7182
}
7283

73-
size_t Interpreter::variableCount()
74-
{
75-
return mPimpl->mVariableCount;
76-
}
77-
78-
double *Interpreter::variables()
84+
std::vector<double> Interpreter::variables()
7985
{
8086
return mPimpl->mVariables;
8187
}

src/interpreter_p.h

+4-8
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,11 @@ struct Interpreter::InterpreterImpl
3232
AnalyserModelPtr mModel;
3333

3434
double mVoi = 0.0;
35+
std::vector<double> mStates;
36+
std::vector<double> mRates;
37+
std::vector<double> mVariables;
3538

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;
39+
void setModel(const AnalyserModelPtr &model);
4440
};
4541

4642
} // namespace libcellml

tests/bindings/javascript/interpreter.test.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ limitations under the License.
1515
*/
1616

1717
const loadLibCellML = require('libcellml.js/libcellml.common')
18-
const { basicModel } = require('./resources')
18+
const { hhSquidAxon1952 } = require('./resources')
1919

2020
let libcellml = null
2121

@@ -27,7 +27,7 @@ describe("Interpreter tests", () => {
2727
const i = new libcellml.Interpreter()
2828
const p = new libcellml.Parser(true)
2929

30-
m = p.parseModel(basicModel)
30+
m = p.parseModel(hhSquidAxon1952)
3131
a = new libcellml.Analyser()
3232

3333
a.analyseModel(m)
@@ -40,14 +40,9 @@ describe("Interpreter tests", () => {
4040

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

43-
expect(i.stateCount()).toBe(0)
44-
expect(i.states()).toHaveLength(0)
45-
46-
expect(i.rateCount()).toBe(0)
47-
expect(i.rates()).toHaveLength(0)
48-
49-
expect(i.variableCount()).toBe(0)
50-
expect(i.variables()).toHaveLength(0)
43+
expect(i.states()).toHaveLength(4)
44+
expect(i.rates()).toHaveLength(4)
45+
expect(i.variables()).toHaveLength(18)
5146

5247
i.initialiseVariables()
5348
i.computeComputedConstants()

tests/bindings/python/test_interpreter.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ def test_create_destroy(self):
1212
x = Interpreter()
1313
del x
1414

15-
def test_algebraic_eqn_computed_var_on_rhs(self):
15+
def test_hodgkin_huxley_squid_axon_model_1952(self):
1616
from libcellml import Analyser
1717
from libcellml import AnalyserModel
1818
from libcellml import Interpreter
1919
from libcellml import Parser
2020
from test_resources import file_contents
2121

2222
p = Parser()
23-
m = p.parseModel(file_contents('generator/algebraic_eqn_computed_var_on_rhs/model.cellml'))
24-
23+
m = p.parseModel(file_contents('generator/hodgkin_huxley_squid_axon_model_1952/model.cellml'))
2524
a = Analyser()
25+
2626
a.analyseModel(m)
2727

2828
am = a.model()
2929

30-
self.assertEqual(AnalyserModel.Type.ALGEBRAIC, am.type())
30+
self.assertEqual(AnalyserModel.Type.ODE, am.type())
3131

3232
i = Interpreter()
3333

@@ -39,14 +39,9 @@ def test_algebraic_eqn_computed_var_on_rhs(self):
3939

4040
self.assertEqual(0.0, i.voi())
4141

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())
42+
self.assertEqual(4, len(i.states()))
43+
self.assertEqual(4, len(i.rates()))
44+
self.assertEqual(18, len(i.variables()))
5045

5146
i.initialiseVariables()
5247
i.computeComputedConstants()

tests/generator/generator.cpp

+3-8
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,9 @@ TEST(Generator, emptyModel)
4545

4646
EXPECT_EQ(0.0, interpreter->voi());
4747

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());
48+
EXPECT_EQ(size_t(0), interpreter->states().size());
49+
EXPECT_EQ(size_t(0), interpreter->rates().size());
50+
EXPECT_EQ(size_t(0), interpreter->variables().size());
5651
}
5752

5853
TEST(Generator, algebraicEqnComputedVarOnRhs)

0 commit comments

Comments
 (0)