-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStep5.cpp
91 lines (75 loc) · 2.24 KB
/
Step5.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
#include <iostream>
#include <cmath>
#include <vector>
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main(int argc, char* argv[])
{
int nx = 81;
int ny = 81; // Number of dots
double dx = 2.0/(nx - 1); // Local spacing in x
double dy = 2.0/(ny - 1); // Local spacing in y
int nt = 100; // Number of timesteps
double sigma = 0.2;
double dt = sigma * dx; // Time step size
int c = 1; // Velocity
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;
}
// Initialize the matrix with ones
std::vector<std::vector<double>> u(nx, std::vector<double> (ny,1.0));
std::vector<std::vector<double>> un(nx, std::vector<double>(ny));
for (int i=19; i<(nx/2); i++)
{
for (int j=19; j<(ny/2); j++){
u[i][j] = 2.0;
}
}
// for (int i=9; i<(ny/2); i++)
// {
// for (int j=19; j<(ny/2); j++){
// u[i][j] = 2.0;
// }
// }
plt::ion();
for (int n=0; n<nt; n++){
for (int i = 0; i < nx; ++i){
for (int j = 0; j < ny; j++){
un[i][j] = u[i][j];
}
}
for (int i=1; i<nx; i++) {
for (int j = 1; j<ny; j++){
u[i][j] = un[i][j] - c*dt/dx*(un[j][i]-u[j][i-1]) - c*dt/dy*(un[j][i]-u[j-1][i]);
}
}
// Boundary conditions
for(int i=0; i<nx; i++) u[0][i] = 1.0;
for(int i=0; i<ny; i++) u[i][0] = 1.0;
for (int i = 0; i < nx; i++) u[i][ny-1] = 1.0;
for (int i = 0; i < ny; i++) u[nx-1][i] = 1.0;
std::vector<std::vector<double>> X(nx, std::vector<double>(ny));
std::vector<std::vector<double>> Y(nx, std::vector<double>(ny));
for (int i = 0; i < nx; ++i) {
for (int j = 0; j < ny; ++j) {
X[i][j] = i * dx;
Y[i][j] = j * dy;
}
}
// , {}, animatedFigconst long animatedFig = plt::figure(1);
plt::clf();
plt::plot_surface(X, Y, u);
plt::xlabel("x");
plt::ylabel("y");
plt::pause(0.1);
}
plt::show();
return 0;
}