[petsc-users] Creating multiple Vecs with petsc4py

Matthew Knepley knepley at gmail.com
Sat Feb 12 07:30:09 CST 2022


On Sat, Feb 12, 2022 at 8:21 AM Samar Khatiwala <
samar.khatiwala at earth.ox.ac.uk> wrote:

> Hello,
>
> I’d like to create an array of Vecs in petsc4py by calling
> VecDuplicateVecs but I can’t find the corresponding method (I’ve tried
> various iterations such as q = x.duplicateVecs(4), etc).
> Is this not implemented in petsc4py? One workaround I’ve come up with is
> something like:
>
> q={}
> for i in range(0, 3):
>     q[i]=x.duplicate()
>
> Is there another/better way? And how do I then use PETSc functions that
> operate on Vecs (e.g., VecMAXPY)? Would I just call VecAXPY in a loop as
> above?
>

I don't think so, but maybe Lisandro has a suggestion. You can do this in
one line

q = [x.duplicate() for i in range(0, 3)]


> Ultimately, what I really want to do is wrap my own C functions with
> Cython that take an array of Vecs as an argument and then operate on them.
> (The function needs the entire array
> of Vecs to do its thing so I can’t loop over the elements of the array.)
> For instance, I want to pass the q above to myCfunc(Vec *q, Vec *qout). How
> do I go about doing that?
>

I think you can do the same thing as VecMAXPY,

    def maxpy(self, alphas, vecs):
        cdef PetscInt n = 0
        cdef PetscScalar *a = NULL
        cdef PetscVec *v = NULL
        cdef object tmp1 = iarray_s(alphas, &n, &a)
        cdef object tmp2 = oarray_p(empty_p(n),NULL, <void**>&v)
        assert n == len(vecs)
        cdef Py_ssize_t i=0
        for i from 0 <= i < n:
            v[i] = (<Vec?>(vecs[i])).vec
        CHKERR( VecMAXPY(self.vec, n, a, v) )

  Thanks,

     Matt

Thanks very much!
>
> Best,
>
> Samar
>
>

-- 
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

https://www.cse.buffalo.edu/~knepley/ <http://www.cse.buffalo.edu/~knepley/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20220212/a727640d/attachment-0001.html>


More information about the petsc-users mailing list