Skip to content

Commit

Permalink
work in progress; cleaning up more warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMeisrimelModelon committed Jan 24, 2024
1 parent d39fe14 commit 62afd1d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
37 changes: 18 additions & 19 deletions src/pyfmi/fmi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ FMI2_KIND_DISCRETE = FMIL.fmi2_dependency_factor_kind_discrete
FMI_ME = FMIL.fmi1_fmu_kind_enu_me
FMI_CS_STANDALONE = FMIL.fmi1_fmu_kind_enu_cs_standalone
FMI_CS_TOOL = FMIL.fmi1_fmu_kind_enu_cs_tool
FMI_MIME_CS_STANDALONE = encode("application/x-fmu-sharedlibrary")
cdef FMI_MIME_CS_STANDALONE = encode("application/x-fmu-sharedlibrary")

FMI_REGISTER_GLOBALLY = 1
FMI_DEFAULT_LOG_LEVEL = FMIL.jm_log_level_error
Expand Down Expand Up @@ -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]*<size_t>nref, dtype=float, ndmin=1)

if nref == 0: ## get_real([])
return val
Expand Down Expand Up @@ -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]*<size_t>nref, dtype=int,ndmin=1)

if nref == 0: ## get_integer([])
return val
Expand Down Expand Up @@ -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]*<size_t>nref, dtype=N.uint32,ndmin=1).ravel()
orders = N.array([0]*<size_t>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]*<size_t>nref,dtype=float, ndmin=1)

status = FMIL.fmi1_import_get_real_output_derivatives(self._fmu, <FMIL.fmi1_value_reference_t*>value_refs.data, nref, <FMIL.fmi1_integer_t*>orders.data, <FMIL.fmi1_real_t*>values.data)

Expand Down Expand Up @@ -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]*<size_t>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.")
Expand All @@ -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]*<size_t>nref, dtype=N.uint32,ndmin=1).ravel()
for i in range(nref):
value_refs[i] = self.get_variable_valueref(variables[i])
else:
Expand Down Expand Up @@ -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(<int>variable_list_size):

variable = FMIL.fmi2_import_get_variable(variable_list, i)

Expand Down Expand Up @@ -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(<int>variable_list_size):

variable = FMIL.fmi2_import_get_variable(variable_list, i)

Expand Down Expand Up @@ -7197,8 +7197,8 @@ cdef class FMUModelCS2(FMUModelBase2):
cdef N.ndarray[FMIL.fmi2_integer_t, ndim=1, mode='c'] orders
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)
cdef FMIL.size_t nref = N.size(val)
orders = N.array([0]*<size_t>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?
Expand All @@ -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]*<size_t>nref, dtype=N.uint32,ndmin=1).ravel()
for i in range(<int>nref):
value_refs[i] = self.get_variable_valueref(variables[i])
orders[i] = order
else:
Expand Down Expand Up @@ -7260,12 +7260,11 @@ cdef class FMUModelCS2(FMUModelBase2):
"""
cdef int status
cdef unsigned int max_output_derivative
cdef FMIL.size_t nref
cdef FMIL.size_t nref
cdef N.ndarray[FMIL.fmi2_real_t, ndim=1, mode='c'] values
cdef N.ndarray[FMIL.fmi2_value_reference_t, ndim=1, mode='c'] value_refs
cdef N.ndarray[FMIL.fmi2_integer_t, ndim=1, mode='c'] orders


max_output_derivative = FMIL.fmi2_import_get_capability(self._fmu, FMIL.fmi2_cs_maxOutputDerivativeOrder)

if order < 1 or order > max_output_derivative:
Expand All @@ -7278,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]*<size_t>nref, dtype=N.uint32, ndmin=1).ravel()
orders = N.array([0]*<size_t>nref, dtype=N.int32)
for i in range(<int>nref):
value_refs[i] = self.get_variable_valueref(variables[i])
orders[i] = order
else:
Expand Down
32 changes: 25 additions & 7 deletions src/pyfmi/fmi_util.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,7 @@ 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')

Expand All @@ -1181,7 +1182,9 @@ 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(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
fread_ret = fread(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
if fread_ret != 1:
print("fread failed")
i = i + 1

fclose(cfile)
Expand All @@ -1208,6 +1211,7 @@ 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 = <DTYPE_t*>data.data
Expand All @@ -1217,7 +1221,9 @@ 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(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
fread_ret = fread(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
if fread_ret != 1:
print("fread failed")
i = i + 1

fclose(cfile)
Expand Down Expand Up @@ -1256,6 +1262,7 @@ 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')

Expand All @@ -1274,28 +1281,36 @@ 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(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
fread_ret = fread(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
if fread_ret != 1:
print("fread failed")
i += 1
else:
while iter_point < end_point:
os_specific_fseek(cfile, file_position+iter_point,0)
fread(<void*>(flag_ptr), sizeof_dtype, 1, cfile)
fread_ret = fread(<void*>(flag_ptr), sizeof_dtype, 1, cfile)
if fread_ret != 1:
print("fread failed")
iter_point += sizeof_type;
file_pos = os_specific_ftell(cfile)
if flag[0] == 1.0:
file_pos_model_var[model_var_counter] = file_pos
model_var_counter +=1
if not read_diag_data:
os_specific_fseek(cfile, file_position+iter_point+data_index*sizeof_type, 0)
fread(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
fread_ret = fread(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
if fread_ret != 1:
print("fread failed")
i += 1
iter_point += model_var_interval
elif flag[0] == 2.0:
file_pos_diag_var[diag_var_counter] = file_pos
diag_var_counter +=1
if read_diag_data:
os_specific_fseek(cfile, file_position+iter_point+data_index*sizeof_type, 0)
fread(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
fread_ret = fread(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
if fread_ret != 1:
print("fread failed")
i += 1
iter_point += diag_var_interval
else:
Expand Down Expand Up @@ -1334,6 +1349,7 @@ def read_name_list(file_name, int file_position, int nbr_variables, int max_leng
cdef char *tmp = <char*>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:
Expand All @@ -1346,7 +1362,9 @@ 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(<void*>(tmp), max_length, 1, cfile)
fread_ret = fread(<void*>(tmp), max_length, 1, cfile)
if fread_ret != 1:
print("fread failed")
py_str = tmp

if i == 0:
Expand Down

0 comments on commit 62afd1d

Please sign in to comment.