Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_single_sample(self):
t = np.array([0])
y = np.array([2])
# "interpolate" input with only one sample
v = Input.interpolate(1, t, y)
self.assertEqual(v, 2)
def test_input_continuous(self):
t = np.array( [ 0, 1, 2, 3])
y = np.array([[ 0, 0, 3, 3],
[-1, 0, 1, 2]])
# extrapolate left (hold)
v1, v2 = Input.interpolate(-1, t, y)
self.assertEqual(v1, 0)
self.assertEqual(v2, -1)
# hit sample
v1, v2 = Input.interpolate(1, t, y)
self.assertEqual(v1, 0)
self.assertEqual(v2, 0)
# interpolate (linear)
v1, v2 = Input.interpolate(1.5, t, y)
self.assertAlmostEqual(v1, 1.5)
self.assertAlmostEqual(v2, 0.5)
# extrapolate right (hold)
v1, v2 = Input.interpolate(4, t, y)
self.assertEqual(v1, 3)
self.assertEqual(v2, 2)
def simulateCS(model_description, fmu, start_time, stop_time, relative_tolerance, start_values, apply_default_start_values, input_signals, output, output_interval, timeout, step_finished):
if output_interval is None:
output_interval = auto_interval(stop_time - start_time)
sim_start = current_time()
is_fmi1 = model_description.fmiVersion == '1.0'
is_fmi2 = model_description.fmiVersion == '2.0'
if is_fmi2:
fmu.setupExperiment(tolerance=relative_tolerance, startTime=start_time)
input = Input(fmu=fmu, modelDescription=model_description, signals=input_signals)
time = start_time
apply_start_values(fmu, model_description, start_values, apply_default_start_values)
# initialize the model
if is_fmi1:
input.apply(time)
fmu.initialize(tStart=time, stopTime=stop_time)
elif is_fmi2:
fmu.enterInitializationMode()
input.apply(time)
fmu.exitInitializationMode()
else:
fmu.enterInitializationMode(tolerance=relative_tolerance, startTime=start_time)
input.apply(time)
>>> import numpy as np
>>> dtype = [('time', np.double), ('step', np.double), ('switch', np.bool_)]
>>> signals = np.array([(0.0, 0.0, False), (0.5, 0.0, False), (0.5, 0.1, True), (1.0, 1.0, True)], dtype=dtype)
"""
self.fmu = fmu
if signals is None:
self.t = None
return
# get the time grid
self.t = signals[signals.dtype.names[0]]
# find events
self.t_events = Input.findEvents(signals, modelDescription)
is_fmi1 = isinstance(fmu, _FMU1)
is_fmi2 = isinstance(fmu, _FMU2)
setters = dict()
# get the setters
if is_fmi1:
setters['Real'] = (fmu.fmi1SetReal, fmi1Real)
setters['Integer'] = (fmu.fmi1SetInteger, fmi1Integer)
setters['Boolean'] = (fmu.fmi1SetBoolean, c_int8)
elif is_fmi2:
setters['Real'] = (fmu.fmi2SetReal, fmi2Real)
setters['Integer'] = (fmu.fmi2SetInteger, fmi2Integer)
setters['Boolean'] = (fmu.fmi2SetBoolean, fmi2Boolean)
else:
step_size /= 2
sim_start = current_time()
time = start_time
is_fmi1 = model_description.fmiVersion == '1.0'
is_fmi2 = model_description.fmiVersion == '2.0'
is_fmi3 = model_description.fmiVersion.startswith('3.0')
if is_fmi1:
fmu.setTime(time)
elif is_fmi2:
fmu.setupExperiment(startTime=start_time)
input = Input(fmu, model_description, input_signals)
apply_start_values(fmu, model_description, start_values, apply_default_start_values)
# initialize
if is_fmi1:
input.apply(time)
(iterationConverged,
stateValueReferencesChanged,
stateValuesChanged,
terminateSimulation,
nextEventTimeDefined,
nextEventTime) = fmu.initialize()
if terminateSimulation: