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 bool equal = val == val_ref;
45 if (val == val_ref)
46 return true;
47
48 if (mode == 1)
49 {
50 Float test_val_real = std::abs(val.real() - val_ref.real());
51 Float ref_val_real = std::abs(val.real()) + float_eps;
52
53 Float test_val_imag = std::abs(val.imag() - val_ref.imag());
54 Float ref_val_imag = std::abs(val.imag()) + float_eps;
55
56 if (test_val_real <= tol * ref_val_real &&
58 return true;
59 }
60 else if (mode == 2)
61 {
62 Float test_val_abs = std::abs(std::abs(val.imag() - val_ref.imag()));
63 Float ref_val_abs = std::abs(val) + float_eps;
64
65 Float test_val_arg = std::abs(std::arg(val) - std::arg(val_ref));
66 Float ref_val_arg = std::abs(std::arg(val)) + float_eps;
67
68 if (test_val_abs <= tol * ref_val_abs &&
70 return true;
71 }
72 else if (mode == 3)
73 {
74 if (std::abs(val - val_ref) <= tol * (std::abs(val_ref) + float_eps))
75 return true;
76 }
77
78 return false;
79
80};
81
82
83template <uint8_t dim>
84std::vector<QuadratureRule<dim>> load_rules(
85 const std::string file,
87 )
88{
89
90 std::size_t pos = std::string(__FILE__).find_last_of("/");
91 std::string file_with_path = std::string(__FILE__).substr(0, pos) + "/" + file;
92
93 std::ifstream in_stream (file_with_path);
94 json data = json::parse(in_stream);
95
96 std::vector<QuadratureRule<dim>> rules;
97 rules.reserve(orders.size());
98
99 for (Index order: orders)
100 {
101 json order_data = data[std::to_string(order)];
102
104 rule.num_nodes = order_data["num_nodes"].template get<Index> ();
105
106 std::vector<std::vector<Float>> nodes = order_data["nodes"];
107
108 rule.nodes.resize(dim, rule.num_nodes);
109 for (Index ii = 0; ii < nodes.size(); ++ii)
110 for (uint8_t jj = 0; jj < dim; ++jj)
111 rule.nodes(jj, ii) = nodes[ii][jj];
112
113 rule.weights = Eigen::Map<EigMat<Float>> (
114 ((std::vector<Float>) order_data["weights"]).data(), 1, rule.num_nodes
115 ).eval();
116
117 rules.push_back(rule);
118 }
119
120 return rules;
121
122};
123
124
125template std::vector<QuadratureRule<1>> load_rules(
126 const std::string file,
128 );
129
130template std::vector<QuadratureRule<2>> load_rules(
131 const std::string file,
133 );
134
135template std::vector<QuadratureRule<3>> load_rules(
136 const std::string file,
138 );
139
140}
141
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:84
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