我在尝试配置的简单联合仿真中遇到了一些奇怪的行为。我在 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”的库文件:
double CAdd(double x, double y){
double answer;
answer = x + y;
return answer;
}
在 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()
除了看到这种奇怪的行为之外,FMU 结果中的输入“FirstInput”和“SecondInput”与 python 代码中指定的 u1 和 u2 不匹配。我希望有人可以帮助我更好地了解发生了什么。
最好的,
贾斯汀