Interpolation Data Has Wrong Shape/Size

I am trying to interpolate a 2D solution onto a 3D mesh. I am doing this because I am solving stokes flow, and I want my inlet boundary condition to be fully devolved flow. I have code that returns the mesh and a fem.function solution of fully developed flow (2D) and I want to interpolate this onto a 3D mesh to use as the inflow boundary condition. To start, I am trying to interpolate a simple 2D function onto a 3D cube, and I am using code made by forum post by user LeErnsrt (LeErnst’s Code)

I have modified LeErnsrt’s code to interpolate from 2D to 3D (The original code goes from 3D to 2D), however when I run the code, I get the following error.

Cell In[1], line 26
     24 # Interpolate on 2D mesh
     25 u1 = fem.Function(V1, dtype=xtype)
---> 26 u1.interpolate(f)
     27 u1.x.scatter_forward()
     29 # Interpolate 2D->3D

File ~/packages/Anaconda3/envs/fenicsx-env/lib/python3.12/site-packages/dolfinx/fem/function.py:497, in Function.interpolate(self, u, cells, cell_map, expr_mesh, nmm_interpolation_data)
    495 assert callable(u)
    496 x = _cpp.fem.interpolation_coords(self._V.element, self._V.mesh.geometry, cells)
--> 497 self._cpp_object.interpolate(np.asarray(u(x), dtype=self.dtype), cells)

RuntimeError: Interpolation data has the wrong shape/size.

I do not know how to fix this error, I have modified the f(x) function to only have x[0] and x[1], which I assumed prevent any errors relating to the dimensionality of the problem.

Here is my code:

from dolfinx.mesh import create_unit_square, create_unit_cube, CellType
from basix.ufl import element
from dolfinx import fem

from mpi4py import MPI
import numpy as np

xtype = np.float64
cell_type0 = CellType.hexahedron
cell_type1 = CellType.triangle

mesh0 = create_unit_cube(MPI.COMM_WORLD, 5, 6, 7, cell_type=cell_type0, dtype=xtype)
mesh1 = create_unit_square(MPI.COMM_WORLD, 5, 4, cell_type=cell_type1, dtype=xtype)

def f(x):
    return (7 * x[1], 3 * x[0])
    
el0 = element("Lagrange", mesh0.topology.cell_name(), 1)
V0 = fem.functionspace(mesh0, el0)
el1 = element("Lagrange", mesh1.topology.cell_name(), 1)
V1 = fem.functionspace(mesh1, el1)

# Interpolate on 2D mesh
u1 = fem.Function(V1, dtype=xtype)
u1.interpolate(f)
u1.x.scatter_forward()

# Interpolate 2D->3D
u0 = Function(V0, dtype=xtype)
u0.interpolate(u1, nmm_interpolation_data=fem.create_nonmatching_meshes_interpolation_data(
    u0.function_space.mesh._cpp_object,
    u0.function_space.element,
    u1.function_space.mesh._cpp_object, 0))
u0.x.scatter_forward()

As the error states, u1 does not have the correct size. Consider

def f(x):
    return np.vstack((7 * x[1], 3 * x[0]))
    
el1 = element("Lagrange", mesh1.topology.cell_name(), 1, shape=(2,))
V1 = fem.functionspace(mesh1, el1)

# Interpolate on 2D mesh
u1 = fem.Function(V1, dtype=xtype)
u1.interpolate(f)

Thank you for your help, your code fixed the error, however I now get a different

error Cell In[1], line 31
     29 # Interpolate 2D->3D
     30 u0 = fem.Function(V0, dtype=xtype)
---> 31 u0.interpolate(u1, nmm_interpolation_data=fem.create_nonmatching_meshes_interpolation_data(
     32     u0.function_space.mesh._cpp_object,
     33     u0.function_space.element,
     34     u1.function_space.mesh._cpp_object, 0))
     35 u0.x.scatter_forward()

AttributeError: 'dolfinx.cpp.mesh.Mesh_float64' object has no attribute '_cpp_object'

I have found a couple of forum posts about not having a _ccp_object they are quite and are different from the error I am currently having (forum 1 and forum 2).

Do you know how to fix this error?

Also, here is my new code

from dolfinx.mesh import create_unit_square, create_unit_cube, CellType
from basix.ufl import element
from dolfinx import fem

from mpi4py import MPI
import numpy as np

xtype = np.float64
cell_type0 = CellType.hexahedron
cell_type1 = CellType.triangle

mesh0 = create_unit_cube(MPI.COMM_WORLD, 5, 6, 7, cell_type=cell_type0, dtype=xtype)
mesh1 = create_unit_square(MPI.COMM_WORLD, 5, 4, cell_type=cell_type1, dtype=xtype)

def f(x):
    return np.vstack((7 * x[1], 3 * x[0]))
    
el0 = element("Lagrange", mesh0.topology.cell_name(), 1, shape=(3,))
V0 = fem.functionspace(mesh0, el0)
el1 = element("Lagrange", mesh1.topology.cell_name(), 1, shape=(2,))
V1 = fem.functionspace(mesh1, el1)

# Interpolate on 2D mesh
u1 = fem.Function(V1, dtype=xtype)
u1.interpolate(f)
u1.x.scatter_forward()

# Interpolate 2D->3D
u0 = fem.Function(V0, dtype=xtype)
u0.interpolate(u1, nmm_interpolation_data=fem.create_nonmatching_meshes_interpolation_data(
    u0.function_space.mesh._cpp_object,
    u0.function_space.element,
    u1.function_space.mesh._cpp_object, 0))
u0.x.scatter_forward()

You would have to state what version of DOLFINx that you are running, as the API of create_nonmatching_meshes_interpolation_data has changed with v0.6, v0.7 and v0.8 of DOLFINx

I am using dolfinx version 0.8.0, installed on Linux (Ubuntu) using anaconda

Then try using the following:

from dolfinx.mesh import create_unit_square, create_unit_cube, CellType
from basix.ufl import element
from dolfinx import fem

from mpi4py import MPI
import numpy as np

xtype = np.float64
cell_type0 = CellType.hexahedron
cell_type1 = CellType.triangle

mesh0 = create_unit_cube(MPI.COMM_WORLD, 5, 6, 7, cell_type=cell_type0, dtype=xtype)
mesh1 = create_unit_square(MPI.COMM_WORLD, 5, 4, cell_type=cell_type1, dtype=xtype)


def f(x):
    return np.vstack((7 * x[1], 3 * x[0]))


el0 = element("Lagrange", mesh0.topology.cell_name(), 1, shape=(3,))
V0 = fem.functionspace(mesh0, el0)
el1 = element("Lagrange", mesh1.topology.cell_name(), 1, shape=(2,))
V1 = fem.functionspace(mesh1, el1)

# Interpolate on 2D mesh
u1 = fem.Function(V1, dtype=xtype)
u1.interpolate(f)
u1.x.scatter_forward()

# Interpolate 2D->3D
u0 = fem.Function(V0, dtype=xtype)
u0.interpolate(
    u1,
    nmm_interpolation_data=fem.create_nonmatching_meshes_interpolation_data(
        u0.function_space.mesh,
        u0.function_space.element,
        u1.function_space.mesh,
        padding=1.0e-6,
    ),
)
u0.x.scatter_forward()
1 Like

That worked, thank you for your help