Hello,
I am following this code trying to implement it in a Vectorfunctionspace (Example linear elasticity and have following error:
Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
File ~/miniconda3/envs/fenicsx-env/lib/python3.10/site-packages/dolfinx/fem/bcs.py:125, in DirichletBCMetaClass.__init__(self, value, dofs, V)
124 try:
--> 125 super().__init__(_value, dofs, V) # type: ignore
126 except TypeError:
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)
2. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Constant<double>, dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace)
3. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double>, dofs: numpy.ndarray[numpy.int32])
4. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double>, dofs: List[numpy.ndarray[numpy.int32][2]], V: dolfinx::fem::FunctionSpace)
Invoked with: <dolfinx.cpp.fem.Function_float64 object at 0x7fc437d0fdf0>, array([3839, 3840, 3841, 3842, 3844, 3845, 3847, 3849, 3853, 4081, 4082,
4083, 4085, 4086, 4089, 4093, 4094, 4095, 4096, 4098, 4101, 4305,
4306, 4307, 4309, 4310, 4313, 4317, 4318, 4319, 4321, 4325, 4326,
4327, 4328, 4330, 4333, 4505, 4506, 4507, 4509, 4510, 4513, 4517,
4518, 4519, 4521, 4525, 4526, 4527, 4529, 4533, 4534, 4535, 4536,
4538, 4541, 4673, 4674, 4675, 4677, 4678, 4681, 4685, 4686, 4687,
4689, 4693, 4694, 4695, 4697, 4701, 4702, 4703, 4705, 4709, 4710,
4711, 4712, 4714, 4717, 4801, 4802, 4803, 4805, 4809, 4810, 4811,
4813, 4817, 4818, 4819, 4821, 4825, 4826, 4827, 4829, 4881, 4882,
4883, 4885, 4889, 4890, 4891, 4893, 4897, 4898, 4899, 4901, 4929,
4930, 4931, 4933, 4937, 4938, 4939, 4941, 4953, 4954, 4955, 4957],
dtype=int32), FunctionSpace(Mesh(VectorElement(Basix element (P, hexahedron, 1, gll_warped, unset, False), 3), 12), VectorElement(Basix element (P, hexahedron, 2, gll_warped, unset, False), 3))
...
4711, 4712, 4714, 4717, 4801, 4802, 4803, 4805, 4809, 4810, 4811,
4813, 4817, 4818, 4819, 4821, 4825, 4826, 4827, 4829, 4881, 4882,
4883, 4885, 4889, 4890, 4891, 4893, 4897, 4898, 4899, 4901, 4929,
4930, 4931, 4933, 4937, 4938, 4939, 4941, 4953, 4954, 4955, 4957],
dtype=int32), <dolfinx.cpp.fem.FunctionSpace object at 0x7fc437838430>
And here is my code:
import numpy as np
import ufl
from petsc4py import PETSc
from mpi4py import MPI
from dolfinx import fem, mesh, plot
from dolfinx.fem import Function, FunctionSpace, Constant
from ufl import TestFunction, dx, inner
L = 20.0
domain = mesh.create_box(MPI.COMM_WORLD,[[0.0,0.0,0.0],[L,1,1]],[20,5,5], mesh.CellType.hexahedron)
V = fem.VectorFunctionSpace(domain, ("CG", 2))
def left(x):
return np.isclose(x[0], 0)
def right(x):
return np.isclose(x[0], L)
fdim = domain.topology.dim -1
left_facets = mesh.locate_entities_boundary(domain, fdim, left)
right_facets = mesh.locate_entities_boundary(domain, fdim, right)
# Concatenate and sort the arrays based on facet indices. Left facets marked with 1, right facets with 2.as_integer_ratio
marked_facets = np.hstack([left_facets,right_facets])
marked_values = np.hstack([np.full_like(left_facets,1), np.full_like(right_facets,2)])
sorted_facets = np.argsort(marked_facets)
facet_tag = mesh.meshtags(domain, fdim, marked_facets[sorted_facets],marked_values[sorted_facets])
# boundary condition on the left side: fixed
u_bc = np.array((0,) * domain.geometry.dim, dtype=PETSc.ScalarType)
left_dofs = fem.locate_dofs_topological(V,facet_tag.dim, facet_tag.find(1))
bc_l = fem.dirichletbc(u_bc, left_dofs, V)
# B.C on the right side = a function to interpolate
import numpy as np
import ufl
from petsc4py import PETSc
from mpi4py import MPI
from dolfinx import fem, mesh, plot
from dolfinx.fem import Function, FunctionSpace, Constant
from ufl import TestFunction, dx, inner
from mpi4py import MPI
scale = 0.5
y0 = 0.5
z0 = 0.5
class MyExpression:
def __init__(self):
self.theta = np.pi/3
def eval(self, x):
return (np.zeros_like(x[1]),
scale * (y0 + (x[1] - y0) * np.cos(self.theta) - (x[2] - z0) * np.sin(self.theta) - x[1]),
scale * (z0 + (x[1] - y0) * np.sin(self.theta) + (x[2] - z0) * np.cos(self.theta) - x[2]))
f = MyExpression()
u_D = Function(V)
u_D.interpolate(f.eval)
# implement this function as DirichletBC at the right side of the beam
right_dofs = fem.locate_dofs_topological(V,facet_tag.dim, facet_tag.find(2))
bc_r = fem.dirichletbc(u_D,right_dofs, V)
bcs = [bc_l, bc_r]
It seems the problem is the u_D has a incompatible format for DirhicletBC? Can someone pls expain it?