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