AttributeError (object has no attribute '_cpp_object')

Hi
After 2 years of working with 2017.2 version, finally I decided to move to the 2019 version. I have a code that used to work fine in the previous version but I am getting AttributeError in 2019 version that seems to appear in the last line while interpolation. Here is the code:

from dolfin import *

#Initial value
first = 150
second = 180

mesh = UnitSquareMesh(10, 10)

Element = FiniteElement("CG", mesh.ufl_cell(), 1)

V = FunctionSpace(mesh, Element)

# Previous solution
class IC(UserExpression):
    def __init__(self, **kwargs):

        self.FIRST = kwargs["FIRST"]
        self.SECOND = kwargs["SECOND"]

    def eval(self, value, x):

        if  x[0] >= 0.02425- DOLFIN_EPS and x[0] <= 0.02575+ DOLFIN_EPS and (x[1] >= 0.015- DOLFIN_EPS ) and (x[1] <= 0.035+ DOLFIN_EPS ):
            value[0] = self.FIRST

        else:
            value[0] = self.SECOND

IC_2 = IC(FIRST = first, SECOND = second,  degree=0)

# Previous solution
Previous_Solution = interpolate(IC_2, V)

This is the error:

Traceback (most recent call last):
  File "/home/Desktop/0TEST/NEW.py", line 31, in <module>
    Previous_Solution = interpolate(IC_2, V)
  File "/usr/lib/python3/dist-packages/dolfin/fem/interpolation.py", line 73, in interpolate
    Pv.interpolate(v)
  File "/usr/lib/python3/dist-packages/dolfin/function/function.py", line 363, in interpolate
    self._cpp_object.interpolate(u._cpp_object)
AttributeError: 'IC' object has no attribute '_cpp_object'

I am not getting error if I remove the last line but I need to do the interpolation.
Thanks in advance for your help.

Guess you Need to add initialization to the expression.
super().__init__(**kwargs)

Thanks for the reply. However that was not the fix for this issue. It raises another error:

RuntimeError: Invalid keyword argument
Anyways, I figured it out. Here is the solution for those who may encounter the same issue:

from dolfin import *

#Initial value
first = 150
second = 180

mesh = UnitSquareMesh(10, 10)

Element = FiniteElement("CG", mesh.ufl_cell(), 1)

V = FunctionSpace(mesh, Element)

# Previous solution
class IC(UserExpression):
    def __init__(self, **kwargs):

        super().__init__(degree=kwargs["degree"])

        self.FIRST = kwargs["FIRST"]
        self.SECOND = kwargs["SECOND"]

    def eval(self, value, x):

        if  x[0] >= 0.02425- DOLFIN_EPS and x[0] <= 0.02575+ DOLFIN_EPS and (x[1] >= 0.015- DOLFIN_EPS ) and (x[1] <= 0.035+ DOLFIN_EPS ):
            value[0] = self.FIRST

        else:
            value[0] = self.SECOND
            
    def value_shape(self):
        return ()

IC_2 = IC(FIRST = first, SECOND = second,  degree=0)

# Previous solution
Previous_Solution = interpolate(IC_2, V)

Thats because you have to remove kwargs from my statement, as you dont declear it in the initializer

Hi,

The function cpp is in the submodule dolfin.function.functionspace. However, dirichletbc.py, in the folder fem, invokes the submodule dolfin.function.functionspace.FunctionSpace. Does not this seem a reason why this type of error can be produced?