Loading...
Searching...
No Matches
UDQConfig.hpp
/*
Copyright 2019 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 UDQINPUT_HPP_
#define UDQINPUT_HPP_
#include <opm/input/eclipse/Schedule/UDQ/UDQAssign.hpp>
#include <opm/input/eclipse/Schedule/UDQ/UDQDefine.hpp>
#include <opm/input/eclipse/Schedule/UDQ/UDQEnums.hpp>
#include <opm/input/eclipse/Schedule/UDQ/UDQFunctionTable.hpp>
#include <opm/input/eclipse/Schedule/UDQ/UDQInput.hpp>
#include <opm/input/eclipse/Schedule/UDQ/UDQParams.hpp>
#include <opm/input/eclipse/Schedule/UDQ/UDT.hpp>
#include <opm/input/eclipse/EclipseState/Util/OrderedMap.hpp>
#include <opm/input/eclipse/EclipseState/Util/IOrderSet.hpp>
#include <array>
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
namespace Opm {
class DeckRecord;
class KeywordLocation;
class GroupOrder;
class RegionSetMatcher;
class Schedule;
class SegmentMatcher;
class SummaryState;
class UDQState;
class WellMatcher;
} // namespace Opm
namespace Opm::RestartIO {
struct RstState;
class RstUDQ;
} // namespace Opm::RestartIO
namespace Opm {
class UDQConfig
{
public:
class DynamicSelector
{
public:
class StringRange
{
public:
using Iter = std::vector<std::string>::const_iterator;
StringRange() = default;
StringRange(Iter first, Iter last)
: first_ { first }
, last_ { last }
{}
auto begin() const { return this->first_; }
auto end() const { return this->last_; }
std::vector<std::string> asVector() const
{
return { this->begin(), this->end() };
}
private:
Iter first_{};
Iter last_{};
};
DynamicSelector() = default;
DynamicSelector& wells(std::vector<std::string>::const_iterator first,
std::vector<std::string>::const_iterator last)
{
this->wells_.emplace(first, last);
return *this;
}
DynamicSelector& wells(const StringRange& srange)
{
this->wells_.emplace(srange);
return *this;
}
const std::optional<StringRange> wells() const
{
return this->wells_;
}
private:
std::optional<StringRange> wells_{};
};
using RegionSetMatcherFactory = std::function<std::unique_ptr<RegionSetMatcher>()>;
using SegmentMatcherFactory = std::function<std::unique_ptr<SegmentMatcher>()>;
UDQConfig() = default;
explicit UDQConfig(const UDQParams& params);
UDQConfig(const UDQParams& params, const RestartIO::RstState& rst_state);
static UDQConfig serializationTestObject();
const std::string& unit(const std::string& key) const;
bool has_unit(const std::string& keyword) const;
bool has_keyword(const std::string& keyword) const;
std::optional<std::string>
add_record(SegmentMatcherFactory create_segment_matcher,
const DeckRecord& record,
const KeywordLocation& location,
std::size_t report_step,
const std::optional<DynamicSelector>& dynamic_selector = std::nullopt);
void add_unit(const std::string& keyword,
const std::string& unit);
void add_update(const std::string& keyword,
std::size_t report_step,
const KeywordLocation& location,
const std::vector<std::string>& data);
void add_assign(const std::string& quantity,
SegmentMatcherFactory create_segment_matcher,
const std::vector<std::string>& selector,
double value,
std::size_t report_step,
const std::optional<DynamicSelector>& dynamic_selector = std::nullopt);
void add_define(const std::string& quantity,
const KeywordLocation& location,
const std::vector<std::string>& expression,
std::size_t report_step);
void add_table(const std::string& name, UDT udt);
bool clear_pending_assignments();
bool clear_update_next_for_new_report_step();
void eval_assign(const WellMatcher& wm,
const GroupOrder& go,
SegmentMatcherFactory create_segment_matcher,
SummaryState& st,
UDQState& udq_state) const;
void eval(std::size_t report_step,
const WellMatcher& wm,
const GroupOrder& go,
SegmentMatcherFactory create_segment_matcher,
RegionSetMatcherFactory create_region_matcher,
SummaryState& st,
UDQState& udq_state) const;
const UDQDefine& define(const std::string& key) const;
const UDQAssign& assign(const std::string& key) const;
std::vector<UDQDefine> definitions() const;
std::vector<UDQDefine> definitions(UDQVarType var_type) const;
std::vector<UDQInput> input() const;
void exportTypeCount(std::array<int, static_cast<std::size_t>(UDQVarType::NumTypes)>& count) const;
std::size_t size() const;
UDQInput operator[](const std::string& keyword) const;
UDQInput operator[](std::size_t insert_index) const;
std::vector<UDQAssign> assignments() const;
std::vector<UDQAssign> assignments(UDQVarType var_type) const;
const UDQParams& params() const;
const UDQFunctionTable& function_table() const;
const std::unordered_map<std::string, UDT>& tables() const;
bool operator==(const UDQConfig& config) const;
void required_summary(std::unordered_set<std::string>& summary_keys) const;
template<class Serializer>
void serializeOp(Serializer& serializer)
{
serializer(udq_params);
serializer(m_definitions);
serializer(m_assignments);
serializer(m_tables);
serializer(units);
serializer(define_order);
serializer(input_index);
serializer(type_count);
serializer(pending_assignments_);
// The UDQFunction table is constant up to udq_params, so we can
// just construct a new instance here.
if (!serializer.isSerializing()) {
udqft = UDQFunctionTable(udq_params);
}
}
private:
UDQParams udq_params;
UDQFunctionTable udqft;
// The following data structures are constrained by compatibility
// requirements in our simulation restart files. In particular we
// need to control the keyword ordering. In this class the ordering
// is maintained mainly by the input_index map which tracks the
// insertion order of each keyword and whether the keyword (UDQ
// name) is currently DEFINE'ed or ASSIGN'ed.
std::unordered_map<std::string, UDQDefine> m_definitions{};
std::unordered_map<std::string, UDQAssign> m_assignments{};
std::unordered_map<std::string, UDT> m_tables{};
std::unordered_map<std::string, std::string> units{};
IOrderSet<std::string> define_order;
OrderedMap<UDQIndex> input_index;
std::map<UDQVarType, std::size_t> type_count{};
mutable std::vector<std::string> pending_assignments_{};
void add_node(const std::string& quantity, UDQAction action);
void add_assign(const RestartIO::RstUDQ& udq, const std::size_t report_step);
void add_define(const RestartIO::RstUDQ& udq, const std::size_t report_step);
void eval_assign(UDQContext& context) const;
void eval_define(std::size_t report_step,
const UDQState& udq_state,
UDQContext& context) const;
void add_enumerated_assign(const std::string& quantity,
SegmentMatcherFactory create_segment_matcher,
const std::vector<std::string>& selector,
double value,
std::size_t report_step);
void add_assign_wellvar(const std::string& quantity,
const std::vector<std::string>& selector,
double value,
std::size_t report_step,
const std::optional<DynamicSelector>& dynamic_selector);
template <typename... Args>
void add_assign_impl(const std::string& quantity,
Args&&... args)
{
// Note: Duplicate 'quantity' arguments is intentional here.
// The first is the key in 'm_assignments', while the second is
// the first argument to the UDQAssign constructor.
const auto& [asgnPos, inserted] =
this->m_assignments.try_emplace(quantity, quantity, args...);
if (! inserted) {
// We already have an assignment object for 'quantity'. Add
// a new assignment record to that object.
asgnPos->second.add_record(std::forward<Args>(args)...);
}
}
};
} // namespace Opm
#endif // UDQINPUT_HPP_
Opm::DeckRecord
Definition DeckRecord.hpp:32
Opm::GroupOrder
Collection of group names with built-in ordering.
Definition NameOrder.hpp:69
Opm::KeywordLocation
Definition KeywordLocation.hpp:27
Opm::Schedule
Definition Schedule.hpp:101
Opm::Serializer
Class for (de-)serializing.
Definition Serializer.hpp:94
Opm::Serializer::isSerializing
bool isSerializing() const
Returns true if we are currently doing a serialization operation.
Definition Serializer.hpp:207
Opm::SummaryState
Definition SummaryState.hpp:72
Opm::UDQAssign
Representation of a UDQ ASSIGN statement.
Definition UDQAssign.hpp:41
Opm::UDQConfig::DynamicSelector::StringRange
Random access range of string values.
Definition UDQConfig.hpp:82
Opm::UDQConfig::DynamicSelector::StringRange::StringRange
StringRange(Iter first, Iter last)
Constructor.
Definition UDQConfig.hpp:103
Opm::UDQConfig::DynamicSelector::StringRange::StringRange
StringRange()=default
Default constructor.
Opm::UDQConfig::DynamicSelector::StringRange::end
auto end() const
End of range's elements.
Definition UDQConfig.hpp:112
Opm::UDQConfig::DynamicSelector::StringRange::asVector
std::vector< std::string > asVector() const
Convert value range to a std::vector.
Definition UDQConfig.hpp:117
Opm::UDQConfig::DynamicSelector::StringRange::Iter
std::vector< std::string >::const_iterator Iter
Random access iterator.
Definition UDQConfig.hpp:88
Opm::UDQConfig::DynamicSelector::StringRange::begin
auto begin() const
Beginning of range's elements.
Definition UDQConfig.hpp:109
Opm::UDQConfig::DynamicSelector::wells
DynamicSelector & wells(const StringRange &srange)
Include a range of well names into the dynamic selector.
Definition UDQConfig.hpp:155
Opm::UDQConfig::DynamicSelector::wells
const std::optional< StringRange > wells() const
Retrieve current dynamic well set.
Definition UDQConfig.hpp:164
Opm::UDQConfig::DynamicSelector::wells
DynamicSelector & wells(std::vector< std::string >::const_iterator first, std::vector< std::string >::const_iterator last)
Include a range of well names into the dynamic selector.
Definition UDQConfig.hpp:143
Opm::UDQConfig::DynamicSelector::DynamicSelector
DynamicSelector()=default
Default constructor.
Opm::UDQConfig::size
std::size_t size() const
Total number of active DEFINE and ASSIGN statements.
Definition UDQConfig.cpp:552
Opm::UDQConfig::define
const UDQDefine & define(const std::string &key) const
Retrieve defining expression and evaluation object for a single UDQ.
Definition UDQConfig.cpp:485
Opm::UDQConfig::params
const UDQParams & params() const
Retrieve run's active UDQ parameters.
Definition UDQConfig.cpp:642
Opm::UDQConfig::add_assign
void add_assign(const std::string &quantity, SegmentMatcherFactory create_segment_matcher, const std::vector< std::string > &selector, double value, std::size_t report_step, const std::optional< DynamicSelector > &dynamic_selector=std::nullopt)
Incorporate a UDQ assignment.
Definition UDQConfig.cpp:370
Opm::UDQConfig::eval_assign
void eval_assign(const WellMatcher &wm, const GroupOrder &go, SegmentMatcherFactory create_segment_matcher, SummaryState &st, UDQState &udq_state) const
Apply all pending assignments.
Definition UDQConfig.cpp:447
Opm::UDQConfig::serializationTestObject
static UDQConfig serializationTestObject()
Create a serialisation test object.
Definition UDQConfig.cpp:247
Opm::UDQConfig::eval
void eval(std::size_t report_step, const WellMatcher &wm, const GroupOrder &go, SegmentMatcherFactory create_segment_matcher, RegionSetMatcherFactory create_region_matcher, SummaryState &st, UDQState &udq_state) const
Compute new values for all UDQs.
Definition UDQConfig.cpp:464
Opm::UDQConfig::serializeOp
void serializeOp(Serializer &serializer)
Convert between byte array and object representation.
Definition UDQConfig.hpp:541
Opm::UDQConfig::tables
const std::unordered_map< std::string, UDT > & tables() const
Retrieve run's active user defined tables.
Definition UDQConfig.cpp:652
Opm::UDQConfig::add_define
void add_define(const std::string &quantity, const KeywordLocation &location, const std::vector< std::string > &expression, std::size_t report_step)
Incorporate a UDQ defining expressions.
Definition UDQConfig.cpp:402
Opm::UDQConfig::UDQConfig
UDQConfig()=default
Default constructor.
Opm::UDQConfig::exportTypeCount
void exportTypeCount(std::array< int, static_cast< std::size_t >(UDQVarType::NumTypes)> &count) const
Export count of all known UDQ categories in the current run.
Definition UDQConfig.cpp:543
Opm::UDQConfig::clear_update_next_for_new_report_step
bool clear_update_next_for_new_report_step()
Clear "UPDATE NEXT" flags for all pertinent UDQ definitions.
Definition UDQConfig.cpp:435
Opm::UDQConfig::add_unit
void add_unit(const std::string &keyword, const std::string &unit)
Incorporate a unit string for a UDQ.
Definition UDQConfig.cpp:325
Opm::UDQConfig::has_keyword
bool has_keyword(const std::string &keyword) const
Query whether or not a particular UDQ exists in the collection.
Definition UDQConfig.cpp:278
Opm::UDQConfig::add_record
std::optional< std::string > add_record(SegmentMatcherFactory create_segment_matcher, const DeckRecord &record, const KeywordLocation &location, std::size_t report_step, const std::optional< DynamicSelector > &dynamic_selector=std::nullopt)
Incorporate a single UDQ record into the known collection.
Definition UDQConfig.cpp:285
Opm::UDQConfig::definitions
std::vector< UDQDefine > definitions() const
Retrieve defining expressions and evaluation objects for all known UDQs.
Definition UDQConfig.cpp:495
Opm::UDQConfig::RegionSetMatcherFactory
std::function< std::unique_ptr< RegionSetMatcher >()> RegionSetMatcherFactory
Factory function for constructing region set matchers.
Definition UDQConfig.hpp:177
Opm::UDQConfig::assignments
std::vector< UDQAssign > assignments() const
Retrieve pending assignment objects for all known UDQs.
Definition UDQConfig.cpp:613
Opm::UDQConfig::clear_pending_assignments
bool clear_pending_assignments()
Clear all pending assignments.
Definition UDQConfig.cpp:426
Opm::UDQConfig::has_unit
bool has_unit(const std::string &keyword) const
Query whether or not a particular UDQ has an associated unit string.
Definition UDQConfig.cpp:273
Opm::UDQConfig::add_update
void add_update(const std::string &keyword, std::size_t report_step, const KeywordLocation &location, const std::vector< std::string > &data)
Incorporate update status change for a UDQ.
Definition UDQConfig.cpp:340
Opm::UDQConfig::add_table
void add_table(const std::string &name, UDT udt)
Incorporate a user defined table.
Definition UDQConfig.cpp:421
Opm::UDQConfig::assign
const UDQAssign & assign(const std::string &key) const
Retrieve any pending assignment object for a single UDQ.
Definition UDQConfig.cpp:490
Opm::UDQConfig::operator[]
UDQInput operator[](const std::string &keyword) const
Unprocessed input object for named quantity.
Definition UDQConfig.cpp:564
Opm::UDQConfig::unit
const std::string & unit(const std::string &key) const
Retrieve unit string for a particular UDQ.
Definition UDQConfig.cpp:263
Opm::UDQConfig::SegmentMatcherFactory
std::function< std::unique_ptr< SegmentMatcher >()> SegmentMatcherFactory
Factory function for constructing segment set matchers.
Definition UDQConfig.hpp:180
Opm::UDQConfig::input
std::vector< UDQInput > input() const
Retrieve unprocessed input objects for all UDQs.
Definition UDQConfig.cpp:524
Opm::UDQConfig::required_summary
void required_summary(std::unordered_set< std::string > &summary_keys) const
Export all summary vectors needed to compute values for the current collection of user defined quanti...
Definition UDQConfig.cpp:672
Opm::UDQConfig::operator==
bool operator==(const UDQConfig &config) const
Equality predicate.
Definition UDQConfig.cpp:657
Opm::UDQConfig::function_table
const UDQFunctionTable & function_table() const
Retrieve run's active UDQ function table.
Definition UDQConfig.cpp:647
Opm::UDQDefine
Definition UDQDefine.hpp:51
Opm::UDQFunctionTable
Definition UDQFunctionTable.hpp:32
Opm::UDQInput
Definition UDQInput.hpp:84
Opm::UDQParams
Definition UDQParams.hpp:31
Opm::UDQState
Definition UDQState.hpp:40
Opm::UDT
Definition UDT.hpp:30
Opm
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30
Opm::RestartIO::RstState
Definition state.hpp:57