Question about fenics.Function.vector().apply()

Hello everyone,

I have a quick question regarding the apply method in (legacy) fenics. Consider the following: If I want to change the vector values of some function with those provided in some petsc4py.PETSc.Vec object, I can use the following code, which works nicely in serial

from fenics import *

mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, "CG", 1)

u = Function(V)

x = Function(V)
x_vec = x.vector().vec()  # This usually comes from somewhere else

u.vector().vec().setArray(x_vec)

Now, when I run the code in parallel, things break. This is because the petsc4py.PETSc.Vec is not assembled correctly and ghost values are not updated correctly. In order to fix it in parallel, one has to add a call to

u.vector().apply("")

after the setArray method. My question is: What does the argument of the apply method actually do? As far as I have gathered from the source code here, calling .apply("") should be similar (or even identical) to calling the petsc4py methods

u.vector().vec().assemble()
u.vector().vec().ghostUpdate()

or, more MPI friendly

u.vector().vec().assemblyBegin()
u.vector().vec().assemblyEnd()
u.vector().vec().ghostUpdateBegin()
u.vector().vec().ghostUpdateEnd()

which is explained here.

From the C++ source code, I don’t see that the string mode is even used when the apply method is called. Why is this argument even used?
And further: Can I replace all calls to apply with the petsc4py.PETSc.Vec method assemble and ghostUpdate or is there something that I am overlooking / missing?

Thanks a lot in advance and best regards,
Sebastian

As legacy dolfin have different backends, the generic vector requires a mode, for instance tpetra:
https://bitbucket.org/fenics-project/dolfin/src/1c52e837eb54cc34627f90bde254be4aa8a2ae17/dolfin/la/TpetraVector.cpp?at=master#TpetraVector.cpp-79

There are times where you want to accumulate values and then you would use an insert mode in other backends than petsc.

You can use petsc update methods if you want to, i see no issue with that.

Great, thanks @dokken for the quick reply and the clarification!