Skip to content

Commit

Permalink
Merge branch 'main' into doc1
Browse files Browse the repository at this point in the history
  • Loading branch information
visr authored Sep 25, 2023
2 parents 6b55e30 + 104eea0 commit e68ae73
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/core_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- uses: actions/checkout@v4
- uses: prefix-dev/[email protected]
with:
pixi-version: latest
pixi-version: "v0.3.0"
cache: true
- name: Prepare pixi
run: pixi run post-install-without-pre-commit
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: latest
pixi-version: "v0.3.0"
cache: true
- name: Prepare pixi
run: pixi run post-install-without-pre-commit
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

- uses: prefix-dev/[email protected]
with:
pixi-version: latest
pixi-version: "v0.3.0"
cache: true
- name: Prepare pixi
run: pixi run post-install-without-pre-commit
Expand Down
2 changes: 1 addition & 1 deletion core/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ LoggingExtras = "1"
OrdinaryDiffEq = "6.7"
PreallocationTools = "0.4"
SQLite = "1.5.1"
SciMLBase = "1.60"
SciMLBase = "1.60, 2"
StructArrays = "0.6.13"
Tables = "1"
TerminalLoggers = "0.1.7"
Expand Down
3 changes: 2 additions & 1 deletion docs/contribute/python.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Before running the Julia tests or building binaries, example model input needs t
This is done by running the following:

