PLaSK library
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2023 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#ifndef PLASK_COMMON_FEM_MATRIX_HPP
15#define PLASK_COMMON_FEM_MATRIX_HPP
16
17#include <plask/plask.hpp>
18
19namespace plask {
20
21template <typename T = double> struct FemMatrix {
22 const size_t rank;
23 const size_t size;
24 T* data;
25 const Solver* solver;
26
27 FemMatrix(const Solver* solver, size_t rank, size_t size)
29 clear();
30 }
31
32 FemMatrix(const FemMatrix&) = delete;
33
35
41 virtual T& operator()(size_t r, size_t c) = 0;
42
44 virtual void clear() { std::fill_n(data, size, 0.); }
45
50 virtual void factorize() {}
51
59 virtual void solverhs(DataVector<T>& B, DataVector<T>& X) = 0;
60
68 factorize();
69 solverhs(B, X);
70 }
71
78 void solve(DataVector<T>& B) { solve(B, B); }
79
85 virtual void mult(const DataVector<const T>& vector, DataVector<T>& result) = 0;
86
92 virtual void addmult(const DataVector<const T>& vector, DataVector<T>& result) = 0;
93
100 virtual void setBC(DataVector<T>& B, size_t r, T val) = 0;
101
107 template <typename BoundaryConditonsT> void applyBC(const BoundaryConditonsT& bconds, DataVector<T>& B) {
108 // boundary conditions of the first kind
109 for (auto cond : bconds) {
110 for (auto r : cond.place) {
111 setBC(B, r, cond.value);
112 }
113 }
114 }
115
116 virtual std::string describe() const { return format("rank={}, size={}", rank, size); }
117};
118
119template <typename T = double> struct BandMatrix : FemMatrix<T> {
120 const size_t ld;
121 const size_t kd;
122
123 BandMatrix(const Solver* solver, size_t rank, size_t kd, size_t ld)
124 : FemMatrix<T>(solver, rank, rank * (ld + 1)), ld(ld), kd(kd) {}
125
126 void setBC(DataVector<T>& B, size_t r, T val) override {
127 B[r] = val;
128 (*this)(r, r) = 1.;
129 size_t start = (r > kd) ? r - kd : 0;
130 size_t end = (r + kd < this->rank) ? r + kd + 1 : this->rank;
131 for (size_t c = start; c < r; ++c) {
132 B[c] -= (*this)(r, c) * val;
133 (*this)(r, c) = 0.;
134 }
135 for (size_t c = r + 1; c < end; ++c) {
136 B[c] -= (*this)(r, c) * val;
137 (*this)(r, c) = 0.;
138 }
139 }
140
141 std::string describe() const override { return format("rank={}, bands={}, size={}", this->rank, kd + 1, this->size); }
142};
143
144} // namespace plask
145
146#endif