OGS
ConstantParameter.cpp
Go to the documentation of this file.
1 
11 #include "ConstantParameter.h"
12 
13 #include "BaseLib/ConfigTree.h"
14 #include "BaseLib/Error.h"
15 #include "BaseLib/Logging.h"
16 
17 namespace ParameterLib
18 {
19 std::unique_ptr<ParameterBase> createConstantParameter(
20  std::string const& name, BaseLib::ConfigTree const& config)
21 {
23  config.checkConfigParameter("type", "Constant");
24 
25  // Optional case for single-component variables where the value can be used.
26  // If the 'value' tag is found, use it and return. Otherwise continue with
27  // then required tag 'values'.
28  {
29  auto const value =
31  config.getConfigParameterOptional<std::vector<double>>("value");
32 
33  if (value)
34  {
35  if (value->size() != 1)
36  {
37  OGS_FATAL(
38  "Expected to read exactly one value, but {:d} were given.",
39  value->size());
40  }
41  DBUG("Using value {:g} for constant parameter.", (*value)[0]);
42  return std::make_unique<ConstantParameter<double>>(name,
43  (*value)[0]);
44  }
45  }
46 
47  // Value tag not available; continue with required values tag.
48  std::vector<double> const values =
50  config.getConfigParameter<std::vector<double>>("values");
51 
52  if (values.empty())
53  {
54  OGS_FATAL("No value available for constant parameter.");
55  }
56 
57  DBUG("Using following values for the constant parameter:");
58  for (double const v : values)
59  {
60  (void)v; // unused value if building w/o DBUG output.
61  DBUG("\t{:g}", v);
62  }
63 
64  return std::make_unique<ConstantParameter<double>>(name, values);
65 }
66 
67 } // namespace ParameterLib
#define OGS_FATAL(...)
Definition: Error.h:26
void DBUG(char const *fmt, Args const &... args)
Definition: Logging.h:27
void checkConfigParameter(std::string const &param, T const &value) const
std::optional< T > getConfigParameterOptional(std::string const &param) const
T getConfigParameter(std::string const &param) const
std::unique_ptr< ParameterBase > createConstantParameter(std::string const &name, BaseLib::ConfigTree const &config)