@@ -8,18 +8,19 @@ For this tutorial we will need [OrdinaryDiffEq.jl](https://docs.sciml.ai/Ordinar
88using FMIExchange
99using OrdinaryDiffEq
1010using Plots
11+ import ADTypes
1112```
1213
1314To initialise the FMU, we must provide the path to the FMU, its inputs, outputs and states and the simulation start and stop times.
1415If 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
1819bbstart = 0.0 # simulation start
1920bbstop = 10.0 # simulation stop
2021bbins = String[] # FMU inputs (this FMU has none)
2122bbouts = 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
2324bbparameters = Dict("eps"=>1e-2) # FMU parameters (optional)
2425nothing # hide
2526```
@@ -36,15 +37,15 @@ nothing # hide
3637
3738Let'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]
4041p0 = Float64[]
4142tspan = (bbstart, bbstop)
4243
4344sol = 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)
4849savefig("nocb.png"); nothing # hide
4950```
5051![ ] ( nocb.png )
@@ -57,11 +58,12 @@ If we include them, the ball behaves as expected.
5758cbs = get_callbacks(model, bbstart, bbstop)
5859sol = 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)
6567savefig("cb.png"); nothing # hide
6668```
6769![ ] ( cb.png )
@@ -78,14 +80,15 @@ using FMIExchange
7880using LinearAlgebra
7981using OrdinaryDiffEq
8082using Plots
83+ import ADTypes
8184
8285bbloc = 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
8487bbstart = 0.0 # simulation start
8588bbstop = 10.0 # simulation stop
8689bbins = String[] # FMU inputs (this FMU has none)
8790bbouts = 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
8992bb_radius = 0.1
9093bbparameters = Dict("eps"=>1e-2, "r" => bb_radius) # FMU parameters (optional)
9194nothing # 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
177180u0 = zeros(length(keys(umap)))
181+ u0[umap["active_x"]] = 1.0
182+ u0[umap["active_y"]] = 1.0
178183u0[umap["x"]] = 0.5
179184u0[umap["dx"]] = 0.5
180185u0[umap["y"]] = 1.0
@@ -185,18 +190,19 @@ p0 = Float64[0.2, 0.2]
185190tspan = (bbstart, bbstop)
186191sol = 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)
196201nothing # hide
197202```
198203
199204We can plot the solution as a nice animation.
205+
200206``` @example ss
201207import Logging # hide
202208Logging.disable_logging(Logging.Info) # hide
0 commit comments