lbl-srg / modelica-buildings

Modelica Buildings library
254 stars 158 forks source link

setting the PYTHONPATH variable fails on some Linux installations #1421

Closed mwetter closed 5 years ago

mwetter commented 5 years ago

For Utilities.IO.Python27, setting the PYTHONPATH fails in some linux installations.

mwetter commented 5 years ago

The development branch is issue1421_python

mwetter commented 5 years ago

@AntoineGautier : Could you please checkout branch issue1421_python on a 32 bit Ubuntu and run

cd Buildings/Resources/src/python
make -f Makefile.linux

This should produce a new file ./Resources/Library/linux32/libModelicaBuildingsPython2.7.so

Can you please also run Makefile.windows.bat to produce the files in win32 and win64.

mwetter commented 5 years ago

@AntoineGautier : It turns out that the travis tests fail. Let's wait with compiling these binaries until the tests on travis are successful.

mwetter commented 5 years ago

Commit https://github.com/lbl-srg/modelica-buildings/commit/857ddb4814f172c84ab6a638aecca78fb16648c1 fails. It appears that the constructor is only executed once (as earlier tested by printing its address). The error message is

$ jm_ipython.sh jmodelica.py Buildings.Utilities.IO.Python27.Functions.Examples.Exchange
...
ModelicaError: <msg:Call to Python function "r2_r1" failed.
 This is often due to an error in the Python script,
 or because the list of arguments of the Python function is incorrect.
 Check the module "testFunctions".
 The error message is "can't multiply sequence by non-int of type 'float'".>
Error in flattened model:
  Assertion failed: Error in function r2_r1, obtained 0.00000

Applying the change

$ git diff Buildings/Utilities/IO/Python27/Functions/Examples/Exchange.mo
diff --git a/Buildings/Utilities/IO/Python27/Functions/Examples/Exchange.mo b/Buildings/Utilities/IO/Python27/Functions/Examples/Exchange.mo
index cb08f03b5..366e83fad 100644
--- a/Buildings/Utilities/IO/Python27/Functions/Examples/Exchange.mo
+++ b/Buildings/Utilities/IO/Python27/Functions/Examples/Exchange.mo
@@ -33,7 +33,7 @@ equation
     yR1_2 = Buildings.Utilities.IO.Python27.Functions.exchange(
       moduleName="testFunctions",
       functionName="r2_r1",
-      pytObj=pytObj[2],
+      pytObj=pytObj2,
       passPythonObject=passPythonObject,
       dblWri={2.0, 3.0},
       intWri={0},

This fix has been applied in https://github.com/lbl-srg/modelica-buildings/commit/4be974f806b96c0128a057e6b2c12f77057efe7e

mwetter commented 5 years ago

With 5361c560b6f7014e327ff0c595a1b49d3714dc01,

export PYTHONPATH=`pwd`/Buildings/Resources/Python-Sources
jm_ipython.sh jmodelica.py Buildings.Utilities.IO.Python27.Examples.SimpleRoom

fails with FMUException: Failed to update the events at time: 6.108000E+04. The other 3 models with Python work from the console, but not from BuildingsPy if run as

../bin/runUnitTests.py -t jmodelica -s Buildings.Utilities.IO.Python27
AntoineGautier commented 5 years ago

Same behavior on my system (I have not tested with BuildingsPy, only from terminal). For the 3 models that simulate I also get a weird exception though it does not seem to make the simulation fail.

(python2) agautier@agautier-laptop:~/github/modelica-buildings$ Buildings/Resources/Scripts/travis/bin/jm_ipython.sh jmodelica.py Buildings.Utilities.IO.Python27.Functions.Examples
*** Compiling Buildings.Utilities.IO.Python27.Functions.Examples

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/local/JModelica/Python/pymodelica/compiler_logging.py", line 218, in run
    xml.sax.parse(self.stream, LogErrorParser(self.result))
  File "/usr/lib/python2.7/xml/sax/__init__.py", line 33, in parse
    parser.parse(source)
  File "/usr/lib/python2.7/xml/sax/expatreader.py", line 116, in parse
    self._close_source()
  File "/usr/lib/python2.7/xml/sax/expatreader.py", line 235, in _close_source
    file.close()
AttributeError: KeepLastStream instance has no attribute 'close'

Final Run Statistics: --- 

 Number of steps                                 : 3
 Number of function evaluations                  : 7
 Number of Jacobian evaluations                  : 1
 Number of function eval. due to Jacobian eval.  : 1
 Number of error test failures                   : 0
 Number of nonlinear iterations                  : 3
 Number of nonlinear convergence failures        : 0

Solver options:

 Solver                   : CVode
 Linear multistep method  : BDF
 Nonlinear solver         : Newton
 Linear solver type       : DENSE
 Maximal order            : 5
 Tolerances (absolute)    : 1e-06
 Tolerances (relative)    : 1e-06

Simulation interval    : 0.0 - 1.0 seconds.
Elapsed simulation time: 0.231622934341 seconds.
mwetter commented 5 years ago

@AntoineGautier : The exception is due to a problem in the Python implementation of JModelica I believe.

The commit https://github.com/lbl-srg/modelica-buildings/commit/2e8cb869aa0a747a24952166fd509c1bdc2a2f33 fixes the problem for SimpleRoom. See the comment in the committed Python code.

Printing time in every invocation of the Python code leads to something like

Time is 0.0
Time is 86220.0
Time is 86220.0
Time is 0.0
Time is 86280.0
Time is 86280.0
Time is 0.0
Time is 86340.0
Time is 86340.0
Time is 0.0
Time is 86400.0