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