World Builder  1.1.0-pre
A geodynamic initial conditions generator
random.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018-2024 by the authors of the World Builder code.
3 
4  This file is part of the World Builder.
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19 
21 
22 
23 #include "world_builder/nan.h"
30 #include "world_builder/world.h"
31 #include <random>
32 
33 
34 
35 namespace WorldBuilder
36 {
37 
38  using namespace Utilities;
39 
40  namespace Features
41  {
42  using namespace FeatureUtilities;
43  namespace ContinentalPlateModels
44  {
45  namespace Composition
46  {
47  Random::Random(WorldBuilder::World *world_)
48  :
49  min_depth(NaN::DSNAN),
50  max_depth(NaN::DSNAN)
51  {
52  this->world = world_;
53  this->name = "random";
54  }
55 
57  = default;
58 
59  void
60  Random::declare_entries(Parameters &prm, const std::string & /*unused*/)
61  {
62  // Document plugin and require entries if needed.
63  // Add compositions to the required parameters.
64  prm.declare_entry("", Types::Object({"compositions"}),
65  "Uniform compositional model. Sets constant compositional field.");
66 
67  // Declare entries of this plugin
69  "The depth in meters from which the composition of this feature is present.");
70 
71  prm.declare_entry("max depth", Types::OneOf(Types::Double(std::numeric_limits<double>::max()),Types::Array(Types::ValueAtPoints(std::numeric_limits<double>::max(), 2.))),
72  "The depth in meters to which the composition of this feature is present.");
73 
74  prm.declare_entry("compositions", Types::Array(Types::UnsignedInt(),0),
75  "A list with the labels of the composition which are present there.");
76 
77  prm.declare_entry("min value", Types::Array(Types::Double(0.0),1),
78  "Minimum value of the range within which we want to generate a random compositional value "
79  "corresponding to the compositional field.");
80 
81  prm.declare_entry("max value", Types::Array(Types::Double(1.0),1),
82  "Maximum value of the range within which we want to generate a random compositional value "
83  "corresponding to the compositional field.");
84 
85  prm.declare_entry("operation", Types::String("replace", std::vector<std::string> {"replace", "replace defined only", "add", "subtract"}),
86  "Whether the value should replace any value previously defined at this location (replace) or "
87  "add the value to the previously define value. Replacing implies that all compositions not "
88  "explicitly defined are set to zero. To only replace the defined compositions use the replace only defined option.");
89 
90  }
91 
92  void
93  Random::parse_entries(Parameters &prm, const std::vector<Point<2>> &coordinates)
94  {
95  min_depth_surface = Objects::Surface(prm.get("min depth",coordinates));
97  max_depth_surface = Objects::Surface(prm.get("max depth",coordinates));
99 
100  compositions = prm.get_vector<unsigned int>("compositions");
101  min_value = prm.get_vector<double>("min value");
102  max_value = prm.get_vector<double>("max value");
103  operation = string_operations_to_enum(prm.get<std::string>("operation"));
104  }
105 
106 
107  double
108  Random::get_composition(const Point<3> & /*position_in_cartesian_coordinates*/,
109  const Objects::NaturalCoordinate &position_in_natural_coordinates,
110  const double depth,
111  const unsigned int composition_number,
112  double composition,
113  const double /*feature_min_depth*/,
114  const double /*feature_max_depth*/) const
115  {
116  if (depth <= max_depth && depth >= min_depth)
117  {
118  const double min_depth_local = min_depth_surface.constant_value ? min_depth : min_depth_surface.local_value(position_in_natural_coordinates.get_surface_point()).interpolated_value;
119  const double max_depth_local = max_depth_surface.constant_value ? max_depth : max_depth_surface.local_value(position_in_natural_coordinates.get_surface_point()).interpolated_value;
120  if (depth <= max_depth_local && depth >= min_depth_local)
121  {
122  for (unsigned int i : compositions)
123  {
124  if (i == composition_number)
125  {
126  std::uniform_real_distribution<> dist(min_value[0], max_value[0]);
127  const double composition_ = dist(world->get_random_number_engine());
128  return apply_operation(operation,composition,composition_);
129  }
130  }
131 
132  if (operation == Operations::REPLACE)
133  return 0.0;
134  }
135  }
136  return composition;
137  }
139  } // namespace Composition
140  } // namespace ContinentalPlateModels
141  } // namespace Features
142 } // namespace WorldBuilder
143 
144 
double get_composition(const Point< 3 > &position, const Objects::NaturalCoordinate &position_in_natural_coordinates, const double depth, const unsigned int composition_number, double composition, const double feature_min_depth, const double feature_max_depth) const override final
Definition: random.cc:108
const double DSNAN
Definition: nan.h:41
std::mt19937 & get_random_number_engine()
Definition: world.cc:556
#define WB_REGISTER_FEATURE_CONTINENTAL_PLATE_COMPOSITION_MODEL(classname, name)
Definition: interface.h:146
Operations string_operations_to_enum(const std::string &operation)
SurfaceValueInfo local_value(const Point< 2 > &check_point) const
Definition: surface.cc:149
static void declare_entries(Parameters &prm, const std::string &parent_name="")
Definition: random.cc:60
double apply_operation(const Operations operation, const double old_value, const double new_value)
void declare_entry(const std::string &name, const Types::Interface &type, const std::string &documentation)
Definition: parameters.cc:197
std::vector< T > get_vector(const std::string &name)
T get(const std::string &name)
void parse_entries(Parameters &prm, const std::vector< Point< 2 >> &coordinates) override final
Definition: random.cc:93