# Creating a generic updatable vector through PETSc

**URL:** <https://fenicsproject.discourse.group/t/creating-a-generic-updatable-vector-through-petsc/910>\
**Category:** Uncategorized\
**Created:** [May 29, 2019, 8:17am UTC](https://fenicsproject.discourse.group/t/creating-a-generic-updatable-vector-through-petsc/910 "2019-05-29T08:17:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcsj](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bcsj/32/253_2.png) [@bcsj](https://fenicsproject.discourse.group/u/bcsj)\
**Post date:** [May 29, 2019, 8:18am UTC](https://fenicsproject.discourse.group/t/creating-a-generic-updatable-vector-through-petsc/910/1 "2019-05-29T08:18:00Z")

</div>

I’m trying to generate a generic vector (not related to a particular function space), but I receive an error when I try to perform an update of the vector. Example code snippet below.

```
from dolfin import Vector, PETScVector
from petsc4py import PETSc
import numpy as np
x = PETSc.Vec().createSeq(3)
X = Vector(PETScVector(x))
X.get_local()
#array([0., 0., 0.])
X.set_local(np.array([1,2,3],dtype=np.float64))

```

The last command produces the following error

```
*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
*** fenics-support@googlegroups.com
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error: Unable to successfully call PETSc function 'VecSetValuesLocal'.
*** Reason: PETSc error code is: 73 (Object is in wrong state).
*** Where: This error was encountered inside /tmp/sebo/fenics/dolfin-2017.2.0/dolfin/la/PETScVector.cpp.
*** Process: 0
*** 
*** DOLFIN version: 2017.2.0
*** Git changeset:
*** -------------------------------------------------------------------------
```

---

<div class="post-metadata">

**Author:** ![kamensky](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@kamensky](https://fenicsproject.discourse.group/u/kamensky)\
**Post date:** [May 29, 2019, 12:53pm UTC](https://fenicsproject.discourse.group/t/creating-a-generic-updatable-vector-through-petsc/910/2 "2019-05-29T12:53:59Z")

</div>

Maybe try instead to create a DOLFIN `Vector` first, then access the underlying PETSc object as necessary:

```python
from dolfin import Vector, PETScVector, MPI, as_backend_type
from petsc4py import PETSc
import numpy as np

# Use DOLFIN constructor directly with MPI.comm_self to
# behave like VecCreateSeq:
X = Vector(MPI.comm_self,3)

print(X.get_local())

X.set_local(np.array([1,2,3],dtype=np.float64))

print(X.get_local())

# To access underlying PETSc Vec:
print(as_backend_type(X).vec())

```

---

<div class="post-metadata">

**Author:** ![bcsj](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bcsj/32/253_2.png) [@bcsj](https://fenicsproject.discourse.group/u/bcsj)\
**Post date:** [May 29, 2019, 7:06pm UTC](https://fenicsproject.discourse.group/t/creating-a-generic-updatable-vector-through-petsc/910/3 "2019-05-29T19:06:53Z")

</div>

That definitely looks better. I wasn’t aware that it could be done like that. I will definitely try this. Can I construct a matrix in this fashion too?

---

<div class="post-metadata">

**Author:** ![bcsj](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bcsj/32/253_2.png) [@bcsj](https://fenicsproject.discourse.group/u/bcsj)\
**Post date:** [May 30, 2019, 12:48pm UTC](https://fenicsproject.discourse.group/t/creating-a-generic-updatable-vector-through-petsc/910/4 "2019-05-30T12:48:39Z")

</div>

So I tried this, and for some reason the MPI object in my version (2017.2.0) does not appear to have this `comm_self` property (or method)?

---

<div class="post-metadata">

**Author:** ![kamensky](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@kamensky](https://fenicsproject.discourse.group/u/kamensky)\
**Post date:** [May 30, 2019, 1:01pm UTC](https://fenicsproject.discourse.group/t/creating-a-generic-updatable-vector-through-petsc/910/5 "2019-05-30T13:01:29Z")

</div>

This was a change from 2017.2 to 2018.1. In 2017.2, the world and self communicators are `mpi_comm_world()` and `mpi_comm_self()`.

---

<div class="post-metadata">

**Author:** ![nate](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/nate/32/17_2.png) [@nate](https://fenicsproject.discourse.group/u/nate)\
**Post date:** [May 30, 2019, 2:35pm UTC](https://fenicsproject.discourse.group/t/creating-a-generic-updatable-vector-through-petsc/910/6 "2019-05-30T14:35:49Z")

</div>

An additional difference between the two versions (if I recall correctly) is that if you compiled against mpi4py, you will have an mpi4py communicator in 2018.1. In 2017.2 it’s a DOLFIN MPI wrapper which doesn’t offer the same functionality.
