我正在嘗試配置一個簡單的聯合仿真中出現一些奇怪的行爲。我在EnergyPlus中建立了一個建築能量模型來測試JModelica生成的FMU。然而,建築能源模型會在聯合仿真階段掛起。然後我在JModelica運行FMU,得到了一些非常奇怪的結果。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中運行上面的示例與wrapp呃,它的效果很好。
然後,我使用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不匹配。我希望有人能幫助我更好地理解正在發生的事情。
最佳,
賈斯汀
任何你爲了增加兩個'雙打'而使用C模塊不必要地增加複雜度的原因? – Olaf
@Olaf是的,這是一個更大模型的簡單配置,我們使用c代碼來調用python模型。 –
對不起,但這聽起來很奇怪。但是由於我並不真正瞭解modelica以及它如何與Python集成,所以我將它留在這裏。 – Olaf