Skip to content

Commit

Permalink
Various small cleanups; names; imports
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMeisrimelModelon committed Jan 26, 2024
1 parent e9fc6ac commit 4a60892
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/common/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,7 @@ def _get_interpolated_trajectory(self, data_index):
def _get_description(self):
if not self._description:
description = scipy.io.loadmat(self._fname,chars_as_strings=False, variable_names=["description"])["description"]
self._description = ["".join(description[:,i]).rstrip() for i in range(description[0,:].size)]
self._description = ["".join(description[:,i]).rstrip() for i in range(np.size(description[0,:]))]

return self._description

Expand Down
5 changes: 2 additions & 3 deletions src/common/log/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@

from xml import sax
import re
import os
import numpy as np
from distutils.util import strtobool
from pyfmi.common.log.tree import *
from pyfmi.common.log.tree import Node, Comment
from pyfmi.fmi_util import python3_flag
from pyfmi.fmi import FMUException

Expand Down Expand Up @@ -206,7 +205,7 @@ def extract_xml_log(dest, log, modulename = 'Model'):
dest --
Name of the file which holds the extracted log, or a stream to write to
that supports the function 'write'. Default behaviour is to write to a file.
that supports the function 'write'. Default behavior is to write to a file.
Default: get_log_filename() + xml
log --
String of filename to extract log from or a stream. The support for stream
Expand Down
2 changes: 1 addition & 1 deletion src/common/log/prettyprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"""

from numpy import ndarray
from pyfmi.common.log.tree import *
from pyfmi.common.log.tree import Node, Comment

def prettyprint(write, node):
"""Prettyprint a log node to the write callback write."""
Expand Down
20 changes: 10 additions & 10 deletions src/pyfmi/fmi.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ cdef class FMUModelBase2(ModelBase):
cdef int _get_directional_derivative(self, N.ndarray v_ref, N.ndarray z_ref, N.ndarray dv, N.ndarray dz) except -1
cpdef set_real(self, valueref, values)
cpdef N.ndarray get_real(self, valueref)
cdef int _set_real(self, FMIL.fmi2_value_reference_t* vrefs, FMIL.fmi2_real_t* values, size_t size)
cdef int _get_real_by_ptr(self, FMIL.fmi2_value_reference_t* vrefs, size_t size, FMIL.fmi2_real_t* values)
cdef int _get_real_by_list(self, FMIL.fmi2_value_reference_t[:] valueref, size_t size, FMIL.fmi2_real_t[:] values)
cdef int _get_integer(self, FMIL.fmi2_value_reference_t[:] valueref, size_t size, FMIL.fmi2_integer_t[:] values)
cdef int _get_boolean(self, FMIL.fmi2_value_reference_t[:] valueref, size_t size, FMIL.fmi2_real_t[:] values)
cdef int _set_real(self, FMIL.fmi2_value_reference_t* vrefs, FMIL.fmi2_real_t* values, size_t _size)
cdef int _get_real_by_ptr(self, FMIL.fmi2_value_reference_t* vrefs, size_t _size, FMIL.fmi2_real_t* values)
cdef int _get_real_by_list(self, FMIL.fmi2_value_reference_t[:] valueref, size_t _size, FMIL.fmi2_real_t[:] values)
cdef int _get_integer(self, FMIL.fmi2_value_reference_t[:] valueref, size_t _size, FMIL.fmi2_integer_t[:] values)
cdef int _get_boolean(self, FMIL.fmi2_value_reference_t[:] valueref, size_t _size, FMIL.fmi2_real_t[:] values)

cdef class FMUModelCS2(FMUModelBase2):

Expand Down Expand Up @@ -276,10 +276,10 @@ cdef class WorkerClass2:
cpdef verify_dimensions(self, int dim)

cdef class _ForTestingFMUModelME2(FMUModelME2):
cdef int _get_real_by_ptr(self, FMIL.fmi2_value_reference_t* vrefs, size_t size, FMIL.fmi2_real_t* values)
cdef int _set_real(self, FMIL.fmi2_value_reference_t* vrefs, FMIL.fmi2_real_t* values, size_t size)
cdef int _get_real_by_list(self, FMIL.fmi2_value_reference_t[:] valueref, size_t size, FMIL.fmi2_real_t[:] values)
cdef int _get_integer(self, FMIL.fmi2_value_reference_t[:] valueref, size_t size, FMIL.fmi2_integer_t[:] values)
cdef int _get_boolean(self, FMIL.fmi2_value_reference_t[:] valueref, size_t size, FMIL.fmi2_real_t[:] values)
cdef int _get_real_by_ptr(self, FMIL.fmi2_value_reference_t* vrefs, size_t _size, FMIL.fmi2_real_t* values)
cdef int _set_real(self, FMIL.fmi2_value_reference_t* vrefs, FMIL.fmi2_real_t* values, size_t _size)
cdef int _get_real_by_list(self, FMIL.fmi2_value_reference_t[:] valueref, size_t _size, FMIL.fmi2_real_t[:] values)
cdef int _get_integer(self, FMIL.fmi2_value_reference_t[:] valueref, size_t _size, FMIL.fmi2_integer_t[:] values)
cdef int _get_boolean(self, FMIL.fmi2_value_reference_t[:] valueref, size_t _size, FMIL.fmi2_real_t[:] values)
cdef int _get_nominal_continuous_states_fmil(self, FMIL.fmi2_real_t* xnominal, size_t nx)
cpdef set_initialized_fmu(self, int value)
52 changes: 26 additions & 26 deletions src/pyfmi/fmi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4255,17 +4255,17 @@ cdef class FMUModelBase2(ModelBase):
if status != 0:
raise FMUException('Failed to set the Real values. See the log for possibly more information.')

cdef int _get_real_by_list(self, FMIL.fmi2_value_reference_t[:] valueref, size_t size, FMIL.fmi2_real_t[:] values):
return FMIL.fmi2_import_get_real(self._fmu, &valueref[0], size, &values[0])
cdef int _get_real_by_list(self, FMIL.fmi2_value_reference_t[:] valueref, size_t _size, FMIL.fmi2_real_t[:] values):
return FMIL.fmi2_import_get_real(self._fmu, &valueref[0], _size, &values[0])

cdef int _get_real_by_ptr(self, FMIL.fmi2_value_reference_t* vrefs, size_t size, FMIL.fmi2_real_t* values):
return FMIL.fmi2_import_get_real(self._fmu, vrefs, size, values)
cdef int _get_real_by_ptr(self, FMIL.fmi2_value_reference_t* vrefs, size_t _size, FMIL.fmi2_real_t* values):
return FMIL.fmi2_import_get_real(self._fmu, vrefs, _size, values)

cdef int _set_real(self, FMIL.fmi2_value_reference_t* vrefs, FMIL.fmi2_real_t* values, size_t size):
return FMIL.fmi2_import_set_real(self._fmu, vrefs, size, values)
cdef int _set_real(self, FMIL.fmi2_value_reference_t* vrefs, FMIL.fmi2_real_t* values, size_t _size):
return FMIL.fmi2_import_set_real(self._fmu, vrefs, _size, values)

cdef int _get_integer(self, FMIL.fmi2_value_reference_t[:] valueref, size_t size, FMIL.fmi2_integer_t[:] values):
return FMIL.fmi2_import_get_integer(self._fmu, &valueref[0], size, &values[0])
cdef int _get_integer(self, FMIL.fmi2_value_reference_t[:] valueref, size_t _size, FMIL.fmi2_integer_t[:] values):
return FMIL.fmi2_import_get_integer(self._fmu, &valueref[0], _size, &values[0])

def get_integer(self, valueref):
"""
Expand Down Expand Up @@ -4333,13 +4333,13 @@ cdef class FMUModelBase2(ModelBase):
if status != 0:
raise FMUException('Failed to set the Integer values. See the log for possibly more information.')

