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