rstudio / reticulate

R Interface to Python
https://rstudio.github.io/reticulate
Apache License 2.0
1.67k stars 327 forks source link

session cannot be stopped in Rstudio while using pyfmi do co-simulation #381

Open jywang2016 opened 5 years ago

jywang2016 commented 5 years ago

Thanks for the fantastic package!

I wanna use the pyFMI python module which is aimed at loading and interacting with Functional Mock-Up Units(FMUs) for co-simulation between R(python) and Energyplus(A building simulation program). But the Rstudio session cannot be stopped while running the following codes:

rm(list = ls())
library(reticulate)

use_python('F:/ProgramData/Anaconda3')
pyfmi <- import('pyfmi')

model <- pyfmi$load_fmu('path to FMU file.fmu')

t_start <- 60 * 60 * 24 * 180
t_end <- 60 * 60 * 24 * 200
t_step_size <- 15 * 60

model$instantiate_slave()
model$initialize(t_start, t_end, TRUE)

image

The final line model$initialize(t_start,t_end,TRUE)will call the CMD( in windows OS) to initialize the FMU(also call EnergyPlus software). The CMD windows will not be closed and wait the further code to control the Energyplus simulation.Therefore, I guess Rstudio cannot judge the CMD status rightly, thus causing the final line never stop.

The codes work successfully in R but not Rstudio. Meanwhile, I use the Jobs background in the preview release of Rstudio to run this code, it also works.

In R, it works:

r

It also works in Jobs of Rstudio preview release rjobs 2

It fails in Rstudio and causes crash. rstudio 2

I guess there are problems between the interacting of Rstudio console and CMD, thus causing Rstudio cannot judge the CMD status rightly and think the initialize codes is running abidingly.

Is there any way to run the code in Rstudio successfully? Any suggestions are pleasure!

Thanks! JY

jywang2016 commented 5 years ago

BTW, the OS is Win10 64bits. The configuration of R and Python:

sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936  LC_CTYPE=Chinese (Simplified)_China.936   
[3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C                              
[5] LC_TIME=Chinese (Simplified)_China.936    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.5.1  tools_3.5.1     packrat_0.4.9-3
py_discover_config()
python:         F:/ProgramData/Anaconda3/python.exe
libpython:      F:/ProgramData/Anaconda3/python36.dll
pythonhome:     F:\ProgramData\Anaconda3
version:        3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          F:\ProgramData\Anaconda3\lib\site-packages\numpy
numpy_version:  1.14.3

python versions found: 
 F:/ProgramData/Anaconda3/python.exe
 F:\ProgramData\Anaconda3\python.exe
kevinushey commented 5 years ago

Any chance you can share a sample .fmu file so we might be able to try and reproduce this locally?

Is there by any chance a more minimal example that we might be able to try running to reproduce this issue (e.g. some standalone Python code that itself tries to open and close a command window)?

jywang2016 commented 5 years ago

Of course. The attachments are the .fmu file and corresponding R(just like the gif shows) and Python code. But, to reproduce the example locally, you need install the EnergyPlus V8-5-0 and add it to environment path(in Windows OS) due to the co-simulation need call EnergyPlus.

Thanks for your kind advice. test_pyFMI.zip