From 0ffd3e12b82dab0ee1bf5ef98cd4d1210f760a4c Mon Sep 17 00:00:00 2001 From: shenw Date: Mon, 22 Jun 2026 20:57:08 +0800 Subject: [PATCH] Add CSR sparse matrix infrastructure --- CMakeLists.txt | 13 ++ src/solver/fem_sparse_csr.f90 | 310 +++++++++++++++++++++++++++++++ tests/module/test_sparse_csr.f90 | 154 +++++++++++++++ 3 files changed, 477 insertions(+) create mode 100644 src/solver/fem_sparse_csr.f90 create mode 100644 tests/module/test_sparse_csr.f90 diff --git a/CMakeLists.txt b/CMakeLists.txt index fac5dc6..23bcbf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,7 @@ add_library(femlite_core STATIC src/element/fem_shape_functions.f90 src/element/fem_element_mass.f90 src/element/fem_element_internal.f90 + src/solver/fem_sparse_csr.f90 src/coupling/fem_coupling.f90 src/boundary/fem_boundary.f90 src/output/fem_output_csv.f90 @@ -111,6 +112,13 @@ add_executable(test_element_state target_link_libraries(test_element_state PRIVATE femlite_core) femlite_apply_fortran_options(test_element_state) +add_executable(test_sparse_csr + tests/module/test_sparse_csr.f90 +) + +target_link_libraries(test_sparse_csr PRIVATE femlite_core) +femlite_apply_fortran_options(test_sparse_csr) + add_executable(perf_p1 tests/performance/perf_p1.f90 ) @@ -147,3 +155,8 @@ add_test(NAME element_state COMMAND test_element_state) set_tests_properties(element_state PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" ) + +add_test(NAME sparse_csr COMMAND test_sparse_csr) +set_tests_properties(sparse_csr PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" +) diff --git a/src/solver/fem_sparse_csr.f90 b/src/solver/fem_sparse_csr.f90 new file mode 100644 index 0000000..6b32575 --- /dev/null +++ b/src/solver/fem_sparse_csr.f90 @@ -0,0 +1,310 @@ +module fem_sparse_csr + use fem_precision + use fem_error + implicit none + + private + + type csr_matrix + integer :: nrow = 0 + integer :: ncol = 0 + integer :: nnz = 0 + integer, pointer :: row_ptr(:) => null() + integer, pointer :: col_ind(:) => null() + real(rk), pointer :: val(:) => null() + end type csr_matrix + + public :: csr_matrix + public :: csr_init + public :: csr_free + public :: csr_create_pattern + public :: csr_zero + public :: csr_add_value + public :: csr_set_value + public :: csr_get_value + public :: csr_matvec + public :: csr_apply_dirichlet + +contains + + subroutine csr_init(a) + type(csr_matrix), intent(inout) :: a + + a%nrow = 0 + a%ncol = 0 + a%nnz = 0 + nullify(a%row_ptr) + nullify(a%col_ind) + nullify(a%val) + end subroutine csr_init + + subroutine csr_free(a, ierr) + type(csr_matrix), intent(inout) :: a + integer, intent(out) :: ierr + + if (associated(a%row_ptr)) deallocate(a%row_ptr) + if (associated(a%col_ind)) deallocate(a%col_ind) + if (associated(a%val)) deallocate(a%val) + call csr_init(a) + ierr = FEM_SUCCESS + end subroutine csr_free + + subroutine csr_create_pattern(a, nrow, ncol, row_ptr_in, col_ind_in, ierr) + type(csr_matrix), intent(inout) :: a + integer, intent(in) :: nrow + integer, intent(in) :: ncol + integer, intent(in) :: row_ptr_in(:) + integer, intent(in) :: col_ind_in(:) + integer, intent(out) :: ierr + integer :: i + integer :: k + integer :: stat + integer :: nnz + + if (nrow .le. 0 .or. ncol .le. 0) then + call error_set(FEM_ERR_SOLVER, 'CSR matrix dimensions must be positive', ierr) + return + end if + if (size(row_ptr_in) .ne. nrow + 1) then + call error_set(FEM_ERR_SOLVER, 'CSR row_ptr size is inconsistent with nrow', ierr) + return + end if + if (row_ptr_in(1) .ne. 1) then + call error_set(FEM_ERR_SOLVER, 'CSR row_ptr must use 1-based indexing', ierr) + return + end if + do i = 1, nrow + if (row_ptr_in(i+1) .lt. row_ptr_in(i)) then + call error_set(FEM_ERR_SOLVER, 'CSR row_ptr must be nondecreasing', ierr) + return + end if + end do + + nnz = row_ptr_in(nrow+1) - 1 + if (nnz .ne. size(col_ind_in)) then + call error_set(FEM_ERR_SOLVER, 'CSR col_ind size is inconsistent with row_ptr', ierr) + return + end if + if (nnz .le. 0) then + call error_set(FEM_ERR_SOLVER, 'CSR matrix must contain at least one entry', ierr) + return + end if + + do i = 1, nrow + do k = row_ptr_in(i), row_ptr_in(i+1) - 1 + if (col_ind_in(k) .lt. 1 .or. col_ind_in(k) .gt. ncol) then + call error_set(FEM_ERR_SOLVER, 'CSR column index is out of range', ierr) + return + end if + if (k .gt. row_ptr_in(i)) then + if (col_ind_in(k) .le. col_ind_in(k-1)) then + call error_set(FEM_ERR_SOLVER, 'CSR column indices must be strictly increasing per row', ierr) + return + end if + end if + end do + end do + + call csr_free(a, ierr) + if (ierr .ne. FEM_SUCCESS) return + + allocate(a%row_ptr(nrow+1), a%col_ind(nnz), a%val(nnz), stat=stat) + if (stat .ne. 0) then + call error_set(FEM_ERR_SOLVER, 'failed to allocate CSR matrix', ierr) + return + end if + + a%nrow = nrow + a%ncol = ncol + a%nnz = nnz + a%row_ptr = row_ptr_in + a%col_ind = col_ind_in + a%val = ZERO + ierr = FEM_SUCCESS + end subroutine csr_create_pattern + + subroutine csr_zero(a, ierr) + type(csr_matrix), intent(inout) :: a + integer, intent(out) :: ierr + + if (.not. csr_is_allocated(a)) then + call error_set(FEM_ERR_SOLVER, 'CSR matrix is not allocated', ierr) + return + end if + a%val = ZERO + ierr = FEM_SUCCESS + end subroutine csr_zero + + subroutine csr_add_value(a, row, col, value, ierr) + type(csr_matrix), intent(inout) :: a + integer, intent(in) :: row + integer, intent(in) :: col + real(rk), intent(in) :: value + integer, intent(out) :: ierr + integer :: k + + call csr_find_position(a, row, col, k, ierr) + if (ierr .ne. FEM_SUCCESS) return + a%val(k) = a%val(k) + value + ierr = FEM_SUCCESS + end subroutine csr_add_value + + subroutine csr_set_value(a, row, col, value, ierr) + type(csr_matrix), intent(inout) :: a + integer, intent(in) :: row + integer, intent(in) :: col + real(rk), intent(in) :: value + integer, intent(out) :: ierr + integer :: k + + call csr_find_position(a, row, col, k, ierr) + if (ierr .ne. FEM_SUCCESS) return + a%val(k) = value + ierr = FEM_SUCCESS + end subroutine csr_set_value + + subroutine csr_get_value(a, row, col, value, ierr) + type(csr_matrix), intent(in) :: a + integer, intent(in) :: row + integer, intent(in) :: col + real(rk), intent(out) :: value + integer, intent(out) :: ierr + integer :: k + + if (.not. csr_is_allocated(a)) then + call error_set(FEM_ERR_SOLVER, 'CSR matrix is not allocated', ierr) + return + end if + if (row .lt. 1 .or. row .gt. a%nrow .or. col .lt. 1 .or. col .gt. a%ncol) then + call error_set(FEM_ERR_SOLVER, 'CSR query index is out of range', ierr) + return + end if + + value = ZERO + do k = a%row_ptr(row), a%row_ptr(row+1) - 1 + if (a%col_ind(k) .eq. col) then + value = a%val(k) + exit + end if + end do + ierr = FEM_SUCCESS + end subroutine csr_get_value + + subroutine csr_matvec(a, x, y, ierr) + type(csr_matrix), intent(in) :: a + real(rk), intent(in) :: x(:) + real(rk), intent(out) :: y(:) + integer, intent(out) :: ierr + integer :: i + integer :: k + + if (.not. csr_is_allocated(a)) then + call error_set(FEM_ERR_SOLVER, 'CSR matrix is not allocated', ierr) + return + end if + if (size(x) .ne. a%ncol .or. size(y) .ne. a%nrow) then + call error_set(FEM_ERR_SOLVER, 'CSR matvec vector size mismatch', ierr) + return + end if + + y = ZERO + do i = 1, a%nrow + do k = a%row_ptr(i), a%row_ptr(i+1) - 1 + y(i) = y(i) + a%val(k) * x(a%col_ind(k)) + end do + end do + ierr = FEM_SUCCESS + end subroutine csr_matvec + + subroutine csr_apply_dirichlet(a, rhs, dof, value, ierr) + type(csr_matrix), intent(inout) :: a + real(rk), intent(inout) :: rhs(:) + integer, intent(in) :: dof + real(rk), intent(in) :: value + integer, intent(out) :: ierr + integer :: i + integer :: k + integer :: diag_pos + + if (.not. csr_is_allocated(a)) then + call error_set(FEM_ERR_SOLVER, 'CSR matrix is not allocated', ierr) + return + end if + if (a%nrow .ne. a%ncol) then + call error_set(FEM_ERR_SOLVER, 'Dirichlet application requires a square CSR matrix', ierr) + return + end if + if (size(rhs) .ne. a%nrow) then + call error_set(FEM_ERR_SOLVER, 'Dirichlet RHS size mismatch', ierr) + return + end if + if (dof .lt. 1 .or. dof .gt. a%nrow) then + call error_set(FEM_ERR_SOLVER, 'Dirichlet DOF is out of range', ierr) + return + end if + + diag_pos = 0 + do k = a%row_ptr(dof), a%row_ptr(dof+1) - 1 + if (a%col_ind(k) .eq. dof) diag_pos = k + end do + if (diag_pos .eq. 0) then + call error_set(FEM_ERR_SOLVER, 'Dirichlet DOF has no diagonal entry in CSR pattern', ierr) + return + end if + + do i = 1, a%nrow + if (i .ne. dof) then + do k = a%row_ptr(i), a%row_ptr(i+1) - 1 + if (a%col_ind(k) .eq. dof) then + rhs(i) = rhs(i) - a%val(k) * value + a%val(k) = ZERO + end if + end do + end if + end do + + do k = a%row_ptr(dof), a%row_ptr(dof+1) - 1 + a%val(k) = ZERO + end do + a%val(diag_pos) = ONE + rhs(dof) = value + ierr = FEM_SUCCESS + end subroutine csr_apply_dirichlet + + logical function csr_is_allocated(a) + type(csr_matrix), intent(in) :: a + + csr_is_allocated = associated(a%row_ptr) .and. associated(a%col_ind) .and. & + associated(a%val) + end function csr_is_allocated + + subroutine csr_find_position(a, row, col, pos, ierr) + type(csr_matrix), intent(in) :: a + integer, intent(in) :: row + integer, intent(in) :: col + integer, intent(out) :: pos + integer, intent(out) :: ierr + integer :: k + + pos = 0 + if (.not. csr_is_allocated(a)) then + call error_set(FEM_ERR_SOLVER, 'CSR matrix is not allocated', ierr) + return + end if + if (row .lt. 1 .or. row .gt. a%nrow .or. col .lt. 1 .or. col .gt. a%ncol) then + call error_set(FEM_ERR_SOLVER, 'CSR insertion index is out of range', ierr) + return + end if + + do k = a%row_ptr(row), a%row_ptr(row+1) - 1 + if (a%col_ind(k) .eq. col) then + pos = k + ierr = FEM_SUCCESS + return + end if + end do + + call error_set(FEM_ERR_SOLVER, 'CSR entry is not present in matrix pattern', ierr) + end subroutine csr_find_position + +end module fem_sparse_csr diff --git a/tests/module/test_sparse_csr.f90 b/tests/module/test_sparse_csr.f90 new file mode 100644 index 0000000..7d33c5b --- /dev/null +++ b/tests/module/test_sparse_csr.f90 @@ -0,0 +1,154 @@ +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