# Problem with porting fenics to fenicsx-code

**URL:** <https://fenicsproject.discourse.group/t/problem-with-porting-fenics-to-fenicsx-code/9005>\
**Category:** Linear Algebra\
**Created:** [August 12, 2022, 10:49am UTC](https://fenicsproject.discourse.group/t/problem-with-porting-fenics-to-fenicsx-code/9005 "2022-08-12T10:49:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TobiasFlow](https://avatars.discourse-cdn.com/v4/letter/t/eada6e/32.png) [@TobiasFlow](https://fenicsproject.discourse.group/u/TobiasFlow)\
**Post date:** [August 12, 2022, 10:49am UTC](https://fenicsproject.discourse.group/t/problem-with-porting-fenics-to-fenicsx-code/9005/1 "2022-08-12T10:49:09Z")

</div>

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)

```auto
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:

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [August 12, 2022, 12:45pm UTC](https://fenicsproject.discourse.group/t/problem-with-porting-fenics-to-fenicsx-code/9005/2 "2022-08-12T12:45:23Z")

</div>

Consider the following MWE:

```python
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[:,:])

```

---

<div class="post-metadata">

**Author:** ![TobiasFlow](https://avatars.discourse-cdn.com/v4/letter/t/eada6e/32.png) [@TobiasFlow](https://fenicsproject.discourse.group/u/TobiasFlow)\
**Post date:** [August 16, 2022, 3:00pm UTC](https://fenicsproject.discourse.group/t/problem-with-porting-fenics-to-fenicsx-code/9005/3 "2022-08-16T15:00:30Z")

</div>

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

Greetings, Tobias
