本文介绍了在仿真期间暂停 JModelica 并传递增量输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好 Modelica 社区,

Hi Modelica Community,

我想在 JModelica 中并行运行两个模型,但我不确定如何在模型之间传递变量.一种是python模型,另一种是EnergyPlusToFMU模型.

I would like to run two models in parallel in JModelica but I'm not sure how to pass variables between the models. One model is a python model and the other is an EnergyPlusToFMU model.

JModelica 文档中的示例具有在模型模拟之前定义的完整模拟周期输入.我不明白如何配置一个暂停输入的模型,这是 FMU 和协同仿真的一个关键特性.

The examples in the JModelica documentation has the full simulation period inputs defined prior to the simulation of the model. I don't understand how one would configure a model that pauses for inputs, which is a key feature of FMUs and co-simulation.

有人能给我提供一个示例或一段代码来展示如何在 JModelica 中实现这一点吗?

Can someone provide me with an example or piece of code that shows how this could be implemented in JModelica?

我是否将模拟命令置于循环中?如果是这样,我如何处理预热期和初始化,而不会丢失先前时间步的数据?

Do I put the simulate command in a loop? If so, how do I handle warm up periods and initialization without losing data at prior timesteps?

感谢您的时间,

贾斯汀

推荐答案

迟到的回答,但万一被其他人捡到...

Late answer, but in case it is picked up by others...

您确实可以将模拟放入循环中,您只需要跟踪系统的状态,以便您可以在每次迭代时重新初始化它.考虑以下示例:

You can indeed put the simulation into a loop, you just need to keep track of the state of your system, such that you can re-init it at every iteration. Consider the following example:

Ts = 100
x_k = x_0

for k in range(100):
    # Do whatever you need to get your input here
    u_k = ...

    FMU.reset()
    FMU.set(x_k.keys(), x_k.values())

    sim_res = FMU.simulate(
        start_time=k*Ts,
        final_time=(k+1)*Ts,
        input=u_k
    )

    x_k = get_state(sim_res)

现在,我编写了一个小函数来获取系统的状态,x_k:

Now, I have written a small function to grab the state, x_k, of the system:

# Get state names and their values at given index
def get_state(fmu, results, index):
    # Identify states as variables with a _start_ value
    identifier = "_start_"
    keys = fmu.get_model_variables(filter=identifier + "*").keys()
    # Now, loop through all states, get their value and put it in x
    x = {}
    for name in keys:
        x[name] = results[name[len(identifier):]][index]
    # Return state
    return x

这依赖于设置 "state_initial_equations": True 编译选项.

This relies on setting "state_initial_equations": True compile option.

这篇关于在仿真期间暂停 JModelica 并传递增量输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:06