OGS 6.2.1-97-g73d1aeda3
GroupBasedParameter.cpp
Go to the documentation of this file.
1 
10 #include "GroupBasedParameter.h"
11 
12 #include "BaseLib/ConfigTree.h"
13 #include "BaseLib/Error.h"
14 #include "MeshLib/Mesh.h"
15 
16 namespace ParameterLib
17 {
18 std::unique_ptr<ParameterBase> createGroupBasedParameter(
19  std::string const& name, BaseLib::ConfigTree const& config,
20  MeshLib::Mesh const& mesh)
21 {
23  config.checkConfigParameter("type", "Group");
24 
25  // get a property vector of group IDs
26  std::string const group_id_property_name =
28  config.getConfigParameter<std::string>("group_id_property");
29  DBUG("Using group_id_property %s", group_id_property_name.c_str());
30 
31  auto const& group_id_property =
32  mesh.getProperties().getPropertyVector<int>(group_id_property_name);
33 
34  // parse mapping data
35  using Values = std::vector<double>;
36  using Index_Values = std::pair<int, Values>;
37  std::vector<Index_Values> vec_index_values;
39  for (auto p : config.getConfigSubtreeList("index_values"))
40  {
42  auto const index = p.getConfigParameter<int>("index");
43  {
45  auto const value = p.getConfigParameterOptional<double>("value");
46 
47  if (value)
48  {
49  Values values(1, *value);
50  vec_index_values.emplace_back(index, values);
51  continue;
52  }
53  }
54 
55  // Value tag not available; continue with required values tag.
57  Values const values = p.getConfigParameter<Values>("values");
58 
59  if (values.empty())
60  {
61  OGS_FATAL("No value available for constant parameter.");
62  }
63 
64  vec_index_values.emplace_back(index, values);
65  }
66 
67  // check the input
68  unsigned n_values = vec_index_values.front().second.size();
69  for (auto p : vec_index_values)
70  {
71  auto itr = std::find(group_id_property->begin(),
72  group_id_property->end(), p.first);
73  if (itr == group_id_property->end())
74  {
75  OGS_FATAL(
76  "Specified property index %d does not exist in the property "
77  "vector %s",
78  p.first, group_id_property_name.c_str());
79  }
80 
81  if (p.second.size() != n_values)
82  {
83  OGS_FATAL(
84  "The length of some values (%d) is different from the first "
85  "one (%d). "
86  "The length should be same for all index_values.",
87  p.second.size(), n_values);
88  }
89  }
90 
91  // create a mapping table
92  const int max_index =
93  *std::max_element(group_id_property->begin(), group_id_property->end());
94  std::vector<Values> vec_values(max_index + 1);
95  for (auto p : vec_index_values)
96  {
97  vec_values[p.first] = p.second;
98  }
99 
100  if (group_id_property->getMeshItemType() == MeshLib::MeshItemType::Node)
101  {
102  return std::make_unique<
104  name, mesh, *group_id_property, vec_values);
105  }
106  if (group_id_property->getMeshItemType() == MeshLib::MeshItemType::Cell)
107  {
108  return std::make_unique<
110  name, mesh, *group_id_property, vec_values);
111  }
112 
113  OGS_FATAL("Mesh item type of the specified property is not supported.");
114 }
115 
116 } // namespace ParameterLib
MeshLib::Properties & getProperties()
Definition: Mesh.h:134
Definition of the Mesh class.
T getConfigParameter(std::string const &param) const
void checkConfigParameter(std::string const &param, T const &value) const
std::unique_ptr< ParameterBase > createGroupBasedParameter(std::string const &name, BaseLib::ConfigTree const &config, MeshLib::Mesh const &mesh)
PropertyVector< T > const * getPropertyVector(std::string const &name) const
Definition: Properties.h:119
Range< SubtreeIterator > getConfigSubtreeList(std::string const &root) const
Definition: ConfigTree.cpp:174
#define OGS_FATAL(fmt,...)
Definition: Error.h:63