且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在仿真期间暂停 JModelica 并传递增量输入

更新时间:2023-01-23 13:54:56

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

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.