Skip to content

Commit f6b7d1e

Browse files
committed
refactor periodic centroid projection into a single subroutine
1 parent 067a337 commit f6b7d1e

5 files changed

Lines changed: 46 additions & 78 deletions

File tree

examples/2D_ibm_circle_periodic/case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
Nt = 5000
3636
t_save = 20
3737

38-
# to fully resolve requires 40-60 cells across sphere diameter
38+
# to fully resolve requires 40-60 cells across circle diameter
3939
Nx = 511
4040
Ny = Nx
4141
Nz = 0

src/common/m_compute_levelset.fpp

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ module m_compute_levelset
2424
s_rectangle_levelset, &
2525
s_cuboid_levelset, &
2626
s_sphere_levelset, &
27-
s_ellipse_levelset
27+
s_ellipse_levelset, &
28+
s_periodic_project_center
2829

2930
contains
3031

@@ -47,21 +48,10 @@ contains
4748
center(1, 1) = patch_ib(ib_patch_id)%x_centroid
4849
center(2, 1) = patch_ib(ib_patch_id)%y_centroid
4950

50-
if (periodic_ibs) then ! periodically wrap spheres around domain
51-
if ((center(1, 1) - domain_glb(1, 1)) <= radius) then
52-
center(1, 2) = domain_glb(1, 2) + (center(1, 1) - domain_glb(1, 1))
53-
else if ((domain_glb(1, 2) - center(1, 1)) <= radius) then
54-
center(1, 2) = domain_glb(1, 1) - (domain_glb(1, 2) - center(1, 1))
55-
else
56-
center(1, 2) = center(1, 1)
57-
end if
58-
if ((center(2, 1) - domain_glb(2, 1)) <= radius) then
59-
center(2, 2) = domain_glb(2, 2) + (center(2, 1) - domain_glb(2, 1))
60-
else if ((domain_glb(2, 2) - center(2, 1)) <= radius) then
61-
center(2, 2) = domain_glb(2, 1) - (domain_glb(2, 2) - center(2, 1))
62-
else
63-
center(2, 2) = center(2, 1)
64-
end if
51+
if (periodic_ibs) then ! periodically wrap circles around domain
52+
do i = 1, 2
53+
call s_periodic_project_center(center(i, :), domain_glb(i, :), radius)
54+
end do
6555
end if
6656

