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

326 lines
11 KiB
Fortran

program test_csr_diagnostics
use fem_precision
use fem_error
use fem_sparse_csr
use fem_sparse_csr_diagnostics
implicit none
real(rk), parameter :: TOL = 1.0e-10_rk
call check_spd_profile()
call check_structural_symmetric()
call check_general_profile()
call check_diag_profile()
call check_export_and_residual()
call check_error_paths()
contains
subroutine check_spd_profile()
type(csr_matrix) :: a
type(csr_profile) :: profile
integer :: ierr
integer :: class_id
call make_tridiagonal(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(1)
call csr_analyze(a, profile, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(2)
if (profile%nrow .ne. 3 .or. profile%ncol .ne. 3) call fail(3)
if (profile%nnz .ne. 7) call fail(4)
if (profile%square .ne. 1) call fail(5)
if (profile%symmetric_pattern .ne. 1) call fail(6)
if (profile%symmetric_values .ne. 1) call fail(7)
if (profile%diagonal_complete .ne. 1) call fail(8)
if (profile%class_id .ne. CSR_CLASS_SPD_CAND) call fail(9)
call check_close(profile%one_norm, 4.0_rk, 10)
call check_close(profile%inf_norm, 4.0_rk, 11)
call check_close(profile%frob_norm, sqrt(16.0_rk), 12)
call check_close(profile%asym_norm, ZERO, 13)
call check_close(profile%density, 7.0_rk / 9.0_rk, 14)
if (profile%min_row_nnz .ne. 2) call fail(15)
if (profile%max_row_nnz .ne. 3) call fail(16)
call check_close(profile%avg_row_nnz, 7.0_rk / 3.0_rk, 17)
call csr_profile_class(profile, class_id, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(18)
if (class_id .ne. CSR_CLASS_SPD_CAND) call fail(19)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(20)
end subroutine check_spd_profile
subroutine check_structural_symmetric()
type(csr_matrix) :: a
type(csr_profile) :: profile
integer :: ierr
integer :: nrow
integer :: ncol
integer :: nnz
integer :: class_id
integer :: sym_pat
integer :: sym_val
integer :: missing_diag
integer :: zero_diag
real(rk) :: one_norm
real(rk) :: inf_norm
real(rk) :: asym_norm
real(rk) :: density
call make_structural_symmetric(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(30)
call csr_analyze(a, profile, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(31)
if (profile%symmetric_pattern .ne. 1) call fail(32)
if (profile%symmetric_values .ne. 0) call fail(33)
if (profile%class_id .ne. CSR_CLASS_STRUCT_SYM) call fail(34)
call check_close(profile%asym_norm, ONE, 35)
call check_close(profile%one_norm, 7.0_rk, 36)
call check_close(profile%inf_norm, 7.0_rk, 37)
call csr_profile_summary(profile, nrow, ncol, nnz, class_id, sym_pat, &
sym_val, missing_diag, zero_diag, one_norm, &
inf_norm, asym_norm, density, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(38)
if (nrow .ne. 3 .or. ncol .ne. 3 .or. nnz .ne. 7) call fail(39)
if (class_id .ne. CSR_CLASS_STRUCT_SYM) call fail(40)
if (sym_pat .ne. 1 .or. sym_val .ne. 0) call fail(41)
if (missing_diag .ne. 0 .or. zero_diag .ne. 0) call fail(42)
call check_close(one_norm, profile%one_norm, 43)
call check_close(inf_norm, profile%inf_norm, 44)
call check_close(asym_norm, profile%asym_norm, 45)
call check_close(density, profile%density, 46)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(47)
end subroutine check_structural_symmetric
subroutine check_general_profile()
type(csr_matrix) :: a
type(csr_profile) :: profile
integer :: ierr
integer :: row_ptr(4)
integer :: col_ind(5)
call csr_init(a)
row_ptr = (/ 1, 3, 5, 6 /)
col_ind = (/ 1, 3, 1, 2, 3 /)
call csr_create_pattern(a, 3, 3, row_ptr, col_ind, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(60)
call csr_add_value(a, 1, 1, 2.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(61)
call csr_add_value(a, 1, 3, -3.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(62)
call csr_add_value(a, 2, 1, 4.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(63)
call csr_add_value(a, 2, 2, 5.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(64)
call csr_add_value(a, 3, 3, 6.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(65)
call csr_analyze(a, profile, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(66)
if (profile%symmetric_pattern .ne. 0) call fail(67)
if (profile%symmetric_values .ne. 0) call fail(68)
if (profile%class_id .ne. CSR_CLASS_GENERAL) call fail(69)
if (profile%asym_norm .lt. 3.0_rk - TOL) call fail(70)
call check_close(profile%one_norm, 9.0_rk, 71)
call check_close(profile%inf_norm, 9.0_rk, 72)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(73)
end subroutine check_general_profile
subroutine check_diag_profile()
type(csr_matrix) :: a
type(csr_profile) :: profile
integer :: ierr
integer :: row_ptr(4)
integer :: col_ind(4)
call csr_init(a)
row_ptr = (/ 1, 2, 4, 5 /)
col_ind = (/ 2, 1, 2, 3 /)
call csr_create_pattern(a, 3, 3, row_ptr, col_ind, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(90)
call csr_add_value(a, 1, 2, ONE, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(91)
call csr_add_value(a, 2, 1, ONE, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(92)
call csr_add_value(a, 2, 2, ZERO, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(93)
call csr_add_value(a, 3, 3, -2.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(94)
call csr_analyze(a, profile, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(95)
if (profile%missing_diag_count .ne. 1) call fail(96)
if (profile%zero_diag_count .ne. 1) call fail(97)
if (profile%negative_diag_count .ne. 1) call fail(98)
if (profile%positive_diag_count .ne. 0) call fail(99)
if (profile%diagonal_complete .ne. 0) call fail(100)
if (profile%class_id .ne. CSR_CLASS_SYMMETRIC) call fail(101)
call check_close(profile%min_diag, -2.0_rk, 102)
call check_close(profile%max_diag, ZERO, 103)
call check_close(profile%min_abs_diag, ZERO, 104)
call check_close(profile%max_abs_diag, 2.0_rk, 105)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(106)
end subroutine check_diag_profile
subroutine check_export_and_residual()
type(csr_matrix) :: a
type(csr_profile) :: profile
integer :: ierr
integer :: row_ptr0(4)
integer :: col_ind0(7)
real(rk) :: val_out(7)
real(rk) :: x(3)
real(rk) :: b(3)
real(rk) :: abs_norm
real(rk) :: rel_norm
call make_tridiagonal(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(120)
call csr_export_checked(a, 0, 1, row_ptr0, col_ind0, val_out, profile, &
ierr)
if (ierr .ne. FEM_SUCCESS) call fail(121)
if (any(row_ptr0 .ne. (/ 0, 2, 5, 7 /))) call fail(122)
if (any(col_ind0 .ne. (/ 0, 1, 0, 1, 2, 1, 2 /))) call fail(123)
call check_close(val_out(1), 2.0_rk, 124)
if (profile%class_id .ne. CSR_CLASS_SPD_CAND) call fail(125)
x = (/ ONE, 2.0_rk, 3.0_rk /)
b = (/ ZERO, ZERO, 4.0_rk /)
call csr_residual_norm(a, b, x, abs_norm, rel_norm, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(126)
call check_close(abs_norm, ZERO, 127)
call check_close(rel_norm, ZERO, 128)
x = ZERO
call csr_residual_norm(a, b, x, abs_norm, rel_norm, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(129)
call check_close(abs_norm, 4.0_rk, 130)
call check_close(rel_norm, ONE, 131)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(132)
end subroutine check_export_and_residual
subroutine check_error_paths()
type(csr_matrix) :: a
type(csr_profile) :: profile
integer :: ierr
integer :: row_ptr(3)
integer :: col_ind(2)
real(rk) :: val_out(2)
real(rk) :: abs_norm
real(rk) :: rel_norm
call csr_init(a)
call csr_analyze(a, profile, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(150)
row_ptr = (/ 1, 2, 3 /)
col_ind = (/ 1, 2 /)
call csr_create_pattern(a, 2, 2, row_ptr, col_ind, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(151)
call csr_set_value(a, 1, 1, ONE, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(152)
call csr_set_value(a, 2, 2, ONE, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(153)
call csr_export_checked(a, 2, 1, row_ptr, col_ind, val_out, profile, &
ierr)
if (ierr .eq. FEM_SUCCESS) call fail(154)
call csr_residual_norm(a, (/ ONE /), (/ ONE, ONE /), abs_norm, rel_norm, &
ierr)
if (ierr .eq. FEM_SUCCESS) call fail(155)
call csr_profile_reset(profile)
profile%one_norm = -ONE
call csr_validate_profile(profile, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(156)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(157)
end subroutine check_error_paths
subroutine make_tridiagonal(a, ierr)
type(csr_matrix), intent(inout) :: 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 fill_tridiagonal(a, ierr)
end subroutine make_tridiagonal
subroutine make_structural_symmetric(a, ierr)
type(csr_matrix), intent(inout) :: 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_add_value(a, 1, 1, 2.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_add_value(a, 1, 2, ONE, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_add_value(a, 2, 1, 2.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_add_value(a, 2, 2, 2.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_add_value(a, 2, 3, 3.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_add_value(a, 3, 2, 4.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_add_value(a, 3, 3, 2.0_rk, ierr)
end subroutine make_structural_symmetric
subroutine fill_tridiagonal(a, ierr)
type(csr_matrix), intent(inout) :: a
integer, intent(out) :: ierr
call csr_add_value(a, 1, 1, 2.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_add_value(a, 1, 2, -ONE, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_add_value(a, 2, 1, -ONE, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_add_value(a, 2, 2, 2.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_add_value(a, 2, 3, -ONE, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_add_value(a, 3, 2, -ONE, ierr)
if (ierr .ne. FEM_SUCCESS) return
call csr_add_value(a, 3, 3, 2.0_rk, ierr)
end subroutine fill_tridiagonal
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. TOL * max(ONE, abs(expected))) then
call fail(code)
end if
end subroutine check_close
subroutine fail(code)
integer, intent(in) :: code
write(*,*) 'test_csr_diagnostics failed:', code
stop 1
end subroutine fail
end program test_csr_diagnostics