OGS 6.1.0-1721-g6382411ad
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 ProcessLib
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
27  std::string const group_id_property_name = config.getConfigParameter<std::string>("group_id_property");
28  DBUG("Using group_id_property %s", group_id_property_name.c_str());
29 
30  auto const& group_id_property =
31  mesh.getProperties().getPropertyVector<int>(group_id_property_name);
32 
33  // parse mapping data
34  using Values = std::vector<double>;
35  using Index_Values = std::pair<int, Values>;
36  std::vector<Index_Values> vec_index_values;
38  for (auto p : config.getConfigSubtreeList("index_values"))
39  {
41  auto const index = p.getConfigParameter<int>("index");
42  {
44  auto const value = p.getConfigParameterOptional<double>("value");
45 
46  if (value)
47  {
48  Values values(1, *value);
49  vec_index_values.emplace_back(index, values);
50  continue;
51  }
52  }
53 
54  // Value tag not available; continue with required values tag.
56  Values const values = p.getConfigParameter<Values>("values");
57 
58  if (values.empty())
59  OGS_FATAL("No value available for constant parameter.");
60 
61  vec_index_values.emplace_back(index, values);
62  }
63 
64  // check the input
65  unsigned n_values = vec_index_values.front().second.size();
66  for (auto p : vec_index_values)
67  {
68  auto itr = std::find(group_id_property->begin(), group_id_property->end(), p.first);
69  if (itr == group_id_property->end())
70  OGS_FATAL("Specified property index %d does not exist in the property vector %s",
71  p.first, group_id_property_name.c_str());
72 
73  if (p.second.size() != n_values)
74  OGS_FATAL("The length of some values (%d) is different from the first one (%d). "
75  "The length should be same for all index_values.",
76  p.second.size(), n_values);
77  }
78 
79  // create a mapping table
80  const int max_index =
81  *std::max_element(group_id_property->begin(), group_id_property->end());
82  std::vector<Values> vec_values(max_index + 1);
83  for (auto p : vec_index_values)
84  vec_values[p.first] = p.second;
85 
86  if (group_id_property->getMeshItemType() == MeshLib::MeshItemType::Node)
87  return std::make_unique<
89  name, *group_id_property, vec_values);
90  if (group_id_property->getMeshItemType() == MeshLib::MeshItemType::Cell)
91  return std::make_unique<
93  name, *group_id_property, vec_values);
94 
95  OGS_FATAL("Mesh item type of the specified property is not supported.");
96 }
97 
98 } // ProcessLib
std::unique_ptr< ParameterBase > createGroupBasedParameter(std::string const &name, BaseLib::ConfigTree const &config, MeshLib::Mesh const &mesh)
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
PropertyVector< T > const * getPropertyVector(std::string const &name) const
Definition: Properties.h:119
Range< SubtreeIterator > getConfigSubtreeList(std::string const &root) const
Definition: ConfigTree.cpp:170
#define OGS_FATAL(fmt,...)
Definition: Error.h:71