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"
19
20namespace ParameterLib
21{
22std::unique_ptr<ParameterBase> createRandomFieldMeshElementParameter(
23 std::string const& name, BaseLib::ConfigTree const& config,
24 MeshLib::Mesh& mesh)
25{
27 config.checkConfigParameter("type", "RandomFieldMeshElement");
28 auto const field_name =
30 config.getConfigParameter<std::string>("field_name");
31 auto const range =
33 config.getConfigParameter<std::vector<double>>("range");
34 if (range.size() != 2)
35 {
37 "The range needs to have two components, but {:d} were given.",
38 range.size());
39 }
40 auto const seed =
42 config.getConfigParameter<int>("seed");
43 DBUG("Generating field {:s} with range {:g} to {:g} and seed {:d}.",
44 field_name, range[0], range[1], seed);
45
46 std::vector<double> values(mesh.getElements().size());
47
48 std::mt19937 generator(seed);
49 std::uniform_real_distribution<> distr(range[0], range[1]);
50 auto gen = [&distr, &generator]() { return distr(generator); };
51 generate(begin(values), end(values), gen);
52
54 values);
55
56 auto const& property =
57 mesh.getProperties().getPropertyVector<double>(field_name);
58
59 if (property->getMeshItemType() != MeshLib::MeshItemType::Cell)
60 {
61 OGS_FATAL("The mesh property `{:s}' is not an element property.",
62 field_name);
63 }
64
65 return std::make_unique<RandomFieldMeshElementParameter<double>>(name, mesh,
66 *property);
67}
68
69} // namespace ParameterLib
#define OGS_FATAL(...)
Definition Error.h:26
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
Definition of the Mesh class.
T getConfigParameter(std::string const &param) const
void checkConfigParameter(std::string const &param, std::string_view const value) const
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:109
Properties & getProperties()
Definition Mesh.h:134
PropertyVector< T > const * getPropertyVector(std::string_view name) const
void addPropertyToMesh(Mesh &mesh, std::string_view name, MeshItemType item_type, std::size_t number_of_components, std::vector< T > const &values)
std::unique_ptr< ParameterBase > createRandomFieldMeshElementParameter(std::string const &name, BaseLib::ConfigTree const &config, MeshLib::Mesh &mesh)