From b35cc1b8d1a8ff5ffe4667db37de830b72777730 Mon Sep 17 00:00:00 2001 From: David Ham Date: Fri, 6 Oct 2017 12:23:10 +0100 Subject: [PATCH] Fix multiple derivatives in modified terminals. When a modified terminal contains multiple derivatives, construct_modified_terminals iteratively differentiates it. However this can result in differentiating a Zero, which fails. This works around this. --- tsfc/modified_terminals.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tsfc/modified_terminals.py b/tsfc/modified_terminals.py index 07e9a40a..006124f2 100644 --- a/tsfc/modified_terminals.py +++ b/tsfc/modified_terminals.py @@ -25,7 +25,8 @@ from ufl.classes import (ReferenceValue, ReferenceGrad, NegativeRestricted, PositiveRestricted, Restricted, FacetAvg, CellAvg, ConstantValue, - Jacobian, SpatialCoordinate) + Jacobian, SpatialCoordinate, Zero) +from ufl.checks import is_cellwise_constant class ModifiedTerminal(object): @@ -175,8 +176,16 @@ def construct_modified_terminal(mt, terminal): if mt.reference_value: expr = ReferenceValue(expr) + dim = expr.ufl_domain().topological_dimension() for n in range(mt.local_derivatives): - expr = ReferenceGrad(expr) + # Return zero if expression is trivially constant. This has to + # happen here because ReferenceGrad has no access to the + # topological dimension of a literal zero. + if is_cellwise_constant(expr): + expr = Zero(expr.ufl_shape + (dim,), expr.ufl_free_indices, + expr.ufl_index_dimensions) + else: + expr = ReferenceGrad(expr) if mt.averaged == "cell": expr = CellAvg(expr)