迟到的答案,但万一它被别人捡起......
您确实可以将模拟放入一个循环中,您只需要跟踪系统的状态,这样您就可以在每次迭代时重新初始化它。考虑以下示例:
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
系统的状态 , :
# 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
编译选项。