diff --git a/src/analyser.cpp b/src/analyser.cpp index 9e194495b9..1a7ce6732a 100644 --- a/src/analyser.cpp +++ b/src/analyser.cpp @@ -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); + } } + // 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. @@ -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; diff --git a/tests/analyser/analyser.cpp b/tests/analyser/analyser.cpp index ecc9db714b..75d66bb16a 100644 --- a/tests/analyser/analyser.cpp +++ b/tests/analyser/analyser.cpp @@ -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 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(); @@ -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 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 expectedReferenceRules = { + libcellml::Issue::ReferenceRule::ANALYSER_VARIABLE_OVERCONSTRAINED, + libcellml::Issue::ReferenceRule::ANALYSER_VARIABLE_UNDERCONSTRAINED, + libcellml::Issue::ReferenceRule::ANALYSER_VARIABLE_UNDERCONSTRAINED, + }; + const std::vector 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 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 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 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(); diff --git a/tests/resources/analyser/underconstrained_coupled_rates.cellml b/tests/resources/analyser/underconstrained_coupled_rates.cellml new file mode 100644 index 0000000000..fee51ff943 --- /dev/null +++ b/tests/resources/analyser/underconstrained_coupled_rates.cellml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + t + + x + + + + 1 + + + + t + + y + + + + + + + + + t + + y + + + + 2 + + + + t + + x + + + + + + diff --git a/tests/resources/analyser/unsuitably_constrained_coupled_rates.cellml b/tests/resources/analyser/unsuitably_constrained_coupled_rates.cellml new file mode 100644 index 0000000000..e10a84bc37 --- /dev/null +++ b/tests/resources/analyser/unsuitably_constrained_coupled_rates.cellml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + t + + x + + + + 1 + + + + t + + y + + + + + + + + + t + + y + + + + 2 + + + + t + + x + + + + + + z + 1 + + + + z + 3 + + + + diff --git a/tests/resources/analyser/unsuitably_constrained_coupled_rates_and_nla_system.cellml b/tests/resources/analyser/unsuitably_constrained_coupled_rates_and_nla_system.cellml new file mode 100644 index 0000000000..423dca411c --- /dev/null +++ b/tests/resources/analyser/unsuitably_constrained_coupled_rates_and_nla_system.cellml @@ -0,0 +1,134 @@ + + + + + + + + + + + + + + + + t + + x + + + + 1 + + + + t + + y + + + + + + + + + t + + y + + + + 2 + + + + t + + x + + + + + + + + + + + + + + + x + y + z + + 2 + + + + + + + + 6 + x + + + + -4 + y + + + + 5 + z + + + 31 + + + + + + + + 5 + x + + + + 2 + y + + + + 2 + z + + + 31 + + + + + + + + -2 + x + + y + + + 3 + z + + + -5 + + + +