-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStep11.cpp
139 lines (115 loc) · 4.64 KB
/
Step11.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
#include <iostream>
#include <cmath>
#include <vector>
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main(int argc, char* argv[])
{
int nx = 41; // Number of dots in x
int ny = 41; // Number of dots in y
int nt = 100; // Number of time steps
double dx = 2.0/(nx - 1); // Local spacing in x
double dy = 2.0/(ny - 1); // Local spacing in y
int c = 1; // Velocity
double rho = 1.0;
double nu = .1;
double dt = .001;
double tolerance = .01;
// Initilization
std::vector<double> x(nx);
std::vector<double> y(ny);
for (int i = 0; i<nx; i++){
x[i] = i * dx;
}
for (int i = 0; i<ny; i++){
y[i] = i * dy;
}
std::vector<std::vector<double>> u(nx, std::vector<double> (ny,0.0));
std::vector<std::vector<double>> v(nx, std::vector<double> (ny,0.0));
// std::vector<std::vector<double>> un(nx, std::vector<double> (ny,0.0));
// std::vector<std::vector<double>> vn(nx, std::vector<double> (ny,0.0));
std::vector<std::vector<double>> p(nx, std::vector<double> (ny,0.0));
std::vector<std::vector<double>> b(nx, std::vector<double> (ny,0.0));
// Lid-driven cavity condition
for (int i = 0; i < ny; i++) {
u[nx - 1][i] = 1.0;
}
for (int i=0; i<nt; i++)
{
std::vector<std::vector<double>> un = u;
std::vector<std::vector<double>> vn = v;
// Build up b (This is the part of pressure equation in the end in brackets so that the big pressure equation is split up for better management)
for (int i=1; i<nx-1; i++){
for (int j=1; j<ny-1; j++)
{
b[i][j] = rho * (1.0/dt*(((u[i+1][j] - u[i-1][j])/(2.0*dx)) + ((v[i][j+1] - v[i][j-1])/(2.0*dy)))
- ((u[i+1][j] - u[i-1][j])/(2.0*dx))*((u[i+1][j] - u[i-1][j])/(2.0*dx))
- (2.0*((u[i][j+1] - u[i][j-1])/(2.0*dy))*((v[i+1][j] - v[i-1][j])/(2.0*dx)))
- ((v[i][j+1] - v[i][j-1])/(2.0*dy))*((v[i][j+1] - v[i][j-1])/(2.0*dy)));
}
}
std::vector<std::vector<double>> pn(nx, std::vector<double> (ny,0.0));
// Loop through time steps nt
for (int it=0; it<50; it++){
pn = p;
for (int i=1; i<nx-1; i++){
for (int j=1; j<ny-1; j++)
{
p[i][j] = (((dy*dy*(pn[i+1][j] + pn[i-1][j])) + dx*dx*(pn[i][j+1] + pn[i][j-1])) / (2.0*(dx*dx + dy*dy)))
- ((dx*dx*dy*dy)/(2.0*(dx*dx + dy*dy))) * (b[i][j]) ;
}
}
// Boundary conditions
// Pressure boundary conditions
for (int i = 0; i < nx; i++) {
p[i][0] = p[i][1];
p[i][ny - 1] = p[i][ny - 2];
}
for (int j = 0; j < ny; j++) {
p[0][j] = p[1][j];
p[nx - 1][j] = 0.0;
}
}
///////////////// Computing u and v /////////////////
for (int i=1; i<nx-1; i++){
for (int j=1; j<ny-1; j++){
u[i][j] = un[i][j] - un[i][j]*(dt/dx)*(un[i][j] - un[i-1][j]) - vn[i][j]*(dt/dy)*(un[i][j] - un[i][j-1])
- (dt/(rho*2.0*dx)) * (pn[i+1][j] - pn[i-1][j])
+ nu*((dt/(dx*dx))*(un[i+1][j] - 2.0* un[i][j] + un[i-1][j]) + (dt/(dy*dy))*(un[i][j+1] - 2.0*un[i][j] + un[i][j-1]));
v[i][j] = vn[i][j] - un[i][j]*(dt/dx)*(vn[i][j] - vn[i-1][j]) - vn[i][j]*(dt/dy)*(vn[i][j] - vn[i][j-1])
- (dt/(rho*2.0*dy)) * (pn[i][j+1] - pn[i][j-1])
+ nu*((dt/(dx*dx))*(vn[i+1][j] - 2.0* vn[i][j] + vn[i-1][j]) + (dt/(dy*dy))*(vn[i][j+1] - 2.0* vn[i][j] + vn[i][j-1]));
}
}
for (int i = 0; i < nx; i++) {
u[i][0] = 0.0;
v[i][0] = 0.0;
u[i][ny - 1] = 1.0;
v[i][ny - 1] = 0.0;
}
for (int j = 0; j < ny; j++) {
u[0][j] = 0.0;
v[0][j] = 0.0;
u[nx - 1][j] = 0.0;
v[nx - 1][j] = 0.0;
}
plt::figure(1);
std::vector<double> X1, Y1, P1, U1, V1;
for (int i = 0; i < ny; ++i) {
for (int j = 0; j < nx; ++j) {
X1.push_back(j * dx);
Y1.push_back(i * dy);
P1.push_back(p[i][j]);
U1.push_back(u[j][i]);
V1.push_back(v[j][i]);
}
}
const long animatedFig = plt::figure(1);
plt::ion();
plt::clf();
plt::show();
plt::quiver(X1, Y1, U1, V1);
plt::pause(0.1);
}
return 0;
}