-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_2d.py
301 lines (227 loc) · 9.57 KB
/
test_2d.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import logging
import unittest
import numpy as np
import matplotlib.pyplot as plt
from constants import units, standard_pressure
from two_d import *
logger = logging.getLogger(__name__)
def setUpModule():
FORMAT = '%(asctime)-15s | %(filename)s:%(lineno)s | %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
dx = 10 * units.m
dy = 10 * units.m
dt = 1 * units.s
# side_length = 160
# side_length = 16
side_length = 4
world_shape = (side_length, side_length)
half = world_shape[0] // 2
quarter = half // 2
spatial_change = (dx, dy)
num_steps = 400
def get_initial_conditions():
V = np.zeros((len(world_shape), *world_shape)) * units.m / units.s
p = np.zeros((*world_shape,)) * units.Pa
rho = np.zeros((*world_shape,)) * units.kg * units.m ** -3
q = np.zeros((*world_shape,)) * units.grams
# initial conditions
q[quarter:half, quarter:half] = 1.0 * units.grams
V[0][:] = 2.0 * units.m / units.s
V[1][:] = -2.0 * units.m / units.s
return V, q
class Test2d(unittest.TestCase):
def test_ctu(self):
V, q = get_initial_conditions()
plt.ion()
plt.figure()
plt.imshow(q)
plt.title('Initial state')
plt.show()
q_prev = q
initial_variation = get_total_variation(q)
logger.info("Initial Variation: %s" % (initial_variation,))
for i in range(num_steps):
print("iteration %s" % i)
plt.clf()
plt.imshow(q_prev)
plt.title('current_state...')
plt.show()
plt.pause(0.001) # pause a bit so that plots are updated
q_next = corner_transport_2d(dt, spatial_change, V, q_prev)
q_prev = q_next
current_variation = get_total_variation(q_prev)
if current_variation.m > initial_variation.m + 0.00001:
print("Variation too high: %s" % (current_variation,))
self.assertEqual(True, False)
else:
q_next = q
final_variation = get_total_variation(q_next)
logger.info("Initial Variation: %s Final Variation: %s" % (initial_variation, final_variation))
plt.ioff()
plt.show()
def test_advect_v(self):
V = np.zeros((len(world_shape), *world_shape)) * units.m / units.s
p = np.full((*world_shape,), standard_pressure.m, dtype=np.float) * units.Pa
t = np.full((*world_shape,), 273.15) * units.kelvin
# initial conditions
# V[0][quarter:half, quarter:half] = 1.0 * units.m / units.s
# V[1][half] = -1.0 * units.m / units.s
p[half, half] += 0.0001 * units.Pa
plt.ion()
plt.figure()
plt.imshow(V[0])
plt.title('Initial state')
plt.show()
p_prev = p
v_prev = V
initial_variation = get_total_variation(p)
logger.info("Initial Variation: %s" % (initial_variation,))
for i in range(num_steps):
p_next = finite_volume_advection(dt, spatial_change, v_prev, p_prev)
# v_x_next = fv_advect_axis_upwind(dt, spatial_change, v_prev, v_prev[0])
# v_y_next = fv_advect_axis_upwind(dt, spatial_change, v_prev, v_prev[1])
pgf = pgf_c_grid(dt, spatial_change, p_prev, t)
# v_next = unit_stack([v_x_next, v_y_next]) - pgf
v_next = v_prev - pgf
# t_next = finite_volume_advection(dt, spatial_change, v_prev, t)
# v_next = -pgf
p_prev = p_next
v_prev = v_next
# t = t_next
current_variation = get_total_variation(p_prev)
if current_variation.m > initial_variation.m + 0.00001:
print("Variation too high: %s" % (current_variation,))
# self.assertEqual(True, False)
print("iteration %s" % i)
plt.clf()
plt.imshow(pgf[0])
# plt.imshow(p_next)
plt.title('current_state...')
plt.show()
plt.pause(0.001) # pause a bit so that plots are updated
else:
p_next = p
final_variation = get_total_variation(p_next)
logger.info("Initial Variation: %s Final Variation: %s" % (initial_variation, final_variation))
plt.ioff()
plt.show()
def test_fv(self):
V, q = get_initial_conditions()
plt.ion()
plt.figure()
plt.imshow(q)
plt.title('Initial state')
plt.show()
q_prev = q
initial_variation = get_total_variation(q)
logger.info("Initial Variation: %s" % (initial_variation,))
for i in range(num_steps):
print("iteration %s" % i)
plt.clf()
plt.imshow(q_prev)
plt.title('current_state...')
plt.show()
plt.pause(0.001) # pause a bit so that plots are updated
q_next = finite_volume_advection(dt, spatial_change, V, q_prev)
q_prev = q_next
current_variation = get_total_variation(q_prev)
if current_variation.m > initial_variation.m + 0.00001:
print("Variation too high: %s" % (current_variation,))
self.assertEqual(True, False)
else:
q_next = q
final_variation = get_total_variation(q_next)
logger.info("Initial Variation: %s Final Variation: %s" % (initial_variation, final_variation))
plt.ioff()
plt.show()
def test_pressure_at_edges(self):
p = np.full((3, 3), 1, dtype=np.float) * units.Pa
p[1, 1] = 0 * units.Pa
p_edge = pressure_at_edge(p)
self.assertEqual(p.u, p_edge.u)
self.assertEqual(0.5 * units.Pa, p_edge[0][0][1])
def test_advection_of_momentum(self):
V = np.zeros((len(world_shape), *world_shape)) * units.m / units.s
p = np.full((*world_shape,), standard_pressure.m, dtype=np.float) * units.Pa
# t = np.full((*world_shape,), 273.15) * units.kelvin
p_next = advect_with_momentum(dt, spatial_change, V, p)
p_next = p_next.to_base_units()
self.assertEqual(p.u, p_next.u)
def test_pgf_one_d(self):
U = np.zeros((1, 3)) * units.m / units.s
p = np.full((3,), 1, dtype=np.float) * units.Pa
p[1, 1] = 0.1 * units.Pa
p_edge = pressure_at_edge_one_d(p)
V = U * p_edge
pgf = pgf_one_d(dt, dx, p).to_base_units()
v_next = V - pgf * p_edge
# We just want this to work without exceptions due to units
self.assertIsNotNone(v_next)
def test_pgf_units(self):
U = np.zeros((2, 3, 3)) * units.m / units.s
p = np.full((3, 3), 1, dtype=np.float) * units.Pa
p[1, 1] = 0.1 * units.Pa
p_edge = pressure_at_edge(p)
V = U * p_edge
pgf = pgf_templess(dt, spatial_change, p)
v_next = V - pgf * p_edge
# We just want this to work without exceptions due to units
self.assertIsNotNone(v_next)
def test_pgf_temp_units(self):
U = np.zeros((2, 3, 3)) * units.m / units.s
p = np.full((3, 3), 1, dtype=np.float) * units.Pa
t = np.full((3, 3), standard_temperature.m) * units.K
p[1, 1] = 0.1 * units.Pa
p_edge = pressure_at_edge(p)
V = U * p_edge
pgf = pgf_c_grid(dt, spatial_change, p, t)
v_next = V - pgf * p_edge
# We just want this to work without exceptions due to units
self.assertIsNotNone(v_next)
def test_run_func(self):
V = np.zeros((len(world_shape), *world_shape)) * units.m / units.s
q = np.zeros((*world_shape,), dtype=np.float) * units.grams
q[quarter:half, quarter:half] = 1.0 * units.grams
V[0][:] = 2.0 * units.m / units.s
V[1][:] = -2.0 * units.m / units.s
state = {"V": V, "q": q}
def ft(V, q):
q_next = corner_transport_2d(dt, spatial_change, V, q)
state = {"V": V, "q": q_next}
return state
stable = run_2d_with_ft(state, ft)
self.assertTrue(stable)
def test_iterative_pgf(self):
V = np.zeros((len(world_shape), *world_shape)) * units.m / units.s
p = np.full((*world_shape,), standard_pressure.m, dtype=np.float) * units.Pa
t = np.full((*world_shape,), 273.15) * units.kelvin
p[half, half] += 0.0001 * units.Pa
state = {"V": V, "p": p, "t": t}
def ft(V, p, t):
p_next = finite_volume_advection(dt, spatial_change, V, p)
pgf = pgf_c_grid(dt, spatial_change, p, t)
v_next = V - pgf
state = {"V": v_next, "p": p_next, "t": t}
return state
stable = run_2d_with_ft(state, ft, display_key="p", variation_key="p")
self.assertTrue(stable)
def test_backward_pgf(self):
# uses a backward euler or Matsuno scheme
V = np.zeros((len(world_shape), *world_shape)) * units.m / units.s
p = np.full((*world_shape,), standard_pressure.m, dtype=np.float) * units.Pa
t = np.full((*world_shape,), 273.15) * units.kelvin
p[half, half] += 0.0001 * units.Pa
state = {"V": V, "p": p, "t": t}
def ft(V, p, t):
p_star = finite_volume_advection(dt, spatial_change, V, p)
pgf_star = pgf_c_grid(dt, spatial_change, p, t)
v_star = V - pgf_star
p_next = finite_volume_advection(dt, spatial_change, v_star, p_star)
pgf_next = pgf_c_grid(dt, spatial_change, p_star, t)
v_next = V - pgf_next
state = {"V": v_next, "p": p_next, "t": t}
return state
stable = run_2d_with_ft(state, ft, display_key="p", variation_key="p")
self.assertTrue(stable)
if __name__ == '__main__':
unittest.main()