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()