finish_gs2_diagnostics Subroutine

public subroutine finish_gs2_diagnostics(istep)

Finalise the diagnostics module, writing final timestep diagnostics and closing any open files

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: istep

The current (final) timestep


Contents


Source Code

  subroutine finish_gs2_diagnostics (istep)
    use mp, only: proc0
    use fields_arrays, only: phinew, bparnew
    use dist_fn, only: write_f, write_fyx, collision_error
    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_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
    implicit none
    !> The current (final) timestep
    integer, intent (in) :: istep

    complex, dimension (ntheta0, naky) :: phi0
    logical :: last = .true.

    call debug_message(3, 'gs2_diagnostics::finish_gs2_diagnostics &
         & starting')

    if (write_gyx) call write_fyx (phinew,bparnew,last)
    if (write_g) call write_f (last)
    if (write_cerr) call collision_error (phinew, bparnew, last)
    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) then
          call do_write_final_fields(write_ascii, write_netcdf=.true., file_id=get_netcdf_file_id())
       end if
       if (write_kpar) call do_write_kpar(write_ascii)
       if (write_final_epar) then
          call do_write_final_epar(write_ascii, write_netcdf=.true., file_id=get_netcdf_file_id())
       end if

       ! 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, write_netcdf=.true.,&
                                                         file_id=get_netcdf_file_id())

    if (write_final_antot) then
       call do_write_final_antot(write_ascii, write_netcdf=.true., file_id=get_netcdf_file_id())
    end if

    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 gs2_diagnostics_config%reset()
  end subroutine finish_gs2_diagnostics