2

使用 FMPy 之类的 Python 库,我可以模拟fmpy.simulate_fmu给定的 fmus(使用 ) start_timestop_time. 在这种情况下,函数simulate_fmu 完成模拟并返回时间序列结果。

但是,我想在 fmu 和 Python 函数之间创建一个闭环(即在 Python 脚本中初始化 fmu,每 0.1 秒后从 fmu 获取结果,并在此基础上将输入值更新为下一个时间步的 fmu) . 有没有办法使用现有的库(如 fmpy 或 pyfmi)来实现这一点?

4

1 回答 1

6

The short answer is yes the tools you mention are set up to do what you are asking.

A long answer can be found for FMPy:

But the gist is to perform changes during simulation like you are asking the approach you want you need to go a layer deeper than simulate_fmu and use doStep and associated setup. The functions/approach needed for these are defined by the FMI standard while highler level implmentations like simulate_fmu are not and are therefore tool dependent implementations of the standard.

The cliff notes are:

  • Prep the Simulation Inputs
    • e.g., unzip the FMU, define simulation setup, and instantiate the FMU
from fmpy import read_model_description, extract
from fmpy.fmi2 import FMU2Slave

# 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()
  • Simulation Loop
    • Set/Get values (can do this before and/or after timestep): fmu.setReal and fmu.getReal
    • Solve the time step (fmu.doStep)
    • Store values
    • Loop until complete
  • Terminate Simulation: fmu.terminate() and fmu.freeInstance()
  • Plot results
  • Celebrate!
于 2021-02-04T21:43:33.330 回答