← Back to team overview

dolfin team mailing list archive

Re: Work on a new function.py

 

2008/10/23 Johan Hake <hake@xxxxxxxxx>:
> Hello!
>
> I have worked on a "new" function.py. Please have a look. It is built around a
> metaclass for Functions. It makes it "easy" to create both a) userdefined
> python functions and b) compiled functions.
>
> The work flow is the same for both cases:
>
>  1) Define a class
>  2) Instantiate it with a FunctionSpace, and any user specific arguments
>
> With the suggested design a user can define classes a la:
>
>    class MyCustomFunction(Function):
>        def __init__(self,V,dummy):
>            # Note no call to Function.__init__!
>            self.dummy = dummy
>
>        def eval(self,val,x):
>            val[0] = 0.5
>            val[1] = 0.5
>
>        def dim(self):
>            return 2
>
>        def rank(self):
>            return 1
>
>    # Can also declare dim and rank as int attributes, see below
>
>    class MyCustomCompiledFunction(Function):
>        cppcode = "Jada"
>        dim  = 1
>        rank = 0
>
> They are then instantiated by:
>
>    mesh = UnitSquare(10,10)
>    element = FiniteElement('Lagrange','triangle',1)
>
>    print ""
>    V = FunctionSpace(mesh,element)
>
>    print ""
>    f0 = MyCustomFunction(V,"Dummy")
>
>    print ""
>    f1 = MyCustomCompiledFunction(V)
>
>    try:
>        # Cannot instantiate the Function class
>        f2 = Function(V)
>    except Exception, e:
>        print e
>
> With this interface only one user defined function can be compiled at a time,
> but we will gain in similare workflow between pure python and compiled
> functions.
>
> I realize that the C++ design must be in place first before we can advance,
> but this is a proof of principle, that we can use metaclasses to do nice
> stuff for the end user.
>
> Johan

A possible improvement: deducting dim and rank from cppcode like
compile_functions does today (can probably just reuse the code).
Also, compile_functions extracts variable names from the strings.
Then we can do just:

    class MyFunc(Function):
        cppcode = ("t*sin(x[0]", "cos(x[1])", "x[0]*x[2]")

    f = MyFunc(V)
    f.t = 0.0

Although the V here is a little too magic for my taste, it does save
space and looks very nice...

--
Martin


Follow ups

References