2021-05-05 03:27:51 -05:00
|
|
|
/*
|
|
|
|
Copyright 2021 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 OPM_WELL_CONTAINER_HEADER_INCLUDED
|
|
|
|
#define OPM_WELL_CONTAINER_HEADER_INCLUDED
|
|
|
|
|
2021-05-12 00:15:34 -05:00
|
|
|
#include <initializer_list>
|
2023-01-24 03:19:23 -06:00
|
|
|
#include <optional>
|
2021-05-05 03:27:51 -05:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace Opm {
|
|
|
|
|
2021-05-09 04:54:45 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
The WellContainer<T> class is a small utility class designed to manage the
|
|
|
|
dynamic state of per well quantities, like active control and phase rates. The
|
|
|
|
values are stored continously in a vector, but they are added with a name, and
|
|
|
|
can also be accessed and updated with the name.
|
|
|
|
|
|
|
|
The class is created to facilitate safe and piecewise refactoring of the
|
2021-05-20 06:32:18 -05:00
|
|
|
WellState class, and might have a short life in the
|
2021-05-09 04:54:45 -05:00
|
|
|
development timeline.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-05-05 03:27:51 -05:00
|
|
|
template <class T>
|
|
|
|
class WellContainer {
|
|
|
|
public:
|
2021-05-12 00:15:34 -05:00
|
|
|
WellContainer() = default;
|
|
|
|
|
|
|
|
WellContainer(std::initializer_list<std::pair<std::string,T>> init_list) {
|
|
|
|
for (const auto& [name, value] : init_list)
|
|
|
|
this->add(name, value);
|
|
|
|
}
|
|
|
|
|
2023-02-02 04:52:08 -06:00
|
|
|
static WellContainer serializationTestObject(const T& data)
|
|
|
|
{
|
|
|
|
WellContainer<T> result;
|
|
|
|
|
|
|
|
result.m_data = {data};
|
|
|
|
result.index_map = {{"test1", 1}, {"test2", 4}};
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:48:31 -05:00
|
|
|
bool empty() const {
|
|
|
|
return this->index_map.empty();
|
|
|
|
}
|
2021-05-12 00:15:34 -05:00
|
|
|
|
2021-05-05 03:27:51 -05:00
|
|
|
std::size_t size() const {
|
2021-05-20 09:16:12 -05:00
|
|
|
return this->m_data.size();
|
2021-05-05 03:27:51 -05:00
|
|
|
}
|
|
|
|
|
2021-08-03 13:05:14 -05:00
|
|
|
T& add(const std::string& name, T&& value) {
|
2021-05-05 03:27:51 -05:00
|
|
|
if (index_map.count(name) != 0)
|
|
|
|
throw std::logic_error("An object with name: " + name + " already exists in container");
|
|
|
|
|
2021-05-20 09:16:12 -05:00
|
|
|
this->index_map.emplace(name, this->m_data.size());
|
|
|
|
this->m_data.push_back(std::forward<T>(value));
|
2021-08-03 13:05:14 -05:00
|
|
|
return this->m_data.back();
|
2021-05-05 03:27:51 -05:00
|
|
|
}
|
|
|
|
|
2021-08-03 13:05:14 -05:00
|
|
|
T& add(const std::string& name, const T& value) {
|
2021-05-07 02:05:21 -05:00
|
|
|
if (index_map.count(name) != 0)
|
|
|
|
throw std::logic_error("An object with name: " + name + " already exists in container");
|
|
|
|
|
2021-05-20 09:16:12 -05:00
|
|
|
this->index_map.emplace(name, this->m_data.size());
|
|
|
|
this->m_data.push_back(value);
|
2021-08-03 13:05:14 -05:00
|
|
|
return this->m_data.back();
|
2021-05-07 02:05:21 -05:00
|
|
|
}
|
|
|
|
|
2021-05-05 03:27:51 -05:00
|
|
|
bool has(const std::string& name) const {
|
|
|
|
return (index_map.count(name) != 0);
|
|
|
|
}
|
|
|
|
|
2021-05-09 04:54:45 -05:00
|
|
|
/*
|
|
|
|
Will copy the value from other to this - for all wells which are present
|
|
|
|
in both containers.
|
|
|
|
*/
|
2021-05-05 03:27:51 -05:00
|
|
|
void copy_welldata(const WellContainer<T>& other) {
|
|
|
|
if (this->index_map == other.index_map)
|
2021-05-20 09:16:12 -05:00
|
|
|
this->m_data = other.m_data;
|
2021-05-05 03:27:51 -05:00
|
|
|
else {
|
|
|
|
for (const auto& [name, index] : this->index_map)
|
|
|
|
this->update_if(index, name, other);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 04:54:45 -05:00
|
|
|
/*
|
|
|
|
Will copy the value for well @name from other to this. The well @name must
|
|
|
|
exist in both containers, otherwise an exception is thrown.
|
|
|
|
*/
|
2021-05-05 03:27:51 -05:00
|
|
|
void copy_welldata(const WellContainer<T>& other, const std::string& name) {
|
|
|
|
auto this_index = this->index_map.at(name);
|
|
|
|
auto other_index = other.index_map.at(name);
|
2021-05-20 09:16:12 -05:00
|
|
|
this->m_data[this_index] = other.m_data[other_index];
|
2021-05-05 03:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
T& operator[](std::size_t index) {
|
2021-05-20 09:16:12 -05:00
|
|
|
return this->m_data.at(index);
|
2021-05-05 03:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const T& operator[](std::size_t index) const {
|
2021-05-20 09:16:12 -05:00
|
|
|
return this->m_data.at(index);
|
2021-05-05 03:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
T& operator[](const std::string& name) {
|
|
|
|
auto index = this->index_map.at(name);
|
2021-05-20 09:16:12 -05:00
|
|
|
return this->m_data[index];
|
2021-05-05 03:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const T& operator[](const std::string& name) const {
|
|
|
|
auto index = this->index_map.at(name);
|
2021-05-20 09:16:12 -05:00
|
|
|
return this->m_data[index];
|
2021-05-05 03:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
2021-05-20 09:16:12 -05:00
|
|
|
this->m_data.clear();
|
2021-05-05 03:27:51 -05:00
|
|
|
this->index_map.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
typename std::vector<T>::const_iterator begin() const {
|
2021-05-20 09:16:12 -05:00
|
|
|
return this->m_data.begin();
|
2021-05-05 03:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
typename std::vector<T>::const_iterator end() const {
|
2021-05-20 09:16:12 -05:00
|
|
|
return this->m_data.end();
|
2021-05-05 03:27:51 -05:00
|
|
|
}
|
|
|
|
|
2021-05-20 09:16:12 -05:00
|
|
|
const std::vector<T>& data() const {
|
|
|
|
return this->m_data;
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:48:31 -05:00
|
|
|
std::optional<int> well_index(const std::string& wname) const {
|
|
|
|
auto index_iter = this->index_map.find(wname);
|
|
|
|
if (index_iter != this->index_map.end())
|
|
|
|
return index_iter->second;
|
|
|
|
|
2021-05-26 03:23:17 -05:00
|
|
|
return std::nullopt;
|
2021-05-21 13:48:31 -05:00
|
|
|
}
|
|
|
|
|
2021-07-26 05:06:45 -05:00
|
|
|
const std::string& well_name(std::size_t well_index) const {
|
|
|
|
for (const auto& [wname, windex] : this->index_map) {
|
|
|
|
if (windex == well_index)
|
|
|
|
return wname;
|
|
|
|
}
|
|
|
|
throw std::logic_error("No such well");
|
|
|
|
}
|
|
|
|
|
2021-10-15 07:14:20 -05:00
|
|
|
std::vector<std::string> wells() const {
|
|
|
|
std::vector<std::string> wlist;
|
|
|
|
for (const auto& [wname, _] : this->index_map) {
|
|
|
|
(void)_;
|
|
|
|
wlist.push_back(wname);
|
|
|
|
}
|
|
|
|
return wlist;
|
|
|
|
}
|
|
|
|
|
2023-02-02 04:52:08 -06:00
|
|
|
template<class Serializer>
|
|
|
|
void serializeOp(Serializer& serializer)
|
|
|
|
{
|
|
|
|
serializer(m_data);
|
|
|
|
serializer(index_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const WellContainer<T>& rhs) const
|
|
|
|
{
|
|
|
|
return this->m_data == rhs.m_data &&
|
|
|
|
this->index_map == rhs.index_map;
|
|
|
|
}
|
2021-05-20 09:16:12 -05:00
|
|
|
|
2021-05-05 03:27:51 -05:00
|
|
|
private:
|
|
|
|
void update_if(std::size_t index, const std::string& name, const WellContainer<T>& other) {
|
|
|
|
auto other_iter = other.index_map.find(name);
|
|
|
|
if (other_iter == other.index_map.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto other_index = other_iter->second;
|
2021-05-20 09:16:12 -05:00
|
|
|
this->m_data[index] = other.m_data[other_index];
|
2021-05-05 03:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-20 09:16:12 -05:00
|
|
|
std::vector<T> m_data;
|
2021-05-05 03:27:51 -05:00
|
|
|
std::unordered_map<std::string, std::size_t> index_map;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|