Skip to content

Commit d3303b6

Browse files
committed
Update docs
1 parent aa1e9f1 commit d3303b6

File tree

5 files changed

+42
-37
lines changed

5 files changed

+42
-37
lines changed

deps/BouncingBall2D.fmu

3.56 KB
Binary file not shown.

deps/src/BouncingBall/BouncingBall2D.mo

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,36 @@ model BouncingBall2D
88
parameter Real e = 0.9 "Coefficient of restitution";
99
parameter Modelica.Units.SI.Position x0 = 0.5;
1010
parameter Modelica.Units.SI.Position y0 = 1.0;
11-
parameter Modelica.Units.SI.Position dx0 = 1.0;
12-
parameter Modelica.Units.SI.Position dy0 = 0.0;
11+
parameter Modelica.Units.SI.Velocity dx0 = 1.0;
12+
parameter Modelica.Units.SI.Velocity dy0 = 0.0;
1313
parameter Modelica.Units.SI.Position xmax = 1.0;
1414
parameter Modelica.Units.SI.Position eps = 1e-2;
15+
parameter Real stopFrac = 0.05 "Fraction of velocity left before stopping movement";
16+
parameter Modelica.Units.SI.Acceleration g = 9.81 "Gravitational acceleration";
1517
protected
16-
Real y_done;
17-
Real x_done;
18+
Real active_y;
19+
Real active_x;
1820
initial equation
1921
x = x0;
2022
y = y0;
2123
dx = dx0;
2224
dy = dy0;
23-
y_done = 1.0;
24-
x_done = 1.0;
25+
active_y = 1.0;
26+
active_x = 1.0;
2527
equation
26-
der(x) = if (xmax - x) > r - eps and x > r - eps then dx else 0.0;
27-
der(y) = if y > r - eps then dy else 0.0;
28+
der(x) = dx * active_x;
29+
der(y) = dy * active_y;
2830
m*der(dx) = 0.0;
29-
m*der(dy) = -9.81*m*y_done;
30-
when (y < r and dy < 0 and y_done == 1.0) then
31-
reinit(dy, -pre(dy)*e);
32-
end when;
33-
when (y < r - eps) then
34-
y_done = 0.0;
35-
reinit(dy, 0.0);
31+
m*der(dy) = -g*m*active_y;
32+
der(active_y) = 0.0;
33+
der(active_x) = 0.0;
34+
when (y <= r and dy < 0 and active_y == 1.0) then
35+
reinit(dy, if abs(pre(dy) * e) < sqrt(2 * (y0-r) * g) * stopFrac then 0 else -pre(dy)*e);
36+
reinit(active_y, if abs(pre(dy) * e) < sqrt(2 * (y0-r) * g) * stopFrac then 0.0 else pre(active_y));
3637
end when;
3738
when {x < r, (xmax - x) < r} then
38-
reinit(dx, -pre(dx)*e);
39-
end when;
40-
when (abs(dx) < eps) then
41-
reinit(dx, 0.0);
42-
end when;
43-
when {x < r - eps or (xmax - x) < r - eps} then
44-
x_done = 0.0;
45-
reinit(dx, 0.0);
39+
reinit(dx, if abs(pre(dx) * e) < eps then 0.0 else -pre(dx)*e);
40+
reinit(active_x, if abs(pre(dx) * e) < eps then 0.0 else pre(active_x));
4641
end when;
4742
annotation(
4843
Icon(coordinateSystem(preserveAspectRatio = false)),

docs/Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
[deps]
2+
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
23
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
34
FMIExchange = "d0f58030-e4f1-4214-a600-bc055fa6d38e"
45
FMIImport = "9fcbc62e-52a0-44e9-a616-1359a0008194"
56
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
67
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
8+
9+
[sources]
10+
FMIExchange = {path = ".."}

docs/src/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Pkg.status(; mode = PKGMODE_MANIFEST) # hide
9595

9696
```@example
9797
using FMIImport # hide
98-
println(fmi2Load(joinpath(@__DIR__, "..", "..", "deps", "fmu", "BouncingBall2D.fmu")).modelDescription.generationTool) # hide
98+
println(loadFMU(joinpath(@__DIR__, "..", "..", "deps", "BouncingBall2D.fmu")).modelDescription.generationTool) # hide
9999
```
100100
```@raw html
101101
</details>

docs/src/tutorial.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ For this tutorial we will need [OrdinaryDiffEq.jl](https://docs.sciml.ai/Ordinar
88
using FMIExchange
99
using OrdinaryDiffEq
1010
using Plots
11+
import ADTypes
1112
```
1213

1314
To initialise the FMU, we must provide the path to the FMU, its inputs, outputs and states and the simulation start and stop times.
1415
If we wish to change the parameters of the FMU we can do this by providing a dictionary of parameter-value pairs.
1516
```@example bb
16-
bbloc = joinpath("deps", "fmu", "BouncingBall2D.fmu") # fmu file location
17-
bbloc = joinpath(@__DIR__, "..", "..", "deps", "fmu", "BouncingBall2D.fmu") # hide
17+
bbloc = joinpath("deps", "BouncingBall2D.fmu") # fmu file location
18+
bbloc = joinpath(@__DIR__, "..", "..", "deps", "BouncingBall2D.fmu") # hide
1819
bbstart = 0.0 # simulation start
1920
bbstop = 10.0 # simulation stop
2021
bbins = String[] # FMU inputs (this FMU has none)
2122
bbouts = String[] # FMU outputs (this FMU has none)
22-
bbstates = ["dx", "dy", "x", "y"] # FMU states
23+
bbstates = ["active_x", "active_y", "dx", "dy", "x", "y"] # FMU states
2324
bbparameters = Dict("eps"=>1e-2) # FMU parameters (optional)
2425
nothing # hide
2526
```
@@ -36,15 +37,15 @@ nothing # hide
3637

3738
Let's simulate the FMU with the default solver.
3839
```@example bb
39-
u0 = [1.0, 0.0, 0.5, 1.0]
40+
u0 = [1.0, 1.0, 1.0, 0.0, 0.5, 1.0]
4041
p0 = Float64[]
4142
tspan = (bbstart, bbstop)
4243
4344
sol = solve(
4445
ODEProblem(model, u0, tspan, p0),
45-
AutoTsit5(Rosenbrock23(autodiff=false)),
46+
AutoTsit5(Rosenbrock23(autodiff=ADTypes.AutoFiniteDiff())),
4647
)
47-
plot(sol, idxs=(3,4), legend=false)
48+
plot(sol, idxs=(5,6), legend=false)
4849
savefig("nocb.png"); nothing # hide
4950
```
5051
![](nocb.png)
@@ -57,11 +58,12 @@ If we include them, the ball behaves as expected.
5758
cbs = get_callbacks(model, bbstart, bbstop)
5859
sol = solve(
5960
ODEProblem(model, u0, tspan, p0),
60-
AutoTsit5(Rosenbrock23(autodiff=false)),
61+
AutoTsit5(Rosenbrock23(autodiff=ADTypes.AutoFiniteDiff())),
6162
saveat=bbstart:0.01:bbstop, # for a nicer plot
62-
callback=CallbackSet(cbs...)
63+
callback=CallbackSet(cbs...),
64+
dtmax=0.01
6365
)
64-
plot(sol, idxs=(3,4), legend=false)
66+
plot(sol, idxs=(5,6), legend=false)
6567
savefig("cb.png"); nothing # hide
6668
```
6769
![](cb.png)
@@ -78,14 +80,15 @@ using FMIExchange
7880
using LinearAlgebra
7981
using OrdinaryDiffEq
8082
using Plots
83+
import ADTypes
8184
8285
bbloc = joinpath("deps", "fmu", "BouncingBall2D.fmu") # fmu file location
83-
bbloc = joinpath(@__DIR__, "..", "..", "deps", "fmu", "BouncingBall2D.fmu") # hide
86+
bbloc = joinpath(@__DIR__, "..", "..", "deps", "BouncingBall2D.fmu") # hide
8487
bbstart = 0.0 # simulation start
8588
bbstop = 10.0 # simulation stop
8689
bbins = String[] # FMU inputs (this FMU has none)
8790
bbouts = String[] # FMU outputs (this FMU has none)
88-
bbstates = ["dx", "dy", "x", "y"] # FMU states
91+
bbstates = ["active_x", "active_y", "dx", "dy", "x", "y"] # FMU states
8992
bb_radius = 0.1
9093
bbparameters = Dict("eps"=>1e-2, "r" => bb_radius) # FMU parameters (optional)
9194
nothing # hide
@@ -175,6 +178,8 @@ This is because `models` is not an `AbstractSimModel`, but rather a `Vector{Abst
175178
`dynamics(models)` automatically generates an [OrdinaryDiffEq.jl](https://docs.sciml.ai/OrdinaryDiffEq/stable/)-compatible function that combines the ODEs of both models.
176179
```@example ss
177180
u0 = zeros(length(keys(umap)))
181+
u0[umap["active_x"]] = 1.0
182+
u0[umap["active_y"]] = 1.0
178183
u0[umap["x"]] = 0.5
179184
u0[umap["dx"]] = 0.5
180185
u0[umap["y"]] = 1.0
@@ -185,18 +190,19 @@ p0 = Float64[0.2, 0.2]
185190
tspan = (bbstart, bbstop)
186191
sol = solve(
187192
ODEProblem(dynamics(models), u0, tspan, p0),
188-
AutoTsit5(Rosenbrock23(autodiff=false)),
193+
AutoTsit5(Rosenbrock23(autodiff=ADTypes.AutoFiniteDiff())),
189194
callback=CallbackSet(
190195
reduce(vcat, get_callbacks.(models, bbstart, bbstop))...,
191196
screencb,
192197
collision_cb
193198
),
194-
dtmax=0.01 # The collision callback may give errors when using large steps
199+
dtmax=0.001 # The collision callback may give errors when using large steps
195200
)
196201
nothing # hide
197202
```
198203

199204
We can plot the solution as a nice animation.
205+
200206
```@example ss
201207
import Logging # hide
202208
Logging.disable_logging(Logging.Info) # hide

0 commit comments

Comments
 (0)