World Builder  1.1.0-pre
A geodynamic initial conditions generator
uniform.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"
31 
32 namespace WorldBuilder
33 {
34 
35  using namespace Utilities;
36 
37  namespace Features
38  {
39  namespace PlumeModels
40  {
41  namespace Grains
42  {
43  Uniform::Uniform(WorldBuilder::World *world_)
44  :
45  min_depth(NaN::DSNAN),
46  max_depth(NaN::DSNAN)
47 
48  {
49  this->world = world_;
50  this->name = "uniform";
51  }
52 
54  = default;
55 
56  void
57  Uniform::declare_entries(Parameters &prm, const std::string & /*unused*/)
58  {
59  // Document plugin and require entries if needed.
60  // Add compositions to the required parameters.
61  prm.declare_entry("", Types::Object({"compositions"}),
62  "Uniform grains model. All grains start exactly the same.");
63 
64  // Declare entries of this plugin
65  prm.declare_entry("min depth", Types::Double(0),
66  "The depth in meters from which the grains of this feature are present.");
67  prm.declare_entry("max depth", Types::Double(std::numeric_limits<double>::max()),
68  "The depth in meters to which the grains of this feature are present.");
69 
70  prm.declare_entry("compositions", Types::Array(Types::UnsignedInt(),0),
71  "A list with the integer labels of the composition which are present there.");
72 
73  prm.declare_entry("rotation matrices", Types::Array(Types::Array(Types::Array(Types::Double(0),3,3),3,3),0),
74  "A list with the rotation matrices of the grains which are present there for each compositions.");
75 
76  prm.declare_entry("Euler angles z-x-z", Types::Array(Types::Array(Types::Double(0),3,3),0),
77  "A list with the z-x-z Euler angles of the grains which are present there for each compositions.");
78 
79  prm.declare_entry("orientation operation", Types::String("replace", std::vector<std::string> {"replace", "multiply"}),
80  "Whether the value should replace any value previously defined at this location (replace) or "
81  "add the value to the previously define value (add, not implemented). Replacing implies that all values not "
82  "explicitly defined are set to zero.");
83 
84  prm.declare_entry("grain sizes",
86  "A list of the size of all of the grains in each composition. If set to <0, the size will be set so that the total is equal to 1.");
87 
88 
89  }
90 
91  void
93  {
94  min_depth = prm.get<double>("min depth");
95  max_depth = prm.get<double>("max depth");
96  compositions = prm.get_vector<unsigned int>("compositions");
97 
98  const bool set_euler_angles = prm.check_entry("Euler angles z-x-z");
99  const bool set_rotation_matrices = prm.check_entry("rotation matrices");
100 
101  WBAssertThrow(!(set_euler_angles == true && set_rotation_matrices == true),
102  "Only Euler angles or Rotation matrices may be set, but both are set for " << prm.get_full_json_path());
103 
104 
105  WBAssertThrow(!(set_euler_angles == false && set_rotation_matrices == false),
106  "Euler angles or Rotation matrices have to be set, but neither are set for " << prm.get_full_json_path());
107 
108  if (set_euler_angles)
109  {
110  std::vector<std::array<double,3> > euler_angles_vector = prm.get_vector<std::array<double,3> >("Euler angles z-x-z");
111  rotation_matrices.resize(euler_angles_vector.size());
112  for (size_t i = 0; i<euler_angles_vector.size(); ++i)
113  {
114  rotation_matrices[i] = Utilities::euler_angles_to_rotation_matrix(euler_angles_vector[i][0],euler_angles_vector[i][1],euler_angles_vector[i][2]);
115  }
116 
117  }
118  else
119  {
120  rotation_matrices = prm.get_vector<std::array<std::array<double,3>,3> >("rotation matrices");
121  }
122 
123  operation = prm.get<std::string>("orientation operation");
124  grain_sizes = prm.get_vector<double>("grain sizes");
125 
126 
127  WBAssertThrow(compositions.size() == rotation_matrices.size(),
128  "There are not the same amount of compositions (" << compositions.size()
129  << ") and rotation_matrices (" << rotation_matrices.size() << ").");
130  WBAssertThrow(compositions.size() == grain_sizes.size(),
131  "There are not the same amount of compositions (" << compositions.size()
132  << ") and grain_sizes (" << grain_sizes.size() << ").");
133  }
134 
135 
137  Uniform::get_grains(const Point<3> & /*position_in_cartesian_coordinates*/,
138  const Objects::NaturalCoordinate & /*position_in_natural_coordinates*/,
139  const double depth,
140  const unsigned int composition_number,
141  WorldBuilder::grains grains_,
142  const double /*feature_min_depth*/,
143  const double /*feature_max_depth*/) const
144  {
145  WorldBuilder::grains grains_local = grains_;
146  if (depth <= max_depth && depth >= min_depth)
147  {
148  for (unsigned int i =0; i < compositions.size(); ++i)
149  {
150  if (compositions[i] == composition_number)
151  {
152  std::fill(grains_local.rotation_matrices.begin(),grains_local.rotation_matrices.end(),rotation_matrices[i]);
153 
154  const double size = grain_sizes[i] < 0 ? 1.0/static_cast<double>(grains_local.sizes.size()) : grain_sizes[i];
155  std::fill(grains_local.sizes.begin(),grains_local.sizes.end(),size);
156 
157  return grains_local;
158  }
159  }
160  }
161  return grains_local;
162  }
164  } // namespace Grains
165  } // namespace PlumeModels
166  } // namespace Features
167 } // namespace WorldBuilder
168 
169 
bool check_entry(const std::string &name) const
Definition: parameters.cc:205
const double DSNAN
Definition: nan.h:41
static void declare_entries(Parameters &prm, const std::string &parent_name="")
Definition: uniform.cc:57
WorldBuilder::grains get_grains(const Point< 3 > &position, const Objects::NaturalCoordinate &position_in_natural_coordinates, const double depth, const unsigned int composition_number, WorldBuilder::grains grains, const double feature_min_depth, const double feature_max_depth) const override final
Definition: uniform.cc:137
std::vector< double > sizes
Definition: grains.h:47
#define WB_REGISTER_FEATURE_PLUME_GRAINS_MODEL(classname, name)
Definition: interface.h:152
std::string get_full_json_path(size_t max_size=std::numeric_limits< size_t >::max()) const
Definition: parameters.cc:1933
std::vector< std::array< std::array< double, 3 >, 3 > > rotation_matrices
Definition: uniform.h:103
#define WBAssertThrow(condition, message)
Definition: assert.h:40
std::array< std::array< double, 3 >, 3 > euler_angles_to_rotation_matrix(double phi1_d, double theta_d, double phi2_d)
Definition: utilities.cc:1147
std::vector< std::array< std::array< double, 3 >, 3 > > rotation_matrices
Definition: grains.h:51
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) override final
Definition: uniform.cc:92