OGS
ParameterLib/Utils.h
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#pragma once
5
6#include <vector>
7
10#include "BaseLib/Error.h"
11#include "Parameter.h"
12
13namespace ParameterLib
14{
21 std::string const& parameter_name,
22 std::vector<std::unique_ptr<ParameterBase>> const& parameters);
23
35template <typename ParameterDataType>
37 std::string const& parameter_name,
38 std::vector<std::unique_ptr<ParameterBase>> const& parameters,
39 int const num_components, MeshLib::Mesh const* const mesh = nullptr)
40{
41 // Find corresponding parameter by name.
42 ParameterBase* parameter_ptr =
43 findParameterByName(parameter_name, parameters);
44 if (parameter_ptr == nullptr)
45 {
46 return nullptr;
47 }
48
49 // Check the type correctness of the found parameter.
50 auto* const parameter =
51 dynamic_cast<Parameter<ParameterDataType>*>(parameter_ptr);
52 if (!parameter)
53 {
54 OGS_FATAL("The read parameter `{:s}' is of incompatible type.",
55 parameter_name);
56 }
57
58 if (num_components != 0 &&
59 parameter->getNumberOfGlobalComponents() != num_components)
60 {
62 "The read parameter `{:s}' has the wrong number of components "
63 "({:d} instead of {:d}).",
64 parameter_name, parameter->getNumberOfGlobalComponents(),
65 num_components);
66 }
67
68 // Test the parameter's mesh only if there is a "test"-mesh provided.
69 if (mesh != nullptr)
70 {
71 if (auto const error = isDefinedOnSameMesh(*parameter, *mesh))
72 {
74 "The found parameter is not suitable for the use on the "
75 "required mesh.\n{:s}",
76 error->c_str());
77 }
78 }
79
80 return parameter;
81}
82
94template <typename ParameterDataType>
96 std::string const& parameter_name,
97 std::vector<std::unique_ptr<ParameterBase>> const& parameters,
98 int const num_components,
99 MeshLib::Mesh const* const mesh = nullptr)
100{
102 parameter_name, parameters, num_components, mesh);
103
104 if (!parameter)
105 {
106 OGS_FATAL(
107 "Could not find parameter `{:s}' in the provided parameters list.",
108 parameter_name);
109 }
110 return *parameter;
111}
112
127template <typename ParameterDataType>
129 BaseLib::ConfigTree const& process_config,
130 std::string const& tag,
131 std::vector<std::unique_ptr<ParameterBase>> const& parameters,
132 int const num_components,
133 MeshLib::Mesh const* const mesh = nullptr)
134{
135 // Find parameter name in process config.
137 auto const name = process_config.getConfigParameter<std::string>(tag);
138
139 return findParameter<ParameterDataType>(name, parameters, num_components,
140 mesh);
141}
142
158template <typename ParameterDataType>
160 BaseLib::ConfigTree const& process_config, std::string const& tag,
161 std::vector<std::unique_ptr<ParameterBase>> const& parameters,
162 int const num_components, MeshLib::Mesh const* const mesh = nullptr)
163{
164 // Find parameter name in process config.
165 auto const name =
167 process_config.getConfigParameterOptional<std::string>(tag);
168
169 if (!name)
170 {
171 return nullptr;
172 }
173 return &findParameter<ParameterDataType>(*name, parameters, num_components,
174 mesh);
175}
176} // namespace ParameterLib
#define OGS_NO_DANGLING
#define OGS_FATAL(...)
Definition Error.h:19
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)
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)
std::optional< std::string > isDefinedOnSameMesh(ParameterBase const &parameter, MeshLib::Mesh const &mesh)
ParameterBase * findParameterByName(std::string const &parameter_name, std::vector< std::unique_ptr< ParameterBase > > const &parameters)
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)