cdef int _get_boolean(self, FMIL.fmi2_value_reference_t[:] valueref, size_t size, FMIL.fmi2_real_t[:] values):
cdef int _get_boolean(self, FMIL.fmi2_value_reference_t[:] valueref, size_t _size, FMIL.fmi2_real_t[:] values):
cdef int status
cdef void* output_value = FMIL.malloc(sizeof(FMIL.fmi2_boolean_t)*size)
cdef void* output_value = FMIL.malloc(sizeof(FMIL.fmi2_boolean_t)*_size)

status = FMIL.fmi2_import_get_boolean(self._fmu, &valueref[0], size, <FMIL.fmi2_boolean_t*> output_value)
status = FMIL.fmi2_import_get_boolean(self._fmu, &valueref[0], _size, <FMIL.fmi2_boolean_t*> output_value)

for i in range(size):
for i in range(_size):
values[i] = (<FMIL.fmi2_boolean_t*>output_value)[i]==1

FMIL.free(output_value)
Expand Down Expand Up @@ -8498,25 +8498,25 @@ cdef class FMUModelME2(FMUModelBase2):
return A

cdef class _ForTestingFMUModelME2(FMUModelME2):
cdef int _get_real_by_ptr(self, FMIL.fmi2_value_reference_t* vrefs, size_t size, FMIL.fmi2_real_t* values):
vr = N.zeros(size)
for i in range(size):
cdef int _get_real_by_ptr(self, FMIL.fmi2_value_reference_t* vrefs, size_t _size, FMIL.fmi2_real_t* values):
vr = N.zeros(_size)
for i in range(_size):
vr[i] = vrefs[i]

try:
vv = self.get_real(vr)
except Exception:
return FMIL.fmi2_status_error

for i in range(size):
for i in range(_size):
values[i] = vv[i]

return FMIL.fmi2_status_ok

