Commit 0ce3856c authored by Ibrahim Ahmed's avatar Ibrahim Ahmed
Browse files

Update Modelica in Jupyter.ipynb

parent b9eed96c
Loading
Loading
Loading
Loading
+1 −1
Changes for Modelica in Jupyter.ipynb: 1 added line, 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
    Real F;                 // Total force along pendulum string
    constant Real PI=3.141592653589793;
    parameter Real m=1, g=9.81, L=0.5;
    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-*")
```