Steps to run a Jmodelica docker that supports supports Jmodelica, python2, pyfmi, miniconda3 interaction on a Linux Server
- Docker is installed
- Docker sudo group is created
- User is added to docker sudo group
- Jmodelica source code is available as a zip file
- Xming is installed on local computer if performing this set up on remote server
- DISPLAY environmental variable is set (for eg "localhost:10.0")
The docker image is hosted on docker hub. If you don't want to build from source run
docker pull aviseknaug/jmodelica:2.0
Do not clone this folder! This repo merely exists to have all the isntructions and is by no means complete. Note that pymodelica and pyjmi is supported only for python 2 installed with JModelica
mkdir $HOME/JModelica_docker
cd $HOME/JModelica_docker
wget https://github.com/AvisekNaug/JModelica_docker/raw/master/Dockerfile
wget https://github.com/AvisekNaug/JModelica_docker/raw/master/environment.yml
mv <path to Jmodelica installation zip file> $HOME/JModelica_docker/jmodelica.zip
or download JModelica installation source zip from my googledrive using
source jmodelica_downloader.sh
docker build --tag jmodelica:2.0 .
Start the docker for generic use(for use with buildings library see next step)
Ensure X11 forwarding works correctly(Do this only if working on a remote server). Make sure Xming is installed on local computer and listeing on 10.0
source x11config.sh
docker run -it -e DISPLAY=${DISPLAY} -v $XAUTH:$XAUTH -e XAUTHORITY=$XAUTH jmodelica:1.0
docker run -it -e DISPLAY=${DISPLAY} jmodelica:2.0
source x11config.sh
docker run -it -e DISPLAY=${DISPLAY} -v $XAUTH:$XAUTH -e XAUTHORITY=$XAUTH -v $path/to/modelica-buildings_library:path/to/mount/modelica-buildings_library jmodelica:2.0
for example for my host system with username nauga, having the buildings library inside buildings_library_dev
docker run -it -e DISPLAY=${DISPLAY} -v $XAUTH:$XAUTH -e XAUTHORITY=$XAUTH -v home/nauga/buildings_library_dev:/home/developer/buildings_library_dev:ro jmodelica:2.0
After starting a container, add location of the buildings library to MODELICAPATH
export MODELICAPATH=path/to/mount/modelica-buildings_library:$MODELICAPATH
for example
export MODELICAPATH=home/developer/buildings_library_dev:$MODELICAPATH
- eg -v $HOME/nauga/buildings_library_dev:/home/developer/buildings_library_dev:ro" for read only. remove "ro" if you want to modify the folder components from inside the docker
docker run -it -e DISPLAY=${DISPLAY} -v $path/to/modelica-buildings_library:path/to/mount/modelica-buildings_library jmodelica:2.0
After starting a container, add location of the buildings library to MODELICAPATH
export MODELICAPATH=path/to/mount/modelica-buildings_library:$MODELICAPATH
ipython
from pymodelica import compile_fmu
model="Buildings.Controls.OBC.CDL.Continuous.Validation.LimPID" #taken form https://github.com/lbl-srg/docker-ubuntu-jmodelica/blob/master/jmodelica.py
fmu_name = compile_fmu(model) # writes the fmu to fmu_name as well as to local folder
Now, you can simulate fmu either in pyfmi for python2 or pyfmi for python3
ipython
from pyfmi import load_fmu
mod = load_fmu(Buildings_Controls_OBC_CDL_Continuous_Validation_LimPID.fmu)
res = mod.simulate()
or
conda activate modelicagym
ipython
from pyfmi import load_fmu
mod = load_fmu(Buildings_Controls_OBC_CDL_Continuous_Validation_LimPID.fmu)
res = mod.simulate()
dveloper@container_id# ipython
from pyfmi.examples import fmi_bouncing_ball
fmi_bouncing_ball.run_demo()
This should display the images of position and velocity in a matplotlib plot.
Similarly try,
from pyjmi.examples import cstr_casadi
cstr_casadi.run_demo()
The following example only runs outside modelicagym conda env as pyjmi is not availablr inside modelicagym env
from pyjmi.examples import RLC
RLC.run_demo()
import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt # see link https://github.com/matplotlib/matplotlib/issues/8929#issuecomment-317233404
In case you want to render standard OpenAI gym environments, install the following inside the container
sudo apt-get update
sudo apt update
sudo apt-get install -y git
sudo apt install -y freeglut3 freeglut3-dev