cdef int _set_real(self, FMIL.fmi2_value_reference_t* vrefs, FMIL.fmi2_real_t* values, size_t size):
vr = N.zeros(size)
vv = N.zeros(size)
for i in range(size):
cdef int _set_real(self, FMIL.fmi2_value_reference_t* vrefs, FMIL.fmi2_real_t* values, size_t _size):
vr = N.zeros(_size)
vv = N.zeros(_size)
for i in range(_size):
vr[i] = vrefs[i]
vv[i] = values[i]

Expand All @@ -8527,28 +8527,28 @@ cdef class _ForTestingFMUModelME2(FMUModelME2):

return FMIL.fmi2_status_ok

cdef int _get_real_by_list(self, FMIL.fmi2_value_reference_t[:] valueref, size_t size, FMIL.fmi2_real_t[:] values):
cdef int _get_real_by_list(self, FMIL.fmi2_value_reference_t[:] valueref, size_t _size, FMIL.fmi2_real_t[:] values):
try:
tmp = self.get_real(valueref)
for i in range(size):
for i in range(_size):
values[i] = tmp[i]
except Exception:
return FMIL.fmi2_status_error
return FMIL.fmi2_status_ok

cdef int _get_integer(self, FMIL.fmi2_value_reference_t[:] valueref, size_t size, FMIL.fmi2_integer_t[:] values):
cdef int _get_integer(self, FMIL.fmi2_value_reference_t[:] valueref, size_t _size, FMIL.fmi2_integer_t[:] values):
try:
tmp = self.get_integer(valueref)
for i in range(size):
for i in range(_size):
values[i] = tmp[i]
except Exception:
return FMIL.fmi2_status_error
return FMIL.fmi2_status_ok

cdef int _get_boolean(self, FMIL.fmi2_value_reference_t[:] valueref, size_t size, FMIL.fmi2_real_t[:] values):
cdef int _get_boolean(self, FMIL.fmi2_value_reference_t[:] valueref, size_t _size, FMIL.fmi2_real_t[:] values):
try:
tmp = self.get_boolean(valueref)
for i in range(size):
for i in range(_size):
values[i] = tmp[i]
except Exception:
return FMIL.fmi2_status_error
Expand Down
14 changes: 7 additions & 7 deletions src/pyfmi/fmi_util.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1029,14 +1029,14 @@ class Graph:

cdef class DumpData:
def __init__(self, model, filep, real_var_ref, int_var_ref, bool_var_ref, with_diagnostics):
if type(model) != FMUModelME2:
self.real_var_ref = np.array(real_var_ref, ndmin=1).ravel()
self.int_var_ref = np.array(int_var_ref, ndmin=1).ravel()
self.bool_var_ref = np.array(bool_var_ref, ndmin=1).ravel()
else:
if type(model) == FMUModelME2:
self.real_var_ref = np.array(real_var_ref, dtype=np.uint32, ndmin=1).ravel()
self.int_var_ref = np.array(int_var_ref, dtype=np.uint32, ndmin=1).ravel()
self.bool_var_ref = np.array(bool_var_ref, dtype=np.uint32, ndmin=1).ravel()
else:
self.real_var_ref = np.array(real_var_ref, ndmin=1).ravel()
self.int_var_ref = np.array(int_var_ref, ndmin=1).ravel()
self.bool_var_ref = np.array(bool_var_ref, ndmin=1).ravel()

self.real_size = np.size(self.real_var_ref)
self.int_size = np.size(self.int_var_ref)
Expand Down Expand Up @@ -1065,7 +1065,7 @@ cdef class DumpData:
if self._with_diagnostics:
self.dump_data(np.array(float(1.0)))
if self.model_me2_instance:
self.time_tmp[0] = self.model_me2._get_time()
self.time_tmp[0] = self.model_me2.time
self.dump_data(self.time_tmp)

if self.real_size > 0:
Expand Down Expand Up @@ -1099,7 +1099,7 @@ cdef class DumpData:
""" Saves a point of diagnostics data to the result. """
self.dump_data(np.array(float(2.0)))
if self.model_me2_instance:
self.time_tmp[0] = self.model_me2._get_time()
self.time_tmp[0] = self.model_me2.time
self.dump_data(self.time_tmp)
else:
self.dump_data(np.array(float(self.model.time)))
Expand Down
2 changes: 1 addition & 1 deletion src/pyfmi/simulation/assimulo_interface.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ cdef class FMIODE2(cExplicit_Problem):
self.input_names = []
self.timings = {"handle_result": 0.0}

if type(model) == FMUModelME2: #isinstance(model, FMUModelME2):
if type(model)==FMUModelME2: #if isinstance(model, FMUModelME2):
self.model_me2 = model
self.model_me2_instance = 1
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def get_real(self, vref, evaluate = True):
if evaluate:
self.get_derivatives()
vals = []
if type(vref) == int:
if isinstance(vref, int):
vref = [vref]
for v in vref:
vals.append(self.values[v])
Expand Down

0 comments on commit 4a60892

Please sign in to comment.