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 DBUG("Reusing existing property vector '{}' on mesh '{}'.",
50 property_name, mesh.getName());
51
52 auto* const result =
53 mesh.getProperties().template getPropertyVector<T>(property_name);
54 assert(result);
55 if (auto const mit = result->getMeshItemType(); mit != item_type)
56 {
58 "Found mesh item type '{}' for mesh property '{}' on mesh "
59 "'{}', but expected a '{}' property.",
60 toString(mit), property_name, mesh.getName(),
61 toString(item_type));
62 }
63 if (auto const ncomp = result->getNumberOfGlobalComponents();
64 ncomp != number_of_components)
65 {
67 "Found {} components for mesh property '{}' on mesh '{}', but "
68 "expected {} components.",
69 ncomp, property_name, mesh.getName(), number_of_components);
70 }
71 if (item_type != MeshItemType::IntegrationPoint)
72 {
73 // Test the size if number of mesh items is known, which is not the
74 // case for the integration point data.
75 auto const size = result->size();
76 auto const size_expected =
77 numberOfMeshItems() * number_of_components;
78 if (size != size_expected)
79 {
81 "Actual and expected size of property '{}' on mesh '{}' do "
82 "not match: {} != {}.",
83 property_name, mesh.getName(), size, size_expected);
84 }
85 }
86 return result;
87 }
88
89 auto* const result =
90 mesh.getProperties().template createNewPropertyVector<T>(
91 property_name, item_type, numberOfMeshItems(),
92 number_of_components);
93 assert(result);
94 return result;
95}
96} // namespace MeshLib
#define OGS_FATAL(...)
Definition Error.h:19
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
Properties & getProperties()
Definition Mesh.h:127
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:95
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:92
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:89
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)
static constexpr char const * toString(const MeshItemType t)
Returns a char array for a specific MeshItemType.
Definition MeshEnums.h:26