FMU变量值与Inpu不匹配

2024-04-28 11:09:44 发布

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

我在一个简单的协同模拟中得到了一些奇怪的行为。我在EnergyPlus中建立了一个建筑能量模型来测试JModelica生成的FMU。然而,在联合模拟阶段,建筑能量模型会被挂起。然后我在JModelica运行了FMU,得到了一些非常奇怪的结果。在

Modelica代码是:

model CallAdd
    input Real FirstInput(start=0);
    input Real SecondInput(start=0);
    output Real FMUOutput(start=0); 
    function CAdd
        input Real x(start=0);
        input Real y(start=0);
        output Real z(start=0);
        external "C"  annotation(Library = "CAdd", LibraryDirectory = "modelica://CallAdd");
    end CAdd;
equation
    FMUOutput = CAdd(FirstInput,SecondInput);
    annotation(uses(Modelica(version = "3.2.1")));
end CallAdd;

上面的代码引用了“CAdd”,这是c代码“CAdd.c”的库文件:

^{pr2}$

在CMD中使用以下两个命令将其编译为库文件:

gcc -c CAdd.c -o CAdd.o
ar rcs libCAdd.a CAdd.o

我可以用一个包装器在OpenModelica中运行上面的例子,它工作得很好。在

然后,我使用JModelica将上面的代码编译为一个用于协同仿真的FMU。JModelica编译代码是:

# Import the compiler function
from pymodelica import compile_fmu

# Specify Modelica model and model file (.mo or .mop)
model_name = "CallAdd"
mo_file = "CallAdd.mo"

# Compile the model and save the return argument, for use later if wanted
my_fmu = compile_fmu(model_name, mo_file, target="cs")

然后,我模拟了FMU,得到了JModelica Python代码的奇怪结果:

from pyfmi import load_fmu
import numpy as np
import matplotlib.pyplot as plt

modelName = 'CallAdd'
numSteps = 100
timeStop = 20

# Load FMU created with the last script
myModel = load_fmu(modelName+'.fmu')

# Load options
opts = myModel.simulate_options()

# Set number of timesteps
opts['ncp'] = numSteps

# Set up input, needs more than one value to interpolate the input over time. 
t = np.linspace(0.0,timeStop,numSteps)
u1 = np.sin(t)
u2 = np.empty(len(t)); u2.fill(5.0)
u_traj = np.transpose(np.vstack((t,u1,u2)))
input_object = (['FirstInput','SecondInput'],u_traj)

# Internalize results
res = myModel.simulate(final_time=timeStop, input = input_object, options=opts)
# print 'res: ', res

# Internalize individual results
FMUTime = res['time']
FMUIn1 = res['FirstInput']
FMUIn2 = res['SecondInput']
FMUOut = res['FMUOutput']

plt.figure(2)
FMUIn1Plot = plt.plot(t,FMUTime[1:],label='FMUTime')
# FMUIn1Plot = plt.plot(t,FMUIn1[1:],label='FMUIn1')
# FMUIn2Plot = plt.plot(t,FMUIn2[1:],label='FMUIn2')
# FMUOutPlot = plt.plot(t,FMUOut[1:],label='FMUOut')
plt.grid(True)
plt.legend()
plt.ylabel('FMU time [s]')
plt.xlabel('time [s]')
plt.show()

这导致了结果“FMUTime”与python“t”的绘图: FMU Time does not match simulation time

除了看到这种奇怪的行为,FMU结果中的输入“FirstInput”和“SecondInput”与python代码中指定的u1和u2不匹配。我希望有人能帮助我更好地了解发生了什么。在

最好的

贾斯汀


Tags: 代码inputmodelnprespltrealstart
1条回答
网友
1楼 · 发布于 2024-04-28 11:09:44

根据@christiananderson的建议更新JModelica安装,我上面问题中描述的问题得到了解决。在

jModelica1.17.0于2015年12月发布。在

JModelica-SDK-1.12.0于2016年2月发布,基于源代码构建,解决了这个问题,并为我提供了预期的结果。在

相关问题 更多 >