Assembling Process

Hello,

I wrote this code in Fenicsx 0.8 and Dolfinx 0.7.2 but the error is given below:

import dolfinx
from mpi4py import MPI
import ufl
import numpy as np
from petsc4py import PETSc
from slepc4py import SLEPc
from dolfinx.fem import (FunctionSpace, Function, form, assemble_matrix)
from dolfinx.mesh import create_rectangle
from dolfinx.io import XDMFFile

Create mesh

width = 1.0
height = 0.5
mesh = create_rectangle(MPI.COMM_WORLD, [np.array([0, 0]), np.array([width, height])], [40, 20], cell_type=dolfinx.mesh.CellType.triangle)

Define the function space

degree = 3
V = FunctionSpace(mesh, (“Nedelec 1st kind H(curl)”, degree))

Define the test and trial functions

v = ufl.TestFunction(V)
u = ufl.TrialFunction(V)

def curl_t(w):
return ufl.Dx(w[1], 0) - ufl.Dx(w[0], 1)

s = curl_t(v) * curl_t(u) * ufl.dx
t = ufl.inner(v, u) * ufl.dx

Assemble the stiffness matrix (S) and mass matrix (T)

a_form = form(s)
m_form = form(t)

A = assemble_matrix(a_form)
A.assemble()
B = assemble_matrix(m_form)
B.assemble()

Convert assembled matrix to PETSc.Mat

A_petsc = PETSc.Mat().createAIJ(size=A.getSizes(), csr=(A.indptr, A.indices, A.data))
A_petsc.assemble()
B_petsc = PETSc.Mat().createAIJ(size=B.getSizes(), csr=(B.indptr, B.indices, B.data))
B_petsc.assemble()

Solve the eigensystem using SLEPc

eigensolver = SLEPc.EPS()
eigensolver.create(MPI.COMM_WORLD)
eigensolver.setOperators(A_petsc, B_petsc)
eigensolver.setProblemType(SLEPc.EPS.ProblemType.GHEP)
eigensolver.setWhichEigenpairs(SLEPc.EPS.Which.SMALLEST_REAL)
eigensolver.setFromOptions()
eigensolver.solve()

cutoff = None
for i in range(eigensolver.getConverged()):
eigenvalue = eigensolver.getEigenvalue(i)
if eigenvalue.real > 1 and eigenvalue.imag == 0:
cutoff = np.sqrt(eigenvalue.real)
break

if cutoff is None:
print(“Unable to find dominant mode”)
else:
print(“Cutoff frequency:”, cutoff)

Save the solution to file (optional)

xdmf_file = XDMFFile(mesh.comm, “waveguide_solution.xdmf”, “w”)
xdmf_file.write_mesh(mesh)

Extract and save the corresponding eigenvector

if cutoff is not None:
vr, _ = eigensolver.getEigenpair(i)
eigenvector = Function(V)
eigenvector.vector.setArray(vr)
eigenvector.vector.ghostUpdate()
xdmf_file.write_function(eigenvector)
xdmf_file.close()

AttributeError Traceback (most recent call last)
Cell In[2], line 40
37 m_form = form(t)
39 A = assemble_matrix(a_form)
—> 40 A.assemble()
41 B = assemble_matrix(m_form)
42 B.assemble()

AttributeError: ‘MatrixCSR’ object has no attribute ‘assemble’ In fact, I cannot understand the assembling process and correct code for assembling, What should I do? Also. can you check the whole code if possible, I converted this code from legacy Fenics.

Thank you,

Regards…

You must import `assemble_matrix` from `dolfinx.fem.petsc` (instead of `dolfinx.fem`) if you want it to return a PETSc matrix.