OGS 6.2.1-97-g73d1aeda3
Properties-impl.h
Go to the documentation of this file.
1 
13 template <typename T>
14 PropertyVector<T>* Properties::createNewPropertyVector(
15  std::string const& name,
16  MeshItemType mesh_item_type,
17  std::size_t n_components)
18 {
19  std::map<std::string, PropertyVectorBase*>::const_iterator it(
20  _properties.find(name)
21  );
22  if (it != _properties.end()) {
23  ERR("A property of the name '%s' is already assigned to the mesh.",
24  name.c_str());
25  return nullptr;
26  }
27  auto entry_info(
28  _properties.insert(
29  std::make_pair(
30  name, new PropertyVector<T>(name, mesh_item_type, n_components)
31  )
32  )
33  );
34  return static_cast<PropertyVector<T>*>((entry_info.first)->second);
35 }
36 
37 template <typename T>
38 PropertyVector<T>* Properties::createNewPropertyVector(
39  std::string const& name,
40  std::size_t n_prop_groups,
41  std::vector<std::size_t> const& item2group_mapping,
42  MeshItemType mesh_item_type,
43  std::size_t n_components)
44 {
45  // check if there is already a PropertyVector with the same name and
46  // mesh_item_type
47  std::map<std::string, PropertyVectorBase*>::const_iterator it(
48  _properties.find(name)
49  );
50  if (it != _properties.end()) {
51  ERR("A property of the name '%s' already assigned to the mesh.",
52  name.c_str());
53  return nullptr;
54  }
55 
56  // check entries of item2group_mapping for consistence
57  for (std::size_t k(0); k<item2group_mapping.size(); k++) {
58  std::size_t const group_id (item2group_mapping[k]);
59  if (group_id >= n_prop_groups) {
60  ERR("The mapping to property %d for item %d is not in the correct range [0,%d).", group_id, k, n_prop_groups);
61  return nullptr;
62  }
63  }
64 
65  auto entry_info(
66  _properties.insert(
67  std::pair<std::string, PropertyVectorBase*>(
68  name,
69  new PropertyVector<T>(n_prop_groups,
70  item2group_mapping, name, mesh_item_type, n_components)
71  )
72  )
73  );
74  return static_cast<PropertyVector<T>*>((entry_info.first)->second);
75 }
76 
77 template <typename T>
78 bool Properties::existsPropertyVector(std::string const& name) const
79 {
80  auto it(_properties.find(name));
81  // Check that a PropertyVector with the approriate name exists.
82  if (it == _properties.end())
83  {
84  return false;
85  }
86  // Check that the PropertyVector has the correct data type.
87  return dynamic_cast<PropertyVector<T> const*>(it->second) != nullptr;
88 }
89 
90 template <typename T>
91 bool Properties::existsPropertyVector(std::string const& name,
92  MeshItemType const mesh_item_type,
93  int const number_of_components) const
94 {
95  auto const it = _properties.find(name);
96  if (it == _properties.end())
97  {
98  return false;
99  }
100 
101  auto property = dynamic_cast<PropertyVector<T>*>(it->second);
102  if (property == nullptr)
103  {
104  return false;
105  }
106  if (property->getMeshItemType() != mesh_item_type)
107  {
108  return false;
109  }
110  if (property->getNumberOfComponents() != number_of_components)
111  {
112  return false;
113  }
114  return true;
115 }
116 
117 template <typename T>
118 PropertyVector<T> const* Properties::getPropertyVector(
119  std::string const& name) const
120 {
121  auto it(_properties.find(name));
122  if (it == _properties.end())
123  {
124  OGS_FATAL(
125  "The PropertyVector '%s' is not available in the mesh.",
126  name.c_str());
127  }
128  if (!dynamic_cast<PropertyVector<T> const*>(it->second))
129  {
130  OGS_FATAL(
131  "The PropertyVector '%s' has a different type than the requested "
132  "PropertyVector.",
133  name.c_str());
134  }
135  return dynamic_cast<PropertyVector<T> const*>(it->second);
136 }
137 
138 template <typename T>
139 PropertyVector<T>* Properties::getPropertyVector(std::string const& name)
140 {
141  auto it(_properties.find(name));
142  if (it == _properties.end())
143  {
144  OGS_FATAL(
145  "A PropertyVector with the specified name '%s' is not available.",
146  name.c_str());
147  }
148  if (!dynamic_cast<PropertyVector<T>*>(it->second))
149  {
150  OGS_FATAL(
151  "The PropertyVector '%s' has a different type than the requested "
152  "PropertyVector.",
153  name.c_str());
154  }
155  return dynamic_cast<PropertyVector<T>*>(it->second);
156 }
157 
158 template <typename T>
159 PropertyVector<T> const* Properties::getPropertyVector(
160  std::string const& name, MeshItemType const item_type,
161  int const n_components) const
162 {
163  auto const it = _properties.find(name);
164  if (it == _properties.end())
165  {
166  OGS_FATAL("A PropertyVector with name '%s' does not exist in the mesh.",
167  name.c_str());
168  }
169 
170  auto property = dynamic_cast<PropertyVector<T>*>(it->second);
171  if (property == nullptr)
172  {
173  OGS_FATAL(
174  "Could not cast the data type of the PropertyVector '%s' to "
175  "requested data type.",
176  name.c_str());
177  }
178  if (property->getMeshItemType() != item_type)
179  {
180  OGS_FATAL(
181  "The PropertyVector '%s' has type '%s'. A '%s' field is requested.",
182  name.c_str(), toString(property->getMeshItemType()),
183  toString(item_type));
184  }
185  if (property->getNumberOfComponents() != n_components)
186  {
187  OGS_FATAL(
188  "PropertyVector '%s' has %d components, %d components are needed.",
189  name.c_str(), property->getNumberOfComponents(), n_components);
190  }
191  return property;
192 }
193 
194 template <typename T>
195 PropertyVector<T>* Properties::getPropertyVector(std::string const& name,
196  MeshItemType const item_type,
197  int const n_components)
198 {
199  auto const it = _properties.find(name);
200  if (it == _properties.end())
201  {
202  OGS_FATAL("A PropertyVector with name '%s' does not exist in the mesh.",
203  name.c_str());
204  }
205 
206  auto property = dynamic_cast<PropertyVector<T>*>(it->second);
207  if (property == nullptr)
208  {
209  OGS_FATAL(
210  "Could not cast the data type of the PropertyVector '%s' to "
211  "requested data type.",
212  name.c_str());
213  }
214  if (property->getMeshItemType() != item_type)
215  {
216  OGS_FATAL(
217  "The PropertyVector '%s' has type '%s'. A '%s' field is requested.",
218  name.c_str(), toString(property->getMeshItemType()),
219  toString(item_type));
220  }
221  if (property->getNumberOfComponents() != n_components)
222  {
223  OGS_FATAL(
224  "PropertyVector '%s' has %d components, %d components are needed.",
225  name.c_str(), property->getNumberOfComponents(), n_components);
226  }
227  return property;
228 }
MeshItemType
Definition: Location.h:21
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
const char * toString(mgis::behaviour::Behaviour::Kinematic kin)
Converts MGIS kinematic to a string representation.
Definition: MFront.cpp:95