OGS 6.2.1-97-g73d1aeda3
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  {
54  vec_x[vec_x.size() - 1] = _length;
55  }
56  else
57  {
58  vec_x.push_back(_length);
59  }
60  }
61  return vec_x;
62 }
63 
65  const double L, const std::size_t num_subdivisions, const double multiplier)
66  : _length{L}, _num_subdivisions{num_subdivisions}, _multiplier{multiplier}
67 {
68 }
69 
70 std::vector<double> GradualSubdivisionFixedNum::operator()() const
71 {
72  std::vector<double> subdivisions;
73  subdivisions.reserve(_num_subdivisions + 1);
74  subdivisions.push_back(0.0);
75  auto const q = _multiplier;
76 
77  if (q == 1.0) {
78  double const dx = _length / _num_subdivisions;
79 
80  for (std::size_t i = 1; i < _num_subdivisions; ++i) {
81  subdivisions.push_back(dx * i);
82  }
83  } else {
84  // compute initial subdivision size
85  auto const a =
86  _length * (q - 1.0) / (std::pow(q, _num_subdivisions) - 1.0);
87 
88  double qi = q; // q^i
89  for (std::size_t i = 1; i < _num_subdivisions; ++i) {
90  subdivisions.push_back(a * (qi - 1.0) / (q - 1.0));
91  qi *= q;
92  }
93  }
94 
95  subdivisions.push_back(_length);
96 
97  return subdivisions;
98 }
99 
100 } // namespace BaseLib
GradualSubdivisionFixedNum(const double L, const std::size_t num_subdivisions, const double multiplier)
Definition: Subdivision.cpp:64
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:70
Definition of the quicksort function.
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
GradualSubdivision(const double L, const double dL0, const double max_dL, const double multiplier)
Definition: Subdivision.cpp:19