Skip to content
105 changes: 63 additions & 42 deletions src/analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2583,60 +2583,63 @@ void Analyser::AnalyserImpl::analyseModel(const ModelPtr &model)
}
}

if (mAnalyser->errorCount() != 0) {
auto hasUnderconstrainedVariables = std::any_of(mInternalVariables.begin(), mInternalVariables.end(), [](const auto &iv) {
switch (iv->mType) {
case AnalyserInternalVariable::Type::UNKNOWN:
case AnalyserInternalVariable::Type::SHOULD_BE_STATE:
return true;
default:
return false;
}
});
auto hasOverconstrainedVariables = std::any_of(mInternalVariables.begin(), mInternalVariables.end(), [](const auto &iv) {
return iv->mType == AnalyserInternalVariable::Type::OVERCONSTRAINED;
});
// Make sure that all our states have an equation that computes their rate.
// Note #1: this may not be the case when the rates of states are defined in terms of one another (e.g.,
// dx/dt = 1 + dy/dt and dy/dt = 2 - dx/dt), in which case those states are considered as underconstrained.
// This should change the day we rely on a symbolic math library to compute the rates of states that are
// defined in terms of one another.
// Note #2: this check is done even if some errors have already been reported, so that the type of our model
// reflects all of its issues (e.g., a model with both overconstrained variables and states without a rate
// equation is unsuitably constrained, not just overconstrained).

if (hasUnderconstrainedVariables) {
if (hasOverconstrainedVariables) {
mAnalyserModel->mPimpl->mType = AnalyserModel::Type::UNSUITABLY_CONSTRAINED;
} else {
mAnalyserModel->mPimpl->mType = AnalyserModel::Type::UNDERCONSTRAINED;
}
} else {
mAnalyserModel->mPimpl->mType = AnalyserModel::Type::OVERCONSTRAINED;
}
for (const auto &internalVariable : mInternalVariables) {
if ((internalVariable->mType == AnalyserInternalVariable::Type::STATE)
&& std::none_of(mInternalEquations.begin(), mInternalEquations.end(), [&](const auto &ie) {
return std::find(ie->mUnknownVariables.begin(), ie->mUnknownVariables.end(), internalVariable) != ie->mUnknownVariables.end();
})) {
internalVariable->mType = AnalyserInternalVariable::Type::UNDERCONSTRAINED;

return;
addInvalidVariableIssue(internalVariable, Issue::ReferenceRule::ANALYSER_VARIABLE_UNDERCONSTRAINED);
Comment thread
agarny marked this conversation as resolved.
Resolved
}
}

// Keep track of whether our variables are valid.
// Note: if they are not, then we still want to check our NLA systems (see below), so that the type of our model
// reflects all of its issues (e.g., a model with both states without a rate equation and an overconstrained
// NLA system is unsuitably constrained, not just underconstrained).

auto hasInvalidVariables = mAnalyser->errorCount() != 0;

// Make sure that variables that are initialised using another variable are
// not initialised using an algebraic variable.
// Note: this only makes sense if our variables are valid.

for (const auto &internalVariable : mInternalVariables) {
if ((internalVariable->mInitialisingVariable != nullptr)
&& !isCellMLReal(internalVariable->mInitialisingVariable->initialValue())) {
auto initialisingInternalVariable = Analyser::AnalyserImpl::internalVariable(owningComponent(internalVariable->mInitialisingVariable)->variable(internalVariable->mInitialisingVariable->initialValue()));
if (!hasInvalidVariables) {
for (const auto &internalVariable : mInternalVariables) {
if ((internalVariable->mInitialisingVariable != nullptr)
&& !isCellMLReal(internalVariable->mInitialisingVariable->initialValue())) {
auto initialisingInternalVariable = Analyser::AnalyserImpl::internalVariable(owningComponent(internalVariable->mInitialisingVariable)->variable(internalVariable->mInitialisingVariable->initialValue()));

if (initialisingInternalVariable->mType == AnalyserInternalVariable::Type::ALGEBRAIC_VARIABLE) {
auto issue = Issue::IssueImpl::create();
if (initialisingInternalVariable->mType == AnalyserInternalVariable::Type::ALGEBRAIC_VARIABLE) {
auto issue = Issue::IssueImpl::create();

issue->mPimpl->setDescription("Variable '" + internalVariable->mVariable->name()
+ "' in component '" + owningComponent(internalVariable->mVariable)->name()
+ "' is initialised using variable '" + initialisingInternalVariable->mVariable->name()
+ "', which is an algebraic variable. Only a reference to a constant, a computed constant, a state variable, or a computable non-linear algebraic variable is allowed.");
issue->mPimpl->setReferenceRule(Issue::ReferenceRule::ANALYSER_VARIABLE_INITIALISED_USING_ALGEBRAIC_VARIABLE);
issue->mPimpl->mItem->mPimpl->setVariable(internalVariable->mVariable);
issue->mPimpl->setDescription("Variable '" + internalVariable->mVariable->name()
+ "' in component '" + owningComponent(internalVariable->mVariable)->name()
+ "' is initialised using variable '" + initialisingInternalVariable->mVariable->name()
+ "', which is an algebraic variable. Only a reference to a constant, a computed constant, a state variable, or a computable non-linear algebraic variable is allowed.");
issue->mPimpl->setReferenceRule(Issue::ReferenceRule::ANALYSER_VARIABLE_INITIALISED_USING_ALGEBRAIC_VARIABLE);
issue->mPimpl->mItem->mPimpl->setVariable(internalVariable->mVariable);

addIssue(issue);
addIssue(issue);
}
}
}
}

if (mAnalyser->errorCount() != 0) {
mAnalyserModel->mPimpl->mType = AnalyserModel::Type::INVALID;
if (mAnalyser->errorCount() != 0) {
mAnalyserModel->mPimpl->mType = AnalyserModel::Type::INVALID;

return;
return;
}
}

// Make sure that our equations are valid.
Expand Down Expand Up @@ -2801,9 +2804,27 @@ void Analyser::AnalyserImpl::analyseModel(const ModelPtr &model)
}
}

// Make sure that our model is valid.
// Note: we determine the type of our model based on the type of all of our variables, so that it reflects all of
// its issues, be they related to our variables or to our NLA systems.

if (mAnalyser->errorCount() != 0) {
if (!underconstrainedVariables.empty()) {
if (!overconstrainedVariables.empty()) {
auto hasUnderconstrainedVariables = std::any_of(mInternalVariables.begin(), mInternalVariables.end(), [](const auto &iv) {
switch (iv->mType) {
case AnalyserInternalVariable::Type::UNKNOWN:
case AnalyserInternalVariable::Type::SHOULD_BE_STATE:
case AnalyserInternalVariable::Type::UNDERCONSTRAINED:
return true;
default:
return false;
}
});
auto hasOverconstrainedVariables = std::any_of(mInternalVariables.begin(), mInternalVariables.end(), [](const auto &iv) {
return iv->mType == AnalyserInternalVariable::Type::OVERCONSTRAINED;
});

if (hasUnderconstrainedVariables) {
if (hasOverconstrainedVariables) {
mAnalyserModel->mPimpl->mType = AnalyserModel::Type::UNSUITABLY_CONSTRAINED;
} else {
mAnalyserModel->mPimpl->mType = AnalyserModel::Type::UNDERCONSTRAINED;
Expand Down
106 changes: 106 additions & 0 deletions tests/analyser/analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,32 @@ TEST(Analyser, underconstrained)
EXPECT_EQ(libcellml::AnalyserModel::Type::UNDERCONSTRAINED, analyser->analyserModel()->type());
}

TEST(Analyser, underconstrainedCoupledRates)
{
auto parser = libcellml::Parser::create();
auto model = parser->parseModel(fileContents("analyser/underconstrained_coupled_rates.cellml"));

EXPECT_EQ(size_t(0), parser->issueCount());

const std::vector<std::string> expectedIssues = {
"Variable 'y' in component 'my_component' is underconstrained.",
"Variable 'x' in component 'my_component' is underconstrained.",
};

auto analyser = libcellml::Analyser::create();

analyser->analyseModel(model);

EXPECT_EQ_ISSUES_CELLMLELEMENTTYPES_LEVELS_REFERENCERULES_URLS(expectedIssues,
expectedCellmlElementTypes(expectedIssues.size(), libcellml::CellmlElementType::VARIABLE),
expectedLevels(expectedIssues.size(), libcellml::Issue::Level::ERROR),
expectedReferenceRules(expectedIssues.size(), libcellml::Issue::ReferenceRule::ANALYSER_VARIABLE_UNDERCONSTRAINED),
expectedUrls(expectedIssues.size(), "https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_UNDERCONSTRAINED"),
analyser);

EXPECT_EQ(libcellml::AnalyserModel::Type::UNDERCONSTRAINED, analyser->analyserModel()->type());
}

TEST(Analyser, overconstrained)
{
auto parser = libcellml::Parser::create();
Expand Down Expand Up @@ -405,6 +431,86 @@ TEST(Analyser, unsuitablyConstrained)
EXPECT_EQ(libcellml::AnalyserModel::Type::UNSUITABLY_CONSTRAINED, analyser->analyserModel()->type());
}

TEST(Analyser, unsuitablyConstrainedCoupledRates)
{
auto parser = libcellml::Parser::create();
auto model = parser->parseModel(fileContents("analyser/unsuitably_constrained_coupled_rates.cellml"));

EXPECT_EQ(size_t(0), parser->issueCount());

const std::vector<std::string> expectedIssues = {
"Variable 'z' in component 'my_component' is overconstrained.",
"Variable 'y' in component 'my_component' is underconstrained.",
"Variable 'x' in component 'my_component' is underconstrained.",
};
const std::vector<libcellml::Issue::ReferenceRule> expectedReferenceRules = {
libcellml::Issue::ReferenceRule::ANALYSER_VARIABLE_OVERCONSTRAINED,
libcellml::Issue::ReferenceRule::ANALYSER_VARIABLE_UNDERCONSTRAINED,
libcellml::Issue::ReferenceRule::ANALYSER_VARIABLE_UNDERCONSTRAINED,
};
const std::vector<std::string> expectedUrls = {
"https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_OVERCONSTRAINED",
"https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_UNDERCONSTRAINED",
"https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_UNDERCONSTRAINED",
};

auto analyser = libcellml::Analyser::create();

analyser->analyseModel(model);

EXPECT_EQ_ISSUES_CELLMLELEMENTTYPES_LEVELS_REFERENCERULES_URLS(expectedIssues,
expectedCellmlElementTypes(expectedIssues.size(), libcellml::CellmlElementType::VARIABLE),
expectedLevels(expectedIssues.size(), libcellml::Issue::Level::ERROR),
expectedReferenceRules,
expectedUrls,
analyser);

EXPECT_EQ(libcellml::AnalyserModel::Type::UNSUITABLY_CONSTRAINED, analyser->analyserModel()->type());
}

TEST(Analyser, unsuitablyConstrainedCoupledRatesAndNlaSystem)
{
auto parser = libcellml::Parser::create();
auto model = parser->parseModel(fileContents("analyser/unsuitably_constrained_coupled_rates_and_nla_system.cellml"));

EXPECT_EQ(size_t(0), parser->issueCount());

const std::vector<std::string> expectedIssues = {
"Variable 'y' in component 'my_component' is underconstrained.",
"Variable 'x' in component 'my_component' is underconstrained.",
"Variable 'z' in component 'my_algebraic_system' is overconstrained.",
"Variable 'y' in component 'my_algebraic_system' is overconstrained.",
"Variable 'x' in component 'my_algebraic_system' is overconstrained.",
};
const std::vector<libcellml::Issue::ReferenceRule> expectedReferenceRules = {
libcellml::Issue::ReferenceRule::ANALYSER_VARIABLE_UNDERCONSTRAINED,
libcellml::Issue::ReferenceRule::ANALYSER_VARIABLE_UNDERCONSTRAINED,
libcellml::Issue::ReferenceRule::ANALYSER_VARIABLE_OVERCONSTRAINED,
libcellml::Issue::ReferenceRule::ANALYSER_VARIABLE_OVERCONSTRAINED,
libcellml::Issue::ReferenceRule::ANALYSER_VARIABLE_OVERCONSTRAINED,
};
const std::vector<std::string> expectedUrls = {
"https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_UNDERCONSTRAINED",
"https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_UNDERCONSTRAINED",
"https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_OVERCONSTRAINED",
"https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_OVERCONSTRAINED",
"https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_OVERCONSTRAINED",
};

auto analyser = libcellml::Analyser::create();

analyser->analyseModel(model);

EXPECT_EQ_ISSUES_CELLMLELEMENTTYPES_LEVELS_REFERENCERULES_URLS(expectedIssues,
expectedCellmlElementTypes(expectedIssues.size(), libcellml::CellmlElementType::VARIABLE),
expectedLevels(expectedIssues.size(), libcellml::Issue::Level::ERROR),
expectedReferenceRules,
expectedUrls,
analyser);

EXPECT_EQ(libcellml::AnalyserModel::Type::UNSUITABLY_CONSTRAINED, analyser->analyserModel()->type());
}

TEST(Analyser, addSameExternalVariableAsVariable)
{
auto parser = libcellml::Parser::create();
Expand Down
55 changes: 55 additions & 0 deletions tests/resources/analyser/underconstrained_coupled_rates.cellml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version='1.0' encoding='UTF-8'?>
<model name="my_model" xmlns="http://www.cellml.org/cellml/2.0#" xmlns:cellml="http://www.cellml.org/cellml/2.0#">
<units name="per_second">
<unit units="second" exponent="-1"/>
</units>
<component name="my_component">
<variable name="t" units="second"/>
<variable name="x" units="dimensionless" initial_value="1"/>
<variable name="y" units="dimensionless" initial_value="2"/>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<eq/>
<apply>
<diff/>
<bvar>
<ci>t</ci>
</bvar>
<ci>x</ci>
</apply>
<apply>
<plus/>
<cn cellml:units="per_second">1</cn>
<apply>
<diff/>
<bvar>
<ci>t</ci>
</bvar>
<ci>y</ci>
</apply>
</apply>
</apply>
<apply>
<eq/>
<apply>
<diff/>
<bvar>
<ci>t</ci>
</bvar>
<ci>y</ci>
</apply>
<apply>
<minus/>
<cn cellml:units="per_second">2</cn>
<apply>
<diff/>
<bvar>
<ci>t</ci>
</bvar>
<ci>x</ci>
</apply>
</apply>
</apply>
</math>
</component>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version='1.0' encoding='UTF-8'?>
<model name="my_model" xmlns="http://www.cellml.org/cellml/2.0#" xmlns:cellml="http://www.cellml.org/cellml/2.0#">
<units name="per_second">
<unit units="second" exponent="-1"/>
</units>
<component name="my_component">
<variable name="t" units="second"/>
<variable name="x" units="dimensionless" initial_value="1"/>
<variable name="y" units="dimensionless" initial_value="2"/>
<variable name="z" units="dimensionless"/>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<eq/>
<apply>
<diff/>
<bvar>
<ci>t</ci>
</bvar>
<ci>x</ci>
</apply>
<apply>
<plus/>
<cn cellml:units="per_second">1</cn>
<apply>
<diff/>
<bvar>
<ci>t</ci>
</bvar>
<ci>y</ci>
</apply>
</apply>
</apply>
<apply>
<eq/>
<apply>
<diff/>
<bvar>
<ci>t</ci>
</bvar>
<ci>y</ci>
</apply>
<apply>
<minus/>
<cn cellml:units="per_second">2</cn>
<apply>
<diff/>
<bvar>
<ci>t</ci>
</bvar>
<ci>x</ci>
</apply>
</apply>
</apply>
<apply>
<eq/>
<ci>z</ci>
<cn cellml:units="dimensionless">1</cn>
</apply>
<apply>
<eq/>
<ci>z</ci>
<cn cellml:units="dimensionless">3</cn>
</apply>
</math>
</component>
</model>
Loading
Loading