OGS
IntegrationPointMetaData.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <spdlog/fmt/ranges.h>
7
8#include <nlohmann/json.hpp>
9#include <range/v3/algorithm/find.hpp>
10#include <range/v3/iterator/operations.hpp>
11#include <range/v3/range/conversion.hpp>
12#include <range/v3/view/filter.hpp>
13#include <range/v3/view/transform.hpp>
14#include <unordered_set>
15
16#include "BaseLib/Error.h"
17
18using nlohmann::json;
19
20namespace MeshLib
21{
22
24 std::string_view const json_string)
25{
26 json const meta_data = json::parse(json_string);
27
28 fields_ = meta_data["integration_point_arrays"] |
29 ranges::views::transform(
30 [](auto const& md)
31 {
33 md["name"], md["number_of_components"],
34 md["integration_order"]};
35 }) |
36 ranges::to<std::vector>;
37
39}
40
42 std::string const& field_name) const
43{
44 auto it = ranges::find(
46
47 if (it == fields_.end())
48 {
49 OGS_FATAL("No integration point meta data with name '{:s}' found.",
50 field_name);
51 }
52 return *it;
53}
54
56{
57 json json_meta_data;
58 json_meta_data["integration_point_arrays"] = json::array();
59
60 for (auto const& field : fields_)
61 {
62 json_meta_data["integration_point_arrays"].push_back(
63 {{"name", field.field_name},
64 {"number_of_components", field.n_components},
65 {"integration_order", field.integration_order}});
66 }
67
68 return json_meta_data.dump();
69}
70
72{
73 std::unordered_set<std::string> seen;
74
75 auto const duplicates =
76 fields_ |
77 ranges::views::transform(
79 ranges::views::filter([&seen](std::string const& name)
80 { return !seen.insert(name).second; }) |
81 ranges::to<std::vector>;
82
83 if (!duplicates.empty())
84 {
85 OGS_FATAL("Duplicate integration point meta data names found: {:s}.",
86 fmt::join(duplicates, ", "));
87 }
88}
89
91 std::optional<IntegrationPointMetaData> const& ip_meta_data,
92 std::string const& field_name)
93{
94 if (!ip_meta_data)
95 {
97 "The required 'IntegrationPointMetaData' array is not available in "
98 "the vtk field data but is needed to evaluate the integration "
99 "point property '{:s}'.",
100 field_name);
101 }
102 return (*ip_meta_data)[field_name];
103}
104} // namespace MeshLib
#define OGS_FATAL(...)
Definition Error.h:19
IntegrationPointMetaData(std::string_view const json_string)
Constructs integration point meta data from a JSON encoded string.
std::string toJsonString() const
Converts integration point meta data to a JSON string.
IntegrationPointMetaDataSingleField const & operator[](std::string const &field_name) const
std::vector< IntegrationPointMetaDataSingleField > fields_
IntegrationPointMetaDataSingleField getIntegrationPointMetaDataSingleField(std::optional< IntegrationPointMetaData > const &ip_meta_data, std::string const &field_name)