<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" dir="ltr">
<p></p>
<div>Dear Stefano,<br>
<br>
That was exactly what I needed. It works now, thank you!<br>
<br>
Regards,<br>
Elena</div>
<br>
<p></p>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Stefano Zampini <stefano.zampini@gmail.com><br>
<b>Sent:</b> 07 August 2025 19:55:12<br>
<b>To:</b> Moral Sanchez, Elena<br>
<b>Cc:</b> PETSc users list<br>
<b>Subject:</b> Re: [petsc-users] error using CG with matrix-free operator and MG preconditioner</font>
<div> </div>
</div>
<div>
<div dir="auto">
<div>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.
<a href="https://urldefense.us/v3/__https://petsc.org/release/petsc4py/petsc_python_types.html__;!!G_uCfscf7eWS!ci0JOiv6LHgM3Yfprnw7HmOcqzmdfgfp959aPpTEkIEr74xepQ4MSMrVMnS89dMTBA3e_vujFa3L5OfLfHuYdRsBecF9tispRqmV$">https://petsc.org/release/petsc4py/petsc_python_types.html</a></div>
<div><br>
</div>
<div data-smartmail="gmail_signature">Stefano</div>
</div>
<br>
<div class="gmail_quote gmail_quote_container">
<div dir="ltr" class="gmail_attr">On Thu, Aug 7, 2025, 19:52 Moral Sanchez, Elena <<a href="mailto:Elena.Moral.Sanchez@ipp.mpg.de">Elena.Moral.Sanchez@ipp.mpg.de</a>> wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">
<div id="m_1452812555748948361divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif" dir="ltr">
<p>Hello,<br>
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.
<br>
<br>
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:<br>
<br>
Traceback (most recent call last):<br>
  File "/home/elena-hp/code/Poisson_MG_Petsc.py", line 321, in <module><br>
    solver.solve(b, u)<br>
  File "petsc4py/PETSc/KSP.pyx", line 1782, in petsc4py.PETSc.KSP.solve<br>
petsc4py.PETSc.Error: error code 62<br>
[0] KSPSolve() at /home/elena-hp/petsc/src/ksp/ksp/interface/<a rel="noreferrer">itfunc.c:1089</a><br>
[0] KSPSolve_Private() at /home/elena-hp/petsc/src/ksp/ksp/interface/<a rel="noreferrer">itfunc.c:912</a><br>
[0] KSPSolve_CG() at /home/elena-hp/petsc/src/ksp/ksp/impls/cg/<a rel="noreferrer">cg.c:213</a><br>
[0] KSP_PCApply() at /home/elena-hp/petsc/include/petsc/private/<a rel="noreferrer">kspimpl.h:414</a><br>
[0] PCApply() at /home/elena-hp/petsc/src/ksp/pc/interface/<a rel="noreferrer">precon.c:528</a><br>
[0] PCApply_MG() at /home/elena-hp/petsc/src/ksp/pc/impls/mg/<a rel="noreferrer">mg.c:651</a><br>
[0] PCApply_MG_Internal() at /home/elena-hp/petsc/src/ksp/pc/impls/mg/<a rel="noreferrer">mg.c:629</a><br>
[0] PCMGMCycle_Private() at /home/elena-hp/petsc/src/ksp/pc/impls/mg/<a rel="noreferrer">mg.c:27</a><br>
[0] KSPSolve() at /home/elena-hp/petsc/src/ksp/ksp/interface/<a rel="noreferrer">itfunc.c:1089</a><br>
[0] KSPSolve_Private() at /home/elena-hp/petsc/src/ksp/ksp/interface/<a rel="noreferrer">itfunc.c:912</a><br>
[0] KSPSolve_CG() at /home/elena-hp/petsc/src/ksp/ksp/impls/cg/<a rel="noreferrer">cg.c:162</a><br>
[0] VecCopy() at /home/elena-hp/petsc/src/vec/vec/interface/<a rel="noreferrer">vector.c:1796</a><br>
[0] VecCopyAsync_Private() at /home/elena-hp/petsc/src/vec/vec/interface/<a rel="noreferrer">vector.c:1755</a><br>
[0] VecCopy_Nest() at /home/elena-hp/petsc/src/vec/vec/impls/nest/<a rel="noreferrer">vecnest.c:56</a><br>
[0] Invalid argument<br>
[0] Nest vector argument 2 not setup.<br>
<br>
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.<br>
<br>
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:<br>
<br>
Traceback (most recent call last):<br>
  File "petsc4py/PETSc/libpetsc4py.pyx", line 965, in petsc4py.PETSc.MatCreateVecs_Python<br>
petsc4py.PETSc.Error: error code 86<br>
[0] MatCreateVecs() at /home/elena-hp/petsc/src/mat/interface/<a rel="noreferrer">matrix.c:9557</a><br>
[0] VecSetType() at /home/elena-hp/petsc/src/vec/vec/interface/<a rel="noreferrer">vecreg.c:140</a><br>
[0] Unknown type. Check for miss-spelling or missing package: <a href="https://urldefense.us/v3/__https://petsc.org/release/install/install/*external-packages__;Iw!!G_uCfscf7eWS!dEBOuSXxIqEDpI33Xza0TlRPC5n6sN3fXNslohXzfITEmMwZLwQWJUTyNE-mMJHiXZjxQ-OlWu5625crKTR81qTlPMyhvkR09mUB$" target="_blank" rel="noreferrer">
https://petsc.org/release/install/install/#external-packages</a><br>
[0] Unknown vector type: nest</p>
<p>The above exception was the direct cause of the following exception:<br>
<br>
Traceback (most recent call last):<br>
  File "<string>", line 1, in <module><br>
  File "petsc4py/PETSc/Mat.pyx", line 3411, in petsc4py.PETSc.Mat.createVecs<br>
petsc4py.PETSc.Error: error code 101<br>
[0] MatCreateVecs() at /home/elena-hp/petsc/src/mat/interface/<a rel="noreferrer">matrix.c:9552</a><br>
<br>
Do you know how to fix this? </p>
<p>Thank you in advance.<br>
<br>
Regards,<br>
Elena<br>
</p>
</div>
</div>
</blockquote>
</div>
</div>
</body>
</html>