From 882162796ff5b250162771858b205aad279be23f Mon Sep 17 00:00:00 2001 From: PeterMeisrimelModelon <92585725+PeterMeisrimelModelon@users.noreply.github.com> Date: Wed, 24 Jan 2024 16:14:49 +0100 Subject: [PATCH] More cleanup; reverting some changes --- src/pyfmi/fmi.pyx | 30 +++++++++++++++--------------- src/pyfmi/fmi_extended.pyx | 10 +++++----- src/pyfmi/fmi_util.pyx | 34 +++++++--------------------------- 3 files changed, 27 insertions(+), 47 deletions(-) diff --git a/src/pyfmi/fmi.pyx b/src/pyfmi/fmi.pyx index 36da6c0a..e60b8268 100644 --- a/src/pyfmi/fmi.pyx +++ b/src/pyfmi/fmi.pyx @@ -1546,7 +1546,7 @@ cdef class FMUModelBase(ModelBase): cdef int status cdef N.ndarray[FMIL.fmi1_value_reference_t, ndim=1,mode='c'] val_ref = N.array(valueref, copy=False, dtype=N.uint32).ravel() cdef FMIL.size_t nref = N.size(val_ref) - cdef N.ndarray[FMIL.fmi1_real_t, ndim=1,mode='c'] val = N.array([0.0]*nref, dtype=float, ndmin=1) + cdef N.ndarray[FMIL.fmi1_real_t, ndim=1,mode='c'] val = N.array([0.0]*nref, dtype=float, ndmin=1) if nref == 0: ## get_real([]) return val @@ -1613,7 +1613,7 @@ cdef class FMUModelBase(ModelBase): cdef int status cdef N.ndarray[FMIL.fmi1_value_reference_t, ndim=1,mode='c'] val_ref = N.array(valueref, dtype=N.uint32,ndmin=1).ravel() cdef FMIL.size_t nref = N.size(val_ref) - cdef N.ndarray[FMIL.fmi1_integer_t, ndim=1,mode='c'] val = N.array([0]*nref, dtype=int,ndmin=1) + cdef N.ndarray[FMIL.fmi1_integer_t, ndim=1,mode='c'] val = N.array([0]*nref, dtype=int,ndmin=1) if nref == 0: ## get_integer([]) return val @@ -2906,15 +2906,15 @@ cdef class FMUModelCS1(FMUModelBase): value_refs[0] = self.get_variable_valueref(variables) elif isinstance(variables,list) and isinstance(variables[-1],str): nref = len(variables) - value_refs = N.array([0]*nref, dtype=N.uint32,ndmin=1).ravel() - orders = N.array([0]*nref, dtype=N.int32) + value_refs = N.array([0]*nref, dtype=N.uint32,ndmin=1).ravel() + orders = N.array([0]*nref, dtype=N.int32) for i in range(nref): value_refs[i] = self.get_variable_valueref(variables[i]) orders[i] = order else: raise FMUException("The variables must either be a string or a list of strings") - values = N.array([0.0]*nref,dtype=float, ndmin=1) + values = N.array([0.0]*nref,dtype=float, ndmin=1) status = FMIL.fmi1_import_get_real_output_derivatives(self._fmu, value_refs.data, nref, orders.data, values.data) @@ -2980,7 +2980,7 @@ cdef class FMUModelCS1(FMUModelBase): cdef N.ndarray[FMIL.fmi1_value_reference_t, ndim=1,mode='c'] value_refs cdef N.ndarray[FMIL.fmi1_real_t, ndim=1,mode='c'] val = N.array(values, dtype=float, ndmin=1).ravel() cdef FMIL.size_t nref = N.size(val) - orders = N.array([0]*nref, dtype=N.int32) + orders = N.array([0]*nref, dtype=N.int32) if nref != N.size(np_orders): raise FMUException("The number of variables must be the same as the number of orders.") @@ -2999,7 +2999,7 @@ cdef class FMUModelCS1(FMUModelBase): value_refs = N.array([0], dtype=N.uint32,ndmin=1).ravel() value_refs[0] = self.get_variable_valueref(variables) elif isinstance(variables,list) and isinstance(variables[-1],str): - value_refs = N.array([0]*nref, dtype=N.uint32,ndmin=1).ravel() + value_refs = N.array([0]*nref, dtype=N.uint32,ndmin=1).ravel() for i in range(nref): value_refs[i] = self.get_variable_valueref(variables[i]) else: @@ -5098,7 +5098,7 @@ cdef class FMUModelBase2(ModelBase): filter_list = self._convert_filter(filter) length_filter = len(filter_list) - for i in range(variable_list_size): + for i in range(variable_list_size): variable = FMIL.fmi2_import_get_variable(variable_list, i) @@ -5227,7 +5227,7 @@ cdef class FMUModelBase2(ModelBase): filter_list = self._convert_filter(filter) length_filter = len(filter_list) - for i in range(variable_list_size): + for i in range(variable_list_size): variable = FMIL.fmi2_import_get_variable(variable_list, i) @@ -7198,7 +7198,7 @@ cdef class FMUModelCS2(FMUModelBase2): cdef N.ndarray[FMIL.fmi2_value_reference_t, ndim=1, mode='c'] value_refs cdef N.ndarray[FMIL.fmi2_real_t, ndim=1, mode='c'] val = N.array(values, dtype=float, ndmin=1).ravel() cdef FMIL.size_t nref = N.size(val) - orders = N.array([0]*nref, dtype=N.int32) + orders = N.array([0]*nref, dtype=N.int32) can_interpolate_inputs = FMIL.fmi2_import_get_capability(self._fmu, FMIL.fmi2_cs_canInterpolateInputs) #NOTE IS THIS THE HIGHEST ORDER OF INTERPOLATION OR SIMPLY IF IT CAN OR NOT? @@ -7212,8 +7212,8 @@ cdef class FMUModelCS2(FMUModelBase2): value_refs = N.array([0], dtype=N.uint32, ndmin=1).ravel() value_refs[0] = self.get_variable_valueref(variables) elif isinstance(variables,list) and N.prod([int(isinstance(v,str)) for v in variables]): #prod equals 0 or 1 - value_refs = N.array([0]*nref, dtype=N.uint32,ndmin=1).ravel() - for i in range(nref): + value_refs = N.array([0]*nref, dtype=N.uint32,ndmin=1).ravel() + for i in range(nref): value_refs[i] = self.get_variable_valueref(variables[i]) orders[i] = order else: @@ -7277,9 +7277,9 @@ cdef class FMUModelCS2(FMUModelBase2): value_refs[0] = self.get_variable_valueref(variables) elif isinstance(variables,list) and N.prod([int(isinstance(v,str)) for v in variables]): #prod equals 0 or 1 nref = len(variables) - value_refs = N.array([0]*nref, dtype=N.uint32, ndmin=1).ravel() - orders = N.array([0]*nref, dtype=N.int32) - for i in range(nref): + value_refs = N.array([0]*nref, dtype=N.uint32, ndmin=1).ravel() + orders = N.array([0]*nref, dtype=N.int32) + for i in range(nref): value_refs[i] = self.get_variable_valueref(variables[i]) orders[i] = order else: diff --git a/src/pyfmi/fmi_extended.pyx b/src/pyfmi/fmi_extended.pyx index e0fa9407..01ee7045 100644 --- a/src/pyfmi/fmi_extended.pyx +++ b/src/pyfmi/fmi_extended.pyx @@ -293,9 +293,9 @@ cdef class FMUModelME1Extended(FMUModelME1): cdef N.ndarray[FMIL.fmi1_real_t, ndim=1,mode='c'] val = N.array(values, dtype=float, ndmin=1).flatten() nref = len(val) - orders = N.array([0]*nref, dtype=N.int32) + orders = N.array([0]*nref, dtype=N.int32) - if nref != len(np_orders): + if nref != len(np_orders): raise FMUException("The number of variables must be the same as the number of orders.") for i in range(len(np_orders)): @@ -308,13 +308,13 @@ cdef class FMUModelME1Extended(FMUModelME1): value_refs = N.array([0], dtype=N.uint32,ndmin=1).flatten() value_refs[0] = self.get_variable_valueref(variables) elif isinstance(variables,list) and isinstance(variables[-1],str): - value_refs = N.array([0]*nref, dtype=N.uint32,ndmin=1).flatten() - for i in range(nref): + value_refs = N.array([0]*nref, dtype=N.uint32,ndmin=1).flatten() + for i in range(nref): value_refs[i] = self.get_variable_valueref(variables[i]) else: raise FMUException("The variables must either be a string or a list of strings") - for i in range(nref): + for i in range(nref): self._input_derivatives[value_refs[i]][np_orders[i]-1] = val[i]/self._input_factorials[np_orders[i]-1] #Activate input diff --git a/src/pyfmi/fmi_util.pyx b/src/pyfmi/fmi_util.pyx index c5186d6e..e126eb57 100644 --- a/src/pyfmi/fmi_util.pyx +++ b/src/pyfmi/fmi_util.pyx @@ -1105,8 +1105,6 @@ cdef class DumpData: self.dump_data(np.array(float(self.model.time))) self.dump_data(diag_data) -# from libc.stdio cimport * - cdef extern from "stdio.h": FILE *fdopen(int, const char *) FILE *fopen(const char *, const char *) @@ -1174,7 +1172,6 @@ cdef _read_trajectory32( cdef np.ndarray[DTYPE32_t, ndim=1] data cdef DTYPE32_t* data_ptr cdef size_t sizeof_dtype = sizeof(DTYPE32_t) - cdef size_t fread_ret cfile = fopen(file_name, 'rb') @@ -1185,9 +1182,7 @@ cdef _read_trajectory32( #for offset in range(start_point, end_point, interval): for offset from start_point <= offset < end_point by interval: os_specific_fseek(cfile, file_position + offset, 0) - fread_ret = fread((data_ptr + i), sizeof_dtype, 1, cfile) - if fread_ret != 1: - print("fread failed") + fread((data_ptr + i), sizeof_dtype, 1, cfile) i = i + 1 fclose(cfile) @@ -1214,7 +1209,6 @@ cdef _read_trajectory64( cdef np.ndarray[DTYPE_t, ndim=1] data cdef DTYPE_t* data_ptr cdef size_t sizeof_dtype = sizeof(DTYPE_t) - cdef size_t fread_ret data = np.empty(nbr_points, dtype=DTYPE) data_ptr = data.data @@ -1224,9 +1218,7 @@ cdef _read_trajectory64( #for offset in range(start_point, end_point, interval): for offset from start_point <= offset < end_point by interval: os_specific_fseek(cfile, file_position + offset, 0) - fread_ret = fread((data_ptr + i), sizeof_dtype, 1, cfile) - if fread_ret != 1: - print("fread failed") + fread((data_ptr + i), sizeof_dtype, 1, cfile) i = i + 1 fclose(cfile) @@ -1265,7 +1257,6 @@ def read_diagnostics_trajectory( cdef size_t sizeof_dtype = sizeof(DTYPE_t) cdef np.ndarray[DTYPE_t, ndim=1] flag cdef DTYPE_t* flag_ptr - cdef size_t fread_ret cfile = fopen(file_name, 'rb') @@ -1284,16 +1275,12 @@ def read_diagnostics_trajectory( file_pos_list = file_pos_model_var for file_pos in file_pos_list: os_specific_fseek(cfile, file_pos+data_index*sizeof_type, 0) - fread_ret = fread((data_ptr + i), sizeof_dtype, 1, cfile) - if fread_ret != 1: - print("fread failed") + fread((data_ptr + i), sizeof_dtype, 1, cfile) i += 1 else: while iter_point < end_point: os_specific_fseek(cfile, file_position+iter_point,0) - fread_ret = fread((flag_ptr), sizeof_dtype, 1, cfile) - if fread_ret != 1: - print("fread failed") + fread((flag_ptr), sizeof_dtype, 1, cfile) iter_point += sizeof_type; file_pos = os_specific_ftell(cfile) if flag[0] == 1.0: @@ -1301,9 +1288,7 @@ def read_diagnostics_trajectory( model_var_counter +=1 if not read_diag_data: os_specific_fseek(cfile, file_position+iter_point+data_index*sizeof_type, 0) - fread_ret = fread((data_ptr + i), sizeof_dtype, 1, cfile) - if fread_ret != 1: - print("fread failed") + fread((data_ptr + i), sizeof_dtype, 1, cfile) i += 1 iter_point += model_var_interval elif flag[0] == 2.0: @@ -1311,9 +1296,7 @@ def read_diagnostics_trajectory( diag_var_counter +=1 if read_diag_data: os_specific_fseek(cfile, file_position+iter_point+data_index*sizeof_type, 0) - fread_ret = fread((data_ptr + i), sizeof_dtype, 1, cfile) - if fread_ret != 1: - print("fread failed") + fread((data_ptr + i), sizeof_dtype, 1, cfile) i += 1 iter_point += diag_var_interval else: @@ -1352,7 +1335,6 @@ def read_name_list(file_name, int file_position, int nbr_variables, int max_leng cdef char *tmp = FMIL.calloc(max_length,sizeof(char)) cdef bytes py_str cdef dict data = {} - cdef size_t fread_ret # This if-statement contributes to a performance gain within the for-loop that follows if python3_flag: @@ -1365,9 +1347,7 @@ def read_name_list(file_name, int file_position, int nbr_variables, int max_leng cfile = fopen(file_name, 'rb') fseek(cfile, file_position, 0) for i in range(nbr_variables): - fread_ret = fread((tmp), max_length, 1, cfile) - if fread_ret != 1: - print("fread failed") + fread((tmp), max_length, 1, cfile) py_str = tmp if i == 0: