PyFMI模型交换和协同模拟的结果不同?

2024-05-16 18:41:30 发布

您现在位置:Python中文网/ 问答频道 /正文

在PyFMI中将cartpole问题模拟为FMU时,根据使用“模型交换”或“联合模拟”,我会得到不同的结果,并给出相同的输入。ME结果是正确的,CS结果似乎完全不正确

模型交换结果

model exchange results

model = load_fmu(fmu='CartPole.fmu', 
                 kind='ME')

model.set('theta_0', 0)
model.set('m_cart', 20)
model.set('m_pole', 5)
model.set('length', 2)
model.set('poleCartConnection.density', 0)
model.set('f', 0)
res = model.simulate(start_time=0, final_time=10)


plt.plot(res['f'])
plt.plot(res['x'])
plt.plot(res['x_dot'])
plt.plot(res['theta'])
plt.plot(res['theta_dot'])
plt.legend(['f', 'x', 'x_dot', 'theta', 'theta_dot'])
plt.show() 

与相比(即完全相同,但使用CS而不是我)

model = load_fmu(fmu='CartPole.fmu', 
                 kind='CS')

model.set('theta_0', 0)
model.set('m_cart', 20)
model.set('m_pole', 5)
model.set('length', 2)
model.set('poleCartConnection.density', 0)
model.set('f', 0)
res = model.simulate(start_time=0, final_time=10)


plt.plot(res['f'])
plt.plot(res['x'])
plt.plot(res['x_dot'])
plt.plot(res['theta'])
plt.plot(res['theta_dot'])
plt.legend(['f', 'x', 'x_dot', 'theta', 'theta_dot'])
plt.show()

具有默认ncp的联合仿真结果

co-simulation results with default ncp

我怀疑这是由于解算器设置,但在CS情况下无法设置这些设置?当我将“ncp”设置为一个非常高的数字时,错误会减少。 非常感谢您的回复

具有高ncp的联合仿真结果

co-simulation with high ncp

干杯


Tags: 模型modeltimeplotloadrespltcs