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