• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FEniCS / ufl / 45640399807

25 Nov 2026 02:57PM UTC coverage: 82.559% (-0.02%) from 82.575%
45640399807

push

github

web-flow
Merge the interpolation pullbacks into apply_function_pullbacks (#564)

* Merge the interpolation pullbacks into apply_function_pullbacks

FunctionPullbackApplier now also evaluates an Interpolate on the reference
cell of its target element, so one traversal maps both the form arguments and
the interpolations. compute_form_data applies it once, after degree estimation,
and apply_interpolate_pullbacks is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiE9EbBapfyhts8ndUSq73

* Deprecate apply_interpolate_pullbacks

apply_function_pullbacks now lowers the interpolations, so the old
entry point warns and returns the expression unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Jørgen Schartum Dokken <dokken92@gmail.com>

4 of 6 new or added lines in 1 file covered. (66.67%)

11313 of 13703 relevant lines covered (82.56%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.46
/ufl/algorithms/apply_function_pullbacks.py
1
"""Algorithm for replacing gradients in an expression."""
2

3
# Copyright (C) 2008-2016 Martin Sandve Alnæs
4
#
5
# This file is part of UFL (https://www.fenicsproject.org)
6
#
7
# SPDX-License-Identifier:    LGPL-3.0-or-later
8

9
from __future__ import annotations
1 ✔
10

11
import warnings
1 ✔
12
from functools import singledispatchmethod
1 ✔
13

14
from ufl.algorithms.map_integrands import map_integrands
1 ✔
15
from ufl.classes import Argument, Coefficient, Expr, FormArgument, Interpolate, ReferenceValue
1 ✔
16
from ufl.corealg.dag_traverser import DAGTraverser
1 ✔
17
from ufl.domain import extract_unique_domain
1 ✔
18
from ufl.form import BaseForm
1 ✔
19

20

21
class FunctionPullbackApplier(DAGTraverser):
1 ✔
22
    """A pull back applier for form arguments and interpolations."""
23

24
    @singledispatchmethod
1 ✔
25
    def process(self, o: Expr | BaseForm) -> Expr | BaseForm:
1 ✔
26
        """Process ``o``.
27

28
        Args:
29
            o: `Expr` or `BaseForm` to be processed.
30

31
        Returns:
32
            Processed `Expr` or `BaseForm`.
33

34
        """
35
        return super().process(o)
×
36

37
    @process.register(Expr)
1 ✔
38
    @process.register(BaseForm)
1 ✔
39
    def _(self, o: Expr | BaseForm) -> Expr | BaseForm:
1 ✔
40
        """Handle Expr and BaseForm."""
41
        return self.reuse_if_untouched(o)
1 ✔
42

43
    @process.register(FormArgument)
1 ✔
44
    def _(self, o: Argument | Coefficient) -> Expr:
1 ✔
45
        """Represent 0-derivatives of a form argument on the reference element."""
46
        r = ReferenceValue(o)
1 ✔
47
        space = o.ufl_function_space()
1 ✔
48
        element = o.ufl_element()
1 ✔
49

50
        if r.ufl_shape != element.reference_value_shape:
1 ✔
51
            raise ValueError(
×
52
                "Expecting reference space expression with shape "
53
                f"'{element.reference_value_shape}', got '{r.ufl_shape}'"
54
            )
55
        f = element.pullback.apply(r)
1 ✔
56
        if f.ufl_shape != space.value_shape:
1 ✔
57
            raise ValueError(
×
58
                f"Expecting pulled back expression with shape '{space.value_shape}', "
59
                f"got '{f.ufl_shape}'"
60
            )
61

62
        assert f.ufl_shape == o.ufl_shape
1 ✔
63
        return f
1 ✔
64

65
    @process.register(Interpolate)
1 ✔
66
    @DAGTraverser.postorder
1 ✔
67
    def _(self, o: Interpolate, operand: Expr) -> Expr:
1 ✔
68
        """Evaluate an interpolation on the reference cell of its target element."""
69
        dual_arg, _ = o.argument_slots()
1 ✔
70
        element = o.ufl_element()
1 ✔
71
        domain = extract_unique_domain(operand) or dual_arg.ufl_function_space().ufl_domain()
1 ✔
72
        # Build the node here rather than reconstructing o: the mapped operand
73
        # no longer has the physical value shape that a subclass may check.
74
        r = Interpolate(apply_inverse_pullback(operand, element, domain), dual_arg)
1 ✔
75
        return element.pullback.apply(ReferenceValue(r), domain)
1 ✔
76

77

78
def apply_inverse_pullback(expr, element, domain=None):
1 ✔
79
    """Map a physical expression onto the reference cell of an element.
80

81
    This is a rule on a single node, not a DAG traversal: the expression is
82
    mapped as a whole, and an interpolation inside it is left alone for
83
    `apply_function_pullbacks` to lower.
84

85
    Args:
86
        expr: An expression on a physical cell, whose shape must be the
87
            physical value shape of the element
88
        element: The element whose pull back is inverted
89
        domain: The domain to use if the expression carries none
90

91
    Returns:
92
        The expression on the reference cell, with shape
93
        ``element.reference_value_shape``
94
    """
95
    mesh = extract_unique_domain(expr) or domain
1 ✔
96
    if domain is not None and mesh != domain:
1 ✔
97
        raise NotImplementedError("Multiple domains not supported")
1 ✔
98
    physical_value_shape = element.pullback.physical_value_shape(element, mesh)
1 ✔
99
    if expr.ufl_shape != physical_value_shape:
1 ✔
100
        raise ValueError(
1 ✔
101
            f"Expecting physical expression with shape '{physical_value_shape}', "
102
            f"got '{expr.ufl_shape}'"
103
        )
104
    r = element.pullback.apply_inverse(expr, mesh)
1 ✔
105
    if r.ufl_shape != element.reference_value_shape:
1 ✔
106
        raise ValueError(
×
107
            f"Expecting reference expression with shape "
108
            f"'{element.reference_value_shape}', got '{r.ufl_shape}'"
109
        )
110
    return r
1 ✔
111

112

113
def apply_interpolate_pullbacks(expr):
1 ✔
114
    """Return the expression unchanged.
115

116
    Deprecated: `apply_function_pullbacks` now changes the representation of
117
    the interpolations together with the form arguments.
118

119
    Args:
120
        expr: An Expr or Form
121

122
    Returns:
123
        The expression, unchanged
124
    """
NEW
125
    warnings.warn(
×
126
        "apply_interpolate_pullbacks is deprecated, "
127
        "apply_function_pullbacks now applies the interpolation pullbacks.",
128
        DeprecationWarning,
129
        stacklevel=2,
130
    )
NEW
131
    return expr
×
132

133

134
def apply_function_pullbacks(expr):
1 ✔
135
    """Change representation of coefficients and arguments in an expression.
136

137
    Applies Piola mappings where applicable and represents all
138
    form arguments in reference value. An interpolation is evaluated on the
139
    reference cell of its target element, so its operand is mapped there and
140
    the result is pulled back to the physical cell for the expression that
141
    holds it.
142

143
    Args:
144
        expr: An Expression
145
    """
146
    return map_integrands(FunctionPullbackApplier(), expr)
1 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc