Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question
1

How to use python libraries in the Buildings.Utilities.IO.Python_3_8 Real_Real module

asked 2023-05-25 22:16:41 -0600

grid_designer's avatar

updated 2023-05-26 07:27:31 -0600

Hello, I have successfully used my own python function for peak-shaving for my microgrid in openmodelica. I am trying to port a load forecasting machine learning algorithm to the modelica simulation, but the Real_Real module does not seem to run any python code that has an external library. How are external libraries such pandas, numpy, scikit-learn, etc exported to the PYTHONPATH, and read by the Real_Real module? I have a very simple example that I am trying to implement just to test using an external library, but I cannot get it to work. Thank you for the help.

Modelica and python code:

https://drive.google.com/drive/folder...

Modelica model:

model test_python_library
  Buildings.Utilities.IO.Python_3_8.Real_Real random_output(functionName = "random_output", moduleName = "random_output", nDblRea = 1, nDblWri = 1, samplePeriod = 1)  annotation(

 Placement(visible = true, transformation(origin = {-2, 4}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Sources.Constant const(k = 1)  annotation(
    Placement(visible = true, transformation(origin = {-72, 4}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
  connect(const.y, random_output.uR[1]) annotation(
    Line(points = {{-61, 4}, {-14, 4}}, color = {0, 0, 127}));

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

Python code (Note if I comment out line 2 and 4, and uncomment line 3, the model runs perfectly, so I know the python library is causing the error):

def random_output(input):
    import numpy as np
    #return 5
    return np.random.randint(100)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-26 20:23:10 -0600

If your numpy package is on the PYTHONPATH it should work, but make sure you restart dymola from the same shell in which you export the PYTHONPATH.

To test, you can do the following:

cd Buildings
mkdir myPython
mv Resources/Python-Sources/testFunctions.py myPython/  
export PYTHONPATH=${PYTHONPATH}:`pwd`/myPython
dymola package.mo &

Then run the python examples in the Buildings library which will work as the module is searched on the PYTHONPATH. If you don't specify the export PYTHONPATH line, it won't work as the module can't be found.

edit flag offensive delete link more

Comments

Does this only work for Dymola or has it only been tested on Dymola. I am using OpenModelica,

grid_designer's avatar grid_designer  ( 2023-05-26 21:00:26 -0600 )edit

Did you test it with OpenModelica? It works on my system with OpenModelica and Dymola.

Michael Wetter's avatar Michael Wetter  ( 2023-05-27 07:29:23 -0600 )edit
1

It works now thank you. I was exporting the PYTHONPATH correctly, but my problem was that while I had libraries installed for python 3.9, I did not have any libraries installed for python 3.8. A note for anyone who has a similar problem, on your terminal write python3,8 then run import numpy to verify python3.8 has the libraries installed.

grid_designer's avatar grid_designer  ( 2023-05-27 16:55:53 -0600 )edit

excuse me ,Why can numpy call, pandas and sklearn error, is the pip version wrong

asd's avatar asd  ( 2024-05-30 03:07:59 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Training Workshops

Careers

Question Tools

1 follower

Stats

Asked: 2023-05-25 22:16:41 -0600

Seen: 172 times

Last updated: May 27 '23