[petsc-users] Push Back in Vector

Barry Smith bsmith at mcs.anl.gov
Thu Feb 25 20:18:47 CST 2010

On Feb 25, 2010, at 7:50 PM, Pedro Torres wrote:

> 2010/2/25 Jed Brown <jed at 59a2.org>
> On Thu, 25 Feb 2010 13:37:21 -0300, Pedro Torres <torres.pedrozpk at gmail.com 
> > wrote:
> > Well, in my case I have a sequential vector and I'm trying to  
> append one
> > element at the end of the array, without the need of destroy this.
> PETSc Vec doesn't work this way.  Use whatever data structure you like
> to put this thing together, then give it to PETSc once you know what  
> you
> have.
> > - Create a new vector with size+1
> > - copy old array in the new array and append the element,
> This is what dynamically sized array containers (e.g. std::vector) do
> internally, but they guess at how much extra will be needed (usually
> increasing the allocated size by a geometric factor).
> (When I asked what you are trying to do, I was looking for a higher
> level description of what you wanted to achieve, and why you thought
> appending to a Vec was a desirable thing.)
> The things is that I don't have the final dimension of the vector,  
> because it depends of mesh size, boundary conditions,etc. But I  
> think that I can do the following,
> - use std::vector
> - when I get final size, I create a Vec with VecCreateSeqWithArray  
> and Petsc will use the array in std::vector.

    You could do this. In general what we do is to "process" the mesh,  
boundary conditions etc to determine the needed size, set the vector  
size,  then go back and compute the vector/matrix entries.


> Is that right?
> Thanks a lot!
> Pedro
> Jed

More information about the petsc-users mailing list