OpenBEM
Open-source framework for electromagnetic simulation with the boundary element method.
Loading...
Searching...
No Matches
iterative_trapz.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
19
20#include <functional>
21#include <stdexcept>
22
23#include "types.hpp"
24#include "constants.hpp"
27
28
29namespace bem
30{
31
32template <uint8_t dim>
37 )
38{
39 if (!eval)
40 throw std::invalid_argument(
41 "IterativeTrapzLineQuadrature::compute_points_weights(): invalid or missing eval."
42 );
43
44 uint16_t starting_iter = ceil(std::log2((Float)starting_num_segments_));
45 uint16_t num_segments = std::pow(2, starting_iter);
46
48 trapz_quad.compute_points_weights(p1, p2);
49
50 EigRowVec<Complex> vals = eval(trapz_quad.points());
51 Complex val_ref = trapz_quad.weights().dot(vals);
52
56
57 if (max_iters_ > LINE_MAX_ORDER)
58 throw std::domain_error(
59 "IterativeTrapzLineQuadrature::compute_points_weights(): max iterations exceeded.");
60
62 Float line_size = p.norm();
63
64 if (line_size == 0.0)
65 {
66 base::points_ = p1;
67 base::weights_ = EigRowVec<Float>::Zero(1, 1);
68 base::points_weights_computed_ = true;
69 converged_ = true;
70 return;
71 }
72
73 uint16_t max_iters_mod = std::min(max_iters_, (uint16_t)15);
74 converged_ = false;
75
77 {
78 Float segment_size = line_size / (points_temp.cols() - 1);
80
83 uint16_t num_segments_new = std::pow(2, iter) - 1;
84
85 trapz_quad.set_num_segments(num_segments_new);
86 trapz_quad.compute_points_weights(p1_new, p2_new);
87
88 vals = eval(trapz_quad.points());
89 Complex val = trapz_quad.weights().dot(vals);
90
91 points_temp.conservativeResize(dim, points_temp.cols() + trapz_quad.points().cols());
92 points_temp.rightCols(trapz_quad.points().cols()) = trapz_quad.points();
93
94 weights_temp.resize(points_temp.cols());
95 weights_temp.setConstant(segment_size);
96 weights_temp[0] = segment_size * 0.5;
97 weights_temp[starting_num_segments_] = segment_size * 0.5;
98
99 vals_temp = eval(points_temp);
101
102 converged_ = compare_with_tol(val, val_ref, tol_, 1);
103 if (converged_)
104 {
105 converged_num_segments_ = points_temp.cols() - 1;
106 break;
107 }
108 val_ref = val;
109 }
110
111 Float segment_size = line_size / (points_temp.cols() - 1);
112 weights_temp.resize(points_temp.cols());
113 weights_temp.setConstant(segment_size);
114 weights_temp[0] = segment_size * 0.5;
115 weights_temp[starting_num_segments_] = segment_size * 0.5;
116
117 base::points_ = points_temp;
118 base::weights_ = weights_temp;
119 base::points_weights_computed_ = true;
120
121 return;
122};
123
124
128
129}
void compute_points_weights(ConstEigRef< EigColVecN< Float, dim > > p1, ConstEigRef< EigColVecN< Float, dim > > p2, 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.
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
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
Primary namespace for the OpenBEM library.
Definition constants.hpp:31