This repository has been archived by the owner on Dec 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
187 lines (154 loc) · 6.15 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "common.h"
int nX, nY; // number of x-axis and y-axis elements
int N; // linear system dimension
int i, k, n, m, step = 0, flag = 0; // counters and flags
int *ig, *jg; // portrait
double *gridX, *gridY; // x-axis and y-axis grids
double *edges; // types of boundary conditions on four sides
double *ggl, *ggu; // lower and upper triangles of the global matrix
double *L, *U; // lower and upper triangles of the factorized global matrix
double *diOrig, *di; // diagonals of the global matrix before and after factorization
double *x, *f; // solution vector and right side vector
double *r, *p, *z, *q; // auxiliary vectors for LOS
double alpha, beta, breakValue; // LOS coefficients and exit value
double xPoint, yPoint, xUpperPoint, yUpperPoint, hx, hy; // finite element parameters
double lambda, gamma; // area parameters
double temp, hx_2, hy_2, el1, el2, el3, el4; // variables for elements of G and C
int main()
{
readSize();
config();
readGrid();
globalAsm();
boundaryCond();
factorization();
los();
printResult();
return 0;
}
void globalAsm() // building local matrices and assembling the global matrix
{
int node[4]; // node numbers
double G[4][4], C[4][4]; // stiffness and mass (with gamma = 1) matrices
double fl[4], b[4]; // right side values and local vector
double A[4][4]; // local matrix
for (k = 0; k < nY - 1; k++)
for (i = 0; i < nX - 1; i++)
{
// points defining the finite element
xPoint = gridX[i];
xUpperPoint = gridX[i + 1];
yPoint = gridY[k];
yUpperPoint = gridY[k + 1];
// horizontal and vertical steps
hx = xUpperPoint - xPoint;
hy = yUpperPoint - yPoint;
node[0] = nX * k + i;
node[1] = nX * k + i + 1;
node[2] = nX * (k + 1) + i;
node[3] = nX * (k + 1) + i + 1;
hx_2 = hx * hx;
hy_2 = hy * hy;
temp = 1.0 / (hx * hy);
el1 = (hx_2 + hy_2) * temp / 3;
el2 = (hx_2 - 2 * hy_2) * temp / 6;
el3 = - (2 * hx_2 - hy_2) * temp / 6;
el4 = - (hx_2 + hy_2) * temp / 6;
G[0][0] = el1, G[0][1] = el2, G[0][2] = el3, G[0][3] = el4;
G[1][0] = el2, G[1][1] = el1, G[1][2] = el4, G[1][3] = el3;
G[2][0] = el3, G[2][1] = el4, G[2][2] = el1, G[2][3] = el2;
G[3][0] = el4, G[3][1] = el3, G[3][2] = el2, G[3][3] = el1;
/*
lambda1 = getLambda(xPoint, yPoint);
lambda2 = getLambda(xPoint + hx / 2.0, yPoint);
lambda3 = getLambda(xPoint + hx, yPoint);
lambda4 = getLambda(xPoint, yPoint + hy / 2.0);
lambda5 = getLambda(xPoint + hx / 2.0, yPoint + hy / 2.0);
lambda6 = getLambda(xPoint + hx, yPoint + hy / 2.0);
lambda7 = getLambda(xPoint, yPoint + hy);
lambda8 = getLambda(xPoint + hx / 2.0, yPoint + hy);
lambda9 = getLambda(xPoint + hx, yPoint + hy);
gamma = getGamma(xPoint + hx / 2.0, yPoint + hy / 2.0);
G[0][0] = lambda1 * ... + ... + lambda9 * ...;
G[1][0] = lambda1 * ... + ... + lambda9 * ...;
...
*/
el1 = hx * hy / 9.0;
el2 = hx * hy / 18.0;
el3 = el2;
el4 = hx * hy / 36.0;
C[0][0] = el1, C[0][1] = el2, C[0][2] = el3, C[0][3] = el4;
C[1][0] = el2, C[1][1] = el1, C[1][2] = el4, C[1][3] = el3;
C[2][0] = el3, C[2][1] = el4, C[2][2] = el1, C[2][3] = el2;
C[3][0] = el4, C[3][1] = el3, C[3][2] = el2, C[3][3] = el1;
fl[0] = getF(xPoint, yPoint);
fl[1] = getF(xUpperPoint, yPoint);
fl[2] = getF(xPoint, yUpperPoint);
fl[3] = getF(xUpperPoint, yUpperPoint);
b[0] = C[0][0] * fl[0] + C[0][1] * fl[1] + C[0][2] * fl[2] + C[0][3] * fl[3];
b[1] = C[1][0] * fl[0] + C[1][1] * fl[1] + C[1][2] * fl[2] + C[1][3] * fl[3];
b[2] = C[2][0] * fl[0] + C[2][1] * fl[1] + C[2][2] * fl[2] + C[2][3] * fl[3];
b[3] = C[3][0] * fl[0] + C[3][1] * fl[1] + C[3][2] * fl[2] + C[3][3] * fl[3];
// adding elements to the global matrix
for (n = 0; n < 4; n++)
{
f[node[n]] += b[n]; // assembling the global vector
for (m = 0; m < 4; m++)
{
A[n][m] = getLambda(xPoint + hx / 2.0, yPoint + hy / 2.0) * G[n][m] + // building a local matrix
getGamma(xPoint + hx / 2.0, yPoint + hy / 2.0) * C[n][m];
addGlobal(A[n][m], node[n], node[m]); // adding to the global matrix
}
}
}
}
void addGlobal(double elem, int i, int j)
{
int k;
if (i > j)
{
for (k = ig[i]; k < ig[i + 1]; k++)
if (jg[k] == j)
ggl[k] += elem;
}
else if (i < j)
{
for (k = ig[j]; k < ig[j + 1]; k++)
if (jg[k] == i)
ggu[k] += elem;
}
else diOrig[i] += elem;
}
void los()
{
for (i = 0; i < N; i++)
{
x[i] = 0; // solution vector
r[i] = f[i]; // r = f - Ax
}
forwardGauss(r, r); // r = L'r
backwardGauss(r, z); // z = U'r
multVectByA(z, p); // p = Az
forwardGauss(p, p); // p = L'Az
for (k = 0; k < numIter && flag == 0; k++)
{
alpha = multVectByVect(p, r) / multVectByVect(p, p);
for (i = 0; i < N; i++)
{
x[i] += alpha * z[i];
r[i] -= alpha * p[i];
}
backwardGauss(r, q); // q = U'r
multVectByA(q, q); // q = AU'r
forwardGauss(q, q); // q = L'AU'r
beta = - (multVectByVect(p, q) / multVectByVect(p, p));
for (i = 0; i < N; i++)
{
z[i] = beta * z[i] + q[i];
p[i] = beta * p[i] + q[i];
}
breakValue = multVectByVect(r, r);
if (breakValue < eps)
flag = 1;
}
}