hp-FEM
Adaptive hp-FEM suite for nano-optics
Loading...
Searching...
No Matches
special_functions.hpp
Go to the documentation of this file.
1#pragma once
11
12#include <cmath>
13#include <complex>
14#include <vector>
15
16#if defined(HPFEM_USE_BOOST_MATH)
17#include <boost/math/special_functions/bessel.hpp>
18#endif
19
20#include "hpfem/core/error.hpp"
21#include "hpfem/core/types.hpp"
22
23namespace hpfem {
24
25#if defined(HPFEM_USE_BOOST_MATH)
26namespace detail {
29using SpecialMathPolicy = boost::math::policies::policy<
30 boost::math::policies::domain_error<boost::math::policies::errno_on_error>,
31 boost::math::policies::pole_error<boost::math::policies::errno_on_error>,
32 boost::math::policies::overflow_error<boost::math::policies::errno_on_error>,
33 boost::math::policies::evaluation_error<boost::math::policies::errno_on_error>,
34 boost::math::policies::promote_double<false>>;
35} // namespace detail
36#endif
37
40[[nodiscard]] inline Real cyl_bessel_j(Real nu, Real x) {
41#if defined(HPFEM_USE_BOOST_MATH)
42 return boost::math::cyl_bessel_j(nu, x, detail::SpecialMathPolicy());
43#else
44 return std::cyl_bessel_j(nu, x);
45#endif
46}
47
50[[nodiscard]] inline Real cyl_neumann(Real nu, Real x) {
51#if defined(HPFEM_USE_BOOST_MATH)
52 return boost::math::cyl_neumann(nu, x, detail::SpecialMathPolicy());
53#else
54 return std::cyl_neumann(nu, x);
55#endif
56}
57
59[[nodiscard]] inline Real sph_bessel(unsigned order, Real x) {
60#if defined(HPFEM_USE_BOOST_MATH)
61 return boost::math::sph_bessel(order, x, detail::SpecialMathPolicy());
62#else
63 return std::sph_bessel(order, x);
64#endif
65}
66
68[[nodiscard]] inline Real sph_neumann(unsigned order, Real x) {
69#if defined(HPFEM_USE_BOOST_MATH)
70 return boost::math::sph_neumann(order, x, detail::SpecialMathPolicy());
71#else
72 return std::sph_neumann(order, x);
73#endif
74}
75
78[[nodiscard]] inline Real bessel_j(int order, Real x) {
79 const Real value = cyl_bessel_j(static_cast<Real>(order < 0 ? -order : order), x);
80 return (order < 0 && (order % 2) != 0) ? -value : value;
81}
82
84[[nodiscard]] inline Real bessel_y(int order, Real x) {
85 const Real value = cyl_neumann(static_cast<Real>(order < 0 ? -order : order), x);
86 return (order < 0 && (order % 2) != 0) ? -value : value;
87}
88
90[[nodiscard]] inline Real bessel_j_derivative(int order, Real x) {
91 return bessel_j(order - 1, x) - static_cast<Real>(order) / x * bessel_j(order, x);
92}
93
95[[nodiscard]] inline Complex hankel1(int order, Real x) {
96 return {bessel_j(order, x), bessel_y(order, x)};
97}
98
100[[nodiscard]] inline Complex hankel1_derivative(int order, Real x) {
101 return hankel1(order - 1, x) - static_cast<Real>(order) / x * hankel1(order, x);
102}
103
104// --- spherical Bessel functions (Mie series of the sphere) --------------------------------------
105
107[[nodiscard]] inline Real spherical_bessel_j(int order, Real x) {
108 return sph_bessel(static_cast<unsigned>(order), x);
109}
110
112[[nodiscard]] inline Real spherical_bessel_y(int order, Real x) {
113 return sph_neumann(static_cast<unsigned>(order), x);
114}
115
118[[nodiscard]] inline Complex spherical_hankel1(int order, Real x) {
119 return {spherical_bessel_j(order, x), spherical_bessel_y(order, x)};
120}
121
127[[nodiscard]] inline std::vector<Complex> spherical_bessel_j(int max_order, Complex z) {
128 if (max_order < 0) throw InvalidArgument("spherical_bessel_j: max_order must be non-negative");
129 std::vector<Complex> j(static_cast<std::size_t>(max_order) + 1, Complex{0.0, 0.0});
130 if (std::abs(z) < 1e-300) {
131 j[0] = Complex{1.0, 0.0};
132 return j;
133 }
134 // start the recurrence where j_n is negligible relative to j_N; values grow while descending
135 const int start = max_order + 20 + static_cast<int>(std::ceil(std::abs(z)));
136 Complex next{0.0, 0.0}; // f_{n+1}
137 Complex current{1e-300, 0.0}; // f_n, arbitrary scale
138 for (int n = start; n > 0; --n) {
139 const Complex previous = static_cast<Real>(2 * n + 1) / z * current - next; // f_{n-1}
140 next = current;
141 current = previous;
142 if (n - 1 <= max_order) j[static_cast<std::size_t>(n - 1)] = current;
143 if (std::abs(current) > 1e200) { // rescale against overflow
144 constexpr Real kScale = 1e-200;
145 current *= kScale;
146 next *= kScale;
147 for (auto& v : j) v *= kScale;
148 }
149 }
150 const Complex scale = std::sin(z) / z / j[0];
151 for (auto& v : j) v *= scale;
152 return j;
153}
154
162[[nodiscard]] inline std::vector<Real> bessel_i_scaled(int max_order, Real x) {
163 if (max_order < 0) throw InvalidArgument("bessel_i_scaled: max_order must be non-negative");
164 if (!(x >= 0)) throw InvalidArgument("bessel_i_scaled: x must be non-negative");
165 std::vector<Real> out(static_cast<std::size_t>(max_order) + 1, 0.0);
166 if (x < 1e-150) { // I_n(x) = (x/2)^n / n! + …: below 1e-150 for n ≥ 1
167 out[0] = 1.0;
168 return out;
169 }
170 const int start = max_order + 32 + static_cast<int>(std::ceil(std::sqrt(80.0 * x)));
171 Real next = 0.0; // I_{n+1}
172 Real current = 1e-300; // I_n, arbitrary scale
173 Real sum = 0.0; // 2 Σ_{k ≥ n} I_k
174 for (int n = start; n > 0; --n) {
175 sum += 2.0 * current;
176 const Real previous = 2.0 * n / x * current + next; // I_{n-1}
177 next = current;
178 current = previous;
179 if (n - 1 <= max_order) out[static_cast<std::size_t>(n - 1)] = current;
180 if (current > 1e200) { // rescale against overflow
181 constexpr Real kScale = 1e-200;
182 current *= kScale;
183 next *= kScale;
184 sum *= kScale;
185 for (auto& v : out) v *= kScale;
186 }
187 }
188 sum += current; // + I_0
189 for (auto& v : out) v /= sum;
190 return out;
191}
192
193} // namespace hpfem
Definition error.hpp:15
Definition axisymmetric_estimator.hpp:39
double Real
Definition types.hpp:15
Complex spherical_hankel1(int order, Real x)
Definition special_functions.hpp:118
Complex hankel1_derivative(int order, Real x)
Derivative .
Definition special_functions.hpp:100
Real spherical_bessel_y(int order, Real x)
Spherical Bessel function of the second kind , n ≥ 0, x > 0.
Definition special_functions.hpp:112
Complex hankel1(int order, Real x)
Hankel function of the first kind , x > 0.
Definition special_functions.hpp:95
Real cyl_bessel_j(Real nu, Real x)
Definition special_functions.hpp:40
Real spherical_bessel_j(int order, Real x)
Spherical Bessel function of the first kind , n ≥ 0, real x ≥ 0.
Definition special_functions.hpp:107
Real sph_bessel(unsigned order, Real x)
Spherical Bessel function of the first kind , x ≥ 0 (std::sph_bessel).
Definition special_functions.hpp:59
Real bessel_j_derivative(int order, Real x)
Derivative .
Definition special_functions.hpp:90
std::vector< Real > bessel_i_scaled(int max_order, Real x)
Definition special_functions.hpp:162
Real bessel_y(int order, Real x)
Bessel function of the second kind , x > 0, any integer order.
Definition special_functions.hpp:84
Real bessel_j(int order, Real x)
Definition special_functions.hpp:78
Real cyl_neumann(Real nu, Real x)
Definition special_functions.hpp:50
Real sph_neumann(unsigned order, Real x)
Spherical Bessel function of the second kind , x > 0 (std::sph_neumann).
Definition special_functions.hpp:68
std::complex< double > Complex
Definition types.hpp:16