OpenBEM
Open-source framework for electromagnetic simulation with the boundary element method.
Loading...
Searching...
No Matches
adaptive.hpp
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#ifndef ADAPTIVE_TRI_QUAD_H
19#define ADAPTIVE_TRI_QUAD_H
20
21#include <cassert>
22
23#include "types.hpp"
24#include "constants.hpp"
25
29
30
31namespace bem
32{
33
34const uint16_t ADAPTIVE_TRI_DEFAULT_MAX_LEVELS = 200;
35const Float ADAPTIVE_TRI_DEFAULT_TOL = 1e-3;
36
48template <uint8_t dim>
50{
51
52 using base = TriangleQuadratureBase<dim>;
53
54public:
55
61 {
62 max_levels_ = max_levels;
63 base::points_weights_computed_ = false;
64 converged_ = false;
65 return;
66 };
67
68
73 uint16_t max_levels() const { return max_levels_; };
74
75
80 void set_tol(const Float tol)
81 {
82 tol_ = tol;
83 base::points_weights_computed_ = false;
84 converged_ = false;
85 return;
86 };
87
88
93 Float tol() const { return tol_; };
94
95
102 const Triangle<dim>& tri,
103 std::function<EigRowVec<Complex> (ConstEigRef<EigMatNX<Float, dim>>)> eval = {}
104 ) override;
105
106
111 bool converged() const
112 {
113 if (!base::points_weights_computed_)
114 throw std::runtime_error(
115 "AdaptiveTriangleQuadrature::converged(): must call `compute_points_weights()` first.");
116 return converged_;
117 };
118
119
124 uint16_t converged_iter() const { return converged_iter_; };
125
126
127private:
128
137 Complex run_recursion(
138 int& level,
139 const Triangle<dim>& tri,
142 );
143
144
151 static void get_5_points(
153 const Triangle<dim>& tri,
155 );
156
157
165 void get_subtris(
168 const Triangle<dim>& tri,
170 );
171
172
179 bool check_convergence(const Complex val_coarse, const Complex val_fine)
180 {
182
183 assert((converged || val_fine != zero) &&
184 "AdaptiveTriangleQuadrature::check_convergence(): divide by zero.");
185 // converged = converged || check_convergence(val_fine);
186 // check_convergence(val_fine);
187 // std::cout << val_fine << ", " << val_coarse << ", " << std::endl;
188 return converged;
189 }
190
191
197 bool check_convergence(const Complex val)
198 {
199 // Complex I_new = I_prev_ + val;
200 // bool converged = compare_with_tol(I_new, I_prev_, tol, 3);
201 // I_prev_ += val;
202
203 Complex I_new = base::weights_.dot(vals_) + val;
204 bool converged = compare_with_tol(I_new, I_prev_, tol_, 3);
205
206 if (converged)
207 I_prev_ = I_new;
208
209 return converged;
210 }
211
212
217 uint16_t total_recursions()
218 { return total_recursions_; };
219
220
224 void reset_recursion_count()
225 {
226 total_recursions_ = 0;
227 return;
228 };
229
230
231 uint16_t max_levels_ = ADAPTIVE_TRI_DEFAULT_MAX_LEVELS;
232 Float tol_ = ADAPTIVE_TRI_DEFAULT_TOL;
233 bool converged_ = false;
234 uint16_t converged_iter_ = 0;
235
236 const EigColVecN<Float, 10> weights10_ = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 2.0};
237 const EigColVecN<Float, 5> weights5_ = {1.0, 1.0, 1.0, 1.0, 2.0};
238
241 EigRowVecN<Complex, 5> vals_main_, vals_subtri1_, vals_subtri2_;
242 EigRowVecN<Complex, 6> vals_sub_;
243 EigRowVec<Complex> vals_;
244
245 EigRowVecN<uint32_t, 2> idx_extra1_, idx_extra2_;
246 Complex I_prev_ = 0.0;
247 uint16_t total_recursions_ = 0;
248
249};
250
255}
256
257#ifndef BEM_LINKED
259#endif
260
261#endif
Class for adaptive quadrature over a triangle. Reference: O. Ergul, L. Gurel, "The Multilevel Fast Mu...
Definition adaptive.hpp:50
void compute_points_weights(const Triangle< dim > &tri, std::function< EigRowVec< Complex >(ConstEigRef< EigMatNX< Float, dim > >)> eval={}) override
Computes and stores the points on which to evaluate the integrand, and the corresponding weights.
Definition adaptive.cpp:33
bool converged() const
Checks whether the recursion converged.
Definition adaptive.hpp:111
uint16_t max_levels() const
Returns the maximum number of recursion levels allowed even if not converged.
Definition adaptive.hpp:73
void set_tol(const Float tol)
Sets the relative convergence tolerance defining when recursion should stop.
Definition adaptive.hpp:80
uint16_t converged_iter() const
Returns the recursion level at which the recursion converged.
Definition adaptive.hpp:124
Float tol() const
Returns the relative convergence tolerance defining when recursion should stop.
Definition adaptive.hpp:93
void set_max_levels(const uint16_t max_levels)
Sets the maximum number of recursion levels allowed even if not converged.
Definition adaptive.hpp:60
Base class for quadrature over a triangle.
Definition base.hpp:46
bool compare_with_tol(const Complex val, const Complex val_ref, const Float tol, const uint8_t mode)
Compares two complex numbers within a given tolerance based on a given rule.
Definition utility.cpp:36
const Eigen::Ref< const EigObj > ConstEigRef
Read-only reference to an Eigen object.
Definition types.hpp:98
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
Primary namespace for the OpenBEM library.
Definition constants.hpp:31