Project works but interpolate does not

The following code works with project() at the end but not interpolate()

mesh = BoxMesh(Point(0, 0, 0), Point(1, 1, 1),
                     32, 32, 32)
V = FunctionSpace(mesh, "CG", 1)


A = Expression("5", degree=1)
B = Expression("5", degree=1)

a = project(A, V)
b = project(B, V)

c = a + b
project(c, V)

When I use interpolate() I get the following error

AttributeError: 'Sum' object has no attribute '_cpp_object'

Although this question is similar to this question, my code here is a more minimal example and the expressions I am working with are not discontinuous. Also, although A and B start out as Expression types here, this was just for the purposes of this example; I want to add functions or form new functions out of simple polynomial expressions of other functions.

I want to use interpolate() and not project() because I am performing this operation over and over and I am not expecting significant errors because the mesh and finite elements are exactly the same between c, a, and b. Thus, using project() is unnecessarily costly because project() involves solving a linear equation, whereas interpolate() does not, and again I’m not expecting the error minimization to make a difference.

How can I add these functions without using project()? I suppose I could just add the degrees of freedom directly, but that could become cumbersome if I want to do more complex operations like a + k*b where k is some number or even another function.

Thanks in advance!

Old DOLFIN doesn’t support interpolation of UFL expressions. Consider:

  • DOLFINx, e.g. here.
  • Write your summation in its own expression Expression("something + something_else", ...)
  • Write the projection yourself and store the matrix factorisation for the space V, therefore you only need to compute the linear system once and back substitute thereafter.
1 Like

Actually, I found a better solution that does everything I want, namely

c = Function(V)
c.assign(a + b)

This is fast and doesn’t require setting up any linear algebra ahead of time.

1 Like