opm-common
NameOrder.hpp
1 /*
2  Copyright 2021 Equinor ASA.
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef NAME_ORDER_HPP
20 #define NAME_ORDER_HPP
21 
22 #include <cstddef>
23 #include <initializer_list>
24 #include <optional>
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28 
29 namespace Opm {
30 
31 // The purpose of this small class is to ensure that well and group name
32 // always come in the order they are defined in the deck.
33 
34 class NameOrder
35 {
36 public:
37  NameOrder() = default;
38  explicit NameOrder(std::initializer_list<std::string> names);
39  explicit NameOrder(const std::vector<std::string>& names);
40 
41  void add(const std::string& name);
42  std::vector<std::string> sort(std::vector<std::string> names) const;
43  const std::vector<std::string>& names() const;
44  bool has(const std::string& wname) const;
45 
46  template <class Serializer>
47  void serializeOp(Serializer& serializer)
48  {
49  serializer(m_index_map);
50  serializer(m_name_list);
51  }
52 
53  static NameOrder serializationTestObject();
54 
55  const std::string& operator[](std::size_t index) const;
56  bool operator==(const NameOrder& other) const;
57 
58  auto begin() const { return this->m_name_list.begin(); }
59  auto end() const { return this->m_name_list.end(); }
60  auto size() const { return this->m_name_list.size(); }
61 
62 private:
63  std::unordered_map<std::string, std::size_t> m_index_map;
64  std::vector<std::string> m_name_list;
65 };
66 
69 {
70 public:
74  GroupOrder() = default;
75 
80  explicit GroupOrder(std::size_t max_groups);
81 
84 
88  void add(const std::string& name);
89 
94  const std::vector<std::string>& names() const
95  {
96  return this->name_list_;
97  }
98 
106  std::vector<std::string> names(const std::string& pattern) const;
107 
113  bool has(const std::string& gname) const;
114 
123  bool anyGroupMatches(const std::string& pattern) const;
124 
130  std::vector<std::optional<std::string>> restart_groups() const;
131 
138  bool operator==(const GroupOrder& other) const;
139 
143  auto begin() const { return this->name_list_.begin(); }
144 
146  auto end() const { return this->name_list_.end(); }
147 
153  template <class Serializer>
154  void serializeOp(Serializer& serializer)
155  {
156  serializer(this->name_list_);
157  serializer(this->max_groups_);
158  }
159 
160 private:
162  std::size_t max_groups_{};
163 
165  std::vector<std::string> name_list_{};
166 };
167 
168 } // namespace Opm
169 
170 #endif // NAME_ORDER_HPP
void serializeOp(Serializer &serializer)
Convert between byte array and object representation.
Definition: NameOrder.hpp:154
void add(const std::string &name)
Add a group name to ordered collection.
Definition: NameOrder.cpp:117
std::vector< std::optional< std::string > > restart_groups() const
Retrieve sequence of group names ordered appropriately for restart file output.
Definition: NameOrder.cpp:163
Definition: NameOrder.hpp:34
const std::vector< std::string > & names() const
Retrieve current list of group names.
Definition: NameOrder.hpp:94
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
bool operator==(const GroupOrder &other) const
Equality predicate.
Definition: NameOrder.cpp:175
static GroupOrder serializationTestObject()
Create a serialisation test object.
Definition: NameOrder.cpp:107
auto begin() const
Start of group name sequence.
Definition: NameOrder.hpp:143
auto end() const
End of group name sequence.
Definition: NameOrder.hpp:146
bool has(const std::string &gname) const
Group name existence predicate.
Definition: NameOrder.cpp:125
GroupOrder()=default
Default constructor.
bool anyGroupMatches(const std::string &pattern) const
Group name existence predicate.
Definition: NameOrder.cpp:130
Class for (de-)serializing.
Definition: Serializer.hpp:94
Collection of group names with built-in ordering.
Definition: NameOrder.hpp:68