[petsc-users] Shell matrix in petsc4py?

Gaetan Kenway gaetank at gmail.com
Wed Jul 30 20:01:22 CDT 2014


Hi Jonathan

I'm not a developer of petsc4py but have it extensively. I'm not entirely
sure what setUp() does only that it has to be called before the matrix can
be used.

As for the implementation, I'm fairly sure that the x and y vectors need to
be petsc4py PETSc Vecs. Mostly likely what you will may want to do
something like this:

def mult(self, A, X, Y):
    with X as x: # x is now a numpy array
        with Y as y: # y is also a numpy array
              y = do_something(x)

Hope that helps.

Gaetan



On Wed, Jul 30, 2014 at 7:56 PM, Jonathan Tu <jonathantu at gmail.com> wrote:

> Hi Gaetan,
>
> Thanks for pointing me to the example.  I think I understand more now.  It
> seems that setPythonContext should point to an object that performs the
> multi(x, y) operation, which takes x and modifies y inside the multi()
> function.  What does setUp() do?
>
> Also, can I assume that the implementation is Pythonic, such that the
> shell can operate on my own custom vector objects, rather than PETSc Vecs?
>  Defining the multi function to do this would be easy, but I wanted to make
> sure that the KSP solver wouldn't complain for some reason.
>
>
>
> Thanks,
> Jonathan Tu
>
>
> On Jul 30, 2014, at 4:48 PM, Gaetan Kenway <gaetank at gmail.com> wrote:
>
> Hi Jonathan
>
> I've successfully used petsc shell matrices from Python. There is an
> example of how to do it in demo/poisson2d/poisson2d.py. The example is a
> little sparse, but it has all the important information. The most important
> is to define the
>
> def mult(self, mat, x, y):
>
> routine inside of a Python class that is given in mat.setPythonContext()
> command.
>
> Hope that helps.
>
> Gaetan
>
>
> On Wed, Jul 30, 2014 at 7:29 PM, Jonathan Tu <jonathantu at gmail.com> wrote:
>
>> Hi,
>>
>> I would like to solve a large linear system.  I have a linear operator
>> that operates on a "state" vector.  The linear operator does things like
>> take the sum of the state elements, take the cross product of the state
>> with a known vector, and then multiply the state by a large (distributed)
>> matrix.  You could imagine it like this:
>>
>> [sum of elements, cross product with other vector, multiplication with
>> large matrix] = A * x
>>
>> Now, I realize it is possible to implement all of this using a large
>> matrix.  After all, these are all linear operations.  However, I thought it
>> might be faster and less error-prone to use some of numpy's built-in
>> operations, for the sums and cross products, and only use a distributed
>> matrix for the last part of the output.
>>
>> From the PETSc documentation, it seems like a matrix shell would be
>> perfect for this.  I could define my own mult operator that does all of
>> these things in steps, using built-in numpy functions when appropriate and
>> direct matrix multiplication when appropriate.  However, I cannot figure
>> out how to do this in petsc4py.  I found somewhere online that perhaps
>> createPython is the way I need to go about this, but I couldn't figure out
>> the details.
>>
>> Any ideas?
>>
>>
>>
>> Jonathan Tu
>>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20140730/24112ef3/attachment.html>


More information about the petsc-users mailing list