# Taking function converting it to petsc4py vector, and then putting it back into function?

**URL:** <https://fenicsproject.discourse.group/t/taking-function-converting-it-to-petsc4py-vector-and-then-putting-it-back-into-function/386>\
**Category:** Uncategorized\
**Created:** [March 17, 2019, 4:34pm UTC](https://fenicsproject.discourse.group/t/taking-function-converting-it-to-petsc4py-vector-and-then-putting-it-back-into-function/386 "2019-03-17T16:34:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Vogt](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ryan_vogt/32/552_2.png) [@Ryan\_Vogt](https://fenicsproject.discourse.group/u/Ryan_Vogt)\
**Post date:** [March 17, 2019, 4:34pm UTC](https://fenicsproject.discourse.group/t/taking-function-converting-it-to-petsc4py-vector-and-then-putting-it-back-into-function/386/1 "2019-03-17T16:34:49Z")

</div>

Howdy,

So essentially my example is more complicated but I have an example - I take a function, make it a petsc4py vector, manipulate it a bit, and then I want to put it back into the function. But it seems that it isn’t trivial, anyone know how to do this easily?

```
from petsc4py import PETSc
from dolfin import *

size=128
p1 = Point(-1.0,-1.0)
p2 = Point(1.0,1.0)
mesh = RectangleMesh(p1,p2,size,size)
singlespace = FunctionSpace(mesh,'CG',1)
w=Function(singlespace)
w=w.vector()

lb=w.vec()

vec_size = lb.size

for i in range(0,vec_size):
	lb.setValue(i,20.0)

w_new = Function(singlespace)

#how to put lb back into w_new- this doesn't work?
#ValueError: allocator<T>::allocate(size_t n) 'n' exceeds maximum supported size
w_new.vector().set_local(lb)
```

---

<div class="post-metadata">

**Author:** ![MiroK](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mirok/32/33_2.png) [@MiroK](https://fenicsproject.discourse.group/u/MiroK)\
**Post date:** [March 17, 2019, 4:55pm UTC](https://fenicsproject.discourse.group/t/taking-function-converting-it-to-petsc4py-vector-and-then-putting-it-back-into-function/386/2 "2019-03-17T16:55:52Z")

</div>

Consider

```python
w_new = Function(singlespace) # 0
w_new.vector().axpy(1, w) # w_new = w_new + 1*w

```

---

<div class="post-metadata">

**Author:** ![Ryan\_Vogt](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ryan_vogt/32/552_2.png) [@Ryan\_Vogt](https://fenicsproject.discourse.group/u/Ryan_Vogt)\
**Post date:** [March 17, 2019, 5:20pm UTC](https://fenicsproject.discourse.group/t/taking-function-converting-it-to-petsc4py-vector-and-then-putting-it-back-into-function/386/3 "2019-03-17T17:20:52Z")

</div>

Hey MiroK,

I wanted to put lb into w\_new not w. lb is a petsc4py vector

> ```
> from petsc4py import PETSc
> from dolfin import *
> 
> size=128
> p1 = Point(-1.0,-1.0)
> p2 = Point(1.0,1.0)
> mesh = RectangleMesh(p1,p2,size,size)
> singlespace = FunctionSpace(mesh,'CG',1)
> w=Function(singlespace)
> w=w.vector()
> 
> lb=w.vec()
> 
> vec_size = lb.size
> 
> for i in range(0,vec_size):
> lb.setValue(i,20.0)
> 
> w_new = Function(singlespace)
> 
> #how to put lb back into w_new- this doesn't work?
> #ValueError: allocator<T>::allocate(size_t n) 'n' exceeds maximum supported size
> w_new.vector().axpy(1,lb)
> '''
> 
> Traceback (most recent call last):
> File "test.py", line 28, in <module>
> w_new.vector().axpy(1,lb)
> TypeError: axpy(): incompatible function arguments. The following argument types are supported:
> 1. (self: dolfin.cpp.la.GenericVector, arg0: float, arg1: dolfin.cpp.la.GenericVector) -> None
> 
> Invoked with: <dolfin.cpp.la.PETScVector object at 0x11d05ad00>, 1, <petsc4py.PETSc.Vec object at 0x11d05aca8>
> '''
> 
> ```

---

<div class="post-metadata">

**Author:** ![MiroK](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mirok/32/33_2.png) [@MiroK](https://fenicsproject.discourse.group/u/MiroK)\
**Post date:** [March 17, 2019, 5:40pm UTC](https://fenicsproject.discourse.group/t/taking-function-converting-it-to-petsc4py-vector-and-then-putting-it-back-into-function/386/4 "2019-03-17T17:40:27Z")

</div>

Above `lb = w.vec()` does not give you a copy so manipulating `lb` is reflected in `w` and the rest of the code should be fine

```python
print assemble(inner(w_new-Constant(20), w_new-Constant(20))*dx)

```

---

<div class="post-metadata">

**Author:** ![Ryan\_Vogt](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ryan_vogt/32/552_2.png) [@Ryan\_Vogt](https://fenicsproject.discourse.group/u/Ryan_Vogt)\
**Post date:** [March 17, 2019, 6:58pm UTC](https://fenicsproject.discourse.group/t/taking-function-converting-it-to-petsc4py-vector-and-then-putting-it-back-into-function/386/5 "2019-03-17T18:58:12Z")

</div>

To be more detailed, I’m essentially calling TAO which takes a petsc vector and returns a petsc vector, I wanted to save that back into a function.

---

<div class="post-metadata">

**Author:** ![newbyJulia](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@newbyJulia](https://fenicsproject.discourse.group/u/newbyJulia)\
**Post date:** [April 25, 2021, 12:55pm UTC](https://fenicsproject.discourse.group/t/taking-function-converting-it-to-petsc4py-vector-and-then-putting-it-back-into-function/386/6 "2021-04-25T12:55:22Z")

</div>

Hey. I have the same problem. Can you tell me please if you could solve it?

Thanks!
