Skip to content

Add check_resources hook for resource checks in check_case_data - #109

Open
espenbodal wants to merge 2 commits into
mainfrom
enhance/check_resources
Open

espenbodal wants to merge 2 commits into
mainfrom
enhance/check_resources

Conversation

@espenbodal

Copy link
Copy Markdown
Member

Closes #107. Adds check_resources(case, 𝒫ˢᵘᵇ, modeltype, check_timeprofiles), called from check_case_data for every resource family; the default checks nothing. check_case_data gains the arguments modeltype and check_timeprofiles. See the issue for the discussion. Includes tests, NEWS entry and documentation.

@espenbodal
espenbodal requested review from JulStraus and a balanced review from Copilot October 8, 2026 13:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The linked issue’s element-level hook and resource log grouping are missing, and two documentation links are unresolved.

3 open findings
What changed in this PR

Adds resource-level validation to the case consistency-checking framework.

Changes:

  • Adds the check_resources extension hook.
  • Adds resource-check tests and extension guidance.
  • Updates internal documentation and release notes.
File Description
src/​checks.jl Implements and invokes the resource-check hook.
test/​test_checks.jl Tests custom resource validation.
docs/​src/​how-to/​extend-resource-functionality.md Documents implementing resource checks.
docs/​src/​library/​internals/​functions.md Lists the new internal function.
NEWS.md Records the new API.

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Comment thread src/checks.jl

# Check the resources of the case per resource family
for 𝒫ˢᵘᵇ ∈ res_types_vec(get_products(case))
check_resources(case, 𝒫ˢᵘᵇ, modeltype, check_timeprofiles)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is whether that should be invoked directly here or could be a different PR. Given the description, it would not be necessary to have it here while I agree that Issue #107 states that it should be included. In this case, however, the question is where it is based located. As this is still up to debate, I would leave it at the moment be and ignore this comment.

Comment thread src/checks.jl
Comment on lines +162 to +164
for 𝒫ˢᵘᵇ ∈ res_types_vec(get_products(case))
check_resources(case, 𝒫ˢᵘᵇ, modeltype, check_timeprofiles)
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous answer.

Comment thread src/checks.jl Outdated
- The individual elements vector must be unique, that it is not possible to have two vector
of nodes within the elements vector.
- Check that the coupling functions do return elements and not only an empty vector
- Call of [`check_resources`](@ref) for every resource family ([`resource_family`](@ref)) of

@JulStraus JulStraus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too many things to adjust. There is however an issue that we only consider ResourceEmits that are included in the case description. Any other Resources directly in the case description do not matter.

That is however something we have to consider separately.

Comment thread src/checks.jl

# Check the resources of the case per resource family
for 𝒫ˢᵘᵇ ∈ res_types_vec(get_products(case))
check_resources(case, 𝒫ˢᵘᵇ, modeltype, check_timeprofiles)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is whether that should be invoked directly here or could be a different PR. Given the description, it would not be necessary to have it here while I agree that Issue #107 states that it should be included. In this case, however, the question is where it is based located. As this is still up to debate, I would leave it at the moment be and ignore this comment.

Comment thread src/checks.jl
Comment on lines +162 to +164
for 𝒫ˢᵘᵇ ∈ res_types_vec(get_products(case))
check_resources(case, 𝒫ˢᵘᵇ, modeltype, check_timeprofiles)
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous answer.

Comment thread src/checks.jl
Comment on lines +170 to +179
Check the resources `𝒫ˢᵘᵇ` of a single resource type segment ([`res_types_vec`](@ref)) of
the `case`. The function is called from [`check_case_data`](@ref) for every resource type
segment of the case products. The default method does not check anything; extension
packages that introduce resource types provide methods for their resource types.

The `case` is included as argument so that the combination of the resources with the
elements carrying them can be checked as well, *e.g.*, through iterating over
`get_nodes(case)`. Checks that depend only on the type of an element belong in the check
functions of the package introducing the element type ([`check_node`](@ref),
[`check_node_data`](@ref), [`check_link`](@ref)).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too verbose.

Suggested change
Check the resources `𝒫ˢᵘᵇ` of a single resource type segment ([`res_types_vec`](@ref)) of
the `case`. The function is called from [`check_case_data`](@ref) for every resource type
segment of the case products. The default method does not check anything; extension
packages that introduce resource types provide methods for their resource types.
The `case` is included as argument so that the combination of the resources with the
elements carrying them can be checked as well, *e.g.*, through iterating over
`get_nodes(case)`. Checks that depend only on the type of an element belong in the check
functions of the package introducing the element type ([`check_node`](@ref),
[`check_node_data`](@ref), [`check_link`](@ref)).
Check the resources `𝒫ˢᵘᵇ` of a single resource type segment ([`res_types_vec`](@ref)) of
the `case`.
The default method does not check anything; extension packages that introduce resource types
provide methods for their resource types.

Comment thread NEWS.md
Comment on lines +5 to +8
* Added the function `check_resources(case, 𝒫ˢᵘᵇ, modeltype, check_timeprofiles)`, called from `check_case_data` for every resource family of the case products.
* The default method does not check anything.
* Extension packages that introduce resource types can provide methods for checking the resource parameters and the combination of the resources with the elements carrying them.
* `check_case_data` has the additional arguments `modeltype` and `check_timeprofiles`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Added the function `check_resources(case, 𝒫ˢᵘᵇ, modeltype, check_timeprofiles)`, called from `check_case_data` for every resource family of the case products.
* The default method does not check anything.
* Extension packages that introduce resource types can provide methods for checking the resource parameters and the combination of the resources with the elements carrying them.
* `check_case_data` has the additional arguments `modeltype` and `check_timeprofiles`.
* Added the function `check_resources(case, 𝒫ˢᵘᵇ, modeltype, check_timeprofiles)` for every resource family of the case products.
The default method does not check anything.

Comment thread test/test_checks.jl
end

# Function for setting up the system
function resource_graph(p_checked)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename the function resource_graph to check_graph_res as we only use it for testing the checks. That requires adjustments in all @test_throws calls.

Comment thread test/test_checks.jl
Comment on lines +1012 to +1030
# A positive parameter leaves the node check, which still fails
case, model = resource_graph(CheckedResource("checked", 0.0, 1.0))
@test_throws AssertionError create_model(case, model)

# The checks are not run if the resource is not included in the case products
case, model = resource_graph(CheckedResource("checked", 0.0, -1.0))
CO2 = get_products(case)[2]
case_wo = Case(
get_time_struct(case),
[CO2],
[get_nodes(case), get_links(case)],
[[get_nodes, get_links]],
)
@test create_model(case_wo, model) isa JuMP.Model

# A resource without a method is not affected
Power = ResourceCarrier("Power", 0.0)
case, model = resource_graph(Power)
@test create_model(case, model) isa JuMP.Model

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are not relevant. We only have to test that the function you created is actually called. However, l. 1025 points towards another issue, i.e., that a resource in the current stage only must be included in the products field if and only if it is a ResourceEmit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

check_resource and check_resources hooks

3 participants