subroutine output_ensemble(send_n_particles_for_output,send_particles,&
send_payload,typefile)
use mod_global_parameters
integer, intent(in) :: send_n_particles_for_output
type(particle_t), dimension(send_n_particles_for_output),&
intent(in) :: send_particles
double precision, dimension(npayload,send_n_particles_for_output),&
intent(in) :: send_payload
character(len=*), intent(in) :: typefile
character(len=std_len) :: filename
type(particle_t), allocatable, dimension(:) :: receive_particles
double precision, allocatable, dimension(:,:) :: receive_payload
integer :: status(MPI_STATUS_SIZE)
integer,dimension(0:npe-1) :: receive_n_particles_for_output_from_ipe
integer :: ipe, ipart, nout
logical :: file_exists
receive_n_particles_for_output_from_ipe(:) = 0
call MPI_ALLGATHER(send_n_particles_for_output, 1, MPI_INTEGER,&
receive_n_particles_for_output_from_ipe, 1, MPI_INTEGER, icomm,&
ierrmpi)
! If there are no particles to be written, skip the output
if (sum(receive_n_particles_for_output_from_ipe(:)) == 0) return
if (mype > 0) then
call mpi_send_wrapper(send_particles,send_n_particles_for_output,type_particle,0,&
mype,icomm,ierrmpi)
call mpi_send_wrapper(send_payload,npayload*send_n_particles_for_output,&
MPI_DOUBLE_PRECISION,0,mype,icomm,ierrmpi)
else
! Create file and write header
if(typefile=='destroy') then ! Destroyed file
write(filename,"(a,a,i6.6,a)") trim(base_filename) // '_',&
trim(typefile) // '.csv'
n_output_destroyed= n_output_destroyed + 1
inquire(file=filename, exist=file_exists)
if (.not. file_exists) then
open(unit=unitparticles, file=filename)
write(unitparticles,"(a)") trim(csv_header)
else
open(unit=unitparticles, file=filename, access='sequential',&
position='append')
end if
else ! Ensemble file
write(filename,"(a,a,i6.6,a)") trim(base_filename) // '_',&
trim(typefile) // '_', nint(t_next_output/dtsave_particles),'.csv'
open(unit=unitparticles,file=filename)
write(unitparticles,"(a)") trim(csv_header)
end if
! Write own particles
do ipart=1,send_n_particles_for_output
call output_particle(send_particles(ipart),send_payload(1:npayload,&
ipart),0,unitparticles)
end do
! Write particles from other tasks
do ipe=1,npe-1
allocate(receive_particles(1:receive_n_particles_for_output_from_ipe(&
ipe)))
allocate(receive_payload(1:npayload,&
1:receive_n_particles_for_output_from_ipe(ipe)))
call mpi_recv_wrapper(receive_particles&
,receive_n_particles_for_output_from_ipe(ipe),type_particle,ipe,ipe,&
icomm,status,ierrmpi)
call mpi_recv_wrapper(receive_payload&
,npayload*receive_n_particles_for_output_from_ipe(ipe),&
MPI_DOUBLE_PRECISION,ipe,ipe,icomm,status,ierrmpi)
do ipart=1,receive_n_particles_for_output_from_ipe(ipe)
call output_particle(receive_particles(ipart),&
receive_payload(1:npayload,ipart),ipe,unitparticles)
end do ! ipart
deallocate(receive_particles)
deallocate(receive_payload)
end do ! ipe
close(unitparticles)
end if
end subroutine output_ensemble