Skip to content

Commit

Permalink
Merge branch 'main' into allocate_per_source
Browse files Browse the repository at this point in the history
  • Loading branch information
visr authored Nov 26, 2024
2 parents 30239dc + d65ad7e commit b067036
Show file tree
Hide file tree
Showing 25 changed files with 382 additions and 331 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/core_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- uses: julia-actions/julia-processcoverage@v1
with:
directories: core/src
- uses: codecov/codecov-action@v4
- uses: codecov/codecov-action@v5
with:
files: lcov.info
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/python_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ jobs:
- name: Test Ribasim Python
run: pixi run --environment ${{ matrix.pixi-environment }} test-ribasim-python-cov
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/qgis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ jobs:
- name: Run tests
run: pixi run test-ribasim-qgis-docker
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
35 changes: 35 additions & 0 deletions build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,38 @@ pixi run build
```

> :warning: If the build is failing, because it cannot find certain files, chances are high that you need to enable long paths in Windows.
## Ribasim CLI

In order to find out about it's usage call `ribasim --help`

## Libribasim

Libribasim is a shared library that exposes Ribasim functionality to external (non-Julian)
programs. It can be compiled using [PackageCompiler's
create_lib](https://julialang.github.io/PackageCompiler.jl/stable/libs.html), which is set
up in this directory. The C API that is offered to control Ribasim is the C API of the
[Basic Model Interface](https://bmi.readthedocs.io/en/latest/), also known as BMI.

Not all BMI functions are implemented yet.
Couplings to other models are implemented in [`imod_coupler`](https://github.com/Deltares/imod_coupler).

Here is an example of using libribasim from Python:

```python
In [1]: from ctypes import CDLL, c_int, c_char_p, create_string_buffer, byref

In [2]: c_dll = CDLL("libribasim", winmode=0x08) # winmode for Windows

In [3]: argument = create_string_buffer(0)
...: c_dll.init_julia(c_int(0), byref(argument))
Out[3]: 1

In [4]: config_path = "ribasim.toml"

In [5]: c_dll.initialize(c_char_p(config_path.encode()))
Out[5]: 0

In [6]: c_dll.update()
Out[6]: 0
```
77 changes: 18 additions & 59 deletions build/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,6 @@ using PackageCompiler
using TOML
using LibGit2

"""
# Ribasim CLI
In order to find out about it's usage call `ribasim --help`
# Libribasim
Libribasim is a shared library that exposes Ribasim functionality to external (non-Julian)
programs. It can be compiled using [PackageCompiler's
create_lib](https://julialang.github.io/PackageCompiler.jl/stable/libs.html), which is set
up in this directory. The C API that is offered to control Ribasim is the C API of the
[Basic Model Interface](https://bmi.readthedocs.io/en/latest/), also known as BMI.
Not all BMI functions are implemented yet, this has been set up as a proof of concept to
demonstrate that we can use other software such as
[`imod_coupler`](https://github.com/Deltares/imod_coupler) to control Ribasim and couple it to
other models.
Here is an example of using libribasim from Python:
```python
In [1]: from ctypes import CDLL, c_int, c_char_p, create_string_buffer, byref
In [2]: c_dll = CDLL("libribasim", winmode=0x08) # winmode for Windows
In [3]: argument = create_string_buffer(0)
...: c_dll.init_julia(c_int(0), byref(argument))
Out[3]: 1
In [4]: config_path = "ribasim.toml"
In [5]: c_dll.initialize(c_char_p(config_path.encode()))
Out[5]: 0
In [6]: c_dll.update()
Out[6]: 0
```
"""
function main()
project_dir = "../core"
license_file = "../LICENSE"
Expand All @@ -61,23 +23,31 @@ function main()
force = true,
)

readme = @doc(build_app)
add_metadata(project_dir, license_file, output_dir, git_repo, readme)
add_metadata(project_dir, license_file, output_dir, git_repo, readme_start)
run(Cmd(`cargo build --release`; dir = "cli"))
ribasim = Sys.iswindows() ? "ribasim.exe" : "ribasim"
cp("cli/target/release/$ribasim", "ribasim/$ribasim"; force = true)
end

function set_version(filename, version; group = nothing)
readme_start = """
# Ribasim
Ribasim is a water resources model to simulate the physical behavior of a managed open water system
based on a set of control rules and a prioritized water allocation strategy.
Usage: `ribasim path/to/model/ribasim.toml`
Documentation: https://ribasim.org/
"""

"Use the git tag for `ribasim --version`,
so dev builds can be identified by <tag>-g<short-commit>"
function set_version(filename::String, tag::String)::Nothing
data = TOML.parsefile(filename)
if !isnothing(group)
data[group]["version"] = version
else
data["version"] = version
end
data["package"]["version"] = tag
open(filename, "w") do io
TOML.print(io, data)
end
return nothing
end

"""
Expand Down Expand Up @@ -111,15 +81,6 @@ function add_metadata(project_dir, license_file, output_dir, git_repo, readme)
force = true,
)

# since the exact Ribasim version may be hard to find in the Manifest.toml file
# we can also extract that information, and add it to the README.md
manifest = TOML.parsefile(normpath(git_repo, "Manifest.toml"))
if !haskey(manifest, "manifest_format")
error("Manifest.toml is in the old format, run Pkg.upgrade_manifest()")
end
julia_version = manifest["julia_version"]
ribasim_entry = only(manifest["deps"]["Ribasim"])
version = ribasim_entry["version"]
repo = GitRepo(git_repo)
branch = LibGit2.head(repo)
commit = LibGit2.peel(LibGit2.GitCommit, branch)
Expand Down Expand Up @@ -149,17 +110,15 @@ function add_metadata(project_dir, license_file, output_dir, git_repo, readme)
This build uses the Ribasim version mentioned below.
```toml
release = "$tag"
version = "$tag"
commit = "$url/$short_commit"
branch = "$url/$short_name"
julia_version = "$julia_version"
core_version = "$version"
```"""
println(io, version_info)
end

# Override the Cargo.toml file with the git version
set_version("cli/Cargo.toml", tag; group = "package")
set_version("cli/Cargo.toml", tag)
end

main()
6 changes: 5 additions & 1 deletion build/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import subprocess
from pathlib import Path

Expand Down Expand Up @@ -30,7 +31,10 @@ def test_version():
[executable, "--version"], check=True, capture_output=True, text=True
)

assert ribasim.__version__ in result.stdout
# ribasim --version is based on the git tag so can be different from
# ribasim.__version__ during development
version_pattern = r"ribasim \d{4,}\.\d+\.\d+"
assert re.match(version_pattern, result.stdout)


def test_help():
Expand Down
2 changes: 1 addition & 1 deletion core/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ SciMLBase = "2.36"
SparseArrays = "1"
SparseConnectivityTracer = "0.6.8"
Statistics = "1"
StructArrays = "0.6.13"
StructArrays = "0.6.13, 0.7"
TOML = "1"
Tables = "1"
TerminalLoggers = "0.1.7"
Expand Down
3 changes: 2 additions & 1 deletion core/src/Ribasim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ using MetaGraphsNext:
using EnumX: EnumX, @enumx

# Easily change an immutable field of an object.
using Accessors: @set
using Accessors: @set, @reset

# Iteration utilities, used to partition and group tables.
import IterTools
Expand Down Expand Up @@ -168,6 +168,7 @@ include("read.jl")
include("write.jl")
include("bmi.jl")
include("callback.jl")
include("concentration.jl")
include("main.jl")
include("libribasim.jl")

Expand Down
Loading

0 comments on commit b067036

Please sign in to comment.