Dear Stefano,

That was exactly what I needed. It works now, thank you!

Regards,
Elena


________________________________
From: Stefano Zampini <stefano.zamp...@gmail.com>
Sent: 07 August 2025 19:55:12
To: Moral Sanchez, Elena
Cc: PETSc users list
Subject: Re: [petsc-users] error using CG with matrix-free operator and MG 
preconditioner

If the problem is that the vector is not of nest type, you can overload the 
createVecs for the python matrix type, see e.g. 
https://urldefense.us/v3/__https://petsc.org/release/petsc4py/petsc_python_types.html__;!!G_uCfscf7eWS!ci0JOiv6LHgM3Yfprnw7HmOcqzmdfgfp959aPpTEkIEr74xepQ4MSMrVMnS89dMTBA3e_vujFa3L5OfLfHuYdRsBecF9tispRqmV$
 

Stefano

On Thu, Aug 7, 2025, 19:52 Moral Sanchez, Elena 
<elena.moral.sanc...@ipp.mpg.de<mailto:elena.moral.sanc...@ipp.mpg.de>> wrote:

Hello,
I want to solve a linear system with conjugate gradient and multigrid 
preconditioner. I am using petsc4py. The solution is a Vector of type NEST and 
the operator is a matrix-free operator created using PETSc.Mat().createPython. 
The restriction and interpolation operators are all type NEST and their VecType 
is set to NEST. As smoother I am using CG without preconditioner. For the 
moment I am just using a classical V-cycle with 2 levels.

I am able to solve the system with CG without the MG preconditioner. However, 
when I try to solve it with the MG preconditioner, it throws this error:

Traceback (most recent call last):
  File "/home/elena-hp/code/Poisson_MG_Petsc.py", line 321, in <module>
    solver.solve(b, u)
  File "petsc4py/PETSc/KSP.pyx", line 1782, in petsc4py.PETSc.KSP.solve
petsc4py.PETSc.Error: error code 62
[0] KSPSolve() at /home/elena-hp/petsc/src/ksp/ksp/interface/itfunc.c:1089
[0] KSPSolve_Private() at 
/home/elena-hp/petsc/src/ksp/ksp/interface/itfunc.c:912
[0] KSPSolve_CG() at /home/elena-hp/petsc/src/ksp/ksp/impls/cg/cg.c:213
[0] KSP_PCApply() at /home/elena-hp/petsc/include/petsc/private/kspimpl.h:414
[0] PCApply() at /home/elena-hp/petsc/src/ksp/pc/interface/precon.c:528
[0] PCApply_MG() at /home/elena-hp/petsc/src/ksp/pc/impls/mg/mg.c:651
[0] PCApply_MG_Internal() at /home/elena-hp/petsc/src/ksp/pc/impls/mg/mg.c:629
[0] PCMGMCycle_Private() at /home/elena-hp/petsc/src/ksp/pc/impls/mg/mg.c:27
[0] KSPSolve() at /home/elena-hp/petsc/src/ksp/ksp/interface/itfunc.c:1089
[0] KSPSolve_Private() at 
/home/elena-hp/petsc/src/ksp/ksp/interface/itfunc.c:912
[0] KSPSolve_CG() at /home/elena-hp/petsc/src/ksp/ksp/impls/cg/cg.c:162
[0] VecCopy() at /home/elena-hp/petsc/src/vec/vec/interface/vector.c:1796
[0] VecCopyAsync_Private() at 
/home/elena-hp/petsc/src/vec/vec/interface/vector.c:1755
[0] VecCopy_Nest() at /home/elena-hp/petsc/src/vec/vec/impls/nest/vecnest.c:56
[0] Invalid argument
[0] Nest vector argument 2 not setup.

The error does not appear when the operator is also of type NEST and the 
VecType is set to NEST. Unfortunately, for my application I cannot write the 
operator as type NEST.

I think that the problem is related to the fact that the method createVecs() of 
the matrix-free operator does not return NEST vectors. I tried to do 
setVecType(PETSc.Vec.Type.NEST) on the matrix-free operator but when I call 
createVecs() it throws this error:

Traceback (most recent call last):
  File "petsc4py/PETSc/libpetsc4py.pyx", line 965, in 
petsc4py.PETSc.MatCreateVecs_Python
petsc4py.PETSc.Error: error code 86
[0] MatCreateVecs() at /home/elena-hp/petsc/src/mat/interface/matrix.c:9557
[0] VecSetType() at /home/elena-hp/petsc/src/vec/vec/interface/vecreg.c:140
[0] Unknown type. Check for miss-spelling or missing package: 
https://urldefense.us/v3/__https://petsc.org/release/install/install/*external-packages__;Iw!!G_uCfscf7eWS!ci0JOiv6LHgM3Yfprnw7HmOcqzmdfgfp959aPpTEkIEr74xepQ4MSMrVMnS89dMTBA3e_vujFa3L5OfLfHuYdRsBecF9tkMvgYZ5$
 
<https://urldefense.us/v3/__https://petsc.org/release/install/install/*external-packages__;Iw!!G_uCfscf7eWS!dEBOuSXxIqEDpI33Xza0TlRPC5n6sN3fXNslohXzfITEmMwZLwQWJUTyNE-mMJHiXZjxQ-OlWu5625crKTR81qTlPMyhvkR09mUB$>
[0] Unknown vector type: nest

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "petsc4py/PETSc/Mat.pyx", line 3411, in petsc4py.PETSc.Mat.createVecs
petsc4py.PETSc.Error: error code 101
[0] MatCreateVecs() at /home/elena-hp/petsc/src/mat/interface/matrix.c:9552

Do you know how to fix this?

Thank you in advance.

Regards,
Elena

Reply via email to