OGS
GroupBasedParameter.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
7#include "BaseLib/Error.h"
8#include "MeshLib/Mesh.h"
9
10namespace ParameterLib
11{
12std::unique_ptr<ParameterBase> createGroupBasedParameter(
13 std::string const& name, BaseLib::ConfigTree const& config,
14 MeshLib::Mesh const& mesh)
15{
17 config.checkConfigParameter("type", "Group");
18
19 // get a property vector of group IDs
20 std::string const group_id_property_name =
22 config.getConfigParameter<std::string>("group_id_property");
23 DBUG("Using group_id_property {:s}", group_id_property_name);
24
25 auto const& group_id_property =
26 mesh.getProperties().getPropertyVector<int>(group_id_property_name);
27
28 // parse mapping data
29 using Values = std::vector<double>;
30 std::map<int, Values> vec_index_values;
32 for (auto p : config.getConfigSubtreeList("index_values"))
33 {
35 auto const index = p.getConfigParameter<int>("index");
36 {
38 auto const value = p.getConfigParameterOptional<double>("value");
39
40 if (value)
41 {
42 Values values(1, *value);
43 vec_index_values.emplace(index, values);
44 continue;
45 }
46 }
47
48 // Value tag not available; continue with required values tag.
50 Values const values = p.getConfigParameter<Values>("values");
51
52 if (values.empty())
53 {
54 OGS_FATAL("No value available for constant parameter.");
55 }
56
57 vec_index_values.emplace(index, values);
58 }
59
60 // check the input
61 for (auto p : vec_index_values)
62 {
63#ifndef USE_PETSC // In case of partitioned meshes not all of the material ids
64 // might be available in the particular partition, therefore
65 // the check is omitted.
66 if (std::find(group_id_property->begin(), group_id_property->end(),
67 p.first) == group_id_property->end())
68 {
70 "Specified property index {:d} does not exist in the property "
71 "vector {:s}",
72 p.first, group_id_property_name);
73 }
74#endif
75
76 auto const n_values = vec_index_values.begin()->second.size();
77 if (p.second.size() != n_values)
78 {
80 "The length of some values ({:d}) is different from the first "
81 "one ({:d}). The length should be same for all index_values.",
82 p.second.size(), n_values);
83 }
84 }
85
86 if (group_id_property->getMeshItemType() == MeshLib::MeshItemType::Node)
87 {
88 return std::make_unique<
90 name, mesh, *group_id_property, vec_index_values);
91 }
92 if (group_id_property->getMeshItemType() == MeshLib::MeshItemType::Cell)
93 {
94 return std::make_unique<
96 name, mesh, *group_id_property, vec_index_values);
97 }
98
99 OGS_FATAL("Mesh item type of the specified property is not supported.");
100}
101
102} // namespace ParameterLib
#define OGS_FATAL(...)
Definition Error.h:19
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
T getConfigParameter(std::string const &param) const
Range< SubtreeIterator > getConfigSubtreeList(std::string const &root) const
void checkConfigParameter(std::string const &param, std::string_view const value) const
Properties & getProperties()
Definition Mesh.h:125
PropertyVector< T > const * getPropertyVector(std::string_view name) const
std::unique_ptr< ParameterBase > createGroupBasedParameter(std::string const &name, BaseLib::ConfigTree const &config, MeshLib::Mesh const &mesh)