If you add:
print(f"Convergence reason: { solver.getConvergedReason()}")
to your code, you will see that it prints
Convergence reason: -9
which in petsc terms means KSPConvergedReason
KSP_DIVERGED_NANORINF
I think the first issue is that Hypre does not support complex numbers, see: Summary of Sparse Linear Solvers Available In PETSc — PETSc 3.16.5 documentation
In real mode I think your problem executes without issues.
Secondly, to use AMS, you need to supply some extra information:
from dolfinx.cpp.fem.petsc import discrete_gradient, interpolation_matrix
pc = solver.getPC()
V_CG = FunctionSpace(msh, ("CG", 1))._cpp_object
G = discrete_gradient(V_CG, V._cpp_object)
G.assemble()
pc.setHYPREDiscreteGradient(G)
cvec_0 = Function(V)
cvec_0.interpolate(lambda x: np.vstack((np.ones_like(x[0]),
np.zeros_like(x[0]),
np.zeros_like(x[0]))))
cvec_1 = Function(V)
cvec_1.interpolate(lambda x: np.vstack((np.zeros_like(x[0]),
np.ones_like(x[0]),
np.zeros_like(x[0]))))
cvec_2 = Function(V)
cvec_2.interpolate(lambda x: np.vstack((np.zeros_like(x[0]),
np.zeros_like(x[0]),
np.ones_like(x[0]))))
pc.setHYPRESetEdgeConstantVectors(cvec_0.vector,
cvec_1.vector,
cvec_2.vector)
# Attach discrete gradient to preconditioner
# Only available in PETSc main post: https://gitlab.com/petsc/petsc/-/commit/ee0c4ed34c3ea92bb645a7c3669aa02417f19b28
# from ufl import VectorElement
# Vec_CG = FunctionSpace(msh, VectorElement("CG", msh.ufl_cell(), 2))
# Pi = interpolation_matrix(Vec_CG._cpp_object, V._cpp_object)
# Pi.assemble()
# pc.setHYPRESetInterpolations(mesh.geometry.dim, None, None, Pi, None)
which in DOLFINx real mode converges in 5 iterations as opposed to boomeramg
s 53 iterations (using N1curl of order 1).
N2curl of order 2
required changing the order in V_CG
to 2 and boomeramg
then converges in 1295 iterations, while AMS
takes 43 iterations.
This should be even faster if one uses the main branch of PETSc and the feature pc.setHYPRESetInterpolations
as shown in the last comment in my snippet.
The code I post above is using very new functionality from DOLFINx (merged into main last week).