From ad35d65a656a731dc7779b83337e4029da36dab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Sun, 8 Dec 2024 12:34:56 +0100 Subject: [PATCH] Formatting --- cadet/cadet.py | 31 ++-- cadet/cadet_dll_parameterprovider.py | 5 + cadet/cadet_dll_utils.py | 235 +++++++++++++++------------ 3 files changed, 154 insertions(+), 117 deletions(-) diff --git a/cadet/cadet.py b/cadet/cadet.py index 4de20f1..5a37840 100644 --- a/cadet/cadet.py +++ b/cadet/cadet.py @@ -474,55 +474,56 @@ def inverse_transform(self, x: str) -> str: """ return str.lower(x) - def run( + def run_load( self, timeout: Optional[int] = None, + clear: bool = True ) -> ReturnInformation: """ - Run the CADET simulation. + Run the CADET simulation and load the results. Parameters ---------- timeout : Optional[int] Maximum time allowed for the simulation to run, in seconds. + clear : bool + If True, clear previous results after loading new ones. Returns ------- ReturnInformation Information about the simulation run. """ - return_information = self.cadet_runner.run( - self, - timeout=timeout, - ) + return_information = self.run(timeout) + print(return_information) + self.load_results() + if clear: + self.clear() return return_information - def run_load( + def run( self, timeout: Optional[int] = None, - clear: bool = True ) -> ReturnInformation: """ - Run the CADET simulation and load the results. + Run the CADET simulation. Parameters ---------- timeout : Optional[int] Maximum time allowed for the simulation to run, in seconds. - clear : bool - If True, clear previous results after loading new ones. Returns ------- ReturnInformation Information about the simulation run. """ - return_information = self.run(timeout) - self.load_results() + return_information = self.cadet_runner.run( + self, + timeout=timeout, + ) - if clear: - self.clear() return return_information def load_results(self) -> None: diff --git a/cadet/cadet_dll_parameterprovider.py b/cadet/cadet_dll_parameterprovider.py index 9b95153..2615580 100644 --- a/cadet/cadet_dll_parameterprovider.py +++ b/cadet/cadet_dll_parameterprovider.py @@ -133,23 +133,28 @@ def __init__(self, simulation: "Cadet") -> None: self.popScope = self._fields_[17][1](utils.param_provider_pop_scope) _fields_ = [ + # 0 (Position must match indices in __init__ method.) ('userData', ctypes.py_object), + # 1 ('getDouble', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.POINTER(ctypes.c_double))), ('getInt', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int)), ('getBool', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.POINTER(ctypes.c_uint8))), ('getString', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p))), + # 5 ('getDoubleArray', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int, array_double)), ('getIntArray', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int, ctypes.POINTER(point_int))), ('getBoolArray', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int, ctypes.POINTER(ctypes.POINTER(ctypes.c_uint8)))), ('getStringArray', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, point_int, ctypes.POINTER(ctypes.POINTER(ctypes.c_char_p)))), + # 9 ('getDoubleArrayItem', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(ctypes.c_double))), ('getIntArrayItem', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.c_int, point_int)), ('getBoolArrayItem', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(ctypes.c_uint8))), ('getStringArrayItem', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(ctypes.c_char_p))), + # 13 ('exists', ctypes.CFUNCTYPE(ctypes.c_int, ctypes.py_object, ctypes.c_char_p)), ('isArray', ctypes.CFUNCTYPE(c_cadet_result, ctypes.py_object, ctypes.c_char_p, ctypes.POINTER(ctypes.c_uint8))), ('numElements', ctypes.CFUNCTYPE(ctypes.c_int, ctypes.py_object, ctypes.c_char_p)), diff --git a/cadet/cadet_dll_utils.py b/cadet/cadet_dll_utils.py index 204b52f..d61596a 100644 --- a/cadet/cadet_dll_utils.py +++ b/cadet/cadet_dll_utils.py @@ -1,14 +1,19 @@ import ctypes +from typing import Any + import numpy as np -from typing import Any, Optional def null(*args: Any) -> None: """Do nothing (used as a placeholder function).""" pass + log_print = print if 0 else null + +# %% Single entries + def param_provider_get_double( reader: Any, name: ctypes.c_char_p, @@ -34,18 +39,19 @@ def param_provider_get_double( n = name.decode('utf-8') c = reader.current() - if n in c: - o = c[n] - try: - float_val = float(o) - except TypeError: - float_val = float(o[0]) + if n not in c: + log_print(f"Parameter {n} not found.") + return -1 - val[0] = ctypes.c_double(float_val) - log_print(f"GET scalar [double] {n}: {float(val[0])}") - return 0 + o = c[n] + try: + float_val = float(o) + except TypeError: + float_val = float(o[0]) - return -1 + val[0] = ctypes.c_double(float_val) + log_print(f"GET scalar [double] {n}: {float(val[0])}") + return 0 def param_provider_get_int( @@ -73,18 +79,20 @@ def param_provider_get_int( n = name.decode('utf-8') c = reader.current() - if n in c: - o = c[n] - try: - int_val = int(o) - except TypeError: - int_val = int(o[0]) + if n not in c: + log_print(f"Parameter {n} not found.") + return -1 - val[0] = ctypes.c_int(int_val) - log_print(f"GET scalar [int] {n}: {int(val[0])}") - return 0 + o = c[n] + try: + int_val = int(o) + except TypeError: + int_val = int(o[0]) - return -1 + val[0] = ctypes.c_int(int_val) + + log_print(f"GET scalar [int] {n}: {int(val[0])}") + return 0 def param_provider_get_bool( @@ -112,18 +120,20 @@ def param_provider_get_bool( n = name.decode('utf-8') c = reader.current() - if n in c: - o = c[n] - try: - int_val = int(o) - except TypeError: - int_val = int(o[0]) + if n not in c: + log_print(f"Parameter {n} not found.") + return -1 - val[0] = ctypes.c_uint8(int_val) - log_print(f"GET scalar [bool] {n}: {bool(val[0])}") - return 0 + o = c[n] + try: + int_val = int(o) + except TypeError: + int_val = int(o[0]) - return -1 + val[0] = ctypes.c_uint8(int_val) + + log_print(f"GET scalar [bool] {n}: {bool(val[0])}") + return 0 def param_provider_get_string( @@ -151,25 +161,28 @@ def param_provider_get_string( n = name.decode('utf-8') c = reader.current() - if n in c: - o = c[n] + if n not in c: + log_print(f"Parameter {n} not found.") + return -1 - if hasattr(o, 'encode'): - bytes_val = o.encode('utf-8') - elif hasattr(o, 'decode'): - bytes_val = o - elif hasattr(o[0], 'encode'): - bytes_val = o[0].encode('utf-8') - elif hasattr(o[0], 'decode'): - bytes_val = o[0] - - reader.buffer = bytes_val - val[0] = ctypes.cast(reader.buffer, ctypes.c_char_p) - return 0 + o = c[n] - return -1 + if hasattr(o, 'encode'): + bytes_val = o.encode('utf-8') + elif hasattr(o, 'decode'): + bytes_val = o + elif hasattr(o[0], 'encode'): + bytes_val = o[0].encode('utf-8') + elif hasattr(o[0], 'decode'): + bytes_val = o[0] + + reader.buffer = bytes_val + val[0] = ctypes.cast(reader.buffer, ctypes.c_char_p) + return 0 +# %% Arrays + def param_provider_get_double_array( reader: Any, name: ctypes.c_char_p, @@ -256,10 +269,13 @@ def param_provider_get_int_array( return -1 +# %% Array items + def param_provider_get_double_array_item( reader: Any, name: ctypes.c_char_p, - index: int, val: ctypes.POINTER(ctypes.c_double) + index: int, + val: ctypes.POINTER(ctypes.c_double) ) -> int: """ Retrieve an item from a double array in the reader based on the provided name and index. @@ -283,25 +299,28 @@ def param_provider_get_double_array_item( n = name.decode('utf-8') c = reader.current() - if n in c: - o = c[n] + if n not in c: + log_print(f"Parameter {n} not found.") + return -1 - try: - float_val = float(o) - except TypeError: - float_val = float(o[index]) + o = c[n] - val[0] = ctypes.c_double(float_val) - log_print(f"GET array [double] ({index}) {n}: {val[0]}") - return 0 + try: + float_val = float(o) + except TypeError: + float_val = float(o[index]) - return -1 + val[0] = ctypes.c_double(float_val) + + log_print(f"GET array [double] ({index}) {n}: {val[0]}") + return 0 def param_provider_get_int_array_item( reader: Any, name: ctypes.c_char_p, - index: int, val: ctypes.POINTER(ctypes.c_int) + index: int, + val: ctypes.POINTER(ctypes.c_int) ) -> int: """ Retrieve an item from an integer array in the reader based on the provided name and index. @@ -325,25 +344,28 @@ def param_provider_get_int_array_item( n = name.decode('utf-8') c = reader.current() - if n in c: - o = c[n] + if n not in c: + log_print(f"Parameter {n} not found.") + return -1 - try: - int_val = int(o) - except TypeError: - int_val = int(o[index]) + o = c[n] - val[0] = ctypes.c_int(int_val) - log_print(f"GET array [int] ({index}) {n}: {val[0]}") - return 0 + try: + int_val = int(o) + except TypeError: + int_val = int(o[index]) - return -1 + val[0] = ctypes.c_int(int_val) + + log_print(f"GET array [int] ({index}) {n}: {val[0]}") + return 0 def param_provider_get_bool_array_item( reader: Any, name: ctypes.c_char_p, - index: int, val: ctypes.POINTER(ctypes.c_uint8) + index: int, + val: ctypes.POINTER(ctypes.c_uint8) ) -> int: """ Retrieve an item from a boolean array in the reader based on the provided name and index. @@ -367,25 +389,28 @@ def param_provider_get_bool_array_item( n = name.decode('utf-8') c = reader.current() - if n in c: - o = c[n] + if n not in c: + log_print(f"Parameter {n} not found.") + return -1 - try: - int_val = int(o) - except TypeError: - int_val = int(o[index]) + o = c[n] - val[0] = ctypes.c_uint8(int_val) - log_print(f"GET array [bool] ({index}) {n}: {bool(val[0])}") - return 0 + try: + int_val = int(o) + except TypeError: + int_val = int(o[index]) - return -1 + val[0] = ctypes.c_uint8(int_val) + + log_print(f"GET array [bool] ({index}) {n}: {bool(val[0])}") + return 0 def param_provider_get_string_array_item( reader: Any, name: ctypes.c_char_p, - index: int, val: ctypes.POINTER(ctypes.c_char_p) + index: int, + val: ctypes.POINTER(ctypes.c_char_p) ) -> int: """ Retrieve an item from a string array in the reader based on the provided name and index. @@ -406,30 +431,34 @@ def param_provider_get_string_array_item( int 0 if the value was found and retrieved successfully, -1 otherwise. """ - name_str = name.decode('utf-8') - current_reader = reader.current() - - if name_str in current_reader: - str_value = current_reader[name_str] - if isinstance(str_value, bytes): - bytes_val = str_value - elif isinstance(str_value, str): - bytes_val = str_value.encode('utf-8') - elif isinstance(str_value, np.ndarray): - bytes_val = str_value[index] - else: - raise TypeError( - "Unexpected type for str_value. " - "Must be of type bytes, str, or np.ndarray." - ) - - reader.buffer = bytes_val - val[0] = ctypes.cast(reader.buffer, ctypes.c_char_p) - log_print(f"GET array [string] ({index}) {name_str}: {reader.buffer.decode('utf-8')}") - return 0 + n = name.decode('utf-8') + c = reader.current() + + if n not in c: + log_print(f"Parameter {n} not found.") + return -1 + + o = c[n] + if isinstance(o, bytes): + bytes_val = o + elif isinstance(o, str): + bytes_val = o.encode('utf-8') + elif isinstance(o, np.ndarray): + bytes_val = o[index] + else: + raise TypeError( + "Unexpected type for name {n}: {type(o)}. " + "Must be of type bytes, str, or np.ndarray." + ) + + reader.buffer = bytes_val + val[0] = ctypes.cast(reader.buffer, ctypes.c_char_p) + + log_print(f"GET array [string] ({index}) {n}: {reader.buffer.decode('utf-8')}") + return 0 - return -1 +# %% Misc def param_provider_exists( reader: Any, @@ -484,6 +513,7 @@ def param_provider_is_array( c = reader.current() if n not in c: + log_print(f"Parameter {n} not found.") return -1 o = c[n] @@ -516,6 +546,7 @@ def param_provider_num_elements( c = reader.current() if n not in c: + log_print(f"Parameter {n} not found.") return -1 o = c[n]