OpenBEM
Open-source framework for electromagnetic simulation with the boundary element method.
Loading...
Searching...
No Matches
hgf.cpp
Go to the documentation of this file.
1// OpenBEM - Copyright (C) 2026 Shashwat Sharma
2
3// This file is part of OpenBEM.
4
5// OpenBEM is free software: you can redistribute it and/or modify it under the terms of the
6// GNU General Public License as published by the Free Software Foundation, either version 3
7// of the License, or (at your option) any later version.
8
9// You should have received a copy of the GNU General Public License along with OpenBEM.
10// If not, see <https://www.gnu.org/licenses/>.
11
12
18#include "kernels/hgf.hpp"
19
20#include <cassert>
21#include <limits>
22#include <cmath>
23
24#include "types.hpp"
25#include "constants.hpp"
26
27
28namespace bem
29{
30
34 const Complex k
35 ) const
36{
37 EigRowVec<Float> r = (r_src.colwise() - r_obs).colwise().norm();
38 assert(r.array().all() > 0 && "HGF::compute(): Distance must be greater than 0.");
39 return Eigen::exp(-J * k * r.array()) / r.array() / four_pi;
40}
41
42
46 const Complex k
47 ) const
48{
49 EigMatNX<Float, 3> r_diff = -(r_src.colwise() - r_obs);
50 EigRowVec<Float> r = r_diff.colwise().norm();
51 assert(r.array().all() > 0 && "HGF::compute_grad(): Distance must be greater than 0.");
52
53 const EigRowVec<Complex> jkr = J * k * r.array();
54 const EigRowVec<Complex> scalar_term = -Eigen::exp(-jkr.array()) * (one + jkr.array()) /
55 (r.array() * r.array() * r.array()) / four_pi;
56
57 EigMatNX<Complex, 3> result = r_diff.array().rowwise() * scalar_term.array();
58 return result;
59}
60
61
65 const Complex k
66 ) const
67{
68 EigRowVec<Float> r = (r_src.colwise() - r_obs).colwise().norm();
69 assert(r.array().all() > 0 && "SingularitySubtractedHGF::compute(): Distance must be greater than 0.");
70 return (Eigen::exp(-J * k * r.array()) - one) / r.array() / four_pi;
71}
72
73
77 const Complex k
78 ) const
79{
80 EigMatNX<Float, 3> r_diff = -(r_src.colwise() - r_obs);
81 EigRowVec<Float> r = r_diff.colwise().norm();
82 assert(r.array().all() > 0 && "SingularitySubtractedHGF::compute_grad(): Distance must be greater than 0.");
83
84 const EigRowVec<Complex> jkr = J * k * r.array();
85 const EigRowVec<Float> r_sq = r.array() * r.array();
86 const EigRowVec<Float> r_cu = r_sq.array() * r.array();
87
89 Eigen::exp(-jkr.array()) * (one + jkr.array()) - (one + half * k * k * r_sq.array())
90 ) / r_cu.array() / four_pi;
91
92 EigMatNX<Complex, 3> result = r_diff.array().rowwise() * scalar_term.array();
93 return result;
94}
95
96
100 const Complex k
101 ) const
102{
103 EigRowVec<Float> r = (r_src.colwise() - r_obs).colwise().norm();
104
105 if (std::abs(k) == 0.0)
106 return EigRowVec<Complex>::Zero(1, r.cols());
107
108 const Float tol = KERNEL_DEFAULT_TOL;
109 const Complex jk = J * k;
110 const EigRowVec<Complex> jkr = jk * r;
111
114
115 for (uint32_t jj = 2; true; jj++)
116 {
117 multiplier.array() *= -jkr.array() / (Float)jj;
118 val += multiplier;
119 if ((multiplier.array().abs() <= tol * val.array().abs()).all())
120 break;
121 }
122 val.array() /= four_pi;
123
124 return val;
125}
126
127
131 const Complex k
132 ) const
133{
134 EigMatNX<Float, 3> r_diff = -(r_src.colwise() - r_obs);
135 EigRowVec<Float> r = r_diff.colwise().norm();
136
137 if (std::abs(k) == 0.0)
138 return EigMatNX<Complex, 3>::Zero(3, r.cols());
139
140 const Float tol = KERNEL_DEFAULT_TOL;
141 const Complex jk = J * k;
142 const Complex jkc = jk * k * k;
143 const EigRowVec<Complex> jkr = jk * r;
144
145 EigRowVec<Complex> multiplier = jkc * (one + jkr.array()) / (Float)6.0;
146 EigRowVec<Complex> scalar_term = -jkc / two + multiplier.array();
147 for (uint32_t jj = 4; true; jj++)
148 {
149 multiplier.array() *= -jkr.array() / (Float)jj;
151 if ((multiplier.array().abs() <= tol * scalar_term.array().abs()).all())
152 break;
153 }
154 scalar_term *= -1.0 / four_pi;
155
156 EigMatNX<Complex, 3> result = r_diff.array().rowwise() * scalar_term.array();
157 return result;
158}
159
160}
EigMatNX< Complex, 3 > compute_grad(ConstEigRef< EigColVecN< Float, 3 > > r_obs, ConstEigRef< EigMatNX< Float, 3 > > r_src, const Complex k) const
Computes the gradient of the kernel for given observation and source points.
Definition hgf.cpp:43
EigRowVec< Complex > compute(ConstEigRef< EigColVecN< Float, 3 > > r_obs, ConstEigRef< EigMatNX< Float, 3 > > r_src, const Complex k) const
Computes the kernel for given observation and source points.
Definition hgf.cpp:31
EigMatNX< Complex, 3 > compute_grad(ConstEigRef< EigColVecN< Float, 3 > > r_obs, ConstEigRef< EigMatNX< Float, 3 > > r_src, const Complex k) const
Computes the gradient of the kernel for given observation and source points.
Definition hgf.cpp:74
EigRowVec< Complex > compute(ConstEigRef< EigColVecN< Float, 3 > > r_obs, ConstEigRef< EigMatNX< Float, 3 > > r_src, const Complex k) const
Computes the kernel for given observation and source points.
Definition hgf.cpp:62
EigMatNX< Complex, 3 > compute_grad(ConstEigRef< EigColVecN< Float, 3 > > r_obs, ConstEigRef< EigMatNX< Float, 3 > > r_src, const Complex k) const
Computes the gradient of the kernel for given observation and source points.
Definition hgf.cpp:128
EigRowVec< Complex > compute(ConstEigRef< EigColVecN< Float, 3 > > r_obs, ConstEigRef< EigMatNX< Float, 3 > > r_src, const Complex k) const
Computes the kernel for given observation and source points.
Definition hgf.cpp:97
const Complex J
Imaginary unit.
Definition constants.hpp:40
const Eigen::Ref< const EigObj > ConstEigRef
Read-only reference to an Eigen object.
Definition types.hpp:98
Eigen::Matrix< T, 1, Eigen::Dynamic > EigRowVec
Dynamic-size row vector containing type T.
Definition types.hpp:90
double Float
Floating point number.
Definition types.hpp:47
std::complex< Float > Complex
Complex floating point number.
Definition types.hpp:51
Eigen::Matrix< T, N, 1 > EigColVecN
Fixed-size column vector of size N containing type T.
Definition types.hpp:86
Eigen::Matrix< T, N, Eigen::Dynamic > EigMatNX
Fixed-height matrix with N rows containing type T.
Definition types.hpp:78
Primary namespace for the OpenBEM library.
Definition constants.hpp:31