Skip to content

Commit 0072f07

Browse files
Removed several memory copies from various IB patch projection subroutines
1 parent a51862d commit 0072f07

1 file changed

Lines changed: 43 additions & 53 deletions

File tree

src/simulation/m_ib_patches.fpp

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,6 @@ contains
456456

457457
center(1) = patch_ib(patch_id)%x_centroid + real(xp, wp)*(x_domain%end - x_domain%beg)
458458
center(2) = patch_ib(patch_id)%y_centroid + real(yp, wp)*(y_domain%end - y_domain%beg)
459-
length(1) = patch_ib(patch_id)%length_x
460-
length(2) = patch_ib(patch_id)%length_y
461-
inverse_rotation(:,:) = patch_ib(patch_id)%rotation_matrix_inverse(:,:)
462459

463460
! encode the periodicity information into the patch_id
464461
call s_encode_patch_periodicity(patch_ib(patch_id)%gbl_patch_id, xp, yp, 0, encoded_patch_id)
@@ -473,16 +470,16 @@ contains
473470
call get_bounding_indices(center(2) - corner_distance, center(2) + corner_distance, y_cc, jl, jr)
474471

475472
! Assign primitive variables if rectangle covers cell and patch has write permission
476-
$:GPU_PARALLEL_LOOP(private='[i, j, xy_local]', copyin='[encoded_patch_id, center, length, inverse_rotation, x_cc, &
477-
& y_cc]', collapse=2)
473+
$:GPU_PARALLEL_LOOP(private='[i, j, xy_local]', copyin='[encoded_patch_id, center]', collapse=2)
478474
do j = jl, jr
479475
do i = il, ir
480476
! get the x and y coordinates in the local IB frame
481477
xy_local = [x_cc(i) - center(1), y_cc(j) - center(2), 0._wp]
482-
xy_local = matmul(inverse_rotation, xy_local)
478+
xy_local = matmul(patch_ib(patch_id)%rotation_matrix_inverse, xy_local)
483479

484-
if (-0.5_wp*length(1) <= xy_local(1) .and. 0.5_wp*length(1) >= xy_local(1) .and. -0.5_wp*length(2) <= xy_local(2) &
485-
& .and. 0.5_wp*length(2) >= xy_local(2)) then
480+
if (-0.5_wp*patch_ib(patch_id)%length_x <= xy_local(1) .and. 0.5_wp*patch_ib(patch_id)%length_x >= xy_local(1) &
481+
& .and. -0.5_wp*patch_ib(patch_id)%length_y <= xy_local(2) &
482+
& .and. 0.5_wp*patch_ib(patch_id)%length_y >= xy_local(2)) then
486483
! Updating the patch identities bookkeeping variable
487484
ib_markers%sf(i, j, 0) = encoded_patch_id
488485
end if
@@ -556,19 +553,14 @@ contains
556553
integer, intent(in) :: xp, yp, zp !< integers containing the periodicity projection information
557554
integer :: i, j, k, ir, il, jr, jl, kr, kl !< Generic loop iterators
558555
integer :: encoded_patch_id
559-
real(wp), dimension(1:3) :: xyz_local, center, length !< x and y coordinates in local IB frame
560-
real(wp), dimension(1:3,1:3) :: inverse_rotation
556+
real(wp), dimension(1:3) :: xyz_local, center !< x and y coordinates in local IB frame
561557
real(wp) :: corner_distance
562558

