Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise code #120

Merged
merged 3 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ docs/site/

Manifest.toml

/docs/src/models/
docs/src/overview.md
docs/src/models/
docs/src/overview*
84 changes: 42 additions & 42 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,48 @@ makedocs(; sitename="ReachabilityModels",
pagesonly=true,
pages=["Home" => "index.md",
"Usage" => "usage.md",
"Overview" => "overview.md",
"Linear ODEs" => Any["Overview" => "models/linear_overview.md",
"Beam" => "models/beam.md",
"Building" => "models/building.md",
"CD Player" => "models/cdplayer.md",
"Crane" => "models/crane.md",
"Ellipse" => "models/ellipse.md",
"Five Dim System" => "models/five_dim_sys.md",
"Fom" => "models/fom.md",
"Heat" => "models/heat.md",
"Helicopter" => "models/helicopter.md",
"ISS" => "models/iss.md",
"MNA1" => "models/mna1.md",
"MNA5" => "models/mna5.md",
"Motor" => "models/motor.md",
"PDE" => "models/pde.md",
"Projectile" => "models/projectile.md"],
"Nonlinear ODEs" => Any["Overview" => "models/nonlinear_overview.md",
"Biomodel 7d" => "models/biomodel7d.md",
"Biomodel 9d" => "models/biomodel9d.md",
"Brusselator" => "models/brusselator.md",
"Bucking column" => "models/buckling_column.md",
"Coupled VanDerPol" => "models/coupled_vanderpol.md",
"Double Gyre" => "models/doublegyre.md",
"Jet Engine" => "models/jet_engine.md",
"Henon-Helies" => "models/henon_heiles.md",
"Lotka-Volterra" => "models/lotka_volterra.md",
"Lorenz" => "models/lorenz.md",
"Robot Arm" => "models/robot_arm.md",
"Roessler" => "models/roessler.md",
"Spring Pendulum" => "models/spring_pendulum.md",
"Steam Governor" => "models/steam_governor.md",
"VanDerPol" => "models/vanderpol.md"],
"Hybrid ODEs" => Any["Overview" => "models/hybrid_overview.md",
"Bouncing ball" => "models/bouncing_ball.md",
"Nonlinear Bouncing ball" => "models/bouncing_ball_nonlinear.md",
"Cardiac Cell" => "models/cardiac_cell.md",
"Clocked Thermostat" => "models/clocked_thermostat.md",
"Navigation System" => "models/navigation_system.md",
"Powertrain Control" => "models/powertrain_control.md",
"Spiking Neuron" => "models/spiking_neuron.md",
"Thermostat" => "models/thermostat.md"],
"Overview" => "overview_total.md",
"Linear Dynamics" => Any["Overview" => "overview_linear.md",
"Beam" => "models/beam.md",
"Building" => "models/building.md",
"CD Player" => "models/cdplayer.md",
"Crane" => "models/crane.md",
"Ellipse" => "models/ellipse.md",
"Five Dim System" => "models/five_dim_sys.md",
"Fom" => "models/fom.md",
"Heat" => "models/heat.md",
"Helicopter" => "models/helicopter.md",
"ISS" => "models/iss.md",
"MNA1" => "models/mna1.md",
"MNA5" => "models/mna5.md",
"Motor" => "models/motor.md",
"PDE" => "models/pde.md",
"Projectile" => "models/projectile.md"],
"Nonlinear Dynamics" => Any["Overview" => "overview_nonlinear.md",
"Biomodel 7d" => "models/biomodel7d.md",
"Biomodel 9d" => "models/biomodel9d.md",
"Brusselator" => "models/brusselator.md",
"Bucking column" => "models/buckling_column.md",
"Coupled VanDerPol" => "models/coupled_vanderpol.md",
"Double Gyre" => "models/doublegyre.md",
"Jet Engine" => "models/jet_engine.md",
"Henon-Helies" => "models/henon_heiles.md",
"Lotka-Volterra" => "models/lotka_volterra.md",
"Lorenz" => "models/lorenz.md",
"Robot Arm" => "models/robot_arm.md",
"Roessler" => "models/roessler.md",
"Spring Pendulum" => "models/spring_pendulum.md",
"Steam Governor" => "models/steam_governor.md",
"VanDerPol" => "models/vanderpol.md"],
"Hybrid Dynamics" => Any["Overview" => "overview_hybrid.md",
"Bouncing ball" => "models/bouncing_ball.md",
"Nonlinear Bouncing ball" => "models/bouncing_ball_nonlinear.md",
"Cardiac Cell" => "models/cardiac_cell.md",
"Clocked Thermostat" => "models/clocked_thermostat.md",
"Navigation System" => "models/navigation_system.md",
"Powertrain Control" => "models/powertrain_control.md",
"Spiking Neuron" => "models/spiking_neuron.md",
"Thermostat" => "models/thermostat.md"],
"References" => "references.md",
"About" => "about.md"])

