OGS 6.1.0-1721-g6382411ad
ProcessUtils.cpp
Go to the documentation of this file.
1 
10 #include "ProcessUtils.h"
11 #include <iterator>
13 
14 namespace
15 {
17  std::vector<ProcessLib::ProcessVariable> const& variables,
18  std::string const& name, std::string const& tag)
19 {
20  // Find corresponding variable by name.
21  auto variable = std::find_if(variables.cbegin(), variables.cend(),
22  [&name](ProcessLib::ProcessVariable const& v) {
23  return v.getName() == name;
24  });
25 
26  if (variable == variables.end())
27  {
28  OGS_FATAL(
29  "Could not find process variable '%s' in the provided variables "
30  "list for config tag <%s>.",
31  name.c_str(), tag.c_str());
32  }
33  DBUG("Found process variable '%s' for config tag <%s>.",
34  variable->getName().c_str(), tag.c_str());
35 
36  // Const cast is needed because of variables argument constness.
37  return const_cast<ProcessLib::ProcessVariable&>(*variable);
38 }
39 } // namespace
40 
41 namespace ProcessLib
42 {
44  std::vector<ProcessVariable> const& variables,
45  BaseLib::ConfigTree const& pv_config, std::string const& tag)
46 {
47  // Find process variable name in process config.
49  std::string const name = pv_config.getConfigParameter<std::string>(tag);
50  return findVariableByName(variables, name, tag);
51 }
52 
53 std::vector<std::reference_wrapper<ProcessVariable>> findProcessVariables(
54  std::vector<ProcessVariable> const& variables,
55  BaseLib::ConfigTree const& pv_config,
56  std::initializer_list<std::string>
57  tags)
58 {
59  std::vector<std::reference_wrapper<ProcessVariable>> vars;
60  vars.reserve(variables.size());
61 
62  if (variables.size() > tags.size())
63  DBUG("Found multiple process variables with a same tag.");
64 
65  for (auto& tag : tags)
66  {
67  auto vars_per_tag = findProcessVariables(variables, pv_config, tag);
68  vars.insert(vars.end(), vars_per_tag.begin(), vars_per_tag.end());
69  }
70 
71  return vars;
72 }
73 
74 std::vector<std::reference_wrapper<ProcessVariable>> findProcessVariables(
75  std::vector<ProcessVariable> const& variables,
76  BaseLib::ConfigTree const& pv_config,
77  std::string const& tag)
78 {
79  std::vector<std::reference_wrapper<ProcessVariable>> vars;
80 
82  auto var_names = pv_config.getConfigParameterList<std::string>(tag);
83 
84  if (var_names.empty())
85  OGS_FATAL("No entity is found with config tag <%s>.", tag.c_str());
86 
87  std::vector<std::string> cached_var_names;
88 
89  for (std::string const& var_name : var_names)
90  {
91  vars.push_back(findVariableByName(variables, var_name, tag));
92  cached_var_names.push_back(var_name);
93  }
94 
95  // Eliminate duplicates in the set of variable names
96  BaseLib::makeVectorUnique(cached_var_names);
97 
98  if (cached_var_names.size() != var_names.size())
99  OGS_FATAL("Found duplicates with config tag <%s>.", tag.c_str());
100 
101  return vars;
102 }
103 } // namespace ProcessLib
ProcessVariable & findProcessVariable(std::vector< ProcessVariable > const &variables, BaseLib::ConfigTree const &pv_config, std::string const &tag)
void makeVectorUnique(std::vector< T > &v)
Definition: Algorithm.h:177
ProcessLib::ProcessVariable & findVariableByName(std::vector< ProcessLib::ProcessVariable > const &variables, std::string const &name, std::string const &tag)
T getConfigParameter(std::string const &param) const
std::vector< std::reference_wrapper< ProcessVariable > > findProcessVariables(std::vector< ProcessVariable > const &variables, BaseLib::ConfigTree const &pv_config, std::initializer_list< std::string > tags)
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
Range< ValueIterator< T > > getConfigParameterList(std::string const &param) const