OGS
ProcessUtils.cpp
Go to the documentation of this file.
1
11#include "ProcessUtils.h"
12
13#include "BaseLib/ConfigTree.h"
15
16namespace
17{
19 std::vector<ProcessLib::ProcessVariable> const& variables,
20 std::string const& name, std::string const& tag)
21{
22 // Find corresponding variable by name.
23 auto variable = std::find_if(variables.cbegin(), variables.cend(),
25 { return v.getName() == name; });
26
27 if (variable == variables.end())
28 {
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, tag);
34 }
35 DBUG("Found process variable '{:s}' for config tag <{:s}>.",
36 variable->getName(), tag);
37
38 // Const cast is needed because of variables argument constness.
39 return const_cast<ProcessLib::ProcessVariable&>(*variable);
40}
41} // namespace
42
43namespace 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
55std::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
76std::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);
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);
105 }
106
107 return vars;
108}
109} // namespace ProcessLib
#define OGS_FATAL(...)
Definition: Error.h:26
void DBUG(char const *fmt, Args const &... args)
Definition: Logging.h:29
T getConfigParameter(std::string const &param) const
Range< ValueIterator< T > > getConfigParameterList(std::string const &param) const
void makeVectorUnique(std::vector< T > &v)
Definition: Algorithm.h:209
static const double v
std::vector< std::reference_wrapper< ProcessVariable > > findProcessVariables(std::vector< ProcessVariable > const &variables, BaseLib::ConfigTree const &pv_config, std::initializer_list< std::string > tags)
ProcessVariable & findProcessVariable(std::vector< ProcessVariable > const &variables, BaseLib::ConfigTree const &pv_config, std::string const &tag)
ProcessLib::ProcessVariable & findVariableByName(std::vector< ProcessLib::ProcessVariable > const &variables, std::string const &name, std::string const &tag)