Add explicit restart support

This commit is contained in:
shenw
2026-06-22 23:49:30 +08:00
parent 5d1e6e5526
commit 6ff7630b25
5 changed files with 545 additions and 0 deletions
+25
View File
@@ -15,6 +15,7 @@ module fem_api
use fem_contact
use fem_output_csv
use fem_output_vtk
use fem_restart
implicit none
logical :: api_initialized = .false.
@@ -583,6 +584,30 @@ contains
call output_vtk_write(step, ierr)
end subroutine fem_write_output
subroutine fem_write_restart(filename, ierr)
character(len=*), intent(in) :: filename
integer, intent(out) :: ierr
if (.not. api_initialized) then
call error_set(FEM_ERR_INTERNAL, 'fem_write_restart called before fem_initialize', ierr)
return
end if
call restart_write(filename, ierr)
end subroutine fem_write_restart
subroutine fem_read_restart(filename, ierr)
character(len=*), intent(in) :: filename
integer, intent(out) :: ierr
if (.not. api_initialized) then
call error_set(FEM_ERR_INTERNAL, 'fem_read_restart called before fem_initialize', ierr)
return
end if
call restart_read(filename, ierr)
end subroutine fem_read_restart
subroutine fem_estimate_stable_dt(dtcrit, ierr)
real(rk), intent(out) :: dtcrit
integer, intent(out) :: ierr
+354
View File
@@ -0,0 +1,354 @@
module fem_restart
use fem_precision
use fem_error
use fem_control
use fem_database
use fem_element_state
implicit none
private
public :: restart_write
public :: restart_read
contains
subroutine restart_write(filename, ierr)
character(len=*), intent(in) :: filename
integer, intent(out) :: ierr
integer :: unit_id
integer :: ios
if (len_trim(filename) .eq. 0) then
call error_set(FEM_ERR_OUTPUT, 'restart filename is empty', ierr)
return
end if
if (.not. allocated(u) .or. .not. allocated(v) .or. &
.not. allocated(a) .or. .not. allocated(vhalf)) then
call error_set(FEM_ERR_OUTPUT, 'restart write requires allocated vectors', ierr)
return
end if
unit_id = 65
open(unit=unit_id, file=trim(filename), status='replace', &
action='write', iostat=ios)
if (ios .ne. 0) then
call error_set(FEM_ERR_OUTPUT, 'failed to open restart output', ierr)
return
end if
call write_restart_header(unit_id, ios)
if (ios .ne. 0) go to 900
call write_restart_dofs(unit_id, ios)
if (ios .ne. 0) go to 900
call write_restart_elements(unit_id, ios)
if (ios .ne. 0) go to 900
call write_restart_gauss(unit_id, ios)
if (ios .ne. 0) go to 900
write(unit_id,'(A)',iostat=ios) 'END'
if (ios .ne. 0) go to 900
close(unit_id)
ierr = FEM_SUCCESS
return
900 continue
close(unit_id)
call error_set(FEM_ERR_OUTPUT, 'failed to write restart output', ierr)
end subroutine restart_write
subroutine restart_read(filename, ierr)
character(len=*), intent(in) :: filename
integer, intent(out) :: ierr
integer :: unit_id
integer :: ios
character(len=32) :: tag
integer :: version
integer :: file_nnode
integer :: file_nelem
integer :: file_ndof
integer :: file_ngauss
integer :: file_step
real(rk) :: file_time
if (len_trim(filename) .eq. 0) then
call error_set(FEM_ERR_OUTPUT, 'restart filename is empty', ierr)
return
end if
if (.not. allocated(u) .or. .not. allocated(v) .or. &
.not. allocated(a) .or. .not. allocated(vhalf)) then
call error_set(FEM_ERR_OUTPUT, 'restart read requires built model vectors', ierr)
return
end if
unit_id = 66
open(unit=unit_id, file=trim(filename), status='old', &
action='read', iostat=ios)
if (ios .ne. 0) then
call error_set(FEM_ERR_OUTPUT, 'failed to open restart input', ierr)
return
end if
read(unit_id,*,iostat=ios) tag, version
if (ios .ne. 0 .or. trim(tag) .ne. 'FEMLITE_RESTART') go to 910
if (version .ne. 1) then
close(unit_id)
call error_set(FEM_ERR_OUTPUT, 'unsupported restart version', ierr)
return
end if
read(unit_id,*,iostat=ios) tag, file_nnode, file_nelem, &
file_ndof, file_ngauss
if (ios .ne. 0 .or. trim(tag) .ne. 'COUNTS') go to 910
call check_restart_counts(file_nnode, file_nelem, file_ndof, &
file_ngauss, ierr)
if (ierr .ne. FEM_SUCCESS) then
close(unit_id)
return
end if
read(unit_id,*,iostat=ios) tag, file_step, file_time
if (ios .ne. 0 .or. trim(tag) .ne. 'TIME') go to 910
call read_restart_dofs(unit_id, ierr)
if (ierr .ne. FEM_SUCCESS) then
close(unit_id)
return
end if
call read_restart_elements(unit_id, ierr)
if (ierr .ne. FEM_SUCCESS) then
close(unit_id)
return
end if
call read_restart_gauss(unit_id, file_ngauss, ierr)
if (ierr .ne. FEM_SUCCESS) then
close(unit_id)
return
end if
read(unit_id,*,iostat=ios) tag
if (ios .ne. 0 .or. trim(tag) .ne. 'END') go to 910
close(unit_id)
current_step = file_step
current_time = file_time
call update_current_coordinates()
ierr = FEM_SUCCESS
return
910 continue
close(unit_id)
call error_set(FEM_ERR_OUTPUT, 'invalid restart file format', ierr)
end subroutine restart_read
subroutine write_restart_header(unit_id, ios)
integer, intent(in) :: unit_id
integer, intent(out) :: ios
write(unit_id,'(A,1X,I4)',iostat=ios) 'FEMLITE_RESTART', 1
if (ios .ne. 0) return
write(unit_id,'(A,4(1X,I12))',iostat=ios) 'COUNTS', nnode, nelem, &
ndof, ngauss_total
if (ios .ne. 0) return
write(unit_id,'(A,1X,I12,1X,ES24.16)',iostat=ios) &
'TIME', current_step, current_time
end subroutine write_restart_header
subroutine write_restart_dofs(unit_id, ios)
integer, intent(in) :: unit_id
integer, intent(out) :: ios
integer :: dof
write(unit_id,'(A)',iostat=ios) 'DOF_STATE'
if (ios .ne. 0) return
do dof = 1, ndof
write(unit_id,'(I12,4(1X,ES24.16))',iostat=ios) &
dof, u(dof), v(dof), a(dof), vhalf(dof)
if (ios .ne. 0) return
end do
end subroutine write_restart_dofs
subroutine write_restart_elements(unit_id, ios)
integer, intent(in) :: unit_id
integer, intent(out) :: ios
integer :: elem
write(unit_id,'(A)',iostat=ios) 'ELEMENT_STATUS'
if (ios .ne. 0) return
do elem = 1, nelem
write(unit_id,'(I12,1X,I8)',iostat=ios) elem_id_ext(elem), elem_status(elem)
if (ios .ne. 0) return
end do
end subroutine write_restart_elements
subroutine write_restart_gauss(unit_id, ios)
integer, intent(in) :: unit_id
integer, intent(out) :: ios
integer :: gp
write(unit_id,'(A)',iostat=ios) 'GAUSS_STATE'
if (ios .ne. 0) return
if (ngauss_total .le. 0) return
if (.not. allocated(stress) .or. .not. allocated(strain) .or. &
.not. allocated(statev)) return
do gp = 1, ngauss_total
write(unit_id,'(I12,44(1X,ES24.16))',iostat=ios) gp, &
stress(1,gp), stress(2,gp), stress(3,gp), &
stress(4,gp), stress(5,gp), stress(6,gp), &
strain(1,gp), strain(2,gp), strain(3,gp), &
strain(4,gp), strain(5,gp), strain(6,gp), &
statev(1,gp), statev(2,gp), statev(3,gp), statev(4,gp), &
statev(5,gp), statev(6,gp), statev(7,gp), statev(8,gp), &
statev(9,gp), statev(10,gp), statev(11,gp), statev(12,gp), &
statev(13,gp), statev(14,gp), statev(15,gp), statev(16,gp), &
statev(17,gp), statev(18,gp), statev(19,gp), statev(20,gp), &
statev(21,gp), statev(22,gp), statev(23,gp), statev(24,gp), &
statev(25,gp), statev(26,gp), statev(27,gp), statev(28,gp), &
statev(29,gp), statev(30,gp), statev(31,gp), statev(32,gp)
if (ios .ne. 0) return
end do
end subroutine write_restart_gauss
subroutine check_restart_counts(file_nnode, file_nelem, file_ndof, &
file_ngauss, ierr)
integer, intent(in) :: file_nnode
integer, intent(in) :: file_nelem
integer, intent(in) :: file_ndof
integer, intent(in) :: file_ngauss
integer, intent(out) :: ierr
if (file_nnode .ne. nnode .or. file_nelem .ne. nelem .or. &
file_ndof .ne. ndof) then
call error_set(FEM_ERR_OUTPUT, 'restart model size does not match', ierr)
return
end if
if (file_ngauss .gt. 0) then
call element_state_ensure(ierr)
if (ierr .ne. FEM_SUCCESS) return
end if
if (file_ngauss .ne. ngauss_total) then
call error_set(FEM_ERR_OUTPUT, 'restart Gauss state count does not match', ierr)
return
end if
ierr = FEM_SUCCESS
end subroutine check_restart_counts
subroutine read_restart_dofs(unit_id, ierr)
integer, intent(in) :: unit_id
integer, intent(out) :: ierr
character(len=32) :: tag
integer :: ios
integer :: dof
integer :: file_dof
read(unit_id,*,iostat=ios) tag
if (ios .ne. 0 .or. trim(tag) .ne. 'DOF_STATE') then
call error_set(FEM_ERR_OUTPUT, 'restart missing DOF state', ierr)
return
end if
do dof = 1, ndof
read(unit_id,*,iostat=ios) file_dof, u(dof), v(dof), a(dof), vhalf(dof)
if (ios .ne. 0 .or. file_dof .ne. dof) then
call error_set(FEM_ERR_OUTPUT, 'invalid restart DOF state', ierr)
return
end if
end do
ierr = FEM_SUCCESS
end subroutine read_restart_dofs
subroutine read_restart_elements(unit_id, ierr)
integer, intent(in) :: unit_id
integer, intent(out) :: ierr
character(len=32) :: tag
integer :: ios
integer :: elem
integer :: elem_id
integer :: status_value
read(unit_id,*,iostat=ios) tag
if (ios .ne. 0 .or. trim(tag) .ne. 'ELEMENT_STATUS') then
call error_set(FEM_ERR_OUTPUT, 'restart missing element status', ierr)
return
end if
do elem = 1, nelem
read(unit_id,*,iostat=ios) elem_id, status_value
if (ios .ne. 0 .or. elem_id .ne. elem_id_ext(elem)) then
call error_set(FEM_ERR_OUTPUT, 'invalid restart element status', ierr)
return
end if
elem_status(elem) = status_value
end do
ierr = FEM_SUCCESS
end subroutine read_restart_elements
subroutine read_restart_gauss(unit_id, file_ngauss, ierr)
integer, intent(in) :: unit_id
integer, intent(in) :: file_ngauss
integer, intent(out) :: ierr
character(len=32) :: tag
integer :: ios
integer :: gp
integer :: file_gp
read(unit_id,*,iostat=ios) tag
if (ios .ne. 0 .or. trim(tag) .ne. 'GAUSS_STATE') then
call error_set(FEM_ERR_OUTPUT, 'restart missing Gauss state', ierr)
return
end if
if (file_ngauss .le. 0) then
ierr = FEM_SUCCESS
return
end if
if (.not. allocated(stress) .or. .not. allocated(strain) .or. &
.not. allocated(statev)) then
call error_set(FEM_ERR_OUTPUT, 'restart Gauss state is not allocated', ierr)
return
end if
do gp = 1, file_ngauss
read(unit_id,*,iostat=ios) file_gp, &
stress(1,gp), stress(2,gp), stress(3,gp), &
stress(4,gp), stress(5,gp), stress(6,gp), &
strain(1,gp), strain(2,gp), strain(3,gp), &
strain(4,gp), strain(5,gp), strain(6,gp), &
statev(1,gp), statev(2,gp), statev(3,gp), statev(4,gp), &
statev(5,gp), statev(6,gp), statev(7,gp), statev(8,gp), &
statev(9,gp), statev(10,gp), statev(11,gp), statev(12,gp), &
statev(13,gp), statev(14,gp), statev(15,gp), statev(16,gp), &
statev(17,gp), statev(18,gp), statev(19,gp), statev(20,gp), &
statev(21,gp), statev(22,gp), statev(23,gp), statev(24,gp), &
statev(25,gp), statev(26,gp), statev(27,gp), statev(28,gp), &
statev(29,gp), statev(30,gp), statev(31,gp), statev(32,gp)
if (ios .ne. 0 .or. file_gp .ne. gp) then
call error_set(FEM_ERR_OUTPUT, 'invalid restart Gauss state', ierr)
return
end if
end do
ierr = FEM_SUCCESS
end subroutine read_restart_gauss
subroutine update_current_coordinates()
integer :: node
integer :: idir
integer :: dof
if (.not. allocated(coord)) return
do node = 1, nnode
coord(:,node) = coord0(:,node)
do idir = 1, NDIM
dof = dof_id(idir,node)
if (dof .gt. 0) coord(idir,node) = coord(idir,node) + u(dof)
end do
end do
end subroutine update_current_coordinates
end module fem_restart