Skip to content

Commit

Permalink
More cleanup; reverting some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMeisrimelModelon committed Jan 24, 2024
1 parent 69f6e07 commit 8821627
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 47 deletions.
30 changes: 15 additions & 15 deletions src/pyfmi/fmi.pyx
Original file line number Diff line number Diff line change
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]*<size_t>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
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]*<size_t>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
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]*<size_t>nref, dtype=N.uint32,ndmin=1).ravel()
orders = N.array([0]*<size_t>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]*<size_t>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, <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]*<size_t>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.")
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]*<size_t>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:
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(<size_t>variable_list_size):
for i in range(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(<size_t>variable_list_size):
for i in range(variable_list_size):

variable = FMIL.fmi2_import_get_variable(variable_list, i)

Expand Down Expand Up @@ -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]*<size_t>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?
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]*<size_t>nref, dtype=N.uint32,ndmin=1).ravel()
for i in range(<size_t>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:
Expand Down Expand Up @@ -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]*<size_t>nref, dtype=N.uint32, ndmin=1).ravel()
orders = N.array([0]*<size_t>nref, dtype=N.int32)
for i in range(<size_t>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:
Expand Down
10 changes: 5 additions & 5 deletions src/pyfmi/fmi_extended.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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]*<size_t>nref, dtype=N.int32)
orders = N.array([0]*nref, dtype=N.int32)

if <size_t>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)):
Expand All @@ -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]*<size_t>nref, dtype=N.uint32,ndmin=1).flatten()
for i in range(<size_t>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(<size_t>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
Expand Down
34 changes: 7 additions & 27 deletions src/pyfmi/fmi_util.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)
Expand Down Expand Up @@ -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')

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

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

fclose(cfile)
Expand Down Expand Up @@ -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')

Expand All @@ -1284,36 +1275,28 @@ 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(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
if fread_ret != 1:
print("fread failed")
fread(<void*>(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(<void*>(flag_ptr), sizeof_dtype, 1, cfile)
if fread_ret != 1:
print("fread failed")
fread(<void*>(flag_ptr), sizeof_dtype, 1, cfile)
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_ret = fread(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
if fread_ret != 1:
print("fread failed")
fread(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
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_ret = fread(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
if fread_ret != 1:
print("fread failed")
fread(<void*>(data_ptr + i), sizeof_dtype, 1, cfile)
i += 1
iter_point += diag_var_interval
else:
Expand Down Expand Up @@ -1352,7 +1335,6 @@ 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 @@ -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(<void*>(tmp), max_length, 1, cfile)
if fread_ret != 1:
print("fread failed")
fread(<void*>(tmp), max_length, 1, cfile)
py_str = tmp

if i == 0:
Expand Down

0 comments on commit 8821627

Please sign in to comment.