Mimetic Operators Library Enhanced 4.0
Loading...
Searching...
No Matches
divergence.cpp
Go to the documentation of this file.
1#include "divergence.h"
2
3// 1-D Constructor
4Divergence::Divergence(u16 k, u32 m, Real dx) : sp_mat(m+2, m+1)
5{
6 assert(!(k%2));
7 assert(k > 1 && k < 7);
8 assert(m > 2*k);
9
10 switch (k) {
11 case 2:
12 for (u32 i = 1; i < m+1; i++) {
13 at(i, i-1) = -1.0;
14 at(i, i) = 1.0;
15 }
16 // Weights
17 Q << 1.0 << 1.0 << 1.0 << 1.0 << 1.0;
18 break;
19 case 4:
20 // A
21 at(1, 0) = -11.0/12.0;
22 at(1, 1) = 17.0/24.0;
23 at(1, 2) = 3.0/8.0;
24 at(1, 3) = -5.0/24.0;
25 at(1, 4) = 1.0/24.0;
26 // A'
27 at(m, m) = 11.0/12.0;
28 at(m, m-1) = -17.0/24.0;
29 at(m, m-2) = -3.0/8.0;
30 at(m, m-3) = 5.0/24.0;
31 at(m, m-4) = -1.0/24.0;
32 // Middle
33 for (u32 i = 2; i < m; i++) {
34 at(i, i-2) = 1.0/24.0;
35 at(i, i-1) = -9.0/8.0;
36 at(i, i) = 9.0/8.0;
37 at(i, i+1) = -1.0/24.0;
38 }
39 // Weights
40 //
41 Q << 2186.0/1943.0 << 2125.0/2828.0 << 1441.0/1240.0 << 648.0/673.0
42 << 349.0/350.0 << 648.0/673.0 << 1441.0/1240.0 << 2125.0/2828.0
43 << 2186.0/1943.0;
44 break;
45 case 6:
46 // A
47 at(1, 0) = -1627.0/1920.0;
48 at(1, 1) = 211.0/640.0;
49 at(1, 2) = 59.0/48.0;
50 at(1, 3) = -235.0/192.0;
51 at(1, 4) = 91.0/128.0;
52 at(1, 5) = -443.0/1920.0;
53 at(1, 6) = 31.0/960.0;
54 at(2, 0) = 31.0/960.0;
55 at(2, 1) = -687.0/640.0;
56 at(2, 2) = 129.0/128.0;
57 at(2, 3) = 19.0/192.0;
58 at(2, 4) = -3.0/32.0;
59 at(2, 5) = 21.0/640.0;
60 at(2, 6) = -3.0/640.0;
61 // A'
62 at(m, m) = 1627.0/1920.0;
63 at(m, m-1) = -211.0/640.0;
64 at(m, m-2) = -59.0/48.0;
65 at(m, m-3) = 235.0/192.0;
66 at(m, m-4) = -91.0/128.0;
67 at(m, m-5) = 443.0/1920.0;
68 at(m, m-6) = -31.0/960.0;
69 at(m-1, m) = -31.0/960.0;
70 at(m-1, m-1) = 687.0/640.0;
71 at(m-1, m-2) = -129.0/128.0;
72 at(m-1, m-3) = -19.0/192.0;
73 at(m-1, m-4) = 3.0/32.0;
74 at(m-1, m-5) = -21.0/640.0;
75 at(m-1, m-6) = 3.0/640.0;
76 // Middle
77 for (u32 i = 3; i < m-1; i++) {
78 at(i, i-3) = -3.0/640.0;
79 at(i, i-2) = 25.0/384.0;
80 at(i, i-1) = -75.0/64.0;
81 at(i, i) = 75.0/64.0;
82 at(i, i+1) = -25.0/384.0;
83 at(i, i+2) = 3.0/640.0;
84 }
85 // Weights
86 Q << 2383.0/2005.0 << 929.0/2002.0 << 887.0/531.0 << 3124.0/5901.0
87 << 1706.0/1457.0 << 457.0/467.0 << 1057.0/1061.0 << 457.0/467.0
88 << 1706.0/1457.0 << 3124.0/5901.0 << 887.0/531.0 << 929.0/2002.0
89 << 2383.0/2005.0;
90 break;
91 }
92
93 // Scaling
94 *this /= dx;
95}
96
97// 2-D Constructor
98Divergence::Divergence(u16 k, u32 m, u32 n, Real dx, Real dy)
99{
100 Divergence Dx(k, m, dx);
101 Divergence Dy(k, n, dy);
102
103 sp_mat Im = speye(m+2, m+2);
104 sp_mat In = speye(n+2, n+2);
105
106 Im.shed_col(0);
107 Im.shed_col(m);
108 In.shed_col(0);
109 In.shed_col(n);
110
111 sp_mat D1 = Utils::spkron(In, Dx);
112 sp_mat D2 = Utils::spkron(Dy, Im);
113
114 // Dimensions = (m+2)*(n+2), 2*m*n+m+n
115 if (m != n)
116 *this = Utils::spjoin_rows(D1, D2);
117 else {
118 sp_mat A1(1, 2);
119 sp_mat A2(1, 2);
120 A1(0, 0) = A2(0, 1) = 1.0;
121 *this = Utils::spkron(A1, D1) + Utils::spkron(A2, D2);
122 }
123}
124
125// 3-D Constructor
126Divergence::Divergence(u16 k, u32 m, u32 n, u32 o, Real dx, Real dy, Real dz)
127{
128 Divergence Dx(k, m, dx);
129 Divergence Dy(k, n, dy);
130 Divergence Dz(k, o, dz);
131
132 sp_mat Im = speye(m+2, m+2);
133 sp_mat In = speye(n+2, n+2);
134 sp_mat Io = speye(o+2, o+2);
135
136 Im.shed_col(0);
137 Im.shed_col(m);
138 In.shed_col(0);
139 In.shed_col(n);
140 Io.shed_col(0);
141 Io.shed_col(o);
142
143 sp_mat D1 = Utils::spkron(Utils::spkron(Io, In), Dx);
144 sp_mat D2 = Utils::spkron(Utils::spkron(Io, Dy), Im);
145 sp_mat D3 = Utils::spkron(Utils::spkron(Dz, In), Im);
146
147 // Dimensions = (m+2)*(n+2)*(o+2), 3*m*n*o+m*n+m*o+n*o
148 if ((m != n) || (n != o))
149 *this = Utils::spjoin_rows(Utils::spjoin_rows(D1, D2), D3);
150 else {
151 sp_mat A1(1, 3);
152 sp_mat A2(1, 3);
153 sp_mat A3(1, 3);
154 A1(0, 0) = A2(0, 1) = A3(0, 2) = 1.0;
155 *this = Utils::spkron(A1, D1) + Utils::spkron(A2, D2) + Utils::spkron(A3, D3);
156 }
157}
158
159// Returns weights
161{
162 return Q;
163}
Divergence(u16 k, u32 m, Real dx)
Definition divergence.cpp:4
static sp_mat spjoin_rows(const sp_mat &A, const sp_mat &B)
Definition utils.cpp:85
static sp_mat spkron(const sp_mat &A, const sp_mat &B)
Definition utils.cpp:53
double Real
Definition utils.h:8