OGS
CreateFunction.cpp
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#include <vector>
5
7#include "Function.h"
9
10namespace MaterialPropertyLib
11{
12namespace
13{
16template <typename ConfigSubtreeList>
17std::vector<std::string> parseExpressionList(
18 ConfigSubtreeList const& expression_configs)
19{
20 std::vector<std::string> expressions;
21 expressions.reserve(expression_configs.size());
22 std::transform(std::begin(expression_configs), std::end(expression_configs),
23 std::back_inserter(expressions),
24 [](BaseLib::ConfigTree const& p)
25 { return p.getValue<std::string>(); });
26 return expressions;
27}
28} // namespace
29
30std::unique_ptr<Function> createFunction(
31 BaseLib::ConfigTree const& config,
32 std::map<std::string,
33 std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
34 curves)
35{
37 config.checkConfigParameter("type", "Function");
38
39 // Second access for storage.
41 auto property_name = config.peekConfigParameter<std::string>("name");
42
43 DBUG("Create Function property {:s}.", property_name);
44
46 auto const& value_config = config.getConfigSubtree("value");
47
48 auto const value_expressions = parseExpressionList(
50 value_config.getConfigSubtreeList("expression"));
51
52 // For each derivative a name of the variable and the list of expressions.
53 std::vector<std::pair<std::string, std::vector<std::string>>>
54 dvalue_expressions;
56 for (auto const& dvalue_config : config.getConfigSubtreeList("dvalue"))
57 {
58 auto variable_name =
60 dvalue_config.getConfigParameter<std::string>("variable_name");
61
62 auto expressions = parseExpressionList(
64 dvalue_config.getConfigSubtreeList("expression"));
65
66 dvalue_expressions.emplace_back(std::move(variable_name),
67 std::move(expressions));
68 }
69
70 // For each second derivative: two variable names and expression list.
71 std::vector<D2ValueConfig> d2value_expressions;
73 for (auto const& d2value_config : config.getConfigSubtreeList("d2value"))
74 {
75 auto variable_names_range =
77 d2value_config.getConfigParameterList<std::string>("variable_name");
78
79 std::vector<std::string> variable_names(variable_names_range.begin(),
80 variable_names_range.end());
81
82 if (variable_names.size() != 2)
83 {
85 "Function property '{}': each <d2value> block must contain "
86 "exactly two <variable_name> entries, but {:d} were given.",
87 property_name, variable_names.size());
88 }
89
90 auto expressions = parseExpressionList(
92 d2value_config.getConfigSubtreeList("expression"));
93
94 d2value_expressions.emplace_back(std::move(variable_names[0]),
95 std::move(variable_names[1]),
96 std::move(expressions));
97 }
98
99 return std::make_unique<MaterialPropertyLib::Function>(
100 std::move(property_name),
101 value_expressions,
102 dvalue_expressions,
103 d2value_expressions,
104 curves);
105}
106} // namespace MaterialPropertyLib
#define OGS_FATAL(...)
Definition Error.h:10
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
T peekConfigParameter(std::string const &param) const
Range< SubtreeIterator > getConfigSubtreeList(std::string const &root) const
ConfigTree getConfigSubtree(std::string const &root) const
void checkConfigParameter(std::string const &param, std::string_view const value) const
std::vector< std::string > parseExpressionList(ConfigSubtreeList const &expression_configs)
std::unique_ptr< Function > createFunction(BaseLib::ConfigTree const &config, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves)