# Set values of PETScVector from numpy array

**URL:** <https://fenicsproject.discourse.group/t/set-values-of-petscvector-from-numpy-array/14569>\
**Category:** General\
**Created:** [May 7, 2024, 3:56pm UTC](https://fenicsproject.discourse.group/t/set-values-of-petscvector-from-numpy-array/14569 "2024-05-07T15:56:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![williams\_Lima](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/williams_lima/32/6645_2.png) [@williams\_Lima](https://fenicsproject.discourse.group/u/williams_Lima)\
**Post date:** [May 7, 2024, 3:56pm UTC](https://fenicsproject.discourse.group/t/set-values-of-petscvector-from-numpy-array/14569/1 "2024-05-07T15:56:43Z")

</div>

How can I set the values of a variable of type PETScVector from a numpy array?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [May 7, 2024, 6:11pm UTC](https://fenicsproject.discourse.group/t/set-values-of-petscvector-from-numpy-array/14569/2 "2024-05-07T18:11:49Z")

</div>

This is a very general question, whose answer depend on a few things:

- what version of fenics are you using?
- should your code work in parallel, if so, how is the numpy array distributed?

To answer the questions above, please provide a minimal reproducible example.

---

<div class="post-metadata">

**Author:** ![williams\_Lima](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/williams_lima/32/6645_2.png) [@williams\_Lima](https://fenicsproject.discourse.group/u/williams_Lima)\
**Post date:** [May 7, 2024, 6:29pm UTC](https://fenicsproject.discourse.group/t/set-values-of-petscvector-from-numpy-array/14569/3 "2024-05-07T18:29:38Z")

</div>

- FEniCS 2019.1.0
- not parallel

* * *

(not reproducible) code is like this:  
def func(u)  
“”"  
u is a PETScVector  
“”"  
u\_t = func\_returning \_numpy\_array()

* * *

I need to fill u with the values in u\_t

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [May 7, 2024, 6:31pm UTC](https://fenicsproject.discourse.group/t/set-values-of-petscvector-from-numpy-array/14569/4 "2024-05-07T18:31:52Z")

</div>

`u.set_local(u_t)` should do it.

---

<div class="post-metadata">

**Author:** ![williams\_Lima](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/williams_lima/32/6645_2.png) [@williams\_Lima](https://fenicsproject.discourse.group/u/williams_Lima)\
**Post date:** [May 7, 2024, 6:35pm UTC](https://fenicsproject.discourse.group/t/set-values-of-petscvector-from-numpy-array/14569/5 "2024-05-07T18:35:11Z")

</div>

Thanks dokken. I passed many times by set\_local in the documentation but was not sure about using it.

Thanks.
