program test_coupling_particle_export_080 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 use sph_database, only: pressure, SPH_PHASE_SOLID, SPH_STATUS_ACTIVE use sph_coupling_interface, only: sph_coupling_get_region_particle_count, & sph_coupling_export_region_particles, & SPH_COUPLING_PARTICLE_SCALAR_COUNT, & SPH_COUPLING_PARTICLE_SCALAR_MASS, & SPH_COUPLING_PARTICLE_SCALAR_VOLUME, & SPH_COUPLING_PARTICLE_SCALAR_DENSITY, & SPH_COUPLING_PARTICLE_SCALAR_PRESSURE, & SPH_COUPLING_PARTICLE_SCALAR_ENERGY, & SPH_COUPLING_PARTICLE_SCALAR_TEMPERATURE, & SPH_COUPLING_PARTICLE_SCALAR_H, & SPH_COUPLING_PARTICLE_VECTOR_COUNT, & SPH_COUPLING_PARTICLE_VECTOR_X, SPH_COUPLING_PARTICLE_VECTOR_V, & SPH_COUPLING_PARTICLE_VECTOR_A implicit none integer :: ierr call check_particle_export() call check_particle_export_errors() contains subroutine check_particle_export() integer :: count integer :: ext_ids(3) integer :: internal_ids(3) integer :: phases(3) integer :: statuses(3) integer :: materials(3) integer :: sections(3) real(rk) :: scalars(SPH_COUPLING_PARTICLE_SCALAR_COUNT,3) real(rk) :: vectors(SPH_NDIM,SPH_COUPLING_PARTICLE_VECTOR_COUNT,3) call build_contact_case() call sph_coupling_get_region_particle_count('CONTACT_PARTICLES', count, ierr) call assert_ok(ierr, 'particle region count failed') if (count /= 3) call fail('particle region count mismatch') call sph_coupling_export_region_particles('CONTACT_PARTICLES', 3, count, & ext_ids, internal_ids, phases, statuses, materials, sections, & scalars, vectors, ierr) call assert_ok(ierr, 'particle region export failed') if (count /= 3) call fail('particle export count mismatch') if (ext_ids(1) /= 1 .or. ext_ids(3) /= 3) call fail('external id order mismatch') if (internal_ids(1) /= 1 .or. internal_ids(3) /= 3) then call fail('internal id order mismatch') end if if (any(phases(1:3) /= SPH_PHASE_SOLID)) call fail('phase export mismatch') if (any(statuses(1:3) /= SPH_STATUS_ACTIVE)) call fail('status export mismatch') if (minval(materials(1:3)) <= 0) call fail('material export mismatch') if (minval(sections(1:3)) <= 0) call fail('section export mismatch') call assert_close(scalars(SPH_COUPLING_PARTICLE_SCALAR_MASS,1), 1.0_rk, & 'mass scalar mismatch') if (scalars(SPH_COUPLING_PARTICLE_SCALAR_VOLUME,1) <= 0.0_rk) then call fail('volume scalar mismatch') end if if (scalars(SPH_COUPLING_PARTICLE_SCALAR_DENSITY,1) <= 0.0_rk) then call fail('density scalar mismatch') end if call assert_close(scalars(SPH_COUPLING_PARTICLE_SCALAR_PRESSURE,1), & 0.0_rk, 'pressure scalar mismatch') if (scalars(SPH_COUPLING_PARTICLE_SCALAR_ENERGY,1) < 0.0_rk) then call fail('energy scalar mismatch') end if if (scalars(SPH_COUPLING_PARTICLE_SCALAR_TEMPERATURE,1) <= 0.0_rk) then call fail('temperature scalar mismatch') end if if (scalars(SPH_COUPLING_PARTICLE_SCALAR_H,1) <= 0.0_rk) then call fail('h scalar mismatch') end if call assert_close(vectors(1,SPH_COUPLING_PARTICLE_VECTOR_X,2), 0.1_rk, & 'position vector mismatch') call assert_close(maxval(abs(vectors(:,SPH_COUPLING_PARTICLE_VECTOR_V,1))), & 0.0_rk, 'velocity vector mismatch') call assert_close(maxval(abs(vectors(:,SPH_COUPLING_PARTICLE_VECTOR_A,1))), & 0.0_rk, 'acceleration vector mismatch') call sph_coupling_get_region_particle_count('CONTACT_DISABLED', count, ierr) call assert_ok(ierr, 'disabled particle count failed') if (count /= 0) call fail('disabled region returned particles') call sph_coupling_export_region_particles('CONTACT_DISABLED', 3, count, & ext_ids, internal_ids, phases, statuses, materials, sections, & scalars, vectors, ierr) call assert_ok(ierr, 'disabled particle export failed') if (count /= 0) call fail('disabled particle export count mismatch') call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed after particle export') end subroutine check_particle_export subroutine check_particle_export_errors() integer :: count integer :: ext_ids(3) integer :: internal_ids(3) integer :: phases(3) integer :: statuses(3) integer :: materials(3) integer :: sections(3) real(rk) :: scalars(SPH_COUPLING_PARTICLE_SCALAR_COUNT,3) real(rk) :: vectors(SPH_NDIM,SPH_COUPLING_PARTICLE_VECTOR_COUNT,3) call build_contact_case() call sph_coupling_get_region_particle_count('NOT_A_REGION', count, ierr) if (ierr == SPH_SUCCESS) call fail('invalid region count did not fail') call sph_coupling_export_region_particles('CONTACT_PARTICLES', 2, count, & ext_ids(1:2), internal_ids(1:2), phases(1:2), statuses(1:2), & materials(1:2), sections(1:2), scalars(:,1:2), & vectors(:,:,1:2), ierr) if (ierr == SPH_SUCCESS) call fail('small particle capacity did not fail') pressure(1) = huge(pressure(1)) call sph_coupling_export_region_particles('CONTACT_PARTICLES', 3, count, & ext_ids, internal_ids, phases, statuses, materials, sections, & scalars, vectors, ierr) if (ierr == SPH_SUCCESS) call fail('nonfinite particle export did not fail') call sph_finalize(ierr) call assert_ok(ierr, 'finalize failed after particle export errors') end subroutine check_particle_export_errors subroutine build_contact_case() call sph_initialize(ierr) call assert_ok(ierr, 'initialize failed for contact export') call sph_read_inp('tests/data/coupling_region_080.inp', ierr) call assert_ok(ierr, 'read failed for contact export') call sph_build_model(ierr) call assert_ok(ierr, 'build failed for contact export') end subroutine build_contact_case 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_particle_export_080