Subclassing Expression with non-hard-coded value_shape

Asked by Jonathan Mynard

Is it possible to subclass Expression without hard-coding the value_shape? For example, I would like to do something like

class ExpSubClass(Expression):

    def __init__(self, V, **kwargs):
 self.ndims = kwargs['ndims']

    def eval_cell(self, values, x, ufc_cell):
        values[:] = whatever....

    def value_shape(self):
        return self.ndims, # instead of "return 3," for example

But if I do this, I get the following error:

Traceback (most recent call last):
....
  File "/home/s/steinman/krisvs/dorsal-trunk/Work/FEniCS/lib/python2.7/site-packages/dolfin/functions/expression.py", line 177, in __init__
    element = _auto_select_element_from_shape(self.value_shape(),
  File "/scratch/s/steinman/jmynard/compflow/problems/pipewave.py", line 59, in value_shape
    return self.ndims,
AttributeError: 'ExpSubClass' object has no attribute 'ndims'

After some testing, it would appear that value_shape gets called before __init__ . I have read the documentation about subclassing Expression which says **kwargs should be used, but I'm not sure if I have understood the "magic behind the scenes" correctly :)

Thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Mikael Mortensen
Solved:
Last query:
Last reply:
Revision history for this message
Best Mikael Mortensen (mikael-mortensen) said :
#1

Hi,

You could do something like

my_exp = ExpSubClass(element=V.ufl_elment())

Then you would not need to implement a value_shape function nor an __init__ function, just the eval_cell would do.

Best regards

Mikael

Revision history for this message
Jonathan Mynard (jmynard) said :
#2

Thanks Mikael, that solved my problem!

Jonathan.