diff --git a/src/analyser.cpp b/src/analyser.cpp index 9e194495b..88c5ae91a 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/src/validator.cpp b/src/validator.cpp index 00ffb10ea..01c4dca45 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/generator/generatorunary.cpp b/tests/generator/generatorunary.cpp index 2353fb03f..81e27cf10 100644 --- a/tests/generator/generatorunary.cpp +++ b/tests/generator/generatorunary.cpp @@ -20,6 +20,80 @@ limitations under the License. #include +// Unary min/max tests. + +namespace { + +static void expectUnaryMinMaxExample(const std::string &example, bool needMaxFunction) +{ + auto parser = libcellml::Parser::create(); + auto model = parser->parseModel(fileContents("generator/cellml_unary_min_max/" + example + ".cellml")); + + EXPECT_EQ(size_t(0), parser->issueCount()); + + 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()); + + 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(GeneratorUnary, unaryMinMaxExample01) +{ + // algebraicVariables[0] = min(voi); + + expectUnaryMinMaxExample("example_01", false); +} + +TEST(GeneratorUnary, unaryMinMaxExample02) +{ + // algebraicVariables[0] = max(voi); + + expectUnaryMinMaxExample("example_02", false); +} + +TEST(GeneratorUnary, unaryMinMaxExample03) +{ + // computedConstants[0] = 2.0*min(constants[1]+constants[0]); + + expectUnaryMinMaxExample("example_03", false); +} + +TEST(GeneratorUnary, unaryMinMaxExample04) +{ + // computedConstants[0] = 2.0*max(constants[1]+constants[0]); + + expectUnaryMinMaxExample("example_04", false); +} + +TEST(GeneratorUnary, unaryMinMaxExample05) +{ + // algebraicVariables[0] = max(min(voi), constants[0]); + + expectUnaryMinMaxExample("example_05", true); +} + +// Unary minus tests. + TEST(GeneratorUnaryMinus, example01) { // rates[0] = --1.0; 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 000000000..35d436405 --- /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/cellml_unary_min_max/example_01.cellml b/tests/resources/generator/cellml_unary_min_max/example_01.cellml new file mode 100644 index 000000000..bce3f1b2d --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_01.cellml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + t + + x + + 1 + + + + y + + + t + + + + + 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 000000000..cab8dce21 --- /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 000000000..a6d52c807 --- /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 000000000..b39e30993 --- /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/cellml_unary_min_max/example_02.cellml b/tests/resources/generator/cellml_unary_min_max/example_02.cellml new file mode 100644 index 000000000..18c084eeb --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_02.cellml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + t + + x + + 1 + + + + y + + + t + + + + + 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 000000000..cab8dce21 --- /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 000000000..a6d52c807 --- /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 000000000..aa7286bf1 --- /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/cellml_unary_min_max/example_03.cellml b/tests/resources/generator/cellml_unary_min_max/example_03.cellml new file mode 100644 index 000000000..971ef4c45 --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_03.cellml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + t + + x + + 1 + + + + y + + + 2 + + + + + a + b + + + + + + + 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 000000000..cab8dce21 --- /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 000000000..390460bc5 --- /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 000000000..98925b32a --- /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/cellml_unary_min_max/example_04.cellml b/tests/resources/generator/cellml_unary_min_max/example_04.cellml new file mode 100644 index 000000000..6a21131f6 --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_04.cellml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + t + + x + + 1 + + + + y + + + 2 + + + + + a + b + + + + + + + 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 000000000..cab8dce21 --- /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 000000000..390460bc5 --- /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 000000000..2af0759c8 --- /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/cellml_unary_min_max/example_05.cellml b/tests/resources/generator/cellml_unary_min_max/example_05.cellml new file mode 100644 index 000000000..21b63b5a2 --- /dev/null +++ b/tests/resources/generator/cellml_unary_min_max/example_05.cellml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + t + + x + + 1 + + + + y + + + + + t + + a + + + + + 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 000000000..cab8dce21 --- /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 000000000..9d4cb83dd --- /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]) diff --git a/tests/resources/invalidminmaxremsiblings.cellml b/tests/resources/invalidminmaxremsiblings.cellml new file mode 100644 index 000000000..864c86160 --- /dev/null +++ b/tests/resources/invalidminmaxremsiblings.cellml @@ -0,0 +1,107 @@ + + + + + + + + + + + + + + + + + + + + + y1 + + + a + + + + + y2 + + + a + b + c + + + + + y3 + + + a + b + + + + + + y4 + + + + + + + y5 + + + + + + + y6 + + + + + + + + y7 + + + a + + + + + y8 + + + a + b + c + + + + + + y9 + + a + + + + + + y10 + + a + + b + + + + + diff --git a/tests/validator/validator.cpp b/tests/validator/validator.cpp index 243d9a289..b319dd65f 100644 --- a/tests/validator/validator.cpp +++ b/tests/validator/validator.cpp @@ -4431,3 +4431,27 @@ 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.", + "Math has a 'rem' 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); +}