Issue in saving vector results as VTK file

Hi Everyone,

I am trying to save a vector function which contains two vectors as .vtk file as follows:

UU = as_vector([U1, U2])
vtkfile = File('Figures/2Ddisp.pvd')
vtkfile << UU

*) U1 and U2 are my 2D displacement vector results

But I have received this error:

vtkfile << UU
TypeError: in method ‘File___lshift__’, argument 2 of type ‘dolfin::Function const &’
Aborted (core dumped)

P.S. I have tried to get the figure by matplotlib which is working fine but I can not get the deformed shape just displacement vectors on my initial configuration.

Thank you in advance.
Ali

The function as_vector() does not return a Function object in the appropriate vector function space, ie the mixed space consisting of the two scalar function spaces.

The simplest solution would be to construct the appropriate vector function space yourself and project your solution onto that space. That solution can be written to a VTK file since it will be a function object.

Check out this MWE:

from dolfin import *

mesh = UnitSquareMesh(3,3)

# Create example functions
FE = FiniteElement('Lagrange', mesh.ufl_cell(),1)
V = FunctionSpace(mesh, FE)
U1 = Function(V)
U2 = Function(V)

# Build space for vector functions
vFE = VectorElement('Lagrange', mesh.ufl_cell(),1)
W = FunctionSpace(mesh, vFE)
UU_array = as_vector([U1, U2])
UU = project(UU_array, W)

vtkfile = File('Figures/2Ddisp.pvd')
vtkfile << UU
1 Like

It is Also possible to use a ‘FunctionAssigner’, avoiding projections Which are time consuming on big meshes, see: https://bitbucket.org/fenics-project/dolfin/src/master/python/demo/undocumented/sub-function-assignment/demo_sub-function-assignment.py

2 Likes