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

201 lines
5.8 KiB
Fortran

program test_sparse_csr
use fem_precision
use fem_error
use fem_sparse_csr
implicit none
real(rk), parameter :: TOL = 1.0e-10_rk
call check_basic_operations()
call check_dirichlet()
call check_export()
call check_invalid_pattern()
contains
subroutine check_basic_operations()
type(csr_matrix) :: a
integer :: ierr
integer :: row_ptr(4)
integer :: col_ind(7)
real(rk) :: x(3)
real(rk) :: y(3)
real(rk) :: value
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) call fail(1)
call fill_tridiagonal(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(2)
x = (/ ONE, 2.0_rk, 4.0_rk /)
call csr_matvec(a, x, y, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(3)
call check_close(y(1), ZERO, 4)
call check_close(y(2), -ONE, 5)
call check_close(y(3), 6.0_rk, 6)
call csr_get_value(a, 1, 3, value, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(7)
call check_close(value, ZERO, 8)
call csr_add_value(a, 1, 3, ONE, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(9)
call csr_zero(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(10)
call csr_get_value(a, 2, 2, value, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(11)
call check_close(value, ZERO, 12)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(13)
end subroutine check_basic_operations
subroutine check_dirichlet()
type(csr_matrix) :: a
integer :: ierr
integer :: row_ptr(4)
integer :: col_ind(7)
real(rk) :: rhs(3)
real(rk) :: value
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) call fail(20)
call fill_tridiagonal(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(21)
rhs = ZERO
call csr_apply_dirichlet(a, rhs, 2, 5.0_rk, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(22)
call check_close(rhs(1), 5.0_rk, 23)
call check_close(rhs(2), 5.0_rk, 24)
call check_close(rhs(3), 5.0_rk, 25)
call csr_get_value(a, 1, 1, value, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(26)
call check_close(value, 2.0_rk, 27)
call csr_get_value(a, 1, 2, value, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(28)
call check_close(value, ZERO, 29)
call csr_get_value(a, 2, 2, value, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(30)
call check_close(value, ONE, 31)
call csr_get_value(a, 2, 1, value, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(32)
call check_close(value, ZERO, 33)
call csr_get_value(a, 3, 2, value, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(34)
call check_close(value, ZERO, 35)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(36)
end subroutine check_dirichlet
subroutine check_export()
type(csr_matrix) :: a
integer :: ierr
integer :: nrow
integer :: ncol
integer :: nnz
integer :: row_ptr(4)
integer :: col_ind(7)
integer :: row_ptr0(4)
integer :: col_ind0(7)
integer :: row_ptr1(4)
integer :: col_ind1(7)
real(rk) :: val_out(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) call fail(60)
call fill_tridiagonal(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(61)
call csr_get_shape(a, nrow, ncol, nnz, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(62)
if (nrow .ne. 3 .or. ncol .ne. 3 .or. nnz .ne. 7) call fail(63)
call csr_export(a, 1, row_ptr1, col_ind1, val_out, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(64)
if (any(row_ptr1 .ne. row_ptr)) call fail(65)
if (any(col_ind1 .ne. col_ind)) call fail(66)
call check_close(val_out(1), 2.0_rk, 67)
call check_close(val_out(4), 2.0_rk, 68)
call csr_export(a, 0, row_ptr0, col_ind0, val_out, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(69)
if (any(row_ptr0 .ne. row_ptr - 1)) call fail(70)
if (any(col_ind0 .ne. col_ind - 1)) call fail(71)
call csr_export(a, 2, row_ptr1, col_ind1, val_out, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(72)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(73)
end subroutine check_export
subroutine check_invalid_pattern()
type(csr_matrix) :: a
integer :: ierr
integer :: row_ptr(3)
integer :: col_ind(2)
call csr_init(a)
row_ptr = (/ 1, 3, 3 /)
col_ind = (/ 2, 1 /)
call csr_create_pattern(a, 2, 2, row_ptr, col_ind, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(40)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(41)
end subroutine check_invalid_pattern
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) call fail(code)
end subroutine check_close
subroutine fail(code)
integer, intent(in) :: code
write(*,*) 'test_sparse_csr failed:', code
stop 1
end subroutine fail
end program test_sparse_csr