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_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_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