How to use the fmpy.fmi2.FMU2Slave 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 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
    i = 0
    # simulation loop
github CATIA-Systems / FMPy / tests / test_common_functions.py View on Github external
for v in model_description.modelVariables:
                variables[v.name] = v

            args = {
                'guid': guid,
                'modelIdentifier': model_description.coSimulation.modelIdentifier,
                'unzipDirectory': unzipdir,
                'instanceName': None
            }

            if fmi_version == '1.0':
                fmu = FMU1Slave(**args)
                fmu.instantiate("instance1")
                fmu.initialize()
            else:
                fmu = FMU2Slave(**args)
                fmu.instantiate(loggingOn=False)
                fmu.setupExperiment(tolerance=None)
                fmu.enterInitializationMode()
                fmu.exitInitializationMode()

            # get types platform
            types_platform = fmu.getTypesPlatform()

            if fmi_version == '1.0':
                self.assertEqual('standard32', types_platform)
            else:
                self.assertEqual('default', types_platform)

            # get FMI version
            version = fmu.getVersion()
            self.assertEqual(fmi_version, version)
github CATIA-Systems / FMPy / tests / test_serialize_fmu_state.py View on Github external
def test_serialize_fmu_state(self):

        fmu_filename = 'Rectifier.fmu'

        # download the FMU
        download_test_file('2.0', 'cs', 'MapleSim', '2016.2', 'Rectifier', fmu_filename)

        # read the model description
        model_description = read_model_description(fmu_filename)

        # extract the FMU
        unzipdir = extract(fmu_filename)

        # instantiate the FMU
        fmu = FMU2Slave(guid=model_description.guid,
                        unzipDirectory=unzipdir,
                        modelIdentifier=model_description.coSimulation.modelIdentifier)

        # initialize
        fmu.instantiate()
        fmu.setupExperiment()
        fmu.enterInitializationMode()
        fmu.exitInitializationMode()

        # get the FMU state
        state = fmu.getFMUstate()

        # serialize the FMU state
        serialized_state = fmu.serializeFMUstate(state)

        # de-serialize the FMU state
github CATIA-Systems / FMPy / fmpy / util.py View on Github external
fmu_kwargs = {
        'guid': model_description.guid,
        'modelIdentifier': implementation.modelIdentifier,
        'unzipDirectory': unzipdir,
    }

    if model_description.fmiVersion == '1.0':
        if model_description.coSimulation is not None:
            fmu = FMU1Slave(**fmu_kwargs)
        else:
            fmu = FMU1Model(**fmu_kwargs)
        fmu.instantiate()
        fmu.initialize()
    else:
        if model_description.coSimulation is not None:
            fmu = FMU2Slave(**fmu_kwargs)
        else:
            fmu = FMU2Model(**fmu_kwargs)
        fmu.instantiate()
        fmu.enterInitializationMode()
        fmu.exitInitializationMode()

    # read the start values
    start_values = {}

    for variable in model_description.modelVariables:
        try:
            vr = [variable.valueReference]

            if variable.type == 'Real':
                value = fmu.getReal(vr=vr)
                start_values[variable.name] = str(value[0])
github NTNU-IHB / FMI4j / misc / benchmark / python / benchmark.py View on Github external
def main():

    option = options[1]

    model_description = read_model_description(option.fmu_filename)

    unzipdir = extract(option.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=option.stop_time)
    fmu.enterInitializationMode()
    fmu.exitInitializationMode()

    start = datetime.datetime.now()

    i = 0
    t = 0.0
    sum = 0.0
    # simulation loop
github NTNU-IHB / FMI4j / benchmark / python / benchmark.py View on Github external
def main():

    option = options[1]

    model_description = read_model_description(option.fmu_filename)

    unzipdir = extract(option.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=option.stop_time)
    fmu.enterInitializationMode()
    fmu.exitInitializationMode()

    start = datetime.datetime.now()

    i = 0
    t = 0.0
    sum = 0.0
    # simulation loop
github CATIA-Systems / FMPy / fmpy / fmi2.py View on Github external
def __init__(self, instanceName=None, **kwargs):

        kwargs['instanceName'] = instanceName

        super(FMU2Slave, self).__init__(**kwargs)

        # Simulating the slave

        self._fmi2Function('fmi2SetRealInputDerivatives',
                           ['c', 'vr', 'nvr', 'order', 'value'],
                           [fmi2Component, POINTER(fmi2ValueReference), c_size_t, POINTER(fmi2Integer), POINTER(fmi2Real)])

        self._fmi2Function('fmi2GetRealOutputDerivatives',
                           ['c', 'vr', 'nvr', 'order', 'value'],
                           [fmi2Component, POINTER(fmi2ValueReference), c_size_t, POINTER(fmi2Integer), POINTER(fmi2Real)])

        self._fmi2Function('fmi2DoStep',
                           ['component', 'currentCommunicationPoint', 'communicationStepSize', 'noSetFMUStatePriorToCurrentPoint'],
                           [fmi2Component, fmi2Real, fmi2Real, fmi2Boolean])

        self._fmi2Function('fmi2CancelStep', ['component'], [fmi2Component])
github CATIA-Systems / FMPy / fmpy / examples / custom_input.py View on Github external
# read the model description
    model_description = read_model_description(fmu_filename)

    # collect the value references
    vrs = {}
    for variable in model_description.modelVariables:
        vrs[variable.name] = variable.valueReference

    # get the value references for the variables we want to get/set
    vr_inputs   = vrs['inputs']      # normalized force on the 3rd clutch
    vr_outputs4 = vrs['outputs[4]']  # angular velocity of the 4th inertia

    # extract the FMU
    unzipdir = extract(fmu_filename)

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

    # initialize
    fmu.instantiate()
    fmu.setupExperiment(startTime=start_time)
    fmu.enterInitializationMode()
    fmu.exitInitializationMode()

    time = start_time

    rows = []  # list to record the results

    # simulation loop
    while time < stop_time: