Chi-Tech
unknown_manager.cc
Go to the documentation of this file.
1#include "unknown_manager.h"
2
3#include "chi_runtime.h"
4#include "chi_log.h"
5
6//###################################################################
7/**Adds an unknown to the manager. This method will figure out
8 * where the last unknown ends and where to begin the next one.*/
11 unsigned int dimension)
12{
13 auto& log = chi::ChiLog::GetInstance();
14
15 unsigned int last_unknown_end = -1;
16 if (not unknowns_.empty())
17 last_unknown_end = unknowns_.back().GetMapEnd();
18
19 unsigned int new_unknown_index = unknowns_.size();
20
21 if (unk_type == UnknownType::SCALAR)
22 {
23 unknowns_.emplace_back(UnknownType::SCALAR, 1, last_unknown_end + 1);
24 unknowns_.back().text_name_ = "Unknown_" + std::to_string(unknowns_.size() - 1);
25 }
26 else if (unk_type == UnknownType::VECTOR_2)
27 {
28 unknowns_.emplace_back(UnknownType::VECTOR_2, 2, last_unknown_end + 1);
29 unknowns_.back().text_name_ = "Unknown_" + std::to_string(unknowns_.size() - 1);
30 }
31 else if (unk_type == UnknownType::VECTOR_3)
32 {
33 unknowns_.emplace_back(UnknownType::VECTOR_3, 3, last_unknown_end + 1);
34 unknowns_.back().text_name_ = "Unknown_" + std::to_string(unknowns_.size() - 1);
35 }
36 else if (unk_type == UnknownType::VECTOR_N)
37 {
38 if (dimension == 0)
39 {
40 log.LogAllError()
41 << "UnknownManager: When adding unknown of type VECTOR_N, "
42 << "the dimension must not be 0.";
43 Chi::Exit(EXIT_FAILURE);
44 }
45
46 unknowns_.emplace_back(UnknownType::VECTOR_N, dimension, last_unknown_end + 1);
47 unknowns_.back().text_name_ = "Unknown_" + std::to_string(unknowns_.size() - 1);
48 }
49 else if (unk_type == UnknownType::TENSOR)
50 {
51 if (dimension == 0 or dimension == 1)
52 {
53 log.LogAllError()
54 << "UnknownManager: When adding unknown of type TENSOR, "
55 << "the dimension must not be 0 or 1.";
56 Chi::Exit(EXIT_FAILURE);
57 }
58
59 throw std::invalid_argument("UnknownManager: TENSOR unknowns are not "
60 "supported yet.");
61 }
62 else
63 {
64 throw std::logic_error("UnknownManager: Invalid call to AddUnknown(). "
65 "Unknown type is probably not supported yet.");
66 }
67
68 return new_unknown_index;
69}
70
71//###################################################################
72/**Maps the unknown's component within the storage of a node.*/
74 MapUnknown(unsigned int unknown_id, unsigned int component) const
75{
76 auto& log = chi::ChiLog::GetInstance();
77
78 if (unknown_id < 0 or unknown_id >= unknowns_.size())
79 {
80 log.LogAllError()
81 << "UnknownManager failed call to MapUnknown " << unknown_id;
82 Chi::Exit(EXIT_FAILURE);
83 }
84 return unknowns_[unknown_id].GetMap(component);
85}
86
87//###################################################################
88/**Determines the total number of components over all unknowns.*/
90{
91 if (unknowns_.empty())
92 return 0;
93
94 return unknowns_.back().GetMapEnd() + 1;
95}
96
97//###################################################################
98/**Sets the number of off block connections for the given unknown.
99 * All the components will be set to the same amount.*/
101 SetUnknownNumOffBlockConnections(unsigned int unknown_id,
102 int num_conn)
103{
104 auto& log = chi::ChiLog::GetInstance();
105
106 if (unknown_id < 0 or unknown_id >= unknowns_.size())
107 {
108 log.LogAllError()
109 << "UnknownManager failed call to SetUnknownNumOffBlockConnections,"
110 " illegal index. " << unknown_id;
111 Chi::Exit(EXIT_FAILURE);
112 }
113
114 for (auto& val : unknowns_[unknown_id].num_off_block_connections_)
115 val = num_conn;
116}
117//###################################################################
118/**Sets the number of off block connections for the given unknown-
119 * component pair.*/
121 SetUnknownComponentNumOffBlockConnections(unsigned int unknown_id,
122 unsigned int component,
123 int num_conn)
124{
125 auto& log = chi::ChiLog::GetInstance();
126
127 if (unknown_id < 0 or unknown_id >= unknowns_.size())
128 {
129 log.LogAllError()
130 << "UnknownManager failed call to SetUnknownComponentTextName,"
131 " illegal unknown index. " << unknown_id;
132 Chi::Exit(EXIT_FAILURE);
133 }
134
135 if (component < 0 or component >= unknowns_[unknown_id].num_components_)
136 {
137 log.LogAllError()
138 << "UnknownManager failed call to SetUnknownComponentTextName,"
139 " illegal component index. " << component;
140 Chi::Exit(EXIT_FAILURE);
141 }
142
143 unknowns_[unknown_id].num_off_block_connections_[component] = num_conn;
144
145}
146
147//###################################################################
148/**Sets a text name for the indicated unknown.*/
150 SetUnknownTextName(unsigned int unknown_id,
151 const std::string& in_text_name)
152{
153 auto& log = chi::ChiLog::GetInstance();
154
155 if (unknown_id < 0 or unknown_id >= unknowns_.size())
156 {
157 log.LogAllError()
158 << "UnknownManager failed call to SetUnknownTextName,"
159 " illegal index. " << unknown_id;
160 Chi::Exit(EXIT_FAILURE);
161 }
162
163 unknowns_[unknown_id].text_name_ = in_text_name;
164}
165
166//###################################################################
167/**Sets the text name to be associated with each component of the
168 * unknown.*/
170 SetUnknownComponentTextName(unsigned int unknown_id,
171 unsigned int component,
172 const std::string& in_text_name)
173{
174 auto& log = chi::ChiLog::GetInstance();
175
176 if (unknown_id < 0 or unknown_id >= unknowns_.size())
177 {
178 log.LogAllError()
179 << "UnknownManager failed call to SetUnknownComponentTextName,"
180 " illegal unknown index. " << unknown_id;
181 Chi::Exit(EXIT_FAILURE);
182 }
183
184 if (component < 0 or component >= unknowns_[unknown_id].num_components_)
185 {
186 log.LogAllError()
187 << "UnknownManager failed call to SetUnknownComponentTextName,"
188 " illegal component index. " << component;
189 Chi::Exit(EXIT_FAILURE);
190 }
191
192 unknowns_[unknown_id].component_text_names_[component] = in_text_name;
193
194}
static void Exit(int error_code)
Definition: chi_runtime.cc:342
static ChiLog & GetInstance() noexcept
Definition: chi_log.cc:12
unsigned int AddUnknown(UnknownType unk_type, unsigned int dimension=0)
void SetUnknownComponentNumOffBlockConnections(unsigned int unknown_id, unsigned int component, int num_conn)
void SetUnknownNumOffBlockConnections(unsigned int unknown_id, int num_conn)
void SetUnknownComponentTextName(unsigned int unknown_id, unsigned int component, const std::string &in_text_name)
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)
unsigned int GetTotalUnknownStructureSize() const