From b6523a018aa24f8cff53152eceec3b84ac63de9d Mon Sep 17 00:00:00 2001 From: schillic Date: Fri, 9 Feb 2024 19:17:38 +0100 Subject: [PATCH] outsource 'modelpath' to ReachabilityBase --- Project.toml | 2 + docs/Project.toml | 2 + docs/generate.jl | 6 +-- src/ReachabilityModels.jl | 6 +-- src/models/beam/beam.jl | 4 +- src/models/building/building.jl | 4 +- src/models/cdplayer/cdplayer.jl | 4 +- src/models/fom/fom.jl | 4 +- src/models/heat/heat.jl | 4 +- src/models/helicopter/helicopter.jl | 4 +- src/models/iss/iss.jl | 4 +- .../linear_switching/linear_switching.jl | 5 ++- src/models/mna1/mna1.jl | 4 +- src/models/mna5/mna5.jl | 4 +- src/models/pde/pde.jl | 4 +- src/utils.jl | 44 ------------------- 16 files changed, 33 insertions(+), 72 deletions(-) diff --git a/Project.toml b/Project.toml index e0bdae00..7099eff6 100644 --- a/Project.toml +++ b/Project.toml @@ -6,6 +6,7 @@ version = "0.1.0" MAT = "23992714-dd62-5051-b70f-ba57cb901cac" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" ReachabilityAnalysis = "1e97bd63-91d1-579d-8e8d-501d2b57c93f" +ReachabilityBase = "379f33d0-9447-4353-bd03-d664070e549f" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" @@ -14,6 +15,7 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7" MAT = "0.9, 0.10" ModelingToolkit = "1, 2, 3, 4, 5, 6, 7, 8" ReachabilityAnalysis = "0.18.6 - 0.23" +ReachabilityBase = "0.2.3" Reexport = "1" Symbolics = "0.1, 1, 2, 3, 4, 5" julia = "1.6" diff --git a/docs/Project.toml b/docs/Project.toml index e141b1aa..06506931 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -5,6 +5,7 @@ MAT = "23992714-dd62-5051-b70f-ba57cb901cac" ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" ReachabilityAnalysis = "1e97bd63-91d1-579d-8e8d-501d2b57c93f" +ReachabilityBase = "379f33d0-9447-4353-bd03-d664070e549f" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" [compat] @@ -14,3 +15,4 @@ MAT = "0.10" ModelingToolkit = "8" Plots = "1" ReachabilityAnalysis = "0.22 - 0.23" +ReachabilityBase = "0.2.3" diff --git a/docs/generate.jl b/docs/generate.jl index d16fec0a..f9751bd3 100644 --- a/docs/generate.jl +++ b/docs/generate.jl @@ -1,13 +1,13 @@ import Literate -import ReachabilityModels: @modelpath +import ReachabilityBase.CurrentPath: @current_path source_dir = joinpath(@__DIR__, "..", "src", "models") target_dir = joinpath(@__DIR__, "src", "models") mkpath(target_dir) # overwrite to use the correct model path -macro modelpath(model_path::String, name::String) - return joinpath(source_dir, model_path, name) +macro current_path(prefix::String, filename::String) + return joinpath(source_dir, prefix, filename) end for model in readdir(source_dir) diff --git a/src/ReachabilityModels.jl b/src/ReachabilityModels.jl index c3751f59..9750e8c8 100644 --- a/src/ReachabilityModels.jl +++ b/src/ReachabilityModels.jl @@ -2,6 +2,7 @@ module ReachabilityModels using Reexport @reexport using ReachabilityAnalysis +@reexport using ReachabilityBase.CurrentPath: @current_path include("utils.jl") @@ -11,7 +12,6 @@ fetch_meta = load_meta export fetch_model, fetch_meta, load_model, load_meta, - list_models, - @modelpath + list_models -end # module +end # module diff --git a/src/models/beam/beam.jl b/src/models/beam/beam.jl index 7b2db408..fa23ecc1 100644 --- a/src/models/beam/beam.jl +++ b/src/models/beam/beam.jl @@ -4,9 +4,9 @@ module beam #jl -using ReachabilityModels, MAT +using ReachabilityModels, MAT, ReachabilityBase.CurrentPath -file = matopen(@modelpath("beam", "beam.mat")) +file = matopen(@current_path("beam", "beam.mat")) ## system matrix A = read(file, "A") diff --git a/src/models/building/building.jl b/src/models/building/building.jl index ecc7305d..5e0acb2b 100644 --- a/src/models/building/building.jl +++ b/src/models/building/building.jl @@ -4,9 +4,9 @@ module building #jl -using ReachabilityModels, MAT +using ReachabilityModels, MAT, ReachabilityBase.CurrentPath -file = matopen(@modelpath("building", "building.mat")) +file = matopen(@current_path("building", "building.mat")) ## system matrix A = read(file, "A") diff --git a/src/models/cdplayer/cdplayer.jl b/src/models/cdplayer/cdplayer.jl index ff5bb378..6d101322 100644 --- a/src/models/cdplayer/cdplayer.jl +++ b/src/models/cdplayer/cdplayer.jl @@ -4,9 +4,9 @@ module cdplayer #jl -using ReachabilityModels, MAT +using ReachabilityModels, MAT, ReachabilityBase.CurrentPath -file = matopen(@modelpath("cdplayer", "cdplayer.mat")) +file = matopen(@current_path("cdplayer", "cdplayer.mat")) ## system matrix A = read(file, "A") diff --git a/src/models/fom/fom.jl b/src/models/fom/fom.jl index 279bbcf6..2a0535b9 100644 --- a/src/models/fom/fom.jl +++ b/src/models/fom/fom.jl @@ -1,8 +1,8 @@ module fom #jl -using ReachabilityModels, MAT +using ReachabilityModels, MAT, ReachabilityBase.CurrentPath -file = matopen(@modelpath("fom", "fom.mat")) +file = matopen(@current_path("fom", "fom.mat")) # system matrix A = float(read(file, "A")) # the matrix has Int entries diff --git a/src/models/heat/heat.jl b/src/models/heat/heat.jl index 94e4cdb7..abd2ab34 100644 --- a/src/models/heat/heat.jl +++ b/src/models/heat/heat.jl @@ -4,9 +4,9 @@ module heat #jl -using ReachabilityModels, MAT, SparseArrays +using ReachabilityModels, MAT, SparseArrays, ReachabilityBase.CurrentPath -file = matopen(@modelpath("heat", "heat.mat")) +file = matopen(@current_path("heat", "heat.mat")) ## system matrix A = read(file, "A") diff --git a/src/models/helicopter/helicopter.jl b/src/models/helicopter/helicopter.jl index ba7dba0b..8ac1fd08 100644 --- a/src/models/helicopter/helicopter.jl +++ b/src/models/helicopter/helicopter.jl @@ -9,9 +9,9 @@ module helicopter #jl -using ReachabilityModels, MAT +using ReachabilityModels, MAT, ReachabilityBase.CurrentPath -file = matopen(@modelpath("helicopter", "sx/heli.mat")) +file = matopen(@current_path("helicopter", "sx/heli.mat")) # system matrix A = read(file, "A") diff --git a/src/models/iss/iss.jl b/src/models/iss/iss.jl index 8629e69b..c372cfd2 100644 --- a/src/models/iss/iss.jl +++ b/src/models/iss/iss.jl @@ -1,8 +1,8 @@ module iss #jl -using ReachabilityModels, MAT +using ReachabilityModels, MAT, ReachabilityBase.CurrentPath -file = matopen(@modelpath("iss", "iss.mat")) +file = matopen(@current_path("iss", "iss.mat")) # system matrix A = read(file, "A") diff --git a/src/models/linear_switching/linear_switching.jl b/src/models/linear_switching/linear_switching.jl index cc7a2a6e..ad2a58da 100644 --- a/src/models/linear_switching/linear_switching.jl +++ b/src/models/linear_switching/linear_switching.jl @@ -1,13 +1,14 @@ module linear_switching -using ReachabilityModels, ModelingToolkit, SymEngine +using ReachabilityModels, ModelingToolkit, SymEngine, ReachabilityBase.CurrentPath using SpaceExParser: readsxmodel, _get_coeffs + n = 5 ## state dimension U = Interval(-1.0, 1.0) ## common input domain m = dim(U) ## input dimension ε = 1e-6 ## auxiliary bloating of guards for ensuring intersection -file = @modelpath("linear_switching", "SpaceEx/model.xml") +file = @current_path("linear_switching", "SpaceEx/model.xml") _model = readsxmodel(file; raw_dict=true) variables = convert.(Basic, [f.args[1].args[1] for f in _model["flows"][1]]) inputs = [convert(Basic, :u)] diff --git a/src/models/mna1/mna1.jl b/src/models/mna1/mna1.jl index 2af7f60e..a5aef85e 100644 --- a/src/models/mna1/mna1.jl +++ b/src/models/mna1/mna1.jl @@ -1,8 +1,8 @@ module mna1 #jl -using ReachabilityModels, MAT, SparseArrays +using ReachabilityModels, MAT, SparseArrays, ReachabilityBase.CurrentPath -file = matopen(@modelpath("mna1", "mna1.mat")) +file = matopen(@current_path("mna1", "mna1.mat")) # system matrix A = sparse(read(file, "A")) diff --git a/src/models/mna5/mna5.jl b/src/models/mna5/mna5.jl index 917db751..e7a98749 100644 --- a/src/models/mna5/mna5.jl +++ b/src/models/mna5/mna5.jl @@ -1,8 +1,8 @@ module mna5 #jl -using ReachabilityModels, MAT, SparseArrays +using ReachabilityModels, MAT, SparseArrays, ReachabilityBase.CurrentPath -file = matopen(@modelpath("mna5", "mna5.mat")) +file = matopen(@current_path("mna5", "mna5.mat")) # system matrix A = sparse(read(file, "A")) diff --git a/src/models/pde/pde.jl b/src/models/pde/pde.jl index 2a53a98a..668307dd 100644 --- a/src/models/pde/pde.jl +++ b/src/models/pde/pde.jl @@ -2,9 +2,9 @@ module pde #jl -using ReachabilityModels, MAT +using ReachabilityModels, MAT, ReachabilityBase.CurrentPath -file = matopen(@modelpath("pde", "pde.mat")) +file = matopen(@current_path("pde", "pde.mat")) ## system matrix A = float(read(file, "A")) # the matrix has Int entries diff --git a/src/utils.jl b/src/utils.jl index ef549266..c956b60e 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -41,50 +41,6 @@ function list_models(arg, f) return models end -""" - @modelpath(model_path, name) - -Return the absolute path to file `name` relative to the executing script. - -### Input - -- `model_path` -- folder name (ignored by default) -- `name` -- filename - -### Output - -A string. - -### Notes - -This macro is equivalent to `joinpath(@__DIR__, name)`. -The `@modelpath` macro is used in model scripts to load data files relative to the -location of the model, without having to change the directory of the Julia session. -For instance, suppose that the folder `/home/projects/models` contains the script -`my_model.jl`, and suppose that the data file `my_data.dat` located in the same -directory is required to be loaded by `my_model.jl`. Then, - -```julia -# suppose the working directory is /home/julia/ and so we ran the script as -# julia -e "include("../projects/models/my_model.jl")" -# in the model file /home/projects/models/my_model.jl we write: -d = open(@modelpath("", "my_data.dat")) -# do stuff with d -``` - -In this example, the macro `@modelpath("", "my_data.dat")` evaluates to the string -`/home/projects/models/my_data.dat`. If the script `my_model.jl` only had -`d = open("my_data.dat")`, without `@modelpath`, this command would fail as julia -would have looked for `my_data.dat` in the *working* directory, resulting in an -error that the file `/home/julia/my_data.dat` is not found. -""" -macro modelpath(model_path::String, name::String) - __source__.file === nothing && return nothing - _dirname = dirname(String(__source__.file)) - dir = isempty(_dirname) ? pwd() : abspath(_dirname) - return joinpath(dir, name) -end - # generate models overview in documentation function generate_summary()