Mimetic Operators Library Enhanced 4.0
Loading...
Searching...
No Matches
mixedbc.cpp
Go to the documentation of this file.
1/*
2* SPDX-License-Identifier: GPL-3.0-or-later
3* © 2008-2024 San Diego State University Research Foundation (SDSURF).
4* See LICENSE file or https://www.gnu.org/licenses/gpl-3.0.html for details.
5*/
6
7/*
8 * @file mixedbc.cpp
9 *
10 * @brief Mixed Boundary Condition Class functions
11 *
12 * @date 2024/10/15
13 */
14
15#include "mixedbc.h"
16
17// 1-D Constructor
18MixedBC::MixedBC(u16 k, u32 m, Real dx, const std::string &left,
19 const std::vector<Real> &coeffs_left, const std::string &right,
20 const std::vector<Real> &coeffs_right) {
21 sp_mat A(m + 2, m + 2);
22 sp_mat BG(m + 2, m + 2);
23
24 Gradient *grad = nullptr;
25
26 // Handle the left boundary condition
27 if (left == "Dirichlet") {
28 A.at(0, 0) = coeffs_left[0];
29 } else if (left == "Neumann") {
30 grad = new Gradient(k, m, dx);
31 BG.row(0) = -coeffs_left[0] * grad->row(0);
32 } else if (left == "Robin") {
33 A.at(0, 0) = coeffs_left[0];
34 grad = new Gradient(k, m, dx);
35 BG.row(0) = -coeffs_left[1] * grad->row(0);
36 } else {
37 throw std::invalid_argument("Unknown boundary condition type");
38 }
39
40 // Handle the right boundary condition
41 if (right == "Dirichlet") {
42 A.at(m + 1, m + 1) = coeffs_right[0];
43 } else if (right == "Neumann") {
44 if (!grad)
45 grad = new Gradient(k, m, dx);
46 BG.row(m + 1) = coeffs_right[0] * grad->row(m);
47 } else if (right == "Robin") {
48 A.at(m + 1, m + 1) = coeffs_right[0];
49 if (!grad)
50 grad = new Gradient(k, m, dx);
51 BG.row(m + 1) = coeffs_right[1] * grad->row(m);
52 } else {
53 throw std::invalid_argument("Unknown boundary condition type");
54 }
55
56 *this = A + BG;
57
58 delete grad;
59}
60
61// 2-D Constructor
62MixedBC::MixedBC(u16 k, u32 m, Real dx, u32 n, Real dy, const std::string &left,
63 const std::vector<Real> &coeffs_left, const std::string &right,
64 const std::vector<Real> &coeffs_right,
65 const std::string &bottom,
66 const std::vector<Real> &coeffs_bottom, const std::string &top,
67 const std::vector<Real> &coeffs_top) {
68 MixedBC Bm(k, m, dx, left, coeffs_left, right, coeffs_right);
69 MixedBC Bn(k, n, dy, bottom, coeffs_bottom, top, coeffs_top);
70
71 sp_mat Im = speye(m + 2, m + 2);
72 sp_mat In = speye(n + 2, n + 2);
73
74 In.at(0, 0) = 0;
75 In.at(n + 1, n + 1) = 0;
76
77 sp_mat BC1 = Utils::spkron(In, Bm);
78 sp_mat BC2 = Utils::spkron(Bn, Im);
79
80 *this = BC1 + BC2;
81}
82
83// 3-D Constructor
84MixedBC::MixedBC(u16 k, u32 m, Real dx, u32 n, Real dy, u32 o, Real dz,
85 const std::string &left, const std::vector<Real> &coeffs_left,
86 const std::string &right,
87 const std::vector<Real> &coeffs_right,
88 const std::string &bottom,
89 const std::vector<Real> &coeffs_bottom, const std::string &top,
90 const std::vector<Real> &coeffs_top, const std::string &front,
91 const std::vector<Real> &coeffs_front, const std::string &back,
92 const std::vector<Real> &coeffs_back) {
93 MixedBC Bm(k, m, dx, left, coeffs_left, right, coeffs_right);
94 MixedBC Bn(k, n, dy, bottom, coeffs_bottom, top, coeffs_top);
95 MixedBC Bo(k, o, dz, front, coeffs_front, back, coeffs_back);
96
97 sp_mat Im = speye(m + 2, m + 2);
98 sp_mat In = speye(n + 2, n + 2);
99 sp_mat Io = speye(o + 2, o + 2);
100
101 Io.at(0, 0) = 0;
102 Io.at(o + 1, o + 1) = 0;
103
104 sp_mat In2 = In;
105 In2.at(0, 0) = 0;
106 In2.at(n + 1, n + 1) = 0;
107
108 sp_mat BC1 = Utils::spkron(Utils::spkron(Io, In2), Bm);
109 sp_mat BC2 = Utils::spkron(Utils::spkron(Io, Bn), Im);
110 sp_mat BC3 = Utils::spkron(Utils::spkron(Bo, In), Im);
111
112 *this = BC1 + BC2 + BC3;
113}
Mimetic Gradient operator.
Definition gradient.h:27
MixedBC(u16 k, u32 m, Real dx, const std::string &left, const std::vector< Real > &coeffs_left, const std::string &right, const std::vector< Real > &coeffs_right)
1-D Constructor
Definition mixedbc.cpp:18
static sp_mat spkron(const sp_mat &A, const sp_mat &B)
A wrappper for implementing a sparse Kroenecker product.
Definition utils.cpp:70
double Real
Definition utils.h:21