How to use the fmpy.simulate_fmu function in FMPy

To help you get started, we’ve selected a few FMPy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CATIA-Systems / Test-FMUs / test_build.py View on Github external
in_csv = os.path.join(test_fmus_dir, model, model + '_in.csv')
                input = read_csv(in_csv)
            else:
                start_values = {}
                input = None

            ref_csv = os.path.join(test_fmus_dir, model, model + '_ref.csv')

            for fmi_type in fmi_types:

                ref = read_csv(ref_csv)

                if compile:
                    compile_platform_binary(fmu_filename)

                result = simulate_fmu(fmu_filename,
                                      fmi_type=fmi_type,
                                      start_values=start_values,
                                      input=input)

                dev = validate_result(result, ref)

                self.assertLess(dev, 0.2, "Failed to validate " + model)
github CATIA-Systems / FMPy / tests / test_fmu_info.py View on Github external
def test_unsupported_fmi_type(self):
        with self.assertRaises(Exception) as context:
            simulate_fmu('CoupledClutches.fmu', fmi_type='CoSimulation')
        self.assertEqual('FMI type "CoSimulation" is not supported by the FMU', str(context.exception))
github CATIA-Systems / FMPy / tests / test_output_grid.py View on Github external
T2 = 0.5

        # common arguments
        kwargs = {
            'filename': 'CoupledClutches.fmu',
            'start_time': start_time,
            'stop_time': stop_time,
            'fmi_type': 'ModelExchange',
            'step_size': step_size,
            'output_interval': output_interval,
            'input': input,
            'start_values': {'CoupledClutches1_T2': T2}
        }

        # fixed step w/o events
        result = simulate_fmu(solver='Euler', record_events=False, **kwargs)

        time = result['time']
        self.assertAlmostEqual(time[0], start_time, msg="First sample time must be equal to start_time")
        self.assertAlmostEqual(time[-1], stop_time, msg="Last sample time must be equal to stop_time")
        self.assertTrue(np.all(np.isclose(np.diff(time), output_interval)), msg="Output intervals must be regular")

        # fixed step w/ events
        result = simulate_fmu(solver='Euler', record_events=True, **kwargs)

        time = result['time']
        self.assertAlmostEqual(time[0], start_time, msg="First sample time must be equal to start_time")
        self.assertAlmostEqual(time[-1], stop_time, msg="Last sample time must be equal to stop_time")

        # variable step w/o events
        result = simulate_fmu(solver='CVode', record_events=False, **kwargs)
github CATIA-Systems / FMPy / fmpy / command_line.py View on Github external
if args.start_values:
            if len(args.start_values) % 2 != 0:
                raise Exception("Start values must be name-value pairs.")
            start_values = {k: v for k, v in zip(args.start_values[::2], args.start_values[1::2])}
        else:
            start_values = {}

        input = read_csv(args.input_file) if args.input_file else None

        if args.fmi_logging:
            fmi_call_logger = lambda s: print('[FMI] ' + s)
        else:
            fmi_call_logger = None

        result = simulate_fmu(args.fmu_filename,
                              validate=args.validate,
                              start_time=args.start_time,
                              stop_time=args.stop_time,
                              solver=args.solver,
                              step_size=args.step_size,
                              relative_tolerance=args.relative_tolerance,
                              output_interval=args.output_interval,
                              record_events=not args.dont_record_events,
                              fmi_type=None,
                              start_values=start_values,
                              apply_default_start_values=args.apply_default_start_values,
                              input=input,
                              output=args.output_variables,
                              timeout=args.timeout,
                              debug_logging=args.debug_logging,
                              visible=args.visible,
github CATIA-Systems / FMPy / fmpy / examples / coupled_clutches.py View on Github external
def simulate_coupled_clutches(fmi_version='2.0',
                              fmi_type='ModelExchange',
                              output=['outputs[1]', 'outputs[2]', 'outputs[3]', 'outputs[4]'],
                              solver='CVode',
                              fmi_logging=False,
                              show_plot=True):

    # download the FMU and input file
    for filename in ['CoupledClutches.fmu', 'CoupledClutches_in.csv']:
        download_test_file(fmi_version, fmi_type, 'MapleSim', '2017', 'CoupledClutches', filename)

    print("Loading input...")
    input = np.genfromtxt('CoupledClutches_in.csv', delimiter=',', names=True)

    print("Simulating CoupledClutches.fmu (FMI %s, %s, %s)..." % (fmi_version, fmi_type, solver))
    result = simulate_fmu(
        filename='CoupledClutches.fmu',
        start_time=0,
        stop_time=1.5,
        solver=solver,
        step_size=1e-2,
        output_interval=2e-2,
        start_values={'CoupledClutches1_freqHz': 0.4},
        input=input,
        output=output,
        validate=False,
        fmi_logging=fmi_logging)

    if show_plot:
        print("Plotting results...")
        from fmpy.util import plot_result
        plot_result(result=result,
github CATIA-Systems / FMPy / fmpy / cross_check / __main__.py View on Github external
def simulate(options):

    # read the input file
    if 'input_filename' in options:
        input = read_csv(options['input_filename'])
    else:
        input = None

    # simulate the FMU
    result = fmpy.simulate_fmu(filename=options['fmu_filename'],
                               validate=False,
                               step_size=options['step_size'],
                               stop_time=options['stop_time'],
                               input=input,
                               output=options['output_variable_names'])

    return result