diff --git a/core/src/create.jl b/core/src/create.jl index 5d0878d6f..a303a1c92 100644 --- a/core/src/create.jl +++ b/core/src/create.jl @@ -708,15 +708,6 @@ function User(db::DB, config::Config)::User errors = true end - for (col, id) in zip(interpolations, node_ids) - for (demand_p_itp, p_itp) in zip(col, priorities) - if any(demand_p_itp.u .< 0.0) - @error "Demand of user node $id with priority #$p_itp should be non-negative" - errors = true - end - end - end - if !isnothing(first_row) min_level_ = coalesce(first_row.min_level, 0.0) return_factor_ = first_row.return_factor diff --git a/core/src/solve.jl b/core/src/solve.jl index ab37fd191..56dae81a2 100644 --- a/core/src/solve.jl +++ b/core/src/solve.jl @@ -415,6 +415,32 @@ struct User <: AbstractParameterNode allocated::Vector{Float64}, abstracted::Vector{Float64}, } + + function User( + node_id, + active, + demand, + allocated, + return_factor, + min_level, + priorities, + record, + ) + if valid_demand(node_id, demand, priorities) + return new( + node_id, + active, + demand, + allocated, + return_factor, + min_level, + priorities, + record, + ) + else + error("Invalid demand") + end + end end "Subgrid linearly interpolates basin levels." diff --git a/core/src/validation.jl b/core/src/validation.jl index d49dc3506..4de1dad85 100644 --- a/core/src/validation.jl +++ b/core/src/validation.jl @@ -660,3 +660,21 @@ function valid_subgrid( return !errors end + +function valid_demand( + node_id::Vector{NodeID}, + demand::Vector{Vector{ScalarInterpolation}}, + priorities::Vector{Int}, +)::Bool + errors = false + + for (col, id) in zip(demand, node_id) + for (demand_p_itp, p_itp) in zip(col, priorities) + if any(demand_p_itp.u .< 0.0) + @error "Demand of user node $id with priority #$p_itp should be non-negative" + errors = true + end + end + end + return !errors +end