Skip to content

Commit

Permalink
Merge branch 'main_network_allocation_main' into main_network_allocat…
Browse files Browse the repository at this point in the history
…ion_optimization
  • Loading branch information
SouthEndMusic committed Jan 23, 2024
2 parents e7c5703 + 3b733c9 commit 268d71e
Show file tree
Hide file tree
Showing 27 changed files with 205 additions and 173 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/core_tests.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Julia Tests
on:
push:
branches: [main]
branches: [main, update/pixi-lock]
paths-ignore: [".teamcity/**"]
tags: ["*"]
pull_request:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Docs
on:
push:
branches: [main]
branches: [main, update/pixi-lock]
paths-ignore: [".teamcity/**"]
pull_request:
merge_group:
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/pixi_auto_update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Pixi auto update

on:
schedule:
# At 03:00 on day 1 of the month
- cron: "0 3 1 * *"
# on demand
workflow_dispatch:

jobs:
auto-update:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
- uses: prefix-dev/[email protected]
with:
pixi-version: "latest"
cache: false
- name: Update pixi lock file
run: |
rm pixi.lock
pixi install
- uses: peter-evans/create-pull-request@v5
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch: update/pixi-lock
title: Update pixi lock file
commit-message: "Update `pixi.lock`"
body: Update pixi dependencies to the latest version.
author: "GitHub <[email protected]>"
2 changes: 1 addition & 1 deletion .github/workflows/pre-commit_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
pull_request:
merge_group:
push:
branches: [main, update/pre-commit-hooks]
branches: [main, update/pre-commit-hooks, update/pixi-lock]

jobs:
check:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python_lint.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Python Lint
on:
push:
branches: [main]
branches: [main, update/pixi-lock]
paths-ignore: [".teamcity/**"]
tags: ["*"]
pull_request:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python_tests.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Ribasim Python Tests
on:
push:
branches: [main]
branches: [main, update/pixi-lock]
paths-ignore: [".teamcity/**"]
tags: ["*"]
pull_request:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/qgis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: QGIS Tests

on:
push:
branches: [main]
branches: [main, update/pixi-lock]
paths-ignore: [".teamcity/**"]
tags: ["*"]
pull_request:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ pixi run build-ribasim-cli]]></param>
<param name="secure:github_access_token" value="credentialsJSON:71420214-373c-4ccd-ba32-2ea886843f62" />
</parameters>
</extension>
<extension id="PROVIDE_AWS_CREDS" type="PROVIDE_AWS_CREDS">
<parameters>
<param name="awsConnectionId" value="awsConnection_1" />
<param name="awsSessionDuration" value="60" />
</parameters>
</extension>
</build-extensions>
<cleanup />
</settings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pixi run test-ribasim-api]]></param>
<parameters>
<param name="afterSuccessfulBuildOnly" value="true" />
<param name="branchFilter" value="+:&lt;default&gt;" />
<param name="dependsOn" value="Ribasim_RibasimLinux_BuildLibribasimWindows" />
<param name="dependsOn" value="Ribasim_Linux_BuildLibribasim" />
</parameters>
</build-trigger>
</build-triggers>
Expand Down
2 changes: 1 addition & 1 deletion Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.10.0"
manifest_format = "2.0"
project_hash = "212d113fb58ab2e43d8fe0283d717923e72a9a88"
project_hash = "77dde6fd5d6ca1581c6694925c34d2bb0d69830e"

[[deps.ADTypes]]
git-tree-sha1 = "41c37aa88889c171f1300ceac1313c06e891d245"
Expand Down
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ description = "Meta-project used to share the Manifest of Ribasim and its depend
[deps]
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
BasicModelInterface = "59605e27-edc0-445a-b93d-c09a3a50b330"
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
Infiltrator = "5903a43b-9cc3-4c30-8d17-598619ec4e9b"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
Expand All @@ -16,6 +17,7 @@ Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
Ribasim = "aac5e3d9-0b8f-4d4f-8241-b1a7a9632635"
SQLite = "0aa819cd-b072-5ff4-a722-6bc24af294d9"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
TestEnv = "1e6cf692-eddd-4d53-88a5-2d735e33781b"
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"
Expand Down
6 changes: 2 additions & 4 deletions core/src/bmi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ function BMI.initialize(T::Type{Model}, config::Config)::Model
end
@debug "Read database into memory."

storage, errors = get_storages_from_levels(parameters.basin, state.node_id, state.level)
if errors
error("Encountered errors while parsing the initial levels of basins.")
end
storage = get_storages_from_levels(parameters.basin, state.level)

# Synchronize level with storage
set_current_basin_properties!(parameters.basin, storage)

Expand Down
45 changes: 20 additions & 25 deletions core/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -381,33 +381,28 @@ function get_storage_from_level(basin::Basin, state_idx::Int, level::Float64)::F
end

