Skip to content

Commit

Permalink
AnalyserModel: keep track of the constants, computed constants, and e…
Browse files Browse the repository at this point in the history
…xternal variables.
  • Loading branch information
agarny committed Aug 9, 2024
1 parent c6ba0e2 commit 3f1e632
Show file tree
Hide file tree
Showing 173 changed files with 964 additions and 1,096 deletions.
25 changes: 23 additions & 2 deletions src/analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3134,10 +3134,31 @@ void Analyser::AnalyserImpl::analyseModel(const ModelPtr &model)
aiv2avMappings.emplace(internalVariable, variable);
v2avMappings.emplace(internalVariable->mVariable, variable);

if (type == AnalyserVariable::Type::STATE) {
switch (type) {
case AnalyserVariable::Type::STATE:
mModel->mPimpl->mStates.push_back(variable);
} else {

break;
case AnalyserVariable::Type::CONSTANT:
mModel->mPimpl->mConstants.push_back(variable);

break;
case AnalyserVariable::Type::COMPUTED_CONSTANT:
mModel->mPimpl->mComputedConstants.push_back(variable);

break;
case AnalyserVariable::Type::ALGEBRAIC:
mModel->mPimpl->mAlgebraic.push_back(variable);

break;
case AnalyserVariable::Type::EXTERNAL:
mModel->mPimpl->mExternals.push_back(variable);

break;
default: // AnalyserVariable::Type::VARIABLE_OF_INTEGRATION.
// This is the variable of integration, so skip it.

break;
}
}

Expand Down
15 changes: 4 additions & 11 deletions src/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,7 @@ AnalyserVariablePtr Generator::GeneratorImpl::analyserVariable(const VariablePtr
res = doAnalyserVariable(variable, mModel->states());

if (res == nullptr) {
res = doAnalyserVariable(variable, mModel->constants());
}

if (res == nullptr) {
res = doAnalyserVariable(variable, mModel->computedConstants());
}

if (res == nullptr) {
res = doAnalyserVariable(variable, mModel->algebraic());
res = doAnalyserVariable(variable, variables(mModel));
}
}

Expand Down Expand Up @@ -357,8 +349,9 @@ void Generator::GeneratorImpl::addStateAndVariableCountCode(bool interface)
"[ALGEBRAIC_COUNT]", std::to_string(mModel->algebraicCount()));
}

