diff --git a/src/analyser.cpp b/src/analyser.cpp index 9e194495b9..a36c2191d1 100644 --- a/src/analyser.cpp +++ b/src/analyser.cpp @@ -1029,8 +1029,8 @@ void Analyser::AnalyserImpl::analyseEquationAst(const AnalyserEquationAstPtr &as // We have found the first occurrence of our variable of // integration, but now we must ensure that it (or any of // its equivalent variables) is not initialised. - - auto isVoiInitialised = false; + // Note: we keep track of our variable of integration even if it is initialised, so that we only + // check it once (and therefore report any issue only once). for (const auto &voiEquivalentVariable : equivalentVariables(voi)) { if (!voiEquivalentVariable->initialValue().empty()) { @@ -1043,17 +1043,13 @@ void Analyser::AnalyserImpl::analyseEquationAst(const AnalyserEquationAstPtr &as issue->mPimpl->mItem->mPimpl->setVariable(voiEquivalentVariable); addIssue(issue); - - isVoiInitialised = true; } } - if (!isVoiInitialised) { - mAnalyserModel->mPimpl->mVoi = AnalyserVariable::AnalyserVariableImpl::create(); + mAnalyserModel->mPimpl->mVoi = AnalyserVariable::AnalyserVariableImpl::create(); - mAnalyserModel->mPimpl->mVoi->mPimpl->populate(AnalyserVariable::Type::VARIABLE_OF_INTEGRATION, - 0, nullptr, voi, mAnalyserModel, {}); - } + mAnalyserModel->mPimpl->mVoi->mPimpl->populate(AnalyserVariable::Type::VARIABLE_OF_INTEGRATION, + 0, nullptr, voi, mAnalyserModel, {}); } } while (voi == nullptr); } else { diff --git a/tests/analyser/analyser.cpp b/tests/analyser/analyser.cpp index ecc9db714b..39882d9fc8 100644 --- a/tests/analyser/analyser.cpp +++ b/tests/analyser/analyser.cpp @@ -134,6 +134,32 @@ TEST(Analyser, initialisedVariableOfIntegrationInNonFirstComponent) EXPECT_EQ(libcellml::AnalyserModel::Type::INVALID, analyser->analyserModel()->type()); } +TEST(Analyser, initialisedVariableOfIntegrationWithSeveralOdes) +{ + auto parser = libcellml::Parser::create(); + auto model = parser->parseModel(fileContents("analyser/initialised_variable_of_integration_several_odes.cellml")); + + EXPECT_EQ(size_t(0), parser->issueCount()); + + const std::vector expectedIssues = { + "Variable 'time' in component 'my_component' cannot be both a variable of integration and initialised.", + }; + + 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_VOI_INITIALISED), + expectedUrls(expectedIssues.size(), "https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VOI_INITIALISED"), + analyser); + + EXPECT_EQ(libcellml::AnalyserModel::Type::INVALID, analyser->analyserModel()->type()); + EXPECT_EQ(nullptr, analyser->analyserModel()->voi()); +} + TEST(Analyser, twoVariablesOfIntegration) { auto parser = libcellml::Parser::create(); diff --git a/tests/resources/analyser/initialised_variable_of_integration_several_odes.cellml b/tests/resources/analyser/initialised_variable_of_integration_several_odes.cellml new file mode 100644 index 0000000000..e5f4884d8c --- /dev/null +++ b/tests/resources/analyser/initialised_variable_of_integration_several_odes.cellml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + time + + x + + 1 + + + + + + + time + + y + + 2 + + + +