dolfinx.fem.function.Function add with dolfinx.fem.function.Function

hey

a quick question can i add or subtract 2 objects with type <class ‘dolfinx.fem.function’> somehow?
for example
<class ‘dolfinx.fem.function.Function’> - <class ‘dolfinx.fem.function.Function’>

because this is the data type that comes out for me
<class ‘ufl.algebra.Sum’>

but I cannot use this with

with io.VTKFile(mesh.comm, "print.pvd", "w") as vtk:
    vtk.write([x._cpp_object])

create as .pvd

Are the functions in the same function space?
If so you could use a third function in the space:

u, v = Function(V), Function(V)
w = Function(V)
w.x.array[:] = u.x.array[:] - v.x.array[:]

If not you would need to use dolfinx.fem.Expression

expr = dolfinx.fem.Expression(u-v, V.element.interpolation_points())
w.interpolate(expr)
1 Like