Add conjugate gradient linear solver

This commit is contained in:
shenw
2026-06-22 21:00:21 +08:00
parent 0ffd3e12b8
commit 5375112eaf
3 changed files with 256 additions and 0 deletions
+13
View File
@@ -47,6 +47,7 @@ add_library(femlite_core STATIC
src/element/fem_element_mass.f90
src/element/fem_element_internal.f90
src/solver/fem_sparse_csr.f90
src/solver/fem_linear_solver_cg.f90
src/coupling/fem_coupling.f90
src/boundary/fem_boundary.f90
src/output/fem_output_csv.f90
@@ -119,6 +120,13 @@ add_executable(test_sparse_csr
target_link_libraries(test_sparse_csr PRIVATE femlite_core)
femlite_apply_fortran_options(test_sparse_csr)
add_executable(test_linear_solver_cg
tests/module/test_linear_solver_cg.f90
)
target_link_libraries(test_linear_solver_cg PRIVATE femlite_core)
femlite_apply_fortran_options(test_linear_solver_cg)
add_executable(perf_p1
tests/performance/perf_p1.f90
)
@@ -160,3 +168,8 @@ add_test(NAME sparse_csr COMMAND test_sparse_csr)
set_tests_properties(sparse_csr PROPERTIES
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
)
add_test(NAME linear_solver_cg COMMAND test_linear_solver_cg)
set_tests_properties(linear_solver_cg PROPERTIES
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
)
+115
View File
@@ -0,0 +1,115 @@
module fem_linear_solver_cg
use fem_precision
use fem_error
use fem_sparse_csr
implicit none
private
public :: linear_cg_solve
contains
subroutine linear_cg_solve(a, b, x, max_iter, tol, iter, res_norm, ierr)
type(csr_matrix), intent(in) :: a
real(rk), intent(in) :: b(:)
real(rk), intent(inout) :: x(:)
integer, intent(in) :: max_iter
real(rk), intent(in) :: tol
integer, intent(out) :: iter
real(rk), intent(out) :: res_norm
integer, intent(out) :: ierr
real(rk), allocatable :: r(:)
real(rk), allocatable :: p(:)
real(rk), allocatable :: ap(:)
real(rk) :: alpha
real(rk) :: beta
real(rk) :: denom
real(rk) :: norm_b
real(rk) :: rsold
real(rk) :: rsnew
real(rk) :: threshold
integer :: n
integer :: stat
iter = 0
res_norm = ZERO
if (a%nrow .le. 0 .or. a%ncol .le. 0) then
call error_set(FEM_ERR_SOLVER, 'CG solver received an empty matrix', ierr)
return
end if
if (a%nrow .ne. a%ncol) then
call error_set(FEM_ERR_SOLVER, 'CG solver requires a square matrix', ierr)
return
end if
if (size(b) .ne. a%nrow .or. size(x) .ne. a%ncol) then
call error_set(FEM_ERR_SOLVER, 'CG solver vector size mismatch', ierr)
return
end if
if (max_iter .le. 0) then
call error_set(FEM_ERR_SOLVER, 'CG solver max_iter must be positive', ierr)
return
end if
if (tol .le. ZERO) then
call error_set(FEM_ERR_SOLVER, 'CG solver tolerance must be positive', ierr)
return
end if
n = a%nrow
allocate(r(n), p(n), ap(n), stat=stat)
if (stat .ne. 0) then
call error_set(FEM_ERR_SOLVER, 'failed to allocate CG workspace', ierr)
return
end if
call csr_matvec(a, x, ap, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
r = b - ap
p = r
norm_b = sqrt(dot_product(b, b))
threshold = tol * max(ONE, norm_b)
rsold = dot_product(r, r)
res_norm = sqrt(rsold)
if (res_norm .le. threshold) then
ierr = FEM_SUCCESS
go to 900
end if
do iter = 1, max_iter
call csr_matvec(a, p, ap, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
denom = dot_product(p, ap)
if (denom .le. tiny(ONE)) then
call error_set(FEM_ERR_SOLVER, 'CG solver detected non-positive curvature', ierr)
go to 900
end if
alpha = rsold / denom
x = x + alpha * p
r = r - alpha * ap
rsnew = dot_product(r, r)
res_norm = sqrt(rsnew)
if (res_norm .le. threshold) then
ierr = FEM_SUCCESS
go to 900
end if
beta = rsnew / rsold
p = r + beta * p
rsold = rsnew
end do
iter = max_iter
call error_set(FEM_ERR_SOLVER, 'CG solver did not converge', ierr)
900 continue
if (allocated(r)) deallocate(r)
if (allocated(p)) deallocate(p)
if (allocated(ap)) deallocate(ap)
end subroutine linear_cg_solve
end module fem_linear_solver_cg
+128
View File
@@ -0,0 +1,128 @@
program test_linear_solver_cg
use fem_precision
use fem_error
use fem_sparse_csr
use fem_linear_solver_cg
implicit none
real(rk), parameter :: TOL = 1.0e-9_rk
call check_spd_solve()
call check_zero_rhs()
call check_nonconvergence()
contains
subroutine check_spd_solve()
type(csr_matrix) :: a
integer :: ierr
integer :: iter
real(rk) :: b(3)
real(rk) :: x(3)
real(rk) :: res_norm
call make_tridiagonal(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(1)
b = (/ ZERO, ZERO, 4.0_rk /)
x = ZERO
call linear_cg_solve(a, b, x, 20, 1.0e-12_rk, iter, res_norm, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(2)
if (iter .le. 0 .or. iter .gt. 3) call fail(3)
if (res_norm .gt. 1.0e-10_rk) call fail(4)
call check_close(x(1), ONE, 5)
call check_close(x(2), 2.0_rk, 6)
call check_close(x(3), 3.0_rk, 7)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(8)
end subroutine check_spd_solve
subroutine check_zero_rhs()
type(csr_matrix) :: a
integer :: ierr
integer :: iter
real(rk) :: b(3)
real(rk) :: x(3)
real(rk) :: res_norm
call make_tridiagonal(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(20)
b = ZERO
x = ZERO
call linear_cg_solve(a, b, x, 20, 1.0e-12_rk, iter, res_norm, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(21)
if (iter .ne. 0) call fail(22)
if (res_norm .gt. ZERO) call fail(23)
if (maxval(abs(x)) .gt. ZERO) call fail(24)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(25)
end subroutine check_zero_rhs
subroutine check_nonconvergence()
type(csr_matrix) :: a
integer :: ierr
integer :: iter
real(rk) :: b(3)
real(rk) :: x(3)
real(rk) :: res_norm
call make_tridiagonal(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(30)
b = (/ ZERO, ZERO, 4.0_rk /)
x = ZERO
call linear_cg_solve(a, b, x, 1, 1.0e-12_rk, iter, res_norm, ierr)
if (ierr .eq. FEM_SUCCESS) call fail(31)
if (iter .ne. 1) call fail(32)
if (res_norm .le. ZERO) call fail(33)
call csr_free(a, ierr)
if (ierr .ne. FEM_SUCCESS) call fail(34)
end subroutine check_nonconvergence
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 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 make_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_linear_solver_cg failed:', code
stop 1
end subroutine fail
end program test_linear_solver_cg