Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
additive_recurrence.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2025 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes 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 GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22
23#ifndef VULPES_UTILS_RANDOM_ADDITIVE_RECURRENCE_HPP_
24#define VULPES_UTILS_RANDOM_ADDITIVE_RECURRENCE_HPP_
25
26#include <array>
27#include <cassert>
28#include <cmath>
29#include <cstdlib>
30#include <cstring>
31#include <ctime>
32#include <limits>
33#include <vector>
34
36
37namespace vulpes {
38namespace random {
39
40namespace internal {
58inline double computePhiM(std::size_t m) {
59 double phi = 1.0;
60 constexpr std::size_t iter_max = 30;
61 for (std::size_t it = 0; it < iter_max; ++it) {
62 const double phim = vmath::upow(phi, m);
63 const double dphi =
64 (phi * (phim - 1) - 1) / (static_cast<double>(m + 1) * phim - 1);
65 phi -= dphi;
66 if (std::abs(dphi) < 2 * std::numeric_limits<double>::epsilon()) {
67 break;
68 }
69 }
70 return phi;
71}
72} // namespace internal
73
101template <std::size_t M>
102std::vector<std::array<double, M>> additiveRecurrence(
103 std::size_t n0, std::size_t n1) {
104 std::array<double, M> phi_inv_Md;
105 // if M is large this is not accurate since phi approaches 1
106 phi_inv_Md[0] = 1.0 / internal::computePhiM(M);
107 for (std::size_t m = 1; m < M; ++m) {
108 phi_inv_Md[m] = phi_inv_Md[m - 1] * phi_inv_Md[0];
109 }
110 std::vector<std::array<double, M>> pts;
111 for (std::size_t i = n0; i < n1; ++i) {
112 std::array<double, M> pt;
113 for (std::size_t m = 0; m < M; ++m) {
114 pt[m] = std::fmod((0.5 + phi_inv_Md[m] * i), 1.0);
115 }
116 pts.push_back(pt);
117 }
118 return pts;
119}
120
121} // namespace random
122} // namespace vulpes
123
124#endif // VULPES_UTILS_RANDOM_ADDITIVE_RECURRENCE_HPP_
Definition additive_recurrence.hpp:40
double computePhiM(std::size_t m)
Computes the solution to the equation using Newton's method.
Definition additive_recurrence.hpp:58
Definition additive_recurrence.hpp:38
std::vector< std::array< double, M > > additiveRecurrence(std::size_t n0, std::size_t n1)
Generates a sequence of points using an additive recurrence formula.
Definition additive_recurrence.hpp:102
constexpr U upow(U base, V exp, U result=U(1))
Computes the unsigned power of a number.
Definition vmath.hpp:107
Definition cell_manager.hpp:38