-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1D_BTCS.cpp
65 lines (57 loc) · 1.16 KB
/
1D_BTCS.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
#include "1D_BTCS.h"
using namespace std;
void BTCS_1D()
{
double x_l = -1.0;
double x_r = 1.0;
double dx = 0.025;
int nx = ceil((x_r - x_l) / dx);
dx = (x_r - x_l) / nx;
double t = 1.0;
double dt = 0.0025;
int nt = ceil(t / dt);
dt = t / nt;
double alpha = 1 / (Pi * Pi);
double CFL = alpha * dt / pow(dx, 2);
vector<vector<double>> u(nt + 1, vector<double>(nx + 1, 0));// one timestep = one row
vector<double> a(nx + 1, 0);
vector<double> b(nx + 1, 0);
vector<double> c(nx + 1, 0);
vector<double> q(nx + 1, 0);
// initial condition
for (int i = 0; i < nx + 1; i++)
{
u[0][i] = -sin(Pi * (dx * i - 1));
}
a[0] = c[0] = q[0] = 0;
b[0] = 1;
for (int i = 1; i < nx; i++)
{
a[i] = c[i] = -CFL;
b[i] = 1 + 2 * CFL;
}
a[nx] = c[nx] = q[nx] = 0;
b[nx] = 1;
for (int j = 1; j < nt + 1; j++)
{
for (int i = 1; i < nx; i++)
{
q[i] = u[j - 1][i];
}
thomasTridiagonal(a, b, c, u[j],q);
}
ofstream outfile("1d_BTCS_u.dat");
if (outfile.is_open())
{
for (int i = 0; i < nx + 1; i++)
{
outfile << u[nt][i] << " ";
}
outfile << endl;
}
else
{
std::cerr << "Error: unable to open file for writing" << std::endl;
}
return;
}