Complete V4.8 hardening and examples coverage
FemLite CI / build-test (push) Has been cancelled

This commit is contained in:
shenw
2026-06-25 11:27:59 +08:00
parent 240d2728fa
commit 8f9c1ece61
109 changed files with 2122 additions and 49 deletions
+167 -1
View File
@@ -13,6 +13,7 @@ module fem_linear_solver_cg
public :: linear_solver_get_stats
public :: linear_cg_solve
public :: linear_bicgstab_solve
public :: linear_gmres_solve
contains
@@ -40,6 +41,9 @@ contains
case (LSOLVER_BICGSTAB)
call linear_bicgstab_solve(a, b, x, max_iter, tol, iter, res_norm, &
ierr, preconditioner)
case (LSOLVER_GMRES)
call linear_gmres_solve(a, b, x, max_iter, tol, iter, res_norm, &
ierr, preconditioner)
case (LSOLVER_EXTERNAL)
iter = 0
res_norm = ZERO
@@ -61,7 +65,7 @@ contains
integer, intent(out) :: ierr
if (method .ne. LSOLVER_CG .and. method .ne. LSOLVER_BICGSTAB .and. &
method .ne. LSOLVER_EXTERNAL) then
method .ne. LSOLVER_GMRES .and. method .ne. LSOLVER_EXTERNAL) then
call error_set(FEM_ERR_SOLVER, 'invalid linear solver method', ierr)
return
end if
@@ -358,6 +362,111 @@ contains
if (allocated(diag_inv)) deallocate(diag_inv)
end subroutine linear_bicgstab_solve
subroutine linear_gmres_solve(a, b, x, max_iter, tol, iter, res_norm, &
ierr, preconditioner)
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
integer, intent(in), optional :: preconditioner
real(rk), allocatable :: diag_inv(:), r(:), w(:), ax(:), dx(:), xt(:)
real(rk), allocatable :: v(:,:), h(:,:), g(:), y(:)
real(rk) :: beta, hnorm, norm_b, threshold
integer :: n, m, k, j, stat, prec
iter = 0
res_norm = ZERO
prec = LPRECOND_JACOBI
if (present(preconditioner)) prec = preconditioner
call validate_linear_system(a, b, x, max_iter, tol, 'GMRES', ierr)
if (ierr .ne. FEM_SUCCESS) return
n = a%nrow
m = min(max_iter, n)
allocate(diag_inv(n), r(n), w(n), ax(n), dx(n), xt(n), v(n,m+1), &
h(m+1,m), g(m+1), y(m), stat=stat)
if (stat .ne. 0) then
call error_set(FEM_ERR_SOLVER, 'failed to allocate GMRES workspace', ierr)
return
end if
call build_preconditioner(a, prec, diag_inv, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
call csr_matvec(a, x, ax, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
r = b - ax
norm_b = sqrt(dot_product(b, b))
threshold = tol * max(ONE, norm_b)
res_norm = sqrt(dot_product(r, r))
if (res_norm .le. threshold) then
ierr = FEM_SUCCESS
go to 900
end if
call apply_jacobi_inverse(diag_inv, r, w)
beta = sqrt(dot_product(w, w))
if (beta .le. tiny(ONE)) then
call error_set(FEM_ERR_SOLVER, 'GMRES preconditioned residual is zero', ierr)
go to 900
end if
v = ZERO
h = ZERO
g = ZERO
v(:,1) = w / beta
g(1) = beta
dx = ZERO
do k = 1, m
call csr_matvec(a, v(:,k), ax, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
call apply_jacobi_inverse(diag_inv, ax, w)
do j = 1, k
h(j,k) = dot_product(w, v(:,j))
w = w - h(j,k) * v(:,j)
end do
hnorm = sqrt(dot_product(w, w))
h(k+1,k) = hnorm
if (hnorm .gt. tiny(ONE) .and. k .lt. m) v(:,k+1) = w / hnorm
call gmres_least_squares(h, g, k, y, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
dx = ZERO
do j = 1, k
dx = dx + y(j) * v(:,j)
end do
xt = x + dx
call csr_matvec(a, xt, ax, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
r = b - ax
res_norm = sqrt(dot_product(r, r))
iter = k
if (res_norm .le. threshold) then
x = xt
ierr = FEM_SUCCESS
go to 900
end if
end do
x = x + dx
iter = m
call error_set(FEM_ERR_SOLVER, 'GMRES solver did not converge', ierr)
900 continue
if (allocated(diag_inv)) deallocate(diag_inv)
if (allocated(r)) deallocate(r)
if (allocated(w)) deallocate(w)
if (allocated(ax)) deallocate(ax)
if (allocated(dx)) deallocate(dx)
if (allocated(xt)) deallocate(xt)
if (allocated(v)) deallocate(v)
if (allocated(h)) deallocate(h)
if (allocated(g)) deallocate(g)
if (allocated(y)) deallocate(y)
end subroutine linear_gmres_solve
subroutine validate_linear_system(a, b, x, max_iter, tol, solver_name, ierr)
type(csr_matrix), intent(in) :: a
real(rk), intent(in) :: b(:)
@@ -452,4 +561,61 @@ contains
end do
end subroutine apply_jacobi_inverse
subroutine gmres_least_squares(h, g, k, y, ierr)
real(rk), intent(in) :: h(:,:)
real(rk), intent(in) :: g(:)
integer, intent(in) :: k
real(rk), intent(inout) :: y(:)
integer, intent(out) :: ierr
real(rk), allocatable :: ata(:,:), rhs(:)
real(rk) :: pivot, factor
integer :: i, j, row, stat
y = ZERO
if (k .le. 0 .or. size(y) .lt. k) then
call error_set(FEM_ERR_SOLVER, 'GMRES least-squares size mismatch', ierr)
return
end if
allocate(ata(k,k), rhs(k), stat=stat)
if (stat .ne. 0) then
call error_set(FEM_ERR_SOLVER, 'failed to allocate GMRES least-squares', ierr)
return
end if
ata = ZERO
rhs = ZERO
do i = 1, k
do row = 1, k + 1
rhs(i) = rhs(i) + h(row,i) * g(row)
do j = 1, k
ata(i,j) = ata(i,j) + h(row,i) * h(row,j)
end do
end do
end do
do i = 1, k
pivot = ata(i,i)
if (abs(pivot) .le. tiny(ONE)) then
call error_set(FEM_ERR_SOLVER, 'GMRES least-squares singular system', ierr)
go to 900
end if
do row = i + 1, k
factor = ata(row,i) / pivot
ata(row,i:k) = ata(row,i:k) - factor * ata(i,i:k)
rhs(row) = rhs(row) - factor * rhs(i)
end do
end do
do i = k, 1, -1
y(i) = rhs(i)
if (i .lt. k) y(i) = y(i) - dot_product(ata(i,i+1:k), y(i+1:k))
y(i) = y(i) / ata(i,i)
end do
ierr = FEM_SUCCESS
900 continue
if (allocated(ata)) deallocate(ata)
if (allocated(rhs)) deallocate(rhs)
end subroutine gmres_least_squares
end module fem_linear_solver_cg