Skip to content

Commit 64e0491

Browse files
authored
python - add take_array (#1995)
* python - add take_array * python - add test t109
2 parents 0fcffb7 + 53188c3 commit 64e0491

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

python/ceed_vector.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,29 @@ def set_array(self, array, memtype=MEM_HOST, cmode=COPY_VALUES):
101101
self._pointer[0], memtype, cmode, array_pointer)
102102
self._ceed._check_error(err_code)
103103

104+
# Take Vector's data array
105+
def take_array(self, memtype=MEM_HOST):
106+
"""Take ownership of the array set by set_array with USE_POINTER and remove the array from the Vector
107+
108+
Args:
109+
**memtype: memory type of the array being taken, default CEED_MEM_HOST
110+
111+
Returns:
112+
*array: array passed to set_array"""
113+
114+
# Setup the pointer's pointer
115+
array_pointer = ffi.new("CeedScalar **")
116+
117+
# libCEED call
118+
err_code = lib.CeedVectorTakeArray(
119+
self._pointer[0], memtype, array_pointer)
120+
self._ceed._check_error(err_code)
121+
122+
# Return array
123+
array = self._array_reference
124+
self._array_reference = None
125+
return array
126+
104127
# Get Vector's data array
105128
def get_array(self, memtype=MEM_HOST):
106129
"""Get read/write access to a Vector via the specified memory type.

python/tests/test-1-vector.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,33 @@ def test_108(ceed_resource, capsys):
208208

209209
assert abs(norm - 9.) < TOL
210210

211+
# -------------------------------------------------------------------------------
212+
# Test set_array and take_array
213+
# -------------------------------------------------------------------------------
214+
215+
216+
def test_109(ceed_resource, capsys):
217+
ceed = libceed.Ceed(ceed_resource)
218+
219+
n = 10
220+
x = ceed.Vector(n)
221+
222+
a = np.arange(0, n, dtype=ceed.scalar_type())
223+
for i in range(n):
224+
a[i] = -3.14 if i == 3 else 0
225+
x.set_array(a, cmode=libceed.USE_POINTER)
226+
227+
# Verify correct array
228+
a = x.take_array()
229+
for i in range(n):
230+
assert abs(a[i] + (3.14 if i == 3 else 0)) < TOL
231+
232+
# And ensure access removed in Vector
233+
x.set_value(0)
234+
with x.array() as b:
235+
b[5] = 3.14
236+
assert abs(a[5]) < TOL
237+
211238
# -------------------------------------------------------------------------------
212239
# Test taking the reciprocal of a vector
213240
# -------------------------------------------------------------------------------

tests/t109-vector.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// @file
2-
/// Test CeedVectorSetArray to remove array access
3-
/// \test Test CeedVectorSetArray to remove array access
2+
/// Test CeedVectorTakeArray to remove array access
3+
/// \test Test CeedVectorTakeArray to remove array access
44
#include <ceed.h>
55
#include <math.h>
66
#include <stdio.h>

0 commit comments

Comments
 (0)