Hello people,
I am confused on how Dirichlet boundary conditions are applied in several examples I’ve seen, with dolfinx. I have a code like the one at Computing fluxes accurately - #4 by dokken but the difference is that I am changing the part:
T_left = 100
# Define Dirichlet boundary conditions to the facets corresponding to the left end of the rod.
left_facets = facet_markers.find(T_fixed_surface)
left_dofs = locate_dofs_topological(V, mesh.topology.dim-1, left_facets)
bc = dirichletbc(ScalarType(T_left), left_dofs, V)
bcs = [bc]
To a time dependent boundary condition, say T_left = 10 * np.sin(t)
. Obviously it isn’t as simple at this simple statement.
One of my numerous attempt is:
# Define Dirichlet boundary conditions to the facets corresponding to the left end of the rod.
class time_dependent_T_left:
def __init__(self, t):
self.t = 0.0
def eval(self, x):
return np.full(x.shape[1], 10 * np.sin(self.t), dtype = ScalarType)
#return np.full(x.shape[1], 10 * np.sin(self.t))
T_left_expr = time_dependent_T_left()
T_left_expr.t = 0.0
bc_fun = Function(V)
bc_fun.interpolate(T_left_expr.eval)
left_facets = facet_markers.find(T_fixed_surface)
left_dofs = locate_dofs_topological(V, mesh.topology.dim-1, left_facets)
#print(dir(T_left_expr))
#exit()
bc = dirichletbc(bc_fun, left_dofs, V)
The errors I get are of the kind
Traceback (most recent call last):
File "/usr/local/dolfinx-real/lib/python3.10/dist-packages/dolfinx/fem/bcs.py", line 175, in dirichletbc
bc = bctype(_value, dofs, V)
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
1. dolfinx.cpp.fem.DirichletBC_float64(g: numpy.ndarray[numpy.float64], dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace<double>)
2. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Constant<double>, dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace<double>)
3. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double, double>, dofs: numpy.ndarray[numpy.int32])
4. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double, double>, dofs: Annotated[List[numpy.ndarray[numpy.int32]], FixedSize(2)], V: dolfinx::fem::FunctionSpace<double>)
Invoked with: <dolfinx.cpp.fem.Function_float64 object at 0x7f148725d070>, array([418, 422, 427, 428, 429, 430, 439, 443, 444, 445, 447, 448, 452],
dtype=int32), FunctionSpace(Mesh(blocked element (Basix element (P, tetrahedron, 1, equispaced, unset, False), (3,)), 0), Basix element (P, tetrahedron, 2, gll_warped, unset, False))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/root/shared/results.py", line 15, in <module>
simulation = thermal_simulation(heat_transfers, mesh_options, saving_options)
File "/root/shared/barra_calor.py", line 121, in thermal_simulation
bc = dirichletbc(bc_fun, left_dofs, V)
File "/usr/local/dolfinx-real/lib/python3.10/dist-packages/dolfinx/fem/bcs.py", line 177, in dirichletbc
bc = bctype(_value, dofs, V._cpp_object)
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
1. dolfinx.cpp.fem.DirichletBC_float64(g: numpy.ndarray[numpy.float64], dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace<double>)
2. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Constant<double>, dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace<double>)
3. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double, double>, dofs: numpy.ndarray[numpy.int32])
4. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double, double>, dofs: Annotated[List[numpy.ndarray[numpy.int32]], FixedSize(2)], V: dolfinx::fem::FunctionSpace<double>)
Invoked with: <dolfinx.cpp.fem.Function_float64 object at 0x7f148725d070>, array([418, 422, 427, 428, 429, 430, 439, 443, 444, 445, 447, 448, 452],
dtype=int32), <dolfinx.cpp.fem.FunctionSpace_float64 object at 0x7f1487251b30>
I don’t really understand what “x” stands for. It has shape (3, 1980), and the mesh has:
Info : 27 entities
Info : 86 nodes
Info : 366 elements
so I don’t know where the 1980 comes from. I guess I could fix the error I get by modifying the return statement in the time_dependent_T_left class, but I’m not sure how… Any guide is welcome.