-
Notifications
You must be signed in to change notification settings - Fork 17
/
boundary_condition.py
491 lines (395 loc) · 17 KB
/
boundary_condition.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
from pathlib import Path
import numpy as np
import taichi as ti
from differentiation import sample
@ti.data_oriented
class BoundaryCondition:
def __init__(self, bc_const, bc_mask):
self._bc_const, self._bc_mask = BoundaryCondition.to_field(bc_const, bc_mask)
@ti.kernel
def set_velocity_boundary_condition(self, vc: ti.template()):
bc_mask = ti.static(self._bc_mask)
for i, j in vc:
if (
bc_mask[i, j] == 1
and 1 <= i < bc_mask.shape[0] - 1
and 1 <= j < bc_mask.shape[1] - 1
):
# for kk scheme
# 壁内部の境界条件を設定、壁の厚さは片側2ピクセル以上を仮定する
if bc_mask[i - 1, j] == 0 and bc_mask[i, j - 1] == 1 and bc_mask[i, j + 1] == 1:
vc[i + 1, j] = -sample(vc, i - 1, j)
elif bc_mask[i + 1, j] == 0 and bc_mask[i, j - 1] == 1 and bc_mask[i, j + 1] == 1:
vc[i - 1, j] = -sample(vc, i + 1, j)
elif bc_mask[i, j - 1] == 0 and bc_mask[i - 1, j] == 1 and bc_mask[i + 1, j] == 1:
vc[i, j + 1] = -sample(vc, i, j - 1)
elif bc_mask[i, j + 1] == 0 and bc_mask[i - 1, j] == 1 and bc_mask[i + 1, j] == 1:
vc[i, j - 1] = -sample(vc, i, j + 1)
elif bc_mask[i, j] == 2:
vc[i, j] = self._bc_const[i, j]
elif bc_mask[i, j] == 3:
vc[i, j].x = ti.max(sample(vc, i - 1, j).x, 0.05) # 逆流しないようにする
@ti.kernel
def set_pressure_boundary_condition(self, pc: ti.template()):
bc_mask = ti.static(self._bc_mask)
for i, j in pc:
if bc_mask[i, j] == 1:
if bc_mask[i - 1, j] == 0 and bc_mask[i, j - 1] == 1 and bc_mask[i, j + 1] == 1:
pc[i, j] = sample(pc, i - 1, j)
elif bc_mask[i + 1, j] == 0 and bc_mask[i, j - 1] == 1 and bc_mask[i, j + 1] == 1:
pc[i, j] = sample(pc, i + 1, j)
elif bc_mask[i, j - 1] == 0 and bc_mask[i - 1, j] == 1 and bc_mask[i + 1, j] == 1:
pc[i, j] = sample(pc, i, j - 1)
elif bc_mask[i, j + 1] == 0 and bc_mask[i - 1, j] == 1 and bc_mask[i + 1, j] == 1:
pc[i, j] = sample(pc, i, j + 1)
elif bc_mask[i - 1, j] == 0 and bc_mask[i, j + 1] == 0:
pc[i, j] = (sample(pc, i - 1, j) + sample(pc, i, j + 1)) / 2.0
elif bc_mask[i + 1, j] == 0 and bc_mask[i, j + 1] == 0:
pc[i, j] = (sample(pc, i + 1, j) + sample(pc, i, j + 1)) / 2.0
elif bc_mask[i - 1, j] == 0 and bc_mask[i, j - 1] == 0:
pc[i, j] = (sample(pc, i - 1, j) + sample(pc, i, j - 1)) / 2.0
elif bc_mask[i + 1, j] == 0 and bc_mask[i, j - 1] == 0:
pc[i, j] = (sample(pc, i + 1, j) + sample(pc, i, j - 1)) / 2.0
elif bc_mask[i, j] == 2:
pc[i, j] = sample(pc, i + 1, j)
elif bc_mask[i, j] == 3:
pc[i, j] = 0.0
@ti.func
def is_wall(self, i, j):
return self._bc_mask[i, j] == 1
@ti.func
def is_fluid_domain(self, i, j):
return self._bc_mask[i, j] == 0
def get_resolution(self):
return self._bc_const.shape[:2]
@staticmethod
def to_field(bc, bc_mask):
bc_field = ti.Vector.field(2, ti.f32, shape=bc.shape[:2])
bc_field.from_numpy(bc)
bc_mask_field = ti.field(ti.u8, shape=bc_mask.shape[:2])
bc_mask_field.from_numpy(bc_mask)
return bc_field, bc_mask_field
class DyeBoundaryCondition(BoundaryCondition):
def __init__(self, bc_const, bc_dye, bc_mask):
self._bc_const, self._bc_dye, self._bc_mask = DyeBoundaryCondition.to_field(
bc_const, bc_dye, bc_mask
)
@ti.kernel
def set_dye_boundary_condition(self, dye: ti.template()):
bc_mask = ti.static(self._bc_mask)
for i, j in dye:
if bc_mask[i, j] == 2:
dye[i, j] = self._bc_dye[i, j]
@staticmethod
def to_field(bc, bc_dye, bc_mask):
bc_field = ti.Vector.field(2, ti.f32, shape=bc.shape[:2])
bc_field.from_numpy(bc)
bc_dye_field = ti.Vector.field(3, ti.f32, shape=bc_dye.shape[:2])
bc_dye_field.from_numpy(bc_dye)
bc_mask_field = ti.field(ti.u8, shape=bc_mask.shape[:2])
bc_mask_field.from_numpy(bc_mask)
return bc_field, bc_dye_field, bc_mask_field
def create_bc_array(x_resolution, y_resolution):
bc = np.zeros((x_resolution, y_resolution, 2), dtype=np.float32)
bc_mask = np.zeros((x_resolution, y_resolution), dtype=np.uint8)
bc_dye = np.zeros((x_resolution, y_resolution, 3), dtype=np.float32)
return bc, bc_mask, bc_dye
def create_color_map(color_list, n_samples):
color_arr = np.vstack(color_list)
x = np.linspace(0.0, 1.0, color_arr.shape[0], endpoint=True)
x_ = np.linspace(0.0, 1.0, n_samples, endpoint=True)
r_arr = np.interp(x_, x, color_arr[:, 0])
g_arr = np.interp(x_, x, color_arr[:, 1])
b_arr = np.interp(x_, x, color_arr[:, 2])
return np.vstack((r_arr, g_arr, b_arr)).T
def set_circle(bc, bc_mask, bc_dye, center, radius):
center = np.asarray(center)
l_ = np.round(np.maximum(center - radius, 0)).astype(np.int32)
u0 = round(min(center[0] + radius, bc.shape[0]))
u1 = round(min(center[1] + radius, bc.shape[1]))
for i in range(l_[0], u0):
for j in range(l_[1], u1):
x = np.array([i, j]) + 0.5
if np.linalg.norm(x - center) < radius:
bc[i, j] = np.array([0.0, 0.0])
bc_mask[i, j] = 1
bc_dye[i, j] = np.array([0.0, 0.0, 0.0])
def set_plane(bc, bc_mask, bc_dye, lower_left, upper_right):
bc[lower_left[0] : upper_right[0], lower_left[1] : upper_right[1]] = np.array([0.0, 0.0])
bc_mask[lower_left[0] : upper_right[0], lower_left[1] : upper_right[1]] = 1
bc_dye[lower_left[0] : upper_right[0], lower_left[1] : upper_right[1]] = np.array(
[0.0, 0.0, 0.0]
)
def set_obstacle_fromfile(bc, bc_mask, bc_dye, filepath):
"""画像ファイルをもとに障害物を設定する
黒の領域を障害物として設定する
"""
from PIL import Image
image = Image.open(filepath).convert("L")
x_res, y_res = bc.shape[:2]
# アスペクト比を維持して画像サイズをy_resに合わせてリサイズする
x_ratio = x_res / image.width
y_ratio = y_res / image.height
resize_size = (
(x_res, round(image.height * x_ratio))
if x_ratio < y_ratio
else (round(image.width * y_ratio), y_res)
)
image = image.resize(resize_size)
# 領域の中央へ移動する
image_ = Image.new(image.mode, (x_res, y_res), 255)
image_.paste(image, ((x_res - image.width) // 2, 0))
mask_indices = np.flip(np.array(image_).T, axis=1) < 200
bc[mask_indices] = np.array([0.0, 0.0])
bc_mask[mask_indices] = 1
bc_dye[mask_indices] = np.array([0.0, 0.0, 0.0])
def get_boundary_condition(num, resolution, no_dye):
if num == 1:
boundary_condition = create_boundary_condition1(resolution, no_dye)
elif num == 2:
boundary_condition = create_boundary_condition2(resolution, no_dye)
elif num == 3:
boundary_condition = create_boundary_condition3(resolution, no_dye)
elif num == 4:
boundary_condition = create_boundary_condition4(resolution, no_dye)
elif num == 5:
boundary_condition = create_boundary_condition5(resolution, no_dye)
elif num == 6:
boundary_condition = create_boundary_condition6(resolution, no_dye)
else:
raise NotImplementedError
return boundary_condition
def create_boundary_condition1(resolution, no_dye=False):
# 1: 壁, 2: 流入部, 3: 流出部
x_res, y_res = 2 * resolution, resolution
bc, bc_mask, bc_dye = create_bc_array(x_res, y_res)
# 流入部の設定
def set_inflow():
bc[:2, :] = np.array([1.0, 0.0])
bc_mask[:2, :] = 2
y = np.array([1.1, 1.1, 0.2])
b = np.array([0.2, 0.2, 1.1])
r = np.array([1.1, 0.2, 0.2])
c = np.array([0.2, 1.1, 1.1])
color_map = create_color_map([c, r, b, y] * 3, bc_dye.shape[1])
bc_dye[:2, :] = np.stack((color_map, color_map), axis=0)
# 流出部の設定
def set_outflow():
bc[-1, :] = np.array([0.0, 0.0])
bc_mask[-1, :] = 3
# 壁の設定
def set_wall():
set_plane(bc, bc_mask, bc_dye, (0, 0), (x_res, 2)) # 下
set_plane(bc, bc_mask, bc_dye, (0, y_res - 2), (x_res, y_res)) # 上
# 円柱の設定
r = y_res // 18
c = (x_res // 4, y_res // 2)
set_circle(bc, bc_mask, bc_dye, c, r)
set_inflow()
set_outflow()
set_wall()
if no_dye:
boundary_condition = BoundaryCondition(bc, bc_mask)
else:
boundary_condition = DyeBoundaryCondition(bc, bc_dye, bc_mask)
return boundary_condition
def create_boundary_condition2(resolution, no_dye=False):
# 1: 壁, 2: 流入部, 3: 流出部
x_res, y_res = 2 * resolution, resolution
bc, bc_mask, bc_dye = create_bc_array(x_res, y_res)
# 流入部の設定
def set_inflow():
bc[:2, :] = np.array([1.0, 0.0])
bc_mask[:2, :] = 2
bc_dye[:2, :] = np.array([0.2, 0.2, 1.2])
width = y_res // 10
for i in range(0, y_res, width):
bc_dye[:2, i : i + width // 2] = np.array([1.2, 1.2, 0.2])
# 壁の設定
def set_wall():
set_plane(bc, bc_mask, bc_dye, (0, 0), (2, y_res // 3)) # 左下
set_plane(bc, bc_mask, bc_dye, (0, 2 * y_res // 3), (2, y_res)) # 左上
set_plane(bc, bc_mask, bc_dye, (x_res - 2, 0), (x_res, y_res)) # 右
set_plane(bc, bc_mask, bc_dye, (0, 0), (x_res, 2)) # 下
set_plane(bc, bc_mask, bc_dye, (0, y_res - 2), (x_res, y_res)) # 上
x_point = x_res // 5
y_point = y_res // 2
size = y_res // 32 # 壁幅
# 左
set_plane(bc, bc_mask, bc_dye, (x_point - size, y_point), (x_point + size, y_res))
# 真ん中左
set_plane(bc, bc_mask, bc_dye, (2 * x_point - size, 0), (2 * x_point + size, y_point))
# 真ん中右
set_plane(bc, bc_mask, bc_dye, (3 * x_point - size, y_point), (3 * x_point + size, y_res))
# 右
set_plane(bc, bc_mask, bc_dye, (4 * x_point - size, 0), (4 * x_point + size, y_point))
# 流出部の設定
def set_outflow():
y_point = y_res // 3
bc[-2:, y_point : 2 * y_point] = np.array([0.0, 0.0])
bc_mask[-2:, y_point : 2 * y_point] = 3
set_inflow()
set_wall()
set_outflow()
if no_dye:
boundary_condition = BoundaryCondition(bc, bc_mask)
else:
boundary_condition = DyeBoundaryCondition(bc, bc_dye, bc_mask)
return boundary_condition
def create_boundary_condition3(resolution, no_dye=False):
# 1: 壁, 2: 流入部, 3: 流出部
x_res, y_res = 2 * resolution, resolution
bc, bc_mask, bc_dye = create_bc_array(x_res, y_res)
# 流入部の設定
def set_inflow():
bc[:2, :] = np.array([1.0, 0.0])
bc_mask[:2, :] = 2
y = np.array([1.1, 1.1, 0.2])
b = np.array([0.2, 0.2, 1.1])
r = np.array([1.1, 0.2, 0.2])
c = np.array([0.2, 1.1, 1.1])
color_map = create_color_map([c, r, b, y], bc_dye.shape[1])
bc_dye[:2, :] = np.stack((color_map, color_map), axis=0)
# 流出部の設定
def set_outflow():
bc[-1, :] = np.array([0.0, 0.0])
bc_mask[-1, :] = 3
# 壁の設定
def set_wall():
set_plane(bc, bc_mask, bc_dye, (0, 0), (x_res, 2)) # 下
set_plane(bc, bc_mask, bc_dye, (0, y_res - 2), (x_res, y_res)) # 上
# 円柱ランダム生成
ref_resolution = 500
np.random.seed(123)
points = np.random.uniform(0, x_res, (100, 2))
points = points[points[:, 1] < y_res]
r = 16 * (y_res / ref_resolution)
for p in points:
set_circle(bc, bc_mask, bc_dye, p, r)
set_inflow()
set_outflow()
set_wall()
if no_dye:
boundary_condition = BoundaryCondition(bc, bc_mask)
else:
boundary_condition = DyeBoundaryCondition(bc, bc_dye, bc_mask)
return boundary_condition
def create_boundary_condition4(resolution, no_dye=False):
# 1: 壁, 2: 流入部, 3: 流出部
x_res, y_res = 2 * resolution, resolution
bc, bc_mask, bc_dye = create_bc_array(x_res, y_res)
# 壁の設定
def set_wall():
set_plane(bc, bc_mask, bc_dye, (0, 0), (2, y_res)) # 左
set_plane(bc, bc_mask, bc_dye, (x_res - 2, 0), (x_res, y_res)) # 右
set_plane(bc, bc_mask, bc_dye, (0, 0), (x_res, 2)) # 下
set_plane(bc, bc_mask, bc_dye, (0, y_res - 2), (x_res, y_res)) # 上
# 流入部の設定
def set_inflow():
y = np.array([1.1, 1.1, 0.2])
b = np.array([0.2, 0.2, 1.1])
r = np.array([1.1, 0.2, 0.2])
c = np.array([0.2, 1.1, 1.1])
color_map = create_color_map([c, r, b, y], y_res // 4 - 2)
bc_dye[:2, 3 * y_res // 4 : -2] = np.stack((color_map, color_map), axis=0)
bc_dye[:2, 2 : y_res // 4] = np.stack((color_map, color_map), axis=0)
# 左上
bc[:2, 3 * y_res // 4 : -2] = np.array([1.0, 0.0])
bc_mask[:2, 3 * y_res // 4 : -2] = 2
# 左下
bc[:2, 2 : y_res // 4] = np.array([1.0, 0.0])
bc_mask[:2, 2 : y_res // 4] = 2
# 流出部の設定
def set_outflow():
# 中央
bc[-2:, 3 * y_res // 8 : 5 * y_res // 8] = np.array([0.0, 0.0])
bc_mask[-2:, 3 * y_res // 8 : 5 * y_res // 8] = 3
set_wall()
set_inflow()
set_outflow()
if no_dye:
boundary_condition = BoundaryCondition(bc, bc_mask)
else:
boundary_condition = DyeBoundaryCondition(bc, bc_dye, bc_mask)
return boundary_condition
def create_boundary_condition5(resolution, no_dye=False):
# 1: 壁, 2: 流入部, 3: 流出部
x_res, y_res = 2 * resolution, resolution
bc, bc_mask, bc_dye = create_bc_array(x_res, y_res)
# 流出部の設定
def set_outflow():
bc[-2:, :] = np.array([0.0, 0.0])
bc_mask[-2:, :] = 3
# 流入部の設定
def set_inflow():
bc[:2, 2 : y_res // 3] = np.array([1.0, 0.0])
bc_mask[:2, 2 : y_res // 3] = 2
bc_dye[:2, 2 : y_res // 3] = np.array([1.2, 0.2, 0.2])
bc[:2, 2 * y_res // 3 : y_res - 2] = np.array([1.0, 0.0])
bc_mask[:2, 2 * y_res // 3 : y_res - 2] = 2
bc_dye[:2, 2 * y_res // 3 : y_res - 2] = np.array([0.2, 1.2, 1.2])
# 壁の設定
def set_wall():
set_plane(bc, bc_mask, bc_dye, (0, 0), (x_res, 2)) # 下
set_plane(bc, bc_mask, bc_dye, (0, y_res - 2), (x_res, y_res)) # 上
size = x_res // 64
set_plane(
bc, bc_mask, bc_dye, (0, y_res // 5), (11 * x_res // 30, 4 * y_res // 5)
) # 左真中
set_plane(
bc, bc_mask, bc_dye, (x_res // 2 - size, 0), (x_res // 2 + size, 2 * y_res // 5)
) # 下真中
set_plane(
bc, bc_mask, bc_dye, (x_res // 2 - size, 3 * y_res // 5), (x_res // 2 + size, y_res)
) # 上真中
# 障害物
y_point = y_res // 6
v = np.array([y_res, y_res]) // 25
params = [(7, 8, 9, 10, 11), (0, 1, 0, 1, 0)]
for a, b in zip(*params):
for i in range(1, 6 + b):
p = np.array([a * x_res // 12, i * y_point - b * y_res // 12])
lower_left = p - v
upper_right = p + v
set_plane(bc, bc_mask, bc_dye, lower_left, upper_right)
set_inflow()
set_outflow()
set_wall()
if no_dye:
boundary_condition = BoundaryCondition(bc, bc_mask)
else:
boundary_condition = DyeBoundaryCondition(bc, bc_dye, bc_mask)
return boundary_condition
def create_boundary_condition6(resolution, no_dye=False):
# 1: 壁, 2: 流入部, 3: 流出部
x_res, y_res = 2 * resolution, resolution
bc, bc_mask, bc_dye = create_bc_array(x_res, y_res)
# 流入部の設定
def set_inflow():
bc[:2, :] = np.array([1.0, 0.0])
bc_mask[:2, :] = 2
y = np.array([1.1, 1.1, 0.2])
b = np.array([0.2, 0.2, 1.1])
r = np.array([1.1, 0.2, 0.2])
c = np.array([0.2, 1.1, 1.1])
color_map = create_color_map([c, r, b, y], bc_dye.shape[1])
bc_dye[:2, :] = np.stack((color_map, color_map), axis=0)
# 流出部の設定
def set_outflow():
bc[-1, :] = np.array([0.0, 0.0])
bc_mask[-1, :] = 3
# 壁の設定
def set_wall():
set_plane(bc, bc_mask, bc_dye, (0, 0), (x_res, 2)) # 下
set_plane(bc, bc_mask, bc_dye, (0, y_res - 2), (x_res, y_res)) # 上
basepath = Path(__file__).resolve().parent
filepath = basepath / "images/bc_mask/dragon.png"
set_obstacle_fromfile(bc, bc_mask, bc_dye, filepath)
set_inflow()
set_outflow()
set_wall()
if no_dye:
boundary_condition = BoundaryCondition(bc, bc_mask)
else:
boundary_condition = DyeBoundaryCondition(bc, bc_dye, bc_mask)
return boundary_condition