Another question about piecewise defined function

Asked by cutejeff

I have taken a look at Achim Schroll's question and the answers. But I still have trouble to make it run.

My problem is a little different, I need to apply an expression instead of constants in the piecewise function.

e.g.

alpha = Expression("1 + x[0]*x[0] + 3*x[1]*x[1] + 1.2*t")

class Alpha(Expression):
    def eval(self, v, x):
        v[0] = 0
        if x[0] > 0.5: v[0] = alpha
u0 = Alpha()
u_prev = interpolate(u0, V)

When I interpolate u0 to apply initial condition, the system gave me error like:

 line 28, in interpolate
    Pv.interpolate(v)
Exception: Swig director method error. Error detected when calling 'Expression.eval_data'

I tried to use just constants, it ran well. but it failed for the expressions. Anyone knows why? Thank you.

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Johan Hake
Solved:
Last query:
Last reply:
Revision history for this message
Best Johan Hake (johan-hake) said :
#1

Hello!

You need to _call_ alpha inside the eval method.

class Alpha(Expression):
    def eval(self, v, x):
        v[0] = 0
        if x[0] > 0.5: v[0] = alpha(x)

Worked fine for me.

I am sorry for the none-instructive error message you get. It should raise:

  ValueError: setting an array element with a sequence.

Not that this one is so much better ;)

I haven't figured out how to solve this. SWIG just detects an error in the
overloaded Python method but do not report which error. I think we once had
this in place but we lost it on the way...

Johan

On Monday 22 March 2010 23:45:35 cutejeff wrote:
> New question #105262 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/105262
>
> I have taken a look at Achim Schroll's question and the answers. But I
> still have trouble to make it run.
>
> My problem is a little different, I need to apply an expression instead of
> constants in the piecewise function.
>
> e.g.
>
> alpha = Expression("1 + x[0]*x[0] + 3*x[1]*x[1] + 1.2*t")
>
> class Alpha(Expression):
> def eval(self, v, x):
> v[0] = 0
> if x[0] > 0.5: v[0] = alpha
> u0 = Alpha()
> u_prev = interpolate(u0, V)
>
> When I interpolate u0 to apply initial condition, the system gave me error
> like:
>
> line 28, in interpolate
> Pv.interpolate(v)
> Exception: Swig director method error. Error detected when calling
> 'Expression.eval_data'
>
> I tried to use just constants, it ran well. but it failed for the
> expressions. Anyone knows why? Thank you.
>

Revision history for this message
cutejeff (illw84u) said :
#2

Thanks Johan Hake, that solved my question.

Revision history for this message
cutejeff (illw84u) said :
#3

Thank you very much. It works now.