[petsc-users] Creating multiple Vecs with petsc4py

Jed Brown jed at jedbrown.org
Sat Feb 12 07:33:06 CST 2022


VecDuplicateVecs isn't implemented in petsc4py, but it internally just loops over VecDuplicate so you can use

qs = [x.duplicate() for i in range(4)]

y.maxpy(alphas, qs)


where the Python binding here handles qs being a Python array.

Samar Khatiwala <samar.khatiwala at earth.ox.ac.uk> writes:

> 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?
>
> 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?
>
> Thanks very much!
>
> Best,
>
> Samar


More information about the petsc-users mailing list