← Back to team overview

dolfin team mailing list archive

Re: Problem with Expressions in Python

 

On Friday 16 October 2009 13:51:18 Anders Logg wrote:
> My fault. The problem here was that I forgot to change from Function
> to Expression in the code string. But perhaps the check needs to be
> improved? The error message does not help.

I think we need to extend the syntax if we want to have better messages. Now 
we first search for the string:

  "class () public:Expression"

If this is found we assume that the user wants to create a complex Expression. 

When this is not found we assume that the user want to create an Expression 
based on the simple syntax. We then check if any C++ keywords are used in that 
string, and it was this check that kicked in for you.

If we want to keep the syntax we might change the error to:

  TypeError: You are trying to compile a simple Expression and the C++ keyword 
'class' was detected in the passed string.

We might also change the syntax all the way and make things easier for us. 
(More drastic change though...)

  * class Expression is only used to subclass Expression in pure Python.
  * function CompileExpression is used to compile:
    1) simple expressions
    2) complex expressions
       Where we use kwargs do differentiate between the two.
  * As before CompileExpression takes an ufl.FiniteElement or a FunctionSpace
    to instantiate the compiled class.
  * CompileExpressions can do what Expressions do now, creating several
    classes
  * Remove the possibility to create compiled classes, only instantiated ones.

This will:
  * remove the need for metaclasses, 
  * make it explicit that we are using compiled stuff
  * easier to maintain (soon leaving you know :) )

> My next problem now is that one needs to set the geometric dimension
> at the time of construction for a C++ expression. How can I send this
> to my Expression sub class from Python?

For now you have to instantiate the parent Expression in your C++ code. This 
could be done with:

  code = """
  class MyExpression : public Expression
      MyExpression() : Expression(%d)
      ...
  """

  f = Expression(code%mesh.geometry().dim(), V=V)

But this you have already found out...

Johan

> --
> Anders
> 
> On Fri, Oct 16, 2009 at 01:29:57PM +0200, Anders Logg wrote:
> > I'm getting this error with a code that used to work:
> >
> >   f = Expression(code, V=V)
> >
> >  File
> > 
> > "/home/logg/scratch/src/dolfin-dev/local/lib/python2.6/site-packages/dolf
> >in/expression.py", line 575, in __new__
> >     [element.cell().geometric_dimension()])[0]
> >   File
> >  
> > "/home/logg/scratch/src/dolfin-dev/local/lib/python2.6/site-packages/dolf
> >in/compile_expressions.py", line 216, in compile_expressions
> >     classname, code = expression_to_dolfin_expression(cpparg,
> >     defaults, dim)
> >   File
> >  
> > "/home/logg/scratch/src/dolfin-dev/local/lib/python2.6/site-packages/dolf
> >in/compile_expressions.py", line 65, in expression_to_dolfin_expression
> >     fragments = expression_to_code_fragments(expr, defaults,
> >     ["v","x"])
> >   File
> >  
> > "/home/logg/scratch/src/dolfin-dev/local/lib/python2.6/site-packages/dolf
> >in/compile_extension_module.py", line 66, in expression_to_code_fragments
> >     raise TypeError, "The c++ keyword '%s' was detected in C++
> >     expression."%sym
> > TypeError: The c++ keyword 'class' was detected in C++ expression.
> >
> >
> >
> >
> > _______________________________________________
> > DOLFIN-dev mailing list
> > DOLFIN-dev@xxxxxxxxxx
> > http://www.fenics.org/mailman/listinfo/dolfin-dev
> 


Follow ups

References