Corresponding functions in dolfinx

Hi all, I am converting my FeniCS code to FEniCS-X (dolfinx). Suppose we already have the left-hand side a and the right-hand side L, what are the corresponding functions of assemble(a).array(), assemble(L).get_local(), assemble_local(a, cell), and VectorSpace.dofmap().cell_dofs(el) in dolfinx?

from fenics import *
a = inner(sigma(u), epsilon(v))*dx # left-hand side of the weak form
L = dot(f, v)*dx + dot(T, v)*ds(1) # right-hand side of the weak form

K = assemble(a).array() # global stiffness matrix
F = assemble(L).get_local() # global external force vector

for el, cell in enumerate(cells(mesh)):
    ke = assemble_local(a, cell) # element stiffness matrix
    eDof = VectorSpace.dofmap().cell_dofs(el) # element dofs

Hi Yingqi_Jia,

You can refer to this link for a functionality similar to VectorSpace.dofmap().cell_dofs(el), Now each dofmap has a block size (we only store one index for each vector dimension as in the scalar case).

The recommended way for getting the local matrix is using A.getValuesCSR and wrapping with a scipy sparse matrix. I would not recommend using a dense array for that.

You can get the local part of a vector by:

b = dolfinx.assemble_vector(L)
with b.localForm() as b_local:
        b_local.set(0.0)

Dolfinx doesn’t have a function that corresponds to assemble_local, but you can create a custom assembler accessing the kernel generated by ffcx. You can find an example here.

4 Likes

Hi IgorBaratta, thanks for your reply. I am studying the code you shared assemble_cell.py · GitHub. However, I have three questions.

(1) When I try to test the function K = assemble_matrix(a) in this code, it does not work. The code is as follows.

# The beginning part is the same as assemble_cell.py in GitHub
# https://gist.github.com/IgorBaratta/d0b84fd5d77f2628204097a1c0b180fb

# Now we add the following lines
import dolfinx
import numpy as np
from mpi4py import MPI
from dolfinx.cpp.mesh import CellType
import ufl

mesh = dolfinx.RectangleMesh(MPI.COMM_WORLD, [np.array([0, 0, 0]), np.array([2, 2, 0])], [2, 1],     CellType.quadrilateral)
V = dolfinx.VectorFunctionSpace(mesh, ("CG", 1))
lambda_ = 1
mu = 1
T = dolfinx.Constant(mesh, (0, -1)) # traction
f = dolfinx.Constant(mesh, (0, 0)) # body force
def epsilon(u):
    return ufl.sym(ufl.grad(u)) # Equivalent to 0.5*(ufl.nabla_grad(u) + ufl.nabla_grad(u).T)
def sigma(u):
    return lambda_ * ufl.nabla_div(u) * ufl.Identity(u.geometric_dimension()) + 2*mu*epsilon(u)
ds = ufl.Measure("ds", domain=mesh)
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
a = ufl.inner(sigma(u), epsilon(v)) * ufl.dx
K = assemble_matrix(a)

The error message is:

 root@6a07b7581972:/shared# /usr/bin/python3 /shared/assemble_cell.py
 Traceback (most recent call last):
 File "/shared/assemble_cell.py", line 205, in <module>
 K = assemble_matrix(a)
 File "/shared/assemble_cell.py", line 65, in assemble_matrix
 ufc_form = dolfinx.jit.ffcx_jit(a, form_compiler_parameters={
 File "/usr/local/dolfinx-real/lib/python3.8/dist-packages/dolfinx/jit.py", line 60, in mpi_jit
 if comm.size == 1:
 AttributeError: 'Form' object has no attribute 'size'

However, when I use the function K = dolfinx.fem.assemble_matrix(a) in the official package, it does work.

(2) In addition, which type of parameter should I pass in active_entities of function assemble_matrix(a, active_entities=None) in assemble_cell.py · GitHub to obtain the element stiffness matrix?

(3) It seems FEniCS and FEniCS-X are excellent open-source projects, and I do find part of the source codes in GitHub. But where can I find the source code such as dolfinx.fem.assemble_matrix and dolfinx.fem.assemble_vector?

Thanks!

The python code (https://github.com/FEniCS/dolfinx/blob/main/python/dolfinx/fem/assemble.py#L226) is a wrapper ( https://github.com/FEniCS/dolfinx/blob/main/python/dolfinx/wrappers/fem.cpp#L314) for the following C++ function:
https://github.com/FEniCS/dolfinx/blob/main/cpp/dolfinx/fem/assemble_matrix_impl.h#L85

You can follow a similar path to get to assemble_vector

Hello, Yingqi_Jia. I also want to get the element stiffness matrix using dolfinx0.7.2, I would like to ask you for help, can you share with me the proven code to get the element stiffness matrix. Thank you very much.