This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Convert dolfin generic vector into PETSc vector

+1 vote

Hi,

I need to extract a PETScVector object from a dolfin <class 'dolfin.cpp.la.GenericVector'> object - named X - :

V = VectorFunctionSpace(mesh, "Lagrange", 1)
u = Function(V)
X = u.vector()[:]

Thank you in advance,

Claire

asked May 29, 2015 by Claire L FEniCS User (2,120 points)

1 Answer

+3 votes

Hi, provided you linear algebra backend is PETSc the way to do it is

from dolfin import *

mesh = UnitSquareMesh(20, 20)

V = FunctionSpace(mesh, 'CG', 1)
v = Function(V).vector()
print type(v)

v = as_backend_type(v)
print type(v)
print 'length of v', v.vec().size  # petsc4py
answered May 29, 2015 by MiroK FEniCS Expert (80,920 points)
...