From 3ee1f779df9ca577ab286b79ec13bff24b520221 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Tue, 24 Nov 2026 18:43:58 +1300 Subject: [PATCH 1/5] Analyser: treat a unary min/max as its argument. --- src/analyser.cpp | 24 ++++- tests/generator/generatorunaryminmax.cpp | 90 +++++++++++++++++++ tests/generator/tests.cmake | 1 + .../generator/unary_min_max/example_01.cellml | 31 +++++++ .../generator/unary_min_max/example_02.cellml | 31 +++++++ .../generator/unary_min_max/example_03.cellml | 39 ++++++++ .../generator/unary_min_max/example_04.cellml | 39 ++++++++ .../generator/unary_min_max/example_05.cellml | 35 ++++++++ 8 files changed, 288 insertions(+), 2 deletions(-) create mode 100644 tests/generator/generatorunaryminmax.cpp create mode 100644 tests/resources/generator/unary_min_max/example_01.cellml create mode 100644 tests/resources/generator/unary_min_max/example_02.cellml create mode 100644 tests/resources/generator/unary_min_max/example_03.cellml create mode 100644 tests/resources/generator/unary_min_max/example_04.cellml create mode 100644 tests/resources/generator/unary_min_max/example_05.cellml diff --git a/src/analyser.cpp b/src/analyser.cpp index 58f1dfaf01..b8eaeb3eb8 100644 --- a/src/analyser.cpp +++ b/src/analyser.cpp @@ -522,6 +522,18 @@ void Analyser::AnalyserImpl::analyseNode(const XmlNodePtr &node, analyseNode(mathmlChildNode(node, 0), tempAst, astParent, component, equation); + // A unary "min" or "max" is equivalent to its argument, so replace it with its argument. + + if ((childCount == 2) + && ((tempAst->mPimpl->mType == AnalyserEquationAst::Type::MIN) + || (tempAst->mPimpl->mType == AnalyserEquationAst::Type::MAX))) { + tempAst = tempAst->mPimpl->mOwnedLeftChild; + + tempAst->mPimpl->mParent = astParent; + } + + // Update the AST to be the one we just created. + ast = tempAst; // Relational and logical operators. @@ -605,11 +617,19 @@ void Analyser::AnalyserImpl::analyseNode(const XmlNodePtr &node, } else if (node->isMathmlElement("min")) { ast->mPimpl->populate(AnalyserEquationAst::Type::MIN, astParent); - mAnalyserModel->mPimpl->mNeedMinFunction = true; + // Note: a unary "min" is replaced with its argument (see above), so it doesn't need our "min" function. + + if (mathmlChildCount(node->parent()) > 2) { + mAnalyserModel->mPimpl->mNeedMinFunction = true; + } } else if (node->isMathmlElement("max")) { ast->mPimpl->populate(AnalyserEquationAst::Type::MAX, astParent); - mAnalyserModel->mPimpl->mNeedMaxFunction = true; + // Note: a unary "max" is replaced with its argument (see above), so it doesn't need our "max" function. + + if (mathmlChildCount(node->parent()) > 2) { + mAnalyserModel->mPimpl->mNeedMaxFunction = true; + } } else if (node->isMathmlElement("rem")) { ast->mPimpl->populate(AnalyserEquationAst::Type::REM, astParent); diff --git a/tests/generator/generatorunaryminmax.cpp b/tests/generator/generatorunaryminmax.cpp new file mode 100644 index 0000000000..641178054a --- /dev/null +++ b/tests/generator/generatorunaryminmax.cpp @@ -0,0 +1,90 @@ +/* +Copyright libCellML Contributors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +#include "test_utils.h" + +#include "gtest/gtest.h" + +#include + +namespace { + +std::string implementationCode(const std::string &fileName, bool needMinFunction, bool needMaxFunction) +{ + auto parser = libcellml::Parser::create(); + auto model = parser->parseModel(fileContents(fileName)); + + EXPECT_NE(nullptr, model); + + auto analyser = libcellml::Analyser::create(); + + analyser->analyseModel(model); + + EXPECT_EQ(size_t(0), analyser->issueCount()); + + auto analyserModel = analyser->analyserModel(); + + EXPECT_EQ(needMinFunction, analyserModel->needMinFunction()); + EXPECT_EQ(needMaxFunction, analyserModel->needMaxFunction()); + + return libcellml::Generator::create()->implementationCode(analyserModel); +} + +} // namespace + +TEST(GeneratorUnaryMinMax, example01) +{ + // algebraicVariables[0] = min(voi); + + auto code = implementationCode("generator/unary_min_max/example_01.cellml", false, false); + + EXPECT_TRUE(code.find("algebraicVariables[0] = voi;\n") != std::string::npos); +} + +TEST(GeneratorUnaryMinMax, example02) +{ + // algebraicVariables[0] = max(voi); + + auto code = implementationCode("generator/unary_min_max/example_02.cellml", false, false); + + EXPECT_TRUE(code.find("algebraicVariables[0] = voi;\n") != std::string::npos); +} + +TEST(GeneratorUnaryMinMax, example03) +{ + // computedConstants[0] = 2.0*min(constants[1]+constants[0]); + + auto code = implementationCode("generator/unary_min_max/example_03.cellml", false, false); + + EXPECT_TRUE(code.find("computedConstants[0] = 2.0*(constants[1]+constants[0]);\n") != std::string::npos); +} + +TEST(GeneratorUnaryMinMax, example04) +{ + // computedConstants[0] = 2.0*max(constants[1]+constants[0]); + + auto code = implementationCode("generator/unary_min_max/example_04.cellml", false, false); + + EXPECT_TRUE(code.find("computedConstants[0] = 2.0*(constants[1]+constants[0]);\n") != std::string::npos); +} + +TEST(GeneratorUnaryMinMax, example05) +{ + // algebraicVariables[0] = max(min(voi), constants[0]); + + auto code = implementationCode("generator/unary_min_max/example_05.cellml", false, true); + + EXPECT_TRUE(code.find("algebraicVariables[0] = max(voi, constants[0]);\n") != std::string::npos); +} diff --git a/tests/generator/tests.cmake b/tests/generator/tests.cmake index 38e5dc0b37..f3caa31087 100644 --- a/tests/generator/tests.cmake +++ b/tests/generator/tests.cmake @@ -6,6 +6,7 @@ list(APPEND LIBCELLML_TESTS ${CURRENT_TEST}) set(${CURRENT_TEST}_SRCS ${CMAKE_CURRENT_LIST_DIR}/generator.cpp ${CMAKE_CURRENT_LIST_DIR}/generatorprofile.cpp + ${CMAKE_CURRENT_LIST_DIR}/generatorunaryminmax.cpp ${CMAKE_CURRENT_LIST_DIR}/generatorunaryminus.cpp ${CMAKE_CURRENT_LIST_DIR}/generatorvariabletracker.cpp ) diff --git a/tests/resources/generator/unary_min_max/example_01.cellml b/tests/resources/generator/unary_min_max/example_01.cellml new file mode 100644 index 0000000000..bce3f1b2dc --- /dev/null +++ b/tests/resources/generator/unary_min_max/example_01.cellml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + t + + x + + 1 + + + + y + + + t + + + + + diff --git a/tests/resources/generator/unary_min_max/example_02.cellml b/tests/resources/generator/unary_min_max/example_02.cellml new file mode 100644 index 0000000000..18c084eebe --- /dev/null +++ b/tests/resources/generator/unary_min_max/example_02.cellml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + t + + x + + 1 + + + + y + + + t + + + + + diff --git a/tests/resources/generator/unary_min_max/example_03.cellml b/tests/resources/generator/unary_min_max/example_03.cellml new file mode 100644 index 0000000000..971ef4c456 --- /dev/null +++ b/tests/resources/generator/unary_min_max/example_03.cellml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + t + + x + + 1 + + + + y + + + 2 + + + + + a + b + + + + + + + diff --git a/tests/resources/generator/unary_min_max/example_04.cellml b/tests/resources/generator/unary_min_max/example_04.cellml new file mode 100644 index 0000000000..6a21131f64 --- /dev/null +++ b/tests/resources/generator/unary_min_max/example_04.cellml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + t + + x + + 1 + + + + y + + + 2 + + + + + a + b + + + + + + + diff --git a/tests/resources/generator/unary_min_max/example_05.cellml b/tests/resources/generator/unary_min_max/example_05.cellml new file mode 100644 index 0000000000..21b63b5a26 --- /dev/null +++ b/tests/resources/generator/unary_min_max/example_05.cellml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + t + + x + + 1 + + + + y + + + + + t + + a + + + + + From e96e9b0680702dacffa4e5a7f3c414e97fcaaf76 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Tue, 24 Nov 2026 18:48:58 +1300 Subject: [PATCH 2/5] Validator: check the number of arguments of `min`, `max`, and `rem`. --- src/validator.cpp | 13 ++- .../resources/invalidminmaxremsiblings.cellml | 97 +++++++++++++++++++ tests/validator/validator.cpp | 23 +++++ 3 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 tests/resources/invalidminmaxremsiblings.cellml diff --git a/src/validator.cpp b/src/validator.cpp index bfc93e7eea..2c3be83c6c 100644 --- a/src/validator.cpp +++ b/src/validator.cpp @@ -2102,9 +2102,18 @@ void Validator::ValidatorImpl::validateMathMLElementsChildrenAndSiblings(const X hasOneMathmlSibling(parentNode, node, component) && isFirstMathmlSibling(parentNode, node, component); - } else if (node->isMathmlElement("min")) { - } else if (node->isMathmlElement("max")) { + } else if (node->isMathmlElement("min") + || node->isMathmlElement("max")) { + auto parentNode = node->parent(); + + hasAtLeastOneMathmlSibling(parentNode, node, component) + && isFirstMathmlSibling(parentNode, node, component); } else if (node->isMathmlElement("rem")) { + auto parentNode = node->parent(); + + hasTwoMathmlSiblings(parentNode, node, component) + && isFirstMathmlSibling(parentNode, node, component); + // Calculus elements. } else if (node->isMathmlElement("diff")) { diff --git a/tests/resources/invalidminmaxremsiblings.cellml b/tests/resources/invalidminmaxremsiblings.cellml new file mode 100644 index 0000000000..d7bd90acb9 --- /dev/null +++ b/tests/resources/invalidminmaxremsiblings.cellml @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + y1 + + + a + + + + + y2 + + + a + b + c + + + + + y3 + + + a + b + + + + + + y4 + + + + + + + y5 + + + + + + + y6 + + + + + + + + y7 + + + a + + + + + y8 + + + a + b + c + + + + + + y9 + + a + + + + + + diff --git a/tests/validator/validator.cpp b/tests/validator/validator.cpp index b3527f98db..875d3adf82 100644 --- a/tests/validator/validator.cpp +++ b/tests/validator/validator.cpp @@ -4006,3 +4006,26 @@ TEST(Validator, invalidMathMLElementsChildrenOrSiblings) EXPECT_EQ_ISSUES(expectedIssues, validator); } + +TEST(Validator, invalidMinMaxRemSiblings) +{ + const std::vector expectedIssues = { + "Math has a 'min' element without at least one MathML sibling.", + "Math has a 'max' element without at least one MathML sibling.", + "Math has a 'rem' element without exactly two MathML siblings.", + "Math has a 'rem' element without exactly two MathML siblings.", + "Math has a 'rem' element without exactly two MathML siblings.", + "Math has a 'min' element which is not the first MathML sibling.", + }; + + auto parser = libcellml::Parser::create(); + auto model = parser->parseModel(fileContents("invalidminmaxremsiblings.cellml")); + + EXPECT_EQ(size_t(0), parser->issueCount()); + + auto validator = libcellml::Validator::create(); + + validator->validateModel(model); + + EXPECT_EQ_ISSUES(expectedIssues, validator); +} From 3e137bbed6845b44cd4c0f4fba030de60e31c4fb Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Tue, 24 Nov 2026 19:10:48 +1300 Subject: [PATCH 3/5] Tests: cover a `rem` element which is not the first MathML sibling. --- tests/resources/invalidminmaxremsiblings.cellml | 12 +++++++++++- tests/validator/validator.cpp | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/resources/invalidminmaxremsiblings.cellml b/tests/resources/invalidminmaxremsiblings.cellml index d7bd90acb9..864c861609 100644 --- a/tests/resources/invalidminmaxremsiblings.cellml +++ b/tests/resources/invalidminmaxremsiblings.cellml @@ -13,6 +13,7 @@ + @@ -83,7 +84,7 @@ c - + y9 @@ -92,6 +93,15 @@ + + + y10 + + a + + b + + diff --git a/tests/validator/validator.cpp b/tests/validator/validator.cpp index 875d3adf82..283162b57d 100644 --- a/tests/validator/validator.cpp +++ b/tests/validator/validator.cpp @@ -4016,6 +4016,7 @@ TEST(Validator, invalidMinMaxRemSiblings) "Math has a 'rem' element without exactly two MathML siblings.", "Math has a 'rem' element without exactly two MathML siblings.", "Math has a 'min' element which is not the first MathML sibling.", + "Math has a 'rem' element which is not the first MathML sibling.", }; auto parser = libcellml::Parser::create(); From ea8b8adf686191aa6801ac3221ad7306404b8931 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Thu, 26 Nov 2026 15:04:02 +1300 Subject: [PATCH 4/5] Generator: slightly reworked our unary tests. --- ...ratorunaryminus.cpp => generatorunary.cpp} | 74 +++++++++++++++ tests/generator/generatorunaryminmax.cpp | 90 ------------------- tests/generator/tests.cmake | 3 +- 3 files changed, 75 insertions(+), 92 deletions(-) rename tests/generator/{generatorunaryminus.cpp => generatorunary.cpp} (69%) delete mode 100644 tests/generator/generatorunaryminmax.cpp diff --git a/tests/generator/generatorunaryminus.cpp b/tests/generator/generatorunary.cpp similarity index 69% rename from tests/generator/generatorunaryminus.cpp rename to tests/generator/generatorunary.cpp index 48e198a5b5..26d7f0e1c6 100644 --- a/tests/generator/generatorunaryminus.cpp +++ b/tests/generator/generatorunary.cpp @@ -19,6 +19,80 @@ limitations under the License. #include +namespace { + +std::string implementationCode(const std::string &fileName, bool needMaxFunction) +{ + auto parser = libcellml::Parser::create(); + auto model = parser->parseModel(fileContents(fileName)); + + EXPECT_NE(nullptr, model); + + auto analyser = libcellml::Analyser::create(); + + analyser->analyseModel(model); + + EXPECT_EQ(size_t(0), analyser->issueCount()); + + auto analyserModel = analyser->analyserModel(); + + EXPECT_FALSE(analyserModel->needMinFunction()); + EXPECT_EQ(needMaxFunction, analyserModel->needMaxFunction()); + + return libcellml::Generator::create()->implementationCode(analyserModel); +} + +} // namespace + +// Test cases for the generator of unary min and max functions. + +TEST(GeneratorUnaryMinMax, example01) +{ + // algebraicVariables[0] = min(voi); + + auto code = implementationCode("generator/unary_min_max/example_01.cellml", false); + + EXPECT_TRUE(code.find("algebraicVariables[0] = voi;\n") != std::string::npos); +} + +TEST(GeneratorUnaryMinMax, example02) +{ + // algebraicVariables[0] = max(voi); + + auto code = implementationCode("generator/unary_min_max/example_02.cellml", false); + + EXPECT_TRUE(code.find("algebraicVariables[0] = voi;\n") != std::string::npos); +} + +TEST(GeneratorUnaryMinMax, example03) +{ + // computedConstants[0] = 2.0*min(constants[1]+constants[0]); + + auto code = implementationCode("generator/unary_min_max/example_03.cellml", false); + + EXPECT_TRUE(code.find("computedConstants[0] = 2.0*(constants[1]+constants[0]);\n") != std::string::npos); +} + +TEST(GeneratorUnaryMinMax, example04) +{ + // computedConstants[0] = 2.0*max(constants[1]+constants[0]); + + auto code = implementationCode("generator/unary_min_max/example_04.cellml", false); + + EXPECT_TRUE(code.find("computedConstants[0] = 2.0*(constants[1]+constants[0]);\n") != std::string::npos); +} + +TEST(GeneratorUnaryMinMax, example05) +{ + // algebraicVariables[0] = max(min(voi), constants[0]); + + auto code = implementationCode("generator/unary_min_max/example_05.cellml", true); + + EXPECT_TRUE(code.find("algebraicVariables[0] = max(voi, constants[0]);\n") != std::string::npos); +} + +// Test cases for the generator of unary minus functions. + TEST(GeneratorUnaryMinus, example01) { // rates[0] = --1.0; diff --git a/tests/generator/generatorunaryminmax.cpp b/tests/generator/generatorunaryminmax.cpp deleted file mode 100644 index 641178054a..0000000000 --- a/tests/generator/generatorunaryminmax.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* -Copyright libCellML Contributors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ -#include "test_utils.h" - -#include "gtest/gtest.h" - -#include - -namespace { - -std::string implementationCode(const std::string &fileName, bool needMinFunction, bool needMaxFunction) -{ - auto parser = libcellml::Parser::create(); - auto model = parser->parseModel(fileContents(fileName)); - - EXPECT_NE(nullptr, model); - - auto analyser = libcellml::Analyser::create(); - - analyser->analyseModel(model); - - EXPECT_EQ(size_t(0), analyser->issueCount()); - - auto analyserModel = analyser->analyserModel(); - - EXPECT_EQ(needMinFunction, analyserModel->needMinFunction()); - EXPECT_EQ(needMaxFunction, analyserModel->needMaxFunction()); - - return libcellml::Generator::create()->implementationCode(analyserModel); -} - -} // namespace - -TEST(GeneratorUnaryMinMax, example01) -{ - // algebraicVariables[0] = min(voi); - - auto code = implementationCode("generator/unary_min_max/example_01.cellml", false, false); - - EXPECT_TRUE(code.find("algebraicVariables[0] = voi;\n") != std::string::npos); -} - -TEST(GeneratorUnaryMinMax, example02) -{ - // algebraicVariables[0] = max(voi); - - auto code = implementationCode("generator/unary_min_max/example_02.cellml", false, false); - - EXPECT_TRUE(code.find("algebraicVariables[0] = voi;\n") != std::string::npos); -} - -TEST(GeneratorUnaryMinMax, example03) -{ - // computedConstants[0] = 2.0*min(constants[1]+constants[0]); - - auto code = implementationCode("generator/unary_min_max/example_03.cellml", false, false); - - EXPECT_TRUE(code.find("computedConstants[0] = 2.0*(constants[1]+constants[0]);\n") != std::string::npos); -} - -TEST(GeneratorUnaryMinMax, example04) -{ - // computedConstants[0] = 2.0*max(constants[1]+constants[0]); - - auto code = implementationCode("generator/unary_min_max/example_04.cellml", false, false); - - EXPECT_TRUE(code.find("computedConstants[0] = 2.0*(constants[1]+constants[0]);\n") != std::string::npos); -} - -TEST(GeneratorUnaryMinMax, example05) -{ - // algebraicVariables[0] = max(min(voi), constants[0]); - - auto code = implementationCode("generator/unary_min_max/example_05.cellml", false, true); - - EXPECT_TRUE(code.find("algebraicVariables[0] = max(voi, constants[0]);\n") != std::string::npos); -} diff --git a/tests/generator/tests.cmake b/tests/generator/tests.cmake index f3caa31087..3c484fcd13 100644 --- a/tests/generator/tests.cmake +++ b/tests/generator/tests.cmake @@ -6,7 +6,6 @@ list(APPEND LIBCELLML_TESTS ${CURRENT_TEST}) set(${CURRENT_TEST}_SRCS ${CMAKE_CURRENT_LIST_DIR}/generator.cpp ${CMAKE_CURRENT_LIST_DIR}/generatorprofile.cpp - ${CMAKE_CURRENT_LIST_DIR}/generatorunaryminmax.cpp - ${CMAKE_CURRENT_LIST_DIR}/generatorunaryminus.cpp + ${CMAKE_CURRENT_LIST_DIR}/generatorunary.cpp ${CMAKE_CURRENT_LIST_DIR}/generatorvariabletracker.cpp ) From b1233bd53dcba8ec92365254ef199dd8e378fea1 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Wed, 2 Dec 2026 18:16:46 +1300 Subject: [PATCH 5/5] Generator tests: some cleaning up. --- tests/generator/generatorunary.cpp | 54 ++++----- .../cellml_unary_min_max/example_01.c | 102 +++++++++++++++++ .../example_01.cellml | 0 .../cellml_unary_min_max/example_01.h | 37 ++++++ .../cellml_unary_min_max/example_01.py | 65 +++++++++++ .../cellml_unary_min_max/example_02.c | 102 +++++++++++++++++ .../example_02.cellml | 0 .../cellml_unary_min_max/example_02.h | 37 ++++++ .../cellml_unary_min_max/example_02.py | 65 +++++++++++ .../cellml_unary_min_max/example_03.c | 102 +++++++++++++++++ .../example_03.cellml | 0 .../cellml_unary_min_max/example_03.h | 37 ++++++ .../cellml_unary_min_max/example_03.py | 65 +++++++++++ .../cellml_unary_min_max/example_04.c | 102 +++++++++++++++++ .../example_04.cellml | 0 .../cellml_unary_min_max/example_04.h | 37 ++++++ .../cellml_unary_min_max/example_04.py | 65 +++++++++++ .../cellml_unary_min_max/example_05.c | 107 ++++++++++++++++++ .../example_05.cellml | 0 .../cellml_unary_min_max/example_05.h | 37 ++++++ .../cellml_unary_min_max/example_05.py | 69 +++++++++++ 21 files changed, 1056 insertions(+), 27 deletions(-) create mode 100644 tests/resources/generator/cellml_unary_min_max/example_01.c rename tests/resources/generator/{unary_min_max => cellml_unary_min_max}/example_01.cellml (100%) create mode 100644 tests/resources/generator/cellml_unary_min_max/example_01.h create mode 100644 tests/resources/generator/cellml_unary_min_max/example_01.py create mode 100644 tests/resources/generator/cellml_unary_min_max/example_02.c rename tests/resources/generator/{unary_min_max => cellml_unary_min_max}/example_02.cellml (100%) create mode 100644 tests/resources/generator/cellml_unary_min_max/example_02.h create mode 100644 tests/resources/generator/cellml_unary_min_max/example_02.py create mode 100644 tests/resources/generator/cellml_unary_min_max/example_03.c rename tests/resources/generator/{unary_min_max => cellml_unary_min_max}/example_03.cellml (100%) create mode 100644 tests/resources/generator/cellml_unary_min_max/example_03.h create mode 100644 tests/resources/generator/cellml_unary_min_max/example_03.py create mode 100644 tests/resources/generator/cellml_unary_min_max/example_04.c rename tests/resources/generator/{unary_min_max => cellml_unary_min_max}/example_04.cellml (100%) create mode 100644 tests/resources/generator/cellml_unary_min_max/example_04.h create mode 100644 tests/resources/generator/cellml_unary_min_max/example_04.py create mode 100644 tests/resources/generator/cellml_unary_min_max/example_05.c rename tests/resources/generator/{unary_min_max => cellml_unary_min_max}/example_05.cellml (100%) create mode 100644 tests/resources/generator/cellml_unary_min_max/example_05.h create mode 100644 tests/resources/generator/cellml_unary_min_max/example_05.py diff --git a/tests/generator/generatorunary.cpp b/tests/generator/generatorunary.cpp index cac69c5e59..81e27cf106 100644 --- a/tests/generator/generatorunary.cpp +++ b/tests/generator/generatorunary.cpp @@ -20,14 +20,16 @@ limitations under the License. #include +// Unary min/max tests. + namespace { -std::string implementationCode(const std::string &fileName, bool needMaxFunction) +static void expectUnaryMinMaxExample(const std::string &example, bool needMaxFunction) { auto parser = libcellml::Parser::create(); - auto model = parser->parseModel(fileContents(fileName)); + auto model = parser->parseModel(fileContents("generator/cellml_unary_min_max/" + example + ".cellml")); - EXPECT_NE(nullptr, model); + EXPECT_EQ(size_t(0), parser->issueCount()); auto analyser = libcellml::Analyser::create(); @@ -40,59 +42,57 @@ std::string implementationCode(const std::string &fileName, bool needMaxFunction EXPECT_FALSE(analyserModel->needMinFunction()); EXPECT_EQ(needMaxFunction, analyserModel->needMaxFunction()); - return libcellml::Generator::create()->implementationCode(analyserModel); + auto generator = libcellml::Generator::create(); + auto profile = libcellml::GeneratorProfile::create(); + + profile->setInterfaceFileNameString(example + ".h"); + + EXPECT_EQ_FILE_CONTENTS("generator/cellml_unary_min_max/" + example + ".h", generator->interfaceCode(analyserModel, profile)); + EXPECT_EQ_FILE_CONTENTS("generator/cellml_unary_min_max/" + example + ".c", generator->implementationCode(analyserModel, profile)); + + profile = libcellml::GeneratorProfile::create(libcellml::GeneratorProfile::Profile::PYTHON); + + EXPECT_EQ_FILE_CONTENTS("generator/cellml_unary_min_max/" + example + ".py", generator->implementationCode(analyserModel, profile)); } } // namespace -// Test cases for the generator of unary min and max functions. - -TEST(GeneratorUnaryMinMax, example01) +TEST(GeneratorUnary, unaryMinMaxExample01) { // algebraicVariables[0] = min(voi); - auto code = implementationCode("generator/unary_min_max/example_01.cellml", false); - - EXPECT_TRUE(code.find("algebraicVariables[0] = voi;\n") != std::string::npos); + expectUnaryMinMaxExample("example_01", false); } -TEST(GeneratorUnaryMinMax, example02) +TEST(GeneratorUnary, unaryMinMaxExample02) { // algebraicVariables[0] = max(voi); - auto code = implementationCode("generator/unary_min_max/example_02.cellml", false); - - EXPECT_TRUE(code.find("algebraicVariables[0] = voi;\n") != std::string::npos); + expectUnaryMinMaxExample("example_02", false); } -TEST(GeneratorUnaryMinMax, example03) +TEST(GeneratorUnary, unaryMinMaxExample03) { // computedConstants[0] = 2.0*min(constants[1]+constants[0]); - auto code = implementationCode("generator/unary_min_max/example_03.cellml", false); - - EXPECT_TRUE(code.find("computedConstants[0] = 2.0*(constants[1]+constants[0]);\n") != std::string::npos); + expectUnaryMinMaxExample("example_03", false); } -TEST(GeneratorUnaryMinMax, example04) +TEST(GeneratorUnary, unaryMinMaxExample04) { // computedConstants[0] = 2.0*max(constants[1]+constants[0]); - auto code = implementationCode("generator/unary_min_max/example_04.cellml", false); - - EXPECT_TRUE(code.find("computedConstants[0] = 2.0*(constants[1]+constants[0]);\n") != std::string::npos); + expectUnaryMinMaxExample("example_04", false); } -TEST(GeneratorUnaryMinMax, example05) +TEST(GeneratorUnary, unaryMinMaxExample05) { // algebraicVariables[0] = max(min(voi), constants[0]); - auto code = implementationCode("generator/unary_min_max/example_05.cellml", true); - - EXPECT_TRUE(code.find("algebraicVariables[0] = max(voi, constants[0]);\n") != std::string::npos); + expectUnaryMinMaxExample("example_05", true); } -// Test cases for the generator of unary minus functions. +// Unary minus tests. TEST(GeneratorUnaryMinus, example01) { diff --git a/tests/resources/generator/cellml_unary_min_max/example_01.c b/tests/resources/generator/cellml_unary_min_max/example_01.c new file mode 100644 index 0000000000..35d4364057 --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_01.c @@ -0,0 +1,102 @@ +/* The content of this file was generated using the C profile of libCellML 0.7.2. */ + +#include "example_01.h" + +#include +#include + +const char VERSION[] = "0.8.1"; +const char LIBCELLML_VERSION[] = "0.7.2"; + +const size_t STATE_COUNT = 1; +const size_t CONSTANT_COUNT = 2; +const size_t COMPUTED_CONSTANT_COUNT = 0; +const size_t ALGEBRAIC_VARIABLE_COUNT = 1; + +const VariableInfo VOI_INFO = {"t", "dimensionless", "c"}; + +const VariableInfo STATE_INFO[] = { + {"x", "dimensionless", "c"} +}; + +const VariableInfo CONSTANT_INFO[] = { + {"a", "dimensionless", "c"}, + {"b", "dimensionless", "c"} +}; + +const VariableInfo COMPUTED_CONSTANT_INFO[] = { +}; + +const VariableInfo ALGEBRAIC_VARIABLE_INFO[] = { + {"y", "dimensionless", "c"} +}; + +double * createStatesArray() +{ + double *res = (double *) malloc(STATE_COUNT*sizeof(double)); + + for (size_t i = 0; i < STATE_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createConstantsArray() +{ + double *res = (double *) malloc(CONSTANT_COUNT*sizeof(double)); + + for (size_t i = 0; i < CONSTANT_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createComputedConstantsArray() +{ + double *res = (double *) malloc(COMPUTED_CONSTANT_COUNT*sizeof(double)); + + for (size_t i = 0; i < COMPUTED_CONSTANT_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createAlgebraicVariablesArray() +{ + double *res = (double *) malloc(ALGEBRAIC_VARIABLE_COUNT*sizeof(double)); + + for (size_t i = 0; i < ALGEBRAIC_VARIABLE_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +void deleteArray(double *array) +{ + free(array); +} + +void initialiseArrays(double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + states[0] = 0.0; + constants[0] = 2.0; + constants[1] = 3.0; +} + +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ +} + +void computeRates(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + rates[0] = 1.0; +} + +void computeVariables(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + algebraicVariables[0] = voi; +} diff --git a/tests/resources/generator/unary_min_max/example_01.cellml b/tests/resources/generator/cellml_unary_min_max/example_01.cellml similarity index 100% rename from tests/resources/generator/unary_min_max/example_01.cellml rename to tests/resources/generator/cellml_unary_min_max/example_01.cellml diff --git a/tests/resources/generator/cellml_unary_min_max/example_01.h b/tests/resources/generator/cellml_unary_min_max/example_01.h new file mode 100644 index 0000000000..cab8dce21a --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_01.h @@ -0,0 +1,37 @@ +/* The content of this file was generated using the C profile of libCellML 0.7.2. */ + +#pragma once + +#include + +extern const char VERSION[]; +extern const char LIBCELLML_VERSION[]; + +extern const size_t STATE_COUNT; +extern const size_t CONSTANT_COUNT; +extern const size_t COMPUTED_CONSTANT_COUNT; +extern const size_t ALGEBRAIC_VARIABLE_COUNT; + +typedef struct { + char name[2]; + char units[14]; + char component[2]; +} VariableInfo; + +extern const VariableInfo VOI_INFO; +extern const VariableInfo STATE_INFO[]; +extern const VariableInfo CONSTANT_INFO[]; +extern const VariableInfo COMPUTED_CONSTANT_INFO[]; +extern const VariableInfo ALGEBRAIC_VARIABLE_INFO[]; + +double * createStatesArray(); +double * createConstantsArray(); +double * createComputedConstantsArray(); +double * createAlgebraicVariablesArray(); + +void deleteArray(double *array); + +void initialiseArrays(double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeRates(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeVariables(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); diff --git a/tests/resources/generator/cellml_unary_min_max/example_01.py b/tests/resources/generator/cellml_unary_min_max/example_01.py new file mode 100644 index 0000000000..a6d52c8070 --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_01.py @@ -0,0 +1,65 @@ +# The content of this file was generated using the Python profile of libCellML 0.7.2. + +from enum import Enum +from math import * + + +__version__ = "0.8.1" +LIBCELLML_VERSION = "0.7.2" + +STATE_COUNT = 1 +CONSTANT_COUNT = 2 +COMPUTED_CONSTANT_COUNT = 0 +ALGEBRAIC_VARIABLE_COUNT = 1 + +VOI_INFO = {"name": "t", "units": "dimensionless", "component": "c"} + +STATE_INFO = [ + {"name": "x", "units": "dimensionless", "component": "c"} +] + +CONSTANT_INFO = [ + {"name": "a", "units": "dimensionless", "component": "c"}, + {"name": "b", "units": "dimensionless", "component": "c"} +] + +COMPUTED_CONSTANT_INFO = [ +] + +ALGEBRAIC_VARIABLE_INFO = [ + {"name": "y", "units": "dimensionless", "component": "c"} +] + + +def create_states_array(): + return [nan]*STATE_COUNT + + +def create_constants_array(): + return [nan]*CONSTANT_COUNT + + +def create_computed_constants_array(): + return [nan]*COMPUTED_CONSTANT_COUNT + + +def create_algebraic_variables_array(): + return [nan]*ALGEBRAIC_VARIABLE_COUNT + + +def initialise_arrays(states, rates, constants, computed_constants, algebraic_variables): + states[0] = 0.0 + constants[0] = 2.0 + constants[1] = 3.0 + + +def compute_computed_constants(voi, states, rates, constants, computed_constants, algebraic_variables): + pass + + +def compute_rates(voi, states, rates, constants, computed_constants, algebraic_variables): + rates[0] = 1.0 + + +def compute_variables(voi, states, rates, constants, computed_constants, algebraic_variables): + algebraic_variables[0] = voi diff --git a/tests/resources/generator/cellml_unary_min_max/example_02.c b/tests/resources/generator/cellml_unary_min_max/example_02.c new file mode 100644 index 0000000000..b39e309931 --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_02.c @@ -0,0 +1,102 @@ +/* The content of this file was generated using the C profile of libCellML 0.7.2. */ + +#include "example_02.h" + +#include +#include + +const char VERSION[] = "0.8.1"; +const char LIBCELLML_VERSION[] = "0.7.2"; + +const size_t STATE_COUNT = 1; +const size_t CONSTANT_COUNT = 2; +const size_t COMPUTED_CONSTANT_COUNT = 0; +const size_t ALGEBRAIC_VARIABLE_COUNT = 1; + +const VariableInfo VOI_INFO = {"t", "dimensionless", "c"}; + +const VariableInfo STATE_INFO[] = { + {"x", "dimensionless", "c"} +}; + +const VariableInfo CONSTANT_INFO[] = { + {"a", "dimensionless", "c"}, + {"b", "dimensionless", "c"} +}; + +const VariableInfo COMPUTED_CONSTANT_INFO[] = { +}; + +const VariableInfo ALGEBRAIC_VARIABLE_INFO[] = { + {"y", "dimensionless", "c"} +}; + +double * createStatesArray() +{ + double *res = (double *) malloc(STATE_COUNT*sizeof(double)); + + for (size_t i = 0; i < STATE_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createConstantsArray() +{ + double *res = (double *) malloc(CONSTANT_COUNT*sizeof(double)); + + for (size_t i = 0; i < CONSTANT_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createComputedConstantsArray() +{ + double *res = (double *) malloc(COMPUTED_CONSTANT_COUNT*sizeof(double)); + + for (size_t i = 0; i < COMPUTED_CONSTANT_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createAlgebraicVariablesArray() +{ + double *res = (double *) malloc(ALGEBRAIC_VARIABLE_COUNT*sizeof(double)); + + for (size_t i = 0; i < ALGEBRAIC_VARIABLE_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +void deleteArray(double *array) +{ + free(array); +} + +void initialiseArrays(double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + states[0] = 0.0; + constants[0] = 2.0; + constants[1] = 3.0; +} + +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ +} + +void computeRates(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + rates[0] = 1.0; +} + +void computeVariables(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + algebraicVariables[0] = voi; +} diff --git a/tests/resources/generator/unary_min_max/example_02.cellml b/tests/resources/generator/cellml_unary_min_max/example_02.cellml similarity index 100% rename from tests/resources/generator/unary_min_max/example_02.cellml rename to tests/resources/generator/cellml_unary_min_max/example_02.cellml diff --git a/tests/resources/generator/cellml_unary_min_max/example_02.h b/tests/resources/generator/cellml_unary_min_max/example_02.h new file mode 100644 index 0000000000..cab8dce21a --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_02.h @@ -0,0 +1,37 @@ +/* The content of this file was generated using the C profile of libCellML 0.7.2. */ + +#pragma once + +#include + +extern const char VERSION[]; +extern const char LIBCELLML_VERSION[]; + +extern const size_t STATE_COUNT; +extern const size_t CONSTANT_COUNT; +extern const size_t COMPUTED_CONSTANT_COUNT; +extern const size_t ALGEBRAIC_VARIABLE_COUNT; + +typedef struct { + char name[2]; + char units[14]; + char component[2]; +} VariableInfo; + +extern const VariableInfo VOI_INFO; +extern const VariableInfo STATE_INFO[]; +extern const VariableInfo CONSTANT_INFO[]; +extern const VariableInfo COMPUTED_CONSTANT_INFO[]; +extern const VariableInfo ALGEBRAIC_VARIABLE_INFO[]; + +double * createStatesArray(); +double * createConstantsArray(); +double * createComputedConstantsArray(); +double * createAlgebraicVariablesArray(); + +void deleteArray(double *array); + +void initialiseArrays(double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeRates(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeVariables(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); diff --git a/tests/resources/generator/cellml_unary_min_max/example_02.py b/tests/resources/generator/cellml_unary_min_max/example_02.py new file mode 100644 index 0000000000..a6d52c8070 --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_02.py @@ -0,0 +1,65 @@ +# The content of this file was generated using the Python profile of libCellML 0.7.2. + +from enum import Enum +from math import * + + +__version__ = "0.8.1" +LIBCELLML_VERSION = "0.7.2" + +STATE_COUNT = 1 +CONSTANT_COUNT = 2 +COMPUTED_CONSTANT_COUNT = 0 +ALGEBRAIC_VARIABLE_COUNT = 1 + +VOI_INFO = {"name": "t", "units": "dimensionless", "component": "c"} + +STATE_INFO = [ + {"name": "x", "units": "dimensionless", "component": "c"} +] + +CONSTANT_INFO = [ + {"name": "a", "units": "dimensionless", "component": "c"}, + {"name": "b", "units": "dimensionless", "component": "c"} +] + +COMPUTED_CONSTANT_INFO = [ +] + +ALGEBRAIC_VARIABLE_INFO = [ + {"name": "y", "units": "dimensionless", "component": "c"} +] + + +def create_states_array(): + return [nan]*STATE_COUNT + + +def create_constants_array(): + return [nan]*CONSTANT_COUNT + + +def create_computed_constants_array(): + return [nan]*COMPUTED_CONSTANT_COUNT + + +def create_algebraic_variables_array(): + return [nan]*ALGEBRAIC_VARIABLE_COUNT + + +def initialise_arrays(states, rates, constants, computed_constants, algebraic_variables): + states[0] = 0.0 + constants[0] = 2.0 + constants[1] = 3.0 + + +def compute_computed_constants(voi, states, rates, constants, computed_constants, algebraic_variables): + pass + + +def compute_rates(voi, states, rates, constants, computed_constants, algebraic_variables): + rates[0] = 1.0 + + +def compute_variables(voi, states, rates, constants, computed_constants, algebraic_variables): + algebraic_variables[0] = voi diff --git a/tests/resources/generator/cellml_unary_min_max/example_03.c b/tests/resources/generator/cellml_unary_min_max/example_03.c new file mode 100644 index 0000000000..aa7286bf19 --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_03.c @@ -0,0 +1,102 @@ +/* The content of this file was generated using the C profile of libCellML 0.7.2. */ + +#include "example_03.h" + +#include +#include + +const char VERSION[] = "0.8.1"; +const char LIBCELLML_VERSION[] = "0.7.2"; + +const size_t STATE_COUNT = 1; +const size_t CONSTANT_COUNT = 2; +const size_t COMPUTED_CONSTANT_COUNT = 1; +const size_t ALGEBRAIC_VARIABLE_COUNT = 0; + +const VariableInfo VOI_INFO = {"t", "dimensionless", "c"}; + +const VariableInfo STATE_INFO[] = { + {"x", "dimensionless", "c"} +}; + +const VariableInfo CONSTANT_INFO[] = { + {"b", "dimensionless", "c"}, + {"a", "dimensionless", "c"} +}; + +const VariableInfo COMPUTED_CONSTANT_INFO[] = { + {"y", "dimensionless", "c"} +}; + +const VariableInfo ALGEBRAIC_VARIABLE_INFO[] = { +}; + +double * createStatesArray() +{ + double *res = (double *) malloc(STATE_COUNT*sizeof(double)); + + for (size_t i = 0; i < STATE_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createConstantsArray() +{ + double *res = (double *) malloc(CONSTANT_COUNT*sizeof(double)); + + for (size_t i = 0; i < CONSTANT_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createComputedConstantsArray() +{ + double *res = (double *) malloc(COMPUTED_CONSTANT_COUNT*sizeof(double)); + + for (size_t i = 0; i < COMPUTED_CONSTANT_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createAlgebraicVariablesArray() +{ + double *res = (double *) malloc(ALGEBRAIC_VARIABLE_COUNT*sizeof(double)); + + for (size_t i = 0; i < ALGEBRAIC_VARIABLE_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +void deleteArray(double *array) +{ + free(array); +} + +void initialiseArrays(double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + states[0] = 0.0; + constants[0] = 3.0; + constants[1] = 2.0; +} + +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + computedConstants[0] = 2.0*(constants[1]+constants[0]); +} + +void computeRates(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + rates[0] = 1.0; +} + +void computeVariables(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ +} diff --git a/tests/resources/generator/unary_min_max/example_03.cellml b/tests/resources/generator/cellml_unary_min_max/example_03.cellml similarity index 100% rename from tests/resources/generator/unary_min_max/example_03.cellml rename to tests/resources/generator/cellml_unary_min_max/example_03.cellml diff --git a/tests/resources/generator/cellml_unary_min_max/example_03.h b/tests/resources/generator/cellml_unary_min_max/example_03.h new file mode 100644 index 0000000000..cab8dce21a --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_03.h @@ -0,0 +1,37 @@ +/* The content of this file was generated using the C profile of libCellML 0.7.2. */ + +#pragma once + +#include + +extern const char VERSION[]; +extern const char LIBCELLML_VERSION[]; + +extern const size_t STATE_COUNT; +extern const size_t CONSTANT_COUNT; +extern const size_t COMPUTED_CONSTANT_COUNT; +extern const size_t ALGEBRAIC_VARIABLE_COUNT; + +typedef struct { + char name[2]; + char units[14]; + char component[2]; +} VariableInfo; + +extern const VariableInfo VOI_INFO; +extern const VariableInfo STATE_INFO[]; +extern const VariableInfo CONSTANT_INFO[]; +extern const VariableInfo COMPUTED_CONSTANT_INFO[]; +extern const VariableInfo ALGEBRAIC_VARIABLE_INFO[]; + +double * createStatesArray(); +double * createConstantsArray(); +double * createComputedConstantsArray(); +double * createAlgebraicVariablesArray(); + +void deleteArray(double *array); + +void initialiseArrays(double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeRates(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeVariables(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); diff --git a/tests/resources/generator/cellml_unary_min_max/example_03.py b/tests/resources/generator/cellml_unary_min_max/example_03.py new file mode 100644 index 0000000000..390460bc50 --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_03.py @@ -0,0 +1,65 @@ +# The content of this file was generated using the Python profile of libCellML 0.7.2. + +from enum import Enum +from math import * + + +__version__ = "0.8.1" +LIBCELLML_VERSION = "0.7.2" + +STATE_COUNT = 1 +CONSTANT_COUNT = 2 +COMPUTED_CONSTANT_COUNT = 1 +ALGEBRAIC_VARIABLE_COUNT = 0 + +VOI_INFO = {"name": "t", "units": "dimensionless", "component": "c"} + +STATE_INFO = [ + {"name": "x", "units": "dimensionless", "component": "c"} +] + +CONSTANT_INFO = [ + {"name": "b", "units": "dimensionless", "component": "c"}, + {"name": "a", "units": "dimensionless", "component": "c"} +] + +COMPUTED_CONSTANT_INFO = [ + {"name": "y", "units": "dimensionless", "component": "c"} +] + +ALGEBRAIC_VARIABLE_INFO = [ +] + + +def create_states_array(): + return [nan]*STATE_COUNT + + +def create_constants_array(): + return [nan]*CONSTANT_COUNT + + +def create_computed_constants_array(): + return [nan]*COMPUTED_CONSTANT_COUNT + + +def create_algebraic_variables_array(): + return [nan]*ALGEBRAIC_VARIABLE_COUNT + + +def initialise_arrays(states, rates, constants, computed_constants, algebraic_variables): + states[0] = 0.0 + constants[0] = 3.0 + constants[1] = 2.0 + + +def compute_computed_constants(voi, states, rates, constants, computed_constants, algebraic_variables): + computed_constants[0] = 2.0*(constants[1]+constants[0]) + + +def compute_rates(voi, states, rates, constants, computed_constants, algebraic_variables): + rates[0] = 1.0 + + +def compute_variables(voi, states, rates, constants, computed_constants, algebraic_variables): + pass diff --git a/tests/resources/generator/cellml_unary_min_max/example_04.c b/tests/resources/generator/cellml_unary_min_max/example_04.c new file mode 100644 index 0000000000..98925b32ab --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_04.c @@ -0,0 +1,102 @@ +/* The content of this file was generated using the C profile of libCellML 0.7.2. */ + +#include "example_04.h" + +#include +#include + +const char VERSION[] = "0.8.1"; +const char LIBCELLML_VERSION[] = "0.7.2"; + +const size_t STATE_COUNT = 1; +const size_t CONSTANT_COUNT = 2; +const size_t COMPUTED_CONSTANT_COUNT = 1; +const size_t ALGEBRAIC_VARIABLE_COUNT = 0; + +const VariableInfo VOI_INFO = {"t", "dimensionless", "c"}; + +const VariableInfo STATE_INFO[] = { + {"x", "dimensionless", "c"} +}; + +const VariableInfo CONSTANT_INFO[] = { + {"b", "dimensionless", "c"}, + {"a", "dimensionless", "c"} +}; + +const VariableInfo COMPUTED_CONSTANT_INFO[] = { + {"y", "dimensionless", "c"} +}; + +const VariableInfo ALGEBRAIC_VARIABLE_INFO[] = { +}; + +double * createStatesArray() +{ + double *res = (double *) malloc(STATE_COUNT*sizeof(double)); + + for (size_t i = 0; i < STATE_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createConstantsArray() +{ + double *res = (double *) malloc(CONSTANT_COUNT*sizeof(double)); + + for (size_t i = 0; i < CONSTANT_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createComputedConstantsArray() +{ + double *res = (double *) malloc(COMPUTED_CONSTANT_COUNT*sizeof(double)); + + for (size_t i = 0; i < COMPUTED_CONSTANT_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createAlgebraicVariablesArray() +{ + double *res = (double *) malloc(ALGEBRAIC_VARIABLE_COUNT*sizeof(double)); + + for (size_t i = 0; i < ALGEBRAIC_VARIABLE_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +void deleteArray(double *array) +{ + free(array); +} + +void initialiseArrays(double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + states[0] = 0.0; + constants[0] = 3.0; + constants[1] = 2.0; +} + +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + computedConstants[0] = 2.0*(constants[1]+constants[0]); +} + +void computeRates(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + rates[0] = 1.0; +} + +void computeVariables(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ +} diff --git a/tests/resources/generator/unary_min_max/example_04.cellml b/tests/resources/generator/cellml_unary_min_max/example_04.cellml similarity index 100% rename from tests/resources/generator/unary_min_max/example_04.cellml rename to tests/resources/generator/cellml_unary_min_max/example_04.cellml diff --git a/tests/resources/generator/cellml_unary_min_max/example_04.h b/tests/resources/generator/cellml_unary_min_max/example_04.h new file mode 100644 index 0000000000..cab8dce21a --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_04.h @@ -0,0 +1,37 @@ +/* The content of this file was generated using the C profile of libCellML 0.7.2. */ + +#pragma once + +#include + +extern const char VERSION[]; +extern const char LIBCELLML_VERSION[]; + +extern const size_t STATE_COUNT; +extern const size_t CONSTANT_COUNT; +extern const size_t COMPUTED_CONSTANT_COUNT; +extern const size_t ALGEBRAIC_VARIABLE_COUNT; + +typedef struct { + char name[2]; + char units[14]; + char component[2]; +} VariableInfo; + +extern const VariableInfo VOI_INFO; +extern const VariableInfo STATE_INFO[]; +extern const VariableInfo CONSTANT_INFO[]; +extern const VariableInfo COMPUTED_CONSTANT_INFO[]; +extern const VariableInfo ALGEBRAIC_VARIABLE_INFO[]; + +double * createStatesArray(); +double * createConstantsArray(); +double * createComputedConstantsArray(); +double * createAlgebraicVariablesArray(); + +void deleteArray(double *array); + +void initialiseArrays(double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeRates(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeVariables(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); diff --git a/tests/resources/generator/cellml_unary_min_max/example_04.py b/tests/resources/generator/cellml_unary_min_max/example_04.py new file mode 100644 index 0000000000..390460bc50 --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_04.py @@ -0,0 +1,65 @@ +# The content of this file was generated using the Python profile of libCellML 0.7.2. + +from enum import Enum +from math import * + + +__version__ = "0.8.1" +LIBCELLML_VERSION = "0.7.2" + +STATE_COUNT = 1 +CONSTANT_COUNT = 2 +COMPUTED_CONSTANT_COUNT = 1 +ALGEBRAIC_VARIABLE_COUNT = 0 + +VOI_INFO = {"name": "t", "units": "dimensionless", "component": "c"} + +STATE_INFO = [ + {"name": "x", "units": "dimensionless", "component": "c"} +] + +CONSTANT_INFO = [ + {"name": "b", "units": "dimensionless", "component": "c"}, + {"name": "a", "units": "dimensionless", "component": "c"} +] + +COMPUTED_CONSTANT_INFO = [ + {"name": "y", "units": "dimensionless", "component": "c"} +] + +ALGEBRAIC_VARIABLE_INFO = [ +] + + +def create_states_array(): + return [nan]*STATE_COUNT + + +def create_constants_array(): + return [nan]*CONSTANT_COUNT + + +def create_computed_constants_array(): + return [nan]*COMPUTED_CONSTANT_COUNT + + +def create_algebraic_variables_array(): + return [nan]*ALGEBRAIC_VARIABLE_COUNT + + +def initialise_arrays(states, rates, constants, computed_constants, algebraic_variables): + states[0] = 0.0 + constants[0] = 3.0 + constants[1] = 2.0 + + +def compute_computed_constants(voi, states, rates, constants, computed_constants, algebraic_variables): + computed_constants[0] = 2.0*(constants[1]+constants[0]) + + +def compute_rates(voi, states, rates, constants, computed_constants, algebraic_variables): + rates[0] = 1.0 + + +def compute_variables(voi, states, rates, constants, computed_constants, algebraic_variables): + pass diff --git a/tests/resources/generator/cellml_unary_min_max/example_05.c b/tests/resources/generator/cellml_unary_min_max/example_05.c new file mode 100644 index 0000000000..2af0759c8b --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_05.c @@ -0,0 +1,107 @@ +/* The content of this file was generated using the C profile of libCellML 0.7.2. */ + +#include "example_05.h" + +#include +#include + +const char VERSION[] = "0.8.1"; +const char LIBCELLML_VERSION[] = "0.7.2"; + +const size_t STATE_COUNT = 1; +const size_t CONSTANT_COUNT = 2; +const size_t COMPUTED_CONSTANT_COUNT = 0; +const size_t ALGEBRAIC_VARIABLE_COUNT = 1; + +const VariableInfo VOI_INFO = {"t", "dimensionless", "c"}; + +const VariableInfo STATE_INFO[] = { + {"x", "dimensionless", "c"} +}; + +const VariableInfo CONSTANT_INFO[] = { + {"a", "dimensionless", "c"}, + {"b", "dimensionless", "c"} +}; + +const VariableInfo COMPUTED_CONSTANT_INFO[] = { +}; + +const VariableInfo ALGEBRAIC_VARIABLE_INFO[] = { + {"y", "dimensionless", "c"} +}; + +double max(double x, double y) +{ + return (x > y)?x:y; +} + +double * createStatesArray() +{ + double *res = (double *) malloc(STATE_COUNT*sizeof(double)); + + for (size_t i = 0; i < STATE_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createConstantsArray() +{ + double *res = (double *) malloc(CONSTANT_COUNT*sizeof(double)); + + for (size_t i = 0; i < CONSTANT_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createComputedConstantsArray() +{ + double *res = (double *) malloc(COMPUTED_CONSTANT_COUNT*sizeof(double)); + + for (size_t i = 0; i < COMPUTED_CONSTANT_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +double * createAlgebraicVariablesArray() +{ + double *res = (double *) malloc(ALGEBRAIC_VARIABLE_COUNT*sizeof(double)); + + for (size_t i = 0; i < ALGEBRAIC_VARIABLE_COUNT; ++i) { + res[i] = NAN; + } + + return res; +} + +void deleteArray(double *array) +{ + free(array); +} + +void initialiseArrays(double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + states[0] = 0.0; + constants[0] = 2.0; + constants[1] = 3.0; +} + +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ +} + +void computeRates(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + rates[0] = 1.0; +} + +void computeVariables(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + algebraicVariables[0] = max(voi, constants[0]); +} diff --git a/tests/resources/generator/unary_min_max/example_05.cellml b/tests/resources/generator/cellml_unary_min_max/example_05.cellml similarity index 100% rename from tests/resources/generator/unary_min_max/example_05.cellml rename to tests/resources/generator/cellml_unary_min_max/example_05.cellml diff --git a/tests/resources/generator/cellml_unary_min_max/example_05.h b/tests/resources/generator/cellml_unary_min_max/example_05.h new file mode 100644 index 0000000000..cab8dce21a --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_05.h @@ -0,0 +1,37 @@ +/* The content of this file was generated using the C profile of libCellML 0.7.2. */ + +#pragma once + +#include + +extern const char VERSION[]; +extern const char LIBCELLML_VERSION[]; + +extern const size_t STATE_COUNT; +extern const size_t CONSTANT_COUNT; +extern const size_t COMPUTED_CONSTANT_COUNT; +extern const size_t ALGEBRAIC_VARIABLE_COUNT; + +typedef struct { + char name[2]; + char units[14]; + char component[2]; +} VariableInfo; + +extern const VariableInfo VOI_INFO; +extern const VariableInfo STATE_INFO[]; +extern const VariableInfo CONSTANT_INFO[]; +extern const VariableInfo COMPUTED_CONSTANT_INFO[]; +extern const VariableInfo ALGEBRAIC_VARIABLE_INFO[]; + +double * createStatesArray(); +double * createConstantsArray(); +double * createComputedConstantsArray(); +double * createAlgebraicVariablesArray(); + +void deleteArray(double *array); + +void initialiseArrays(double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeRates(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); +void computeVariables(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables); diff --git a/tests/resources/generator/cellml_unary_min_max/example_05.py b/tests/resources/generator/cellml_unary_min_max/example_05.py new file mode 100644 index 0000000000..9d4cb83ddb --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_05.py @@ -0,0 +1,69 @@ +# The content of this file was generated using the Python profile of libCellML 0.7.2. + +from enum import Enum +from math import * + + +__version__ = "0.8.1" +LIBCELLML_VERSION = "0.7.2" + +STATE_COUNT = 1 +CONSTANT_COUNT = 2 +COMPUTED_CONSTANT_COUNT = 0 +ALGEBRAIC_VARIABLE_COUNT = 1 + +VOI_INFO = {"name": "t", "units": "dimensionless", "component": "c"} + +STATE_INFO = [ + {"name": "x", "units": "dimensionless", "component": "c"} +] + +CONSTANT_INFO = [ + {"name": "a", "units": "dimensionless", "component": "c"}, + {"name": "b", "units": "dimensionless", "component": "c"} +] + +COMPUTED_CONSTANT_INFO = [ +] + +ALGEBRAIC_VARIABLE_INFO = [ + {"name": "y", "units": "dimensionless", "component": "c"} +] + + +def max(x, y): + return x if x > y else y + + +def create_states_array(): + return [nan]*STATE_COUNT + + +def create_constants_array(): + return [nan]*CONSTANT_COUNT + + +def create_computed_constants_array(): + return [nan]*COMPUTED_CONSTANT_COUNT + + +def create_algebraic_variables_array(): + return [nan]*ALGEBRAIC_VARIABLE_COUNT + + +def initialise_arrays(states, rates, constants, computed_constants, algebraic_variables): + states[0] = 0.0 + constants[0] = 2.0 + constants[1] = 3.0 + + +def compute_computed_constants(voi, states, rates, constants, computed_constants, algebraic_variables): + pass + + +def compute_rates(voi, states, rates, constants, computed_constants, algebraic_variables): + rates[0] = 1.0 + + +def compute_variables(voi, states, rates, constants, computed_constants, algebraic_variables): + algebraic_variables[0] = max(voi, constants[0])