Interpolate being both a BaseForm and Expression causes issues
- Dominant language
- Python
- Stars
- 150
- Forks
- 79
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 10
Description
I have encountered the following 2 cases where `Interpolate` being both an `Expr` (i.e. primal) and `BaseForm` (i.e. dual) object causes things to break down.
### Case 1
```py
u = Coefficient(V1)
Iu = Interpolate(u, V2)
uhat = TrialFunction(V1)
FormSum((Iu, 1), (Iu, 1)).arguments() # works fine
(Iu + Iu).arguments()
```
gives
```
> (Iu + Iu).arguments()
^^^^^^^^^^^^^^^^^^^
E AttributeError: 'Sum' object has no attribute 'arguments'
```
### Case 2
```py
u = Coefficient(V1)
Iu = Interpolate(u, V2)
uhat = TrialFunction(V1)
independent = Coefficient(V1)
dIu = derivative(Iu, independent, uhat)
# Sum(ZeroBaseForm, ZeroBaseForm)
expand_derivatives(dIu + dIu)
```
gives
```
def __new__(cls, a, b):
"""Create a new Sum."""
from ufl import BaseForm, FormSum
# Base forms (that aren't also expressions like Interpolate) should
# be cast to FormSums instead.
# if any(isinstance(x, BaseForm) and not isinstance(x, Expr) for x in [a, b]):
# return FormSum((a, 1),(b, 1))
# Make sure everything is an Expr
a = as_ufl(a)
b = as_ufl(b)
# Assert consistent tensor properties
> sh = a.ufl_shape
^^^^^^^^^^^
E AttributeError: 'ZeroBaseForm' object has no attribute 'ufl_shape'
ufl/algebra.py:49: AttributeError
```
These tests are oddly specific but the errors arose inside a complicated piece of adjoint logic where we were taking derivatives of interpolates and summing them together. `Sum(BaseFormOperatorDerivative, BaseFormOperatorDerivative)` is allowed but `Sum(ZeroBaseForm, ZeroBaseForm)` (which naturally arises during `expand_derivatives`) is not.
To me it seems likely that the solution is something complicated to do with the type system. For now we will just avoid the case that triggers this.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.