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

vector expression in Expression

0 votes

Hi, everybody. I am a newer to fenics. So anyone's answer is helpful to me.

I want to get the below Expression.

f = Expression("x[0]", "x[1]", "u"), where u = Expression("x[0]", "x[1]").

So how do I get the right expression f ?

asked Mar 16, 2017 by fanronghong FEniCS User (1,680 points)

3 Answers

+1 vote
 
Best answer

If you want to use f as a source term in a mixed formulation, you should create three separate expressions and dot each of them with the test functions, i.e.

 v0,v1,v2 = TestFunctions(V)
 f0 = Expression("sin(x[0])")
 f1 = Expression("2.")
 f2 = Expression(("cos(x[0])","sin(x[0])"))
 L = f0*v0 + f1*v1 + dot(f2,v2)

provided that V is the function space.

answered Mar 22, 2017 by KristianE FEniCS Expert (12,900 points)
selected Mar 22, 2017 by fanronghong

Great, your idea is good.

Thank you very much for your help and your patience !!!

Thanks.

+1 vote
  u = Expression(("x[0]", "x[1]"))

should work in 2D, but also consider using

u = SpatialCoordinate(mesh)
answered Mar 17, 2017 by KristianE FEniCS Expert (12,900 points)

Sorry, maybe I didn't express my idea correctly.

Actually, I want to create an Expression like the below one:

f = Expression(( 'sin(x[0])', '2.0', ('cos(x[0])', 'sin(x[0])' ) ))

So how should I do ? Thank you very much !!!

You are still not expressing yourself clearly.

Sorry.

I mean I want to create an Expression, which is vector-valued and its third component also is vector-valued.

Am I clearly now ?

+1 vote

If you want a tensor Expression, you can do

 f = Expression((("sin(x[0])","2."),("cos(x[0])","sin(x[0])")))
answered Mar 22, 2017 by KristianE FEniCS Expert (12,900 points)

But I want that the first and second components of the Expression are scalar, but the third one is vector-valued

why do you want that?

Because in my function, the source term f is like the above Expression.

So you have a MixedFunctionSpace?

Yeah, a MixedFunctionSpace.
And one of the MixedFunctionSpace components is vector-valued.

...