如何让 pythonpath 适用于运行 Ubuntu 20 的 openmodelica Building 中的 python 模块

如何让 pythonpath 适用于运行 Ubuntu 20 的 openmodelica Building 中的 python 模块

我在 Ubuntu 20 上的 openmodelica 中运行我的模型时遇到了问题。我尝试按照文档导出路径并安装libpython3.8-dev

https://build.openmodelica.org/Documentation/Buildings.Utilities.IO.Python_3_8.UsersGuide.html

我运行了模型,但出现了这个错误,但我不确定是我的 Python 代码出了问题,还是 Python 路径出了问题

Simulation process failed. Exited with code 255.
/tmp/OpenModelica_sigi-laptop/OMEdit/peakShavingPythonModule/peakShavingPythonModule -port=36007 -logFormat=xmltcp -override=startTime=0,stopTime=10,stepSize=1,tolerance=1e-6,solver=dassl,outputFormat=mat,variableFilter=.* -r=/tmp/OpenModelica_sigi-laptop/OMEdit/peakShavingPythonModule/peakShavingPythonModule_res.mat -w -lv=LOG_STATS -inputPath=/tmp/OpenModelica_sigi-laptop/OMEdit/peakShavingPythonModule -outputPath=/tmp/OpenModelica_sigi-laptop/OMEdit/peakShavingPythonModule
The initialization finished successfully without homotopy method.
Failed to load "/home/sigi-laptop/.openmodelica/libraries/Buildings 9.1.0/Resources/Python-Sources/peak_shaving_no_soc.py". This may occur if you did not set the PYTHONPATH environment variable or if the Python module contains a syntax error. The error message is "(null)"

我的 modelica 代码

model peakShavingPythonModule
  Buildings.Utilities.IO.Python_3_8.Real_Real peak_shaving_test(functionName = "peak_shaving", moduleName = "/home/sigi-laptop/.openmodelica/libraries/Buildings 9.1.0/Resources/Python-Sources/peak_shaving_no_soc.py", nDblRea = 1, nDblWri = 1, samplePeriod = 1)  annotation(
    Placement(visible = true, transformation(origin = {-14, 30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Sources.CombiTimeTable combiTimeTable(table = [0, 0; 1, 100; 2, -100; 3, 150; 4, -150; 5, 75; 6, 30; 7, 10; 8, -5; 9, -32; 10, 42])  annotation(
    Placement(visible = true, transformation(origin = {-64, 30}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
  connect(combiTimeTable.y, peak_shaving_test.uR) annotation(
    Line(points = {{-52, 30}, {-26, 30}}, color = {0, 0, 127}, thickness = 0.5));

annotation(
    uses(Buildings(version = "9.1.0"), Modelica(version = "4.0.0")),
  experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 1),
  __OpenModelica_commandLineOptions = "--matchingAlgorithm=PFPlusExt --indexReductionMethod=dynamicStateSelection -d=initialization,NLSanalyticJacobian",
  __OpenModelica_simulationFlags(lv = "LOG_STATS", s = "dassl", variableFilter = ".*"));
end peakShavingPythonModule;

我的 python 代码(保存到 /.openmodelica/libraries/Buildings 9.1.0/Resources/Python-Sources):

def peak_shaving(net_load):
    if (net_load <= -15) and (net_load >= -100):
        ivt_ctrl = abs(net_load + 15)
    elif (net_load <= -100):
        ivt_ctrl = 100
    elif (net_load >= 0) and (net_load < 100):
        ivt_ctrl = -1*(net_load);
    elif (net_load < 100):
        ivt_ctrl = -100
    else:
        ivt_ctrl = 0
    return int(ivt_ctrl)

感谢您的帮助

答案1

我刚刚发现我忘了在 if 语句中添加一个额外的括号,所以它现在可以工作了(这很奇怪,因为我之前在 jupyter 上测试过它):

def peak_shaving(dblInp):
    [net_load, SOC] = dblInp
    if ((net_load <= -15) and (SOC > 20) and (net_load >= -100)):
        ivt_ctrl = abs(net_load + 15)
    elif ((net_load <= -100) and (SOC > 20)):
        ivt_ctrl = 100
    elif ((net_load >= 0) and (SOC < 90) and (net_load < 100)):
        ivt_ctrl = -1*(net_load);
    elif ((SOC < 90) and (net_load < 100)):
        ivt_ctrl = -100
    else:
        ivt_ctrl = 0
    return int(ivt_ctrl)

相关内容