Skip to content

Commit 6c69026

Browse files
authored
Fix rotation and MPI for multiple IBs (#788)
1 parent 0fa4516 commit 6c69026

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

src/common/m_helper.fpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,11 @@ contains
315315
!> This procedure creates a transformation matrix.
316316
!! @param p Parameters for the transformation.
317317
!! @return Transformation matrix.
318-
function f_create_transform_matrix(p) result(out_matrix)
318+
function f_create_transform_matrix(p, center) result(out_matrix)
319319
320320
type(ic_model_parameters), intent(in) :: p
321-
t_mat4x4 :: sc, rz, rx, ry, tr, out_matrix
321+
t_vec3, optional, intent(in) :: center
322+
t_mat4x4 :: sc, rz, rx, ry, tr, t_back, t_to_origin, out_matrix
322323
323324
sc = transpose(reshape([ &
324325
p%scale(1), 0._wp, 0._wp, 0._wp, &
@@ -350,7 +351,25 @@ contains
350351
0._wp, 0._wp, 1._wp, p%translate(3), &
351352
0._wp, 0._wp, 0._wp, 1._wp], shape(tr)))
352353
353-
out_matrix = matmul(tr, matmul(ry, matmul(rx, matmul(rz, sc))))
354+
if (present(center)) then
355+
! Translation matrix to move center to the origin
356+
t_to_origin = transpose(reshape([ &
357+
1._wp, 0._wp, 0._wp, -center(1), &
358+
0._wp, 1._wp, 0._wp, -center(2), &
359+
0._wp, 0._wp, 1._wp, -center(3), &
360+
0._wp, 0._wp, 0._wp, 1._wp], shape(tr)))
361+
362+
! Translation matrix to move center back to original position
363+
t_back = transpose(reshape([ &
364+
1._wp, 0._wp, 0._wp, center(1), &
365+
0._wp, 1._wp, 0._wp, center(2), &
366+
0._wp, 0._wp, 1._wp, center(3), &
367+
0._wp, 0._wp, 0._wp, 1._wp], shape(tr)))
368+
369+
out_matrix = matmul(tr, matmul(t_back, matmul(ry, matmul(rx, matmul(rz, matmul(sc, t_to_origin))))))
370+
else
371+
out_matrix = matmul(ry, matmul(rx, rz))
372+
end if
354373
355374
end function f_create_transform_matrix
356375
@@ -372,10 +391,10 @@ contains
372391
!> This procedure transforms a triangle by a matrix, one vertex at a time.
373392
!! @param triangle Triangle to transform.
374393
!! @param matrix Transformation matrix.
375-
subroutine s_transform_triangle(triangle, matrix)
394+
subroutine s_transform_triangle(triangle, matrix, matrix_n)
376395
377396
type(t_triangle), intent(inout) :: triangle
378-
t_mat4x4, intent(in) :: matrix
397+
t_mat4x4, intent(in) :: matrix, matrix_n
379398
380399
integer :: i
381400
@@ -385,20 +404,22 @@ contains
385404
call s_transform_vec(triangle%v(i, :), matrix)
386405
end do
387406
407+
call s_transform_vec(triangle%n(1:3), matrix_n)
408+
388409
end subroutine s_transform_triangle
389410
390411
!> This procedure transforms a model by a matrix, one triangle at a time.
391412
!! @param model Model to transform.
392413
!! @param matrix Transformation matrix.
393-
subroutine s_transform_model(model, matrix)
414+
subroutine s_transform_model(model, matrix, matrix_n)
394415
395416
type(t_model), intent(inout) :: model
396-
t_mat4x4, intent(in) :: matrix
417+
t_mat4x4, intent(in) :: matrix, matrix_n
397418
398419
integer :: i
399420
400421
do i = 1, size(model%trs)
401-
call s_transform_triangle(model%trs(i), matrix)
422+
call s_transform_triangle(model%trs(i), matrix, matrix_n)
402423
end do
403424
404425
end subroutine s_transform_model

src/pre_process/m_patches.fpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,18 +1978,18 @@ contains
19781978
19791979
integer :: i, j, k !< Generic loop iterators
19801980
1981-
type(t_bbox) :: bbox
1981+
type(t_bbox) :: bbox, bbox_old
19821982
type(t_model) :: model
19831983
type(ic_model_parameters) :: params
19841984
1985-
t_vec3 :: point
1985+
t_vec3 :: point, model_center
19861986
19871987
real(wp) :: grid_mm(1:3, 1:2)
19881988
19891989
integer :: cell_num
19901990
integer :: ncells
19911991
1992-
t_mat4x4 :: transform
1992+
t_mat4x4 :: transform, transform_n
19931993
19941994
if (present(ib) .and. proc_rank == 0) then
19951995
print *, " * Reading model: "//trim(patch_ib(patch_id)%model_filepath)
@@ -2017,9 +2017,17 @@ contains
20172017
print *, " * Transforming model."
20182018
end if
20192019
2020-
transform = f_create_transform_matrix(params)
2021-
call s_transform_model(model, transform)
2020+
! Get the model center before transforming the model
2021+
bbox_old = f_create_bbox(model)
2022+
model_center(1:3) = (bbox_old%min(1:3) + bbox_old%max(1:3))/2._wp
20222023
2024+
! Compute the transform matrices for vertices and normals
2025+
transform = f_create_transform_matrix(params, model_center)
2026+
transform_n = f_create_transform_matrix(params)
2027+
2028+
call s_transform_model(model, transform, transform_n)
2029+
2030+
! Recreate the bounding box after transformation
20232031
bbox = f_create_bbox(model)
20242032
20252033
! Show the number of vertices in the original STL model

src/simulation/m_start_up.fpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ contains
665665

666666
call MPI_FILE_SET_VIEW(ifile, disp, MPI_INTEGER, MPI_IO_IB_DATA%view, &
667667
'native', mpi_info_int, ierr)
668-
call MPI_FILE_READ(ifile, MPI_IO_IB_DATA%var%sf, data_size * num_ibs, &
668+
call MPI_FILE_READ(ifile, MPI_IO_IB_DATA%var%sf, data_size, &
669669
MPI_INTEGER, status, ierr)
670670

671671
else
@@ -834,7 +834,7 @@ contains
834834

835835
call MPI_FILE_SET_VIEW(ifile, disp, mpi_p, MPI_IO_levelset_DATA%view, &
836836
'native', mpi_info_int, ierr)
837-
call MPI_FILE_READ(ifile, MPI_IO_levelset_DATA%var%sf, data_size, &
837+
call MPI_FILE_READ(ifile, MPI_IO_levelset_DATA%var%sf, data_size * num_ibs, &
838838
mpi_p, status, ierr)
839839

840840
else

0 commit comments

Comments
 (0)