<div dir="ltr">Thanks Lawrence, <div><br></div><div>fixed my issue by calling snes.setUp(), so I can access the linear PC from my NPC. </div><div>This requires pull request 339: <a href="https://bitbucket.org/petsc/petsc/pull-request/339/make-sure-vec_sol-is-not-null-before">https://bitbucket.org/petsc/petsc/pull-request/339/make-sure-vec_sol-is-not-null-before</a></div><div><br></div><div>At some point I'll learn to use app contexts in my python shells. For now, I'm using  _init_() to store any application-specific values in my class.</div><div><br></div><div>Asbjørn</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, 25 Jun 2015 at 22:17 Lawrence Mitchell <<a href="mailto:lawrence.mitchell@imperial.ac.uk">lawrence.mitchell@imperial.ac.uk</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
> On 24 Jun 2015, at 17:02, Asbjørn Nilsen Riseth <<a href="mailto:riseth@maths.ox.ac.uk" target="_blank">riseth@maths.ox.ac.uk</a>> wrote:<br>
><br>
> Hi all,<br>
><br>
> I'm currently trying to set up a nonlinear solver that uses NGMRES with NEWTONLS as a right preconditioner. The NEWTONLS has a custom linear preconditioner. Everything is accessed through petsc4py.<br>
><br>
> *Is there a way I can configure a NPC NEWTONLS KSP CompositePC without first calling solve on my outer snes?*<br>
><br>
> My NEWTONLS has the following KSP setup:<br>
> FGMRES<br>
> | PCCOMPOSITE<br>
> || PYTHON<br>
> || ILU<br>
><br>
> The way I understand things, the NPC is not created/set up until SNESSolve_NGMRES() is called. Therefore, I cannot call snes.getNPC().ksp.pc.getCompositePC(0).setPythonContext(ctx) before I have called snes.solve().<br>
><br>
> What I currently do is a try/except on snes.solve to create/set up the pccomposite. Then I can set my pythoncontext and it runs fine. This is quite ugly though, so I was hoping anyone would have a better approach.<br>
<br>
I think you can do the following:<br>
<br>
Define a class in some module:<br>
<br>
# (in module foo)<br>
class MyPC(object):<br>
<br>
    def setUp(self, pc):<br>
        # do any setup here.<br>
        pass<br>
<br>
    def apply(self, pc, x, y):<br>
        # apply preconditioner<br>
        pass<br>
<br>
And pass:<br>
<br>
-npc_sub_1_pc_type python -npc_sub_1_pc_python_context foo.MyPC<br>
<br>
You can pull the app context out in the class's setUp method by doing:<br>
<br>
ctx = pc.getDM().getAppCtx()<br>
<br>
Assuming that you're running with a branch that contains origin/knepley/fix-pc-composite-dm.<br>
<br>
Cheers,<br>
<br>
Lawrence<br>
</blockquote></div>