OGS
Utils.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <vector>
14
15#include "BaseLib/ConfigTree.h"
16#include "BaseLib/Error.h"
17#include "Parameter.h"
18
19namespace ParameterLib
20{
26ParameterBase* findParameterByName(
27 std::string const& parameter_name,
28 std::vector<std::unique_ptr<ParameterBase>> const& parameters);
29
41template <typename ParameterDataType>
43 std::string const& parameter_name,
44 std::vector<std::unique_ptr<ParameterBase>> const& parameters,
45 int const num_components, MeshLib::Mesh const* const mesh = nullptr)
46{
47 // Find corresponding parameter by name.
48 ParameterBase* parameter_ptr =
49 findParameterByName(parameter_name, parameters);
50 if (parameter_ptr == nullptr)
51 {
52 return nullptr;
53 }
54
55 // Check the type correctness of the found parameter.
56 auto* const parameter =
57 dynamic_cast<Parameter<ParameterDataType>*>(parameter_ptr);
58 if (!parameter)
59 {
60 OGS_FATAL("The read parameter `{:s}' is of incompatible type.",
61 parameter_name);
62 }
63
64 if (num_components != 0 &&
65 parameter->getNumberOfGlobalComponents() != num_components)
66 {
68 "The read parameter `{:s}' has the wrong number of components "
69 "({:d} instead of {:d}).",
70 parameter_name, parameter->getNumberOfGlobalComponents(),
71 num_components);
72 }
73
74 // Test the parameter's mesh only if there is a "test"-mesh provided.
75 if (mesh != nullptr)
76 {
77 if (auto const error = isDefinedOnSameMesh(*parameter, *mesh))
78 {
80 "The found parameter is not suitable for the use on the "
81 "required mesh.\n{:s}",
82 error->c_str());
83 }
84 }
85
86 return parameter;
87}
88
100template <typename ParameterDataType>
102 std::string const& parameter_name,
103 std::vector<std::unique_ptr<ParameterBase>> const& parameters,
104 int const num_components, MeshLib::Mesh const* const mesh = nullptr)
105{
106 auto* parameter = findParameterOptional<ParameterDataType>(
107 parameter_name, parameters, num_components, mesh);
108
109 if (!parameter)
110 {
111 OGS_FATAL(
112 "Could not find parameter `{:s}' in the provided parameters list.",
113 parameter_name);
114 }
115 return *parameter;
116}
117
132template <typename ParameterDataType>
134 BaseLib::ConfigTree const& process_config, std::string const& tag,
135 std::vector<std::unique_ptr<ParameterBase>> const& parameters,
136 int const num_components, MeshLib::Mesh const* const mesh = nullptr)
137{
138 // Find parameter name in process config.
140 auto const name = process_config.getConfigParameter<std::string>(tag);
141
142 return findParameter<ParameterDataType>(name, parameters, num_components,
143 mesh);
144}
145
161template <typename ParameterDataType>
163 BaseLib::ConfigTree const& process_config, std::string const& tag,
164 std::vector<std::unique_ptr<ParameterBase>> const& parameters,
165 int const num_components, MeshLib::Mesh const* const mesh = nullptr)
166{
167 // Find parameter name in process config.
168 auto const name =
170 process_config.getConfigParameterOptional<std::string>(tag);
171
172 if (!name)
173 {
174 return nullptr;
175 }
176 return &findParameter<ParameterDataType>(*name, parameters, num_components,
177 mesh);
178}
179} // namespace ParameterLib
#define OGS_FATAL(...)
Definition Error.h:26
std::optional< T > getConfigParameterOptional(std::string const &param) const
T getConfigParameter(std::string const &param) const
Parameter< ParameterDataType > * findParameterOptional(std::string const &parameter_name, std::vector< std::unique_ptr< ParameterBase > > const &parameters, int const num_components, MeshLib::Mesh const *const mesh=nullptr)
Definition Utils.h:42
Parameter< ParameterDataType > * findOptionalTagParameter(BaseLib::ConfigTree const &process_config, std::string const &tag, std::vector< std::unique_ptr< ParameterBase > > const &parameters, int const num_components, MeshLib::Mesh const *const mesh=nullptr)
Definition Utils.h:162
std::optional< std::string > isDefinedOnSameMesh(ParameterBase const &parameter, MeshLib::Mesh const &mesh)
Definition Parameter.cpp:99
ParameterBase * findParameterByName(std::string const &parameter_name, std::vector< std::unique_ptr< ParameterBase > > const &parameters)
Definition Utils.cpp:15
Parameter< ParameterDataType > & findParameter(std::string const &parameter_name, std::vector< std::unique_ptr< ParameterBase > > const &parameters, int const num_components, MeshLib::Mesh const *const mesh=nullptr)
Definition Utils.h:101