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

Revision history [back]

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

Hello, I am have successfully used my own python function for peak-shaving for my microgrid. 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/folders/1YChj8GzLeyY9jp20MFmX_E3KPB5GzxEl?usp=sharing

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)

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

Hello, I am have successfully used my own python function for peak-shaving for my microgrid. 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/folders/1YChj8GzLeyY9jp20MFmX_E3KPB5GzxEl?usp=sharing

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)

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

Hello, I have successfully used my own python function for peak-shaving for my microgrid. 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/folders/1YChj8GzLeyY9jp20MFmX_E3KPB5GzxEl?usp=sharing

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)

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

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/folders/1YChj8GzLeyY9jp20MFmX_E3KPB5GzxEl?usp=sharing

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)