```
pixi run generate-testmodels```
pixi run generate-testmodels
```

This places example model input files under `./generated_testmodels/`.
If the example models change, re-run this script.
Expand Down
6 changes: 3 additions & 3 deletions docs/python/examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,8 @@
"xy = np.array(\n",
" [\n",
" (0.0, 0.0), # 1: Basin\n",
" (1.0, 0.5), # 2: Pump\n",
" (1.0, -0.5), # 3: Pump\n",
" (1.0, 1.0), # 2: Pump\n",
" (1.0, -1.0), # 3: Pump\n",
" (2.0, 0.0), # 4: LevelBoundary\n",
" (-1.0, 0.0), # 5: TabulatedRatingCurve\n",
" (-2.0, 0.0), # 6: Terminal\n",
Expand Down Expand Up @@ -1449,7 +1449,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.11.4"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions python/ribasim/ribasim/geometry/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ def plot(self, **kwargs) -> Axes:
if color_flow is None:
color_flow = "#3690c0" # lightblue
kwargs_flow["color"] = color_flow
kwargs_flow["label"] = "Flow Edge"
kwargs_flow["label"] = "Flow edge"
else:
color_flow = kwargs["color_flow"]
del kwargs_flow["color_flow"], kwargs_control["color_flow"]

if color_control is None:
color_control = "grey"
kwargs_control["color"] = color_control
kwargs_control["label"] = "Affect Edge"
kwargs_control["label"] = "Control edge"
else:
color_control = kwargs["color_flow"]
del kwargs_flow["color_control"], kwargs_control["color_control"]
Expand Down
2 changes: 1 addition & 1 deletion python/ribasim/ribasim/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def plot_control_listen(self, ax):
[y, y_],
c="gray",
ls="--",
label="Listen Edge" if i == 0 else None,
label="Listen edge" if i == 0 else None,
)

def plot(self, ax=None) -> Any:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,12 @@ def level_setpoint_with_minmax_model():
xy = np.array(
[
(0.0, 0.0), # 1: Basin
(1.0, 0.5), # 2: Pump
(1.0, -0.5), # 3: Pump
(1.0, 1.0), # 2: Pump
(1.0, -1.0), # 3: Pump
(2.0, 0.0), # 4: LevelBoundary
(-1.0, 0.0), # 5: TabulatedRatingCurve
(-2.0, 0.0), # 6: Terminal
(0.0, 1.5), # 7: DiscreteControl
(1.0, 0.0), # 7: DiscreteControl
]
)

Expand Down
136 changes: 136 additions & 0 deletions utils/runstats.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# This runs all testmodels and logs many performance related variables into a table.
# It can be used to track performance over time, or determine the most efficient solver
# settings for a particular model.
# Right now it runs all the generated_testmodels, however running larger models that are
# more typical of real usage is probably more useful.

# TODO look into
# https://github.com/SciML/SciMLBenchmarks.jl
# https://github.com/JuliaCI/PkgBenchmark.jl

import Arrow
import Tables
using Ribasim
using Configurations: to_dict
using DataStructures: OrderedDict
using Dates
using LibGit2

"Add key config settings like solver settings to a dictionary"
function add_config!(dict, config::Ribasim.Config)
confdict = to_dict(config)
for (k, v) in confdict["solver"]
if k == "saveat"
# convert possible vector to scalar
if isempty(v)
v = 0.0
elseif v isa AbstractVector
v = missing
end
end
dict[string("solver_", k)] = something(v, missing)
end
dict["starttime"] = confdict["starttime"]
dict["endtime"] = confdict["endtime"]
return dict
end

"Add solver statistics of the Model to a dictionary, with stats_ prefix"
function add_stats!(dict, model::Ribasim.Model)
stats = model.integrator.sol.stats
for prop in propertynames(stats)
dict[string("stats_", prop)] = getproperty(stats, prop)
end
return dict
end

"Add @timed running time information of the Model to a dictionary, with timed_ prefix"
function add_timed!(dict, timed::NamedTuple)
dict["timed_retcode"] = string(timed.value.integrator.sol.retcode)
dict["timed_time"] = timed.time
dict["timed_bytes"] = timed.bytes
dict["timed_gctime"] = timed.gctime
# timed.gcstats has a Base.GC_Diff
return dict
end

"Add Julia and host information to a dictionary, with julia_ and host_ prefixes"
function add_env!(dict)
dict["time"] = now()
dict["date"] = today()
dict["julia_version"] = VERSION
dict["julia_nthreads"] = Threads.nthreads()
dict["host_cpu"] = Sys.cpu_info()[1].model
dict["host_kernel"] = Sys.KERNEL
dict["host_machine"] = Sys.MACHINE
dict["host_total_memory_gb"] = Sys.total_memory() / 2^30
dict["host_free_memory_gb"] = Sys.free_memory() / 2^30
return dict
end

"Add the Ribasim version, commit and branch name"
function add_git!(dict)
dict["git_ribasim"] = something(pkgversion(Ribasim), missing)
git_repo = normpath(@__DIR__, "..")
repo = GitRepo(git_repo)
branch = LibGit2.head(repo)
commit = LibGit2.peel(LibGit2.GitCommit, branch)
short_name = LibGit2.shortname(branch)
short_commit = string(LibGit2.GitShortHash(LibGit2.GitHash(commit), 10))
url = "https://github.com/Deltares/Ribasim/tree"
dict["git_commit"] = short_commit
dict["git_name"] = short_name
dict["git_commit_url"] = "$url/$short_commit"
dict["git_name_url"] = "$url/$short_name"
return dict
end

"Create a flat OrderedDict of a run with metadata"
function run_dict(toml_path, config, timed)
model = timed.value
dict = OrderedDict{String, Any}()

dict["directory"] = basename(dirname(toml_path))
dict["toml_name"] = basename(toml_path)
add_timed!(dict, timed)
add_stats!(dict, model)
add_git!(dict)
add_config!(dict, config)
add_env!(dict)
return dict
end

"Retrieve the names of the test models from a Python module"
function get_testmodels()::Vector{String}
_, dirs, _ = first(walkdir("generated_testmodels"))

toml_paths = String[]
for dir in dirs
if !startswith(dir, "invalid_")
toml_path = normpath("generated_testmodels", dir, "$dir.toml")
@assert isfile(toml_path)
push!(toml_paths, toml_path)
end
end

@assert length(toml_paths) > 10
return toml_paths
end

toml_paths = get_testmodels()
runs = OrderedDict{String, Any}[]
for toml_path in toml_paths
config = Ribasim.Config(toml_path)
println(basename(toml_path))
# run first to compile, if this takes too long perhaps we can shorten the duration
Ribasim.run(config)
timed = @timed Ribasim.run(config)
model = timed.value
dict = run_dict(toml_path, config, timed)
push!(runs, dict)
end

tbl = Tables.columntable(runs)

# Arrow.append("runs.arrow", tbl)
Arrow.write("runs.arrow", tbl)

0 comments on commit e68ae73

Please sign in to comment.