Add implicit linear static solver

This commit is contained in:
shenw
2026-06-22 22:02:13 +08:00
parent 4f716ad0b1
commit f852a93b91
4 changed files with 407 additions and 0 deletions
+195
View File
@@ -0,0 +1,195 @@
module fem_solver_implicit
use fem_precision
use fem_error
use fem_database
use fem_sparse_csr
use fem_assembly_csr
use fem_linear_solver_cg
use fem_element_stiffness
use fem_boundary
implicit none
private
public :: implicit_solve_linear_static
contains
subroutine implicit_solve_linear_static(time, max_iter, tol, iter, res_norm, ierr)
real(rk), intent(in) :: time
integer, intent(in) :: max_iter
real(rk), intent(in) :: tol
integer, intent(out) :: iter
real(rk), intent(out) :: res_norm
integer, intent(out) :: ierr
type(csr_matrix) :: kmat
real(rk), allocatable :: rhs(:)
integer :: stat
iter = 0
res_norm = ZERO
call csr_init(kmat)
if (ndof .le. 0) then
call error_set(FEM_ERR_SOLVER, 'implicit linear solve requires positive ndof', ierr)
return
end if
if (.not. allocated(u)) then
call error_set(FEM_ERR_INTERNAL, 'implicit linear solve requires allocated solution vector', ierr)
return
end if
allocate(rhs(ndof), stat=stat)
if (stat .ne. 0) then
call error_set(FEM_ERR_SOLVER, 'failed to allocate implicit RHS', ierr)
return
end if
rhs = ZERO
call assembly_build_csr_pattern(kmat, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
call assemble_linear_stiffness(kmat, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
call boundary_apply_prescribed_loads(time, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
rhs = fext
if (allocated(fsph)) rhs = rhs + fsph
if (allocated(fcontact)) rhs = rhs + fcontact
call apply_implicit_dirichlet(kmat, rhs, time, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
u = ZERO
call linear_cg_solve(kmat, rhs, u, max_iter, tol, iter, res_norm, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
if (allocated(v)) v = ZERO
if (allocated(a)) a = ZERO
if (allocated(vhalf)) vhalf = ZERO
900 continue
call csr_free(kmat, stat)
if (allocated(rhs)) deallocate(rhs)
end subroutine implicit_solve_linear_static
subroutine assemble_linear_stiffness(kmat, ierr)
type(csr_matrix), intent(inout) :: kmat
integer, intent(out) :: ierr
real(rk), allocatable :: ke(:,:)
integer :: elem_dofs(MAX_ELEM_DOF)
integer :: n_elem_dof
integer :: elem
integer :: stat
allocate(ke(MAX_ELEM_DOF,MAX_ELEM_DOF), stat=stat)
if (stat .ne. 0) then
call error_set(FEM_ERR_SOLVER, 'failed to allocate implicit element stiffness workspace', ierr)
return
end if
do elem = 1, nelem
if (elem_status(elem) .ne. 0) cycle
call element_linear_stiffness(elem, elem_dofs, n_elem_dof, ke, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
call assembly_add_element_matrix(kmat, elem_dofs, n_elem_dof, ke, ierr)
if (ierr .ne. FEM_SUCCESS) go to 900
end do
ierr = FEM_SUCCESS
900 continue
if (allocated(ke)) deallocate(ke)
end subroutine assemble_linear_stiffness
subroutine apply_implicit_dirichlet(kmat, rhs, time, ierr)
type(csr_matrix), intent(inout) :: kmat
real(rk), intent(inout) :: rhs(:)
real(rk), intent(in) :: time
integer, intent(out) :: ierr
integer :: item
integer :: dof_num
real(rk) :: scale
do item = 1, nbc
scale = database_amplitude_value(bc_amp(item), time)
do dof_num = bc_dof_start(item), bc_dof_end(item)
call apply_bc_target(kmat, rhs, bc_target(item), dof_num, &
scale * bc_value(item), ierr)
if (ierr .ne. FEM_SUCCESS) return
end do
end do
ierr = FEM_SUCCESS
end subroutine apply_implicit_dirichlet
subroutine apply_bc_target(kmat, rhs, target, dof_num, value, ierr)
type(csr_matrix), intent(inout) :: kmat
real(rk), intent(inout) :: rhs(:)
character(len=*), intent(in) :: target
integer, intent(in) :: dof_num
real(rk), intent(in) :: value
integer, intent(out) :: ierr
integer :: node
integer :: set_id
integer :: pos
integer :: external_id
integer :: ios
if (dof_num .lt. 1 .or. dof_num .gt. NDOF_NODE) then
call error_set(FEM_ERR_INPUT, 'implicit boundary dof is outside 1..6', ierr)
return
end if
read(target,*,iostat=ios) external_id
if (ios .eq. 0) then
node = database_find_node(external_id)
if (node .eq. 0) then
call error_set(FEM_ERR_INPUT, 'implicit boundary references unknown node', ierr)
return
end if
call apply_bc_node(kmat, rhs, node, dof_num, value, ierr)
return
end if
set_id = database_find_nset(target)
if (set_id .eq. 0) then
call error_set(FEM_ERR_INPUT, 'implicit boundary references unknown node set', ierr)
return
end if
do pos = nset_start(set_id), nset_start(set_id) + nset_count(set_id) - 1
node = database_find_node(nset_items(pos))
if (node .eq. 0) then
call error_set(FEM_ERR_INPUT, 'implicit boundary node set contains unknown node', ierr)
return
end if
call apply_bc_node(kmat, rhs, node, dof_num, value, ierr)
if (ierr .ne. FEM_SUCCESS) return
end do
ierr = FEM_SUCCESS
end subroutine apply_bc_target
subroutine apply_bc_node(kmat, rhs, node, dof_num, value, ierr)
type(csr_matrix), intent(inout) :: kmat
real(rk), intent(inout) :: rhs(:)
integer, intent(in) :: node
integer, intent(in) :: dof_num
real(rk), intent(in) :: value
integer, intent(out) :: ierr
integer :: dof
dof = dof_id(dof_num,node)
if (dof .gt. 0) then
call csr_apply_dirichlet(kmat, rhs, dof, value, ierr)
if (ierr .ne. FEM_SUCCESS) return
end if
ierr = FEM_SUCCESS
end subroutine apply_bc_node
end module fem_solver_implicit