Skip to content

Commit f20b8c3

Browse files
committed
Update the parameter reader's storage to avoid inconsistencies
1 parent da41c11 commit f20b8c3

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

cadet/cadet_dll_utils.py

+41-23
Original file line numberDiff line numberDiff line change
@@ -211,19 +211,28 @@ def param_provider_get_double_array(
211211
n = name.decode('utf-8')
212212
c = reader.current()
213213

214-
if n in c:
215-
o = c[n]
216-
if isinstance(o, list):
217-
o = np.ascontiguousarray(o)
218-
if not isinstance(o, np.ndarray) or o.dtype != np.double or not o.flags.c_contiguous:
219-
return -1
220-
221-
n_elem[0] = ctypes.c_int(o.size)
222-
val[0] = o.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
223-
log_print(f"GET array [double] {n}: {o}")
224-
return 0
214+
if n not in c:
215+
log_print(f"Parameter {n} not found.")
216+
return -1
217+
218+
o = c[n]
225219

226-
return -1
220+
# Ensure object is a properly aligned numpy array
221+
if isinstance(o, list): # Convert lists to numpy arrays
222+
o = np.array(o, dtype=np.double)
223+
c[n] = o # Update the reader's storage
224+
225+
# Validate the array
226+
if not isinstance(o, np.ndarray) or o.dtype != np.double or not o.flags.c_contiguous:
227+
log_print(f"Error: Parameter {n} is not a contiguous double array.")
228+
return -1
229+
230+
# Provide array data to the caller
231+
n_elem[0] = ctypes.c_int(o.size)
232+
val[0] = np.ctypeslib.as_ctypes(o)
233+
234+
log_print(f"GET array [double] {n}: {o}")
235+
return 0
227236

228237

229238
def param_provider_get_int_array(
@@ -254,19 +263,28 @@ def param_provider_get_int_array(
254263
n = name.decode('utf-8')
255264
c = reader.current()
256265

257-
if n in c:
258-
o = c[n]
259-
if isinstance(o, list):
260-
o = np.ascontiguousarray(o)
261-
if not isinstance(o, np.ndarray) or o.dtype != int or not o.flags.c_contiguous:
262-
return -1
266+
if n not in c:
267+
log_print(f"Parameter {n} not found.")
268+
return -1
263269

264-
n_elem[0] = ctypes.c_int(o.size)
265-
val[0] = o.ctypes.data_as(ctypes.POINTER(ctypes.c_int))
266-
log_print(f"GET array [int] {n}: {o}")
267-
return 0
270+
o = c[n]
268271

269-
return -1
272+
# Ensure object is a properly aligned numpy array
273+
if isinstance(o, list): # Convert lists to numpy arrays
274+
o = np.array(o, dtype=np.double)
275+
c[n] = o # Update the reader's storage
276+
277+
# Validate the array
278+
if not isinstance(o, np.ndarray) or o.dtype != np.int32 or not o.flags.c_contiguous:
279+
log_print(f"Error: Parameter {n} is not a contiguous int array.")
280+
return -1
281+
282+
# Provide array data to the caller
283+
n_elem[0] = ctypes.c_int(o.size)
284+
val[0] = np.ctypeslib.as_ctypes(o)
285+
286+
log_print(f"GET array [int] {n}: {o}")
287+
return 0
270288

271289

272290
# %% Array items

0 commit comments

Comments
 (0)