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

Reorder entries in vector-valued function

+5 votes

Given a Function f in the vector-valued FunctionSpace V*V, say (f(0), f(1)), how can I construct the function g = (-f(1), f(0))?

asked Jun 6, 2013 by nschloe FEniCS User (7,120 points)

1 Answer

+1 vote
 
Best answer

To get around all problems with needing to access the dofs directly, I suggest projecting the swapped components. If the spaces are the same, then you should get back the same result (to within the tolerance of the linear solver). Something like this:

W = V*V
f = Function(W)

g = project(as_vector((-f[1], f[0])), W)
answered Jun 6, 2013 by logg FEniCS Expert (11,790 points)
selected Jun 6, 2013 by nschloe
Hm,
```
from dolfin import *                                                            
                                                                                
mesh = UnitSquareMesh(20, 20)                                                   
V = FunctionSpace(mesh, 'CG', 1)                                                
                                                                                
W = V*V                                                                         
f = Function(W)                                                                 
                                                                                
g = project(as_vector(-f[1], f[0]), W)
```
gives the error
```
ufl.log.UFLException: Expecting Index object.
```
It should be

    g = project(as_vector((-f[1], f[0])), W)

I'll edit my answer above.

That seems quite inefficient. Is there not a cheaper way to do this?

@James: There is, of course, by fiddling dofs directly. Notice how Ander's post starts: To get around all problems with needing to access the dofs directly, ...

...