From 9f54efd8452571cc8079a74cdcac129297b35b64 Mon Sep 17 00:00:00 2001 From: schillic Date: Sat, 9 Mar 2024 23:43:07 +0100 Subject: [PATCH 1/3] revise code --- src/utils.jl | 156 +++++++++++++++++++-------------------------------- 1 file changed, 58 insertions(+), 98 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index c956b60e..bcd24fea 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -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 From a96068bc2320956630c5506e2ff1987254f8c42e Mon Sep 17 00:00:00 2001 From: schillic Date: Sat, 9 Mar 2024 23:44:05 +0100 Subject: [PATCH 2/3] update paths of overview files --- .gitignore | 4 +-- docs/make.jl | 84 ++++++++++++++++++++++++++-------------------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index d614b9b2..931c3909 100644 --- a/.gitignore +++ b/.gitignore @@ -20,5 +20,5 @@ docs/site/ Manifest.toml -/docs/src/models/ -docs/src/overview.md +docs/src/models/ +docs/src/overview* diff --git a/docs/make.jl b/docs/make.jl index 82848cce..2e8abd6a 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -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"]) From 4944033b2092bc8d3644ba9928bc8eb126de5982 Mon Sep 17 00:00:00 2001 From: schillic Date: Sat, 9 Mar 2024 23:45:27 +0100 Subject: [PATCH 3/3] remove unused aliases --- src/ReachabilityModels.jl | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/ReachabilityModels.jl b/src/ReachabilityModels.jl index 9750e8c8..7b5ffe90 100644 --- a/src/ReachabilityModels.jl +++ b/src/ReachabilityModels.jl @@ -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