563-
! Transferring the cuboid's centroid and length information
559+
! Transferring the cuboid's centroid
564560
565561
center(1) = patch_ib(patch_id)%x_centroid + real(xp, wp)*(x_domain%end - x_domain%beg)
566562
center(2) = patch_ib(patch_id)%y_centroid + real(yp, wp)*(y_domain%end - y_domain%beg)
567563
center(3) = patch_ib(patch_id)%z_centroid + real(zp, wp)*(z_domain%end - z_domain%beg)
568-
length(1) = patch_ib(patch_id)%length_x
569-
length(2) = patch_ib(patch_id)%length_y
570-
length(3) = patch_ib(patch_id)%length_z
571-
inverse_rotation(:,:) = patch_ib(patch_id)%rotation_matrix_inverse(:,:)
572564
573565
! encode the periodicity information into the patch_id
574566
call s_encode_patch_periodicity(patch_ib(patch_id)%gbl_patch_id, xp, yp, zp, encoded_patch_id)
@@ -580,25 +572,29 @@ contains
580572
ir = m + gp_layers + 1
581573
jr = n + gp_layers + 1
582574
kr = p + gp_layers + 1
583-
corner_distance = sqrt(dot_product(length, length))/2._wp ! maximum distance any marker can be from the center
575+
corner_distance = 0.5_wp*sqrt(patch_ib(patch_id)%length_x**2 + patch_ib(patch_id)%length_y**2 &
576+
& + patch_ib(patch_id)%length_z**2) ! maximum distance any marker can be from the center
584577
call get_bounding_indices(center(1) - corner_distance, center(1) + corner_distance, x_cc, il, ir)
585578
call get_bounding_indices(center(2) - corner_distance, center(2) + corner_distance, y_cc, jl, jr)
586579
call get_bounding_indices(center(3) - corner_distance, center(3) + corner_distance, z_cc, kl, kr)
587580
588581
! Checking whether the cuboid covers a particular cell in the domain and verifying whether the current patch has permission
589582
! to write to to that cell. If both queries check out, the primitive variables of the current patch are assigned to this
590583
! cell.
591-
$:GPU_PARALLEL_LOOP(private='[i, j, k, xyz_local]', copyin='[encoded_patch_id, center, length, inverse_rotation]', &
592-
& collapse=3)
584+
$:GPU_PARALLEL_LOOP(private='[i, j, k, xyz_local]', copyin='[encoded_patch_id, center]', collapse=3)
593585
do k = kl, kr
594586
do j = jl, jr
595587
do i = il, ir
596588
xyz_local = [x_cc(i), y_cc(j), z_cc(k)] - center ! get coordinate frame centered on IB
597-
xyz_local = matmul(inverse_rotation, xyz_local) ! rotate the frame into the IB's coordinates
598-
599-
if (-0.5*length(1) <= xyz_local(1) .and. 0.5*length(1) >= xyz_local(1) .and. -0.5*length(2) <= xyz_local(2) &
600-
& .and. 0.5*length(2) >= xyz_local(2) .and. -0.5*length(3) <= xyz_local(3) .and. 0.5*length(3) &
601-
& >= xyz_local(3)) then
589+
! rotate the frame into the IB's coordinates
590+
xyz_local = matmul(patch_ib(patch_id)%rotation_matrix_inverse, xyz_local)
591+
592+
if (-0.5_wp*patch_ib(patch_id)%length_x <= xyz_local(1) &
593+
& .and. 0.5_wp*patch_ib(patch_id)%length_x >= xyz_local(1) .and. &
594+
& -0.5_wp*patch_ib(patch_id)%length_y <= xyz_local(2) &
595+
& .and. 0.5_wp*patch_ib(patch_id)%length_y >= xyz_local(2) .and. &
596+
& -0.5_wp*patch_ib(patch_id)%length_z <= xyz_local(3) &
597+
& .and. 0.5_wp*patch_ib(patch_id)%length_z >= xyz_local(3)) then
602598
! Updating the patch identities bookkeeping variable
603599
ib_markers%sf(i, j, k) = encoded_patch_id
604600
end if
@@ -617,21 +613,15 @@ contains
617613
integer, intent(in) :: xp, yp, zp !< integers containing the periodicity projection information
618614
integer :: i, j, k, il, ir, jl, jr, kl, kr !< Generic loop iterators
619615
integer :: encoded_patch_id
620-
real(wp) :: radius
621616
real(wp), dimension(1:3) :: xyz_local, center, length !< x and y coordinates in local IB frame
622617
real(wp), dimension(1:3,1:3) :: inverse_rotation
623618
real(wp) :: corner_distance
624619

