Bugfix + first steps on restart

This commit is contained in:
Vegard Kippe 2023-10-24 16:34:50 +02:00
parent aa2220979b
commit 01890670da
5 changed files with 34 additions and 30 deletions

View File

@ -39,11 +39,12 @@ public:
bool active() const;
void add_branch(Branch branch);
void add_or_replace_branch(Branch branch);
void drop_branch(const std::string& uptree_node, const std::string& downtree_node, const bool recurse = true);
void drop_branch(const std::string& uptree_node, const std::string& downtree_node);
bool has_node(const std::string& name) const;
void update_node(Node node);
const Node& node(const std::string& name) const;
const Node& root() const;
bool is_disconnected(const std::string& node) const;
std::vector<Branch> downtree_branches(const std::string& node) const;
std::vector<const Branch*> branches() const;
std::optional<Branch> uptree_branch(const std::string& node) const;

View File

@ -180,9 +180,9 @@ namespace {
if (alq_eq == Network::Branch::AlqEQ::ALQ_INPUT) {
double alq_value = record.getItem<ParserKeywords::BRANPROP::ALQ>().get<double>(0);
ext_network.add_branch(Network::Branch(downtree_node, uptree_node, vfp_table, alq_value));
ext_network.add_or_replace_branch(Network::Branch(downtree_node, uptree_node, vfp_table, alq_value));
} else {
ext_network.add_branch(Network::Branch(downtree_node, uptree_node, vfp_table, alq_eq));
ext_network.add_or_replace_branch(Network::Branch(downtree_node, uptree_node, vfp_table, alq_eq));
}
}
}

View File

@ -101,14 +101,14 @@ void ExtNetwork::add_or_replace_branch(Branch branch)
}
}
// Remove any other branch uptree from downtree_node
// Remove any other branch uptree from downtree_node (gathering tree structure required)
auto uptree_link = this->uptree_branch( downtree_node );
if (uptree_link.has_value()){
const auto& old_uptree_node = uptree_link.value().uptree_node();
this->drop_branch(old_uptree_node, downtree_node, false);
this->drop_branch(old_uptree_node, downtree_node);
}
// Update existing branch
// Update existing branch if it exists
auto downtree_branches = this->downtree_branches( uptree_node );
if (!downtree_branches.empty()) {
auto branch_iter = std::find_if(this->m_branches.begin(), this->m_branches.end(), [&uptree_node, &downtree_node](const Branch& b) { return (b.uptree_node() == uptree_node && b.downtree_node() == downtree_node); });
@ -121,25 +121,30 @@ void ExtNetwork::add_or_replace_branch(Branch branch)
this->m_branches.push_back( std::move(branch) );
}
bool ExtNetwork::is_disconnected(const std::string& node_name) const {
if (!this->has_node(node_name)) return false;
void ExtNetwork::drop_branch(const std::string& uptree_node, const std::string& downtree_node, const bool recurse) {
auto downtree_branches = this->downtree_branches( downtree_node );
if (downtree_branches.empty() || !recurse) {
auto branch_iter = std::find_if(this->m_branches.begin(), this->m_branches.end(), [&uptree_node, &downtree_node](const Branch& b) { return (b.uptree_node() == uptree_node && b.downtree_node() == downtree_node); });
if (branch_iter != this->m_branches.end())
this->m_branches.erase( branch_iter );
if (this->downtree_branches(node_name).empty()) {
for (const auto& branch : this->m_branches)
if (branch.downtree_node() == node_name) return false;
if (downtree_branches.empty()) this->m_nodes.erase( downtree_node );
} else {
for (const auto& branch : downtree_branches)
this->drop_branch(branch.uptree_node(), branch.downtree_node());
return true;
}
return false;
}
void ExtNetwork::drop_branch(const std::string& uptree_node, const std::string& downtree_node) {
auto branch_iter = std::find_if(this->m_branches.begin(),
this->m_branches.end(),
[&uptree_node, &downtree_node](const Branch& b) { return (b.uptree_node() == uptree_node && b.downtree_node() == downtree_node); });
if (branch_iter != this->m_branches.end()) {
this->m_branches.erase(branch_iter);
}
}
std::optional<Branch> ExtNetwork::uptree_branch(const std::string& node) const {
if (!this->has_node(node))
throw std::out_of_range("No such node: " + node);
if (!this->has_node(node)) return {};
std::vector<Branch> branches;
std::copy_if(this->m_branches.begin(), this->m_branches.end(), std::back_inserter(branches), [&node](const Branch& b) { return b.downtree_node() == node; });
@ -149,15 +154,15 @@ std::optional<Branch> ExtNetwork::uptree_branch(const std::string& node) const {
if (branches.size() == 1)
return std::move(branches[0]);
throw std::logic_error("Bug - more than upstree branch for node: " + node);
throw std::logic_error("Bug - more than uptree branch for node: " + node);
}
std::vector<Branch> ExtNetwork::downtree_branches(const std::string& node) const {
if (!this->has_node(node))
throw std::out_of_range("No such node: " + node);
std::vector<Branch> branches;
if (!this->has_node(node)) return branches;
std::copy_if(this->m_branches.begin(), this->m_branches.end(), std::back_inserter(branches), [&node](const Branch& b) { return b.uptree_node() == node; });
return branches;
}
@ -197,10 +202,6 @@ void ExtNetwork::update_node(Node node)
auto branch = std::find_if(this->m_branches.begin(), this->m_branches.end(),
[&name](const Branch& b) { return b.uptree_node() == name || b.downtree_node() == name;});
if (branch == this->m_branches.end())
throw std::invalid_argument("Node: " + name + " is not referenced by any branch and would be dangling.");
if (branch->downtree_node() == name) {
if (node.as_choke() && branch->vfp_table().has_value())
throw std::invalid_argument("Node: " + name + " should serve as a choke => upstream branch can not have VFP table");

View File

@ -1404,6 +1404,7 @@ File {} line {}.)", pattern, location.keyword, location.filename, location.linen
auto new_branch = old_branch.value();
new_branch.set_uptree_node(parent_name);
network.add_or_replace_branch(new_branch);
this->snapshots.back().network.update( std::move(network));
}
// If no previous uptree branch the child is a fixed-pressure node, so no need to update network
}

View File

@ -233,6 +233,9 @@ double nodePressure(const Opm::Schedule& sched,
// find fixed pressure higher in the node tree
bool fp_flag = false;
auto node_name = nodeName;
auto upt_br_opt = network.uptree_branch(node_name);
if (!upt_br_opt.has_value()) return 0.0; // Node not belonging to the network right now
auto upt_br = network.uptree_branch(node_name).value();
while (!fp_flag) {
if (fixedPressureNode(sched, upt_br.uptree_node(), lookup_step)) {
@ -243,8 +246,7 @@ double nodePressure(const Opm::Schedule& sched,
if (network.uptree_branch(node_name).has_value()) {
upt_br = network.uptree_branch(node_name).value();
} else {
auto msg = fmt::format("Node: {} has no uptree node with fixed pressure condition, uppermost node is: {} ", nodeName, node_name);
throw std::logic_error(msg);
return 0.0; // Subtree not belonging to the network right now
}
}
}
@ -407,8 +409,7 @@ int numberOfBranchesConnToNode(const Opm::Schedule& sched, const std::string& no
noBranches = (network.uptree_branch(nodeName).has_value()) ? noBranches+1 : noBranches;
return noBranches;
} else {
auto msg = fmt::format("Actual node: {} has not been defined at report time: {} ", nodeName, lookup_step+1);
throw std::logic_error(msg);
return 0; // @TODO - Should number of branches in inactive subtree be reported?
}
}