Expand Down
8 changes: 1 addition & 7 deletions src/ReachabilityModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ using Reexport

include("utils.jl")

# name alias
fetch_model = load_model
fetch_meta = load_meta

export fetch_model, fetch_meta,
load_model, load_meta,
list_models
export load_model, load_meta, list_models

end # module
156 changes: 58 additions & 98 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,129 +1,89 @@
RM_dir = joinpath(dirname(pathof(ReachabilityModels)), "..")
MODEL_DIR = joinpath(dirname(pathof(ReachabilityModels)), "..", "src/models")

function load_model(instance::AbstractString; kwargs...)
nakeinstance = replace(instance, Pair(".jl", ""))
if haskey(kwargs, :X0)
X0 = kwargs[:X0]
else
X0 = load_meta(nakeinstance)["X0"]
function load_model(model::String; kwargs...)
X0 = get(kwargs, :X0, nothing)
if isnothing(X0)
X0 = load_meta(model)["X0"]
end
dir = joinpath(RM_dir, "src/models", "$(nakeinstance)/$(nakeinstance).jl")
if isfile(dir)
include(dir)
m = getfield(ReachabilityModels, Symbol(nakeinstance))
Base.invokelatest(m.model, X0)
file = joinpath(MODEL_DIR, model, model * ".jl")
if isfile(file)
include(file)
model_module = getfield(ReachabilityModels, Symbol(model))
Base.invokelatest(model_module.model, X0)
else
throw(ArgumentError("Model $nakeinstance not found"))
throw(ArgumentError("Model $model not found"))
end
end

function load_meta(instance::AbstractString)
nakeinstance = replace(instance, Pair(".jl", ""))

file = joinpath(RM_dir, "src/models", "$(nakeinstance)/$(nakeinstance)_meta.jl")
function load_meta(model::String)
file = joinpath(MODEL_DIR, model, model * "_meta.jl")
if isfile(file)
return include(file)
else
throw(ArgumentError("Meta for $instance not found"))
throw(ArgumentError("Meta for $model not found"))
end
return S
end

function list_models(arg, f)
function list_models(category, predicate)
models = Vector()
dir = joinpath(RM_dir, "src/models")
for model in readdir(dir)
meta = include(joinpath(dir, model, model * "_meta.jl"))
if f(meta["info"][arg])
push!(models, meta["info"]["name"])
for model in readdir(MODEL_DIR)
meta = include(joinpath(MODEL_DIR, model, model * "_meta.jl"))
dict = meta["info"]
if predicate(dict[category])
push!(models, dict["name"])
end
end
return models
end

# generate models overview in documentation
function generate_summary()

