Hi,
assuming a function
mesh = IntervalMesh(comm, 10, 0.0, 0.1)
V = FunctionSpace(mesh, 'CG', 1)
u_0 = Constant(1.0)
u_n = interpolate(u_0, V)
What would be the fastest way to convert its values to a numpy array and back?
Converting the function’s value to a numpy array:
In [5]: time u_n.vector()[:]
CPU times: user 85 µs, sys: 60 µs, total: 145 µs
Wall time: 160 µs
Out[5]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
time u_n.vector().get_local()
CPU times: user 58 µs, sys: 40 µs, total: 98 µs
Wall time: 105 µs
Out[6]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [7]: %timeit u_n.vector()[:]
4.85 µs ± 35.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [8]: %timeit u_n.vector().get_local()
5.01 µs ± 156 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Alternatively, using an Expression:
In [20]: expression_form = Expression("1.0", element=V.ufl_element())
n [21]: time interpolate(expression_form, V).vector()[:]
CPU times: user 528 µs, sys: 0 ns, total: 528 µs
Wall time: 537 µs
Out[21]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [22]: time interpolate(expression_form, V).vector().get_local()
CPU times: user 521 µs, sys: 0 ns, total: 521 µs
Wall time: 530 µs
Out[22]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [23]: %timeit interpolate(expression_form, V).vector()[:]
97.9 µs ± 1.92 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [24]: %timeit interpolate(expression_form, V).vector().get_local()
95.1 µs ± 854 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Defining the value of the function from a numpy array:
In [10]: %timeit u_n.vector()[:] = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
36.9 µs ± 1 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [12]: %timeit u_n.vector().get_local()[:] = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
8.21 µs ± 13.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
As my model demands several of these convertions, I’m wondering if there’s a more efficient way of doing it to improve performance. Any idea/thought about this is appreciated. Thanks!