Please note that your code has way too many imports and unused code. Here is a cleaned up version of your code:
from mpi4py import MPI
from petsc4py import PETSc
import numpy as np
from dolfinx.fem import (
Function,
functionspace,
dirichletbc,
locate_dofs_geometrical,
)
from dolfinx import mesh
from basix.ufl import element
from ufl import (
TestFunction,
TrialFunction,
)
t = 0
T = 10
num_steps = 500
dt = T / num_steps
lenght = 10
width = 10
# create the rectangular mesh
mesh = mesh.create_rectangle(
MPI.COMM_WORLD,
[np.array([0, 0]), np.array([lenght, width])],
[20, 20],
cell_type=mesh.CellType.triangle,
)
t = 0
T = 10
num_steps = 500
dt = T / num_steps
v_cg2 = element("Lagrange", mesh.topology.cell_name(), 2, shape=(mesh.geometry.dim,))
s_cg1 = element("Lagrange", mesh.topology.cell_name(), 1)
V = functionspace(mesh, v_cg2)
Q = functionspace(mesh, s_cg1)
u = TrialFunction(V)
v = TestFunction(V)
p = TrialFunction(Q)
q = TestFunction(Q)
fdim = mesh.topology.dim - 1
def walls(x):
return np.isclose(x[1], 0)
wall_dofs = locate_dofs_geometrical(V, walls)
u_noslip = np.array((0,) * mesh.geometry.dim, dtype=PETSc.ScalarType)
bcu_noslip = dirichletbc(u_noslip, wall_dofs, V)
def inflow(x):
return np.isclose(x[0], 0)
u_ref = 5.5
z_ref = 2.0
class InletVelocity:
def __call__(self, x):
values = np.zeros((2, x.shape[1]), dtype=PETSc.ScalarType)
values[0] = u_ref * (x[1] / z_ref) ** 0.16
return values
inflow_dofs = locate_dofs_geometrical(V, inflow)
# Inlet
u_inlet = Function(V)
inlet_velocity = InletVelocity()
u_inlet.interpolate(inlet_velocity)
bcu_inflow = dirichletbc(u_inlet, inflow_dofs,V)
You get an error (which you should post next time)
Traceback (most recent call last):
File "/root/shared/mwe.py", line 80, in <module>
bcu_inflow = dirichletbc(u_inlet, inflow_dofs, V)
File "/usr/local/dolfinx-real/lib/python3.10/dist-packages/dolfinx/fem/bcs.py", line 189, in dirichletbc
bc = bctype(_value, dofs, V._cpp_object)
TypeError: __init__(): incompatible function arguments. The following argument types are supported:
1. __init__(self, g: ndarray[dtype=float64, writable=False, order='C'], dofs: ndarray[dtype=int32, writable=False, shape=(*), order='C'], V: dolfinx.cpp.fem.FunctionSpace_float64) -> None
2. __init__(self, g: dolfinx.cpp.fem.Constant_float64, dofs: ndarray[dtype=int32, writable=False, shape=(*), order='C'], V: dolfinx.cpp.fem.FunctionSpace_float64) -> None
3. __init__(self, g: dolfinx.cpp.fem.Function_float64, dofs: ndarray[dtype=int32, writable=False, shape=(*), order='C']) -> None
4. __init__(self, g: dolfinx.cpp.fem.Function_float64, dofs: list[ndarray[dtype=int32, writable=False, shape=(*), order='C']], V: dolfinx.cpp.fem.FunctionSpace_float64) -> None
Invoked with types: dolfinx.cpp.fem.DirichletBC_float64, dolfinx.cpp.fem.Function_float64, ndarray, dolfinx.cpp.fem.FunctionSpace_float64
because you are sending in V
as the last argument to dirichletbc
.
The reason for this throwing an error is because you are sending in a function from the space V
, and then you only need two inputs to dirichletbc
:
bcu_inflow = dirichletbc(u_inlet, inflow_dofs)
Consider the following MWE
from dolfinx.fem import set_bc
from dolfinx.io import VTXWriter
u_bc = Function(V)
u_bc.x.array[:] = 3.14 # Some unique value
set_bc(u_bc.x.array, [bcu_inflow, bcu_noslip])
with VTXWriter(mesh.comm, "bc_test.bp", [u_bc], engine="BP4") as bp:
bp.write(0.0)
which yields