OGS
getOrCreateMeshProperty.h
Go to the documentation of this file.
1
10#pragma once
11#include <string>
12
13#include "MeshLib/Location.h"
14#include "MeshLib/Mesh.h"
17
18namespace MeshLib
19{
24template <typename T>
26 std::string const& property_name,
27 MeshItemType const item_type,
28 int const number_of_components)
29{
30 if (property_name.empty())
31 {
33 "Trying to get or to create a mesh property with empty name.");
34 }
35
36 auto numberOfMeshItems = [&mesh, &item_type]() -> std::size_t
37 {
38 switch (item_type)
39 {
41 return mesh.getNumberOfElements();
43 return mesh.getNumberOfNodes();
45 return 0; // For the integration point data the size is
46 // variable
47 default:
49 "getOrCreateMeshProperty cannot handle other "
50 "types than Node, Cell, or IntegrationPoint.");
51 }
52 return 0;
53 };
54
55 if (mesh.getProperties().existsPropertyVector<T>(property_name))
56 {
57 auto result =
58 mesh.getProperties().template getPropertyVector<T>(property_name);
59 assert(result);
60 if (item_type != MeshItemType::IntegrationPoint)
61 {
62 // Test the size if number of mesh items is known, which is not the
63 // case for the integration point data.
64 assert(result->size() ==
65 numberOfMeshItems() * number_of_components);
66 }
67 return result;
68 }
69
70 auto result = mesh.getProperties().template createNewPropertyVector<T>(
71 property_name, item_type, number_of_components);
72 assert(result);
73 result->resize(numberOfMeshItems() * number_of_components);
74 return result;
75}
76} // namespace MeshLib
#define OGS_FATAL(...)
Definition Error.h:26
Definition of the Mesh class.
Properties & getProperties()
Definition Mesh.h:134
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:100
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:97
bool existsPropertyVector(std::string_view name) const
PropertyVector< T > * getOrCreateMeshProperty(Mesh &mesh, std::string const &property_name, MeshItemType const item_type, int const number_of_components)
MeshItemType
Definition Location.h:21