Loading...
Searching...
No Matches
QuantumBridgeQuadrature.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
6#pragma once
7#include <algorithm>
8#include <array>
9#include <cmath>
10#include <cstddef>
11#include <limits>
12#include <stdexcept>
13#include <vector>
15using Real = long double;
16using Value = std::vector<Real>;
23inline void accumulate(const Value &value, Real wk, Real wg, Value &k, Value &g,
24 Value &absolute)
25{
26 if (value.size() != k.size())
27 throw std::invalid_argument("Quadrature vector dimensions differ");
28 for (std::size_t j = 0; j < value.size(); ++j) {
29 if (!std::isfinite(value[j]))
30 throw std::overflow_error("Nonfinite quadrature integrand");
31 k[j] += wk * value[j];
32 g[j] += wg * value[j];
33 absolute[j] += wk * std::abs(value[j]);
34 }
35}
36inline Panel finish(Real left, Real right, const Value &k, const Value &g,
37 const Value &absolute)
38{
39 Panel result;
40 result.left = left;
41 result.right = right;
42 result.integral.resize(k.size());
43 result.error.resize(k.size());
44 const Real half = (right - left) / 2;
45 for (std::size_t j = 0; j < k.size(); ++j) {
46 result.integral[j] = half * k[j];
47 result.error[j] = std::max(
48 std::abs(half * (k[j] - g[j])),
49 64 * std::numeric_limits<Real>::epsilon() * half * absolute[j]);
50 }
51 return result;
52}
53inline constexpr std::array<Real, 8> nodes = {
54 .991455371120812639206854697526329L, .949107912342758524526189684047851L,
55 .864864423359769072789712788640926L, .741531185599394439863864773280788L,
56 .586087235467691130294144838258730L, .405845151377397166906606412076961L,
57 .207784955007898467600689403773245L, 0.L};
58inline constexpr std::array<Real, 8> kronrod = {
59 .022935322010529224963732008058970L, .063092092629978553290700663189204L,
60 .104790010322250183839876322541518L, .140653259715525918745189590510238L,
61 .169004726639267902826583426598550L, .190350578064785409913256402421014L,
62 .204432940075298892414161999234649L, .209482141084727828012999174891714L};
63inline constexpr std::array<Real, 4> gauss = {
64 .129484966168869693270611432679082L, .279705391489276667901467771423780L,
65 .381830050505118944950369775488975L, .417959183673469387755102040816327L};
66template <class Function>
67Panel panel(Real left, Real right, const Function &function,
68 std::size_t &evaluations)
69{
70 if (!std::isfinite(left) || !std::isfinite(right) ||
71 !std::isgreater(right, left))
72 throw std::invalid_argument(
73 "Finite ordered quadrature endpoints required");
74 const Real center = left + (right - left) / 2;
75 const Real half = (right - left) / 2;
76 const Value value = function(center);
77 ++evaluations;
78 Value k(value.size(), 0), g(value.size(), 0), absolute(value.size(), 0);
79 accumulate(value, kronrod[7], gauss[3], k, g, absolute);
80 for (std::size_t i = 0; i < 7; ++i) {
81 const Real wg = i % 2 ? gauss[i / 2] : 0;
82 accumulate(function(center - half * nodes[i]), kronrod[i], wg, k, g,
83 absolute);
84 accumulate(function(center + half * nodes[i]), kronrod[i], wg, k, g,
85 absolute);
86 evaluations += 2;
87 }
88 return finish(left, right, k, g, absolute);
89}
90} // namespace quantumbridge::quadrature
constexpr std::array< Real, 8 > kronrod
constexpr std::array< Real, 8 > nodes
Panel finish(Real left, Real right, const Value &k, const Value &g, const Value &absolute)
constexpr std::array< Real, 4 > gauss
void accumulate(const Value &value, Real wk, Real wg, Value &k, Value &g, Value &absolute)
Panel panel(Real left, Real right, const Function &function, std::size_t &evaluations)