200 lines
7.1 KiB
Fortran
200 lines
7.1 KiB
Fortran
module fem_linear_solver_dispatch
|
|
use fem_precision
|
|
use fem_error
|
|
use fem_control
|
|
use fem_sparse_csr
|
|
use fem_sparse_csr_diagnostics, only: csr_profile, csr_analyze, &
|
|
csr_residual_norm
|
|
use fem_linear_solver_iterative
|
|
use fem_linear_solver_dense
|
|
implicit none
|
|
|
|
private
|
|
|
|
public :: linear_solver_solve
|
|
public :: linear_solver_configure
|
|
public :: linear_solver_get_settings
|
|
public :: linear_solver_get_stats
|
|
public :: linear_solver_get_diagnostics
|
|
|
|
contains
|
|
|
|
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(:)
|
|
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 :: method
|
|
integer :: preconditioner
|
|
integer :: status
|
|
type(csr_profile) :: profile
|
|
real(rk) :: initial_resid
|
|
real(rk) :: initial_rel_resid
|
|
real(rk) :: final_resid
|
|
real(rk) :: final_rel_resid
|
|
integer :: matrix_class
|
|
integer :: audit_ierr
|
|
|
|
method = linear_solver_method
|
|
preconditioner = linear_precond_method
|
|
status = LS_STATUS_FAILED
|
|
matrix_class = 1
|
|
initial_resid = ZERO
|
|
initial_rel_resid = ZERO
|
|
final_resid = ZERO
|
|
final_rel_resid = ZERO
|
|
|
|
call csr_analyze(a, profile, ierr)
|
|
if (ierr .ne. FEM_SUCCESS) then
|
|
iter = 0
|
|
res_norm = ZERO
|
|
call linear_solver_record(method, preconditioner, iter, res_norm, &
|
|
status, matrix_class, initial_resid, &
|
|
final_rel_resid)
|
|
return
|
|
end if
|
|
matrix_class = profile%class_id
|
|
|
|
call csr_residual_norm(a, b, x, initial_resid, initial_rel_resid, ierr)
|
|
if (ierr .ne. FEM_SUCCESS) then
|
|
iter = 0
|
|
res_norm = ZERO
|
|
call linear_solver_record(method, preconditioner, iter, res_norm, &
|
|
status, matrix_class, initial_resid, &
|
|
final_rel_resid)
|
|
return
|
|
end if
|
|
|
|
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_GMRES)
|
|
call linear_gmres_solve(a, b, x, max_iter, tol, iter, res_norm, &
|
|
ierr, preconditioner)
|
|
case (LSOLVER_DENSE)
|
|
call linear_dense_solve(a, b, x, iter, res_norm, ierr)
|
|
case default
|
|
iter = 0
|
|
res_norm = ZERO
|
|
call error_set(FEM_ERR_SOLVER, 'unknown linear solver method', ierr)
|
|
end select
|
|
|
|
if (ierr .ne. FEM_SUCCESS) then
|
|
call csr_residual_norm(a, b, x, final_resid, final_rel_resid, &
|
|
audit_ierr)
|
|
if (audit_ierr .eq. FEM_SUCCESS) res_norm = final_resid
|
|
call linear_solver_record(method, preconditioner, iter, res_norm, &
|
|
status, matrix_class, initial_resid, &
|
|
final_rel_resid)
|
|
return
|
|
end if
|
|
|
|
status = LS_STATUS_CONVERGED
|
|
call csr_residual_norm(a, b, x, final_resid, final_rel_resid, &
|
|
audit_ierr)
|
|
if (audit_ierr .eq. FEM_SUCCESS) res_norm = final_resid
|
|
call linear_solver_record(method, preconditioner, iter, res_norm, &
|
|
status, matrix_class, initial_resid, &
|
|
final_rel_resid)
|
|
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_GMRES .and. method .ne. LSOLVER_DENSE) 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 .and. &
|
|
preconditioner .ne. LPRECOND_GAUSS_SEIDEL .and. &
|
|
preconditioner .ne. LPRECOND_ILU0) 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_get_diagnostics(matrix_class, initial_residual, &
|
|
final_residual, relative_residual, &
|
|
status_code, ierr)
|
|
integer, intent(out) :: matrix_class
|
|
real(rk), intent(out) :: initial_residual
|
|
real(rk), intent(out) :: final_residual
|
|
real(rk), intent(out) :: relative_residual
|
|
integer, intent(out) :: status_code
|
|
integer, intent(out) :: ierr
|
|
|
|
matrix_class = linear_solver_last_matrix_class
|
|
initial_residual = linear_solver_initial_resid
|
|
final_residual = linear_solver_last_resid
|
|
relative_residual = linear_solver_relative_resid
|
|
status_code = linear_solver_last_status
|
|
ierr = FEM_SUCCESS
|
|
end subroutine linear_solver_get_diagnostics
|
|
|
|
subroutine linear_solver_record(method, preconditioner, iteration, &
|
|
residual_norm, status_code, matrix_class, &
|
|
initial_residual, relative_residual)
|
|
integer, intent(in) :: method
|
|
integer, intent(in) :: preconditioner
|
|
integer, intent(in) :: iteration
|
|
real(rk), intent(in) :: residual_norm
|
|
integer, intent(in) :: status_code
|
|
integer, intent(in) :: matrix_class
|
|
real(rk), intent(in) :: initial_residual
|
|
real(rk), intent(in) :: relative_residual
|
|
|
|
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
|
|
linear_solver_last_matrix_class = matrix_class
|
|
linear_solver_initial_resid = initial_residual
|
|
linear_solver_relative_resid = relative_residual
|
|
end subroutine linear_solver_record
|
|
|
|
end module fem_linear_solver_dispatch
|