[petsc-users] [petsc4py] dm/examples/tutorials/ex2 in python

Matthew Knepley knepley at gmail.com
Tue Jun 2 09:35:21 CDT 2015


On Tue, Jun 2, 2015 at 9:30 AM, Francesco Caimmi <francesco.caimmi at polimi.it
> wrote:

> On Tuesday 02 June 2015 07:43:21 Matthew Knepley wrote:
> > On Tue, Jun 2, 2015 at 4:52 AM, Francesco Caimmi <
> francesco.caimmi at gmail.com
> > > wrote:
> > > with global_vec as v:
> > >         for i in xrange(start,end):
> > >             v[i] = 5.0*rank
> >
> > The 'with' construction just uses
> >
> >   VecGetArray()
> >
> > which return the raw C pointer. This is indexed from 0 always, so you
> want
> >
> >   v[i-start] = 5.0*rank
> >
> > If you want to index using [start, end), you need something like
> >
> >   DMDAVecGetArray()
> >
> >   Thanks,
> >
> >      Matt
> Matt,
> thank you very much for your answer. Unfortunately I wasn't able to find
> documentation for the 'with' construction.
>
> As far as I understand using DMDAVecGetArray would give me an object with
> shape (M,N) in this case, not a vector with shape M*N as global_vec, so I
> would have to resort to something different than VecGetOwnershipRange to
> get
> the indexes.
> I would prefer to avoid it because I don't feel it would be very clear.
> Ithink I will go with the "v[i-start]" thing .
>
> Anyway, would it be ok to do as follows?
>
> for i in xrange(start,end):
>         global_vec[i] = 5.0*rank
>

No, you would need xrange(end-start):


> global_vec.assemblyBegin()
> global_vec.assemblyEnd()
>
> I ask because, while the program works, I cannot see assembly operation in
> the
> C code for ex2, so I am trying to figure out why.
> Is there some (python related ) reason that would make such an approach
> unwise?
>

Direct access to arrays can only happen for local values. Thus you do not
need the assembly calls.

  Thanks,

    Matt


> Thanks,
> --
> Francesco Caimmi
>



-- 
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/20150602/20bfcc86/attachment.html>


More information about the petsc-users mailing list