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