-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStep3.cpp
66 lines (52 loc) · 1.39 KB
/
Step3.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
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main(int argc, char* argv[])
{
int nx = 41; // Number of dots
double dx = 2.0/(nx - 1); // Local spacing
int nt = 40; // Number of timesteps
double nu = 0.3;
double sigma = 0.2; // Courant number (the new dt will be calculated based on this)
double dx2 = dx*dx;
double dt = sigma * dx*dx / nu;
std::vector<double> un(nx);
// Initialize the matrix with ones
std::vector<double> u(nx,0.0);
std::vector<double> x(nx);
for (int i = 0; i<nx; i++)
{
x[i] = i * dx;
}
// for (int i=9; i<18; i++)
// {
// u[i] = 1.0;
// }
for (int i = 0; i < nx; i++) {
x[i] = (5.0 * i) / (nx - 1);
u[i] = (x[i] >= 0.5 && x[i] <= 1) ? 1 : 0;
}
plt::plot(x,u);
for (int n=0; n<nt; n++){
// double un[nx];
for (int i = 0; i < nx; ++i){
un[i] = u[i];
}
//u[0] = un[0];
double dx2 = dx*dx;
for (int i=0; i<nx; i++) {
u[i] = un[i] + nu * dt/dx2 * (un[i+1] - 2*un[i] + un[i-1]);
}
plt::plot(x, u);
plt::xlabel("x");
plt::ylabel("u");
plt::xlim(-1.0,3.0);
plt::ylim(-0.1,2.1);
plt::pause(0.1);
plt::clf();
}
plt::show();
}