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

Outsource 'modelpath' to ReachabilityBase #110

Merged
merged 1 commit into from
Feb 10, 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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
2 changes: 2 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -14,3 +15,4 @@ MAT = "0.10"
ModelingToolkit = "8"
Plots = "1"
ReachabilityAnalysis = "0.22 - 0.23"
ReachabilityBase = "0.2.3"
6 changes: 3 additions & 3 deletions docs/generate.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/ReachabilityModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module ReachabilityModels

using Reexport
@reexport using ReachabilityAnalysis
@reexport using ReachabilityBase.CurrentPath: @current_path

include("utils.jl")

Expand All @@ -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
4 changes: 2 additions & 2 deletions src/models/beam/beam.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/models/building/building.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/models/cdplayer/cdplayer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/models/fom/fom.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/models/heat/heat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/models/helicopter/helicopter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/models/iss/iss.jl
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
5 changes: 3 additions & 2 deletions src/models/linear_switching/linear_switching.jl
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions src/models/mna1/mna1.jl
Original file line number Diff line number Diff line change
@@ -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"))
Expand Down
4 changes: 2 additions & 2 deletions src/models/mna5/mna5.jl
Original file line number Diff line number Diff line change
@@ -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"))
Expand Down
4 changes: 2 additions & 2 deletions src/models/pde/pde.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 0 additions & 44 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading