Files
SPHLite/tests/module/test_input.f90
T
2026-07-02 19:50:44 +08:00

97 lines
3.8 KiB
Fortran

program test_input
use sph_error, only: SPH_SUCCESS
use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, &
sph_build_model, sph_get_particle_count, sph_get_input_warning_count
use sph_input_reader, only: sph_input_get_warning_count
use sph_database, only: material_count, section_count, nset_count, &
elset_count, element_count, coupling_region_count
implicit none
integer :: ierr
integer :: count
integer :: warnings
call check_good_input('tests/data/minimal_particles_011.inp', 4, .true.)
if (material_count /= 1) call fail('material metadata was not saved')
if (section_count /= 1) call fail('section metadata was not saved')
if (nset_count <= 0) call fail('node set metadata was not saved')
if (elset_count <= 0) call fail('element set metadata was not saved')
if (element_count <= 0) call fail('element metadata was not saved')
if (coupling_region_count <= 0) call fail('coupling region metadata was not saved')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed after metadata check')
call check_good_input('tests/data/mixed_case_011.inp', 2, .false.)
call check_good_input('tests/data/output_control_011.inp', 2, .false.)
call check_good_input('tests/data/coupling_region_011.inp', 2, .false.)
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for unknown keyword input')
call sph_read_inp('tests/data/unknown_keyword.inp', ierr)
call assert_ok(ierr, 'unknown keyword input read failed')
call sph_input_get_warning_count(warnings)
if (warnings <= 0) call fail('unknown keyword warning was not recorded')
call sph_get_input_warning_count(warnings, ierr)
call assert_ok(ierr, 'warning count API failed')
if (warnings <= 0) call fail('warning count API did not report warning')
call sph_build_model(ierr)
call assert_ok(ierr, 'unknown keyword input build failed')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed after unknown keyword input')
call check_bad_input('tests/data/missing_material_011.inp')
call check_bad_input('tests/data/missing_section_011.inp')
call check_bad_input('tests/data/missing_time_control_011.inp')
contains
subroutine check_good_input(filename, expected_count, leave_open)
character(len=*), intent(in) :: filename
integer, intent(in) :: expected_count
logical, intent(in) :: leave_open
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for good input')
call sph_read_inp(filename, ierr)
call assert_ok(ierr, 'good input read failed')
call sph_build_model(ierr)
call assert_ok(ierr, 'good model build failed')
call sph_get_particle_count(count, ierr)
call assert_ok(ierr, 'good particle count failed')
if (count /= expected_count) call fail('good input particle count mismatch')
if (.not. leave_open) then
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed after good input')
end if
end subroutine check_good_input
subroutine check_bad_input(filename)
character(len=*), intent(in) :: filename
call sph_initialize(ierr)
call assert_ok(ierr, 'initialize failed for bad input')
call sph_read_inp(filename, ierr)
call assert_ok(ierr, 'bad input read should still parse nodes')
call sph_build_model(ierr)
if (ierr == SPH_SUCCESS) call fail('bad input did not fail model build')
call sph_finalize(ierr)
call assert_ok(ierr, 'finalize failed after bad input')
end subroutine check_bad_input
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_input