← Back to team overview

dolfin team mailing list archive

Re: Expression and Function

 

>
>
> kent-and@xxxxxxxxx wrote:
>>>
>>> kent-and@xxxxxxxxx wrote:
>>>> Hi, I am very happy with the  Expression, but old code like
>>>>
>>>> class Dabla(Function):
>>>>     def eval(...)
>>>>
>>>> still works.
>>>>
>>>> The only problem is that the function is zero even though eval is
>>>> implemented. Is it
>>>> possible to turn this old code into an error?
>>> In C++, no. There is no mechanism in C++ to prevent a user overloading
>>> or hiding a member function.
>>>
>>> Garth
>>>
>>
>> But the problem here is that the method of the subclass is not called.
>> Furthermore,
>> code written prior to Expression called the method of the subclass.
>>
>> Consider the following code:
>>
>> from dolfin import *
>>
>> class Foo(Function):
>>     def eval(self, v, x):
>>         v[0] = 1
>>
>> mesh = UnitSquare(12,12)
>> V = FunctionSpace(mesh, 'CG', 1)
>>
>> f = Foo(V)
>> fi = interpolate(f, V)
>> print fi.vector().norm("l2")
>>
>> here:
>> fi.vector().norm("l2")
>> will return 0.
>>
>> which is clearly not expected.
>>
>> What the user should of course do is to use Expression instead of
>> Function. Then
>> the above code would work. And I think the user should somehow be told
>> to use Expression.
>>
>
> You'll have to talk to the C++ standard committee to sort this out in
> C++ ;), or to Dr Hake to fix it on the Python side.
>
> Garth
>

OK, I actually did not realize that eval was no longer a virtual function and
that this was the reason. I thought it was some Python-C++-meta-class stuff.

I do, however, believe that it is possible to design code that prevents users
from making such mistakes. :) Sorry about the frustration, I just spent
a few hours on such a silly bug yesterday. The problem was that
the old code with Function seemed to work, but produced strange results.

Kent



References