Compute the target space of Interpolate only in UFL

Abierto
#5,597 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
45/100
Tipo de issue
Nueva funcionalidad
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
api, backend

Línea de trabajo

Read firedrake/interpolation.py around the linked Interpolate.__init__ lines and compare them with UFL’s ufl/core/base_form_operator.py and ufl/core/interpolate.py; the issue also points to FEniCS/ufl#515 for the UFL work. Trace how the target space and adjoint status are computed, then check the UFL and Firedrake tests for these cases. Done means UFL exposes the target space and adjoint status, and Firedrake uses them without recomputing argument numbers.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Follow-up from the review of #5551 (comment).

Problem

firedrake.Interpolate.__init__ and ufl.Interpolate.__init__ both compute the argument numbers of the expression and use them to decide which argument of the dual slot is contracted. Firedrake does this in its own way:

https://github.com/firedrakeproject/firedrake/blob/7cd82b23096667ec328b01c5c18588c16a11fa2e/firedrake/interpolation.py#L126-L140

UFL does it with _split_dual_slot_arguments in ufl/core/base_form_operator.py, and stores the result as Interpolate._function_space.

The two computations must agree, but nothing makes them agree. They already differ in small ways: Firedrake contracts the primal argument numbered 0, and UFL contracts the lowest-numbered primal argument. Firedrake does not check that the first argument of a form operand is a Coargument, and UFL does.

Why Firedrake computes the numbers again

UFL already computes the space that Firedrake needs, but it does not make it public. Firedrake needs two results from the argument numbers:

  1. The target space. This is the space of the dual-slot argument that the output of the interpolation replaces. Firedrake uses it to select the Interpolator subclass (target_space.mesh(), len(target_space)) and to check the shape of the expression. UFL computes the same space and stores it in the private _function_space. The public method ufl_function_space() does not return it. That method returns the dual of the first argument of the operator, which is a different space when the interpolation is an adjoint or when the dual slot is a form:

    Expression Target space ufl_function_space()
    Interpolate(u, V) V V
    Interpolate(TestFunction(U), V) (adjoint) V U*
    Interpolate(e, Cofunction(V*)) V None
    Interpolate(w0, A(v0, v1)) V0 W0*
    Interpolate(w1, A(v0, v1)) V1 V0*

    Here v0 ∈ V0, v1 ∈ V1, w0 ∈ W0 (number 0), and w1 ∈ W1 (number 1). These values were checked against UFL main. With a 2-form in the dual slot, the target space depends on the argument numbers of the expression (see the BaseFormOperator docstring). Because of this, the target space cannot be found from the dual slot alone.

  2. A Firedrake Argument for a function-space dual slot. If V is a WithGeometry, UFL makes a ufl.Argument(V.dual(), n), which has no .function_space() method. Firedrake therefore makes its own firedrake.Argument before it calls super().__init__. To get n, it must know whether the expression is an adjoint (expr_arg_numbers == {0}), and UFL computes that again. This is also the source of Interpolate.is_adjoint.

A third copy is in Interpolator.__init__, which sets self.target_space = dual_arg.function_space().dual(). That is correct only when the dual slot is a Coargument or a Cofunction.

Proposal

Make the target space a public attribute of ufl.BaseFormOperator. Then firedrake.Interpolate can be a thin wrapper around ufl.Interpolate.

BaseFormOperator.target_space (or ufl_target_space(), if it must follow the UFL naming convention) must return the space that the output of the operator N(u) is in:

  • It is the function_space argument of BaseFormOperator.__init__. __init__ already checks that this is the space of the contracted dual-slot argument.
  • It does not depend on the argument numbering. For the examples above, it is the space in the "Target space" column. It is never a dual space and it is never None, also when the dual slot is a Cofunction.
  • It is defined for every BaseFormOperator, not only for Interpolate. ExternalOperator gets the same attribute.
  • ufl_shape and ufl_element() of Interpolate must be derived from it. At the moment they read _function_space directly.

UFL must also expose whether the interpolation is an adjoint (for example Interpolate.is_adjoint), so that Firedrake does not compute the argument numbers again.

With these changes in UFL, Firedrake can:

  • Remove the computation of expr_arg_numbers and the selection of the contracted argument from firedrake.Interpolate.__init__, and use self.target_space from UFL.
  • Remove Interpolator.target_space = dual_arg.function_space().dual(), and use expr.target_space.
  • Keep only the conversion of a WithGeometry into a firedrake.Argument. This is possible if UFL makes the argument through a hook that Firedrake can override, or if ufl.Argument gets the information that Firedrake needs from .function_space().

FEniCS/ufl#515 changes ufl/core/interpolate.py and can be a good place for the UFL part of this change.


🤖 Drafted with Claude Code (Claude Opus 5.5).

Lenguaje dominante
Python
Estrellas
684
Forks
204
Merge medio
4 d 14 h
PR fusionados (30 d)
46

Preparar el entorno

  • Sin Dockerfile ni archivo de Docker Compose
  • Tiene una plantilla de pull request
  • Sin guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de firedrakeproject/firedrake

Todos los issues de firedrakeproject/firedrake

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.