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