export function initialized by vector to PVD file in parallel

Asked by Doug Arnold

As explained in the eigenvalue demo, once an eigenvector has been computed, it is convenient to use the vector to initialize a finite element function for visualization. However, this does not seem to work when run in parallel. E.g., the following code will generate a PETSc error

r, c, rx, cx = eigensolver.get_eigenpair(0)
u = Function(V, rx)
File("u.pvd") << u

Is there a way around this?

Question information

Language:
English Edit question
Status:
Answered
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

Revision history for this message
Doug Arnold (dnarnold) said :
#1

Sorry, dumb question. Although the syntax above (taken from the eigenvalue demo) works only in
serial, the syntax

  u = Function(V)
  u.vector()[:] = rx
  File("u.pvd") << u

works in serial and parallel, so solves my problem.

Revision history for this message
Doug Arnold (dnarnold) said :
#2

It seems I was wrong. From what I can tell

  u = Function(V, rx) [BAD]

never works in parallel.

  u = Function(V) [BAD]
  u.vector()[:] = rx

sometimes works in parallel, but sometimes generates PETSc errors about ghost variables.

However a third possibility seems to work reliably.

  u = Function(V) [GOOD]
  u.vector().set_local(rx.array())

Can anyone more knowledgeable confirm that this is the right way to attach a vector to a function?

Revision history for this message
Garth Wells (garth-wells) said :
#3

On Wed, Oct 3, 2012 at 2:01 AM, Doug Arnold
<email address hidden> wrote:
> Question #210114 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/210114
>
> Status: Solved => Open
>
> Doug Arnold is still having a problem:
> It seems I was wrong. From what I can tell
>
> u = Function(V, rx) [BAD]
>
> never works in parallel.
>

The above can work if rx is a Vector and the dof map and the vector
have the same parallel partitioning.

> u = Function(V) [BAD]
> u.vector()[:] = rx
>
> sometimes works in parallel, but sometimes generates PETSc errors about
> ghost variables.
>

Can work if the Vectors u.vector() and rx have the same parallel layout.

> However a third possibility seems to work reliably.
>
> u = Function(V) [GOOD]
> u.vector().set_local(rx.array())
>

This sets the local (to the process) values of u equal to rx. The size
of rx should be equal to the size of u on the process, in which case
it will always work.

> Can anyone more knowledgeable confirm that this is the right way to
> attach a vector to a function?
>

The most powerful approach is

   u.add(rx, rows)
   u.apply("add")

where rx are values and rows is a list of positions in u that the rx
values should be added to. This also works across processes.

Garth

> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.

Can you help with this problem?

Provide an answer of your own, or ask Doug Arnold for more information if necessary.

To post a message you must log in.