Switch - case in dolphin Expression

Asked by Chaitanya CH

Hi,

I am modeling resistance inside a cuboid. The resistance varies in steps along the length of the cube.

If it was varying in 2 steps.
0.0 - 0.5 : 100
0.5 - 1.0 : 200
resistance = Expression('x[0]>0 && x[0]<0.5 ? 100 : 200')

However in my case it varies in 4 steps. How do I write an Expression for this?
0.0 - 0.2 : 100
0.2 - 0.5 : 200
0.5 - 0.7 : 150
0.7 - 1.0 : 300

Is there an equivalent of switch - case ?
Thank you.

Question information

Language:
English Edit question
Status:
Solved
For:
FEniCS Project Edit question
Assignee:
No assignee Edit question
Solved by:
Johan Hake
Solved:
Last query:
Last reply:
Revision history for this message
Chaitanya CH (c-chintaluri) said :
#1

I am just adding a comment. Will this do the job? Is this efficient?

Expression('x[0]>0 && x[0]<0.2 ? 100 : (x[0]>0.2 && x[0]<0.5? 200:150)')

Revision history for this message
Best Johan Hake (johan-hake) said :
#2

On 12/13/2012 05:31 PM, Chaitanya CH wrote:
> Question #216715 on FEniCS Project changed:
> https://answers.launchpad.net/fenics/+question/216715
>
> Chaitanya CH posted a new comment:
> I am just adding a comment. Will this do the job? Is this efficient?
>
> Expression('x[0]>0 && x[0]<0.2 ? 100 : (x[0]>0.2 && x[0]<0.5? 200:150)')

You can try time it.

You can also interpolate the expression to a discrete Function over
cells. Then it will only be evaluated once.

Vs = FunctionSpace(mesh, "DG", 0)
resistance = Function(Vs)
resistance.interpolate(e)

Johan

Revision history for this message
Chaitanya CH (c-chintaluri) said :
#3

Thanks Johan Hake, that solved my question.