OGS 6.2.1-97-g73d1aeda3
Parameter.cpp
Go to the documentation of this file.
1 
10 #include "Parameter.h"
11 #include "BaseLib/ConfigTree.h"
12 #include "BaseLib/Error.h"
13 
14 #include "ConstantParameter.h"
15 #include "CurveScaledParameter.h"
16 #include "FunctionParameter.h"
17 #include "GroupBasedParameter.h"
18 #include "MeshElementParameter.h"
19 #include "MeshNodeParameter.h"
21 
22 namespace ParameterLib
23 {
24 std::unique_ptr<ParameterBase> createParameter(
25  BaseLib::ConfigTree const& config,
26  std::vector<std::unique_ptr<MeshLib::Mesh>> const& meshes,
27  std::map<std::string,
28  std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
29  curves)
30 {
32  auto const name = config.getConfigParameter<std::string>("name");
34  auto const type = config.peekConfigParameter<std::string>("type");
35 
36  // Either the mesh name is given, or the first mesh's name will be
37  // taken.
38  auto const mesh_name =
40  config.getConfigParameter<std::string>("mesh", meshes[0]->getName());
41 
42  auto const& mesh = *BaseLib::findElementOrError(
43  begin(meshes), end(meshes),
44  [&mesh_name](auto const& m) { return m->getName() == mesh_name; },
45  "Expected to find a mesh named " + mesh_name + ".");
46 
47  // Create parameter based on the provided type.
48  if (type == "Constant")
49  {
50  INFO("ConstantParameter: %s", name.c_str());
51  return createConstantParameter(name, config);
52  }
53  if (type == "CurveScaled")
54  {
55  INFO("CurveScaledParameter: %s", name.c_str());
56  return createCurveScaledParameter(name, config, curves);
57  }
58  if (type == "Function")
59  {
60  INFO("FunctionParameter: %s", name.c_str());
61  return createFunctionParameter(name, config, mesh);
62  }
63  if (type == "Group")
64  {
65  INFO("GroupBasedParameter: %s", name.c_str());
66  return createGroupBasedParameter(name, config, mesh);
67  }
68  if (type == "MeshElement")
69  {
70  INFO("MeshElementParameter: %s", name.c_str());
71  return createMeshElementParameter(name, config, mesh);
72  }
73  if (type == "MeshNode")
74  {
75  INFO("MeshNodeParameter: %s", name.c_str());
76  return createMeshNodeParameter(name, config, mesh);
77  }
78  if (type == "TimeDependentHeterogeneousParameter")
79  {
80  INFO("TimeDependentHeterogeneousParameter: %s", name.c_str());
82  }
83 
84  OGS_FATAL("Cannot construct a parameter of given type '%s'.", type.c_str());
85 }
86 
87 boost::optional<std::string> isDefinedOnSameMesh(ParameterBase const& parameter,
88  MeshLib::Mesh const& mesh)
89 {
90  // Arbitrary domain of definition.
91  if (parameter.mesh() == nullptr)
92  {
93  return {};
94  }
95 
96  // Equal meshes.
97  if (*parameter.mesh() == mesh)
98  {
99  return {};
100  }
101 
102  return "The parameter's domain of definition mesh '" +
103  parameter.mesh()->getName() + "' differs from the used mesh '" +
104  mesh.getName() +
105  "'. The same mesh (the same name) has to be referenced in the "
106  "project file. Possible reasons are:\n - the parameter used for the "
107  "initial condition is not defined on the bulk mesh,\n - the "
108  "parameter's domain of definition mesh differs from the boundary "
109  "condition or source term domain of definition mesh.";
110 }
111 } // namespace ParameterLib
std::unique_ptr< ParameterBase > createCurveScaledParameter(std::string const &name, BaseLib::ConfigTree const &config, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation >> const &curves)
std::unique_ptr< ParameterBase > createParameter(BaseLib::ConfigTree const &config, std::vector< std::unique_ptr< MeshLib::Mesh >> const &meshes, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation >> const &curves)
Definition: Parameter.cpp:24
std::unique_ptr< ParameterBase > createFunctionParameter(std::string const &name, BaseLib::ConfigTree const &config, MeshLib::Mesh const &mesh)
std::unique_ptr< ParameterBase > createConstantParameter(std::string const &name, BaseLib::ConfigTree const &config)
T getConfigParameter(std::string const &param) const
boost::optional< std::string > isDefinedOnSameMesh(ParameterBase const &parameter, MeshLib::Mesh const &mesh)
Definition: Parameter.cpp:87
const std::string getName() const
Get name of the mesh.
Definition: Mesh.h:102
std::unique_ptr< ParameterBase > createTimeDependentHeterogeneousParameter(std::string const &name, BaseLib::ConfigTree const &config)
T peekConfigParameter(std::string const &param) const
std::unique_ptr< ParameterBase > createGroupBasedParameter(std::string const &name, BaseLib::ConfigTree const &config, MeshLib::Mesh const &mesh)
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
MeshLib::Mesh const * mesh() const
Definition: Parameter.h:71
std::iterator_traits< InputIt >::reference findElementOrError(InputIt begin, InputIt end, Predicate predicate, std::string const &error="")
Definition: Algorithm.h:67
std::unique_ptr< ParameterBase > createMeshElementParameter(std::string const &name, BaseLib::ConfigTree const &config, MeshLib::Mesh const &mesh)
std::unique_ptr< ParameterBase > createMeshNodeParameter(std::string const &name, BaseLib::ConfigTree const &config, MeshLib::Mesh const &mesh)