Commit b9eed96c authored by hazrmard's avatar hazrmard
Browse files

sample notebook to load FMU into python (simulate gives errors TODO)

parent 01c66f60
Loading
Loading
Loading
Loading
+2 −0
Changes for .gitignore: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
*.o
*.libs
*.log
*_log.txt
*.makefile
*.c
*.h
@@ -11,3 +12,4 @@
*_info.json
.ipynb_checkpoints/
dygraph-combined.js
*.fmu
 No newline at end of file
+7 −1
Changes for Modelica in Jupyter.ipynb: 7 added lines, 1 removed line.
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

# Modelica x Jupyter

These cells contain modelica code that can be run via Jupyter.

To install:

* Install OpenModelica
* Install Anaconda
* Create environment in `env.yml`

```
    conda env create -f env.yml
```

## Architecture

```
Modelica ->(compiler)-> Functional Mockup Unit ->(pyFMI)->(gym wrapper) -> RL
```

%% Cell type:code id: tags:

``` OpenModelica
class Pendulum "Pendulum"
    Real F=0;                 // External force tengential to motion
    constant Real PI=3.141592653589793;
    parameter Real m=1, g=9.81, L=0.5;
    Real F;                 // External force tengential to motion
    output Real x(start=0.5),y(start=0);
    output Real vx,vy;      // x and y velocities
equation
    // 5 variables, 5 equations
    m*der(vx)=-(x/L)*F;     // Force equation in x direction
    m*der(vy)=-(y/L)*F-m*g; // Force equation in y direction
    der(x)=vx;              // Relating x position to x velocity
    der(y)=vy;              // Relating y position to y velocity
    x^2+y^2=L^2;            // Constraining x, y to string length
end Pendulum;
```

%% Cell type:code id: tags:

``` OpenModelica
translateModelFMU(Pendulum)
```

%% Cell type:code id: tags:

``` OpenModelica
// generates C code and complies an exe
// that produces a result file
buildModel(Pendulum fileNamePrefix="pendulum-")
```

%% Cell type:code id: tags:

``` OpenModelica
// builds model & runs it
simulate(Pendulum, fileNamePrefix="pendulum-")
```

%% Cell type:code id: tags:

``` OpenModelica
plot(der(x),der(y),m)
```

%% Cell type:code id: tags:

``` OpenModelica
// remove artifacts
remove("pendulum-*")
```

%% Cell type:code id: tags:

``` OpenModelica
class Spring "Damped Spring"
    // mass, gravity, spring constant, air, contact resistance
    parameter Real m=0.5, g=9.81, k=3, r_a=0.1, r_c=0.0;
    output Real x(start=0.0);  // x position
    output Real vx(start=0.2); // x velocity
equation
    // 2 variables, 2 equations
    m*der(vx)= -k*x - r_a*vx - r_c*m*g; // force equation
    der(x)=vx;                          // relating position to velocity
end Spring;
```

%% Cell type:code id: tags:

``` OpenModelica
simulate(Spring, fileNamePrefix="spring-*")
```

%% Cell type:code id: tags:

``` OpenModelica
plot(der(x), x)
```

%% Cell type:code id: tags:

``` OpenModelica
// remove artifacts
remove("spring-*")
```

Python FMUs.ipynb

0 → 100644
+22 −0
Changes for Python FMUs.ipynb: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
from pyfmi import load_fmu
```

%% Cell type:code id: tags:

``` python
model = load_fmu('Pendulum.fmu')
```

%% Cell type:code id: tags:

``` python
res = model.simulate(final_time=5, input=('F', lambda t: 1.))
```

%% Cell type:code id: tags:

``` python
```
+1 −0
Changes for clearfiles.ps1: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -6,5 +6,6 @@ rm *.c
rm *.h
rm *.xml
rm *.exe
rm *_log.txt
rm *_info.json
rm dygraph-combined.js
 No newline at end of file
+1 −0
Changes for clearfiles.sh: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -8,5 +8,6 @@ rm *.log
rm *.xml
rm *.libs
rm *.makefile
rm *_log.txt
find . -type f  ! -name "*.*"  -delete
clear