625-
! Transferring the cylindrical patch's centroid, length, radius,
620+
! Transferring the cylindrical patch's centroid
626621
627622
center(1) = patch_ib(patch_id)%x_centroid + real(xp, wp)*(x_domain%end - x_domain%beg)
628623
center(2) = patch_ib(patch_id)%y_centroid + real(yp, wp)*(y_domain%end - y_domain%beg)
629624
center(3) = patch_ib(patch_id)%z_centroid + real(zp, wp)*(z_domain%end - z_domain%beg)
630-
length(1) = patch_ib(patch_id)%length_x
631-
length(2) = patch_ib(patch_id)%length_y
632-
length(3) = patch_ib(patch_id)%length_z
633-
radius = patch_ib(patch_id)%radius
634-
inverse_rotation(:,:) = patch_ib(patch_id)%rotation_matrix_inverse(:,:)
635625
636626
! encode the periodicity information into the patch_id
637627
call s_encode_patch_periodicity(patch_ib(patch_id)%gbl_patch_id, xp, yp, zp, encoded_patch_id)
@@ -642,28 +632,31 @@ contains
642632
ir = m + gp_layers + 1
643633
jr = n + gp_layers + 1
644634
kr = p + gp_layers + 1
645-
corner_distance = sqrt(radius**2 + maxval(length)**2) ! distance to rim of cylinder
635+
corner_distance = sqrt(patch_ib(patch_id)%radius**2 + maxval(length)**2) ! distance to rim of cylinder
646636
call get_bounding_indices(center(1) - corner_distance, center(1) + corner_distance, x_cc, il, ir)
647637
call get_bounding_indices(center(2) - corner_distance, center(2) + corner_distance, y_cc, jl, jr)
648638
call get_bounding_indices(center(3) - corner_distance, center(3) + corner_distance, z_cc, kl, kr)
649639
650640
! Checking whether the cylinder covers a particular cell in the domain and verifying whether the current patch has the
651641
! permission to write to that cell. If both queries check out, the primitive variables of the current patch are assigned to
652642
! this cell.
653-
$:GPU_PARALLEL_LOOP(private='[i, j, k, xyz_local]', copyin='[encoded_patch_id, center, length, radius, &
654-
& inverse_rotation]', collapse=3)
643+
$:GPU_PARALLEL_LOOP(private='[i, j, k, xyz_local]', copyin='[encoded_patch_id, center]', collapse=3)
655644
do k = kl, kr
656645
do j = jl, jr
657646
do i = il, ir
658647
xyz_local = [x_cc(i), y_cc(j), z_cc(k)] - center ! get coordinate frame centered on IB
659-
xyz_local = matmul(inverse_rotation, xyz_local) ! rotate the frame into the IB's coordinates
660-
661-
if (((.not. f_is_default(length(1)) .and. xyz_local(2)**2 + xyz_local(3)**2 <= radius**2 .and. &
662-
& -0.5_wp*length(1) <= xyz_local(1) .and. 0.5_wp*length(1) >= xyz_local(1)) &
663-
& .or. (.not. f_is_default(length(2)) .and. xyz_local(1)**2 + xyz_local(3)**2 <= radius**2 .and. &
664-
& -0.5_wp*length(2) <= xyz_local(2) .and. 0.5_wp*length(2) >= xyz_local(2)) &
665-
& .or. (.not. f_is_default(length(3)) .and. xyz_local(1)**2 + xyz_local(2)**2 <= radius**2 .and. &
666-
& -0.5_wp*length(3) <= xyz_local(3) .and. 0.5_wp*length(3) >= xyz_local(3)))) then
648+
! rotate the frame into the IB's coordinates
649+
xyz_local = matmul(patch_ib(patch_id)%rotation_matrix_inverse, xyz_local)
650+
651+
if (((.not. f_is_default(patch_ib(patch_id)%length_x) .and. xyz_local(2)**2 + xyz_local(3) &
652+
& **2 <= patch_ib(patch_id)%radius**2 .and. -0.5_wp*patch_ib(patch_id)%length_x <= xyz_local(1) &
653+
& .and. 0.5_wp*patch_ib(patch_id)%length_x >= xyz_local(1)) &
654+
& .or. (.not. f_is_default(patch_ib(patch_id)%length_y) .and. xyz_local(1)**2 + xyz_local(3) &
655+
& **2 <= patch_ib(patch_id)%radius**2 .and. -0.5_wp*patch_ib(patch_id)%length_y <= xyz_local(2) &
656+
& .and. 0.5_wp*patch_ib(patch_id)%length_y >= xyz_local(2)) &
657+
& .or. (.not. f_is_default(patch_ib(patch_id)%length_z) .and. xyz_local(1)**2 + xyz_local(2) &
658+
& **2 <= patch_ib(patch_id)%radius**2 .and. -0.5_wp*patch_ib(patch_id)%length_z <= xyz_local(3) &
659+
& .and. 0.5_wp*patch_ib(patch_id)%length_z >= xyz_local(3)))) then
667660
! Updating the patch identities bookkeeping variable
668661
ib_markers%sf(i, j, k) = encoded_patch_id
669662
end if
@@ -683,40 +676,37 @@ contains
683676
integer :: i, j, il, ir, jl, jr !< Generic loop iterators
684677
integer :: encoded_patch_id
685678
real(wp), dimension(1:3) :: xy_local !< x and y coordinates in local IB frame
686-
real(wp), dimension(1:2) :: ellipse_coeffs !< a and b in the ellipse coefficients
687679
real(wp), dimension(1:2) :: center !< x and y coordinates in local IB frame
688-
real(wp), dimension(1:3,1:3) :: inverse_rotation
680+
real(wp) :: bounding_radius
689681

