summaryrefslogtreecommitdiff
path: root/src/main.py
blob: 5e0d6826042566a6d6f72db1cc48ae8732f85fd8 (plain)
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
import numpy as np
import matplotlib.pyplot as plt

PI = np.pi

np.set_printoptions(precision=2, floatmode="maxprec", suppress=True)
figure, axes = plt.subplots()
cb = None


Re = 100
nu = 1 / Re
domain_size = (1, 2)
step = 0.05
N = int(domain_size[0] / step)
M = int(domain_size[1] / step)


shape = (N, M)
alpha = 1  # Coefficient

t_m = 1
h_c = 0.3
l_c = 0.6

# Staggered vars
u = np.zeros(shape=shape, dtype=float)
u_star = np.zeros(shape=shape, dtype=float)
u_new = np.zeros(shape=shape, dtype=float)

v = np.zeros(shape=shape, dtype=float)
v_star = np.zeros(shape=shape, dtype=float)
v_new = np.zeros(shape=shape, dtype=float)

p = np.zeros(shape=shape, dtype=float)
p_star = np.zeros(shape=shape, dtype=float)
p_new = np.zeros(shape=shape, dtype=float)

d_e = np.zeros(shape=shape, dtype=float)
d_n = np.zeros(shape=shape, dtype=float)
b = np.zeros(shape=shape, dtype=float)


def u_boundary(t, y):
    def f(t):
        return 1 if t_m < t else 0.5 * (np.sin(0.5 * PI * (2 * t / t_m - 1)) + 1)
    return 6 * f(t) * (y - h_c) * (1 - y) / (1 - h_c)**2


# Loop
error = 1
precision = 10 ** -7

t = 0.35

iteration = 0
while error > precision:
    iteration += 1
    # Inflow boundary condition
    for i in range(N):
        y = domain_size[0] - i * step
        u_star[i][0] = u_boundary(t, y)
        v_star[i][0] = 0

    # Sides boundary conditions
    for j in range(M):
        v_star[0][j] = 0
        v_star[N - 1][j] = 0
        u_star[0][j] = 0
        u_star[N - 1][j] = 0

    # x-momentum
    for i in range(1, N - 1):
        for j in range(1, M - 1):
            u_E = 0.5 * (u[i][j] + u[i][j + 1])
            u_W = 0.5 * (u[i][j] + u[i][j - 1])
            v_N = 0.5 * (v[i - 1][j] + v[i - 1][j + 1])
            v_S = 0.5 * (v[i][j] + v[i][j + 1])

            a_E = -0.5 * u_E * step + nu
            a_W = +0.5 * u_W * step + nu
            a_N = -0.5 * v_N * step + nu
            a_S = +0.5 * v_S * step + nu

            a_e = 0.5 * step * (u_E - u_W + v_N - v_S) + 4 * nu
            A_e = -step

            d_e[i][j] = A_e / a_e

            u_star[i][j] = (a_E * u[i][j + 1] + a_W * u[i][j - 1] + a_N * u[i - 1][j] + a_S * u[i + 1][j]) / a_e
            + d_e[i][j] * (p[i][j + 1] - p[i][j])

    # y-momentum
    for i in range(1, N - 1):
        for j in range(1, M - 1):
            u_E = 0.5 * (u[i][j] + u[i + 1][j])
            u_W = 0.5 * (u[i][j - 1] + u[i + 1][j - 1])
            v_N = 0.5 * (v[i - 1][j] + v[i][j])
            v_S = 0.5 * (v[i][j] + v[i + 1][j])

            a_E = -0.5 * u_E * step + nu
            a_W = +0.5 * u_W * step + nu
            a_N = -0.5 * v_N * step + nu
            a_S = +0.5 * v_S * step + nu

            a_n = 0.5 * step * (u_E - u_W + v_N - v_S) + 4 * nu
            A_n = -step

            d_n[i][j] = A_n / a_n

            v_star[i][j] = (a_E * v[i][j + 1] + a_W * v[i][j - 1] + a_N * v[i - 1][j] + a_S * v[i + 1][j]) / a_n
            + d_n[i][j] * (p[i][j] - p[i + 1][j])

    # Backwards-facing step boundary conditions (same as sides)
    for i in range(int(h_c / step)):
        for j in range(int(l_c / step)):
            u_star[i][j] = 0
            v_star[i][j] = 0
            p_new[i][j] = 100

    # Pressure correction
    p_c = np.zeros(shape=shape, dtype=float)
    for i in range(1, N - 1):
        for j in range(1, M - 1):
            a_E = -d_e[i][j] * step
            a_W = -d_e[i][j-1] * step
            a_N = -d_n[i-1][j] * step
            a_S = -d_n[i][j] * step
            a_P = a_E + a_W + a_N + a_S
            b[i][j] = step * (-(u_star[i][j] - u_star[i][j-1]) + (v_star[i][j] - v_star[i-1][j]))

            p_c[i][j] = (a_E * p_c[i][j+1] + a_W * p_c[i][j-1] + a_N * p_c[i-1][j] + a_S * p_c[i+1][j] + b[i][j]) / a_P
    p_new = p + p_c * alpha

    # Pressure boundaries
    for i in range(N - 1):
        p_new[i][0] = p_new[i][1]
        p_new[i][M - 1] = p_new[i][M - 2]
    for j in range(M - 1):
        p_new[0][j] = p_new[1][j]
        p_new[N - 1][j] = p_new[N - 2][j]

    # Velocity correction
    for i in range(1, N - 1):
        for j in range(1, M - 1):
            u_new[i][j] = u_star[i][j] + alpha * d_e[i][j] * (p_c[i + 1][j] - p_c[i][j])
            v_new[i][j] = v_star[i][j] + alpha * d_n[i][j] * (p_c[i][j] - p_c[i + 1][j])

    # Continuity residual as error measure
    error = 0
    for i in range(N):
        for j in range(M):
            error += abs(b[i][j])

    u = u_new
    v = v_new
    p = p_new

    # Plotting
    print(error)
    x, y = np.meshgrid(
        np.linspace(0, domain_size[1], shape[1]),
        np.linspace(0, domain_size[0], shape[0]),
    )

    if iteration % 100 == 0:
        pcolormesh = axes.pcolormesh(x, y, p, cmap='PuBuGn')
        if (cb):
            cb.remove()
        cb = plt.colorbar(pcolormesh)

        factor = np.sqrt(u ** 2 + v ** 2)
        u_normalized = u / factor
        v_normalized = v / factor
        plt.quiver(x, y, u_normalized, v_normalized, scale=30)
        plt.pause(0.0001)


plt.show()