Add coupling output restart and APIs

This commit is contained in:
Codex
2026-07-04 09:59:52 +08:00
parent 5be6b178bc
commit 5d9933d014
22 changed files with 2636 additions and 89 deletions
@@ -0,0 +1,130 @@
program test_coupling_fortran_api_080
use sph_precision, only: rk, SPH_NDIM
use sph_error, only: SPH_SUCCESS
use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model
use sph_coupling_interface, only: sph_coupling_get_region_count, &
sph_coupling_get_region_info, sph_coupling_export_particles, &
sph_coupling_export_facets, sph_coupling_apply_particle_forces, &
sph_coupling_apply_facet_loads, sph_coupling_apply_motion_boundary, &
sph_coupling_clear_imports, sph_coupling_get_diagnostics, &
SPH_COUPLING_PARTICLE_SCALAR_COUNT, &
SPH_COUPLING_PARTICLE_VECTOR_COUNT, SPH_COUPLING_FACET_VECTOR_COUNT, &
SPH_COUPLING_MOTION_VELOCITY, SPH_COUPLING_MOTION_DIRECT
implicit none
integer :: ierr
call check_coupling_fortran_api()
contains
subroutine check_coupling_fortran_api()
integer :: region_count
character(len=64) :: name
integer :: region_type
integer :: export_mode
integer :: import_mode
integer :: enabled
integer :: particle_count
integer :: facet_count
integer :: ext_ids(3)
integer :: internal_indices(3)
integer :: phases(3)
integer :: statuses(3)
integer :: materials(3)
integer :: sections(3)
real(rk) :: scalar_values(SPH_COUPLING_PARTICLE_SCALAR_COUNT,3)
real(rk) :: particle_vectors(SPH_NDIM,SPH_COUPLING_PARTICLE_VECTOR_COUNT,3)
integer :: facet_ids(3)
integer :: facet_particles(3)
real(rk) :: facet_vectors(SPH_NDIM,SPH_COUPLING_FACET_VECTOR_COUNT,3)
real(rk) :: facet_areas(3)
integer :: indices(1)
integer :: facets(1)
real(rk) :: forces(SPH_NDIM,1)
real(rk) :: loads(SPH_NDIM,1)
real(rk) :: target(SPH_NDIM)
real(rk) :: diag_values(22)
integer :: diag_counts(5)
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for coupling Fortran API')
call sph_read_inp('tests/data/coupling_shell_region_080.inp', ierr)
call assert_ok(ierr, 'read failed for coupling Fortran API')
call sph_build_model(ierr)
call assert_ok(ierr, 'build failed for coupling Fortran API')
call sph_coupling_get_region_count(region_count, ierr)
call assert_ok(ierr, 'Fortran API region count failed')
if (region_count /= 2) call fail('Fortran API region count mismatch')
call sph_coupling_get_region_info(2, name, region_type, export_mode, &
import_mode, enabled, particle_count, facet_count, ierr)
call assert_ok(ierr, 'Fortran API region info failed')
if (trim(name) /= 'SHELL_BOTH') call fail('Fortran API region name mismatch')
if (enabled /= 1 .or. particle_count /= 3 .or. facet_count /= 3) then
call fail('Fortran API region info count mismatch')
end if
call sph_coupling_get_region_info(99, name, region_type, export_mode, &
import_mode, enabled, particle_count, facet_count, ierr)
if (ierr == SPH_SUCCESS) call fail('Fortran API invalid region did not fail')
call sph_coupling_export_particles(2, 3, particle_count, ext_ids, &
internal_indices, phases, statuses, materials, sections, scalar_values, &
particle_vectors, ierr)
call assert_ok(ierr, 'Fortran API particle export failed')
if (particle_count /= 3 .or. ext_ids(1) /= 1) then
call fail('Fortran API particle export content mismatch')
end if
call sph_coupling_export_facets(1, 3, facet_count, facet_ids, &
facet_particles, materials, sections, facet_vectors, facet_areas, ierr)
call assert_ok(ierr, 'Fortran API facet export failed')
if (facet_count /= 3 .or. facet_ids(1) /= 1) then
call fail('Fortran API facet export content mismatch')
end if
call sph_coupling_export_particles(2, 2, particle_count, ext_ids(1:2), &
internal_indices(1:2), phases(1:2), statuses(1:2), materials(1:2), &
sections(1:2), scalar_values(:,1:2), particle_vectors(:,:,1:2), ierr)
if (ierr == SPH_SUCCESS) call fail('Fortran API small export did not fail')
indices(1) = 1
forces(:,1) = (/2.0_rk, 0.0_rk, 0.0_rk/)
call sph_coupling_apply_particle_forces(2, 1, indices, forces, ierr)
call assert_ok(ierr, 'Fortran API particle force import failed')
facets(1) = 1
loads(:,1) = (/0.0_rk, 3.0_rk, 0.0_rk/)
call sph_coupling_apply_facet_loads(1, 1, facets, loads, ierr)
call assert_ok(ierr, 'Fortran API facet load import failed')
target = (/1.0_rk, 0.0_rk, 0.0_rk/)
call sph_coupling_apply_motion_boundary(2, 2, SPH_COUPLING_MOTION_VELOCITY, &
SPH_COUPLING_MOTION_DIRECT, target, ierr)
call assert_ok(ierr, 'Fortran API motion import failed')
call sph_coupling_get_diagnostics(22, diag_values, 5, diag_counts, ierr)
call assert_ok(ierr, 'Fortran API diagnostics failed')
if (diag_counts(4) /= 2) call fail('Fortran API import count mismatch')
call sph_coupling_get_diagnostics(21, diag_values(1:21), 5, diag_counts, ierr)
if (ierr == SPH_SUCCESS) call fail('Fortran API small diagnostics did not fail')
call sph_coupling_clear_imports(ierr)
call assert_ok(ierr, 'Fortran API clear imports failed')
call sph_coupling_get_diagnostics(22, diag_values, 5, diag_counts, ierr)
call assert_ok(ierr, 'Fortran API diagnostics after clear failed')
if (diag_counts(1) < 1) call fail('Fortran API stale count missing')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed for coupling Fortran API')
end subroutine check_coupling_fortran_api
subroutine assert_ok(ierr_value, message)
integer, intent(in) :: ierr_value
character(len=*), intent(in) :: message
if (ierr_value /= SPH_SUCCESS) call fail(message)
end subroutine assert_ok
subroutine fail(message)
character(len=*), intent(in) :: message
write(*, '(A)') trim(message)
stop 1
end subroutine fail
end program test_coupling_fortran_api_080