 # How to transform matrices assembled in Fenics into matrices with standard order of DOF?

Hi,

I know how to match every rows of columns of the system matrices assembled in FEM to the nodes (coordinates) of real physical system. For example:

``````mesh = UnitIntervalMesh(2) # length = 1 m, num_ele = 1
n = FacetNormal(mesh)

R_elem = FiniteElement('DG', mesh.ufl_cell(), 1)
V_elem = FiniteElement('CG', mesh.ufl_cell(), 2)

W_elem = MixedElement([R_elem, V_elem])
W = FunctionSpace(mesh, W_elem)

dof_coordinates = W.tabulate_dof_coordinates()
print(tabulate.tabulate([[i, coord] for i, coord in enumerate(dof_coordinates)], headers=['Global dof index', 'dof coordinate']))
``````

However, I have problem in transforming matrices assembled in Fenics into matrices with standard order of DOF in real system (which matches the real coordinates).

For example, the matrix MM is generated in Fenics and its order of rows and columns doesn’t match the order of nodes (coordinates) of real physical system.

``````mm = dot(dp,p) * dX + dot(dq,q) * dX
kk = 1 / JJ * dot(dp,q.dx(0)) * dX - G*Ip * dot(dq.dx(0),p) * dX
MM = PETScMatrix()
KK = PETScMatrix()
assemble(mm, tensor=MM)
bc.apply(MM)
assemble(kk, tensor=KK)
bc.apply(KK)
``````

Therefore, I want Fenics to generate a matrix M_ordered, which is reordered and matches the coordinates. I would like your help, thanks!

Briefly, you can get the array of indices that will properly reorder your degrees of freedom by using the numpy command lexsort on the dof_coordinates:
https://numpy.org/doc/stable/reference/generated/numpy.lexsort.html