OpenBEM
Open-source framework for electromagnetic simulation with the boundary element method.
Loading...
Searching...
No Matches
utility.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 <vector>
21#include <cmath>
22#include <fstream>
23#include <string>
24
25#include "external/json.hpp"
26
27#include "types.hpp"
28#include "constants.hpp"
29
30using json = nlohmann::json;
31
32
33namespace bem
34{
35
37 const Complex val,
38 const Complex val_ref,
39 const Float tol,
40 const uint8_t mode
41 )
42{
43
44 if (val == val_ref)
45 return true;
46
47 if (mode == 1)
48 {
49 Float test_val_real = std::abs(val.real() - val_ref.real());
50 Float ref_val_real = std::abs(val.real()) + float_eps;
51
52 Float test_val_imag = std::abs(val.imag() - val_ref.imag());
53 Float ref_val_imag = std::abs(val.imag()) + float_eps;
54
55 if (test_val_real <= tol * ref_val_real &&
57 return true;
58 }
59 else if (mode == 2)
60 {
61 Float test_val_abs = std::abs(std::abs(val.imag() - val_ref.imag()));
62 Float ref_val_abs = std::abs(val) + float_eps;
63
64 Float test_val_arg = std::abs(std::arg(val) - std::arg(val_ref));
65 Float ref_val_arg = std::abs(std::arg(val)) + float_eps;
66
67 if (test_val_abs <= tol * ref_val_abs &&
69 return true;
70 }
71 else if (mode == 3)
72 {
73 if (std::abs(val - val_ref) <= tol * (std::abs(val_ref) + float_eps))
74 return true;
75 }
76
77 return false;
78
79};
80
81
82template <uint8_t dim>
83std::vector<QuadratureRule<dim>> load_rules(
84 const std::string file,
86 )
87{
88
89 std::size_t pos = std::string(__FILE__).find_last_of("/");
90 std::string file_with_path = std::string(__FILE__).substr(0, pos) + "/" + file;
91
92 std::ifstream in_stream (file_with_path);
93 json data = json::parse(in_stream);
94
95 std::vector<QuadratureRule<dim>> rules;
96 rules.reserve(orders.size());
97
98 for (Index order: orders)
99 {
100 json order_data = data[std::to_string(order)];
101
103 rule.num_nodes = order_data["num_nodes"].template get<Index> ();
104
105 std::vector<std::vector<Float>> nodes = order_data["nodes"];
106
107 rule.nodes.resize(dim, rule.num_nodes);
108 for (Index ii = 0; ii < nodes.size(); ++ii)
109 for (uint8_t jj = 0; jj < dim; ++jj)
110 rule.nodes(jj, ii) = nodes[ii][jj];
111
112 rule.weights = Eigen::Map<EigMat<Float>> (
113 ((std::vector<Float>) order_data["weights"]).data(), 1, rule.num_nodes
114 ).eval();
115
116 rules.push_back(rule);
117 }
118
119 return rules;
120
121};
122
123
124template std::vector<QuadratureRule<1>> load_rules(
125 const std::string file,
127 );
128
129template std::vector<QuadratureRule<2>> load_rules(
130 const std::string file,
132 );
133
134template std::vector<QuadratureRule<3>> load_rules(
135 const std::string file,
137 );
138
139}
140
const Float float_eps
Numerical infinitesimal.
Definition constants.hpp:70
std::vector< QuadratureRule< dim > > load_rules(const std::string file, ConstEigRef< EigColVec< Index > > orders)
Loads quadrature rules from the specified json file assumed to be located in the same directory as th...
Definition utility.cpp:83
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
std::size_t Index
Unsigned integer type for indices and container sizes.
Definition types.hpp:54
Primary namespace for the OpenBEM library.
Definition constants.hpp:31