Complete V4.6 linear solver interface
FemLite CI / build-test (push) Has been cancelled

This commit is contained in:
shenw
2026-06-25 09:49:03 +08:00
parent 62f1293a9f
commit c2db5f59c6
16 changed files with 745 additions and 15 deletions
+279 -3
View File
@@ -1,16 +1,22 @@
module fem_linear_solver_cg
use fem_precision
use fem_error
use fem_control
use fem_sparse_csr
implicit none
private
public :: linear_solver_solve
public :: linear_solver_configure
public :: linear_solver_get_settings
public :: linear_solver_get_stats
public :: linear_cg_solve
public :: linear_bicgstab_solve
contains
subroutine linear_cg_solve(a, b, x, max_iter, tol, iter, res_norm, ierr)
subroutine linear_solver_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(:)
@@ -19,6 +25,110 @@ contains
integer, intent(out) :: iter
real(rk), intent(out) :: res_norm
integer, intent(out) :: ierr
integer :: method
integer :: preconditioner
integer :: status
method = linear_solver_method
preconditioner = linear_precond_method
status = LS_STATUS_FAILED
select case (method)
case (LSOLVER_CG)
call linear_cg_solve(a, b, x, max_iter, tol, iter, res_norm, ierr, &
preconditioner)
case (LSOLVER_BICGSTAB)
call linear_bicgstab_solve(a, b, x, max_iter, tol, iter, res_norm, &
ierr, preconditioner)
case (LSOLVER_EXTERNAL)
iter = 0
res_norm = ZERO
status = LS_STATUS_UNSUPPORTED
call error_set(FEM_ERR_SOLVER, 'external linear solver is not enabled', ierr)
case default
iter = 0
res_norm = ZERO
call error_set(FEM_ERR_SOLVER, 'unknown linear solver method', ierr)
end select
if (ierr .eq. FEM_SUCCESS) status = LS_STATUS_CONVERGED
call linear_solver_record(method, preconditioner, iter, res_norm, status)
end subroutine linear_solver_solve
subroutine linear_solver_configure(method, preconditioner, ierr)
integer, intent(in) :: method
integer, intent(in) :: preconditioner
integer, intent(out) :: ierr
if (method .ne. LSOLVER_CG .and. method .ne. LSOLVER_BICGSTAB .and. &
method .ne. LSOLVER_EXTERNAL) then
call error_set(FEM_ERR_SOLVER, 'invalid linear solver method', ierr)
return
end if
if (preconditioner .ne. LPRECOND_NONE .and. &
preconditioner .ne. LPRECOND_JACOBI) then
call error_set(FEM_ERR_SOLVER, 'invalid linear preconditioner', ierr)
return
end if
linear_solver_method = method
linear_precond_method = preconditioner
ierr = FEM_SUCCESS
end subroutine linear_solver_configure
subroutine linear_solver_get_settings(method, preconditioner, ierr)
integer, intent(out) :: method
integer, intent(out) :: preconditioner
integer, intent(out) :: ierr
method = linear_solver_method
preconditioner = linear_precond_method
ierr = FEM_SUCCESS
end subroutine linear_solver_get_settings
subroutine linear_solver_get_stats(method, preconditioner, iteration, &
residual_norm, status_code, ierr)
integer, intent(out) :: method
integer, intent(out) :: preconditioner
integer, intent(out) :: iteration
real(rk), intent(out) :: residual_norm
integer, intent(out) :: status_code
integer, intent(out) :: ierr
method = linear_solver_last_method
preconditioner = linear_solver_last_prec
iteration = linear_solver_last_iter
residual_norm = linear_solver_last_resid
status_code = linear_solver_last_status
ierr = FEM_SUCCESS
end subroutine linear_solver_get_stats
subroutine linear_solver_record(method, preconditioner, iteration, &
residual_norm, status_code)
integer, intent(in) :: method
integer, intent(in) :: preconditioner
integer, intent(in) :: iteration
real(rk), intent(in) :: residual_norm
integer, intent(in) :: status_code
linear_solver_last_method = method
linear_solver_last_prec = preconditioner
linear_solver_last_iter = iteration
linear_solver_last_resid = residual_norm
linear_solver_last_status = status_code
end subroutine linear_solver_record
subroutine linear_cg_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 :: r(:)
real(rk), allocatable :: z(:)
real(rk), allocatable :: p(:)
@@ -32,10 +142,13 @@ contains
real(rk) :: rsnew
real(rk) :: threshold
integer :: n
integer :: prec
integer :: stat
iter = 0
res_norm = ZERO
prec = LPRECOND_JACOBI
if (present(preconditioner)) prec = preconditioner
if (a%nrow .le. 0 .or. a%ncol .le. 0) then
call error_set(FEM_ERR_SOLVER, 'CG solver received an empty matrix', ierr)
@@ -65,8 +178,7 @@ contains
return
end if
diag_inv = ONE
call build_jacobi_inverse(a, diag_inv, ierr)
call build_preconditioner(a, prec, diag_inv, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
call csr_matvec(a, x, ap, ierr)
@@ -132,6 +244,170 @@ contains
if (allocated(diag_inv)) deallocate(diag_inv)
end subroutine linear_cg_solve
subroutine linear_bicgstab_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 :: r(:), rhat(:), p(:), v(:), s(:), t(:)
real(rk), allocatable :: phat(:), shat(:), diag_inv(:)
real(rk) :: alpha, beta, omega, rho_old, rho_new
real(rk) :: denom, tt, norm_b, threshold
integer :: n, 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, 'BiCGSTAB', ierr)
if (ierr .ne. FEM_SUCCESS) return
n = a%nrow
allocate(r(n), rhat(n), p(n), v(n), s(n), t(n), phat(n), shat(n), &
diag_inv(n), stat=stat)
if (stat .ne. 0) then
call error_set(FEM_ERR_SOLVER, 'failed to allocate BiCGSTAB 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, v, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
r = b - v
rhat = r
p = ZERO
v = ZERO
rho_old = ONE
alpha = ONE
omega = ONE
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
do iter = 1, max_iter
rho_new = dot_product(rhat, r)
if (abs(rho_new) .le. tiny(ONE)) then
call error_set(FEM_ERR_SOLVER, 'BiCGSTAB rho breakdown', ierr)
go to 900
end if
beta = (rho_new / rho_old) * (alpha / omega)
p = r + beta * (p - omega * v)
call apply_jacobi_inverse(diag_inv, p, phat)
call csr_matvec(a, phat, v, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
denom = dot_product(rhat, v)
if (abs(denom) .le. tiny(ONE)) then
call error_set(FEM_ERR_SOLVER, 'BiCGSTAB alpha breakdown', ierr)
go to 900
end if
alpha = rho_new / denom
s = r - alpha * v
res_norm = sqrt(dot_product(s, s))
if (res_norm .le. threshold) then
x = x + alpha * phat
ierr = FEM_SUCCESS
go to 900
end if
call apply_jacobi_inverse(diag_inv, s, shat)
call csr_matvec(a, shat, t, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
tt = dot_product(t, t)
if (tt .le. tiny(ONE)) then
call error_set(FEM_ERR_SOLVER, 'BiCGSTAB omega breakdown', ierr)
go to 900
end if
omega = dot_product(t, s) / tt
x = x + alpha * phat + omega * shat
r = s - omega * t
res_norm = sqrt(dot_product(r, r))
if (res_norm .le. threshold) then
ierr = FEM_SUCCESS
go to 900
end if
if (abs(omega) .le. tiny(ONE)) then
call error_set(FEM_ERR_SOLVER, 'BiCGSTAB omega stagnation', ierr)
go to 900
end if
rho_old = rho_new
end do
iter = max_iter
call error_set(FEM_ERR_SOLVER, 'BiCGSTAB solver did not converge', ierr)
900 continue
if (allocated(r)) deallocate(r)
if (allocated(rhat)) deallocate(rhat)
if (allocated(p)) deallocate(p)
if (allocated(v)) deallocate(v)
if (allocated(s)) deallocate(s)
if (allocated(t)) deallocate(t)
if (allocated(phat)) deallocate(phat)
if (allocated(shat)) deallocate(shat)
if (allocated(diag_inv)) deallocate(diag_inv)
end subroutine linear_bicgstab_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(:)
real(rk), intent(in) :: x(:)
integer, intent(in) :: max_iter
real(rk), intent(in) :: tol
character(len=*), intent(in) :: solver_name
integer, intent(out) :: ierr
if (a%nrow .le. 0 .or. a%ncol .le. 0) then
call error_set(FEM_ERR_SOLVER, trim(solver_name)//' solver received an empty matrix', ierr)
return
end if
if (a%nrow .ne. a%ncol) then
call error_set(FEM_ERR_SOLVER, trim(solver_name)//' 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, trim(solver_name)//' solver vector size mismatch', ierr)
return
end if
if (max_iter .le. 0) then
call error_set(FEM_ERR_SOLVER, trim(solver_name)//' solver max_iter must be positive', ierr)
return
end if
if (tol .le. ZERO) then
call error_set(FEM_ERR_SOLVER, trim(solver_name)//' solver tolerance must be positive', ierr)
return
end if
ierr = FEM_SUCCESS
end subroutine validate_linear_system
subroutine build_preconditioner(a, preconditioner, diag_inv, ierr)
type(csr_matrix), intent(in) :: a
integer, intent(in) :: preconditioner
real(rk), intent(inout) :: diag_inv(:)
integer, intent(out) :: ierr
select case (preconditioner)
case (LPRECOND_NONE)
diag_inv = ONE
ierr = FEM_SUCCESS
case (LPRECOND_JACOBI)
call build_jacobi_inverse(a, diag_inv, ierr)
case default
call error_set(FEM_ERR_SOLVER, 'unsupported linear preconditioner', ierr)
end select
end subroutine build_preconditioner
subroutine build_jacobi_inverse(a, diag_inv, ierr)
type(csr_matrix), intent(in) :: a
real(rk), intent(inout) :: diag_inv(:)
+6 -6
View File
@@ -91,7 +91,7 @@ contains
if (ierr .ne. FEM_SUCCESS) go to 900
u = ZERO
call linear_cg_solve(kmat, rhs, u, max_iter, tol, iter, res_norm, ierr)
call linear_solver_solve(kmat, rhs, u, max_iter, tol, iter, res_norm, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
if (allocated(v)) v = ZERO
@@ -316,7 +316,7 @@ contains
if (ierr .ne. FEM_SUCCESS) go to 900
u = u_pred
call linear_cg_solve(kmat, rhs, u, max_iter, tol, iter, res_norm, ierr)
call linear_solver_solve(kmat, rhs, u, max_iter, tol, iter, res_norm, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
call newmark_linear_finish(dt, gamma, a0, target_time, kmat, &
@@ -692,8 +692,8 @@ contains
call apply_implicit_dirichlet_zero(kmat, rhs_corr, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
call linear_cg_solve(kmat, rhs_corr, du, max_iter * 20, tol, &
cg_iter, cg_res, ierr)
call linear_solver_solve(kmat, rhs_corr, du, max_iter * 20, tol, &
cg_iter, cg_res, ierr)
900 continue
call csr_free(kmat, stat)
@@ -1137,8 +1137,8 @@ contains
call apply_implicit_dirichlet_zero(kmat, rhs_corr, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
call linear_cg_solve(kmat, rhs_corr, du, max_iter * 20, tol, &
cg_iter, cg_res, ierr)
call linear_solver_solve(kmat, rhs_corr, du, max_iter * 20, tol, &
cg_iter, cg_res, ierr)
900 continue
call csr_free(kmat, stat)