Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down
13 changes: 11 additions & 2 deletions src/validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
74 changes: 74 additions & 0 deletions tests/generator/generatorunary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,80 @@ limitations under the License.

#include <libcellml>

// 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;
Expand Down
102 changes: 102 additions & 0 deletions tests/resources/generator/cellml_unary_min_max/example_01.c
Original file line number Diff line number Diff line change
@@ -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 <math.h>
#include <stdlib.h>

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;
}
31 changes: 31 additions & 0 deletions tests/resources/generator/cellml_unary_min_max/example_01.cellml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<model xmlns="http://www.cellml.org/cellml/2.0#" name="m">
<component name="c">
<variable name="t" units="dimensionless"/>
<variable name="x" units="dimensionless" initial_value="0"/>
<variable name="y" units="dimensionless"/>
<variable name="a" units="dimensionless" initial_value="2"/>
<variable name="b" units="dimensionless" initial_value="3"/>
<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:cellml="http://www.cellml.org/cellml/2.0#">
<apply>
<eq/>
<apply>
<diff/>
<bvar>
<ci>t</ci>
</bvar>
<ci>x</ci>
</apply>
<cn cellml:units="dimensionless">1</cn>
</apply>
<apply>
<eq/>
<ci>y</ci>
<apply>
<min/>
<ci>t</ci>
</apply>
</apply>
</math>
</component>
</model>
37 changes: 37 additions & 0 deletions tests/resources/generator/cellml_unary_min_max/example_01.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* The content of this file was generated using the C profile of libCellML 0.7.2. */

#pragma once

#include <stddef.h>

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);
65 changes: 65 additions & 0 deletions tests/resources/generator/cellml_unary_min_max/example_01.py
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading