-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiisph.py
445 lines (346 loc) · 12.6 KB
/
iisph.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
import sys
import os
import taichi as ti
import time
import math
import numpy as np
from Canvas import Canvas
#from HashGrid import HashGrid
from ParticleData import ParticleData
#ti.init(arch=ti.gpu,advanced_optimization=False)
ti.init(arch=ti.gpu,advanced_optimization=True)
#gui param
imgSizeX = 512
imgSizeY = 512
current_time = 0.0
total_time = 5.0
eps = 1e-5
test_id = 0
eps = 1e-5
#particle param
particleRadius = 0.025
gridR = particleRadius * 2.0
invGridR = 1.0 / gridR
particleDimX = 20
particleDimY = 20
particleDimZ = 20
particleLiquidNum = particleDimX*particleDimY*particleDimZ
rho_L0 = 1000.0
rho_S0 = rho_L0
VL0 = particleRadius * particleRadius * particleRadius * 0.8 * 8.0
VS0 = VL0
liqiudMass = VL0 * rho_L0
boundary = 2.0
#kernel param
searchR = gridR*2.0
pi = 3.1415926
h3 = searchR*searchR*searchR
m_k = 8.0 / (pi*h3)
m_l = 48.0 / (pi*h3)
#advetion param
gravity = ti.Vector([0.0, -9.81, 0.0])
vel_guess = ti.Vector.field(3, dtype=ti.f32, shape=(particleLiquidNum))
vel = ti.Vector.field(3, dtype=ti.f32, shape=(particleLiquidNum))
vel_max = ti.field( dtype=ti.f32, shape=(particleLiquidNum))
d_vel = ti.Vector.field(3, dtype=ti.f32, shape=(particleLiquidNum))
#pressure param
a_ii = ti.field(dtype=ti.f32, shape=(particleLiquidNum))
d_ii = ti.Vector.field(3, dtype=ti.f32, shape=(particleLiquidNum))
dij_pj = ti.Vector.field(3, dtype=ti.f32, shape=(particleLiquidNum))
pressure_pre = ti.field( dtype=ti.f32, shape=(particleLiquidNum))
pressure = ti.field( dtype=ti.f32, shape=(particleLiquidNum))
rho = ti.field( dtype=ti.f32, shape=(particleLiquidNum))
adv_rho = ti.field( dtype=ti.f32, shape=(particleLiquidNum))
#CFL time step
vs_iter = 0
dv_iter = 0
pr_iter = 0
user_max_t = 0.005
user_min_t = 0.00005
deltaT = ti.field( dtype=ti.f32, shape=(1))
#viscorcity cg sovler
dim_coff = 10.0
omega = 0.5
viscosity = 2.0
viscosity_b = 3.0
viscosity_err = 0.05
avg_density_err = ti.field( dtype=ti.f32, shape=(1))
cg_delta = ti.field( dtype=ti.f32, shape=(1))
cg_delta_old = ti.field( dtype=ti.f32, shape=(1))
cg_delta_zero = ti.field( dtype=ti.f32, shape=(1))
cg_Minv = ti.Matrix.field(3, 3, dtype=ti.f32, shape=(particleLiquidNum))
cg_r = ti.Vector.field(3, dtype=ti.f32, shape=(particleLiquidNum))
cg_dir = ti.Vector.field(3, dtype=ti.f32, shape=(particleLiquidNum))
cg_Ad = ti.Vector.field(3, dtype=ti.f32, shape=(particleLiquidNum))
cg_s = ti.Vector.field(3, dtype=ti.f32, shape=(particleLiquidNum))
global particle_data
def init_particle(filename):
global particle_data
particle_data = ParticleData(gridR)
ZxY = particleDimZ*particleDimY
dis = particleRadius * 2.0
for i in range(particleLiquidNum):
particle_data.add_liquid_point([float(i//ZxY)* dis - particleRadius ,
float((i%ZxY)//particleDimZ)* dis + 0.1,
float(i%particleDimZ)* dis - particleRadius])
particle_data.add_obj(filename)
particle_data.setup_data_gpu()
particle_data.setup_data_cpu()
def compute_nonpressure_force():
init_viscosity_para()
global vs_iter
vs_iter = 0
while vs_iter < 100:
compute_viscosity_force()
vs_iter+=1
if cg_delta[0] <= viscosity_err * cg_delta_zero[0] or cg_delta_zero[0] < eps:
break
combine_nonpressure()
def solve_pressure():
global pr_iter
pr_iter = 0
err = 0.0
while (err > 0.001 or pr_iter < 2) and (pr_iter < 100):
update_iter_info()
update_pressure_force()
err = avg_density_err.to_numpy()[0] / float(particleLiquidNum)
pr_iter += 1
@ti.func
def gradW(r):
res = ti.Vector([0.0, 0.0, 0.0])
rl = r.norm()
q = rl / searchR
if ((rl > 1.0e-5) and (q <= 1.0)):
gradq = r / ( rl*searchR)
if (q <= 0.5):
res = m_l*q*(3.0*q - 2.0)*gradq
else:
factor = 1.0 - q
res = -m_l*(factor*factor)*gradq
return res
@ti.func
def W_norm(v):
res = 0.0
q = v / searchR
if q <= 1.0:
if (q <= 0.5):
qq = q*q
qqq = qq*q
res = m_k*(6.0*qqq - 6.0*qq+1.0)
else:
factor = 1.0 - q
res = m_k*2.0*factor*factor*factor
return res
@ti.func
def W(v):
return W_norm(v.norm())
@ti.kernel
def reset_param():
for i in vel:
vel[i] = ti.Vector([0.0, 0.0, 0.0])
pressure[i] = 0.0
deltaT[0] = 0.001
@ti.func
def get_viscosity_Ax(x: ti.template(), i):
ret = ti.Vector([0.0,0.0,0.0])
cur_neighbor = particle_data.hash_grid.neighborCount[i]
k=0
while k < cur_neighbor:
j = particle_data.hash_grid.neighbor[i, k]
r = particle_data.pos[i] - particle_data.pos[j]
if j < particleLiquidNum:
ret += dim_coff*viscosity * liqiudMass / rho[j] * (x[i] - x[j]).dot(r) / (r.norm_sqr() + 0.01*searchR*searchR) * gradW(r) / rho[i] * deltaT[0]
else:
ret += dim_coff*viscosity_b * rho_S0 / rho[i] * VS0 * x[i].dot(r) / (r.norm_sqr() + 0.01*searchR*searchR) * gradW(r) / rho[i] * deltaT[0]
k+=1
return x[i] - ret
@ti.kernel
def init_viscosity_para():
for i in vel_guess:
vel_guess[i] += vel[i]
for i in cg_Minv:
m = ti.Matrix([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]])
cur_neighbor = particle_data.hash_grid.neighborCount[i]
k=0
while k < cur_neighbor:
j = particle_data.hash_grid.neighbor[i, k]
r = particle_data.pos[i] - particle_data.pos[j]
grad_xij = gradW(r).outer_product(r)
if j < particleLiquidNum:
m += dim_coff * viscosity * liqiudMass / rho[j] / (r.norm_sqr() + 0.01*searchR*searchR) * grad_xij
else:
m += dim_coff * viscosity_b * rho_S0 / rho[i] * VS0 / (r.norm_sqr() + 0.01*searchR*searchR) * grad_xij
k+=1
cg_Minv[i] = (ti.Matrix([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]) - m * (deltaT[0]/rho[i]) ).inverse()
#cg_Minv[i] = ti.Matrix([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
cg_delta_zero[0] = 0.0
for i in cg_r:
cg_r[i] = vel[i] - get_viscosity_Ax(vel_guess, i)
cg_dir[i] = cg_Minv[i] @ cg_r[i]
cg_delta_zero[0] += cg_r[i].dot(cg_dir[i])
cg_delta[0] = cg_delta_zero[0]
@ti.kernel
def compute_viscosity_force():
cg_dAd = eps
for i in cg_r:
cg_Ad[i] = get_viscosity_Ax(cg_dir, i)
cg_dAd += cg_dir[i].dot(cg_Ad[i])
alpha = cg_delta[0] / cg_dAd
cg_delta_old[0] = cg_delta[0]
cg_delta[0] = 0.0
for i in cg_r:
vel_guess[i] += alpha * cg_dir[i]
cg_r[i] = cg_r[i] - alpha * cg_Ad[i]
cg_s[i] = cg_Minv[i] @ cg_r[i]
cg_delta[0] += cg_r[i].dot(cg_s[i])
beta = cg_delta[0] / cg_delta_old[0]
for i in cg_r:
cg_dir[i] = cg_s[i] + beta * cg_dir[i]
@ti.kernel
def compute_density():
for i in rho:
rho[i] = VL0 * W_norm(0.0) * rho_L0
cur_neighbor = particle_data.hash_grid.neighborCount[i]
k=0
while k < cur_neighbor:
j = particle_data.hash_grid.neighbor[i, k]
r = particle_data.pos[i] - particle_data.pos[j]
if j < particleLiquidNum:
rho[i] += VL0 * W(r) * rho_L0
else:
rho[i] += VS0 * W(r) * rho_S0
k += 1
@ti.kernel
def combine_nonpressure():
for i in d_vel:
d_vel[i] = gravity + (vel_guess[i] - vel[i]) / deltaT[0]
vel_guess[i] = vel_guess[i]-vel[i]
@ti.kernel
def compute_advection():
for i in d_ii:
d_ii[i] = ti.Vector([0.0, 0.0, 0.0])
vel[i] += deltaT[0] * d_vel[i]
cur_neighbor = particle_data.hash_grid.neighborCount[i]
k=0
while k < cur_neighbor:
j = particle_data.hash_grid.neighbor[i, k]
r = particle_data.pos[i] - particle_data.pos[j]
gradV = gradW(r)
inv_den = rho_L0 / rho[i]
d_ii[i] += -VL0 * inv_den * inv_den * gradV
k += 1
for i in a_ii:
a_ii[i] = 0.0
density = rho[i] / rho_L0
adv_rho[i] = density
pressure_pre[i] = 0.5 * pressure[i]
cur_neighbor = particle_data.hash_grid.neighborCount[i]
k=0
while k < cur_neighbor:
j = particle_data.hash_grid.neighbor[i, k]
r = particle_data.pos[i] - particle_data.pos[j]
gradV = gradW(r)
if j < particleLiquidNum:
adv_rho[i] += deltaT[0] * VL0 * ((vel[i] - vel[j]).dot(gradV))
else:
adv_rho[i] += deltaT[0] * VS0 * (vel[i] .dot(gradV))
d_ji = VL0 / (density*density) * gradV
a_ii[i] += VL0 * (d_ii[i] - d_ji).dot(gradV)
k += 1
@ti.kernel
def update_iter_info():
for i in dij_pj:
avg_density_err[0] = 0.0
dij_pj[i] = ti.Vector([0.0, 0.0, 0.0])
cur_neighbor = particle_data.hash_grid.neighborCount[i]
k=0
while k < cur_neighbor:
j = particle_data.hash_grid.neighbor[i, k]
if j < particleLiquidNum:
r = particle_data.pos[i] - particle_data.pos[j]
gradV=gradW(r)
densityj = rho[j] / rho_L0
dij_pj[i] += -VL0/(densityj*densityj)*pressure_pre[j]*gradV
k += 1
@ti.kernel
def update_pressure_force():
for i in pressure:
sum=0.0
cur_neighbor = particle_data.hash_grid.neighborCount[i]
k=0
while k < cur_neighbor:
j = particle_data.hash_grid.neighbor[i, k]
r = particle_data.pos[i] - particle_data.pos[j]
gradV = gradW(r)
if j < particleLiquidNum:
density = rho[i] / rho_L0
dji = VL0 / (density*density) * gradV
d_ji_pi = dji * pressure_pre[i]
d_jk_pk = dij_pj[j]
sum += VL0 * ( dij_pj[i] - d_ii[j]*pressure_pre[j] - (d_jk_pk - d_ji_pi)).dot(gradV)
else:
sum += VS0 * dij_pj[i].dot(gradV)
k += 1
b = 1.0 - adv_rho[i]
h2 = deltaT[0] * deltaT[0]
denom = a_ii[i]*h2
if (ti.abs(denom) > eps):
pressure[i] = ti.max( (1.0 - omega) *pressure_pre[i] + omega / denom * (b - h2*sum), 0.0)
#print( adv_rho[i],rho[i] / rho_L0, h2*sum, omega / denom)
else:
pressure[i] = 0.0
if pressure[i] != 0.0:
avg_density_err[0] += (a_ii[i]*pressure[i] + sum)*h2 - b
@ti.kernel
def update_pos():
for i in d_vel:
d_vel[i] = ti.Vector([0.0, 0.0, 0.0])
cur_neighbor = particle_data.hash_grid.neighborCount[i]
k=0
while k < cur_neighbor:
j = particle_data.hash_grid.neighbor[i, k]
r = particle_data.pos[i] - particle_data.pos[j]
gradV = gradW(r)
density_i = rho[i]/ rho_L0
dpi = pressure[i] / (density_i*density_i)
if j < particleLiquidNum:
density_j = rho[j]/ rho_L0
dpj = pressure[j] / (density_j*density_j)
d_vel[i] += - VL0 * (dpi + dpj) * gradV
else:
d_vel[i] += - VS0 * dpi * gradV
k += 1
for i in vel:
vel[i] += d_vel[i] * deltaT[0]
particle_data.pos[i] += vel[i] * deltaT[0]
@ti.kernel
def draw_particle():
for i in particle_data.pos:
if i < particleLiquidNum:
sph_canvas.draw_sphere(particle_data.pos[i], ti.Vector([1.0,1.0,1.0]))
else:
sph_canvas.draw_point(particle_data.pos[i], ti.Vector([0.3,0.3,0.3]))
gui = ti.GUI('iisph', res=(imgSizeX, imgSizeY))
sph_canvas = Canvas(imgSizeX, imgSizeY)
init_particle("model/box_boundry.obj")
reset_param()
while gui.running:
sph_canvas.yaw_cam(0.0,1.0,0.0)
#sph_canvas.static_cam(0.0,1.0,0.0)
particle_data.hash_grid.update_grid()
compute_density()
compute_nonpressure_force()
compute_advection()
solve_pressure()
update_pos()
sph_canvas.clear_canvas()
draw_particle()
gui.set_image(sph_canvas.img.to_numpy())
gui.show()
dt = deltaT.to_numpy()[0]
current_time += dt
print("time:%.3f"%current_time, "step:%.4f"%dt, "viscorcity:", vs_iter, "pressure:", pr_iter)
#sph_canvas.export_png(current_time)
if math.isnan(particle_data.pos.to_numpy()[test_id, 0]) or current_time >= total_time:
print(adv_rho.to_numpy()[test_id], particle_data.pos.to_numpy()[test_id], d_vel.to_numpy()[test_id])
sys.exit()