program test_coupling_restart_080_compat use sph_precision, only: rk, SPH_NDIM use sph_error, only: SPH_SUCCESS use sph_api, only: sph_initialize, sph_finalize, sph_read_inp, sph_build_model, & sph_write_restart, sph_read_restart use sph_database, only: npart, coupling_region_count, & coupling_region_disabled_count, coupling_region_name, & coupling_region_set_name, coupling_region_surface_name, & coupling_region_material_name, coupling_region_section_name, & coupling_region_type, coupling_region_export_mode, & coupling_region_import_mode, coupling_region_enabled, & coupling_region_phase_filter, coupling_region_material_filter, & coupling_region_section_filter, coupling_region_internal_id, & coupling_region_particle_count, coupling_region_particles, & SPH_COUPLING_LOAD_PARTICLE_FORCE, SPH_COUPLING_LOAD_FACET_FORCE, & SPH_COUPLING_DISTRIBUTION_UNIFORM, SPH_COUPLING_CONTACT_STATE_NONE use sph_coupling_interface, only: coupling_external_force, & coupling_motion_velocity, coupling_motion_position, coupling_motion_flag, & coupling_motion_mode, coupling_contact_flag, coupling_contact_gap, & coupling_contact_penetration, coupling_contact_pressure, & coupling_cumulative_impulse, coupling_momentum_exchange, & coupling_cumulative_work, coupling_energy_exchange, & coupling_stale_load_count, coupling_skipped_particle_count, & coupling_nonfinite_import_count, coupling_imported_force_count, & sph_coupling_apply_particle_forces, sph_coupling_apply_facet_loads, & sph_coupling_get_region_modes implicit none integer :: ierr call check_legacy_restart_read() contains subroutine check_legacy_restart_read() integer :: indices(1) integer :: facets(1) real(rk) :: forces(SPH_NDIM,1) real(rk) :: loads(SPH_NDIM,1) integer :: load_mode integer :: distribution_mode integer :: contact_state_mode call delete_file_if_exists('build-codex-unix/restart_coupling_080_seed.tmp') call delete_file_if_exists('build-codex-unix/restart_coupling_080_compat.tmp') call sph_initialize(ierr) call assert_ok(ierr, 'initialize failed for restart 080 compat') call sph_read_inp('tests/data/coupling_shell_region_080.inp', ierr) call assert_ok(ierr, 'read failed for restart 080 compat') call sph_build_model(ierr) call assert_ok(ierr, 'build failed for restart 080 compat') indices(1) = 1 forces(:,1) = (/1.0_rk, 2.0_rk, 3.0_rk/) call sph_coupling_apply_particle_forces(2, 1, indices, forces, ierr) call assert_ok(ierr, 'particle force import failed for restart 080 compat') facets(1) = 1 loads(:,1) = (/0.0_rk, 0.0_rk, 5.0_rk/) call sph_coupling_apply_facet_loads(1, 1, facets, loads, ierr) call assert_ok(ierr, 'facet load import failed for restart 080 compat') call sph_write_restart('build-codex-unix/restart_coupling_080_seed.tmp', ierr) call assert_ok(ierr, 'seed restart write failed for restart 080 compat') call write_legacy_restart('build-codex-unix/restart_coupling_080_seed.tmp', & 'build-codex-unix/restart_coupling_080_compat.tmp') call scramble_coupling_state() call sph_read_restart('build-codex-unix/restart_coupling_080_compat.tmp', ierr) call assert_ok(ierr, 'legacy restart read failed') call sph_coupling_get_region_modes(1, load_mode, distribution_mode, & contact_state_mode, ierr) call assert_ok(ierr, 'legacy region 1 mode query failed') if (load_mode /= SPH_COUPLING_LOAD_FACET_FORCE) then call fail('legacy region 1 load mode mismatch') end if call assert_legacy_common_modes(distribution_mode, contact_state_mode) call sph_coupling_get_region_modes(2, load_mode, distribution_mode, & contact_state_mode, ierr) call assert_ok(ierr, 'legacy region 2 mode query failed') if (load_mode /= SPH_COUPLING_LOAD_PARTICLE_FORCE) then call fail('legacy region 2 load mode mismatch') end if call assert_legacy_common_modes(distribution_mode, contact_state_mode) call assert_close(coupling_external_force(1,1), 1.0_rk, & 'legacy restart force x mismatch') call assert_close(coupling_external_force(2,1), 2.0_rk, & 'legacy restart force y mismatch') call assert_close(coupling_external_force(3,1), 8.0_rk, & 'legacy restart force z mismatch') if (coupling_imported_force_count /= 2) then call fail('legacy restart imported force count mismatch') end if if (coupling_contact_flag(1) /= 0) call fail('legacy restart contact flag mismatch') call assert_close(coupling_contact_gap(1), 0.0_rk, 'legacy restart gap mismatch') call assert_close(coupling_contact_penetration(1), 0.0_rk, & 'legacy restart penetration mismatch') call assert_close(coupling_contact_pressure(1), 0.0_rk, & 'legacy restart contact pressure mismatch') call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed for restart 080 compat') end subroutine check_legacy_restart_read subroutine write_legacy_restart(source_name, target_name) character(len=*), intent(in) :: source_name character(len=*), intent(in) :: target_name character(len=2048) :: line integer :: ios open(unit=91, file=trim(source_name), status='old', action='read', iostat=ios) if (ios /= 0) call fail('legacy restart seed open failed') open(unit=92, file=trim(target_name), status='unknown', action='write', iostat=ios) if (ios /= 0) call fail('legacy restart target open failed') read(91, '(A)', iostat=ios) line if (ios /= 0) call fail('legacy restart seed header read failed') write(92, '(A)') 'SPHLITE_RESTART 0 8 1 STATE_0_8' do read(91, '(A)', iostat=ios) line if (ios /= 0) call fail('legacy restart coupling marker missing') if (trim(line) == 'SPHLITE_COUPLING_STATE') exit write(92, '(A)') trim(line) end do call write_legacy_coupling_block(92) close(unit=91) close(unit=92) end subroutine write_legacy_restart subroutine write_legacy_coupling_block(unit) integer, intent(in) :: unit integer :: region integer :: i write(unit, '(A)') 'SPHLITE_COUPLING_STATE' write(unit, *) coupling_region_count, coupling_region_disabled_count do region = 1, coupling_region_count write(unit, '(A)') trim(coupling_region_name(region)) write(unit, '(A)') trim(coupling_region_set_name(region)) write(unit, '(A)') trim(coupling_region_surface_name(region)) write(unit, '(A)') trim(coupling_region_material_name(region)) write(unit, '(A)') trim(coupling_region_section_name(region)) write(unit, *) coupling_region_type(region), coupling_region_export_mode(region), & coupling_region_import_mode(region), coupling_region_enabled(region), & coupling_region_phase_filter(region), coupling_region_material_filter(region), & coupling_region_section_filter(region), coupling_region_internal_id(region), & coupling_region_particle_count(region) if (coupling_region_particle_count(region) > 0) then write(unit, *) coupling_region_particles(1:coupling_region_particle_count(region), & region) end if end do write(unit, *) coupling_cumulative_impulse(:), coupling_momentum_exchange(:) write(unit, *) coupling_cumulative_work, coupling_energy_exchange write(unit, *) coupling_stale_load_count, coupling_skipped_particle_count, & coupling_nonfinite_import_count, coupling_imported_force_count write(unit, *) npart do i = 1, npart write(unit, *) coupling_external_force(:,i), coupling_motion_velocity(:,i), & coupling_motion_position(:,i), coupling_motion_flag(i), & coupling_motion_mode(i) end do end subroutine write_legacy_coupling_block subroutine assert_legacy_common_modes(distribution_mode, contact_state_mode) integer, intent(in) :: distribution_mode integer, intent(in) :: contact_state_mode if (distribution_mode /= SPH_COUPLING_DISTRIBUTION_UNIFORM) then call fail('legacy restart distribution mode mismatch') end if if (contact_state_mode /= SPH_COUPLING_CONTACT_STATE_NONE) then call fail('legacy restart contact state mode mismatch') end if end subroutine assert_legacy_common_modes subroutine scramble_coupling_state() coupling_external_force = -999.0_rk coupling_motion_velocity = -999.0_rk coupling_motion_position = -999.0_rk coupling_motion_flag = -999 coupling_motion_mode = -999 coupling_contact_flag = -999 coupling_contact_gap = -999.0_rk coupling_contact_penetration = -999.0_rk coupling_contact_pressure = -999.0_rk coupling_imported_force_count = -999 end subroutine scramble_coupling_state subroutine delete_file_if_exists(filename) character(len=*), intent(in) :: filename logical :: exists integer :: ios inquire(file=trim(filename), exist=exists) if (.not. exists) return open(unit=93, file=trim(filename), status='old', iostat=ios) if (ios == 0) close(unit=93, status='delete') end subroutine delete_file_if_exists subroutine assert_ok(ierr_value, message) integer, intent(in) :: ierr_value character(len=*), intent(in) :: message if (ierr_value /= SPH_SUCCESS) call fail(message) end subroutine assert_ok subroutine assert_close(actual, expected, message) real(rk), intent(in) :: actual real(rk), intent(in) :: expected character(len=*), intent(in) :: message if (abs(actual - expected) > 1.0e-12_rk) call fail(message) end subroutine assert_close subroutine fail(message) character(len=*), intent(in) :: message write(*, '(A)') trim(message) stop 1 end subroutine fail end program test_coupling_restart_080_compat