"""Compute the storages of the basins based on the water level of the basins."""
function get_storages_from_levels(
basin::Basin,
state_node_id::Vector{Int},
state_level::Vector{Float64},
)::Tuple{Vector{Float64}, Bool}
(; node_id) = basin

storages = fill(1.0, length(node_id))
n_specified_states = length(state_node_id)

if n_specified_states > 0
basin_state_index = 1
basin_state_node_id = state_node_id[1]

for (i, id) in enumerate(node_id)
if basin_state_node_id == id.value
storages[i] =
get_storage_from_level(basin, i, state_level[basin_state_index])
basin_state_index += 1
if basin_state_index > n_specified_states
break
end
basin_state_node_id = state_node_id[basin_state_index]
end
function get_storages_from_levels(basin::Basin, levels::Vector)::Vector{Float64}
errors = false
state_length = length(levels)
basin_length = length(basin.level)
if state_length != basin_length
@error "Unexpected 'Basin / state' length." state_length basin_length
errors = true
end
storages = zeros(state_length)

for (i, level) in enumerate(levels)
storage = get_storage_from_level(basin, i, level)
if isnan(storage)
errors = true
end
storages[i] = storage
end
if errors
error("Encountered errors while parsing the initial levels of basins.")
end
return storages, any(isnan.(storages))

return storages
end

"""
Expand Down
24 changes: 0 additions & 24 deletions core/src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,30 +338,6 @@ function variable_nt(s::Any)
NamedTuple{names}((getfield(s, x) for x in names))
end

function is_consistent(node, edge, state, static, profile, time)

# Check that node ids exist
# TODO Do we need to check the reverse as well? All ids in use?
ids = node.fid
@assert edge.from_node_id ids "Edge from_node_id not in node ids"
@assert edge.to_node_id ids "Edge to_node_id not in node ids"
@assert state.node_id ids "State id not in node ids"
@assert static.node_id ids "Static id not in node ids"
@assert profile.node_id ids "Profile id not in node ids"
@assert time.node_id ids "Time id not in node ids"

# Check edges for uniqueness
@assert allunique(edge, [:from_node_id, :to_node_id]) "Duplicate edge found"

# TODO Check states

# TODO Check statics

# TODO Check forcings

true
end

# functions used by sort(x; by)
sort_by_fid(row) = row.fid
sort_by_id(row) = row.node_id
Expand Down
1 change: 1 addition & 0 deletions core/test/allocation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ end
db_path = Ribasim.input_path(cfg, cfg.database)
db = SQLite.DB(db_path)
p = Ribasim.Parameters(db, cfg)
close(db)
(; allocation, graph) = p
(; main_network_connections) = allocation

Expand Down
2 changes: 1 addition & 1 deletion core/test/bmi_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ end
toml_path = normpath(@__DIR__, "../../generated_testmodels/basic/ribasim.toml")
model = BMI.initialize(Ribasim.Model, toml_path)
storage0 = BMI.get_value_ptr(model, "volume")
@test storage0 == ones(4)
@test storage0 ones(4)
@test_throws "Unknown variable foo" BMI.get_value_ptr(model, "foo")
BMI.update_until(model, 86400.0)
storage = BMI.get_value_ptr(model, "volume")
Expand Down
2 changes: 1 addition & 1 deletion core/test/run_models_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
@test flow.from_node_id[1:4] == [6, typemax(Int), 0, 6]
@test flow.to_node_id[1:4] == [6, typemax(Int), typemax(Int), 0]

@test basin.storage[1] == 1.0
@test basin.storage[1] 1.0
@test basin.level[1] 0.044711584

# The exporter interpolates 1:1 for three subgrid elements, but shifted by 1.0 meter.
Expand Down
12 changes: 5 additions & 7 deletions core/test/utils_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,14 @@ end

logger = TestLogger()
with_logger(logger) do
storages, errors = Ribasim.get_storages_from_levels(basin, [1], [-1.0])
@test isnan(storages[1])
@test errors
# Storage for basin with unspecified level is set to 1.0
@test storages[2] == 1.0
@test_throws ErrorException Ribasim.get_storages_from_levels(basin, [-1.0])
end

@test length(logger.logs) == 1
@test length(logger.logs) == 2
@test logger.logs[1].level == Error
@test logger.logs[1].message ==
@test logger.logs[1].message == "Unexpected 'Basin / state' length."
@test logger.logs[2].level == Error
@test logger.logs[2].message ==
"The level -1.0 of basin #1 is lower than the bottom of this basin 0.0."

# Converting from storages to levels and back should return the same storages
Expand Down
Loading

0 comments on commit 268d71e

Please sign in to comment.