OGS 6.1.0-1699-ge946d4c5f
Subdivision.cpp
Go to the documentation of this file.
1 
10 #include "Subdivision.h"
11 
12 #include <algorithm>
13 #include <cmath>
14 
15 #include <BaseLib/Error.h>
16 
17 namespace BaseLib
18 {
20  const double dL0,
21  const double max_dL,
22  const double multiplier)
23  : _length(L), _dL0(dL0), _max_dL(max_dL), _multiplier(multiplier)
24 {
25  // Check if accumulated subdivisions can ever sum up to length.
26  // Cf. geometric series formula.
27  if (multiplier < 1.0 && dL0 / (1.0 - multiplier) < L) {
28  OGS_FATAL(
29  "Using dL0=%g and multiplier=%g the generated subdivisions can not "
30  "sum up to a total length of %g.",
31  dL0,
32  multiplier,
33  L);
34  }
35 }
36 
37 std::vector<double> GradualSubdivision::operator()() const
38 {
39  std::vector<double> vec_x;
40 
41  double x = 0;
42  unsigned i = 0;
43  do {
44  vec_x.push_back(x);
45  x += std::min(_max_dL,
46  _dL0 * std::pow(_multiplier, static_cast<double>(i)));
47  i++;
48  } while (x < _length);
49 
50  if (vec_x.back() < _length) {
51  double last_dx = vec_x[vec_x.size() - 1] - vec_x[vec_x.size() - 2];
52  if (_length - vec_x.back() < last_dx)
53  vec_x[vec_x.size() - 1] = _length;
54  else
55  vec_x.push_back(_length);
56  }
57  return vec_x;
58 }
59 
61  const double L, const std::size_t num_subdivisions, const double multiplier)
62  : _length{L}, _num_subdivisions{num_subdivisions}, _multiplier{multiplier}
63 {
64 }
65 
66 std::vector<double> GradualSubdivisionFixedNum::operator()() const
67 {
68  std::vector<double> subdivisions;
69  subdivisions.reserve(_num_subdivisions + 1);
70  subdivisions.push_back(0.0);
71  auto const q = _multiplier;
72 
73  if (q == 1.0) {
74  double const dx = _length / _num_subdivisions;
75 
76  for (std::size_t i = 1; i < _num_subdivisions; ++i) {
77  subdivisions.push_back(dx * i);
78  }
79  } else {
80  // compute initial subdivision size
81  auto const a =
82  _length * (q - 1.0) / (std::pow(q, _num_subdivisions) - 1.0);
83 
84  double qi = q; // q^i
85  for (std::size_t i = 1; i < _num_subdivisions; ++i) {
86  subdivisions.push_back(a * (qi - 1.0) / (q - 1.0));
87  qi *= q;
88  }
89  }
90 
91  subdivisions.push_back(_length);
92 
93  return subdivisions;
94 }
95 
96 } // namespace BaseLib
GradualSubdivisionFixedNum(const double L, const std::size_t num_subdivisions, const double multiplier)
Definition: Subdivision.cpp:60
std::vector< double > operator()() const override
Returns a vector of subdivided points.
Definition: Subdivision.cpp:37
static const double q
std::vector< double > operator()() const override
Returns a vector of subdivided points.
Definition: Subdivision.cpp:66
Build information.
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
GradualSubdivision(const double L, const double dL0, const double max_dL, const double multiplier)
Definition: Subdivision.cpp:19