# Dolfinx C++ equivalent of function.vector.set\_local/get\_local

**URL:** <https://fenicsproject.discourse.group/t/dolfinx-c-equivalent-of-function-vector-set-local-get-local/19320>\
**Category:** General\
**Created:** [January 8, 2026, 4:43am UTC](https://fenicsproject.discourse.group/t/dolfinx-c-equivalent-of-function-vector-set-local-get-local/19320 "2026-01-08T04:43:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haibo\_Ni](https://avatars.discourse-cdn.com/v4/letter/h/439d5e/32.png) [@Haibo\_Ni](https://fenicsproject.discourse.group/u/Haibo_Ni)\
**Post date:** [January 8, 2026, 4:43am UTC](https://fenicsproject.discourse.group/t/dolfinx-c-equivalent-of-function-vector-set-local-get-local/19320/1 "2026-01-08T04:43:08Z")

</div>

Hi all,

I am converting from the legacy dolfin to dolfinx C++. We used set/get local values of a function vector by

```C++
std::vector<double> cell_voltages ( cells.size() );
// update cell_voltages based on reaction part, transfer to u_prev
( *u_prev.vector() ).set_local( cell_voltages );

//after solution of u, assigning back to cell_voltages
u.vector()->get_local( cell_voltages );

```

I have some difficulties in finding the c++ function for the get\_lcoal and set\_local functions in dolfinx. So far, I am using `std::copy()` to perform the vector value assignment.  
Thanks,

---

<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:** [January 8, 2026, 10:09am UTC](https://fenicsproject.discourse.group/t/dolfinx-c-equivalent-of-function-vector-set-local-get-local/19320/2 "2026-01-08T10:09:14Z")

</div>

In DOLFINx (v0.10.0), the suggested way of doing this is by accessing the `dolfinx.la.Vector` and its underlying array object:

```c++
// Fill in initial data
std::vector<T> cell_voltages(V->dofmap()->index_map->size_local());
std::iota(cell_voltages.begin(), cell_voltages.end(), 10);
 
// Copy data into uh
dolfinx::fem::Function<T> uh(V);
std::ranges::copy_n(cell_voltages.begin(), cell_voltages.size(),
                        uh.x()->array().begin());
// Inspect data
for(auto c: uh.x()->array())
    std::cout << c << " ";
std::cout << "\n";

```

In DOLFINx, we favor using the C++ standard functions on arrays, rather than implementing our own logic for assigning data.

---

<div class="post-metadata">

**Author:** ![Haibo\_Ni](https://avatars.discourse-cdn.com/v4/letter/h/439d5e/32.png) [@Haibo\_Ni](https://fenicsproject.discourse.group/u/Haibo_Ni)\
**Post date:** [January 9, 2026, 3:11am UTC](https://fenicsproject.discourse.group/t/dolfinx-c-equivalent-of-function-vector-set-local-get-local/19320/3 "2026-01-09T03:11:48Z")

</div>

Thanks a lot for the clarification. I will use the C++ standard functions.
