Files
FemLite/tests/module/test_external_handoff.f90
T
2026-06-26 00:48:57 +08:00

444 lines
16 KiB
Fortran

program test_external_handoff
use fem_precision
use fem_error
use fem_sparse_csr
use fem_sparse_csr_diagnostics
use fem_linear_solver_ext_audit
use fem_linear_solver_ext_io
use fem_linear_solver_ext_handoff
implicit none
real(rk), parameter :: TOL = 1.0e-10_rk
call check_path_builder_and_names()
call check_io_roundtrip()
call check_handoff_export()
call check_handoff_import_audit()
call check_bad_solution_audit()
call check_symmetric_storage()
call check_error_paths()
call cleanup_files()
contains
subroutine check_path_builder_and_names()
character(len=128) :: matrix_file
character(len=128) :: rhs_file
character(len=128) :: initial_file
character(len=128) :: solution_file
character(len=128) :: csr_file
character(len=128) :: manifest_file
character(len=32) :: name
integer :: ierr
call ext_io_build_paths('ext_handoff_unit', matrix_file, rhs_file, &
initial_file, solution_file, csr_file, &
manifest_file, ierr)
call require_success(ierr, 10)
if (trim(matrix_file) .ne. 'ext_handoff_unit_matrix.mtx') call fail(11)
if (trim(rhs_file) .ne. 'ext_handoff_unit_rhs.vec') call fail(12)
if (trim(initial_file) .ne. 'ext_handoff_unit_initial.vec') call fail(13)
if (trim(solution_file) .ne. 'ext_handoff_unit_solution.vec') call fail(14)
if (trim(csr_file) .ne. 'ext_handoff_unit_csr.txt') call fail(15)
if (trim(manifest_file) .ne. 'ext_handoff_unit_manifest.txt') call fail(16)
call ext_io_storage_name(EXT_IO_STORAGE_GENERAL, name, ierr)
call require_success(ierr, 17)
if (trim(name) .ne. 'general') call fail(18)
call ext_io_storage_name(EXT_IO_STORAGE_SYMMETRIC_LOWER, name, ierr)
call require_success(ierr, 19)
if (trim(name) .ne. 'symmetric_lower') call fail(20)
call ext_handoff_status_name(EXT_HANDOFF_EMPTY, name, ierr)
call require_success(ierr, 21)
if (trim(name) .ne. 'empty') call fail(22)
call ext_handoff_status_name(EXT_HANDOFF_AUDITED, name, ierr)
call require_success(ierr, 23)
if (trim(name) .ne. 'audited') call fail(24)
end subroutine check_path_builder_and_names
subroutine check_io_roundtrip()
type(csr_matrix) :: a
type(csr_profile) :: profile
type(ext_io_manifest) :: manifest
type(ext_io_manifest) :: loaded_manifest
real(rk) :: rhs(3)
real(rk) :: read_back(3)
integer :: ierr
integer :: nrow
integer :: ncol
integer :: nnz
integer :: index_base
integer :: storage
real(rk) :: tolerance
real(rk) :: matrix_norm
real(rk) :: rhs_norm
call cleanup_files()
call build_spd(a, ierr)
call require_success(ierr, 30)
call csr_analyze(a, profile, ierr)
call require_success(ierr, 31)
rhs = (/ ONE, ZERO, ONE /)
call ext_io_write_matrix_market('ext_handoff_io_matrix.mtx', a, &
EXT_IO_STORAGE_GENERAL, ierr)
call require_success(ierr, 32)
call require_file_lines('ext_handoff_io_matrix.mtx', 10, 33)
call ext_io_write_csr_arrays('ext_handoff_io_csr.txt', a, &
EXT_IO_INDEX_ZERO, ierr)
call require_success(ierr, 34)
call require_file_lines('ext_handoff_io_csr.txt', 23, 35)
call ext_io_write_vector('ext_handoff_io_rhs.vec', rhs, 'rhs', ierr)
call require_success(ierr, 36)
call ext_io_read_vector('ext_handoff_io_rhs.vec', read_back, ierr)
call require_success(ierr, 37)
call check_close(read_back(1), rhs(1), 38)
call check_close(read_back(2), rhs(2), 39)
call check_close(read_back(3), rhs(3), 40)
call ext_io_fill_manifest(manifest, 'superlu_file', &
'ext_handoff_io_matrix.mtx', 'ext_handoff_io_rhs.vec', &
'ext_handoff_io_initial.vec', 'ext_handoff_io_solution.vec', &
'ext_handoff_io_csr.txt', profile, EXT_IO_INDEX_ZERO, &
EXT_IO_STORAGE_GENERAL, TOL, sqrt(2.0_rk), ierr)
call require_success(ierr, 41)
call ext_io_write_manifest('ext_handoff_io_manifest.txt', manifest, ierr)
call require_success(ierr, 42)
call require_file_lines('ext_handoff_io_manifest.txt', 14, 43)
call ext_io_read_manifest('ext_handoff_io_manifest.txt', &
loaded_manifest, ierr)
call require_success(ierr, 143)
if (trim(loaded_manifest%backend) .ne. 'superlu_file') call fail(144)
if (loaded_manifest%nrow .ne. 3 .or. loaded_manifest%nnz .ne. 7) &
call fail(145)
if (loaded_manifest%storage .ne. EXT_IO_STORAGE_GENERAL) call fail(146)
call ext_io_manifest_summary(manifest, nrow, ncol, nnz, index_base, &
storage, tolerance, matrix_norm, rhs_norm, ierr)
call require_success(ierr, 44)
if (nrow .ne. 3 .or. ncol .ne. 3 .or. nnz .ne. 7) call fail(45)
if (index_base .ne. EXT_IO_INDEX_ZERO) call fail(46)
if (storage .ne. EXT_IO_STORAGE_GENERAL) call fail(47)
call check_close(tolerance, TOL, 48)
call check_close(matrix_norm, 4.0_rk, 49)
call check_close(rhs_norm, sqrt(2.0_rk), 50)
call csr_free(a, ierr)
call require_success(ierr, 51)
call cleanup_files()
end subroutine check_io_roundtrip
subroutine check_handoff_export()
type(csr_matrix) :: a
type(ext_handoff_package) :: package
real(rk) :: rhs(3)
real(rk) :: x0(3)
character(len=128) :: matrix_file
character(len=128) :: rhs_file
character(len=128) :: initial_file
character(len=128) :: solution_file
character(len=128) :: csr_file
integer :: ierr
integer :: status
integer :: ready
integer :: nrow
integer :: ncol
integer :: nnz
real(rk) :: residual
real(rk) :: relative
real(rk) :: backward
call cleanup_files()
call build_spd(a, ierr)
call require_success(ierr, 60)
rhs = (/ ZERO, ZERO, 4.0_rk /)
x0 = ZERO
call ext_handoff_create('ext_handoff_case', 'ref_file', 40, a, rhs, x0, &
TOL, 25, EXT_IO_INDEX_ZERO, EXT_IO_STORAGE_GENERAL, package, ierr)
call require_success(ierr, 61)
if (package%status .ne. EXT_HANDOFF_EXPORTED) call fail(62)
if (package%request%ready .ne. 1) call fail(63)
if (trim(package%backend) .ne. 'ref_file') call fail(64)
call ext_handoff_files(package, matrix_file, rhs_file, initial_file, &
solution_file, csr_file, ierr)
call require_success(ierr, 65)
if (trim(matrix_file) .ne. 'ext_handoff_case_matrix.mtx') call fail(66)
if (trim(solution_file) .ne. 'ext_handoff_case_solution.vec') call fail(67)
call require_file_lines(matrix_file, 10, 68)
call require_file_lines(rhs_file, 6, 69)
call require_file_lines(initial_file, 6, 70)
call require_file_lines(csr_file, 23, 71)
call require_file_lines('ext_handoff_case_manifest.txt', 14, 72)
call ext_handoff_summary(package, status, ready, nrow, ncol, nnz, &
residual, relative, backward, ierr)
call require_success(ierr, 73)
if (status .ne. EXT_HANDOFF_EXPORTED .or. ready .ne. 0) call fail(74)
if (nrow .ne. 3 .or. ncol .ne. 3 .or. nnz .ne. 7) call fail(75)
call check_close(residual, ZERO, 76)
call csr_free(a, ierr)
call require_success(ierr, 77)
call cleanup_files()
end subroutine check_handoff_export
subroutine check_handoff_import_audit()
type(csr_matrix) :: a
type(ext_handoff_package) :: package
real(rk) :: rhs(3)
real(rk) :: x0(3)
real(rk) :: solution(3)
real(rk) :: imported(3)
integer :: ierr
integer :: status
integer :: ready
integer :: nrow
integer :: ncol
integer :: nnz
real(rk) :: residual
real(rk) :: relative
real(rk) :: backward
call cleanup_files()
call build_spd(a, ierr)
call require_success(ierr, 90)
rhs = (/ ZERO, ZERO, 4.0_rk /)
x0 = ZERO
solution = (/ ONE, 2.0_rk, 3.0_rk /)
call ext_handoff_create('ext_handoff_case', 'ref_file', 40, a, rhs, x0, &
TOL, 25, EXT_IO_INDEX_ZERO, EXT_IO_STORAGE_GENERAL, package, ierr)
call require_success(ierr, 91)
call ext_io_write_vector(trim(package%manifest%solution_file), solution, &
'solution', ierr)
call require_success(ierr, 92)
call ext_handoff_import_solution(package, a, rhs, imported, ierr)
call require_success(ierr, 93)
call check_close(imported(1), ONE, 94)
call check_close(imported(2), 2.0_rk, 95)
call check_close(imported(3), 3.0_rk, 96)
if (package%status .ne. EXT_HANDOFF_AUDITED) call fail(97)
if (package%audit%status .ne. EXT_AUDIT_PASS) call fail(98)
call ext_handoff_require_audited(package, ierr)
call require_success(ierr, 99)
call ext_handoff_summary(package, status, ready, nrow, ncol, nnz, &
residual, relative, backward, ierr)
call require_success(ierr, 100)
if (status .ne. EXT_HANDOFF_AUDITED .or. ready .ne. 1) call fail(101)
if (residual .gt. TOL .or. relative .gt. TOL) call fail(102)
if (backward .gt. TOL) call fail(103)
call csr_free(a, ierr)
call require_success(ierr, 104)
call cleanup_files()
end subroutine check_handoff_import_audit
subroutine check_bad_solution_audit()
type(csr_matrix) :: a
type(ext_handoff_package) :: package
real(rk) :: rhs(3)
real(rk) :: x0(3)
real(rk) :: imported(3)
integer :: ierr
call cleanup_files()
call build_spd(a, ierr)
call require_success(ierr, 120)
rhs = (/ ZERO, ZERO, 4.0_rk /)
x0 = ZERO
call ext_handoff_create('ext_handoff_case', 'ref_file', 40, a, rhs, x0, &
TOL, 25, EXT_IO_INDEX_ZERO, EXT_IO_STORAGE_GENERAL, package, ierr)
call require_success(ierr, 121)
call ext_io_write_vector(trim(package%manifest%solution_file), x0, &
'solution', ierr)
call require_success(ierr, 122)
call ext_handoff_import_solution(package, a, rhs, imported, ierr)
call require_success(ierr, 123)
if (package%audit%status .ne. EXT_AUDIT_FAIL) call fail(124)
call ext_handoff_require_audited(package, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(125)
call csr_free(a, ierr)
call require_success(ierr, 126)
call cleanup_files()
end subroutine check_bad_solution_audit
subroutine check_symmetric_storage()
type(csr_matrix) :: a
integer :: ierr
call cleanup_files()
call build_spd(a, ierr)
call require_success(ierr, 140)
call ext_io_write_matrix_market('ext_handoff_sym_matrix.mtx', a, &
EXT_IO_STORAGE_SYMMETRIC_LOWER, ierr)
call require_success(ierr, 141)
call require_file_lines('ext_handoff_sym_matrix.mtx', 8, 142)
call ext_io_write_csr_arrays('ext_handoff_sym_csr.txt', a, &
EXT_IO_INDEX_ONE, ierr)
call require_success(ierr, 143)
call require_file_lines('ext_handoff_sym_csr.txt', 23, 144)
call csr_free(a, ierr)
call require_success(ierr, 145)
call cleanup_files()
end subroutine check_symmetric_storage
subroutine check_error_paths()
type(csr_matrix) :: a
type(ext_handoff_package) :: package
real(rk) :: rhs(3)
real(rk) :: x0(3)
real(rk) :: too_short(2)
character(len=8) :: tiny
integer :: ierr
call build_spd(a, ierr)
call require_success(ierr, 160)
rhs = (/ ONE, ZERO, ONE /)
x0 = ZERO
call ext_handoff_create('', 'ref_file', 40, a, rhs, x0, TOL, 25, &
EXT_IO_INDEX_ZERO, EXT_IO_STORAGE_GENERAL, package, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(161)
call ext_handoff_create('ext_handoff_case', 'ref_file', 40, a, rhs, &
x0, ZERO, 25, EXT_IO_INDEX_ZERO, EXT_IO_STORAGE_GENERAL, package, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(162)
call ext_handoff_create('ext_handoff_case', 'ref_file', 40, a, rhs, &
too_short, TOL, 25, EXT_IO_INDEX_ZERO, EXT_IO_STORAGE_GENERAL, &
package, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(163)
call ext_io_write_matrix_market('', a, EXT_IO_STORAGE_GENERAL, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(164)
call ext_io_write_csr_arrays('ext_handoff_bad_csr.txt', a, 4, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(165)
call ext_io_storage_name(99, tiny, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(166)
call ext_handoff_status_name(99, tiny, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(167)
call ext_handoff_require_exported(package, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(168)
call ext_io_write_vector('ext_handoff_short.vec', rhs, 'rhs', ierr)
call require_success(ierr, 169)
call ext_io_read_vector('ext_handoff_short.vec', too_short, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(170)
call ext_io_read_manifest('missing_ext_manifest.txt', package%manifest, &
ierr)
if (ierr .eq. FEM_SUCCESS) call fail(172)
call csr_free(a, ierr)
call require_success(ierr, 173)
call cleanup_files()
end subroutine check_error_paths
subroutine build_spd(a, ierr)
type(csr_matrix), intent(out) :: a
integer, intent(out) :: ierr
integer :: row_ptr(4)
integer :: col_ind(7)
call csr_init(a)
row_ptr = (/ 1, 3, 6, 8 /)
col_ind = (/ 1, 2, 1, 2, 3, 2, 3 /)
call csr_create_pattern(a, 3, 3, row_ptr, col_ind, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_set_value(a, 1, 1, 2.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_set_value(a, 1, 2, -ONE, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_set_value(a, 2, 1, -ONE, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_set_value(a, 2, 2, 2.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_set_value(a, 2, 3, -ONE, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_set_value(a, 3, 2, -ONE, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_set_value(a, 3, 3, 2.0_rk, ierr)
end subroutine build_spd
subroutine require_file_lines(filename, expected, code)
character(len=*), intent(in) :: filename
integer, intent(in) :: expected
integer, intent(in) :: code
character(len=256) :: line
integer :: unit
integer :: ios
integer :: count
unit = 86
open(unit=unit, file=trim(filename), status='old', action='read', &
iostat=ios)
if (ios .ne. 0) call fail(code)
count = 0
do
read(unit, '(A)', iostat=ios) line
if (ios .lt. 0) exit
if (ios .gt. 0) call fail(code + 1000)
count = count + 1
end do
close(unit)
if (count .ne. expected) call fail(code + 2000)
end subroutine require_file_lines
subroutine check_close(actual, expected, code)
real(rk), intent(in) :: actual
real(rk), intent(in) :: expected
integer, intent(in) :: code
if (abs(actual - expected) .gt. 1.0e-8_rk) call fail(code)
end subroutine check_close
subroutine require_success(ierr, code)
integer, intent(in) :: ierr
integer, intent(in) :: code
if (ierr .ne. FEM_SUCCESS) call fail(code)
end subroutine require_success
subroutine cleanup_files()
call delete_file('ext_handoff_unit_matrix.mtx')
call delete_file('ext_handoff_unit_rhs.vec')
call delete_file('ext_handoff_unit_initial.vec')
call delete_file('ext_handoff_unit_solution.vec')
call delete_file('ext_handoff_unit_csr.txt')
call delete_file('ext_handoff_unit_manifest.txt')
call delete_file('ext_handoff_io_matrix.mtx')
call delete_file('ext_handoff_io_csr.txt')
call delete_file('ext_handoff_io_rhs.vec')
call delete_file('ext_handoff_io_manifest.txt')
call delete_file('ext_handoff_case_matrix.mtx')
call delete_file('ext_handoff_case_rhs.vec')
call delete_file('ext_handoff_case_initial.vec')
call delete_file('ext_handoff_case_solution.vec')
call delete_file('ext_handoff_case_csr.txt')
call delete_file('ext_handoff_case_manifest.txt')
call delete_file('ext_handoff_sym_matrix.mtx')
call delete_file('ext_handoff_sym_csr.txt')
call delete_file('ext_handoff_bad_csr.txt')
call delete_file('ext_handoff_short.vec')
end subroutine cleanup_files
subroutine delete_file(filename)
character(len=*), intent(in) :: filename
integer :: unit
integer :: ios
unit = 87
open(unit=unit, file=trim(filename), status='old', iostat=ios)
if (ios .eq. 0) close(unit, status='delete')
end subroutine delete_file
subroutine fail(code)
integer, intent(in) :: code
call cleanup_files()
write(*,*) 'test_external_handoff failed:', code
stop 1
end subroutine fail
end program test_external_handoff