← Back to team overview

dolfin team mailing list archive

Re: [Question #104855]: piecewise defined function

 

Question #104855 on DOLFIN changed:
https://answers.launchpad.net/dolfin/+question/104855

    Status: Open => Answered

Anders Logg proposed the following answer:
When using eval() you need to subclass Expression (not Function).

That gives you an Expression that can be used in a variational
problem. But an Expression does not have a vector so if you need the
norm of the vector (I guess just for debugging), you need to project
or interpolate the Expression to a Function.

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

alpha = Alpha()
d = interpolate(alpha, V)

print " |d| = %g" %(d.vector().norm("l2"))

--
Anders


On Fri, Mar 19, 2010 at 08:49:35AM -0000, Achim Schroll wrote:
> New question #104855 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/104855
>
> Hi!
> My piecewise defined function alpha (see below) just returns zero.
> What is wrong? How to define it instead?
>
> Best, Achim
>
> # set parameters
> alpha1 = 0.2; alpha2 = 0.8
>
> # create mesh and finite element
> mesh = UnitSquare(20, 20)
> V = FunctionSpace(mesh, "CG", 1)
>
> # define trial & testfunctions
> u_trial = TrialFunction(V); phi = TestFunction(V)
>
> # define diffusion parameter
> class alpha(Function):
>     def __init__(self, V):
>         Function.__init__(self, V)
>     def eval(self, v, x):
>         v[0] = alpha1
>         if x[0] > 0.5: v[0] = alpha2
> d = alpha(V)
> print " |d| = %g" %(d.vector().norm("l2"))
>
>

-- 
You received this question notification because you are a member of
DOLFIN Team, which is an answer contact for DOLFIN.



Follow ups