[petsc-users] petsc4py KSP Question

Matthew Knepley knepley at gmail.com
Wed Mar 7 08:22:59 CST 2012


On Wed, Mar 7, 2012 at 8:18 AM, Gaetan Kenway <kenway at utias.utoronto.ca>wrote:

> Thanks. I have both the SNES and the linear KSP solver working the way I
> want.
>
> One more question: At the beginning of my non-linear SNES function I have
> something like
>
>    def formFunction(self, snes, X, F):
>
>         states = X.getArray()
>
> and a X.resetArray() at the end. Am I correct to assume that this is an
> efficient pointer assignment to the numpy array and does not involve a
> memory copy?
>

It is no-copy. You can use a 'with block, which is a little nicer, and I
think you mean restoreArray().

  Matt


> Thanks
>
> Gaetan
>
>
>
> On Wed, Mar 7, 2012 at 3:16 AM, Lisandro Dalcin <dalcinl at gmail.com> wrote:
>
>> On 7 March 2012 02:22, Gaetan Kenway <kenway at utias.utoronto.ca> wrote:
>> > Hello
>> >
>> > I'm in the process of using petsc4py to solve a large multidisciplinary,
>> > non-linear system and its adjoint. I have the non-linear solution with
>> > snes() working correctly and I'm now doing the linear solution.
>> >
>> > For the non-linear solve, I create the snes and set my user context as
>> > follows:
>> >    # Create SNES Object
>> >   ASContext = ASNKSolver(self) # Context to hold data
>> >   snes = PETSc.SNES().createPython(ASContext, comm=self.gcomm)
>> >   snes.setFunction(ASContext.formFunction, resVec)
>> >
>> > For the linear part, I'm a little confused. I currently have
>> >
>> >     # Create Python Context and KSP Object
>> >     KSPContext = AdjointKSPSolver(self) # Context to hold data
>> >     ksp = PETSc.KSP().createPython(KSPContext, comm=self.gcomm)
>> >
>> > What I'm not sure of is how to specify the operator for the KSP solver
>> in
>> > the KSPContext object. Is this possible? Is there something like def
>> apply()
>> > you must do?
>> >
>> > The petsc4py poisson2d.py example, first creates a Mat, then sets a
>> context
>> > for that, and then uses ksp.setOperators() to use that matrix. Is this
>> the
>> > only way to do it? If, so, what is the use of
>> > the  PETSc.KSP().createPython() command?
>> >
>>
>> Unless you want to implement a custom KSP/SNES in Python, you should
>> just use KSP/SNES.create(). KSP/SNES.createPython() is a rather
>> advanced feature, it is not obvious at all how to use them, there are
>> no demos nor documentation about it (however, there is some code in
>> test/test_{ksp|snes}_py.py ).
>>
>> Please take a look at the demo/bratu2d and demo/bratu3d about how to
>> setup a SNES solver. For KSP, take a look at
>> demo/kspsolve/petsc-mat.py and demo/kspsolve/petsc-ksp.py
>>
>>
>>
>> --
>> Lisandro Dalcin
>> ---------------
>> CIMEC (INTEC/CONICET-UNL)
>> Predio CONICET-Santa Fe
>> Colectora RN 168 Km 472, Paraje El Pozo
>> 3000 Santa Fe, Argentina
>> Tel: +54-342-4511594 (ext 1011)
>> Tel/Fax: +54-342-4511169
>>
>>
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20120307/24ea828a/attachment-0001.htm>


More information about the petsc-users mailing list