Files
SPHLite/src/output/sph_output_csv.f90
T
2026-07-03 09:37:57 +08:00

128 lines
5.4 KiB
Fortran

module sph_output_csv
use sph_precision, only: rk
use sph_error, only: SPH_SUCCESS, SPH_ERROR_ARGUMENT, SPH_ERROR_IO, sph_error_set
use sph_control, only: output_directory, output_interval, output_enabled
use sph_database, only: npart, particle_id_ext, particle_phase, particle_status, &
material_id, section_id, x, v, rho, pressure, temperature, h, &
water_mass, water_momentum, water_max_pressure, water_min_density, &
water_max_density, water_dt_cfl, water_dt_accel, water_dt_viscous, &
gas_particle_count, gas_mass, gas_volume, gas_center, gas_mean_pressure, &
gas_max_pressure, gas_equivalent_radius, gas_radius_rate, gas_dtcrit
implicit none
integer, parameter :: SPH_OUTPUT_UNIT = 41
contains
subroutine sph_output_set_directory(directory, ierr)
character(len=*), intent(in) :: directory
integer, intent(out) :: ierr
ierr = SPH_SUCCESS
if (len_trim(directory) <= 0) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'output directory must not be empty')
ierr = SPH_ERROR_ARGUMENT
return
end if
output_directory = directory
output_enabled = .true.
end subroutine sph_output_set_directory
subroutine sph_output_set_interval(interval, ierr)
integer, intent(in) :: interval
integer, intent(out) :: ierr
ierr = SPH_SUCCESS
if (interval <= 0) then
call sph_error_set(SPH_ERROR_ARGUMENT, 'output interval must be positive')
ierr = SPH_ERROR_ARGUMENT
return
end if
output_interval = interval
end subroutine sph_output_set_interval
subroutine sph_output_write_csv(filename, ierr)
character(len=*), intent(in) :: filename
integer, intent(out) :: ierr
integer :: ios
integer :: i
ierr = SPH_SUCCESS
open(unit=SPH_OUTPUT_UNIT, file=trim(filename), status='unknown', &
action='write', iostat=ios)
if (ios /= 0) then
call sph_error_set(SPH_ERROR_IO, 'failed to open CSV output file')
ierr = SPH_ERROR_IO
return
end if
write(SPH_OUTPUT_UNIT, '(A)') &
'id,phase,status,material,section,x,y,z,vx,vy,vz,rho,pressure,temperature,h'
do i = 1, npart
write(SPH_OUTPUT_UNIT, '(5(I0,","),6(ES24.16,","),4(ES24.16,:,","))') &
particle_id_ext(i), particle_phase(i), particle_status(i), &
material_id(i), section_id(i), x(1,i), x(2,i), x(3,i), &
v(1,i), v(2,i), v(3,i), rho(i), pressure(i), temperature(i), h(i)
end do
close(unit=SPH_OUTPUT_UNIT)
end subroutine sph_output_write_csv
subroutine sph_output_write_step(step, time, ierr)
integer, intent(in) :: step
real(rk), intent(in) :: time
integer, intent(out) :: ierr
character(len=1024) :: filename
write(filename, '(A,"/sphlite_",I8.8,".csv")') trim(output_directory), step
call sph_output_write_csv(filename, ierr)
if (ierr /= SPH_SUCCESS) return
write(filename, '(A,"/sphlite_",I8.8,"_summary.csv")') trim(output_directory), step
call sph_output_write_summary(filename, step, time, ierr)
end subroutine sph_output_write_step
subroutine sph_output_write_summary(filename, step, time, ierr)
character(len=*), intent(in) :: filename
integer, intent(in) :: step
real(rk), intent(in) :: time
integer, intent(out) :: ierr
integer :: ios
real(rk) :: dtcrit
ierr = SPH_SUCCESS
dtcrit = water_dt_cfl
if (water_dt_accel > 0.0_rk .and. water_dt_accel < dtcrit) dtcrit = water_dt_accel
if (water_dt_viscous > 0.0_rk .and. water_dt_viscous < dtcrit) dtcrit = water_dt_viscous
open(unit=SPH_OUTPUT_UNIT, file=trim(filename), status='unknown', &
action='write', iostat=ios)
if (ios /= 0) then
call sph_error_set(SPH_ERROR_IO, 'failed to open summary output file')
ierr = SPH_ERROR_IO
return
end if
write(SPH_OUTPUT_UNIT, '(A)') 'metric,value'
write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'step', step
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'time', time
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'water_mass', water_mass
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'water_momentum_x', water_momentum(1)
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'water_momentum_y', water_momentum(2)
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'water_momentum_z', water_momentum(3)
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'max_pressure', water_max_pressure
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'min_density', water_min_density
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'max_density', water_max_density
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'dtcrit', dtcrit
write(SPH_OUTPUT_UNIT, '(A,",",I0)') 'gas_particles', gas_particle_count
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_mass', gas_mass
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_volume', gas_volume
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_center_x', gas_center(1)
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_center_y', gas_center(2)
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_center_z', gas_center(3)
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_mean_pressure', gas_mean_pressure
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_max_pressure', gas_max_pressure
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_equivalent_radius', gas_equivalent_radius
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_radius_rate', gas_radius_rate
write(SPH_OUTPUT_UNIT, '(A,",",ES24.16)') 'gas_dtcrit', gas_dtcrit
close(unit=SPH_OUTPUT_UNIT)
end subroutine sph_output_write_summary
end module sph_output_csv