A question about assignment

Dear all,

I have a question about the assignment. For example:

V = VectorFunctionSpace(mesh, "CG", 1)
u1 = Function(V)
u2 = Function(V)
......
......
#method1:
u1.assign(u2)
#method2:
u1.vector()[:] = u2.vector()

Are u1.assign(u2) and u1.vector()[:] = u2.vector() the same?
Any help is appreciated.

In serial, yes. It is the same. In parallel the latter approach does not update ghost values. You would have to call ’u1.vector().apply(“insert”)` afterwards to ensure updates.

Got it. Thank you very much, Dokken. Could you please tell me which one you prefer to use in serial? Besides, in parallel u1.assign(u2) will update ghost values and doesn’t need any additional operations? So I think u1.assign(u2) is the better method both in serial and parallel. Can I say this? :slightly_smiling_face:

I don’t use legacy Dolfin much, so either would be fine. Working directly on the vector data is slightly faster than assign, as it does no checks regarding vector compatibility.

1 Like

Got it. Thank you very much, Dokken.