OGS
addPropertyToMesh.h
Go to the documentation of this file.
1
12#pragma once
13
14#include <vector>
15
16#include "BaseLib/Error.h"
17#include "MeshLib/Location.h"
18#include "MeshLib/Mesh.h"
19#include "MeshLib/Properties.h"
20
21namespace MeshLib
22{
33template <typename T>
34void addPropertyToMesh(Mesh& mesh, std::string_view name,
35 MeshItemType item_type, std::size_t number_of_components,
36 std::vector<T> const& values)
37{
38 if (item_type == MeshItemType::Node)
39 {
40 if (mesh.getNumberOfNodes() != values.size() / number_of_components)
41 {
43 "Error number of nodes ({:d}) does not match the number of "
44 "tuples ({:d}).",
45 mesh.getNumberOfNodes(), values.size() / number_of_components);
46 }
47 }
48 if (item_type == MeshItemType::Cell)
49 {
50 if (mesh.getNumberOfElements() != values.size() / number_of_components)
51 {
53 "Error number of elements ({:d}) does not match the number of "
54 "tuples ({:d}).",
56 values.size() / number_of_components);
57 }
58 }
59
60 auto* const property = mesh.getProperties().createNewPropertyVector<T>(
61 name, item_type, number_of_components);
62 if (!property)
63 {
64 OGS_FATAL("Error while creating PropertyVector '{:s}'.", name);
65 }
66 property->reserve(values.size());
67 std::copy(values.cbegin(), values.cend(), std::back_inserter(*property));
68}
69} // namespace MeshLib
#define OGS_FATAL(...)
Definition Error.h:26
Definition of the class Properties that implements a container of properties.
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
PropertyVector< T > * createNewPropertyVector(std::string_view name, MeshItemType mesh_item_type, std::size_t n_components=1)
Definition Properties.h:17
MeshItemType
Definition Location.h:21
void addPropertyToMesh(Mesh &mesh, std::string_view name, MeshItemType item_type, std::size_t number_of_components, std::vector< T > const &values)