This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to work with MixedFunction components ?

+2 votes

I have a mixed function defined as

W = V_q*V_T
qT = Function(W)

To get the components of qT, I can use the split method as in:

(q,T) = qT.split()

How are (q,T) and qT related here ?
Does a modification of qT affect q and T and vice versa ?
How can this be done ?

Thanks.

asked Jun 14, 2013 by micdup FEniCS User (1,120 points)

2 Answers

+4 votes
 
Best answer

How are (q,T) and qT related here ?

Pair (q,T) is a view (shallow copy) into qT unless you do

(q,T) = qT.split(deepcopy=True)

in which case q,T have its own copies of vectors.

Does a modification of qT affect q and T and vice versa ?

In principle yes (in case of shallow copy).

How can this be done ?

In practice you can't access q.vector() and T.vector() for writing (in case of shallow copy). You can't either use q,T in definition of forms (this can be done using qT[0], qT[1] or split(qT)), interpolatate or project to them. You can only q.assign(foo) which will make q a new function having no relation to qT.

On the other hand one can do operations like

qT0 = Function(W)
q0,T0 = qT0.split()

u, v = TrialFunction(W), TestFunction(W) 
proj_form = inner(u, v)*dx
r,U = TestFunctions(W)
qT = Function(W)
solve(proj_form == 42.0*q0*r*dx + 66.0*T0*U*dx, qT)
answered Jun 14, 2013 by Jan Blechta FEniCS Expert (51,420 points)
selected Jun 17, 2013 by Jan Blechta

Thanks a lot.
I think I have in your anwser exactly what I need.
I will work on my script as soon as I can.

+2 votes

So far as I am aware, if you do:

q,T = qT.split()

you will be unable to change the values of q and T, so you have to work with qT.
You could though do things like:

l2 = norm(q)

If you want to have a copy to work with, you can use:

q,T = qT.split(True)

but that will not affect qT.

answered Jun 14, 2013 by chris_richardson FEniCS Expert (31,740 points)

Thank you.
It would have been nice to be able to modify
qT through a shallow copy of q and T. ...

...