function.vector().vec() not working?

Issue #414 invalid
Mike Welland created an issue

I'm trying to get a petsc4py vector from a function with the .vec() command, but it fails with

AttributeError: 'GenericVector' object has no attribute 'vec'

Defining an empty PETScVector works fine though.

MWE:

from dolfin import *
from petsc4py import *
mesh = RectangleMesh(0,0,1,1,10,10)
V = FunctionSpace(mesh, 'Lagrange',1)
f = interpolate(Constant(1.0),V).vector() 
g = PETScVector()
print g
print g.vec()

print f
print f.vec()  # Fails

Comments (3)

  1. Prof Garth Wells

    In your case, f is a Vector and not a PETScVector. You need to cast f to a PETScVector. It goes something like

    f_petsc = as_backend_type(f)
    
  2. Mike Welland reporter

    Thanks for the quick help! The error message is strange then no? The output that I get is:

    <Uninitialized PETScVector> <petsc4py.PETSc.Vec object at 0x2982950> <PETScVector of size 121>

    before the crash. To me this is saying that both g and f are of type PETScVector ...

  3. Log in to comment