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

How can I get two components of a tensorfunction?

0 votes

I am looking to extract two components of a tensor valued function and have naively tried

from dolfin import *
mesh = UnitCubeMesh(2,2,2)
VVV = TensorFunctionSpace(mesh, "DG", 0)

f = Function(VVV)
f2 = f[1:2,:]

This gives the error message
ufl.log.UFLException: Partial slices not implemented, only complete slices like [:]

can anyone suggest a good workaround?

asked Aug 18, 2014 by Gabriel Balaban FEniCS User (1,210 points)

1 Answer

+2 votes

You could use assign

from dolfin import *
mesh = UnitCubeMesh(2,2,2)
VVV = TensorFunctionSpace(mesh, "DG", 0)

f = interpolate(Expression((("0", "1", "2"), 
                        ("3", "4", "5"),
                        ("6", "7", "8"))), VVV)
f2 = Function(FunctionSpace(mesh, "DG", 0))
assign(f2, f.sub(4))
plot(f2) # plots number 4
answered Aug 18, 2014 by mikael-mortensen FEniCS Expert (29,340 points)

Thanks Mikael,
I expanded your idea to get the second component

from dolfin import *
mesh = UnitCubeMesh(2,2,2) 
VVV = TensorFunctionSpace(mesh, "DG", 0) 
VV = VectorFunctionSpace(mesh, "DG", 0, dim = 2)

f  = interpolate(Expression((("0", "1", "2"), 
                             ("3", "4", "5"),
                             ("6", "7", "8"))), VVV)

ff = Function(VV)
assign(ff.sub(0), f.sub(4))
assign(ff.sub(1), f.sub(5))
plot(ff, interactive = True)

It's interesting that this works

assign(ff.sub(1),  f.sub(4))

but this doesn't

assign(ff.sub(1),  f[1,2])

I see what you mean. You could create an issue on bitbucket requesting this from the assign function.

...