OpenBEM
Open-source framework for electromagnetic simulation with the boundary element method.
Loading...
Searching...
No Matches
base.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 MATRIX_BASE_HPP
19#define MATRIX_BASE_HPP
20
21#include <stdexcept>
22#include <cmath>
23#include <vector>
24#include <iostream>
25#include <fstream>
26#include <iomanip>
27#include <string>
28
29#include "types.hpp"
30
31
32namespace bem
33{
34
44template <typename T>
46{
47public:
48
53 virtual Index num_rows() const = 0;
54
55
60 virtual Index num_cols() const = 0;
61
62
68 virtual void resize(Index rows, Index cols) = 0;
69
70
74 virtual void clear() = 0;
75
76
83 virtual T value(Index row, Index col) const = 0;
84
85
92 virtual void set_value(Index row, Index col, const T& a) = 0;
93
94
101 virtual void add_ax(const MatrixBase<T>& x, const T& a = T(1)) = 0;
102
103
108 virtual void scale(const T& a) = 0;
109
110
114 virtual void set_zero() = 0;
115
116
121 virtual const T* data() const = 0;
122
123
128 virtual T* data() = 0;
129
130
138 virtual void mat_solve(MatrixBase<T>& x, const MatrixBase<T>& b) = 0;
139
140
145 virtual Index size() const { return num_rows() * num_cols(); };
146
147
154 virtual void add_value(Index row, Index col, const T& a)
155 {
156 set_value(row, col, value(row, col) + a);
157 return;
158 };
159
160
165 virtual void preallocate(const std::vector<Index>& nnz) {};
166
167
172 virtual void preallocate(Index num_entries) {};
173
174
178 virtual void assemble() {};
179
180
184 virtual void set_identity()
185 {
186 set_zero();
187 for (Index ii = 0; ii < num_rows(); ++ii)
188 set_value(ii, ii, T(1));
189 return;
190 };
191
192
196 virtual void write_binary(const std::string name = "matrix") const
197 {
198 std::ofstream out (name, std::ios::out | std::ios::binary | std::ios::trunc);
200 out.write((char*) (&rows), sizeof(Index));
201 out.write((char*) (&cols), sizeof(Index));
202 out.write((char*) data(), rows * cols * sizeof(T));
203 out.close();
204 return;
205 };
206
207
211 virtual void read_binary(const std::string name = "matrix")
212 {
213 std::ifstream in (name, std::ios::in | std::ios::binary);
214 Index rows = 0, cols = 0;
215 in.read((char*) (&rows), sizeof(Index));
216 in.read((char*) (&cols), sizeof(Index));
217 resize(rows, cols);
218 in.read((char *) data() , rows * cols * sizeof(T));
219 in.close();
220 return;
221 };
222
223
227 virtual void print(const std::string name = "matrix") const
228 {
229 std::cout << "\n--- " << name << " (" << num_rows() << " x " << num_cols() << ") ---" << std::endl;
230 for (Index ii = 0; ii < num_rows(); ++ii)
231 {
232 for (Index jj = 0; jj < num_cols(); ++jj)
233 {
234 std::cout << std::scientific
235 << std::setprecision(4)
236 << std::setw(14)
237 << value(ii, jj)
238 << " "
239 << std::flush;
240 }
241 std::cout << std::endl;
242 }
243 std::cout << "---" << std::endl;
244 return;
245 };
246
247};
248
253}
254
255#endif
Base class for for matrix algebra containers.
Definition base.hpp:46
virtual void mat_solve(MatrixBase< T > &x, const MatrixBase< T > &b)=0
Solves for matrix with a direct solver, where is this matrix, and is a given right-hand side matr...
virtual T value(Index row, Index col) const =0
Returns the matrix value at the specified row and column.
virtual T * data()=0
Returns a writable pointer to the underlying raw data - use with care.
virtual void add_value(Index row, Index col, const T &a)
Adds to the matrix value at the specified row and column.
Definition base.hpp:154
virtual void assemble()
Assembles cached data into the matrix.
Definition base.hpp:178
virtual void write_binary(const std::string name="matrix") const
Writes the matrix to a binary file.
Definition base.hpp:196
virtual void set_zero()=0
Sets all matrix entries to zero.
virtual void set_identity()
Sets the matrix to identity (ones along the diagonal, zeros elsewhere).
Definition base.hpp:184
virtual Index num_cols() const =0
Returns the total number of rows in the matrix.
virtual void clear()=0
Clears all data in the matrix and sets its size to 0.
virtual void read_binary(const std::string name="matrix")
Reads the matrix from a binary file.
Definition base.hpp:211
virtual const T * data() const =0
Returns a read-only pointer to the underlying raw data.
virtual Index num_rows() const =0
Returns the total number of rows in the matrix.
virtual void preallocate(Index num_entries)
Preallocates memory for a given total number of non-zero values.
Definition base.hpp:172
virtual void scale(const T &a)=0
Scales all matrix entries by a given value.
virtual void preallocate(const std::vector< Index > &nnz)
Preallocates memory for a given number of non-zero values per row.
Definition base.hpp:165
virtual void resize(Index rows, Index cols)=0
Resizes the matrix to a new number of rows and columns.
virtual void set_value(Index row, Index col, const T &a)=0
Sets the matrix value at the specified row and column.
virtual void print(const std::string name="matrix") const
Prints the matrix to the terminal in a formatted manner.
Definition base.hpp:227
virtual Index size() const
Returns the size (rows * cols) of the matrix.
Definition base.hpp:145
virtual void add_ax(const MatrixBase< T > &x, const T &a=T(1))=0
Computes where is this matrix, is a scalar, and is a matrix.
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