Skip to content

Commit

Permalink
Fix issue #594 (#600)
Browse files Browse the repository at this point in the history
* add brackets for neg

* Add test
  • Loading branch information
chrisrichardson committed Sep 5, 2023
1 parent 52bcbe4 commit 63f060e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
39 changes: 39 additions & 0 deletions demo/VectorConstant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (C) 2016 Jan Blechta
#
# This file is part of FFCx.
#
# FFCx is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FFCx is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with FFCx. If not, see <http://www.gnu.org/licenses/>.
#
# The bilinear form a(u, v) and linear form L(v) for
# Poisson's equation using bilinear elements on bilinear mesh geometry.
import basix.ufl
from ufl import (Constant, Coefficient, FunctionSpace, Mesh, TestFunction, TrialFunction,
dx, grad, inner)

coords = basix.ufl.element("P", "triangle", 2, rank=1)
mesh = Mesh(coords)
dx = dx(mesh)

element = basix.ufl.element("P", mesh.ufl_cell().cellname(), 2)
space = FunctionSpace(mesh, element)

u = TrialFunction(space)
v = TestFunction(space)
f = Coefficient(space)

L = f * v * dx

mu = Constant(mesh, shape=(3,))
theta = - (mu[1] - 2) / mu[0] - (2 * (2 * mu[0] - 2) * (mu[0] - 1)) / (mu[0] * (mu[1] - 2))
a = theta * inner(grad(u), grad(v)) * dx
16 changes: 7 additions & 9 deletions ffcx/codegeneration/C/c_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,11 @@ def format_binary_op(self, oper) -> str:
# Return combined string
return f"{lhs} {oper.op} {rhs}"

def format_neg(self, val) -> str:
arg = self.c_format(val.arg)
return f"-{arg}"

def format_not(self, val) -> str:
arg = self.c_format(val.arg)
return f"{val.op}({arg})"
def format_unary_op(self, oper) -> str:
arg = self.c_format(oper.arg)
if oper.arg.precedence >= oper.precedence:
return f"{oper.op}({arg})"
return f"{oper.op}{arg}"

def format_literal_float(self, val) -> str:
value = self._format_number(val.value)
Expand Down Expand Up @@ -319,13 +317,13 @@ def format_math_function(self, c) -> str:
"Assign": format_assign,
"AssignAdd": format_assign,
"Product": format_nary_op,
"Neg": format_neg,
"Neg": format_unary_op,
"Sum": format_nary_op,
"Add": format_binary_op,
"Sub": format_binary_op,
"Mul": format_binary_op,
"Div": format_binary_op,
"Not": format_not,
"Not": format_unary_op,
"LiteralFloat": format_literal_float,
"LiteralInt": format_literal_int,
"Symbol": format_symbol,
Expand Down
15 changes: 15 additions & 0 deletions ffcx/codegeneration/lnodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ def __eq__(self, other):
def __float__(self):
return float(self.value)

def __repr__(self):
return str(self.value)


class LiteralInt(LExprTerminal):
"""An integer literal value."""
Expand All @@ -292,6 +295,9 @@ def __eq__(self, other):
def __hash__(self):
return hash(self.value)

def __repr__(self):
return str(self.value)


class Symbol(LExprTerminal):
"""A named symbol."""
Expand All @@ -309,6 +315,9 @@ def __eq__(self, other):
def __hash__(self):
return hash(self.name)

def __repr__(self):
return self.name


class PrefixUnaryOp(LExprOperator):
"""Base class for unary operators."""
Expand All @@ -335,6 +344,9 @@ def __eq__(self, other):
def __hash__(self):
return hash(self.lhs) + hash(self.rhs)

def __repr__(self):
return str(self.lhs) + str(self.op) + str(self.rhs)


class ArithmeticBinOp(BinOp):
def __init__(self, lhs, rhs):
Expand Down Expand Up @@ -600,6 +612,9 @@ def __eq__(self, other):
def __hash__(self):
return hash(self.array)

def __repr__(self):
return str(self.array) + "[" + ", ".join(str(i) for i in self.indices) + "]"


class Conditional(LExprOperator):
precedence = PRECEDENCE.CONDITIONAL
Expand Down

0 comments on commit 63f060e

Please sign in to comment.