def test_fmi_1_0_run_simulation_1(self): import math model_name = 'zigzag' fmu = fmippim.IncrementalFMU(FMU_URI_PRE + model_name, model_name, False, EPS_TIME) # construct string array for init parameter names vars = fmippim.new_string_array(2) fmippim.string_array_setitem(vars, 0, 'k') fmippim.string_array_setitem(vars, 1, 'x') # construct double array for init parameter values vals = fmippim.new_double_array(2) fmippim.double_array_setitem(vals, 0, 1.0) fmippim.double_array_setitem(vals, 1, 0.0) # construct string array with output names outputs = fmippim.new_string_array(2) fmippim.string_array_setitem(outputs, 0, 'x') fmippim.string_array_setitem(outputs, 1, 'der(x)') start_time = 0.0 step_size = 0.0025 horizon = 2 * step_size int_step_size = step_size / 2 fmu.defineRealOutputs(outputs, 2) status = fmu.init('zigzag1', vars, vals, 2, start_time, horizon, step_size, int_step_size) # initialize model self.assertEqual(status, 1) # check status result = fmu.getRealOutputs() self.assertEqual(fmippim.double_array_getitem(result, 0), 0.0) # check value self.assertEqual(fmippim.double_array_getitem(result, 1), 1.0) # check value time = start_time next = fmu.sync(-42.0, time) self.assertEqual(next, horizon) while (time + step_size - 1.0 < EPS_TIME): oldnext = next next = fmu.sync(time, min(time + step_size, next)) result = fmu.getRealOutputs() time = min(time + step_size, oldnext) if (math.fabs(time - 0.5) < 1e-6): x = fmippim.double_array_getitem(result, 0) self.assertTrue(math.fabs(x - 0.5) < 1e-4) self.assertTrue(math.fabs(time - 1.0) < step_size / 2) result = fmu.getRealOutputs() x = fmippim.double_array_getitem(result, 0) self.assertTrue(math.fabs(x - 1.0) < 1e-4)
def test_fmi_1_0_getrealoutputs(self): model_name = 'sine_standalone' fmu = fmippim.FixedStepSizeFMU(FMU_URI_PRE + model_name, model_name) # construct string array for init parameter names init_vars = fmippim.new_string_array(1) fmippim.string_array_setitem(init_vars, 0, 'omega') # construct double array for init parameter values init_vals = fmippim.new_double_array(1) fmippim.double_array_setitem(init_vals, 0, 0.1 * math.pi) # construct string array with output names outputs = fmippim.new_string_array(1) fmippim.string_array_setitem(outputs, 0, 'x') # define real output names fmu.defineRealOutputs(outputs, 1) start_time = 0. step_size = 1. # NB: fixed step size enforced by FMU! status = fmu.init("test_sine", init_vars, init_vals, 1, start_time, step_size) self.assertEqual(status, 1) result = fmu.getRealOutputs() self.assertEqual(fmippim.double_array_getitem(result, 0), 0.0) # check value
def test_fmi_1_0_time_event(self): import math model_name = 'step_t0' fmu = fmippim.IncrementalFMU(FMU_URI_PRE + model_name, model_name, False, EPS_TIME) # construct string array for init parameter names vars = fmippim.new_string_array(1) fmippim.string_array_setitem(vars, 0, 't0') # construct double array for init parameter values vals = fmippim.new_double_array(1) fmippim.double_array_setitem(vals, 0, 0.5) # construct string array with output names outputs = fmippim.new_string_array(1) fmippim.string_array_setitem(outputs, 0, 'x') start_time = 0.0 step_size = 0.3 horizon = 2 * step_size int_step_size = step_size / 2 fmu.defineRealOutputs(outputs, 1) status = fmu.init('step_t0', vars, vals, 1, start_time, horizon, step_size, int_step_size) # initialize model self.assertEqual(status, 1) # check status result = fmu.getRealOutputs() self.assertEqual(fmippim.double_array_getitem(result, 0), 0.0) # check value time = fmu.sync(-4711.0, start_time) self.assertTrue(math.fabs(time - 0.5) < EPS_TIME) result = fmu.getRealOutputs() self.assertEqual(fmippim.double_array_getitem(result, 0), 0.0) # check value time = fmu.sync(start_time, time) self.assertTrue(math.fabs(time - 0.5 - horizon) < EPS_TIME) result = fmu.getRealOutputs() self.assertEqual(fmippim.double_array_getitem(result, 0), 0.0) # check value
def test_fmi_2_0_indicated_event_timing(self): import math model_name = 'zigzag2' fmu = fmippim.IncrementalFMU(FMU_URI_PRE + model_name, model_name, False, EPS_TIME) # construct string array for init parameter names vars = fmippim.new_string_array(2) fmippim.string_array_setitem(vars, 0, 'k') fmippim.string_array_setitem(vars, 1, 'x') # construct double array for init parameter values vals = fmippim.new_double_array(2) fmippim.double_array_setitem(vals, 0, 1.0) fmippim.double_array_setitem(vals, 1, 0.0) # construct string array with output names outputs = fmippim.new_string_array(2) fmippim.string_array_setitem(outputs, 0, 'x') fmippim.string_array_setitem(outputs, 1, 'der(x)') start_time = 0.0 step_size = 0.11 horizon = 10 * step_size int_step_size = step_size / 2 fmu.defineRealOutputs(outputs, 2) status = fmu.init('zigzag1', vars, vals, 2, start_time, horizon, step_size, int_step_size) # initialize model self.assertEqual(status, 1) # check status result = fmu.getRealOutputs() self.assertEqual(fmippim.double_array_getitem(result, 0), 0.0) # check value self.assertEqual(fmippim.double_array_getitem(result, 1), 1.0) # check value # get first event at t=1.0 time = fmu.sync(-42.0, start_time) self.assertTrue(math.fabs(time - 1.0) < 1.0 * 100 * EPS_TIME) # get end of horizon event at t=2.1 time = fmu.sync(start_time, time) self.assertTrue(math.fabs(time - 2.1) < 2 * 2.1 * 100 * EPS_TIME)
def test_fmi_2_0_getrealoutputs(self): model_name = 'zigzag2' fmu = fmippim.IncrementalFMU(FMU_URI_PRE + model_name, model_name, False, EPS_TIME) # construct string array for init parameter names vars = fmippim.new_string_array(2) fmippim.string_array_setitem(vars, 0, 'k') fmippim.string_array_setitem(vars, 1, 'x') # construct double array for init parameter values vals = fmippim.new_double_array(2) fmippim.double_array_setitem(vals, 0, 10.0) fmippim.double_array_setitem(vals, 1, 1.0) # construct string array with output names outputs = fmippim.new_string_array(2) fmippim.string_array_setitem(outputs, 0, 'x') fmippim.string_array_setitem(outputs, 1, 'der(x)') start_time = 0.0 step_size = 0.0025 horizon = 2 * step_size int_step_size = step_size / 2 fmu.defineRealOutputs(outputs, 2) status = fmu.init('zigzag1', vars, vals, 2, start_time, horizon, step_size, int_step_size) # initialize model self.assertEqual(status, 1) # check status result = fmu.getRealOutputs() self.assertEqual(fmippim.double_array_getitem(result, 0), 0.0) # check value self.assertEqual(fmippim.double_array_getitem(result, 1), 10.0) # check value
def test_fmi_1_0_runsimulation(self): model_name = 'sine_standalone' fmu = fmippim.InterpolatingFixedStepSizeFMU(FMU_URI_PRE + model_name, model_name) # construct string array for init parameter names init_vars = fmippim.new_string_array(1) fmippim.string_array_setitem(init_vars, 0, 'omega') # construct double array for init parameter values init_vals = fmippim.new_double_array(1) fmippim.double_array_setitem(init_vals, 0, 0.1 * math.pi) # construct string array with output names outputs = fmippim.new_string_array(1) fmippim.string_array_setitem(outputs, 0, 'x') # define real output names fmu.defineRealOutputs(outputs, 1) start_time = 0. stop_time = 5. fmu_step_size = 1. # NB: fixed step size enforced by FMU! sim_step_size = 0.2 time = start_time status = fmu.init("test_sine", init_vars, init_vals, 1, start_time, fmu_step_size) self.assertEqual(status, 1) while (time <= stop_time): fmu.sync(time, time + sim_step_size) time += sim_step_size result = fmu.getRealOutputs() t0 = fmu_step_size * math.floor(time / fmu_step_size) t1 = t0 + fmu_step_size x0 = math.sin(0.1 * math.pi * t0) x1 = math.sin(0.1 * math.pi * t1) reference = x0 + (time - t0) * (x1 - x0) / (t1 - t0) self.assertTrue( math.fabs(fmippim.double_array_getitem(result, 0) - reference) < 1e-8) # check value