Veltz Romain <[email protected]> writes:

> Hi Petsc users,
>
> I would like to know how the following code must be modified in order to work 
> with the latest versions of Petsc4py.
>
> http://code.google.com/p/petsc4py/issues/attachmentText?id=11&aid=4044638157340430804&name=SNES_MatShell.py&token=e664eb9a46d94c453334a46c924d3e93
>
> Thank you for your help,

See attached.  In the future, it's a good idea to try updating the
example and let us know what went wrong.  We can't tell from your email
if anything needs updating, for example.

import petsc4py, sys
petsc4py.init(sys.argv)
From petsc4py import PETSc

class MyMat:
    def __init__(self):
        self.x_i = None
    def mult(self, A, v, y):
        # J(x)*v = 2*x*v
        y[...] = 2 * self.x_i * v

def fun(snes, x, f):
    # F = x**2
    f[...] = x * x

def jac(snes, x, J, P):
    # J(x)*v = 2*x*v
    msh = J.getPythonContext()
    msh.x_i = x.copy(msh.x_i)
    return False # same nz pattern

N = 1
msh = MyMat()
J = PETSc.Mat().createPython(N, msh)
J.setUp()
x, f = J.getVecs()
b = f.duplicate()

snes = PETSc.SNES().create()

ksp = snes.getKSP()
pc = ksp.getPC()
pc.setType(PETSc.PC.Type.NONE)

snes.setFunction(fun, f)
snes.setJacobian(jac, J, J)
snes.setFromOptions()

x[...] = 1
b[...] = 4

snes.solve(b,x)

Attachment: pgpEVthpjZcFj.pgp
Description: PGP signature

Reply via email to