Finalise the diagnostics module, writing final timestep diagnostics and closing any open files
subroutine finish_gs2_diagnostics
use mp, only: proc0
use fields_arrays, only: phinew, bparnew
use gs2_io, only: nc_finish
use antenna, only: dump_ant_amp
use kt_grids, only: naky, ntheta0
use gs2_time, only: user_time
use le_grids, only: finish_weights
use unit_tests, only: debug_message
use diagnostics_configuration, only: diagnostics_config
use diagnostics_final_routines, only: do_write_final_fields, do_write_kpar
use diagnostics_final_routines, only: do_write_final_epar, do_write_final_db
use diagnostics_final_routines, only: do_write_final_antot, do_write_final_moments
use diagnostics_final_routines, only: do_write_gs
use diagnostics_velocity_space, only: collision_error
implicit none
complex, dimension (ntheta0, naky) :: phi0
call debug_message(3, 'gs2_diagnostics::finish_gs2_diagnostics &
& starting')
if (write_gyx) call do_write_fyx (yxdist_unit, phinew, bparnew)
if (write_g) call do_write_f (dist_unit)
if (write_cerr) call collision_error (cres_unit, phinew, bparnew)
if (write_verr .and. proc0) call finish_weights
! Close some of the open ascii output files
call close_files
if (proc0) then
if (write_eigenfunc) call do_write_eigenfunc(get_netcdf_file_id(), write_ascii, phi0)
if (write_final_fields) call do_write_final_fields(write_ascii, &
file_id=get_netcdf_file_id())
if (write_kpar) call do_write_kpar(write_ascii)
if (write_final_epar) call do_write_final_epar(write_ascii, &
file_id=get_netcdf_file_id())
! definition here assumes we are not using wstar_units
if (write_final_db) call do_write_final_db(write_ascii)
end if
!Note pass in phase factor phi0 which may not be initialised
!this is ok as phi0 will be set in routine if not already set
if (write_final_moments) call do_write_final_moments(phi0, &
use_normalisation=write_eigenfunc, write_text=write_ascii, &
file_id=get_netcdf_file_id())
if (write_final_antot) call do_write_final_antot(write_ascii, &
file_id=get_netcdf_file_id())
call save_restart_dist_fn(save_for_restart, save_distfn, &
save_glo_info_and_grids=save_glo_info_and_grids, &
save_velocities=save_velocities, &
user_time=user_time)
!Finalise the netcdf file
call nc_finish
if (proc0) call dump_ant_amp
if (write_gs) call do_write_gs(write_ascii)
if (proc0) call do_write_geom
!Now tidy up
call deallocate_arrays
wtmp_old = 0. ; nout = 1 ; nout_movie = 1 ; nout_big = 1
initialized = .false.
call diagnostics_config%reset()
end subroutine finish_gs2_diagnostics