-
Notifications
You must be signed in to change notification settings - Fork 125
Add network topology validation at parse time #5313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ElyesAhmed
wants to merge
2
commits into
OPM:master
Choose a base branch
from
ElyesAhmed:network-input-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
242 changes: 242 additions & 0 deletions
242
opm/input/eclipse/Schedule/Network/NetworkValidation.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| /* | ||
| Copyright 2026 Equinor ASA. | ||
|
|
||
| This file is part of the Open Porous Media project (OPM). | ||
|
|
||
| OPM is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| OPM is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with OPM. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include "NetworkValidation.hpp" | ||
|
|
||
| #include <opm/common/OpmLog/KeywordLocation.hpp> | ||
|
|
||
| #include <opm/input/eclipse/Schedule/Network/Branch.hpp> | ||
| #include <opm/input/eclipse/Schedule/Network/ExtNetwork.hpp> | ||
| #include <opm/input/eclipse/Schedule/Network/Node.hpp> | ||
|
|
||
| #include <opm/input/eclipse/Parser/ParseContext.hpp> | ||
|
|
||
| #include <algorithm> | ||
| #include <functional> | ||
| #include <iterator> | ||
| #include <set> | ||
| #include <string> | ||
| #include <unordered_set> | ||
| #include <vector> | ||
|
|
||
| #include <fmt/format.h> | ||
|
|
||
| namespace { | ||
|
|
||
| /// Names of those nodes which take part in the network. | ||
| /// | ||
| /// A node is part of the network only for as long as it is attached to | ||
| /// at least one branch. A node mentioned in NODEPROP alone, or a node | ||
| /// whose only branch has been removed by a BRANPROP entry with a zero | ||
| /// VFP table number, is therefore not included here. | ||
| /// | ||
| /// \param[in] network Extended network. | ||
| /// | ||
| /// \return Names of the network's nodes, in order of first appearance | ||
| /// in the input. | ||
| std::vector<std::string> | ||
| connectedNodes(const Opm::Network::ExtNetwork& network) | ||
| { | ||
| auto connected = std::set<std::string>{}; | ||
| for (const auto* branch : network.branches()) { | ||
| connected.insert(branch->uptree_node()); | ||
| connected.insert(branch->downtree_node()); | ||
| } | ||
|
|
||
| auto nodes = std::vector<std::string>{}; | ||
| nodes.reserve(connected.size()); | ||
|
|
||
| std::ranges::copy_if(network.node_names(), std::back_inserter(nodes), | ||
| [&connected](const std::string& node) | ||
| { return connected.contains(node); }); | ||
|
|
||
| return nodes; | ||
| } | ||
|
|
||
| /// Whether or not a node acts as a source for the network. | ||
| /// | ||
| /// Flow enters a node through its downtree branches, so a node without | ||
| /// downtree branches has no inlets and must supply its own flow rate. | ||
| /// | ||
| /// \param[in] network Extended network. | ||
| /// | ||
| /// \param[in] node Node name. Must be a node of \p network. | ||
| /// | ||
| /// \return Whether or not \p node has any inlets. | ||
| bool isSource(const Opm::Network::ExtNetwork& network, | ||
| const std::string& node) | ||
| { | ||
| return network.downtree_branches(node).empty(); | ||
| } | ||
|
|
||
| /// Report a network inconsistency. | ||
| /// | ||
| /// \param[in] description Description of the inconsistency. Must not | ||
| /// contain any brace characters. | ||
| /// | ||
| /// \param[in] location Location of the keyword which prompted the | ||
| /// check. Included in the diagnostic by the error handling protocol. | ||
| /// | ||
| /// \param[in] parseContext Error handling controls. | ||
| /// | ||
| /// \param[in,out] errors Collection of parse errors. | ||
| void reportInconsistency(const std::string& description, | ||
| const Opm::KeywordLocation& location, | ||
| const Opm::ParseContext& parseContext, | ||
| Opm::ErrorGuard& errors) | ||
| { | ||
| parseContext.handleError(Opm::ParseContext::SCHEDULE_NETWORK_INVALID, | ||
| description, location, errors); | ||
| } | ||
|
|
||
| /// Check that every source node is also a group. | ||
| /// | ||
| /// A source node gets its flow rate from the wells of the group of the | ||
| /// same name. A source node which is not a group has no way of | ||
| /// contributing to the network. | ||
| /// | ||
| /// \param[in] network Extended network. | ||
| /// | ||
| /// \param[in] nodes Names of the network's nodes. | ||
| /// | ||
| /// \param[in] isGroup Predicate returning whether or not a name is that | ||
| /// of a group. | ||
| /// | ||
| /// \param[in] location Location of the keyword which prompted this | ||
| /// check. | ||
| /// | ||
| /// \param[in] parseContext Error handling controls. | ||
| /// | ||
| /// \param[in,out] errors Collection of parse errors. | ||
| void checkSourcesAreGroups(const Opm::Network::ExtNetwork& network, | ||
| const std::vector<std::string>& nodes, | ||
| const std::function<bool(const std::string&)>& isGroup, | ||
| const Opm::KeywordLocation& location, | ||
| const Opm::ParseContext& parseContext, | ||
| Opm::ErrorGuard& errors) | ||
| { | ||
| for (const auto& node : nodes) { | ||
| if (!isSource(network, node) || isGroup(node)) { | ||
| continue; | ||
| } | ||
|
|
||
| reportInconsistency(fmt::format("Network node {0} has no downtree branches, and must " | ||
| "therefore supply the network on its own, but no " | ||
| "group {0} exists.", node), | ||
| location, parseContext, errors); | ||
| } | ||
| } | ||
|
|
||
| /// Check that every flow path ends in a fixed pressure node. | ||
| /// | ||
| /// Follows the flow path uptree from each node. The pressure drop along | ||
| /// the path can be computed only if the path ends in a node of known | ||
| /// pressure. | ||
| /// | ||
| /// Flow paths merge on their way uptree, so a single problem is shared | ||
| /// by every node downtree of it. To report each problem only once we | ||
| /// stop tracing a path as soon as it reaches a node which some earlier | ||
| /// path already passed through. | ||
| /// | ||
| /// \param[in] network Extended network. | ||
| /// | ||
| /// \param[in] nodes Names of the network's nodes. | ||
| /// | ||
| /// \param[in] location Location of the keyword which prompted this | ||
| /// check. | ||
| /// | ||
| /// \param[in] parseContext Error handling controls. | ||
| /// | ||
| /// \param[in,out] errors Collection of parse errors. | ||
| void checkFlowPathsAreTerminated(const Opm::Network::ExtNetwork& network, | ||
| const std::vector<std::string>& nodes, | ||
| const Opm::KeywordLocation& location, | ||
| const Opm::ParseContext& parseContext, | ||
| Opm::ErrorGuard& errors) | ||
| { | ||
| // Nodes whose flow path has been traced already, either to a fixed | ||
| // pressure node or to a problem which has been reported. | ||
| auto traced = std::unordered_set<std::string>{}; | ||
|
|
||
| for (const auto& start : nodes) { | ||
| if (traced.contains(start)) { | ||
| continue; | ||
| } | ||
|
|
||
| auto path = std::vector<std::string> { start }; | ||
| auto onPath = std::unordered_set<std::string> { start }; | ||
|
|
||
| auto node = start; | ||
| while (! network.node(node).terminal_pressure().has_value()) { | ||
| const auto uptree = network.uptree_branch(node); | ||
|
|
||
| if (! uptree.has_value()) { | ||
| reportInconsistency(fmt::format("Flow path from network node {} terminates in " | ||
| "node {}, which has neither an uptree branch " | ||
| "nor a fixed pressure.", start, node), | ||
| location, parseContext, errors); | ||
| break; | ||
| } | ||
|
|
||
| node = uptree->uptree_node(); | ||
|
|
||
| if (traced.contains(node)) { | ||
| // Path merges into one which has been traced already. | ||
| // Whether that path ends well or not, there is nothing | ||
| // new to report here. | ||
| break; | ||
| } | ||
|
|
||
| if (! onPath.insert(node).second) { | ||
| // Cycle in the branch definitions. Stop here to avoid | ||
| // looping forever--this path will never reach a fixed | ||
| // pressure node. | ||
| reportInconsistency(fmt::format("Flow path from network node {} returns to " | ||
| "node {}: the branches form a loop.", | ||
| start, node), | ||
| location, parseContext, errors); | ||
| break; | ||
| } | ||
|
|
||
| path.push_back(node); | ||
| } | ||
|
|
||
| traced.insert(path.begin(), path.end()); | ||
| } | ||
| } | ||
|
|
||
| } // Anonymous namespace | ||
|
|
||
| void Opm::Network::validateTopology(const ExtNetwork& network, | ||
| const std::function<bool(const std::string&)>& isGroup, | ||
| const KeywordLocation& location, | ||
| const ParseContext& parseContext, | ||
| ErrorGuard& errors) | ||
| { | ||
| if (!network.active() || network.is_standard_network()) { | ||
| // Nothing to check, or a standard network (GRUPNET) whose nodes are | ||
| // groups by construction. | ||
| return; | ||
| } | ||
|
|
||
| const auto nodes = connectedNodes(network); | ||
|
|
||
| checkSourcesAreGroups(network, nodes, isGroup, location, parseContext, errors); | ||
| checkFlowPathsAreTerminated(network, nodes, location, parseContext, errors); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| Copyright 2026 Equinor ASA. | ||
|
|
||
| This file is part of the Open Porous Media project (OPM). | ||
|
|
||
| OPM is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| OPM is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with OPM. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #ifndef NETWORK_VALIDATION_HPP | ||
| #define NETWORK_VALIDATION_HPP | ||
|
|
||
| #include <functional> | ||
| #include <string> | ||
|
|
||
| namespace Opm { | ||
| class ErrorGuard; | ||
| class KeywordLocation; | ||
| class ParseContext; | ||
| } // namespace Opm | ||
|
|
||
| namespace Opm::Network { | ||
|
|
||
| class ExtNetwork; | ||
|
|
||
| /// Check that the topology of an extended network is internally consistent. | ||
| /// | ||
| /// The network cannot be balanced unless every source--i.e., every node | ||
| /// without inlets--supplies a flow rate, and unless every flow path ends in | ||
| /// a node of known pressure. Consequently, this function reports, through | ||
| /// the normal error handling protocol, | ||
| /// | ||
| /// -# any node without inlets which is not also a group, and | ||
| /// | ||
| /// -# any flow path which does not end in a fixed pressure node. | ||
| /// | ||
| /// Nodes which are no longer attached to a branch--e.g., because a BRANPROP | ||
| /// entry with a zero VFP table number removed the node's only branch--are | ||
| /// not part of the network and are therefore not checked. Neither are | ||
| /// standard networks (GRUPNET), the nodes of which are groups by | ||
| /// construction. | ||
| /// | ||
| /// \param[in] network Extended network, typically that of the current | ||
| /// report step's schedule state. | ||
| /// | ||
| /// \param[in] isGroup Predicate returning whether or not a name is that of | ||
| /// a group at the current report step. | ||
| /// | ||
| /// \param[in] location Location of the keyword which prompted this check. | ||
| /// | ||
| /// \param[in] parseContext Error handling controls. | ||
| /// | ||
| /// \param[in,out] errors Collection of parse errors. | ||
| void validateTopology(const ExtNetwork& network, | ||
| const std::function<bool(const std::string&)>& isGroup, | ||
| const KeywordLocation& location, | ||
| const ParseContext& parseContext, | ||
| ErrorGuard& errors); | ||
|
|
||
| } // namespace Opm::Network | ||
|
|
||
| #endif // NETWORK_VALIDATION_HPP |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.