首页 文章

FMU变量值与输入不匹配

提问于
浏览
1

我正在尝试配置的简单协同仿真中遇到一些奇怪的行为 . 我在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()

导致结果的情节"FMUTime"与python "t":
FMU Time does not match simulation time

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

最好,

贾斯汀

1 回答

  • 0

    根据@ ChristianAndersson关于更新我的JModelica安装的建议,解决了上述问题中描述的问题 .

    JModelica 1.17.0于2015年12月发布 .

    JModelica-SDK-1.12.0于2016年2月发布,它是根据源代码构建的,它修复了问题并为我提供了预期的结果 .

相关问题