How to use FMPy - 10 common examples

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 NTNU-IHB / FMI4j / test / comparison / benchmark.py View on Github external
def main():

    model_description = read_model_description(options.fmu_filename)

    unzipdir = extract(options.fmu_filename)

    fmu = FMU2Slave(guid=model_description.guid,
                    unzipDirectory=unzipdir,
                    modelIdentifier=model_description.coSimulation.modelIdentifier,
                    instanceName='instance1')

    # initialize
    fmu.instantiate()
    fmu.setupExperiment(tolerance=1E-4, startTime=0.0, stopTime=options.stop_time)
    fmu.enterInitializationMode()
    fmu.exitInitializationMode()

    start = datetime.datetime.now()
github CATIA-Systems / FMPy / tests / test_extracted_fmu.py View on Github external
def test_extracted_fmu(self):
        """ Simulate an extracted FMU """

        download_test_file('2.0', 'CoSimulation', 'MapleSim', '2017', 'CoupledClutches', 'CoupledClutches.fmu')

        # extract the FMU
        tempdir = extract('CoupledClutches.fmu')

        # load the model description before the simulation
        model_description = read_model_description(tempdir)

        result = simulate_fmu(tempdir, model_description=model_description)

        self.assertIsNotNone(result)

        # clean up
        shutil.rmtree(tempdir)
github NTNU-IHB / FMI4j / test / comparison / benchmark.py View on Github external
def main():

    model_description = read_model_description(options.fmu_filename)

    unzipdir = extract(options.fmu_filename)

    fmu = FMU2Slave(guid=model_description.guid,
                    unzipDirectory=unzipdir,
                    modelIdentifier=model_description.coSimulation.modelIdentifier,
                    instanceName='instance1')

    # initialize
    fmu.instantiate()
    fmu.setupExperiment(tolerance=1E-4, startTime=0.0, stopTime=options.stop_time)
    fmu.enterInitializationMode()
    fmu.exitInitializationMode()

    start = datetime.datetime.now()

    t = 0.0
    sum = 0.0
github CATIA-Systems / FMPy / tests / test_extracted_fmu.py View on Github external
def test_extracted_fmu(self):
        """ Simulate an extracted FMU """

        download_test_file('2.0', 'CoSimulation', 'MapleSim', '2017', 'CoupledClutches', 'CoupledClutches.fmu')

        # extract the FMU
        tempdir = extract('CoupledClutches.fmu')

        # load the model description before the simulation
        model_description = read_model_description(tempdir)

        result = simulate_fmu(tempdir, model_description=model_description)

        self.assertIsNotNone(result)

        # clean up
        shutil.rmtree(tempdir)
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 / tests / test_get_start_values.py View on Github external
def test_get_start_values(self):

        if platform.startswith('win'):
            fmi_versions = ['2.0']  # quick fix until FMUs are available
        elif platform.startswith(('darwin', 'linux')):
            fmi_versions = ['2.0']
        else:
            self.fail('Platform not supported')

        for fmi_version in fmi_versions:

            for fmi_type in ['CoSimulation', 'ModelExchange']:

                download_test_file(fmi_version, fmi_type, 'MapleSim', '2016.2', 'CoupledClutches', 'CoupledClutches.fmu')

                start_values = get_start_values('CoupledClutches.fmu')

                self.assertEqual(start_values['CoupledClutches1_freqHz'], '0.2')
github CATIA-Systems / FMPy / tests / test_ssp.py View on Github external
    @skipIf(platform != 'win32', "Current platform not supported by this SSP")
    def test_simulate_sample_system_with_parameters(self):

        ssv_filename = self.ssp_example_path('SampleSystemParameterValues.ssv')
        parameter_set = read_ssv(ssv_filename)

        filename = self.ssp_example_path('SampleSystem.ssp')
        sine = lambda t: np.sin(t * 2 * np.pi)
        result = simulate_ssp(filename, stop_time=1.0, step_size=0.01, parameter_set=parameter_set, input={'In1': sine})

        # check if the input has been applied correctly
        self.assertTrue(np.all(np.abs(result['In1'] - sine(result['time'])) < 0.01))
github CATIA-Systems / FMPy / tests / test_cross_check.py View on Github external
background-color: #5cb85c;
            }
        
    
    
        ''')
    html.write('\n')

    for fmiVersion in fmiVersions:

        for fmiType in fmiTypes:

            platformDir = os.path.join(testFMUsDirectory,
                                       'FMI_1.0' if fmiVersion == 'fmi1' else 'FMI_2.0',
                                       'ModelExchange' if fmiType == 'me' else 'CoSimulation',
                                       fmpy.platform)

            for tool in os.listdir(platformDir):

                if tools is not None and not tool in tools:
                    continue

                toolDir = os.path.join(platformDir, tool)

                if not os.path.isdir(toolDir):
                    continue

                versions = os.listdir(toolDir)
                version = sorted(versions)[-1] # take only the latest version
                versionDir = os.path.join(toolDir, version)

                for model in os.listdir(versionDir):
<table><tbody><tr><th>Model</th><th>XML</th><th>_in.csv</th><th>_cc.csv</th><th>_ref.csv</th></tr></tbody></table>