OGS
Utils.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <vector>
14#include "BaseLib/ConfigTree.h"
15#include "BaseLib/Error.h"
16
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
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, MeshLib::Mesh const* const mesh = nullptr)
106{
107 auto* parameter = findParameterOptional<ParameterDataType>(
108 parameter_name, parameters, num_components, mesh);
109
110 if (!parameter)
111 {
112 OGS_FATAL(
113 "Could not find parameter `{:s}' in the provided parameters list.",
114 parameter_name);
115 }
116 return *parameter;
117}
118
133template <typename ParameterDataType>
135 BaseLib::ConfigTree const& process_config, std::string const& tag,
136 std::vector<std::unique_ptr<ParameterBase>> const& parameters,
137 int const num_components, MeshLib::Mesh const* const mesh = nullptr)
138{
139 // Find parameter name in process config.
141 auto const name = process_config.getConfigParameter<std::string>(tag);
142
143 return findParameter<ParameterDataType>(name, parameters, num_components,
144 mesh);
145}
146
162template <typename ParameterDataType>
164 BaseLib::ConfigTree const& process_config, std::string const& tag,
165 std::vector<std::unique_ptr<ParameterBase>> const& parameters,
166 int const num_components, MeshLib::Mesh const* const mesh = nullptr)
167{
168 // Find parameter name in process config.
169 auto const name =
171 process_config.getConfigParameterOptional<std::string>(tag);
172
173 if (!name)
174 {
175 return nullptr;
176 }
177 return &findParameter<ParameterDataType>(*name, parameters, num_components,
178 mesh);
179}
180} // 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:163
std::optional< std::string > isDefinedOnSameMesh(ParameterBase const &parameter, MeshLib::Mesh const &mesh)
Definition: Parameter.cpp:98
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:102