Skip to content

Commit

Permalink
Validation of the second last input is now done inside inner construc…
Browse files Browse the repository at this point in the history
…tor. Code of validation is now a function in validation.jl
  • Loading branch information
Jingru923 committed Jan 22, 2024
1 parent b8dab67 commit 7667f19
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
9 changes: 0 additions & 9 deletions core/src/create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions core/src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
18 changes: 18 additions & 0 deletions core/src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7667f19

Please sign in to comment.