I created a function and interpolated it into the function space, but I got an error. This is a mwe
domain = mesh.create_unit_square(MPI.COMM_WORLD, 8, 8, mesh.CellType.quadrilateral)
V = functionspace(domain, ("Lagrange", 1, (domain.geometry.dim,))) #vector function space
x = ufl.SpatialCoordinate(domain)
def rect( x):
return [0,(x[0] < 0.6)* (x[0] > 0.4)]
u_D = fem.Function(V)
u_D.interpolate(rect)
def upper_boundary(x):
return np.isclose(x[1], 1)
fdim = domain.topology.dim - 1
boundary_facets_up = mesh.locate_entities_boundary(domain, fdim, upper_boundary)
dofs_up = fem.locate_dofs_topological(V, fdim, boundary_facets_up)
bc_up = fem.dirichletbc(u_D, dofs_up, V)
and I get the following error:
TypeError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/dolfinx/fem/function.py in interpolate(self, u, cells, nmm_interpolation_data) 452 # u is a Function or Expression (or pointer to one) โ 453 _interpolate(u, cells) 454 except TypeError:
3 frames
TypeError: interpolate(): incompatible function arguments. The following argument types are supported: 1. interpolate(self, f: ndarray[dtype=float64, writable=False, shape=(), order=โCโ], cells: ndarray[dtype=int32, writable=False, shape=(), order=โCโ]) โ None 2. interpolate(self, f: ndarray[dtype=float64, writable=False, shape=(, ), order=โCโ], cells: ndarray[dtype=int32, writable=False, shape=(), order=โCโ]) โ None 3. interpolate(self, u: dolfinx.cpp.fem.Function_float64, cells: ndarray[dtype=int32, writable=False, shape=(), order=โCโ], nmm_interpolation_data: tuple[ndarray[dtype=int32, writable=False, shape=(), order=โCโ], ndarray[dtype=int32, writable=False, shape=(), order=โCโ], ndarray[dtype=float64, writable=False, shape=(), order=โCโ], ndarray[dtype=int32, writable=False, shape=(), order=โCโ]]) โ None 4. interpolate(self, expr: dolfinx.cpp.fem.Expression_float64, cells: ndarray[dtype=int32, writable=False, order=โCโ]) โ None Invoked with types: dolfinx.cpp.fem.Function_float64, function, ndarray, dolfinx.fem.function.PointOwnershipData
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/dolfinx/fem/function.py in interpolate(self, u, cells, nmm_interpolation_data) 456 assert callable(u) 457 x = _cpp.fem.interpolation_coords(self._V.element, self._V.mesh.geometry, cells) โ 458 self._cpp_object.interpolate(np.asarray(u(x), dtype=self.dtype), cells) # type: ignore 459 460 def copy(self) โ Function:
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.