690-
! Transferring the ellipse's centroid and length information
682+
! Transferring the ellipse's centroid
691683
692684
center(1) = patch_ib(patch_id)%x_centroid + real(xp, wp)*(x_domain%end - x_domain%beg)
693685
center(2) = patch_ib(patch_id)%y_centroid + real(yp, wp)*(y_domain%end - y_domain%beg)
694-
ellipse_coeffs(1) = 0.5_wp*patch_ib(patch_id)%length_x
695-
ellipse_coeffs(2) = 0.5_wp*patch_ib(patch_id)%length_y
696-
inverse_rotation(:,:) = patch_ib(patch_id)%rotation_matrix_inverse(:,:)
697686
698687
! encode the periodicity information into the patch_id
699688
call s_encode_patch_periodicity(patch_ib(patch_id)%gbl_patch_id, xp, yp, 0, encoded_patch_id)
700689
701690
! find the indices to the left and right of the IB in i, j, k
691+
bounding_radius = 0.5_wp*max(patch_ib(patch_id)%length_x, patch_ib(patch_id)%length_y)
702692
il = -gp_layers - 1
703693
jl = -gp_layers - 1
704694
ir = m + gp_layers + 1
705695
jr = n + gp_layers + 1
706-
call get_bounding_indices(center(1) - maxval(ellipse_coeffs)*2._wp, center(1) + maxval(ellipse_coeffs)*2._wp, x_cc, il, ir)
707-
call get_bounding_indices(center(2) - maxval(ellipse_coeffs)*2._wp, center(2) + maxval(ellipse_coeffs)*2._wp, y_cc, jl, jr)
696+
call get_bounding_indices(center(1) - bounding_radius*2._wp, center(1) + bounding_radius*2._wp, x_cc, il, ir)
697+
call get_bounding_indices(center(2) - bounding_radius*2._wp, center(2) + bounding_radius*2._wp, y_cc, jl, jr)
708698
709699
! Checking whether the ellipse covers a particular cell in the domain
710-
$:GPU_PARALLEL_LOOP(private='[i, j, xy_local]', copyin='[encoded_patch_id, center, ellipse_coeffs, inverse_rotation, &
711-
& x_cc, y_cc]', collapse=2)
700+
$:GPU_PARALLEL_LOOP(private='[i, j, xy_local]', copyin='[encoded_patch_id, center]', collapse=2)
712701
do j = jl, jr
713702
do i = il, ir
714703
! get the x and y coordinates in the local IB frame
715704
xy_local = [x_cc(i) - center(1), y_cc(j) - center(2), 0._wp]
716-
xy_local = matmul(inverse_rotation, xy_local)
705+
xy_local = matmul(patch_ib(patch_id)%rotation_matrix_inverse(:,:), xy_local)
717706
718707
! Ellipse condition (x/a)^2 + (y/b)^2 <= 1
719-
if ((xy_local(1)/ellipse_coeffs(1))**2 + (xy_local(2)/ellipse_coeffs(2))**2 <= 1._wp) then
708+
if ((xy_local(1)/(0.5_wp*patch_ib(patch_id)%length_x))**2 + (xy_local(2)/(0.5_wp*patch_ib(patch_id)%length_y)) &
709+
& **2 <= 1._wp) then
720710
! Updating the patch identities bookkeeping variable
721711
ib_markers%sf(i, j, 0) = encoded_patch_id
722712
end if

0 commit comments

Comments
 (0)