-
Notifications
You must be signed in to change notification settings - Fork 1
/
sphereTagging.f90
604 lines (454 loc) · 16.4 KB
/
sphereTagging.f90
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
! Calculate the volume-of-fluid / solid field given the bounding-box indices defined by ind(3,2) for particle inp
! Using level-set scheme of Kempe & Frohlich (2012) eqns 32-33
! The signed distance function, phi, is evaluated as the distance from the Eulerian-cell-corner point to the plane of the closest triangle centroid
subroutine tagCells(ind,inp)
use param, only: VOFx, VOFy, VOFz, VOFp, solid_mask, dx1, dx2,dx3
use mls_param, only: celvol
use mpih
use mpi_param
implicit none
integer, dimension(3,2) :: ind
integer :: inp
real(8) :: vol_sphere
VOFx(:,:,:) = 1.
VOFy(:,:,:) = 1.
VOFz(:,:,:) = 1.
VOFp(:,:,:) = 1.
solid_mask(:,:,:) = .false.
call convex_hull_qc2(ind,inp)
call convex_hull_q12(ind,inp)
call convex_hull_q22(ind,inp)
call convex_hull_q32(ind,inp)
! vol_sphere = sum( 1.0 - VOFp(:,:,kstart:kend) ) * celvol
! call MPI_ALLREDUCE(MPI_IN_PLACE,vol_sphere,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
! if (myid .eq. 0) then
! write(*,*) "Vsphere = ", vol_sphere
! endif
end subroutine tagCells
subroutine convex_hull_q12(ind,inp)
use mls_param
use mpih
use mpi_param, only: kstart,kend
use local_arrays, only: vx,vy,vz
use param
implicit none
real, dimension(3) :: x_grid
real, dimension(3) :: r, x_GC
real, dimension(3,2) :: lim
real :: volp
real :: alpha
real :: alpha_q
integer :: inp, i,j,k, ii,jj,kk
integer, dimension(3,2) :: ind
real tot_vol_1, r_x_u_1(3)
! run over cell centered grid and find int(u)dV and int(r x u)dV
do i = ind(1,1),ind(1,2)
do j = ind(2,1),ind(2,2)
do kk = ind(3,1),ind(3,2)
! periodic BC
x_gc = pos_cm(1:3,inp)
k = kk
call get_periodic_indices(k,x_gc)
if (k.ge.kstart.and.k.le.kend) then
!x_grid(1) = xc(i)
!x_grid(2) = ym(j)
!x_grid(3) = zm(k)
!r = x_grid - x_GC ! relative distance
call level_set12(i,j,k,inp,x_gc,alpha)
ii = modulo(i-1,n1m) + 1
jj = modulo(j-1,n2m) + 1
if(VOFx(ii,jj,k).lt.1.0)then
VOFx(ii,jj,k) = VOFx(ii,jj,k)
else
VOFx(ii,jj,k) = 1.0 - alpha
end if
endif
end do
end do
end do
end subroutine
subroutine level_set12(ic,jc,kc,inp,x_gc,alpha)
use param, only: xm,yc,zc
use geom
implicit none
integer :: i,j,k ! int running over corners
integer :: ic,jc,kc ! center position
integer :: nf
real :: phi,alpha,phi_tot,inva,invb,invc
real, dimension(3) :: x_GC
real, dimension(3) :: r, v, x_grid
!real, dimension(3,nf) :: tri_bar, tri_nor
integer :: inp, tri_ind
! Compute alpha over cell, see Kempe 2012 (JCP)
! alpha = sum(-phi(m) * H(-phi(m))) / sum(|phi(m)|)
! alpha in [0,1]
phi_tot = 0.
alpha = 0.
!x_grid(1) = xm(ic)
!x_grid(2) = ym(jc)
!x_grid(3) = zm(kc)
!call find_closestTri_ind(tri_ind,x_grid,tri_bar,nf)
! run over 8 corners of cell
do i = ic-1,ic
do j = jc,jc+1
do k = kc,kc+1
x_grid(1) = xm(i)
x_grid(2) = yc(j)
x_grid(3) = zc(k)
phi = signDist_sphere(x_grid,x_gc,inp)
!phi = loopoverbeams(x_grid,x_gc,AA,inp)
!phi = signed_distance(x_grid,tri_bar(1:3,tri_ind,inp),tri_nor(1:3,tri_ind,inp))
!if (phi.gt.0.) alpha = alpha + phi
!if (-phi .gt. 0.0d0 ) alpha = alpha + (-phi)
alpha = alpha + (-phi) * heaviside(-phi)
phi_tot = phi_tot + abs(phi)
end do
end do
end do
alpha = alpha / phi_tot
end subroutine
!=================================
! q2
!=================================
subroutine convex_hull_q22(ind,inp)
use mls_param
use mpih
use mpi_param, only: kstart,kend
use local_arrays, only: vy
use geom
implicit none
real, dimension(3) :: x_grid
real, dimension(3) :: r, x_GC
real, dimension(3,2) :: lim
real :: volp
real :: alpha, alpha_q
integer :: inp, i,j,k, ii,jj,kk
integer, dimension(3,2) :: ind
real tot_vol_2, r_x_u_2(3)
! run over cell centered grid and find int(u)dV and int(r x u)dV
do i = ind(1,1),ind(1,2)
do j = ind(2,1),ind(2,2)
do kk = ind(3,1),ind(3,2)
! periodic BC
x_gc = pos_cm(1:3,inp)
k = kk
call get_periodic_indices(k,x_gc)
if (k.ge.kstart.and.k.le.kend) then
!x_grid(1) = xm(i)
!x_grid(2) = yc(j)
!x_grid(3) = zm(k)
!r = x_grid - x_GC ! relative distance
call level_set22(i,j,k,x_GC,inp,alpha)
ii = modulo(i-1,n1m) + 1
jj = modulo(j-1,n2m) + 1
if(VOFy(ii,jj,k).lt.1.0)then
VOFy(ii,jj,k) = VOFy(ii,jj,k)
else
VOFy(ii,jj,k) = 1.0 - alpha
end if
endif
end do
end do
end do
end subroutine
subroutine level_set22(ic,jc,kc,x_GC,inp,alpha)
use param, only: xc,ym,zc
use geom
implicit none
integer :: i,j,k ! int running over corners
integer :: ic,jc,kc ! center position
real :: phi,alpha,phi_tot,inva,invb,invc
real, dimension(3) :: x_GC
real, dimension(3) :: r, v, x_grid
integer :: inp, tri_ind
! Compute alpha over cell, see Kempe 2012 (JCP)
! alpha = sum(-phi(m) * H(-phi(m))) / sum(|phi(m)|)
! alpha in [0,1]
phi_tot = 0.
alpha = 0.
! run over 8 corners of cell
do i = ic,ic+1
do j = jc-1,jc
do k = kc,kc+1
! set location of cell corner
x_grid(1) = xc(i)
x_grid(2) = ym(j)
x_grid(3) = zc(k)
! KZ: If this proves to be expensive, can just fix tri_ind = const. for entire cell
!call find_closestTri_ind(tri_ind,x_grid,tri_bar(:,:,inp),isGhostFace(:,inp),maxnf)
!phi = signed_distance(x_grid,tri_bar(1:3,tri_ind,inp),tri_nor(1:3,tri_ind,inp))
!phi = loopoverbeams(x_grid,x_gc,AA,inp)
phi = signDist_sphere(x_grid,x_gc,inp)
!if (phi.gt.0.) alpha = alpha + phi
!if (-phi .gt. 0.0d0 ) alpha = alpha + (-phi)
alpha = alpha + (-phi) * heaviside(-phi)
phi_tot = phi_tot + abs(phi)
end do
end do
end do
alpha = alpha / phi_tot
end subroutine
!=================================
! q3
!=================================
subroutine convex_hull_q32(ind,inp)
use mls_param
use param
use mpih
use mpi_param, only: kstart,kend
use local_arrays, only: vz
implicit none
real, dimension(3) :: x_grid
real, dimension(3) :: r, x_GC
real, dimension(3,2) :: lim
real :: volp
real :: alpha, alpha_q
integer :: inp, i,j,k, ii,jj,kk
integer, dimension(3,2) :: ind
real tot_vol_3, r_x_u_3(3)
! run over cell centered grid and find int(u)dV and int(r x u)dV
do i = ind(1,1),ind(1,2)
do j = ind(2,1),ind(2,2)
do kk = ind(3,1),ind(3,2)
! periodic BC
x_gc = pos_cm(1:3,inp)
k = kk
call get_periodic_indices(k,x_gc)
if (k.ge.kstart.and.k.le.kend) then
!x_grid(1) = xm(i)
!x_grid(2) = ym(j)
!x_grid(3) = zc(k)
!r = x_grid - x_GC ! relative distance
call level_set32(i,j,k,x_GC,inp,alpha)
! compute int u over V
ii = modulo(i-1,n1m) + 1
jj = modulo(j-1,n2m) + 1
if(VOFz(ii,jj,k).lt.1.0)then
VOFz(ii,jj,k) = VOFz(ii,jj,k)
else
VOFz(ii,jj,k) = 1.0 - alpha
end if
endif
end do
end do
end do
end subroutine
subroutine level_set32(ic,jc,kc,x_GC,inp,alpha)
use param, only: xc,yc,zm
use geom
implicit none
integer :: i,j,k ! int running over corners
integer :: ic,jc,kc ! center position
real :: phi,alpha,phi_tot,inva,invb,invc
real, dimension(3) :: x_GC
real, dimension(3) :: r, v, x_grid
integer :: inp, tri_ind
! Compute alpha over cell, see Kempe 2012 (JCP)
! alpha = sum(-phi(m) * H(-phi(m))) / sum(|phi(m)|)
! alpha in [0,1]
phi_tot = 0.
alpha = 0.
! run over 8 corners of cell
do i = ic,ic+1
do j = jc,jc+1
do k = kc-1,kc
! set location of cell corner
x_grid(1) = xc(i)
x_grid(2) = yc(j)
x_grid(3) = zm(k)
! KZ: If this proves to be expensive, can just fix tri_ind = const. for entire cell
!call find_closestTri_ind(tri_ind,x_grid,tri_bar(:,:,inp),isGhostFace(:,inp),maxnf)
!phi = signed_distance(x_grid,tri_bar(1:3,tri_ind,inp),tri_nor(1:3,tri_ind,inp))
!phi = loopoverbeams(x_grid,x_gc,AA,inp)
phi = signDist_sphere(x_grid,x_gc,inp)
!if (phi.gt.0.) alpha = alpha + phi
!if (-phi .gt. 0.0d0 ) alpha = alpha + (-phi)
alpha = alpha + (-phi) * heaviside(-phi)
phi_tot = phi_tot + abs(phi)
end do
end do
end do
alpha = alpha / phi_tot
end subroutine
subroutine convex_hull_qc2(ind,inp)
use mls_param
use param
use mpih
use mpi_param, only: kstart,kend
use local_arrays, only: temp
implicit none
real, dimension(3) :: x_grid
real, dimension(3) :: r, x_GC
real, dimension(3,2) :: lim
real :: volp
real :: alpha, alpha_q
integer :: inp, i,j,k, ii,jj,kk
integer, dimension(3,2) :: ind
real tot_vol_3, r_x_u_3(3)
real :: u_imh, u_iph, v_jmh, v_jph, w_kmh, w_kph, h31, h32, h33
real :: udx1, udx2, udx3
integer :: jc,kc
integer :: km,kp,jm,jp,ic,im,ip
udx1=dx1*0.5d0
udx2=dx2*0.5d0
udx3=dx3*0.5d0
! run over cell centered grid and find int(u)dV and int(r x u)dV
do i = ind(1,1),ind(1,2)
do j = ind(2,1),ind(2,2)
do kk = ind(3,1),ind(3,2)
! periodic BC
x_gc = pos_cm(1:3,inp)
k = kk
call get_periodic_indices(k,x_gc)
if (k.ge.kstart.and.k.le.kend) then
call level_setc2(i,j,k,x_GC,inp,alpha)
! compute int u over V
ii = modulo(i-1,n1m) + 1
jj = modulo(j-1,n2m) + 1
kc = k
km=kc-1
kp=kc+1
jc = jj
jm=jmv(jj)
jp=jpv(jj)
ic = ii
im=imv(ii)
ip=ipv(ii)
if(VOFp(ii,jj,k).lt.1.0)then
VOFp(ii,jj,k) = VOFp(ii,jj,k)
else
VOFp(ii,jj,k) = 1.0 - alpha
end if
! Solid cell d_Usolid_dxj
if (VOFp(ii,jj,k).lt. 1.0e-6) then
solid_mask(ii,jj,k) = .true.
! d u T | 1 [ ]
! ----------- | = ----- | uT | - uT | |
! d x |i,j,k dx [ i+1/2 i-1/2 ]
! uT |_{i-1/2}
x_grid(1) = xc(i)
x_grid(2) = ym(j)
x_grid(3) = zm(kk)
r = x_grid - x_GC ! relative distance
u_imh = vel_CM(1,inp) + omega_c(2,inp)*r(3) - omega_c(3,inp)*r(2)
! uT |_{i+1/2}
x_grid(1) = xc(i+1)
r = x_grid - x_GC ! relative distance
u_iph = vel_CM(1,inp) + omega_c(2,inp)*r(3) - omega_c(3,inp)*r(2)
h31=( u_iph*(temp(ip,jc,kc)+temp(ic,jc,kc)) &
-u_imh*(temp(ic,jc,kc)+temp(im,jc,kc)) )*udx1
! d v T | 1 [ ]
! ----------- | = ----- | vT | - vT | |
! d y |i,j,k dy [ j+1/2 j-1/2 ]
! vT |_{j-1/2}
x_grid(1) = xm(i)
x_grid(2) = yc(j)
x_grid(3) = zm(kk)
r = x_grid - x_GC ! relative distance
v_jmh = vel_CM(2,inp) + omega_c(3,inp)*r(1) - omega_c(1,inp)*r(3)
! vT |_{j+1/2}
x_grid(2) = yc(j+1)
r = x_grid - x_GC ! relative distance
v_jph = vel_CM(2,inp) + omega_c(3,inp)*r(1) - omega_c(1,inp)*r(3)
h32=( v_jph*(temp(ic,jp,kc)+temp(ic,jc,kc)) &
-v_jmh*(temp(ic,jc,kc)+temp(ic,jm,kc)) )*udx2
! d w T | 1 [ ]
! ----------- | = ----- | wT | - wT | |
! d z |i,j,k dz [ k+1/2 k-1/2 ]
! wT |_{k-1/2}
x_grid(1) = xm(i)
x_grid(2) = ym(j)
x_grid(3) = zc(kk)
r = x_grid - x_GC ! relative distance
w_kmh = vel_CM(3,inp) + omega_c(1,inp)*r(2) - omega_c(2,inp)*r(1)
! wT |_{k+1/2}
x_grid(3) = zc(kk+1)
r = x_grid - x_GC ! relative distance
w_kph = vel_CM(3,inp) + omega_c(1,inp)*r(2) - omega_c(2,inp)*r(1)
h33=( w_kph*(temp(ic,jc,kp)+temp(ic,jc,kc)) &
-w_kmh*(temp(ic,jc,kc)+temp(ic,jc,km)) )*udx3
d_UsolidT_dxj(ii,jj,k) = (h31+h32+h33)
endif
endif
end do
end do
end do
end subroutine
subroutine level_setc2(ic,jc,kc,x_GC,inp,alpha)
use param, only: xc,yc,zc
use geom
implicit none
integer :: i,j,k ! int running over corners
integer :: ic,jc,kc ! center position
real :: phi,alpha,phi_tot,inva,invb,invc
real, dimension(3) :: x_GC
real, dimension(3) :: r, v, x_grid
integer :: inp, tri_ind
! Compute alpha over cell, see Kempe 2012 (JCP)
! alpha = sum(-phi(m) * H(-phi(m))) / sum(|phi(m)|)
! alpha in [0,1]
phi_tot = 0.
alpha = 0.
! run over 8 corners of cell
do i = ic,ic+1
do j = jc,jc+1
do k = kc,kc+1
! set location of cell corner
x_grid(1) = xc(i)
x_grid(2) = yc(j)
x_grid(3) = zc(k)
! KZ: If this proves to be expensive, can just fix tri_ind = const. for entire cell
!call find_closestTri_ind(tri_ind,x_grid,tri_bar(:,:,inp),isGhostFace(:,inp),maxnf)
!phi = signed_distance(x_grid,tri_bar(1:3,tri_ind,inp),tri_nor(1:3,tri_ind,inp))
!phi = loopoverbeams(x_grid,x_gc,AA,inp)
!if (phi.gt.0.) alpha = alpha + phi
phi = signDist_sphere(x_grid,x_gc,inp)
!if (-phi .gt. 0.0d0 ) alpha = alpha + (-phi)
alpha = alpha + (-phi) * heaviside(-phi)
phi_tot = phi_tot + abs(phi)
end do
end do
end do
alpha = alpha / phi_tot
end subroutine
subroutine get_bbox_inds(bbox_inds,inp)
! Retrieve the xyz indices of the bounding box for a given particle
use param
use mls_param
implicit none
integer :: i, nf, tri_ind
real, dimension(3,2) :: lim
integer, dimension(3,2) :: bbox_inds
integer :: inp, padSize
! Padding size indices for safety
padSize = 2
! get bounding box
do i = 1,3
lim(i,1) = minval( pack(xyzv(i,:,inp) , .not. isGhostVert(:,inp) ) )
lim(i,2) = maxval( pack(xyzv(i,:,inp) , .not. isGhostVert(:,inp) ) )
end do
bbox_inds = floor(lim*dx1) + 1 ! compute indices cell centered
! expanding bounding box to be extra safe
bbox_inds(:,1) = bbox_inds(:,1) - padSize
bbox_inds(:,2) = bbox_inds(:,2) + padSize
! Hard code vertical for testing free-fall
!bbox_inds(3,1) = 1
!bbox_inds(3,2) = n3m
!! Hard-code the full domain for testing
!bbox_inds(:,1) = [1, 1, 1]
!bbox_inds(:,2) = [n1m, n2m, n3m]
end subroutine
subroutine get_periodic_indices(k,x)
use param
implicit none
integer :: k
real :: x(3)
if (k .ge. n3) then
k = k - n3m
x(3) = x(3) - zlen
end if
if (k .lt. 1) then
k = k + n3m
x(3) = x(3) + zlen
end if
end subroutine