Chi-Tech
unknown_manager.h
Go to the documentation of this file.
1#ifndef CHI_MATH_UNKNOWN_MANAGER_H
2#define CHI_MATH_UNKNOWN_MANAGER_H
3
4#include <vector>
5#include <string>
6#include <stdexcept>
7
8namespace chi_math
9{
10
11// #################################################################
12/**Different types of variables.*/
13enum class UnknownType
14{
15 SCALAR = 1,
16 VECTOR_2 = 2,
17 VECTOR_3 = 3,
18 VECTOR_N = 4,
19 TENSOR = 5
20};
21
22/**Nodal variable storage format.*/
24{
25 NODAL = 1,
26 BLOCK = 2
27};
28
29// #################################################################
30/**Basic class for an variable.*/
32{
33public:
35 const unsigned int num_components_;
36 const unsigned int map_begin_;
37 std::string text_name_;
38 std::vector<std::string> component_text_names_;
40
41public:
42 explicit Unknown(UnknownType in_type,
43 unsigned int in_num_components = 1,
44 unsigned int in_map_begin = 0)
45 : type_(in_type),
47 : (type_ == UnknownType::VECTOR_2) ? 2
48 : (type_ == UnknownType::VECTOR_3) ? 3
49 : in_num_components),
50 map_begin_(in_map_begin)
51 {
52 component_text_names_.resize(num_components_, std::string());
53 for (unsigned int c = 0; c < num_components_; ++c)
54 {
55
56 char buffer[100];
57 snprintf(buffer, 100, " %03d", c);
58 component_text_names_[c] = buffer;
59 }
61 }
62
63 unsigned int GetMap(unsigned int component_number = 0) const
64 {
65 unsigned int map_value = 0;
66 switch (type_)
67 {
69 if (component_number >= num_components_)
70 throw std::out_of_range("Attempting to access component " +
71 std::to_string(component_number) +
72 ">=1"
73 " for a SCALAR unknown.");
74 map_value = 0;
75 break;
77 if (component_number >= num_components_)
78 throw std::out_of_range("Attempting to access component " +
79 std::to_string(component_number) +
80 ">=2"
81 " for a VECTOR_2 unknown.");
82 map_value = map_begin_ + component_number;
83 break;
85 if (component_number >= num_components_)
86 throw std::out_of_range("Attempting to access component " +
87 std::to_string(component_number) +
88 ">=3"
89 " for a VECTOR_3 unknown.");
90 map_value = map_begin_ + component_number;
91 break;
93 if (component_number >= num_components_)
94 throw std::out_of_range("Attempting to access component " +
95 std::to_string(component_number) +
96 ">=" + std::to_string(num_components_) +
97 " for a VECTOR_N unknown.");
98 map_value = map_begin_ + component_number;
99 break;
101 if (component_number >= num_components_)
102 throw std::out_of_range("Attempting to access component " +
103 std::to_string(component_number) +
104 ">=" + std::to_string(num_components_) +
105 " for a TENSOR unknown.");
106 map_value = map_begin_ + component_number;
107 break;
108 default:
109 break;
110 }
111
112 return map_value;
113 }
114 unsigned int GetMapEnd() const { return map_begin_ + num_components_ - 1; }
115
116 unsigned int NumComponents() const {return num_components_;}
117};
118
119// ###################################################################
120/**General object for the management of unknowns in mesh-based
121 * mathematical model.*/
123{
124private:
125public:
126 std::vector<Unknown> unknowns_;
128
129 typedef std::pair<UnknownType, unsigned int> UnknownInfo;
130 // Constructors
132 UnknownStorageType in_storage_type = UnknownStorageType::NODAL) noexcept
133 : dof_storage_type_(in_storage_type)
134 {
135 }
136
138 std::initializer_list<UnknownInfo> unknown_info_list,
139 UnknownStorageType in_storage_type = UnknownStorageType::NODAL) noexcept
140 : dof_storage_type_(in_storage_type)
141 {
142 for (const auto& uk_info : unknown_info_list)
143 AddUnknown(uk_info.first, uk_info.second);
144 }
145
147 const std::vector<Unknown>& unknown_info_list,
148 UnknownStorageType in_storage_type = UnknownStorageType::NODAL) noexcept
149 : dof_storage_type_(in_storage_type)
150 {
151 for (const auto& uk : unknown_info_list)
152 AddUnknown(uk.type_, uk.num_components_);
153 }
154
156 std::initializer_list<Unknown> unknowns,
157 UnknownStorageType in_storage_type = UnknownStorageType::NODAL) noexcept
158 : dof_storage_type_(in_storage_type)
159 {
160 size_t ukid = 0;
161 for (const auto& uk : unknowns)
162 {
163 AddUnknown(uk.type_, uk.num_components_);
164 SetUnknownTextName(ukid, uk.text_name_);
165 size_t compid = 0;
166 for (const auto& comp_text_name : uk.component_text_names_)
167 {
168 SetUnknownComponentTextName(ukid, compid, comp_text_name);
169 ++compid;
170 }
171
172 ++ukid;
173 }
174 }
175
176 UnknownManager(const UnknownManager& other) = default;
177 UnknownManager& operator=(const UnknownManager& other) = default;
178
179 // Utilities
181 {
182 return UnknownManager({std::make_pair(UnknownType::SCALAR, 0)});
183 }
184
185 size_t NumberOfUnknowns() const { return unknowns_.size(); }
186 const Unknown& GetUnknown(size_t id) const {return unknowns_[id];}
187
188 void SetDOFStorageType(const UnknownStorageType in_storage_type)
189 {
190 dof_storage_type_ = in_storage_type;
191 }
192
194
195 void Clear() { unknowns_.clear(); }
196
197 unsigned int AddUnknown(UnknownType unk_type, unsigned int dimension = 0);
198
199 unsigned int MapUnknown(unsigned int unknown_id,
200 unsigned int component = 0) const;
201
202 unsigned int GetTotalUnknownStructureSize() const;
203
204 void SetUnknownNumOffBlockConnections(unsigned int unknown_id, int num_conn);
205
206 void SetUnknownComponentNumOffBlockConnections(unsigned int unknown_id,
207 unsigned int component,
208 int num_conn);
209
210 void SetUnknownTextName(unsigned int unknown_id,
211 const std::string& in_text_name);
212
213 void SetUnknownComponentTextName(unsigned int unknown_id,
214 unsigned int component,
215 const std::string& in_text_name);
216
217 ~UnknownManager() = default;
218};
219
220} // namespace chi_math
221
222#endif
const unsigned int num_components_
const unsigned int map_begin_
unsigned int GetMap(unsigned int component_number=0) const
Unknown(UnknownType in_type, unsigned int in_num_components=1, unsigned int in_map_begin=0)
std::vector< int > num_off_block_connections_
std::vector< std::string > component_text_names_
const UnknownType type_
std::string text_name_
unsigned int NumComponents() const
unsigned int GetMapEnd() const
unsigned int AddUnknown(UnknownType unk_type, unsigned int dimension=0)
const Unknown & GetUnknown(size_t id) const
void SetUnknownComponentNumOffBlockConnections(unsigned int unknown_id, unsigned int component, int num_conn)
UnknownManager(UnknownStorageType in_storage_type=UnknownStorageType::NODAL) noexcept
UnknownStorageType GetDOFStorageType() const
void SetUnknownNumOffBlockConnections(unsigned int unknown_id, int num_conn)
void SetUnknownComponentTextName(unsigned int unknown_id, unsigned int component, const std::string &in_text_name)
std::pair< UnknownType, unsigned int > UnknownInfo
UnknownManager & operator=(const UnknownManager &other)=default
UnknownManager(const std::vector< Unknown > &unknown_info_list, UnknownStorageType in_storage_type=UnknownStorageType::NODAL) noexcept
UnknownManager(const UnknownManager &other)=default
unsigned int MapUnknown(unsigned int unknown_id, unsigned int component=0) const
std::vector< Unknown > unknowns_
void SetUnknownTextName(unsigned int unknown_id, const std::string &in_text_name)
UnknownStorageType dof_storage_type_
size_t NumberOfUnknowns() const
UnknownManager(std::initializer_list< Unknown > unknowns, UnknownStorageType in_storage_type=UnknownStorageType::NODAL) noexcept
unsigned int GetTotalUnknownStructureSize() const
static UnknownManager GetUnitaryUnknownManager()
void SetDOFStorageType(const UnknownStorageType in_storage_type)
UnknownManager(std::initializer_list< UnknownInfo > unknown_info_list, UnknownStorageType in_storage_type=UnknownStorageType::NODAL) noexcept