if ((interface && !mProfile->interfaceExternalCountString().empty())
|| (!interface && !mProfile->implementationExternalCountString().empty())) {
if ((mModel->externalCount() != 0)
&& ((interface && !mProfile->interfaceExternalCountString().empty())
|| (!interface && !mProfile->implementationExternalCountString().empty()))) {
code += interface ?
mProfile->interfaceExternalCountString() :
replace(mProfile->implementationExternalCountString(),
Expand Down
6 changes: 6 additions & 0 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,12 @@ std::vector<AnalyserVariablePtr> variables(const AnalyserModelPtr &model)
res.insert(res.end(), algebraic.begin(), algebraic.end());
}

auto externals = model->externals();

if (!externals.empty()) {
res.insert(res.end(), externals.begin(), externals.end());
}

return res;
}

Expand Down
16 changes: 8 additions & 8 deletions tests/bindings/javascript/analysermodel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ describe("Analyser Model tests", () => {
expect(am.state(2).variable().name()).toBe("m")
});
test('Checking Analyser Model constants related API.', () => {
expect(am.constantCount()).toBe(0)
expect(am.constants().size()).toBe(0)
expect(am.constant(2)).toBeNull()
expect(am.constantCount()).toBe(5)
expect(am.constants().size()).toBe(5)
expect(am.constant(2).variable().name()).toBe("g_L")
});
test('Checking Analyser Model computed constants related API.', () => {
expect(am.computedConstantCount()).toBe(0)
expect(am.computedConstants().size()).toBe(0)
expect(am.computedConstant(2)).toBeNull()
expect(am.computedConstantCount()).toBe(3)
expect(am.computedConstants().size()).toBe(3)
expect(am.computedConstant(2).variable().name()).toBe("E_K")
});
test('Checking Analyser Model algebraic variables related API.', () => {
expect(am.algebraicCount()).toBe(18)
expect(am.algebraicVariables().size()).toBe(18)
expect(am.algebraicCount()).toBe(10)
expect(am.algebraicVariables().size()).toBe(10)
expect(am.algebraicVariable(2).variable().name()).toBe("i_K")
});
test('Checking Analyser Model need* API.', () => {
Expand Down
22 changes: 11 additions & 11 deletions tests/bindings/javascript/analyservariable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ describe("Analyser Variable tests", () => {

am = a.model()

expect(am.constantCount()).toBe(0)
expect(am.computedConstantCount()).toBe(0)
expect(am.algebraicCount()).toBe(18)
expect(am.constantCount()).toBe(5)
expect(am.computedConstantCount()).toBe(3)
expect(am.algebraicCount()).toBe(10)
});
test('Checking Analyser Variable type.', () => {
const av = am.algebraicVariable(0)
Expand All @@ -48,26 +48,26 @@ describe("Analyser Variable tests", () => {
});
test('Checking Analyser Variable index.', () => {
const av = am.algebraicVariable(7)
expect(av.index()).toBe(2)
expect(av.index()).toBe(7)
});
test('Checking Analyser Variable initialising variable.', () => {
const av = am.algebraicVariable(15)
expect(av.initialisingVariable().name()).toBe("g_K")
const av = am.constant(3)
expect(av.initialisingVariable().name()).toBe("g_Na")
});
test('Checking Analyser Variable variable.', () => {
const av = am.algebraicVariable(10)
expect(av.variable().name()).toBe("alpha_m")
const av = am.algebraicVariable(3)
expect(av.variable().name()).toBe("i_Na")
});
test('Checking Analyser Equation equationCount.', () => {
const av = am.algebraicVariable(14)
const av = am.computedConstant(1)
expect(av.equationCount()).toBe(1)
});
test('Checking Analyser Variable equations.', () => {
const av = am.algebraicVariable(14)
const av = am.computedConstant(1)
expect(av.equations().size()).toBe(1)
});
test('Checking Analyser Variable equation.', () => {
const av = am.algebraicVariable(14)
const av = am.computedConstant(1)
expect(av.equation(0).type().value).toBe(libcellml.AnalyserEquation.Type.VARIABLE_BASED_CONSTANT.value)
});
})
4 changes: 2 additions & 2 deletions tests/bindings/javascript/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ describe("Generator tests", () => {
g.setModel(a.model())

const interface_lines = g.interfaceCode().split('\n')
expect(interface_lines.length).toBe(43)
expect(interface_lines.length).toBe(42)

const implementation_lines = g.implementationCode().split('\n')
expect(implementation_lines.length).toBe(70)
expect(implementation_lines.length).toBe(69)

const equation_line_1 = libcellml.Generator.equationCode(a.model().equation(0).ast())
expect(equation_line_1.length).toBe(14)
Expand Down
18 changes: 9 additions & 9 deletions tests/bindings/python/test_analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ def test_coverage(self):
self.assertIsNotNone(am.states())
self.assertIsNotNone(am.state(3))

self.assertEqual(0, am.constantCount())
self.assertEqual(5, am.constantCount())
self.assertIsNotNone(am.constants())
self.assertIsNone(am.constant(3))
self.assertIsNotNone(am.constant(3))
self.assertEqual(0, am.computedConstantCount())
self.assertIsNotNone(am.computedConstants())
self.assertIsNone(am.computedConstant(3))
self.assertEqual(17, am.algebraicCount())
self.assertEqual(12, am.algebraicCount())
self.assertIsNotNone(am.algebraic())
self.assertIsNotNone(am.algebraic(3))

Expand Down Expand Up @@ -160,15 +160,15 @@ def test_coverage(self):

av = am.algebraic(3)

self.assertEqual(AnalyserVariable.Type.CONSTANT, av.type())
self.assertEqual("constant", AnalyserVariable.typeAsString(av.type()))
self.assertEqual("constant", AnalyserVariable_typeAsString(av.type()))
self.assertEqual(0, av.index())
self.assertIsNotNone(av.initialisingVariable())
self.assertEqual(AnalyserVariable.Type.ALGEBRAIC, av.type())
self.assertEqual("algebraic", AnalyserVariable.typeAsString(av.type()))
self.assertEqual("algebraic", AnalyserVariable_typeAsString(av.type()))
self.assertEqual(3, av.index())
self.assertIsNone(av.initialisingVariable())
self.assertIsNotNone(av.variable())
self.assertEqual(1, av.equationCount())
self.assertIsNotNone(av.equations())
self.assertIsNone(av.equation(0))
self.assertIsNotNone(av.equation(0))

# Ensure coverage for AnalyserEquation.

Expand Down
30 changes: 7 additions & 23 deletions tests/coverage/coverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,9 @@ TEST(Coverage, generator)
EXPECT_EQ("dae", libcellml::AnalyserModel::typeAsString(analyserModel->type()));

EXPECT_EQ(size_t(1), analyserModel->stateCount());
EXPECT_EQ(size_t(0), analyserModel->constantCount());
EXPECT_EQ(size_t(0), analyserModel->computedConstantCount());
EXPECT_EQ(size_t(209), analyserModel->algebraicCount());
EXPECT_EQ(size_t(7), analyserModel->constantCount());
EXPECT_EQ(size_t(200), analyserModel->computedConstantCount());
EXPECT_EQ(size_t(2), analyserModel->algebraicCount());
EXPECT_EQ(size_t(203), analyserModel->equationCount());

EXPECT_NE(nullptr, analyserModel->voi());
Expand All @@ -608,9 +608,9 @@ TEST(Coverage, generator)
EXPECT_NE(size_t(0), analyserModel->state(0)->equations().size());
EXPECT_NE(nullptr, analyserModel->state(0)->equation(0));
EXPECT_EQ(nullptr, analyserModel->state(analyserModel->stateCount()));
EXPECT_EQ(nullptr, analyserModel->constant(0));
EXPECT_NE(nullptr, analyserModel->constant(0));
EXPECT_EQ(nullptr, analyserModel->constant(analyserModel->constantCount()));
EXPECT_EQ(nullptr, analyserModel->computedConstant(0));
EXPECT_NE(nullptr, analyserModel->computedConstant(0));
EXPECT_EQ(nullptr, analyserModel->computedConstant(analyserModel->computedConstantCount()));
EXPECT_NE(nullptr, analyserModel->algebraic(0));
EXPECT_EQ(nullptr, analyserModel->algebraic(analyserModel->algebraicCount()));
Expand Down Expand Up @@ -652,27 +652,11 @@ TEST(Coverage, generator)
}

for (size_t i = 0; i < analyserModel->constantCount(); ++i) {
if ((i == 1) || (i == 2) || (i == 6) || (i == 18) || (i == 179) || (i == 180) || (i == 182) || (i == 205) || (i == 206)) {
EXPECT_TRUE(analyserModel->constant(i)->initialisingVariable() != nullptr);
}
}

for (size_t i = 0; i < analyserModel->computedConstantCount(); ++i) {
if ((i == 1) || (i == 2) || (i == 6) || (i == 18) || (i == 179) || (i == 180) || (i == 182) || (i == 205) || (i == 206)) {
EXPECT_TRUE(analyserModel->computedConstant(i)->initialisingVariable() != nullptr);
}
EXPECT_NE(nullptr, analyserModel->constant(i)->initialisingVariable());
}

for (size_t i = 0; i < analyserModel->algebraicCount(); ++i) {
if ((i == 1) || (i == 2) || (i == 6) || (i == 18) || (i == 179) || (i == 180) || (i == 182) || (i == 205) || (i == 206)) {
EXPECT_TRUE(analyserModel->algebraic(i)->initialisingVariable() != nullptr);
}
}

for (size_t i = 0; i < analyserModel->externalCount(); ++i) {
if ((i == 1) || (i == 2) || (i == 6) || (i == 18) || (i == 179) || (i == 180) || (i == 182) || (i == 205) || (i == 206)) {
EXPECT_TRUE(analyserModel->external(i)->initialisingVariable() != nullptr);
}
EXPECT_NE(nullptr, analyserModel->algebraic(i)->initialisingVariable());
}

EXPECT_EQ(nullptr, generator->model());
Expand Down
25 changes: 12 additions & 13 deletions tests/resources/coverage/generator/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ const char VERSION[] = "0.6.0";
const char LIBCELLML_VERSION[] = "0.5.0";

const size_t STATE_COUNT = 1;
const size_t CONSTANT_COUNT = 0;
const size_t COMPUTED_CONSTANT_COUNT = 0;
const size_t ALGEBRAIC_COUNT = 209;
const size_t EXTERNAL_COUNT = 0;
const size_t CONSTANT_COUNT = 7;
const size_t COMPUTED_CONSTANT_COUNT = 200;
const size_t ALGEBRAIC_COUNT = 2;

const VariableInfo VOI_INFO = {"t", "second", "my_component", VARIABLE_OF_INTEGRATION};

Expand All @@ -21,13 +20,17 @@ const VariableInfo STATE_INFO[] = {
};

const VariableInfo VARIABLE_INFO[] = {
{"eqnEq", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"m", "dimensionless", "my_component", CONSTANT},
{"n", "dimensionless", "my_component", CONSTANT},
{"o", "dimensionless", "my_component", CONSTANT},
{"p", "dimensionless", "my_component", CONSTANT},
{"q", "dimensionless", "my_component", CONSTANT},
{"r", "dimensionless", "my_component", CONSTANT},
{"s", "dimensionless", "my_component", CONSTANT},
{"eqnEq", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnEqCoverageParentheses", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnNeq", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnNeqCoverageParentheses", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"o", "dimensionless", "my_component", CONSTANT},
{"eqnLt", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnLtCoverageParentheses", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnLeq", "dimensionless", "my_component", COMPUTED_CONSTANT},
Expand All @@ -39,7 +42,6 @@ const VariableInfo VARIABLE_INFO[] = {
{"eqnAnd", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnAndMultiple", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnAndParentheses", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"p", "dimensionless", "my_component", CONSTANT},
{"eqnAndParenthesesLeftPlusWith", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnAndParenthesesLeftPlusWithout", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnAndParenthesesLeftMinusWith", "dimensionless", "my_component", COMPUTED_CONSTANT},
Expand Down Expand Up @@ -200,10 +202,7 @@ const VariableInfo VARIABLE_INFO[] = {
{"eqnPiecewisePiece", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnPiecewisePieceOtherwise", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnPiecewisePiecePiecePiece", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"q", "dimensionless", "my_component", CONSTANT},
{"r", "dimensionless", "my_component", CONSTANT},
{"eqnPiecewisePiecePiecePieceOtherwise", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"s", "dimensionless", "my_component", CONSTANT},
{"eqnWithPiecewise", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnCnInteger", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnCnDouble", "dimensionless", "my_component", COMPUTED_CONSTANT},
Expand All @@ -226,10 +225,10 @@ const VariableInfo VARIABLE_INFO[] = {
{"eqnCoverageForPowerOperator", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnCoverageForRootOperator", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnCoverageForMinusUnary", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnNlaVariable1", "dimensionless", "my_component", ALGEBRAIC},
{"eqnNlaVariable2", "dimensionless", "my_component", ALGEBRAIC},
{"eqnComputedConstant1", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnComputedConstant2", "dimensionless", "my_component", COMPUTED_CONSTANT}
{"eqnComputedConstant2", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnNlaVariable1", "dimensionless", "my_component", ALGEBRAIC},
{"eqnNlaVariable2", "dimensionless", "my_component", ALGEBRAIC}
};

double xor(double x, double y)
Expand Down
1 change: 0 additions & 1 deletion tests/resources/coverage/generator/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ extern const size_t STATE_COUNT;
extern const size_t CONSTANT_COUNT;
extern const size_t COMPUTED_CONSTANT_COUNT;
extern const size_t ALGEBRAIC_COUNT;
extern const size_t EXTERNAL_COUNT;

typedef enum {
VARIABLE_OF_INTEGRATION,
Expand Down
25 changes: 12 additions & 13 deletions tests/resources/coverage/generator/model.modified.profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ const char VERSION[] = "0.6.0.post0";
const char LIBCELLML_VERSION[] = "0.5.0";

const size_t STATE_COUNT = 1;
const size_t CONSTANT_COUNT = 0;
const size_t COMPUTED_CONSTANT_COUNT = 0;
const size_t ALGEBRAIC_COUNT = 209;
const size_t EXTERNAL_COUNT = 0;
const size_t CONSTANT_COUNT = 7;
const size_t COMPUTED_CONSTANT_COUNT = 200;
const size_t ALGEBRAIC_COUNT = 2;

const VariableInfo VOI_INFO = {"t", "second", "my_component", VARIABLE_OF_INTEGRATION};

Expand All @@ -21,13 +20,17 @@ const VariableInfo STATE_INFO[] = {
};

const VariableInfo VARIABLE_INFO[] = {
{"eqnEq", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"m", "dimensionless", "my_component", CONSTANT},
{"n", "dimensionless", "my_component", CONSTANT},
{"o", "dimensionless", "my_component", CONSTANT},
{"p", "dimensionless", "my_component", CONSTANT},
{"q", "dimensionless", "my_component", CONSTANT},
{"r", "dimensionless", "my_component", CONSTANT},
{"s", "dimensionless", "my_component", CONSTANT},
{"eqnEq", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnEqCoverageParentheses", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnNeq", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnNeqCoverageParentheses", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"o", "dimensionless", "my_component", CONSTANT},
{"eqnLt", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnLtCoverageParentheses", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnLeq", "dimensionless", "my_component", COMPUTED_CONSTANT},
Expand All @@ -39,7 +42,6 @@ const VariableInfo VARIABLE_INFO[] = {
{"eqnAnd", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnAndMultiple", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnAndParentheses", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"p", "dimensionless", "my_component", CONSTANT},
{"eqnAndParenthesesLeftPlusWith", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnAndParenthesesLeftPlusWithout", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnAndParenthesesLeftMinusWith", "dimensionless", "my_component", COMPUTED_CONSTANT},
Expand Down Expand Up @@ -200,10 +202,7 @@ const VariableInfo VARIABLE_INFO[] = {
{"eqnPiecewisePiece", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnPiecewisePieceOtherwise", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnPiecewisePiecePiecePiece", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"q", "dimensionless", "my_component", CONSTANT},
{"r", "dimensionless", "my_component", CONSTANT},
{"eqnPiecewisePiecePiecePieceOtherwise", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"s", "dimensionless", "my_component", CONSTANT},
{"eqnWithPiecewise", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnCnInteger", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnCnDouble", "dimensionless", "my_component", COMPUTED_CONSTANT},
Expand All @@ -226,10 +225,10 @@ const VariableInfo VARIABLE_INFO[] = {
{"eqnCoverageForPowerOperator", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnCoverageForRootOperator", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnCoverageForMinusUnary", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnNlaVariable1", "dimensionless", "my_component", ALGEBRAIC},
{"eqnNlaVariable2", "dimensionless", "my_component", ALGEBRAIC},
{"eqnComputedConstant1", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnComputedConstant2", "dimensionless", "my_component", COMPUTED_CONSTANT}
{"eqnComputedConstant2", "dimensionless", "my_component", COMPUTED_CONSTANT},
{"eqnNlaVariable1", "dimensionless", "my_component", ALGEBRAIC},
{"eqnNlaVariable2", "dimensionless", "my_component", ALGEBRAIC}
};

double xor(double x, double y)
Expand Down
Loading

0 comments on commit 3f1e632

Please sign in to comment.