OGS
RandomFieldMeshElementParameter.cpp
Go to the documentation of this file.
1 
12 
13 #include <functional>
14 #include <random>
15 
16 #include "BaseLib/ConfigTree.h"
17 #include "MeshLib/Mesh.h"
18 
19 namespace ParameterLib
20 {
21 std::unique_ptr<ParameterBase> createRandomFieldMeshElementParameter(
22  std::string const& name, BaseLib::ConfigTree const& config,
23  MeshLib::Mesh& mesh)
24 {
26  config.checkConfigParameter("type", "RandomFieldMeshElement");
27  auto const field_name =
29  config.getConfigParameter<std::string>("field_name");
30  auto const range =
32  config.getConfigParameter<std::vector<double>>("range");
33  if (range.size() != 2)
34  {
35  OGS_FATAL(
36  "The range needs to have two components, but {:d} were given.",
37  range.size());
38  }
39  auto const seed =
41  config.getConfigParameter<int>("seed");
42  DBUG("Generating field {:s} with range {:g} to {:g} and seed {:d}.",
43  field_name, range[0], range[1], seed);
44 
45  std::vector<double> values(mesh.getElements().size());
46 
47  std::mt19937 generator(seed);
48  std::uniform_real_distribution<> distr(range[0], range[1]);
49  auto gen = [&distr, &generator]() { return distr(generator); };
50  generate(begin(values), end(values), gen);
51 
53  values);
54 
55  auto const& property =
56  mesh.getProperties().getPropertyVector<double>(field_name);
57 
58  if (property->getMeshItemType() != MeshLib::MeshItemType::Cell)
59  {
60  OGS_FATAL("The mesh property `{:s}' is not an element property.",
61  field_name);
62  }
63 
64  return std::make_unique<RandomFieldMeshElementParameter<double>>(name, mesh,
65  *property);
66 }
67 
68 } // namespace ParameterLib
#define OGS_FATAL(...)
Definition: Error.h:26
void DBUG(char const *fmt, Args const &... args)
Definition: Logging.h:27
Definition of the Mesh class.
void checkConfigParameter(std::string const &param, T const &value) const
T getConfigParameter(std::string const &param) const
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:98
Properties & getProperties()
Definition: Mesh.h:123
PropertyVector< T > const * getPropertyVector(std::string const &name) const
void addPropertyToMesh(Mesh &mesh, std::string const &name, MeshItemType item_type, std::size_t number_of_components, std::vector< T > const &values)
Definition: Mesh.h:193
std::unique_ptr< ParameterBase > createRandomFieldMeshElementParameter(std::string const &name, BaseLib::ConfigTree const &config, MeshLib::Mesh &mesh)