Skip to content

Commit

Permalink
replace relpath by modelpath
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Oct 14, 2023
1 parent b4ec1e8 commit 8b76cb9
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 48 deletions.
5 changes: 5 additions & 0 deletions docs/generate.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import Literate
using Literate: script, markdown, notebook
import ReachabilityModels: @modelpath

src_dirs = [joinpath(@__DIR__, "..", "src", "models")]

trgt_dir_test = joinpath(@__DIR__, "..", "test", "models")
trgt_dir = joinpath(@__DIR__, "src", "models")
mkpath(trgt_dir)

macro modelpath(model_path::String, name::String)
return joinpath(MODELDIR, model_path, name)
end

for src_dir in src_dirs
for dir in readdir(src_dir)
if endswith(dir, ".jl")
Expand Down
2 changes: 1 addition & 1 deletion src/ReachabilityModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ fetch_meta = load_meta
export fetch_model, fetch_meta,
load_model, load_meta,
list_models,
@relpath
@modelpath

end # module
2 changes: 1 addition & 1 deletion src/models/beam/beam.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module beam #jl

using ReachabilityModels, MAT

file = matopen(@relpath "beam.mat")
file = matopen(@modelpath("beam", "beam.mat"))

## system matrix
A = read(file, "A")
Expand Down
5 changes: 2 additions & 3 deletions src/models/building/building.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

module building #jl

using ReachabilityAnalysis, MAT
using ReachabilityModels: @relpath
using ReachabilityModels, MAT

file = matopen(@relpath "building.mat")
file = matopen(@modelpath("building", "building.mat"))

## system matrix
A = read(file, "A")
Expand Down
5 changes: 2 additions & 3 deletions src/models/cdplayer/cdplayer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

module cdplayer #jl

using ReachabilityAnalysis, MAT
using ReachabilityModels: @relpath
using ReachabilityModels, MAT

file = matopen(@relpath "cdplayer.mat")
file = matopen(@modelpath("cdplayer", "cdplayer.mat"))

## system matrix
A = read(file, "A")
Expand Down
5 changes: 2 additions & 3 deletions src/models/fom/fom.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module fom #jl

using ReachabilityAnalysis, MAT
using ReachabilityModels: @relpath
using ReachabilityModels, MAT

file = matopen(@relpath "fom.mat")
file = matopen(@modelpath("fom", "fom.mat"))

# system matrix
A = float(read(file, "A")) # the matrix has Int entries
Expand Down
5 changes: 2 additions & 3 deletions src/models/heat/heat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

module heat #jl

using ReachabilityAnalysis, MAT, SparseArrays
using ReachabilityModels: @relpath
using ReachabilityModels, MAT, SparseArrays

file = matopen(@relpath "heat.mat")
file = matopen(@modelpath("head", "heat.mat"))

## system matrix
A = read(file, "A")
Expand Down
5 changes: 2 additions & 3 deletions src/models/helicopter/helicopter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

module helicopter #jl

using ReachabilityModels: @relpath
using ReachabilityAnalysis, MAT
using ReachabilityModels, MAT

file = matopen(@relpath "sx/heli.mat")
file = matopen(@modelpath("helicopter", "sx/heli.mat"))

# system matrix
A = read(file, "A")
Expand Down
5 changes: 2 additions & 3 deletions src/models/iss/iss.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module iss #jl

using ReachabilityAnalysis, MAT
using ReachabilityModels: @relpath
using ReachabilityModels, MAT

file = matopen(@relpath "iss.mat")
file = matopen(@modelpath("iss", "iss.mat"))

# system matrix
A = read(file, "A")
Expand Down
6 changes: 2 additions & 4 deletions src/models/linear_switching/linear_switching.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
module linear_switching

using ReachabilityAnalysis, ModelingToolkit, SymEngine
using ReachabilityModels, ModelingToolkit, SymEngine
using SpaceExParser: readsxmodel, _get_coeffs
using ReachabilityModels: @relpath

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 = @relpath "SpaceEx/model.xml"
file = @modelpath("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
5 changes: 2 additions & 3 deletions src/models/mna1/mna1.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module mna1 #jl

using ReachabilityAnalysis, MAT, SparseArrays
using ReachabilityModels: @relpath
using ReachabilityModels, MAT, SparseArrays

file = matopen(@relpath "mna1.mat")
file = matopen(@modelpath("mna1", "mna1.mat"))

# system matrix
A = sparse(read(file, "A"))
Expand Down
5 changes: 2 additions & 3 deletions src/models/mna5/mna5.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module mna5 #jl

using ReachabilityAnalysis, MAT, SparseArrays
using ReachabilityModels: @relpath
using ReachabilityModels, MAT, SparseArrays

file = matopen(@relpath "mna5.mat")
file = matopen(@modelpath("mna5", "mna5.mat"))

# system matrix
A = sparse(read(file, "A"))
Expand Down
3 changes: 1 addition & 2 deletions src/models/motor/motor.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module motor #jl

using ReachabilityAnalysis, SparseArrays
using ReachabilityModels: @relpath
using ReachabilityModels, SparseArrays

# system matrix
I = [1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 6, 7, 7, 7, 7, 8]
Expand Down
2 changes: 1 addition & 1 deletion src/models/pde/pde.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module pde #jl

using ReachabilityModels, MAT

file = matopen(@relpath "pde.mat")
file = matopen(@modelpath("pde", "pde.mat"))

## system matrix
A = float(read(file, "A")) # the matrix has Int entries
Expand Down
28 changes: 13 additions & 15 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,43 @@ function list_models(arg, f)
end

"""
@relpath(name)
@modelpath(model_path, name)
Return the absolute path to file `name` relative to the executing script.
## Input
### Input
- `name` -- filename
- `model_path` -- folder name (ignored by default)
- `name` -- filename
## Output
### Output
A string.
## Notes
### Notes
This macro is equivalent to `joinpath(@__DIR__, name)`.
The `@relpath` macro is used in model scripts to load data files relative to the
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,
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(@relpath "my_data.dat")
d = open(@modelpath("", "my_data.dat"))
# do stuff with d
```
In this example, the macro `@relpath "my_data.dat"` evaluates to the string
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 `@relpath`, this command would fail as julia
`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 relpath(name::String)
macro modelpath(model_path::String, name::String)
__source__.file === nothing && return nothing
_dirname = dirname(String(__source__.file))
dir = isempty(_dirname) ? pwd() : abspath(_dirname)
Expand Down

0 comments on commit 8b76cb9

Please sign in to comment.