How to write error (or sum of functions) to a file?

Asked by André Gaul

What is the recommended way of writing a function that is the result of a sum of two functions to a file? The following code fails in the last line with the error message "TypeError: in method 'File___lshift__', argument 2 of type 'dolfin::Function const &'".

=======================
from dolfin import *

mesh = UnitSquareMesh(8,8)
V = FunctionSpace(mesh, 'CG', 1)

u_ex = Expression('x[0]')
u = Function(V)
u.interpolate(u_ex)
File('u.pvd') << u

v_ex = Expression('x[1]')
v = Function(V)
v.interpolate(v_ex)
File('v.pvd') << v

File('error.pvd')<< (u-v)
=======================

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Martin Sandve Alnæs
Solved:
Last query:
Last reply:
Revision history for this message
Best Martin Sandve Alnæs (martinal) said :
#1

In general you can make a projection of any expression and store the resulting function:
  e = project(u-v, V)
  File("e.pvd") << e
In this case its faster to make a copy of u and subtract the vector of v, something like (not tested):
  e = Function(V)
  e.assign(u)
  e.vector().axpy(-1.0, v.vector())

Revision history for this message
André Gaul (andrenarchy) said :
#2

Thanks Martin Sandve Alnæs, that solved my question.