Hi all,
I am using trying to set Dirichlet boundary conditions in a 2D domain, for example, I wanna fix the displacement u_x and u_y on the bottom. Here I tried two different ways to implement this step:
import numpy as np
import ufl
from dolfinx import fem, io, mesh, plot
from ufl import ds, dx, grad, inner
from dolfinx.io import XDMFFile
from dolfinx.fem import FunctionSpace, Function, Constant
from ufl import SpatialCoordinate, FiniteElement, MixedElement, TestFunctions, split
from mpi4py import MPI
from petsc4py.PETSc import ScalarType
Lx, Ly = 1.0, 1.0
nx, ny = 2, 2
# define the domain with quadrilateral mesh
msh = mesh.create_rectangle(comm=MPI.COMM_WORLD,
points=((0.0, 0.0), (Lx, Ly)),
n=(nx, ny),
cell_type=mesh.CellType.quadrilateral)
vector_element = ufl.VectorElement("CG", msh.ufl_cell(), 1) # vector displacement
scalar_element = ufl.FiniteElement("CG", msh.ufl_cell(), 1) # scalar pressure
element = ufl.MixedElement(vector_element, scalar_element)
mixed_space = fem.FunctionSpace(msh, element)
M = mixed_space
V_displacement = M.sub(0) # displacement
P_pressure = M.sub(1) # pressure
ndim = msh.geometry.dim
fdim = ndim -1
# msh.topology.create_connectivity(fdim, msh.topology.dim)
def bottom(x):
return np.isclose(x[1], 0, 1e-6)
def top(x):
return np.isclose(x[1], Ly, 1e-6)
The first way is:
V, submap = V_displacement.collapse()
dofs_bottom = fem.locate_dofs_geometrical(V, bottom)
u_xy_zero = np.array((0,)*ndim, dtype=ScalarType)
BC_bottom_xy= fem.dirichletbc(u_xy_zero, dofs_bottom, V)
which could be run in dolfinx
The second way fails to interpolate the value on the bottom, could someone tell me why?
u1_bc = fem.Function(V)
u1 = lambda x: np.zeros_like(x, dtype=ScalarType)
u1_bc.interpolate(u1)