Skip to content

Commit

Permalink
Interpreter: allocate the different arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Apr 8, 2024
1 parent b67ea7d commit 6195eff
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 132 deletions.
41 changes: 8 additions & 33 deletions src/api/libcellml/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ limitations under the License.
#include "libcellml/exportdefinitions.h"
#include "libcellml/types.h"

#include <vector>

namespace libcellml {

/**
Expand Down Expand Up @@ -75,59 +77,32 @@ class LIBCELLML_EXPORT Interpreter
*/
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.
* @return The model's states as a @c std::vector of @c double.
*/
size_t rateCount();
std::vector<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 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.
* @return The model's rates as a @c std::vector of @c double.
*/
size_t variableCount();
std::vector<double> rates();

/**
* @brief Get the model's variables.
*
* Return the model's variables.
*
* @return The model's variables as an array of @c double.
* @return The model's variables as a @c std::vector of @c double.
*/
double *variables();
std::vector<double> variables();

/**
* @brief Initialise the model's variables.
Expand Down
12 changes: 3 additions & 9 deletions src/bindings/interface/interpreter.i
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define LIBCELLML_EXPORT

%include <std_string.i>
%include <std_vector.i>

%import "analysermodel.i"
%import "createconstructor.i"
Expand All @@ -19,21 +20,12 @@
%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.";

Expand All @@ -53,6 +45,8 @@
#include "libcellml/interpreter.h"
%}

%template(DoubleVector) std::vector<double>;

%pythoncode %{
# libCellML generated wrapper code starts here.
%}
Expand Down
36 changes: 3 additions & 33 deletions src/bindings/javascript/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,9 @@ EMSCRIPTEN_BINDINGS(libcellml_interpreter)
.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("states", &libcellml::Interpreter::states)
.function("rates", &libcellml::Interpreter::rates)
.function("variables", &libcellml::Interpreter::variables)
.function("initialiseVariables", &libcellml::Interpreter::initialiseVariables)
.function("computeComputedConstants", &libcellml::Interpreter::computeComputedConstants)
.function("computeRates", &libcellml::Interpreter::computeRates)
Expand Down
1 change: 1 addition & 0 deletions src/bindings/javascript/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ using namespace emscripten;

EMSCRIPTEN_BINDINGS(libcellml_types)
{
register_vector<double>("VectorDouble");
register_vector<std::string>("VectorString");
register_vector<libcellml::AnyCellmlElementPtr>("VectorAnyCellmlElementPtr");
register_vector<libcellml::VariablePtr>("VectorVariablePtr");
Expand Down
44 changes: 25 additions & 19 deletions src/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,31 @@ limitations under the License.

#include "libcellml/interpreter.h"

#include "libcellml/analysermodel.h"

#include "interpreter_p.h"

namespace libcellml {

void Interpreter::InterpreterImpl::setModel(const AnalyserModelPtr &model)
{
mModel = model;

if (mModel != nullptr) {
mStates.resize(mModel->stateCount());
mRates.resize(mModel->stateCount());
mVariables.resize(mModel->variableCount());

mVoi = 0.0;

static const auto NaN = std::numeric_limits<double>::quiet_NaN();

std::fill(mStates.begin(), mStates.end(), NaN);
std::fill(mRates.begin(), mRates.end(), NaN);
std::fill(mVariables.begin(), mVariables.end(), NaN);
}
}

Interpreter::Interpreter()
: mPimpl(new InterpreterImpl())
{
Expand All @@ -42,40 +63,25 @@ AnalyserModelPtr Interpreter::model()

void Interpreter::setModel(const AnalyserModelPtr &model)
{
mPimpl->mModel = model;
mPimpl->setModel(model);
}

double Interpreter::voi()
{
return mPimpl->mVoi;
}

size_t Interpreter::stateCount()
{
return mPimpl->mStateCount;
}

double *Interpreter::states()
std::vector<double> Interpreter::states()
{
return mPimpl->mStates;
}

size_t Interpreter::rateCount()
{
return mPimpl->mRateCount;
}

double *Interpreter::rates()
std::vector<double> Interpreter::rates()
{
return mPimpl->mRates;
}

size_t Interpreter::variableCount()
{
return mPimpl->mVariableCount;
}

double *Interpreter::variables()
std::vector<double> Interpreter::variables()
{
return mPimpl->mVariables;
}
Expand Down
12 changes: 4 additions & 8 deletions src/interpreter_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ struct Interpreter::InterpreterImpl
AnalyserModelPtr mModel;

double mVoi = 0.0;
std::vector<double> mStates;
std::vector<double> mRates;
std::vector<double> mVariables;

size_t mStateCount = 0;
double *mStates = nullptr;

size_t mRateCount = 0;
double *mRates = nullptr;

size_t mVariableCount = 0;
double *mVariables = nullptr;
void setModel(const AnalyserModelPtr &model);
};

} // namespace libcellml
15 changes: 5 additions & 10 deletions tests/bindings/javascript/interpreter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

const loadLibCellML = require('libcellml.js/libcellml.common')
const { basicModel } = require('./resources')
const { hhSquidAxon1952 } = require('./resources')

let libcellml = null

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

m = p.parseModel(basicModel)
m = p.parseModel(hhSquidAxon1952)
a = new libcellml.Analyser()

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

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)
expect(i.states()).toHaveLength(4)
expect(i.rates()).toHaveLength(4)
expect(i.variables()).toHaveLength(18)

i.initialiseVariables()
i.computeComputedConstants()
Expand Down
19 changes: 7 additions & 12 deletions tests/bindings/python/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ def test_create_destroy(self):
x = Interpreter()
del x

def test_algebraic_eqn_computed_var_on_rhs(self):
def test_hodgkin_huxley_squid_axon_model_1952(self):
from libcellml import Analyser
from libcellml import AnalyserModel
from libcellml import Interpreter
from libcellml import Parser
from test_resources import file_contents

p = Parser()
m = p.parseModel(file_contents('generator/algebraic_eqn_computed_var_on_rhs/model.cellml'))

m = p.parseModel(file_contents('generator/hodgkin_huxley_squid_axon_model_1952/model.cellml'))
a = Analyser()

a.analyseModel(m)

am = a.model()

self.assertEqual(AnalyserModel.Type.ALGEBRAIC, am.type())
self.assertEqual(AnalyserModel.Type.ODE, am.type())

i = Interpreter()

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

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

i.initialiseVariables()
i.computeComputedConstants()
Expand Down
11 changes: 3 additions & 8 deletions tests/generator/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,9 @@ TEST(Generator, emptyModel)

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());
EXPECT_EQ(size_t(0), interpreter->states().size());
EXPECT_EQ(size_t(0), interpreter->rates().size());
EXPECT_EQ(size_t(0), interpreter->variables().size());
}

TEST(Generator, algebraicEqnComputedVarOnRhs)
Expand Down

0 comments on commit 6195eff

Please sign in to comment.