6757
$:GPU_PARALLEL_LOOP(private='[i,j,ix,iy,dist_vec,dist,dist_vec_temp,dist_temp]', &
@@ -71,22 +61,23 @@ contains
7161
dist_vec(1) = x_cc(i) - center(1, 1)
7262
dist_vec(2) = y_cc(j) - center(2, 1)
7363
dist_vec(3) = 0._wp
74-
dist = sqrt(sum(dist_vec**2))
64+
dist = sum(dist_vec**2)
7565
if (periodic_ibs) then
7666
! check all permutations of periodically projected ib to find minimum distance
7767
do ix = 1, 2
7868
do iy = 1, 2
7969
dist_vec_temp(1) = x_cc(i) - center(1, ix)
8070
dist_vec_temp(2) = y_cc(j) - center(2, iy)
8171
dist_vec_temp(3) = 0._wp
82-
dist_temp = sqrt(sum(dist_vec_temp**2))
72+
dist_temp = sum(dist_vec_temp**2)
8373
if (dist_temp < dist) then
8474
dist = dist_temp
8575
dist_vec = dist_vec_temp
8676
end if
8777
end do
8878
end do
8979
end if
80+
dist = sqrt(dist)
9081
levelset%sf(i, j, 0, ib_patch_id) = dist - radius
9182
if (f_approx_equal(dist, 0._wp)) then
9283
levelset_norm%sf(i, j, 0, ib_patch_id, :) = 0
@@ -558,27 +549,9 @@ contains
558549
center(3, 1) = patch_ib(ib_patch_id)%z_centroid
559550
560551
if (periodic_ibs) then ! periodically wrap spheres around domain
561-
if ((center(1, 1) - domain_glb(1, 1)) <= radius) then
562-
center(1, 2) = domain_glb(1, 2) + (center(1, 1) - domain_glb(1, 1))
563-
else if ((domain_glb(1, 2) - center(1, 1)) <= radius) then
564-
center(1, 2) = domain_glb(1, 1) - (domain_glb(1, 2) - center(1, 1))
565-
else
566-
center(1, 2) = center(1, 1)
567-
end if
568-
if ((center(2, 1) - domain_glb(2, 1)) <= radius) then
569-
center(2, 2) = domain_glb(2, 2) + (center(2, 1) - domain_glb(2, 1))
570-
else if ((domain_glb(2, 2) - center(2, 1)) <= radius) then
571-
center(2, 2) = domain_glb(2, 1) - (domain_glb(2, 2) - center(2, 1))
572-
else
573-
center(2, 2) = center(2, 1)
574-
end if
575-
if ((center(3, 1) - domain_glb(3, 1)) <= radius) then
576-
center(3, 2) = domain_glb(3, 2) + (center(3, 1) - domain_glb(3, 1))
577-
else if ((domain_glb(3, 2) - center(3, 1)) <= radius) then
578-
center(3, 2) = domain_glb(3, 1) - (domain_glb(3, 2) - center(3, 1))
579-
else
580-
center(3, 2) = center(3, 1)
581-
end if
552+
do i = 1, 3
553+
call s_periodic_project_center(center(i, :), domain_glb(i, :), radius)
554+
end do
582555
end if
583556
584557
$:GPU_PARALLEL_LOOP(private='[i,j,k,ix,iy,iz,dist_vec,dist,dist_vec_temp,dist_temp]', &
@@ -589,7 +562,7 @@ contains
589562
dist_vec(1) = x_cc(i) - center(1, 1)
590563
dist_vec(2) = y_cc(j) - center(2, 1)
591564
dist_vec(3) = z_cc(k) - center(3, 1)
592-
dist = sqrt(sum(dist_vec**2))
565+
dist = sum(dist_vec**2)
593566
if (periodic_ibs) then
594567
! check all permutations of periodically projected ib to find minimum distance
595568
do ix = 1, 2
@@ -598,7 +571,7 @@ contains
598571
dist_vec_temp(1) = x_cc(i) - center(1, ix)
599572
dist_vec_temp(2) = y_cc(j) - center(2, iy)
600573
dist_vec_temp(3) = z_cc(k) - center(3, iz)
601-
dist_temp = sqrt(sum(dist_vec_temp**2))
574+
dist_temp = sum(dist_vec_temp**2)
602575
if (dist_temp < dist) then
603576
dist = dist_temp
604577
dist_vec = dist_vec_temp
@@ -607,6 +580,7 @@ contains
607580
end do
608581
end do
609582
end if
583+
dist = sqrt(dist)
610584
levelset%sf(i, j, k, ib_patch_id) = dist - radius
611585
if (f_approx_equal(dist, 0._wp)) then
612586
levelset_norm%sf(i, j, k, ib_patch_id, :) = (/1, 0, 0/)
@@ -700,4 +674,19 @@ contains
700674

701675
end subroutine s_cylinder_levelset
702676

677+
subroutine s_periodic_project_center(center, domain_glb, radius)
678+
real(wp), dimension(2), intent(inout) :: center
679+
real(wp), dimension(2), intent(in) :: domain_glb
680+
real(wp), intent(in) :: radius
681+
682+
if ((center(1) - domain_glb(1)) <= radius) then
683+
center(2) = domain_glb(2) + (center(1) - domain_glb(1))
684+
else if ((domain_glb(2) - center(1)) <= radius) then
685+
center(2) = domain_glb(1) - (domain_glb(2) - center(1))
686+
else
687+
center(2) = center(1)
688+
end if
689+
690+
end subroutine s_periodic_project_center
691+
703692
end module m_compute_levelset

src/common/m_ib_patches.fpp

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,10 @@ contains
166166
! that cell. If both queries check out, the primitive variables of
167167
! the current patch are assigned to this cell.
168168

169-
if (periodic_ibs) then ! periodically wrap spheres around domain
170-
if ((center(1, 1) - domain_glb(1, 1)) <= radius) then
171-
center(1, 2) = domain_glb(1, 2) + (center(1, 1) - domain_glb(1, 1))
172-
else if ((domain_glb(1, 2) - center(1, 1)) <= radius) then
173-
center(1, 2) = domain_glb(1, 1) - (domain_glb(1, 2) - center(1, 1))
174-
else
175-
center(1, 2) = center(1, 1)
176-
end if
177-
if ((center(2, 1) - domain_glb(2, 1)) <= radius) then
178-
center(2, 2) = domain_glb(2, 2) + (center(2, 1) - domain_glb(2, 1))
179-
else if ((domain_glb(2, 2) - center(2, 1)) <= radius) then
180-
center(2, 2) = domain_glb(2, 1) - (domain_glb(2, 2) - center(2, 1))
181-
else
182-
center(2, 2) = center(2, 1)
183-
end if
169+
if (periodic_ibs) then ! periodically wrap circles around domain
170+
do i = 1, 2
171+
call s_periodic_project_center(center(i, :), domain_glb(i, :), radius)
172+
end do
184173

185174
$:GPU_PARALLEL_LOOP(private='[i,j,ix,iy]', copy='[ib_markers_sf]',&
186175
& copyin='[patch_id,center,r2]', collapse=2)
@@ -624,27 +613,9 @@ contains
624613
! the current patch are assigned to this cell.
625614
626615
if (periodic_ibs) then ! periodically wrap spheres around domain
627-
if ((center(1, 1) - domain_glb(1, 1)) <= radius) then
628-
center(1, 2) = domain_glb(1, 2) + (center(1, 1) - domain_glb(1, 1))
629-
else if ((domain_glb(1, 2) - center(1, 1)) <= radius) then
630-
center(1, 2) = domain_glb(1, 1) - (domain_glb(1, 2) - center(1, 1))
631-
else
632-
center(1, 2) = center(1, 1)
633-
end if
634-
if ((center(2, 1) - domain_glb(2, 1)) <= radius) then
635-
center(2, 2) = domain_glb(2, 2) + (center(2, 1) - domain_glb(2, 1))
636-
else if ((domain_glb(2, 2) - center(2, 1)) <= radius) then
637-
center(2, 2) = domain_glb(2, 1) - (domain_glb(2, 2) - center(2, 1))
638-
else
639-
center(2, 2) = center(2, 1)
640-
end if
641-
if ((center(3, 1) - domain_glb(3, 1)) <= radius) then
642-
center(3, 2) = domain_glb(3, 2) + (center(3, 1) - domain_glb(3, 1))
643-
else if ((domain_glb(3, 2) - center(3, 1)) <= radius) then
644-
center(3, 2) = domain_glb(3, 1) - (domain_glb(3, 2) - center(3, 1))
645-
else
646-
center(3, 2) = center(3, 1)
647-
end if
616+
do i = 1, 3
617+
call s_periodic_project_center(center(i, :), domain_glb(i, :), radius)
618+
end do
648619
649620
$:GPU_PARALLEL_LOOP(collapse=3, private='[i,j,k,ix,iy,iz]', copy='[ib_markers_sf]', &
650621
& copyin='[patch_id,center,r2]')

src/common/m_mpi_common.fpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,9 @@ contains
19281928
19291929
#ifdef MFC_MPI
19301930
deallocate (buff_send, buff_recv)
1931+
#ifndef MFC_POST_PROCESS
1932+
@:DEALLOCATE(domain_glb)
1933+
#endif
19311934
#endif
19321935
19331936
end subroutine s_finalize_mpi_common_module

toolchain/mfc/case_validator.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ def check_ibm(self):
350350
n = self.get('n', 0)
351351
num_ibs = self.get('num_ibs', 0)
352352
periodic_ibs = self.get('periodic_ibs', 'F') == 'T'
353+
cyl_coord = self.get('cyl_coord', 'F') == 'T'
353354

354355
self.prohibit(ib and n <= 0,
355356
"Immersed Boundaries do not work in 1D (requires n > 0)")
@@ -358,6 +359,10 @@ def check_ibm(self):
358359
self.prohibit(not ib and num_ibs > 0,
359360
"num_ibs is set, but ib is not enabled")
360361
if periodic_ibs:
362+
self.prohibit(not ib,
363+
"periodic_ibs is set to true but ib is not enabled")
364+
self.prohibit(cyl_coord,
365+
"periodic_ibs not compatible with cyl_coords")
361366
for direction in ['x', 'y', 'z']:
362367
for end in ['beg', 'end']:
363368
bc_val = self.get(f'bc_{direction}%{end}')

0 commit comments

Comments
 (0)