OGS 6.1.0-1721-g6382411ad
ProcessUtils.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <vector>
13 #include "BaseLib/ConfigTree.h"
14 #include "BaseLib/Error.h"
16 
17 namespace ProcessLib
18 {
19 class ProcessVariable;
20 
39 std::vector<std::reference_wrapper<ProcessVariable>> findProcessVariables(
40  std::vector<ProcessVariable> const& variables,
41  BaseLib::ConfigTree const& process_config,
42  std::initializer_list<std::string>
43  tags);
44 
45 std::vector<std::reference_wrapper<ProcessVariable>> findProcessVariables(
46  std::vector<ProcessVariable> const& variables,
47  BaseLib::ConfigTree const& pv_config,
48  std::string const& tag);
49 
59 template <typename ParameterDataType>
61  std::string const& parameter_name,
62  std::vector<std::unique_ptr<ParameterBase>> const& parameters,
63  int const num_components)
64 {
65  // Find corresponding parameter by name.
66  auto const parameter_it = std::find_if(
67  parameters.cbegin(), parameters.cend(),
68  [&parameter_name](std::unique_ptr<ParameterBase> const& p) {
69  return p->name == parameter_name;
70  });
71 
72  if (parameter_it == parameters.end()) {
73  OGS_FATAL(
74  "Could not find parameter `%s' in the provided parameters list.",
75  parameter_name.c_str());
76  }
77 
78  DBUG("Found parameter `%s'.", (*parameter_it)->name.c_str());
79 
80  // Check the type correctness of the found parameter.
81  auto* const parameter =
82  dynamic_cast<Parameter<ParameterDataType>*>(parameter_it->get());
83  if (!parameter) {
84  OGS_FATAL("The read parameter `%s' is of incompatible type.",
85  parameter_name.c_str());
86  }
87 
88  if (num_components != 0 &&
89  parameter->getNumberOfComponents() != num_components)
90  {
91  OGS_FATAL(
92  "The read parameter `%s' has the wrong number of components (%lu "
93  "instead of %u).",
94  parameter_name.c_str(), parameter->getNumberOfComponents(),
95  num_components);
96  }
97 
98  return *parameter;
99 }
100 
115 template <typename ParameterDataType>
117  BaseLib::ConfigTree const& process_config, std::string const& tag,
118  std::vector<std::unique_ptr<ParameterBase>> const& parameters,
119  int const num_components)
120 {
121  // Find parameter name in process config.
123  auto const name = process_config.getConfigParameter<std::string>(tag);
124 
125  return findParameter<ParameterDataType>(name, parameters, num_components);
126 }
127 } // ProcessLib
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)
static const double p
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
Parameter< ParameterDataType > & findParameter(std::string const &parameter_name, std::vector< std::unique_ptr< ParameterBase >> const &parameters, int const num_components)
Definition: ProcessUtils.h:60