# Function assignment

Asked by Martin Vymazal on 2013-02-11

Hello,

could you please provide some clarification about assignment of functions in dolfin (C++). I have a function w defined on a mixed space W = V*Q and I'd like to do the following:

a) Initialize u (defined on V) and p (defined on Q).
b) In a loop, solve for w and update function u as u <- w[0]. I need this because u is a coefficient in my weak form.

The only way I was able to compile my code for the moment was to do

Function u = w[0]; // For part a)

I imagine that the object 'Function' holds nodal values over the mesh. Is the assignment above just a shallow copy (i.e. u and w[0] point to the same data in memory), or deep copy? I need u to hold its own data. How can I do this correctly? Maybe using 'interpolate' ?

Suppose I have a bilinear form a with coefficient u0 and I say
a.u0 = u;
u = w[0];

Does this actually change the values seen by a.u0 ?

For part b, I do for the moment
u.interpolate(w[0]);

When I make my iterative loop, I see that the residuals printed by the linear solver in each iteration are precisely the same, therefore I think that I did not actually update u (which my weak form depends on) by w[0]. I'm wondering if this is the effect of the 'shallow copy issue' when I wrote
Function u = w[0];

Unfortunately I don't know how to do it otherwise.

Thank you very much.

Martin Vymazal

## Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Garth Wells
Solved:
2013-02-12
Last query:
2013-02-12
2013-02-11
 Garth Wells (garth-wells) said on 2013-02-11: #1

On 11 February 2013 00:15, Martin Vymazal
> New question #221552 on DOLFIN:
>
> Hello,
>
> could you please provide some clarification about assignment of functions in dolfin (C++). I have a function w defined on a mixed space W = V*Q and I'd like to do the following:
>
> a) Initialize u (defined on V) and p (defined on Q).
> b) In a loop, solve for w and update function u as u <- w[0]. I need this because u is a coefficient in my weak form.
>
> The only way I was able to compile my code for the moment was to do
>
> Function u = w[0]; // For part a)
>

Do

Function& u = w[0];
Function& p = w[1];

(note the '&') outside of a loop, and use u in your second problem. u
and p are views into w. They all share the same vector. If w is
updated, then u and p will be updated.

Garth

> I imagine that the object 'Function' holds nodal values over the mesh. Is the assignment above just a shallow copy (i.e. u and w[0] point to the same data in memory), or deep copy? I need u to hold its own data. How can I do this correctly? Maybe using 'interpolate' ?
>
> Suppose I have a bilinear form a with coefficient u0 and I say
> a.u0 = u;
> u = w[0];
>
> Does this actually change the values seen by a.u0 ?
>
> For part b, I do for the moment
> u.interpolate(w[0]);
>
> When I make my iterative loop, I see that the residuals printed by the linear solver in each iteration are precisely the same, therefore I think that I did not actually update u (which my weak form depends on) by w[0]. I'm wondering if this is the effect of the 'shallow copy issue' when I wrote
> Function u = w[0];
>
> Unfortunately I don't know how to do it otherwise.
>
> Could anyone please explain this?
>
> Thank you very much.
>
> Martin Vymazal
>
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.

 Martin Vymazal (martin-vymazal-gmail) said on 2013-02-12: #2

Thanks Garth Wells, that solved my question.