### OVERVIEW
open(joinpath(dirname(@__FILE__), "../docs/src/overview.md"), "w") do file
nmodels = length(list_models("dim", x -> x > 0))
nlinear = length(list_models("linear", x -> x == true))
nnonlinear = length(list_models("linear", x -> x == false))
nhybrid = length(list_models("hybrid", x -> x == true))
return print(file, """
# Models
Here is a table showing the number of models for each type
of system.

| Linear | Nonlinear | Hybrid | Total |
|:---------|------------:|---------:|---------:|
|$(nlinear)|$(nnonlinear)|$(nhybrid)|$(nmodels)|
""")
end

### LINEAR
open(joinpath(dirname(@__FILE__), "../docs/src/models/linear_overview.md"), "w") do file
function printrow(model)
meta = load_meta(model)["info"]
return println(file, "| **$(meta["name"])** | $(meta["dim"]) | $(-) | $(-) | $(-) |")
end

function write_overview_file(models, TYPE, type)
open(joinpath(dirname(@__FILE__), "../docs/src/overview_$type.md"), "w") do file
print(file,
"""
# Linear models
Here are the models with linear dynamics.

| Name | State dim | Input dim | Safety Property | Application Domain | Nominal Runtime (sec) |
|:------|----------:|----------:|-----------------:|-------------------:|----------------:|
""")

linear_models = list_models("linear", x -> x == true)
for model in linear_models
printrow(model)
end
end
# $TYPE models

### NONLINEAR
open(joinpath(dirname(@__FILE__), "../docs/src/models/nonlinear_overview.md"), "w") do file
function printrow(model)
meta = load_meta(model)["info"]
return println(file, "| **$(meta["name"])** | $(meta["dim"]) | $(-) | $(-) | $(-) |")
end

print(file,
"""
# Nonlinear models
Here are the models with nonlinear dynamics.
The following models have $type dynamics.

| Name | State dim | Input dim | Safesty Property | Application Domain | Nominal Runtime |
|:------|----------:|----------:|-----------------:|-------------------:|----------------:|
| Name | State dimension |
|:------|----------------:|
""")

nonlinear_models = list_models("linear", x -> x == false)
for model in nonlinear_models
printrow(model)
# | Name | State dim | Input dim | Safety Property | Application Domain |
# |:------|----------:|----------:|----------------:|-------------------:|
for model in models
meta = load_meta(model)["info"]
name = replace(meta["name"], "_" => "\\_")
println(file, "| **$name** | $(meta["dim"]) |")# $(-) | $(-) | $(-) |")
end
end
end

### HYBRID
open(joinpath(dirname(@__FILE__), "../docs/src/models/hybrid_overview.md"), "w") do file
function printrow(model)
meta = load_meta(model)["info"]
return println(file, "| **$(meta["name"])** | $(meta["dim"]) | $(-) | $(-) | $(-) |")
end
# generate models summary in documentation
function generate_summary()
linear_models = list_models("linear", x -> x)
nonlinear_models = list_models("linear", x -> !x)
hybrid_models = list_models("hybrid", x -> x)

open(joinpath(dirname(@__FILE__), "../docs/src/overview_total.md"), "w") do file
nlinear = length(linear_models)
nnonlinear = length(nonlinear_models)
nhybrid = length(hybrid_models)
ntotal = nlinear + nnonlinear + nhybrid
print(file, """
# Model overview

The following table shows the number of models for each type of system.

| Linear | Nonlinear | Hybrid | Total |
|:------:|:---------:|:------:|:-----:|
|$nlinear|$nnonlinear|$nhybrid|$ntotal|
""")
return nothing
end

print(file,
"""
# Hybrid models
Here are the models with hybrid systems.
write_overview_file(linear_models, "Linear", "linear")

| Name | State dim | Input dim | Safesty Property | Application Domain | Nominal Runtime |
|:------|----------:|----------:|-----------------:|-------------------:|----------------:|
""")
write_overview_file(nonlinear_models, "Nonlinear", "nonlinear")

hybrid_models = list_models("hybrid", x -> x == true)
for model in hybrid_models
printrow(model)
end
end
return write_overview_file(hybrid_models, "Hybrid", "hybrid")
end
Loading