diff --git a/src/generator.cpp b/src/generator.cpp index f71612677..ad0327ec1 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -130,19 +130,6 @@ double Generator::GeneratorImpl::scalingFactor(const AnalyserModelPtr &analyserM return Units::scalingFactor(analyserVariable->variable()->units(), variable->units()); } -bool Generator::GeneratorImpl::isNegativeNumber(const AnalyserEquationAstPtr &ast) const -{ - if (ast->type() == AnalyserEquationAst::Type::CN) { - double doubleValue; - - convertToDouble(ast->value(), doubleValue); - - return doubleValue < 0.0; - } - - return false; -} - bool Generator::GeneratorImpl::isRelationalOperator(const AnalyserEquationAstPtr &ast) const { switch (ast->type()) { @@ -190,6 +177,12 @@ bool Generator::GeneratorImpl::isLogicalOperator(const AnalyserEquationAstPtr &a return isAndOperator(ast) || isOrOperator(ast) || isXorOperator(ast); } +bool Generator::GeneratorImpl::isNotOperator(const AnalyserEquationAstPtr &ast) const +{ + return (ast->type() == AnalyserEquationAst::Type::NOT) + && mProfile->hasNotOperator(); +} + bool Generator::GeneratorImpl::isPlusOperator(const AnalyserEquationAstPtr &ast) const { return ast->type() == AnalyserEquationAst::Type::PLUS; @@ -243,6 +236,72 @@ AnalyserEquationAstPtr Generator::GeneratorImpl::unaryPlusOperand(const Analyser return res; } +AnalyserEquationAstPtr Generator::GeneratorImpl::negatedOperand(const AnalyserEquationAstPtr &ast) const +{ + // Return the AST of a, if the code generated for the given AST is a negation, i.e. -a, or nullptr otherwise. To + // that end, we look through any unary PLUS (which is generated as its operand alone, see generateCode()) and unary + // MINUS, keeping track of the number of the latter since -(-a) is generated as a (see generateMinusUnaryCode()). + // Note: a negative number, e.g. -3, is also a negation, in which case we return a new AST for its absolute value, + // e.g. 3. + + auto res = ast; + auto negated = false; + + while ((isPlusOperator(res) || isMinusOperator(res)) && (res->rightChild() == nullptr)) { + if (isMinusOperator(res)) { + negated = !negated; + } + + res = res->leftChild(); + } + + // Note: we check the value as written in the model, so its sign is always '-', irrespective of the profile. + + if ((res->type() == AnalyserEquationAst::Type::CN) + && (res->value().rfind('-', 0) == 0)) { + if (negated) { + return nullptr; + } + + auto absoluteValue = AnalyserEquationAst::create(); + + absoluteValue->setType(AnalyserEquationAst::Type::CN); + absoluteValue->setValue(res->value().substr(1)); + + return absoluteValue; + } + + return negated ? res : nullptr; +} + +AnalyserEquationAstPtr Generator::GeneratorImpl::doubleNegationOperand(const AnalyserEquationAstPtr &ast) const +{ + // Return the AST of a, if the given AST (looking through any unary PLUS) is a double negation, i.e. -(-a), or + // nullptr otherwise. + + auto res = unaryPlusOperand(ast); + + return (isMinusOperator(res) && (res->rightChild() == nullptr)) ? negatedOperand(res->leftChild()) : nullptr; +} + +bool Generator::GeneratorImpl::isDoubleNegationOfPiecewiseStatement(const AnalyserEquationAstPtr &ast) const +{ + auto astDoubleNegationOperand = doubleNegationOperand(ast); + + return (astDoubleNegationOperand != nullptr) && isPiecewiseStatement(astDoubleNegationOperand); +} + +AnalyserEquationAstPtr Generator::GeneratorImpl::generatedAst(const AnalyserEquationAstPtr &ast) const +{ + // Return the AST that corresponds to the code that will actually be generated for the given AST, i.e. looking + // through any unary PLUS (see unaryPlusOperand()) and any double negation (since -(-a) is generated as a, see + // generateMinusUnaryCode()). This is needed to determine whether parentheses should be added around some code. + + auto astDoubleNegationOperand = doubleNegationOperand(ast); + + return (astDoubleNegationOperand != nullptr) ? astDoubleNegationOperand : unaryPlusOperand(ast); +} + void Generator::GeneratorImpl::updateVariableInfoSizes(size_t &componentSize, size_t &nameSize, size_t &unitsSize, @@ -1018,11 +1077,28 @@ std::string Generator::GeneratorImpl::generateVariableNameCode(const VariablePtr std::string Generator::GeneratorImpl::generateOperatorCode(const std::string &op, const AnalyserEquationAstPtr &ast) { // Generate the code for the left and right branches of the given AST. + // Note: a-(-b) is generated as a+b, which gives exactly the same result (incl. for NaN and signed zero), but is + // easier to read. So, for a MINUS, we generate the code for b rather than for -b as the right branch. + auto astNegatedRightChild = isMinusOperator(ast) ? negatedOperand(ast->rightChild()) : nullptr; + auto astRightOperand = (astNegatedRightChild != nullptr) ? astNegatedRightChild : ast->rightChild(); auto astLeftChildCode = generateCode(ast->leftChild()); - auto astRightChildCode = generateCode(ast->rightChild()); - auto astLeftChild = unaryPlusOperand(ast->leftChild()); - auto astRightChild = unaryPlusOperand(ast->rightChild()); + auto astRightChildCode = generateCode(astRightOperand); + + // Look through any unary PLUS and double negation (see generatedAst()), so that we can determine what will actually + // be generated. + + auto astLeftChild = generatedAst(ast->leftChild()); + auto astRightChild = generatedAst(astRightOperand); + + // A double negation, e.g. -(-(b+c)), is generated as its operand, i.e. b+c here, but it acts as a pair of + // parentheses. So, we must add those parentheses back wherever not having them would change the way the code gets + // evaluated, even if we wouldn't otherwise add parentheses there. For instance, a+(b+c) is generated as a+b+c, but + // a+(-(-(b+c))) must be generated as a+(b+c) since a+b+c gets evaluated as (a+b)+c, which might give a (slightly) + // different result. + + auto astLeftChildIsDoubleNegation = doubleNegationOperand(ast->leftChild()) != nullptr; + auto astRightChildIsDoubleNegation = doubleNegationOperand(astRightOperand) != nullptr; // Determine whether parentheses should be added around the left and/or // right piece of code, and this is based on the precedence of the operators @@ -1049,7 +1125,9 @@ std::string Generator::GeneratorImpl::generateOperatorCode(const std::string &op if (isRelationalOperator(astRightChild) || isLogicalOperator(astRightChild) - || isPiecewiseStatement(astRightChild)) { + || isPiecewiseStatement(astRightChild) + || (astRightChildIsDoubleNegation + && (isPlusOperator(astRightChild) || isMinusOperator(astRightChild)))) { astRightChildCode = "(" + astRightChildCode + ")"; } } else if (isMinusOperator(ast)) { @@ -1059,13 +1137,34 @@ std::string Generator::GeneratorImpl::generateOperatorCode(const std::string &op astLeftChildCode = "(" + astLeftChildCode + ")"; } - if (isNegativeNumber(astRightChild) - || isRelationalOperator(astRightChild) + if (astNegatedRightChild != nullptr) { + // We are generating a+b rather than a-(-b), so add parentheses as for a PLUS, but also around a PLUS or a + // MINUS since a-(-(b+c)) must be generated as a+(b+c) and not as a+b+c, which would be evaluated as + // (a+b)+c and might therefore give a (slightly) different result. + + if (isRelationalOperator(astRightChild) + || isLogicalOperator(astRightChild) + || isPlusOperator(astRightChild) + || isMinusOperator(astRightChild) + || isPiecewiseStatement(astRightChild)) { + astRightChildCode = "(" + astRightChildCode + ")"; + } + + return astLeftChildCode + mProfile->plusString() + astRightChildCode; + } + + // Note: the right code may start with a unary MINUS (i.e. the profile's minus string) or with a negative number + // (whose sign is always '-', irrespective of the profile, see negatedOperand()). Either way, we want to + // avoid two consecutive minus signs, e.g. a-(-2.0*b) rather than a--2.0*b (or a - -2.0*b with a minus + // string of " - "). + + if (isRelationalOperator(astRightChild) || isLogicalOperator(astRightChild) || isPlusOperator(astRightChild) || isMinusOperator(astRightChild) || isPiecewiseStatement(astRightChild) - || (astRightChildCode.rfind(mProfile->minusString(), 0) == 0)) { + || (astRightChildCode.rfind(mProfile->minusString(), 0) == 0) + || (astRightChildCode.rfind('-', 0) == 0)) { astRightChildCode = "(" + astRightChildCode + ")"; } } else if (isTimesOperator(ast)) { @@ -1082,7 +1181,9 @@ std::string Generator::GeneratorImpl::generateOperatorCode(const std::string &op if (isRelationalOperator(astRightChild) || isLogicalOperator(astRightChild) - || isPiecewiseStatement(astRightChild)) { + || isPiecewiseStatement(astRightChild) + || (astRightChildIsDoubleNegation + && (isTimesOperator(astRightChild) || isDivideOperator(astRightChild)))) { astRightChildCode = "(" + astRightChildCode + ")"; } else if (isPlusOperator(astRightChild) || isMinusOperator(astRightChild)) { @@ -1114,6 +1215,24 @@ std::string Generator::GeneratorImpl::generateOperatorCode(const std::string &op astRightChildCode = "(" + astRightChildCode + ")"; } } + } else if (isRelationalOperator(ast)) { + // Note: we don't add parentheses around anything here, except around a double negation of a relational or + // logical operator or of a PIECEWISE (as an operator). For instance, a < (-(-(b > c))) must be generated + // as a < (b > c) and not as a < b > c, which would be evaluated as (a < b) > c. + + if (astLeftChildIsDoubleNegation + && (isRelationalOperator(astLeftChild) + || isLogicalOperator(astLeftChild) + || isPiecewiseStatement(astLeftChild))) { + astLeftChildCode = "(" + astLeftChildCode + ")"; + } + + if (astRightChildIsDoubleNegation + && (isRelationalOperator(astRightChild) + || isLogicalOperator(astRightChild) + || isPiecewiseStatement(astRightChild))) { + astRightChildCode = "(" + astRightChildCode + ")"; + } } else if (isAndOperator(ast)) { // Note: according to the precedence rules above, we only need to // add parentheses around OR and PIECEWISE. However, it looks @@ -1232,7 +1351,9 @@ std::string Generator::GeneratorImpl::generateOperatorCode(const std::string &op || isMinusOperator(astLeftChild) || isTimesOperator(astLeftChild) || isDivideOperator(astLeftChild) - || isPiecewiseStatement(astLeftChild)) { + || isPiecewiseStatement(astLeftChild) + || (astLeftChildIsDoubleNegation + && (isNotOperator(astLeftChild) || isPowerOperator(astLeftChild) || isRootOperator(astLeftChild)))) { astLeftChildCode = "(" + astLeftChildCode + ")"; } @@ -1254,11 +1375,13 @@ std::string Generator::GeneratorImpl::generateOperatorCode(const std::string &op || isMinusOperator(astRightChild) || isTimesOperator(astRightChild) || isDivideOperator(astRightChild) - || isPiecewiseStatement(astRightChild)) { + || isPiecewiseStatement(astRightChild) + || (astRightChildIsDoubleNegation + && (isNotOperator(astRightChild) || isPowerOperator(astRightChild) || isRootOperator(astRightChild)))) { astRightChildCode = "(" + astRightChildCode + ")"; } - auto astLeftChildLeftChild = unaryPlusOperand(astLeftChild->leftChild()); + auto astLeftChildLeftChild = generatedAst(astLeftChild->leftChild()); if (isRelationalOperator(astLeftChildLeftChild) || isLogicalOperator(astLeftChildLeftChild) @@ -1281,20 +1404,34 @@ std::string Generator::GeneratorImpl::generateOperatorCode(const std::string &op std::string Generator::GeneratorImpl::generateMinusUnaryCode(const AnalyserEquationAstPtr &ast) { // Generate the code for the left branch of the given AST. + // Note: -(-a) is generated as a, which gives exactly the same result (incl. for NaN and signed zero), but is easier + // to read. The double negation does, however, act as a pair of parentheses, which are added back where + // needed (see generateOperatorCode() and the PIECE case in generateCode()). + + auto astDoubleNegationOperand = doubleNegationOperand(ast); + + if (astDoubleNegationOperand != nullptr) { + return generateCode(astDoubleNegationOperand); + } auto code = generateCode(ast->leftChild()); // Determine whether parentheses should be added around the left code. + // Note: we look through any unary PLUS and double negation (see generatedAst()) to determine what will actually be + // generated. Also, the code may start with a unary MINUS (i.e. the profile's minus string) or with a negative + // number (whose sign is always '-', irrespective of the profile, see negatedOperand()). Either way, we want + // to avoid two consecutive minus signs, e.g. -(-2.0*b) rather than --2.0*b (or - -2.0*b with a minus string + // of " - "). - auto astLeftChild = unaryPlusOperand(ast->leftChild()); + auto astLeftChild = generatedAst(ast->leftChild()); if (isRelationalOperator(astLeftChild) || isLogicalOperator(astLeftChild) || isPlusOperator(astLeftChild) || isMinusOperator(astLeftChild) - || isNegativeNumber(astLeftChild) || isPiecewiseStatement(astLeftChild) - || (code.rfind(mProfile->minusString(), 0) == 0)) { + || (code.rfind(mProfile->minusString(), 0) == 0) + || (code.rfind('-', 0) == 0)) { code = "(" + code + ")"; } @@ -1310,10 +1447,10 @@ std::string Generator::GeneratorImpl::generateNotUnaryCode(const AnalyserEquatio // Determine whether parentheses should be added around the left code. // Note: NOT has a higher precedence than all the binary operators, except POWER (as an operator), and than // PIECEWISE (as an operator), so parentheses are needed around any of them, but not around a unary - // MINUS or another NOT. A unary PLUS is generated as its operand alone (see generateCode()), so we look - // through it to determine what will actually be generated. + // MINUS or another NOT. We look through any unary PLUS and double negation (see generatedAst()) to determine + // what will actually be generated. - auto astOperand = unaryPlusOperand(ast->leftChild()); + auto astOperand = generatedAst(ast->leftChild()); if (isRelationalOperator(astOperand) || isLogicalOperator(astOperand) @@ -1694,10 +1831,26 @@ std::string Generator::GeneratorImpl::generateCode(const AnalyserEquationAstPtr code = generateCode(ast->leftChild()) + generatePiecewiseElseCode(mProfile->nanString()); } } break; - case AnalyserEquationAst::Type::PIECE: - code = generatePiecewiseIfCode(generateCode(ast->rightChild()), generateCode(ast->leftChild())); + case AnalyserEquationAst::Type::PIECE: { + auto conditionCode = generateCode(ast->rightChild()); + auto valueCode = generateCode(ast->leftChild()); - break; + // A double negation of a PIECEWISE (as an operator) is generated as that PIECEWISE (see + // generateMinusUnaryCode()), but it acts as a pair of parentheses which we must keep around the value (e.g., + // in Python, (a if b else c) if d else e is not the same as a if b else c if d else e) and around the condition + // (unless the profile already puts the condition between parentheses, as is the case for C). + + if (isDoubleNegationOfPiecewiseStatement(ast->leftChild())) { + valueCode = "(" + valueCode + ")"; + } + + if (isDoubleNegationOfPiecewiseStatement(ast->rightChild()) + && (mProfile->conditionalOperatorIfString().find("([CONDITION])") == std::string::npos)) { + conditionCode = "(" + conditionCode + ")"; + } + + code = generatePiecewiseIfCode(conditionCode, valueCode); + } break; case AnalyserEquationAst::Type::OTHERWISE: code = generateCode(ast->leftChild()); diff --git a/src/generator_p.h b/src/generator_p.h index 9073d870c..5d12beaa9 100644 --- a/src/generator_p.h +++ b/src/generator_p.h @@ -57,13 +57,12 @@ struct Generator::GeneratorImpl: public Logger::LoggerImpl double scalingFactor(const AnalyserModelPtr &analyserModel, const VariablePtr &variable) const; - bool isNegativeNumber(const AnalyserEquationAstPtr &ast) const; - bool isRelationalOperator(const AnalyserEquationAstPtr &ast) const; bool isAndOperator(const AnalyserEquationAstPtr &ast) const; bool isOrOperator(const AnalyserEquationAstPtr &ast) const; bool isXorOperator(const AnalyserEquationAstPtr &ast) const; bool isLogicalOperator(const AnalyserEquationAstPtr &ast) const; + bool isNotOperator(const AnalyserEquationAstPtr &ast) const; bool isPlusOperator(const AnalyserEquationAstPtr &ast) const; bool isMinusOperator(const AnalyserEquationAstPtr &ast) const; bool isTimesOperator(const AnalyserEquationAstPtr &ast) const; @@ -73,6 +72,10 @@ struct Generator::GeneratorImpl: public Logger::LoggerImpl bool isPiecewiseStatement(const AnalyserEquationAstPtr &ast) const; AnalyserEquationAstPtr unaryPlusOperand(const AnalyserEquationAstPtr &ast) const; + AnalyserEquationAstPtr negatedOperand(const AnalyserEquationAstPtr &ast) const; + AnalyserEquationAstPtr doubleNegationOperand(const AnalyserEquationAstPtr &ast) const; + bool isDoubleNegationOfPiecewiseStatement(const AnalyserEquationAstPtr &ast) const; + AnalyserEquationAstPtr generatedAst(const AnalyserEquationAstPtr &ast) const; void updateVariableInfoSizes(size_t &componentSize, size_t &nameSize, size_t &unitsSize, diff --git a/tests/generator/generatorunary.cpp b/tests/generator/generatorunary.cpp index 2353fb03f..6e51a4ef6 100644 --- a/tests/generator/generatorunary.cpp +++ b/tests/generator/generatorunary.cpp @@ -22,7 +22,7 @@ limitations under the License. TEST(GeneratorUnaryMinus, example01) { - // rates[0] = --1.0; + // rates[0] = -(-1.0); where -1.0 is a negative number, i.e. rates[0] = 1.0; auto parser = libcellml::Parser::create(); auto model = parser->parseModel(fileContents("generator/unary_minus/example_01.cellml")); @@ -37,7 +37,7 @@ TEST(GeneratorUnaryMinus, example01) auto implementationCode = generator->implementationCode(analyser->analyserModel()); - EXPECT_TRUE(implementationCode.find("rates[0] = -(-1.0);\n") != std::string::npos); + EXPECT_TRUE(implementationCode.find("rates[0] = 1.0;\n") != std::string::npos); } TEST(GeneratorUnaryMinus, example02) @@ -82,7 +82,7 @@ TEST(GeneratorUnaryMinus, example03) TEST(GeneratorUnaryMinus, example04) { - // rates[0] = -(-1.0); + // rates[0] = -(-1.0); where -1.0 is a unary minus applied to 1.0, i.e. rates[0] = 1.0; auto parser = libcellml::Parser::create(); auto model = parser->parseModel(fileContents("generator/unary_minus/example_04.cellml")); @@ -97,7 +97,7 @@ TEST(GeneratorUnaryMinus, example04) auto implementationCode = generator->implementationCode(analyser->analyserModel()); - EXPECT_TRUE(implementationCode.find("rates[0] = -(-1.0);\n") != std::string::npos); + EXPECT_TRUE(implementationCode.find("rates[0] = 1.0;\n") != std::string::npos); } TEST(GeneratorUnaryMinus, example05) @@ -142,7 +142,7 @@ TEST(GeneratorUnaryMinus, example06) TEST(GeneratorUnaryMinus, example07) { - // rates[0] = constants[1]-(-constants[0]); + // rates[0] = constants[1]-(-constants[0]), i.e. rates[0] = constants[1]+constants[0]; auto parser = libcellml::Parser::create(); auto model = parser->parseModel(fileContents("generator/unary_minus/example_07.cellml")); @@ -157,7 +157,7 @@ TEST(GeneratorUnaryMinus, example07) auto implementationCode = generator->implementationCode(analyser->analyserModel()); - EXPECT_TRUE(implementationCode.find("rates[0] = constants[1]-(-constants[0]);\n") != std::string::npos); + EXPECT_TRUE(implementationCode.find("rates[0] = constants[1]+constants[0];\n") != std::string::npos); } TEST(GeneratorUnaryPlus, operandWithParentheses) @@ -186,7 +186,7 @@ TEST(GeneratorUnaryPlus, operandWithParentheses) EXPECT_NE(std::string::npos, implementationCode.find("algebraicVariables[7] = voi+(voi > 5.5);\n")); EXPECT_NE(std::string::npos, implementationCode.find("algebraicVariables[8] = -(voi+constants[0]);\n")); EXPECT_NE(std::string::npos, implementationCode.find("algebraicVariables[9] = -voi*constants[0];\n")); - EXPECT_NE(std::string::npos, implementationCode.find("algebraicVariables[10] = voi-(-constants[0]);\n")); + EXPECT_NE(std::string::npos, implementationCode.find("algebraicVariables[10] = voi+constants[0];\n")); EXPECT_NE(std::string::npos, implementationCode.find("algebraicVariables[11] = pow(constants[0], 1.0/(voi+constants[0]));\n")); EXPECT_NE(std::string::npos, implementationCode.find("algebraicVariables[12] = constants[0] || ((voi > 5.5) && constants[0]);\n")); @@ -202,7 +202,75 @@ TEST(GeneratorUnaryPlus, operandWithParentheses) EXPECT_NE(std::string::npos, implementationCode.find("algebraic_variables[7] = voi+gt_func(voi, 5.5)\n")); EXPECT_NE(std::string::npos, implementationCode.find("algebraic_variables[8] = -(voi+constants[0])\n")); EXPECT_NE(std::string::npos, implementationCode.find("algebraic_variables[9] = -voi*constants[0]\n")); - EXPECT_NE(std::string::npos, implementationCode.find("algebraic_variables[10] = voi-(-constants[0])\n")); + EXPECT_NE(std::string::npos, implementationCode.find("algebraic_variables[10] = voi+constants[0]\n")); EXPECT_NE(std::string::npos, implementationCode.find("algebraic_variables[11] = pow(constants[0], 1.0/(voi+constants[0]))\n")); EXPECT_NE(std::string::npos, implementationCode.find("algebraic_variables[12] = or_func(constants[0], and_func(gt_func(voi, 5.5), constants[0]))\n")); } + +TEST(GeneratorUnary, unaryMinusDoubleNegation) +{ + auto parser = libcellml::Parser::create(); + auto model = parser->parseModel(fileContents("generator/cellml_unary_minus_double_negation/model.cellml")); + + EXPECT_NE(nullptr, model); + + auto analyser = libcellml::Analyser::create(); + + analyser->analyseModel(model); + + EXPECT_EQ(size_t(0), analyser->issueCount()); + + auto analyserModel = analyser->analyserModel(); + auto generator = libcellml::Generator::create(); + + EXPECT_EQ_FILE_CONTENTS("generator/cellml_unary_minus_double_negation/model.h", generator->interfaceCode(analyserModel)); + EXPECT_EQ_FILE_CONTENTS("generator/cellml_unary_minus_double_negation/model.c", generator->implementationCode(analyserModel)); + + auto profile = libcellml::GeneratorProfile::create(libcellml::GeneratorProfile::Profile::PYTHON); + + EXPECT_EQ_FILE_CONTENTS("generator/cellml_unary_minus_double_negation/model.py", generator->implementationCode(analyserModel, profile)); + + profile = libcellml::GeneratorProfile::create(); + + profile->setHasPowerOperator(true); + profile->setPowerString("^"); + + EXPECT_EQ_FILE_CONTENTS("generator/cellml_unary_minus_double_negation/model_power.out", generator->implementationCode(analyserModel, profile)); + + profile->setHasNotOperator(false); + profile->setNotString("not"); + + EXPECT_EQ_FILE_CONTENTS("generator/cellml_unary_minus_double_negation/model_power_not.out", generator->implementationCode(analyserModel, profile)); +} + +TEST(GeneratorUnary, unaryMinusNegativeNumberOperand) +{ + auto parser = libcellml::Parser::create(); + auto model = parser->parseModel(fileContents("generator/cellml_unary_minus_negative_number_operand/model.cellml")); + + EXPECT_NE(nullptr, model); + + auto analyser = libcellml::Analyser::create(); + + analyser->analyseModel(model); + + EXPECT_EQ(size_t(0), analyser->issueCount()); + + auto analyserModel = analyser->analyserModel(); + auto generator = libcellml::Generator::create(); + + EXPECT_EQ_FILE_CONTENTS("generator/cellml_unary_minus_negative_number_operand/model.h", generator->interfaceCode(analyserModel)); + EXPECT_EQ_FILE_CONTENTS("generator/cellml_unary_minus_negative_number_operand/model.c", generator->implementationCode(analyserModel)); + + auto profile = libcellml::GeneratorProfile::create(libcellml::GeneratorProfile::Profile::PYTHON); + + EXPECT_EQ_FILE_CONTENTS("generator/cellml_unary_minus_negative_number_operand/model.py", generator->implementationCode(analyserModel, profile)); + + // A profile with a minus string other than "-", but where a negative number still starts with "-". + + profile = libcellml::GeneratorProfile::create(); + + profile->setMinusString(" - "); + + EXPECT_EQ_FILE_CONTENTS("generator/cellml_unary_minus_negative_number_operand/model.out", generator->implementationCode(analyserModel, profile)); +} diff --git a/tests/resources/coverage/generator/model.c b/tests/resources/coverage/generator/model.c index 72713e984..e7402eb3a 100644 --- a/tests/resources/coverage/generator/model.c +++ b/tests/resources/coverage/generator/model.c @@ -522,7 +522,7 @@ void computeComputedConstants(double voi, double *states, double *rates, double computedConstants[65] = (constants[1] < constants[0])-(constants[2] > constants[3]); computedConstants[66] = (constants[1] < constants[0])-(constants[2]+constants[3]); computedConstants[67] = (constants[1] < constants[0])-constants[2]; - computedConstants[68] = constants[1]-(-constants[0]); + computedConstants[68] = constants[1]+constants[0]; computedConstants[69] = constants[1]-(-constants[0]*constants[2]); computedConstants[70] = -constants[1]; computedConstants[71] = -(constants[1] < constants[0]); diff --git a/tests/resources/coverage/generator/model.implementation.out b/tests/resources/coverage/generator/model.implementation.out index bb69e81ae..31afb2f87 100644 --- a/tests/resources/coverage/generator/model.implementation.out +++ b/tests/resources/coverage/generator/model.implementation.out @@ -281,7 +281,7 @@ void computeComputedConstants(double voi, double *states, double *rates, double computedConstants[65] = lt(constants[1], constants[0])-gt(constants[2], constants[3]); computedConstants[66] = lt(constants[1], constants[0])-(constants[2]+constants[3]); computedConstants[67] = lt(constants[1], constants[0])-constants[2]; - computedConstants[68] = constants[1]-(-constants[0]); + computedConstants[68] = constants[1]+constants[0]; computedConstants[69] = constants[1]-(-constants[0]*constants[2]); computedConstants[70] = -constants[1]; computedConstants[71] = -lt(constants[1], constants[0]); diff --git a/tests/resources/coverage/generator/model.modified.profile.c b/tests/resources/coverage/generator/model.modified.profile.c index f19fef4ac..1e33467ec 100644 --- a/tests/resources/coverage/generator/model.modified.profile.c +++ b/tests/resources/coverage/generator/model.modified.profile.c @@ -522,7 +522,7 @@ void computeComputedConstants(double voi, double *states, double *rates, double computedConstants[65] = (constants[1] < constants[0])-(constants[2] > constants[3]); computedConstants[66] = (constants[1] < constants[0])-(constants[2]+constants[3]); computedConstants[67] = (constants[1] < constants[0])-constants[2]; - computedConstants[68] = constants[1]-(-constants[0]); + computedConstants[68] = constants[1]+constants[0]; computedConstants[69] = constants[1]-(-constants[0]*constants[2]); computedConstants[70] = -constants[1]; computedConstants[71] = -(constants[1] < constants[0]); diff --git a/tests/resources/coverage/generator/model.modified.profile.py b/tests/resources/coverage/generator/model.modified.profile.py index 07b3f8d02..be37b2b98 100644 --- a/tests/resources/coverage/generator/model.modified.profile.py +++ b/tests/resources/coverage/generator/model.modified.profile.py @@ -488,7 +488,7 @@ def compute_computed_constants(voi, states, rates, constants, computed_constants computed_constants[65] = lt_func(constants[1], constants[0])-gt_func(constants[2], constants[3]) computed_constants[66] = lt_func(constants[1], constants[0])-(constants[2]+constants[3]) computed_constants[67] = lt_func(constants[1], constants[0])-constants[2] - computed_constants[68] = constants[1]-(-constants[0]) + computed_constants[68] = constants[1]+constants[0] computed_constants[69] = constants[1]-(-constants[0]*constants[2]) computed_constants[70] = -constants[1] computed_constants[71] = -lt_func(constants[1], constants[0]) diff --git a/tests/resources/coverage/generator/model.out b/tests/resources/coverage/generator/model.out index bf3f2cd06..caa14af40 100644 --- a/tests/resources/coverage/generator/model.out +++ b/tests/resources/coverage/generator/model.out @@ -192,7 +192,7 @@ void computeComputedConstants(double voi, double *states, double *rates, double computedConstants[65] = (constants[1] < constants[0])-(constants[2] > constants[3]); computedConstants[66] = (constants[1] < constants[0])-(constants[2]+constants[3]); computedConstants[67] = (constants[1] < constants[0])-constants[2]; - computedConstants[68] = constants[1]-(-constants[0]); + computedConstants[68] = constants[1]+constants[0]; computedConstants[69] = constants[1]-(-constants[0]*constants[2]); computedConstants[70] = -constants[1]; computedConstants[71] = -(constants[1] < constants[0]); diff --git a/tests/resources/coverage/generator/model.py b/tests/resources/coverage/generator/model.py index 5848aff4a..de9ea31ff 100644 --- a/tests/resources/coverage/generator/model.py +++ b/tests/resources/coverage/generator/model.py @@ -488,7 +488,7 @@ def compute_computed_constants(voi, states, rates, constants, computed_constants computed_constants[65] = lt_func(constants[1], constants[0])-gt_func(constants[2], constants[3]) computed_constants[66] = lt_func(constants[1], constants[0])-(constants[2]+constants[3]) computed_constants[67] = lt_func(constants[1], constants[0])-constants[2] - computed_constants[68] = constants[1]-(-constants[0]) + computed_constants[68] = constants[1]+constants[0] computed_constants[69] = constants[1]-(-constants[0]*constants[2]) computed_constants[70] = -constants[1] computed_constants[71] = -lt_func(constants[1], constants[0]) diff --git a/tests/resources/generator/algebraic_system_with_three_linked_unknowns/model.c b/tests/resources/generator/algebraic_system_with_three_linked_unknowns/model.c index 13bb31460..2a0b6da30 100644 --- a/tests/resources/generator/algebraic_system_with_three_linked_unknowns/model.c +++ b/tests/resources/generator/algebraic_system_with_three_linked_unknowns/model.c @@ -81,7 +81,7 @@ void objectiveFunction0(double *u, double *f, void *data) algebraicVariables[1] = u[1]; algebraicVariables[2] = u[2]; - f[0] = 2.0*algebraicVariables[2]+algebraicVariables[1]-2.0*algebraicVariables[0]-(-1.0); + f[0] = 2.0*algebraicVariables[2]+algebraicVariables[1]-2.0*algebraicVariables[0]+1.0; f[1] = 3.0*algebraicVariables[2]-3.0*algebraicVariables[1]-algebraicVariables[0]-5.0; f[2] = algebraicVariables[2]-2.0*algebraicVariables[1]+3.0*algebraicVariables[0]-6.0; } diff --git a/tests/resources/generator/algebraic_system_with_three_linked_unknowns/model.py b/tests/resources/generator/algebraic_system_with_three_linked_unknowns/model.py index be015adbe..3123a6174 100644 --- a/tests/resources/generator/algebraic_system_with_three_linked_unknowns/model.py +++ b/tests/resources/generator/algebraic_system_with_three_linked_unknowns/model.py @@ -48,7 +48,7 @@ def objective_function_0(u, f, data): algebraic_variables[1] = u[1] algebraic_variables[2] = u[2] - f[0] = 2.0*algebraic_variables[2]+algebraic_variables[1]-2.0*algebraic_variables[0]-(-1.0) + f[0] = 2.0*algebraic_variables[2]+algebraic_variables[1]-2.0*algebraic_variables[0]+1.0 f[1] = 3.0*algebraic_variables[2]-3.0*algebraic_variables[1]-algebraic_variables[0]-5.0 f[2] = algebraic_variables[2]-2.0*algebraic_variables[1]+3.0*algebraic_variables[0]-6.0 diff --git a/tests/resources/generator/cellml_unary_minus_double_negation/model.c b/tests/resources/generator/cellml_unary_minus_double_negation/model.c new file mode 100644 index 000000000..adab6d88f --- /dev/null +++ b/tests/resources/generator/cellml_unary_minus_double_negation/model.c @@ -0,0 +1,212 @@ +/* The content of this file was generated using the C profile of libCellML 0.7.2. */ + +#include "model.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 = 1; +const size_t COMPUTED_CONSTANT_COUNT = 4; +const size_t ALGEBRAIC_VARIABLE_COUNT = 53; + +const VariableInfo VOI_INFO = {"t", "dimensionless", "c"}; + +const VariableInfo STATE_INFO[] = { + {"x", "dimensionless", "c"} +}; + +const VariableInfo CONSTANT_INFO[] = { + {"x0", "dimensionless", "c"} +}; + +const VariableInfo COMPUTED_CONSTANT_INFO[] = { + {"x2", "dimensionless", "c"}, + {"x3", "dimensionless", "c"}, + {"x9", "dimensionless", "c"}, + {"x52", "dimensionless", "c"} +}; + +const VariableInfo ALGEBRAIC_VARIABLE_INFO[] = { + {"x1", "dimensionless", "c"}, + {"x4", "dimensionless", "c"}, + {"x5", "dimensionless", "c"}, + {"x6", "dimensionless", "c"}, + {"x7", "dimensionless", "c"}, + {"x8", "dimensionless", "c"}, + {"x10", "dimensionless", "c"}, + {"x11", "dimensionless", "c"}, + {"x12", "dimensionless", "c"}, + {"x13", "dimensionless", "c"}, + {"x14", "dimensionless", "c"}, + {"x15", "dimensionless", "c"}, + {"x16", "dimensionless", "c"}, + {"x17", "dimensionless", "c"}, + {"x18", "dimensionless", "c"}, + {"x19", "dimensionless", "c"}, + {"x20", "dimensionless", "c"}, + {"x21", "dimensionless", "c"}, + {"x22", "dimensionless", "c"}, + {"x23", "dimensionless", "c"}, + {"x24", "dimensionless", "c"}, + {"x25", "dimensionless", "c"}, + {"x26", "dimensionless", "c"}, + {"x27", "dimensionless", "c"}, + {"x28", "dimensionless", "c"}, + {"x29", "dimensionless", "c"}, + {"x30", "dimensionless", "c"}, + {"x31", "dimensionless", "c"}, + {"x32", "dimensionless", "c"}, + {"x33", "dimensionless", "c"}, + {"x34", "dimensionless", "c"}, + {"x35", "dimensionless", "c"}, + {"x36", "dimensionless", "c"}, + {"x37", "dimensionless", "c"}, + {"x38", "dimensionless", "c"}, + {"x39", "dimensionless", "c"}, + {"x40", "dimensionless", "c"}, + {"x41", "dimensionless", "c"}, + {"x42", "dimensionless", "c"}, + {"x43", "dimensionless", "c"}, + {"x44", "dimensionless", "c"}, + {"x45", "dimensionless", "c"}, + {"x46", "dimensionless", "c"}, + {"x47", "dimensionless", "c"}, + {"x48", "dimensionless", "c"}, + {"x49", "dimensionless", "c"}, + {"x50", "dimensionless", "c"}, + {"x51", "dimensionless", "c"}, + {"x53", "dimensionless", "c"}, + {"x54", "dimensionless", "c"}, + {"x55", "dimensionless", "c"}, + {"x56", "dimensionless", "c"}, + {"x57", "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] = 3.0; + computedConstants[2] = -3.0; +} + +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + computedConstants[0] = constants[0]; + computedConstants[1] = -constants[0]; + computedConstants[3] = pow(!constants[0], 1.0/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) +{ + algebraicVariables[0] = voi+constants[0]; + algebraicVariables[1] = voi-constants[0]; + algebraicVariables[2] = voi+constants[0]; + algebraicVariables[3] = voi-constants[0]; + algebraicVariables[4] = voi+3.0; + algebraicVariables[5] = voi-3.0; + algebraicVariables[6] = voi+(voi+constants[0]); + algebraicVariables[7] = voi+(voi-constants[0]); + algebraicVariables[8] = voi+voi*constants[0]; + algebraicVariables[9] = voi+(voi > 5.5); + algebraicVariables[10] = voi*(voi+constants[0]); + algebraicVariables[11] = voi/(voi*constants[0]); + algebraicVariables[12] = (voi-constants[0])*voi; + algebraicVariables[13] = voi-(voi+constants[0]); + algebraicVariables[14] = -(voi+constants[0]); + algebraicVariables[15] = !(voi+constants[0]); + algebraicVariables[16] = (voi > 5.5) && constants[0]; + algebraicVariables[17] = constants[0]-voi; + algebraicVariables[18] = voi+((voi > 5.5) && constants[0]); + algebraicVariables[19] = voi+((voi > 5.5)?constants[0]:1.0); + algebraicVariables[20] = voi+(voi+constants[0]); + algebraicVariables[21] = voi+voi*constants[0]; + algebraicVariables[22] = voi*(voi*constants[0]); + algebraicVariables[23] = voi*(voi/constants[0]); + algebraicVariables[24] = voi < (voi > 5.5); + algebraicVariables[25] = (voi > 5.5)?constants[0]:1.0; + algebraicVariables[26] = voi+constants[0]; + algebraicVariables[27] = sin(voi+constants[0]); + algebraicVariables[28] = pow(voi+constants[0], voi*constants[0]); + algebraicVariables[29] = sqrt(voi+constants[0]); + algebraicVariables[30] = pow(voi+constants[0], 1.0/(voi*constants[0])); + algebraicVariables[31] = voi*((voi > 5.5) && constants[0]); + algebraicVariables[32] = voi*!constants[0]; + algebraicVariables[33] = voi*pow(voi, constants[0]); + algebraicVariables[34] = voi*pow(voi, 1.0/constants[0]); + algebraicVariables[35] = voi*((voi > 5.5)?constants[0]:1.0); + algebraicVariables[36] = voi+(voi-constants[0]); + algebraicVariables[37] = (voi > 5.5) < constants[0]; + algebraicVariables[38] = ((voi > 5.5) && constants[0]) < constants[0]; + algebraicVariables[39] = ((voi > 5.5)?constants[0]:1.0) < constants[0]; + algebraicVariables[40] = voi+constants[0] < voi; + algebraicVariables[41] = voi < ((voi > 5.5) && constants[0]); + algebraicVariables[42] = voi < ((voi > 5.5)?constants[0]:1.0); + algebraicVariables[43] = voi < voi*constants[0]; + algebraicVariables[44] = pow(!constants[0], voi); + algebraicVariables[45] = pow(pow(voi, constants[0]), voi); + algebraicVariables[46] = pow(pow(voi, 1.0/constants[0]), voi); + algebraicVariables[47] = pow(sin(voi), voi); + algebraicVariables[48] = pow(pow(voi, constants[0]), 1.0/constants[0]); + algebraicVariables[49] = pow(pow(voi, 1.0/constants[0]), 1.0/constants[0]); + algebraicVariables[50] = pow(sin(voi), 1.0/constants[0]); + algebraicVariables[51] = (voi > 5.5)?((voi > 5.5)?constants[0]:1.0):2.0; + algebraicVariables[52] = ((voi > 5.5)?1.0:0.0)?constants[0]:2.0; +} diff --git a/tests/resources/generator/cellml_unary_minus_double_negation/model.cellml b/tests/resources/generator/cellml_unary_minus_double_negation/model.cellml new file mode 100644 index 000000000..c5c71339c --- /dev/null +++ b/tests/resources/generator/cellml_unary_minus_double_negation/model.cellml @@ -0,0 +1,1254 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + t + + x + + 1 + + + + x1 + + + t + + + x0 + + + + + + x2 + + + + + x0 + + + + + + x3 + + + + + + + x0 + + + + + + + x4 + + + t + + + + + x0 + + + + + + + x5 + + + t + + + + + + + x0 + + + + + + + + x6 + + + t + + + + + + + + + x0 + + + + + + + + + x7 + + + t + -3 + + + + + x8 + + + t + + + -3 + + + + + + x9 + + + + + -3 + + + + + + x10 + + + t + + + + + t + x0 + + + + + + + x11 + + + t + + + + + t + x0 + + + + + + + x12 + + + t + + + + + t + x0 + + + + + + + x13 + + + t + + + + + t + 5.5 + + + + + + + x14 + + + t + + + + + + + t + x0 + + + + + + + + x15 + + + t + + + + + + + t + x0 + + + + + + + + x16 + + + + + + + + + t + x0 + + + + t + + + + + x17 + + + t + + + + + + + t + x0 + + + + + + + + x18 + + + + + + + + + t + x0 + + + + + + + + x19 + + + + + + + + + t + x0 + + + + + + + + x20 + + + + + + + + + t + 5.5 + + + + x0 + + + + + x21 + + + + + + + x0 + + + t + + + + + x22 + + + t + + + + + + + t + 5.5 + + x0 + + + + + + + x23 + + + t + + + + + x0 + + + t + 5.5 + + + + 1 + + + + + + + + x24 + + + t + + + + + + + t + x0 + + + + + + + + x25 + + + t + + + + + + + t + x0 + + + + + + + + x26 + + + t + + + + + + + t + x0 + + + + + + + + x27 + + + t + + + + + + + t + x0 + + + + + + + + x28 + + + t + + + + + + + t + 5.5 + + + + + + + + x29 + + + x0 + + + + + + + t + 5.5 + + + + + + 1 + + + + + + x30 + + + + + + + t + x0 + + + + + + + x31 + + + + + + + + + t + x0 + + + + + + + + x32 + + + + + + + + + t + x0 + + + + + + + + + + t + x0 + + + + + + + + x33 + + + + + + + + + t + x0 + + + + + + + + x34 + + + + + + + + + + t + x0 + + + + + + + + + + + t + x0 + + + + + + + + x35 + + + t + + + + + + + + + t + 5.5 + + x0 + + + + + + + + x36 + + + t + + + + + + + x0 + + + + + + + + x37 + + + t + + + + + + + t + x0 + + + + + + + + x38 + + + t + + + + + + + + x0 + + t + + + + + + + + x39 + + + t + + + + + + + x0 + + + t + 5.5 + + + + 1 + + + + + + + + + x40 + + + t + + + + + + + t + x0 + + + + + + + + x41 + + + + + + + + + t + 5.5 + + + + x0 + + + + + x42 + + + + + + + + + + + t + 5.5 + + x0 + + + + x0 + + + + + x43 + + + + + + + + + x0 + + + t + 5.5 + + + + 1 + + + + + x0 + + + + + x44 + + + + + + + + + t + x0 + + + + t + + + + + x45 + + + t + + + + + + + + + t + 5.5 + + x0 + + + + + + + + x46 + + + t + + + + + + + x0 + + + t + 5.5 + + + + 1 + + + + + + + + + x47 + + + t + + + + + + + t + x0 + + + + + + + + x48 + + + + + + + + + x0 + + + + t + + + + + x49 + + + + + + + + + t + x0 + + + + t + + + + + x50 + + + + + + + + + + x0 + + t + + + + t + + + + + x51 + + + + + + + + + t + + + + t + + + + + x52 + + + + x0 + + + + + + + + x0 + + + + + + + + x53 + + + + x0 + + + + + + + + t + x0 + + + + + + + + x54 + + + + x0 + + + + + + + + + x0 + + t + + + + + + + + x55 + + + + x0 + + + + + + + + t + + + + + + + + x56 + + + + + + + + + x0 + + + t + 5.5 + + + + 1 + + + + + + + t + 5.5 + + + + 2 + + + + + + x57 + + + x0 + + + + + + + 1 + + + t + 5.5 + + + + 0 + + + + + + + 2 + + + + + + diff --git a/tests/resources/generator/cellml_unary_minus_double_negation/model.h b/tests/resources/generator/cellml_unary_minus_double_negation/model.h new file mode 100644 index 000000000..ff49002cc --- /dev/null +++ b/tests/resources/generator/cellml_unary_minus_double_negation/model.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[4]; + 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_minus_double_negation/model.py b/tests/resources/generator/cellml_unary_minus_double_negation/model.py new file mode 100644 index 000000000..2e84d4528 --- /dev/null +++ b/tests/resources/generator/cellml_unary_minus_double_negation/model.py @@ -0,0 +1,190 @@ +# 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 = 1 +COMPUTED_CONSTANT_COUNT = 4 +ALGEBRAIC_VARIABLE_COUNT = 53 + +VOI_INFO = {"name": "t", "units": "dimensionless", "component": "c"} + +STATE_INFO = [ + {"name": "x", "units": "dimensionless", "component": "c"} +] + +CONSTANT_INFO = [ + {"name": "x0", "units": "dimensionless", "component": "c"} +] + +COMPUTED_CONSTANT_INFO = [ + {"name": "x2", "units": "dimensionless", "component": "c"}, + {"name": "x3", "units": "dimensionless", "component": "c"}, + {"name": "x9", "units": "dimensionless", "component": "c"}, + {"name": "x52", "units": "dimensionless", "component": "c"} +] + +ALGEBRAIC_VARIABLE_INFO = [ + {"name": "x1", "units": "dimensionless", "component": "c"}, + {"name": "x4", "units": "dimensionless", "component": "c"}, + {"name": "x5", "units": "dimensionless", "component": "c"}, + {"name": "x6", "units": "dimensionless", "component": "c"}, + {"name": "x7", "units": "dimensionless", "component": "c"}, + {"name": "x8", "units": "dimensionless", "component": "c"}, + {"name": "x10", "units": "dimensionless", "component": "c"}, + {"name": "x11", "units": "dimensionless", "component": "c"}, + {"name": "x12", "units": "dimensionless", "component": "c"}, + {"name": "x13", "units": "dimensionless", "component": "c"}, + {"name": "x14", "units": "dimensionless", "component": "c"}, + {"name": "x15", "units": "dimensionless", "component": "c"}, + {"name": "x16", "units": "dimensionless", "component": "c"}, + {"name": "x17", "units": "dimensionless", "component": "c"}, + {"name": "x18", "units": "dimensionless", "component": "c"}, + {"name": "x19", "units": "dimensionless", "component": "c"}, + {"name": "x20", "units": "dimensionless", "component": "c"}, + {"name": "x21", "units": "dimensionless", "component": "c"}, + {"name": "x22", "units": "dimensionless", "component": "c"}, + {"name": "x23", "units": "dimensionless", "component": "c"}, + {"name": "x24", "units": "dimensionless", "component": "c"}, + {"name": "x25", "units": "dimensionless", "component": "c"}, + {"name": "x26", "units": "dimensionless", "component": "c"}, + {"name": "x27", "units": "dimensionless", "component": "c"}, + {"name": "x28", "units": "dimensionless", "component": "c"}, + {"name": "x29", "units": "dimensionless", "component": "c"}, + {"name": "x30", "units": "dimensionless", "component": "c"}, + {"name": "x31", "units": "dimensionless", "component": "c"}, + {"name": "x32", "units": "dimensionless", "component": "c"}, + {"name": "x33", "units": "dimensionless", "component": "c"}, + {"name": "x34", "units": "dimensionless", "component": "c"}, + {"name": "x35", "units": "dimensionless", "component": "c"}, + {"name": "x36", "units": "dimensionless", "component": "c"}, + {"name": "x37", "units": "dimensionless", "component": "c"}, + {"name": "x38", "units": "dimensionless", "component": "c"}, + {"name": "x39", "units": "dimensionless", "component": "c"}, + {"name": "x40", "units": "dimensionless", "component": "c"}, + {"name": "x41", "units": "dimensionless", "component": "c"}, + {"name": "x42", "units": "dimensionless", "component": "c"}, + {"name": "x43", "units": "dimensionless", "component": "c"}, + {"name": "x44", "units": "dimensionless", "component": "c"}, + {"name": "x45", "units": "dimensionless", "component": "c"}, + {"name": "x46", "units": "dimensionless", "component": "c"}, + {"name": "x47", "units": "dimensionless", "component": "c"}, + {"name": "x48", "units": "dimensionless", "component": "c"}, + {"name": "x49", "units": "dimensionless", "component": "c"}, + {"name": "x50", "units": "dimensionless", "component": "c"}, + {"name": "x51", "units": "dimensionless", "component": "c"}, + {"name": "x53", "units": "dimensionless", "component": "c"}, + {"name": "x54", "units": "dimensionless", "component": "c"}, + {"name": "x55", "units": "dimensionless", "component": "c"}, + {"name": "x56", "units": "dimensionless", "component": "c"}, + {"name": "x57", "units": "dimensionless", "component": "c"} +] + + +def lt_func(x, y): + return 1.0 if x < y else 0.0 + + +def gt_func(x, y): + return 1.0 if x > y else 0.0 + + +def and_func(x, y): + return 1.0 if bool(x) & bool(y) else 0.0 + + +def not_func(x): + return 1.0 if not bool(x) else 0.0 + + +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 + computed_constants[2] = -3.0 + + +def compute_computed_constants(voi, states, rates, constants, computed_constants, algebraic_variables): + computed_constants[0] = constants[0] + computed_constants[1] = -constants[0] + computed_constants[3] = pow(not_func(constants[0]), 1.0/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): + algebraic_variables[0] = voi+constants[0] + algebraic_variables[1] = voi-constants[0] + algebraic_variables[2] = voi+constants[0] + algebraic_variables[3] = voi-constants[0] + algebraic_variables[4] = voi+3.0 + algebraic_variables[5] = voi-3.0 + algebraic_variables[6] = voi+(voi+constants[0]) + algebraic_variables[7] = voi+(voi-constants[0]) + algebraic_variables[8] = voi+voi*constants[0] + algebraic_variables[9] = voi+gt_func(voi, 5.5) + algebraic_variables[10] = voi*(voi+constants[0]) + algebraic_variables[11] = voi/(voi*constants[0]) + algebraic_variables[12] = (voi-constants[0])*voi + algebraic_variables[13] = voi-(voi+constants[0]) + algebraic_variables[14] = -(voi+constants[0]) + algebraic_variables[15] = not_func(voi+constants[0]) + algebraic_variables[16] = and_func(gt_func(voi, 5.5), constants[0]) + algebraic_variables[17] = constants[0]-voi + algebraic_variables[18] = voi+and_func(gt_func(voi, 5.5), constants[0]) + algebraic_variables[19] = voi+(constants[0] if gt_func(voi, 5.5) else 1.0) + algebraic_variables[20] = voi+(voi+constants[0]) + algebraic_variables[21] = voi+voi*constants[0] + algebraic_variables[22] = voi*(voi*constants[0]) + algebraic_variables[23] = voi*(voi/constants[0]) + algebraic_variables[24] = lt_func(voi, gt_func(voi, 5.5)) + algebraic_variables[25] = constants[0] if gt_func(voi, 5.5) else 1.0 + algebraic_variables[26] = voi+constants[0] + algebraic_variables[27] = sin(voi+constants[0]) + algebraic_variables[28] = pow(voi+constants[0], voi*constants[0]) + algebraic_variables[29] = sqrt(voi+constants[0]) + algebraic_variables[30] = pow(voi+constants[0], 1.0/(voi*constants[0])) + algebraic_variables[31] = voi*and_func(gt_func(voi, 5.5), constants[0]) + algebraic_variables[32] = voi*not_func(constants[0]) + algebraic_variables[33] = voi*pow(voi, constants[0]) + algebraic_variables[34] = voi*pow(voi, 1.0/constants[0]) + algebraic_variables[35] = voi*(constants[0] if gt_func(voi, 5.5) else 1.0) + algebraic_variables[36] = voi+(voi-constants[0]) + algebraic_variables[37] = lt_func(gt_func(voi, 5.5), constants[0]) + algebraic_variables[38] = lt_func(and_func(gt_func(voi, 5.5), constants[0]), constants[0]) + algebraic_variables[39] = lt_func(constants[0] if gt_func(voi, 5.5) else 1.0, constants[0]) + algebraic_variables[40] = lt_func(voi+constants[0], voi) + algebraic_variables[41] = lt_func(voi, and_func(gt_func(voi, 5.5), constants[0])) + algebraic_variables[42] = lt_func(voi, constants[0] if gt_func(voi, 5.5) else 1.0) + algebraic_variables[43] = lt_func(voi, voi*constants[0]) + algebraic_variables[44] = pow(not_func(constants[0]), voi) + algebraic_variables[45] = pow(pow(voi, constants[0]), voi) + algebraic_variables[46] = pow(pow(voi, 1.0/constants[0]), voi) + algebraic_variables[47] = pow(sin(voi), voi) + algebraic_variables[48] = pow(pow(voi, constants[0]), 1.0/constants[0]) + algebraic_variables[49] = pow(pow(voi, 1.0/constants[0]), 1.0/constants[0]) + algebraic_variables[50] = pow(sin(voi), 1.0/constants[0]) + algebraic_variables[51] = (constants[0] if gt_func(voi, 5.5) else 1.0) if gt_func(voi, 5.5) else 2.0 + algebraic_variables[52] = constants[0] if (1.0 if gt_func(voi, 5.5) else 0.0) else 2.0 diff --git a/tests/resources/generator/cellml_unary_minus_double_negation/model_power.out b/tests/resources/generator/cellml_unary_minus_double_negation/model_power.out new file mode 100644 index 000000000..0aa5759da --- /dev/null +++ b/tests/resources/generator/cellml_unary_minus_double_negation/model_power.out @@ -0,0 +1,212 @@ +/* The content of this file was generated using a modified C profile of libCellML 0.7.2. */ + +#include "model.h" + +#include +#include + +const char VERSION[] = "0.8.1.post0"; +const char LIBCELLML_VERSION[] = "0.7.2"; + +const size_t STATE_COUNT = 1; +const size_t CONSTANT_COUNT = 1; +const size_t COMPUTED_CONSTANT_COUNT = 4; +const size_t ALGEBRAIC_VARIABLE_COUNT = 53; + +const VariableInfo VOI_INFO = {"t", "dimensionless", "c"}; + +const VariableInfo STATE_INFO[] = { + {"x", "dimensionless", "c"} +}; + +const VariableInfo CONSTANT_INFO[] = { + {"x0", "dimensionless", "c"} +}; + +const VariableInfo COMPUTED_CONSTANT_INFO[] = { + {"x2", "dimensionless", "c"}, + {"x3", "dimensionless", "c"}, + {"x9", "dimensionless", "c"}, + {"x52", "dimensionless", "c"} +}; + +const VariableInfo ALGEBRAIC_VARIABLE_INFO[] = { + {"x1", "dimensionless", "c"}, + {"x4", "dimensionless", "c"}, + {"x5", "dimensionless", "c"}, + {"x6", "dimensionless", "c"}, + {"x7", "dimensionless", "c"}, + {"x8", "dimensionless", "c"}, + {"x10", "dimensionless", "c"}, + {"x11", "dimensionless", "c"}, + {"x12", "dimensionless", "c"}, + {"x13", "dimensionless", "c"}, + {"x14", "dimensionless", "c"}, + {"x15", "dimensionless", "c"}, + {"x16", "dimensionless", "c"}, + {"x17", "dimensionless", "c"}, + {"x18", "dimensionless", "c"}, + {"x19", "dimensionless", "c"}, + {"x20", "dimensionless", "c"}, + {"x21", "dimensionless", "c"}, + {"x22", "dimensionless", "c"}, + {"x23", "dimensionless", "c"}, + {"x24", "dimensionless", "c"}, + {"x25", "dimensionless", "c"}, + {"x26", "dimensionless", "c"}, + {"x27", "dimensionless", "c"}, + {"x28", "dimensionless", "c"}, + {"x29", "dimensionless", "c"}, + {"x30", "dimensionless", "c"}, + {"x31", "dimensionless", "c"}, + {"x32", "dimensionless", "c"}, + {"x33", "dimensionless", "c"}, + {"x34", "dimensionless", "c"}, + {"x35", "dimensionless", "c"}, + {"x36", "dimensionless", "c"}, + {"x37", "dimensionless", "c"}, + {"x38", "dimensionless", "c"}, + {"x39", "dimensionless", "c"}, + {"x40", "dimensionless", "c"}, + {"x41", "dimensionless", "c"}, + {"x42", "dimensionless", "c"}, + {"x43", "dimensionless", "c"}, + {"x44", "dimensionless", "c"}, + {"x45", "dimensionless", "c"}, + {"x46", "dimensionless", "c"}, + {"x47", "dimensionless", "c"}, + {"x48", "dimensionless", "c"}, + {"x49", "dimensionless", "c"}, + {"x50", "dimensionless", "c"}, + {"x51", "dimensionless", "c"}, + {"x53", "dimensionless", "c"}, + {"x54", "dimensionless", "c"}, + {"x55", "dimensionless", "c"}, + {"x56", "dimensionless", "c"}, + {"x57", "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] = 3.0; + computedConstants[2] = -3.0; +} + +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + computedConstants[0] = constants[0]; + computedConstants[1] = -constants[0]; + computedConstants[3] = (!constants[0])^(1.0/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) +{ + algebraicVariables[0] = voi+constants[0]; + algebraicVariables[1] = voi-constants[0]; + algebraicVariables[2] = voi+constants[0]; + algebraicVariables[3] = voi-constants[0]; + algebraicVariables[4] = voi+3.0; + algebraicVariables[5] = voi-3.0; + algebraicVariables[6] = voi+(voi+constants[0]); + algebraicVariables[7] = voi+(voi-constants[0]); + algebraicVariables[8] = voi+voi*constants[0]; + algebraicVariables[9] = voi+(voi > 5.5); + algebraicVariables[10] = voi*(voi+constants[0]); + algebraicVariables[11] = voi/(voi*constants[0]); + algebraicVariables[12] = (voi-constants[0])*voi; + algebraicVariables[13] = voi-(voi+constants[0]); + algebraicVariables[14] = -(voi+constants[0]); + algebraicVariables[15] = !(voi+constants[0]); + algebraicVariables[16] = (voi > 5.5) && constants[0]; + algebraicVariables[17] = constants[0]-voi; + algebraicVariables[18] = voi+((voi > 5.5) && constants[0]); + algebraicVariables[19] = voi+((voi > 5.5)?constants[0]:1.0); + algebraicVariables[20] = voi+(voi+constants[0]); + algebraicVariables[21] = voi+voi*constants[0]; + algebraicVariables[22] = voi*(voi*constants[0]); + algebraicVariables[23] = voi*(voi/constants[0]); + algebraicVariables[24] = voi < (voi > 5.5); + algebraicVariables[25] = (voi > 5.5)?constants[0]:1.0; + algebraicVariables[26] = voi+constants[0]; + algebraicVariables[27] = sin(voi+constants[0]); + algebraicVariables[28] = (voi+constants[0])^(voi*constants[0]); + algebraicVariables[29] = sqrt(voi+constants[0]); + algebraicVariables[30] = (voi+constants[0])^(1.0/(voi*constants[0])); + algebraicVariables[31] = voi*((voi > 5.5) && constants[0]); + algebraicVariables[32] = voi*!constants[0]; + algebraicVariables[33] = voi*voi^constants[0]; + algebraicVariables[34] = voi*voi^(1.0/constants[0]); + algebraicVariables[35] = voi*((voi > 5.5)?constants[0]:1.0); + algebraicVariables[36] = voi+(voi-constants[0]); + algebraicVariables[37] = (voi > 5.5) < constants[0]; + algebraicVariables[38] = ((voi > 5.5) && constants[0]) < constants[0]; + algebraicVariables[39] = ((voi > 5.5)?constants[0]:1.0) < constants[0]; + algebraicVariables[40] = voi+constants[0] < voi; + algebraicVariables[41] = voi < ((voi > 5.5) && constants[0]); + algebraicVariables[42] = voi < ((voi > 5.5)?constants[0]:1.0); + algebraicVariables[43] = voi < voi*constants[0]; + algebraicVariables[44] = (!constants[0])^voi; + algebraicVariables[45] = (voi^constants[0])^voi; + algebraicVariables[46] = (voi^(1.0/constants[0]))^voi; + algebraicVariables[47] = sin(voi)^voi; + algebraicVariables[48] = (voi^constants[0])^(1.0/constants[0]); + algebraicVariables[49] = (voi^(1.0/constants[0]))^(1.0/constants[0]); + algebraicVariables[50] = sin(voi)^(1.0/constants[0]); + algebraicVariables[51] = (voi > 5.5)?((voi > 5.5)?constants[0]:1.0):2.0; + algebraicVariables[52] = ((voi > 5.5)?1.0:0.0)?constants[0]:2.0; +} diff --git a/tests/resources/generator/cellml_unary_minus_double_negation/model_power_not.out b/tests/resources/generator/cellml_unary_minus_double_negation/model_power_not.out new file mode 100644 index 000000000..4a192eca0 --- /dev/null +++ b/tests/resources/generator/cellml_unary_minus_double_negation/model_power_not.out @@ -0,0 +1,212 @@ +/* The content of this file was generated using a modified C profile of libCellML 0.7.2. */ + +#include "model.h" + +#include +#include + +const char VERSION[] = "0.8.1.post0"; +const char LIBCELLML_VERSION[] = "0.7.2"; + +const size_t STATE_COUNT = 1; +const size_t CONSTANT_COUNT = 1; +const size_t COMPUTED_CONSTANT_COUNT = 4; +const size_t ALGEBRAIC_VARIABLE_COUNT = 53; + +const VariableInfo VOI_INFO = {"t", "dimensionless", "c"}; + +const VariableInfo STATE_INFO[] = { + {"x", "dimensionless", "c"} +}; + +const VariableInfo CONSTANT_INFO[] = { + {"x0", "dimensionless", "c"} +}; + +const VariableInfo COMPUTED_CONSTANT_INFO[] = { + {"x2", "dimensionless", "c"}, + {"x3", "dimensionless", "c"}, + {"x9", "dimensionless", "c"}, + {"x52", "dimensionless", "c"} +}; + +const VariableInfo ALGEBRAIC_VARIABLE_INFO[] = { + {"x1", "dimensionless", "c"}, + {"x4", "dimensionless", "c"}, + {"x5", "dimensionless", "c"}, + {"x6", "dimensionless", "c"}, + {"x7", "dimensionless", "c"}, + {"x8", "dimensionless", "c"}, + {"x10", "dimensionless", "c"}, + {"x11", "dimensionless", "c"}, + {"x12", "dimensionless", "c"}, + {"x13", "dimensionless", "c"}, + {"x14", "dimensionless", "c"}, + {"x15", "dimensionless", "c"}, + {"x16", "dimensionless", "c"}, + {"x17", "dimensionless", "c"}, + {"x18", "dimensionless", "c"}, + {"x19", "dimensionless", "c"}, + {"x20", "dimensionless", "c"}, + {"x21", "dimensionless", "c"}, + {"x22", "dimensionless", "c"}, + {"x23", "dimensionless", "c"}, + {"x24", "dimensionless", "c"}, + {"x25", "dimensionless", "c"}, + {"x26", "dimensionless", "c"}, + {"x27", "dimensionless", "c"}, + {"x28", "dimensionless", "c"}, + {"x29", "dimensionless", "c"}, + {"x30", "dimensionless", "c"}, + {"x31", "dimensionless", "c"}, + {"x32", "dimensionless", "c"}, + {"x33", "dimensionless", "c"}, + {"x34", "dimensionless", "c"}, + {"x35", "dimensionless", "c"}, + {"x36", "dimensionless", "c"}, + {"x37", "dimensionless", "c"}, + {"x38", "dimensionless", "c"}, + {"x39", "dimensionless", "c"}, + {"x40", "dimensionless", "c"}, + {"x41", "dimensionless", "c"}, + {"x42", "dimensionless", "c"}, + {"x43", "dimensionless", "c"}, + {"x44", "dimensionless", "c"}, + {"x45", "dimensionless", "c"}, + {"x46", "dimensionless", "c"}, + {"x47", "dimensionless", "c"}, + {"x48", "dimensionless", "c"}, + {"x49", "dimensionless", "c"}, + {"x50", "dimensionless", "c"}, + {"x51", "dimensionless", "c"}, + {"x53", "dimensionless", "c"}, + {"x54", "dimensionless", "c"}, + {"x55", "dimensionless", "c"}, + {"x56", "dimensionless", "c"}, + {"x57", "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] = 3.0; + computedConstants[2] = -3.0; +} + +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + computedConstants[0] = constants[0]; + computedConstants[1] = -constants[0]; + computedConstants[3] = not(constants[0])^(1.0/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) +{ + algebraicVariables[0] = voi+constants[0]; + algebraicVariables[1] = voi-constants[0]; + algebraicVariables[2] = voi+constants[0]; + algebraicVariables[3] = voi-constants[0]; + algebraicVariables[4] = voi+3.0; + algebraicVariables[5] = voi-3.0; + algebraicVariables[6] = voi+(voi+constants[0]); + algebraicVariables[7] = voi+(voi-constants[0]); + algebraicVariables[8] = voi+voi*constants[0]; + algebraicVariables[9] = voi+(voi > 5.5); + algebraicVariables[10] = voi*(voi+constants[0]); + algebraicVariables[11] = voi/(voi*constants[0]); + algebraicVariables[12] = (voi-constants[0])*voi; + algebraicVariables[13] = voi-(voi+constants[0]); + algebraicVariables[14] = -(voi+constants[0]); + algebraicVariables[15] = not(voi+constants[0]); + algebraicVariables[16] = (voi > 5.5) && constants[0]; + algebraicVariables[17] = constants[0]-voi; + algebraicVariables[18] = voi+((voi > 5.5) && constants[0]); + algebraicVariables[19] = voi+((voi > 5.5)?constants[0]:1.0); + algebraicVariables[20] = voi+(voi+constants[0]); + algebraicVariables[21] = voi+voi*constants[0]; + algebraicVariables[22] = voi*(voi*constants[0]); + algebraicVariables[23] = voi*(voi/constants[0]); + algebraicVariables[24] = voi < (voi > 5.5); + algebraicVariables[25] = (voi > 5.5)?constants[0]:1.0; + algebraicVariables[26] = voi+constants[0]; + algebraicVariables[27] = sin(voi+constants[0]); + algebraicVariables[28] = (voi+constants[0])^(voi*constants[0]); + algebraicVariables[29] = sqrt(voi+constants[0]); + algebraicVariables[30] = (voi+constants[0])^(1.0/(voi*constants[0])); + algebraicVariables[31] = voi*((voi > 5.5) && constants[0]); + algebraicVariables[32] = voi*not(constants[0]); + algebraicVariables[33] = voi*voi^constants[0]; + algebraicVariables[34] = voi*voi^(1.0/constants[0]); + algebraicVariables[35] = voi*((voi > 5.5)?constants[0]:1.0); + algebraicVariables[36] = voi+(voi-constants[0]); + algebraicVariables[37] = (voi > 5.5) < constants[0]; + algebraicVariables[38] = ((voi > 5.5) && constants[0]) < constants[0]; + algebraicVariables[39] = ((voi > 5.5)?constants[0]:1.0) < constants[0]; + algebraicVariables[40] = voi+constants[0] < voi; + algebraicVariables[41] = voi < ((voi > 5.5) && constants[0]); + algebraicVariables[42] = voi < ((voi > 5.5)?constants[0]:1.0); + algebraicVariables[43] = voi < voi*constants[0]; + algebraicVariables[44] = not(constants[0])^voi; + algebraicVariables[45] = (voi^constants[0])^voi; + algebraicVariables[46] = (voi^(1.0/constants[0]))^voi; + algebraicVariables[47] = sin(voi)^voi; + algebraicVariables[48] = (voi^constants[0])^(1.0/constants[0]); + algebraicVariables[49] = (voi^(1.0/constants[0]))^(1.0/constants[0]); + algebraicVariables[50] = sin(voi)^(1.0/constants[0]); + algebraicVariables[51] = (voi > 5.5)?((voi > 5.5)?constants[0]:1.0):2.0; + algebraicVariables[52] = ((voi > 5.5)?1.0:0.0)?constants[0]:2.0; +} diff --git a/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.c b/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.c new file mode 100644 index 000000000..2f335c850 --- /dev/null +++ b/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.c @@ -0,0 +1,102 @@ +/* The content of this file was generated using the C profile of libCellML 0.7.2. */ + +#include "model.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 = 1; +const size_t COMPUTED_CONSTANT_COUNT = 1; +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[] = { + {"x0", "dimensionless", "c"} +}; + +const VariableInfo COMPUTED_CONSTANT_INFO[] = { + {"x2", "dimensionless", "c"} +}; + +const VariableInfo ALGEBRAIC_VARIABLE_INFO[] = { + {"x1", "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] = 3.0; +} + +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + computedConstants[0] = -(-2.0*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) +{ + algebraicVariables[0] = voi-(-2.0*constants[0]); +} diff --git a/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.cellml b/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.cellml new file mode 100644 index 000000000..57be71419 --- /dev/null +++ b/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.cellml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + t + + x + + 1 + + + + x1 + + + t + + + -2 + x0 + + + + + + x2 + + + + + -2 + x0 + + + + + + diff --git a/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.h b/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.h new file mode 100644 index 000000000..25d92cdc0 --- /dev/null +++ b/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.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[3]; + 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_minus_negative_number_operand/model.out b/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.out new file mode 100644 index 000000000..a67a0274f --- /dev/null +++ b/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.out @@ -0,0 +1,102 @@ +/* The content of this file was generated using a modified C profile of libCellML 0.7.2. */ + +#include "model.h" + +#include +#include + +const char VERSION[] = "0.8.1.post0"; +const char LIBCELLML_VERSION[] = "0.7.2"; + +const size_t STATE_COUNT = 1; +const size_t CONSTANT_COUNT = 1; +const size_t COMPUTED_CONSTANT_COUNT = 1; +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[] = { + {"x0", "dimensionless", "c"} +}; + +const VariableInfo COMPUTED_CONSTANT_INFO[] = { + {"x2", "dimensionless", "c"} +}; + +const VariableInfo ALGEBRAIC_VARIABLE_INFO[] = { + {"x1", "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] = 3.0; +} + +void computeComputedConstants(double voi, double *states, double *rates, double *constants, double *computedConstants, double *algebraicVariables) +{ + computedConstants[0] = - (-2.0*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) +{ + algebraicVariables[0] = voi - (-2.0*constants[0]); +} diff --git a/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.py b/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.py new file mode 100644 index 000000000..697faef6c --- /dev/null +++ b/tests/resources/generator/cellml_unary_minus_negative_number_operand/model.py @@ -0,0 +1,64 @@ +# 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 = 1 +COMPUTED_CONSTANT_COUNT = 1 +ALGEBRAIC_VARIABLE_COUNT = 1 + +VOI_INFO = {"name": "t", "units": "dimensionless", "component": "c"} + +STATE_INFO = [ + {"name": "x", "units": "dimensionless", "component": "c"} +] + +CONSTANT_INFO = [ + {"name": "x0", "units": "dimensionless", "component": "c"} +] + +COMPUTED_CONSTANT_INFO = [ + {"name": "x2", "units": "dimensionless", "component": "c"} +] + +ALGEBRAIC_VARIABLE_INFO = [ + {"name": "x1", "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] = 3.0 + + +def compute_computed_constants(voi, states, rates, constants, computed_constants, algebraic_variables): + computed_constants[0] = -(-2.0*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): + algebraic_variables[0] = voi-(-2.0*constants[0])