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