Problem with porting fenics to fenicsx-code

Hi People!
Im a working student and have to port python code, which uses the Fenics 2019 module to fenicsx-dolfinx 0.4.1. i have trouble understanding the code since im relativly new to the FEM-method, but for most of the code i have found the corresponding fenicsx methods. But there is one part of the code, where i cant find corresponding fenicsx functions.
(old fenics code)

BC_Diriclet = PETScMatrix()
assemble(lhs(B_real_vf),tensor=BC_Diriclet)
BC_Diriclet.zero()
for bc in bcs:
	bc.apply(BC_Diriclet)

If I understand the old-code correct, there is a petsc-matrix created, which just holds the Boundary conditions

First i have done something like this:

from dolfinx.fem import petsc
BC_Diriclet = petsc.assemble_matrix(fem.form(ufl.lhs(B_real_vf)), bcs=BClist)

But then the matrix still holds the values of the assembly of B_real_vf.
My second idea looks like this:

from dolfinx.fem import petsc
BC_Diriclet = petsc.assemble_matrix(fem.form(ufl.lhs(B_real_vf)))
BC_Diriclet.assemble()
AI, AJ, AV = BC_Diriclet.getValuesCSR()
newValues = np.zeros(AV.shape[0])
BC_Diriclet.setValuesCSR(AI, AJ, newValues)
BC_Diriclet.assemble()

but how can i set the boundary conditions now?
Thank you in advance,
Tobias

Consider the following MWE:

import numpy as np
from mpi4py import MPI

import dolfinx
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 2,2)
V = dolfinx.fem.FunctionSpace(mesh, ('Lagrange', 1))

mesh.topology.create_connectivity(mesh.topology.dim-1, mesh.topology.dim)
boundary_facets = dolfinx.mesh.exterior_facet_indices(mesh.topology)
bdofs = dolfinx.fem.locate_dofs_topological(V, mesh.topology.dim-1, boundary_facets)
def bc_func(x):
    return x[0] + 7*x[1]

u_bc = dolfinx.fem.Function(V)
u_bc.interpolate(bc_func)

bc = dolfinx.fem.dirichletbc(u_bc, bdofs)
dofs = bc.dof_indices()[0]
bc_vals = u_bc.x.array[dofs]
u_bc.x.array[:] = 0
u_bc.x.array[dofs] = bc_vals


pattern = dolfinx.cpp.la.SparsityPattern(mesh.comm, [V.dofmap.index_map, V.dofmap.index_map], 
                                                    [V.dofmap.index_map_bs, V.dofmap.index_map_bs])
pattern.insert_diagonal(np.arange(len(u_bc.vector.array), dtype=np.int32))
pattern.assemble()
A = dolfinx.cpp.la.petsc.create_matrix(mesh.comm, pattern)
A.setDiagonal(u_bc.vector)
A.assemble()
print(A[:,:])
1 Like

Thanks dokken! That helped me alot to solve my Problem.

Greetings, Tobias