Skip to content

Commit

Permalink
Remove component tensors
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrubeck committed Jan 18, 2025
1 parent 7d7c676 commit c9eb371
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions test/test_derivative.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ def test_vector_coefficient_scalar_derivatives(self):
integrand = inner(f, g)

i0, i1, i2, i3, i4 = [Index(count=c) for c in range(5)]
expected = as_tensor(df[i1] * dv, (i1,))[i0] * g[i0]
expected = as_tensor(df[i1], (i1,))[i0] * dv * g[i0]

F = integrand * dx
J = derivative(F, u, dv, cd)
Expand Down Expand Up @@ -693,7 +693,7 @@ def test_vector_coefficient_derivatives(self):
integrand = inner(f, g)

i0, i1, i2, i3, i4 = [Index(count=c) for c in range(5)]
expected = as_tensor(df[i2, i1] * dv[i1], (i2,))[i0] * g[i0]
expected = as_tensor(df[i2, i1], (i2,))[i0] * dv[i1] * g[i0]

F = integrand * dx
J = derivative(F, u, dv, cd)
Expand Down
6 changes: 6 additions & 0 deletions ufl/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ufl.core.operator import Operator
from ufl.core.ufl_type import ufl_type
from ufl.index_combination_utils import merge_unique_indices
from ufl.indexed import Indexed
from ufl.precedence import parstr
from ufl.sorting import sorted_expr

Expand Down Expand Up @@ -89,6 +90,11 @@ def __init__(self, a, b):
"""Initialise."""
Operator.__init__(self)

def _simplify_indexed(self, multiindex):
"""Return a simplified Expr used in the constructor of Indexed(self, multiindex)."""
a, b = self.ufl_operands
return Sum(Indexed(a, multiindex), Indexed(b, multiindex))

def evaluate(self, x, mapping, component, index_values):
"""Evaluate."""
return sum(o.evaluate(x, mapping, component, index_values) for o in self.ufl_operands)
Expand Down
3 changes: 3 additions & 0 deletions ufl/algorithms/compute_form_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from ufl.algorithms.analysis import extract_coefficients, extract_sub_elements, unique_tuple
from ufl.algorithms.apply_algebra_lowering import apply_algebra_lowering
from ufl.algorithms.remove_component_tensors import remove_component_tensors
from ufl.algorithms.apply_derivatives import apply_coordinate_derivatives, apply_derivatives

# These are the main symbolic processing steps:
Expand Down Expand Up @@ -328,6 +329,8 @@ def compute_form_data(

form = apply_coordinate_derivatives(form)

form = remove_component_tensors(form)

# Propagate restrictions to terminals
if do_apply_restrictions:
form = apply_restrictions(form, apply_default=do_apply_default_restrictions)
Expand Down
6 changes: 6 additions & 0 deletions ufl/indexsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ufl.core.multiindex import MultiIndex
from ufl.core.operator import Operator
from ufl.core.ufl_type import ufl_type
from ufl.indexed import Indexed
from ufl.precedence import parstr

# --- Sum over an index ---
Expand Down Expand Up @@ -69,6 +70,11 @@ def ufl_shape(self):
"""Get UFL shape."""
return self.ufl_operands[0].ufl_shape

def _simplify_indexed(self, multiindex):
"""Return a simplified Expr used in the constructor of Indexed(self, multiindex)."""
A, i = self.ufl_operands
return IndexSum(Indexed(A, multiindex), i)

def evaluate(self, x, mapping, component, index_values):
"""Evaluate."""
(i,) = self.ufl_operands[1]
Expand Down

0 comments on commit c9eb371

Please sign in to comment.