Initiating one sub function initiate all sub function

Hi all,

Using the most recent version of dolfinx on WSL2 Ubuntu, I run the following code:

from ufl import SpatialCoordinate, FiniteElement, MixedElement, TestFunctions, split
from dolfinx import mesh
from dolfinx.fem import FunctionSpace, Function, Constant
from mpi4py import MPI
import numpy


def y0_init(x):
    values = numpy.zeros((1, x.shape[1]))
    values[0] = 1.0
    return values


def y1_init(x):
    values = numpy.zeros((1, x.shape[1]))
    values[0] = 2.0
    return values


msh = mesh.create_interval(MPI.COMM_WORLD, 10, points=(0, 1))

x = SpatialCoordinate(msh)

CG1_elem = FiniteElement("CG", msh.ufl_cell(), 1)
ME_elem = MixedElement([CG1_elem, CG1_elem])
ME = FunctionSpace(msh, ME_elem)

y = Function(ME)
y.sub(0).interpolate(y0_init)
y.sub(1).interpolate(y1_init)


assert (y.sub(0).x.array != y.sub(1).x.array).any()

and the assert fails - the arrays are equal.
Why is this happening?

The sub function is just a view into the original function. As python does not have a nice way of viewing strided data, these arrays are the full arrays (equal to y.x.array, you can verify this). However, if you look at each component of your sub function either by visualizing or assembling over them, you can observe that the interpolation does as you expect.

1 Like