← Back to team overview

dolfin team mailing list archive

Work on a new function.py

 

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

Attachment: function.py
Description: application/python


Follow ups