diff --git a/.gitignore b/.gitignore index 1f7d10a..6a8468b 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,6 @@ Testing/ history.csv nodes_*.csv elements_*.csv +result_*.vtk +restart_*.frs /output/ diff --git a/CMakeLists.txt b/CMakeLists.txt index c40a5a5..71b34c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ add_library(femlite_core STATIC src/boundary/fem_contact.f90 src/output/fem_output_csv.f90 src/output/fem_output_vtk.f90 + src/output/fem_restart.f90 src/base/fem_api.f90 ) @@ -202,6 +203,13 @@ add_executable(test_vtk_output target_link_libraries(test_vtk_output PRIVATE femlite_core) femlite_apply_fortran_options(test_vtk_output) +add_executable(test_restart + tests/module/test_restart.f90 +) + +target_link_libraries(test_restart PRIVATE femlite_core) +femlite_apply_fortran_options(test_restart) + add_executable(test_implicit_inp tests/module/test_implicit_inp.f90 ) @@ -306,6 +314,11 @@ set_tests_properties(vtk_output PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" ) +add_test(NAME restart COMMAND test_restart) +set_tests_properties(restart PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" +) + add_test(NAME implicit_inp COMMAND test_implicit_inp) set_tests_properties(implicit_inp PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" diff --git a/src/base/fem_api.f90 b/src/base/fem_api.f90 index 54f74bf..294cfe2 100644 --- a/src/base/fem_api.f90 +++ b/src/base/fem_api.f90 @@ -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 diff --git a/src/output/fem_restart.f90 b/src/output/fem_restart.f90 new file mode 100644 index 0000000..7ee5143 --- /dev/null +++ b/src/output/fem_restart.f90 @@ -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 diff --git a/tests/module/test_restart.f90 b/tests/module/test_restart.f90 new file mode 100644 index 0000000..3fc01b8 --- /dev/null +++ b/tests/module/test_restart.f90 @@ -0,0 +1,151 @@ +program test_restart + use fem_precision + use fem_error + use fem_api + implicit none + + real(rk), parameter :: DT_TEST = 1.0e-8_rk + character(len=*), parameter :: RESTART_FILE = & + 'build-wsl-debug/restart_check.frs' + + integer :: ierr + integer :: ref_step + integer :: rst_step + real(rk) :: ref_time + real(rk) :: rst_time + real(rk) :: ref_u(NDIM) + real(rk) :: ref_v(NDIM) + real(rk) :: ref_a(NDIM) + real(rk) :: rst_u(NDIM) + real(rk) :: rst_v(NDIM) + real(rk) :: rst_a(NDIM) + + call fem_initialize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(1) + + call run_continuous(ref_step, ref_time, ref_u, ref_v, ref_a, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(2) + + call run_restarted(rst_step, rst_time, rst_u, rst_v, rst_a, ierr) + if (ierr .ne. FEM_SUCCESS) call fail(3) + + if (ref_step .ne. 3) call fail(4) + if (rst_step .ne. ref_step) call fail(5) + call check_close(rst_time, ref_time, 6) + call check_vector(rst_u, ref_u, 7) + call check_vector(rst_v, ref_v, 8) + call check_vector(rst_a, ref_a, 9) + + call fem_finalize(ierr) + if (ierr .ne. FEM_SUCCESS) call fail(10) + +contains + + subroutine run_continuous(step, time, disp, vel, acc, ierr) + integer, intent(out) :: step + real(rk), intent(out) :: time + real(rk), intent(out) :: disp(NDIM) + real(rk), intent(out) :: vel(NDIM) + real(rk), intent(out) :: acc(NDIM) + integer, intent(out) :: ierr + + call prepare_model(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call loaded_step(ierr) + if (ierr .ne. FEM_SUCCESS) return + call loaded_step(ierr) + if (ierr .ne. FEM_SUCCESS) return + call loaded_step(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call fem_get_current_time(time, step, ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_get_node_kinematics(2, disp, vel, acc, ierr) + end subroutine run_continuous + + subroutine run_restarted(step, time, disp, vel, acc, ierr) + integer, intent(out) :: step + real(rk), intent(out) :: time + real(rk), intent(out) :: disp(NDIM) + real(rk), intent(out) :: vel(NDIM) + real(rk), intent(out) :: acc(NDIM) + integer, intent(out) :: ierr + + call prepare_model(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call loaded_step(ierr) + if (ierr .ne. FEM_SUCCESS) return + call loaded_step(ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_write_restart(RESTART_FILE, ierr) + if (ierr .ne. FEM_SUCCESS) return + + call prepare_model(ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_read_restart(RESTART_FILE, ierr) + if (ierr .ne. FEM_SUCCESS) return + call loaded_step(ierr) + if (ierr .ne. FEM_SUCCESS) return + + call fem_get_current_time(time, step, ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_get_node_kinematics(2, disp, vel, acc, ierr) + end subroutine run_restarted + + subroutine prepare_model(ierr) + integer, intent(out) :: ierr + + call fem_read_inp('examples/c3d4_one_element.inp', ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_build_model(ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_compute_lumped_mass(ierr) + end subroutine prepare_model + + subroutine loaded_step(ierr) + integer, intent(out) :: ierr + real(rk) :: force(NDIM) + + force = ZERO + force(1) = 5.0_rk + force(2) = 1.0_rk + force(3) = -2.0_rk + + call fem_clear_sph_forces(ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_apply_sph_nodal_force(2, force, ierr) + if (ierr .ne. FEM_SUCCESS) return + call fem_advance_explicit(DT_TEST, ierr) + end subroutine loaded_step + + subroutine check_vector(actual, expected, code) + real(rk), intent(in) :: actual(NDIM) + real(rk), intent(in) :: expected(NDIM) + integer, intent(in) :: code + integer :: idir + + do idir = 1, NDIM + call check_close(actual(idir), expected(idir), code) + end do + end subroutine check_vector + + subroutine check_close(actual, expected, code) + real(rk), intent(in) :: actual + real(rk), intent(in) :: expected + integer, intent(in) :: code + real(rk) :: scale + + scale = max(ONE, abs(expected)) + if (abs(actual - expected) .gt. 1.0e-8_rk * scale) call fail(code) + end subroutine check_close + + subroutine fail(code) + integer, intent(in) :: code + + write(*,*) 'test_restart failed:', code + stop 1 + end subroutine fail + +end program test_restart