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