Dear @dokken,
I appreciate your quick help.
I tried applying a vector valued BC on the dofs (hope that is the correct way).
The error trace throws a type error now.
from mpi4py import MPI
from dolfinx import fem, mesh, plot
import numpy as np
import ufl
from petsc4py.PETSc import ScalarType
from dolfinx.fem.petsc import LinearProblem
import pyvista as pv
domain = mesh.create_unit_square(comm=MPI.COMM_WORLD,
nx=32, ny=32,
cell_type=mesh.CellType.triangle)
P1 = ufl.FiniteElement('BDM', domain.ufl_cell(), 1)
P2 = ufl.FiniteElement('DG', domain.ufl_cell(), 0)
element = ufl.MixedElement([P1, P2])
V = fem.FunctionSpace(domain, element)
# test and trial functions
tau, v = ufl.TestFunctions(V)
sigma, u = ufl.TrialFunctions(V)
# function determining if a node is on the tray top
def on_top_boundary(x):
return np.logical_or(np.isclose(x[0], 0),
np.isclose(x[0], 1))
V0, submap = V.sub(0).collapse()
# determine boundary DOFs
boundary_facets = mesh.locate_entities_boundary(domain, domain.topology.dim-1, on_top_boundary)
boundary_dofs = fem.locate_dofs_topological((V.sub(0), V0), domain.topology.dim-1, boundary_facets)
# apply Dirichlet (vector type) on identified dofs
u_zero = np.array((0,) * domain.geometry.dim, dtype=ScalarType) # vector type
bc = fem.dirichletbc(value=u_zero, dofs=boundary_dofs, V=V.sub(0)) # V.sub(0): Corresponds to BDM element
# apply Neumann BC
x = ufl.SpatialCoordinate(domain=domain)
f = 10.0 * ufl.exp(-((x[0] - 0.5)**2 + (x[1] - 0.5)**2) / 0.02)
g = ufl.sin(5 * x[0])
a = (ufl.dot(sigma, tau) + ufl.div(tau) * u + ufl.div(sigma) * v) * ufl.dx
L = -ufl.inner(f, v) * ufl.dx + ufl.inner(g, v) * ufl.ds
# Solve
problem = LinearProblem(a, L, [bc], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()
Error occurs at line starting with “bc = fem.dirichletbc(…”
Traceback (most recent call last):
File "/Users/anil/Apps/anaconda3/envs/fenics-env/lib/python3.8/site-packages/dolfinx/fem/bcs.py", line 125, in __init__
super().__init__(_value, dofs, V) # type: ignore
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: array([0., 0.]), [array([ 0, 1, 17, 18, 40, 41, 71, 72, 110, 111, 157,
158, 212, 213, 275, 276, 346, 347, 425, 426, 512, 513,
607, 608, 710, 711, 821, 822, 940, 941, 1067, 1068, 1202,
1203, 1345, 1346, 1496, 1497, 1655, 1656, 1822, 1823, 1997, 1998,
2180, 2181, 2371, 2372, 2570, 2571, 2777, 2778, 2992, 2993, 3215,
3216, 3446, 3447, 3685, 3686, 3932, 3933, 4187, 4188, 4194, 4195,
4447, 4448, 4692, 4693, 4929, 4930, 5158, 5159, 5379, 5380, 5592,
5593, 5797, 5798, 5994, 5995, 6183, 6184, 6364, 6365, 6537, 6538,
6702, 6703, 6859, 6860, 7008, 7009, 7149, 7150, 7282, 7283, 7407,
7408, 7524, 7525, 7633, 7634, 7734, 7735, 7827, 7828, 7912, 7913,
7989, 7990, 8058, 8059, 8119, 8120, 8172, 8173, 8217, 8218, 8254,
8255, 8283, 8284, 8304, 8305, 8317, 8318], dtype=int32), array([ 0, 1, 14, 15, 32, 33, 56, 57, 86, 87, 122,
123, 164, 165, 212, 213, 266, 267, 326, 327, 392, 393,
464, 465, 542, 543, 626, 627, 716, 717, 812, 813, 914,
915, 1022, 1023, 1136, 1137, 1256, 1257, 1382, 1383, 1514, 1515,
1652, 1653, 1796, 1797, 1946, 1947, 2102, 2103, 2264, 2265, 2432,
2433, 2606, 2607, 2786, 2787, 2972, 2973, 3164, 3165, 3170, 3171,
3360, 3361, 3544, 3545, 3722, 3723, 3894, 3895, 4060, 4061, 4220,
4221, 4374, 4375, 4522, 4523, 4664, 4665, 4800, 4801, 4930, 4931,
5054, 5055, 5172, 5173, 5284, 5285, 5390, 5391, 5490, 5491, 5584,
5585, 5672, 5673, 5754, 5755, 5830, 5831, 5900, 5901, 5964, 5965,
6022, 6023, 6074, 6075, 6120, 6121, 6160, 6161, 6194, 6195, 6222,
6223, 6244, 6245, 6260, 6261, 6270, 6271], dtype=int32)], FunctionSpace(Mesh(VectorElement(FiniteElement('Lagrange', triangle, 1), dim=2), 0), FiniteElement('Brezzi-Douglas-Marini', triangle, 1))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/anil/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3378, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-ee23d4688714>", line 1, in <module>
runfile('/Users/anil/Py/FEM/mixed_poisson.py', wdir='/Users/anil/Py/FEM')
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/anil/Py/FEM/mixed_poisson.py", line 99, in <module>
bc = fem.dirichletbc(value=u_zero, dofs=boundary_dofs, V=V.sub(0))
File "/Users/anil/Apps/anaconda3/envs/fenics-env/lib/python3.8/site-packages/dolfinx/fem/bcs.py", line 182, in dirichletbc
return formcls(value, dofs, V)
File "/Users/anil/Apps/anaconda3/envs/fenics-env/lib/python3.8/site-packages/dolfinx/fem/bcs.py", line 127, in __init__
super().__init__(_value, dofs, V._cpp_object) # type: ignore
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: array([0., 0.]), [array([ 0, 1, 17, 18, 40, 41, 71, 72, 110, 111, 157,
158, 212, 213, 275, 276, 346, 347, 425, 426, 512, 513,
607, 608, 710, 711, 821, 822, 940, 941, 1067, 1068, 1202,
1203, 1345, 1346, 1496, 1497, 1655, 1656, 1822, 1823, 1997, 1998,
2180, 2181, 2371, 2372, 2570, 2571, 2777, 2778, 2992, 2993, 3215,
3216, 3446, 3447, 3685, 3686, 3932, 3933, 4187, 4188, 4194, 4195,
4447, 4448, 4692, 4693, 4929, 4930, 5158, 5159, 5379, 5380, 5592,
5593, 5797, 5798, 5994, 5995, 6183, 6184, 6364, 6365, 6537, 6538,
6702, 6703, 6859, 6860, 7008, 7009, 7149, 7150, 7282, 7283, 7407,
7408, 7524, 7525, 7633, 7634, 7734, 7735, 7827, 7828, 7912, 7913,
7989, 7990, 8058, 8059, 8119, 8120, 8172, 8173, 8217, 8218, 8254,
8255, 8283, 8284, 8304, 8305, 8317, 8318], dtype=int32), array([ 0, 1, 14, 15, 32, 33, 56, 57, 86, 87, 122,
123, 164, 165, 212, 213, 266, 267, 326, 327, 392, 393,
464, 465, 542, 543, 626, 627, 716, 717, 812, 813, 914,
915, 1022, 1023, 1136, 1137, 1256, 1257, 1382, 1383, 1514, 1515,
1652, 1653, 1796, 1797, 1946, 1947, 2102, 2103, 2264, 2265, 2432,
2433, 2606, 2607, 2786, 2787, 2972, 2973, 3164, 3165, 3170, 3171,
3360, 3361, 3544, 3545, 3722, 3723, 3894, 3895, 4060, 4061, 4220,
4221, 4374, 4375, 4522, 4523, 4664, 4665, 4800, 4801, 4930, 4931,
5054, 5055, 5172, 5173, 5284, 5285, 5390, 5391, 5490, 5491, 5584,
5585, 5672, 5673, 5754, 5755, 5830, 5831, 5900, 5901, 5964, 5965,
6022, 6023, 6074, 6075, 6120, 6121, 6160, 6161, 6194, 6195, 6222,
6223, 6244, 6245, 6260, 6261, 6270, 6271], dtype=int32)], <dolfinx.cpp.fem.FunctionSpace object at 0x7ff3462c9870>