OGS
getOrCreateMeshProperty.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5#include <string>
6
7#include "MeshLib/Mesh.h"
9
10namespace MeshLib
11{
16template <typename T>
18 std::string const& property_name,
19 MeshItemType const item_type,
20 int const number_of_components)
21{
22 if (property_name.empty())
23 {
25 "Trying to get or to create a mesh property with empty name.");
26 }
27
28 auto numberOfMeshItems = [&mesh, &item_type]() -> std::size_t
29 {
30 switch (item_type)
31 {
33 return mesh.getNumberOfElements();
35 return mesh.getNumberOfNodes();
37 return 0; // For the integration point data the size is
38 // variable
39 default:
41 "getOrCreateMeshProperty cannot handle other "
42 "types than Node, Cell, or IntegrationPoint.");
43 }
44 return 0;
45 };
46
47 if (mesh.getProperties().existsPropertyVector<T>(property_name))
48 {
49 auto result =
50 mesh.getProperties().template getPropertyVector<T>(property_name);
51 assert(result);
52 if (item_type != MeshItemType::IntegrationPoint)
53 {
54 // Test the size if number of mesh items is known, which is not the
55 // case for the integration point data.
56 assert(result->size() ==
57 numberOfMeshItems() * number_of_components);
58 }
59 return result;
60 }
61
62 auto result = mesh.getProperties().template createNewPropertyVector<T>(
63 property_name, item_type, numberOfMeshItems(), number_of_components);
64 assert(result);
65 return result;
66}
67} // namespace MeshLib
#define OGS_FATAL(...)
Definition Error.h:19
Properties & getProperties()
Definition Mesh.h:125
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:91
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:88
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)