dyn_mpas_read_write_stream Subroutine

private subroutine dyn_mpas_read_write_stream(self, pio_file, stream_mode, stream_name)

Uses

    • mpas_pool_routines
    • pio
    • mpas_io_streams
    • mpas_stream_manager
    • mpas_derived_types
  • proc~~dyn_mpas_read_write_stream~~UsesGraph proc~dyn_mpas_read_write_stream mpas_dynamical_core_type%dyn_mpas_read_write_stream mpas_derived_types mpas_derived_types proc~dyn_mpas_read_write_stream->mpas_derived_types mpas_io_streams mpas_io_streams proc~dyn_mpas_read_write_stream->mpas_io_streams mpas_pool_routines mpas_pool_routines proc~dyn_mpas_read_write_stream->mpas_pool_routines mpas_stream_manager mpas_stream_manager proc~dyn_mpas_read_write_stream->mpas_stream_manager pio pio proc~dyn_mpas_read_write_stream->pio

In the context of MPAS, the concept of a "pool" resembles a group of (related) variables, while the concept of a "stream" resembles a file. This subroutine reads or writes an MPAS stream. It provides the mechanism for CAM-SIMA to input/output data to/from MPAS dynamical core. Analogous to the {read,write}_stream subroutines in MPAS stream manager.

Type Bound

mpas_dynamical_core_type

Arguments

Type IntentOptional Attributes Name
class(mpas_dynamical_core_type), intent(in) :: self
type(file_desc_t), intent(in), pointer :: pio_file
character(len=*), intent(in) :: stream_mode
character(len=*), intent(in) :: stream_name

Calls

proc~~dyn_mpas_read_write_stream~~CallsGraph proc~dyn_mpas_read_write_stream mpas_dynamical_core_type%dyn_mpas_read_write_stream mpas_closestream mpas_closestream proc~dyn_mpas_read_write_stream->mpas_closestream mpas_pool_destroy_pool mpas_pool_destroy_pool proc~dyn_mpas_read_write_stream->mpas_pool_destroy_pool mpas_readstream mpas_readstream proc~dyn_mpas_read_write_stream->mpas_readstream mpas_writestream mpas_writestream proc~dyn_mpas_read_write_stream->mpas_writestream postread_reindex postread_reindex proc~dyn_mpas_read_write_stream->postread_reindex postwrite_reindex postwrite_reindex proc~dyn_mpas_read_write_stream->postwrite_reindex prewrite_reindex prewrite_reindex proc~dyn_mpas_read_write_stream->prewrite_reindex proc~dyn_mpas_debug_print mpas_dynamical_core_type%dyn_mpas_debug_print proc~dyn_mpas_read_write_stream->proc~dyn_mpas_debug_print proc~dyn_mpas_exchange_halo mpas_dynamical_core_type%dyn_mpas_exchange_halo proc~dyn_mpas_read_write_stream->proc~dyn_mpas_exchange_halo proc~dyn_mpas_init_stream_with_pool mpas_dynamical_core_type%dyn_mpas_init_stream_with_pool proc~dyn_mpas_read_write_stream->proc~dyn_mpas_init_stream_with_pool proc~parse_stream_name parse_stream_name proc~dyn_mpas_read_write_stream->proc~parse_stream_name proc~stringify stringify proc~dyn_mpas_debug_print->proc~stringify proc~dyn_mpas_exchange_halo->proc~dyn_mpas_debug_print mpas_dmpar_exch_halo_field mpas_dmpar_exch_halo_field proc~dyn_mpas_exchange_halo->mpas_dmpar_exch_halo_field mpas_pool_get_field mpas_pool_get_field proc~dyn_mpas_exchange_halo->mpas_pool_get_field mpas_pool_get_field_info mpas_pool_get_field_info proc~dyn_mpas_exchange_halo->mpas_pool_get_field_info proc~dyn_mpas_exchange_halo->proc~stringify proc~dyn_mpas_init_stream_with_pool->proc~dyn_mpas_debug_print proc~dyn_mpas_init_stream_with_pool->proc~parse_stream_name mpas_createstream mpas_createstream proc~dyn_mpas_init_stream_with_pool->mpas_createstream mpas_pool_add_config mpas_pool_add_config proc~dyn_mpas_init_stream_with_pool->mpas_pool_add_config mpas_pool_create_pool mpas_pool_create_pool proc~dyn_mpas_init_stream_with_pool->mpas_pool_create_pool proc~dyn_mpas_init_stream_with_pool->mpas_pool_get_field mpas_streamaddfield mpas_streamaddfield proc~dyn_mpas_init_stream_with_pool->mpas_streamaddfield none~add_stream_attribute add_stream_attribute proc~dyn_mpas_init_stream_with_pool->none~add_stream_attribute pio_file_is_open pio_file_is_open proc~dyn_mpas_init_stream_with_pool->pio_file_is_open proc~dyn_mpas_check_variable_status mpas_dynamical_core_type%dyn_mpas_check_variable_status proc~dyn_mpas_init_stream_with_pool->proc~dyn_mpas_check_variable_status proc~dyn_mpas_init_stream_with_pool->proc~stringify proc~index_unique index_unique proc~parse_stream_name->proc~index_unique proc~parse_stream_name_fragment parse_stream_name_fragment proc~parse_stream_name->proc~parse_stream_name_fragment none~add_stream_attribute_0d add_stream_attribute_0d none~add_stream_attribute->none~add_stream_attribute_0d none~add_stream_attribute_1d add_stream_attribute_1d none~add_stream_attribute->none~add_stream_attribute_1d proc~dyn_mpas_check_variable_status->proc~dyn_mpas_debug_print proc~dyn_mpas_check_variable_status->mpas_pool_get_field proc~dyn_mpas_check_variable_status->pio_file_is_open proc~dyn_mpas_check_variable_status->proc~stringify constituentnames constituentnames proc~dyn_mpas_check_variable_status->constituentnames pio_inq_varid pio_inq_varid proc~dyn_mpas_check_variable_status->pio_inq_varid pio_inq_varndims pio_inq_varndims proc~dyn_mpas_check_variable_status->pio_inq_varndims pio_inq_vartype pio_inq_vartype proc~dyn_mpas_check_variable_status->pio_inq_vartype none~add_stream_attribute_0d->proc~dyn_mpas_debug_print mpas_writestreamatt mpas_writestreamatt none~add_stream_attribute_0d->mpas_writestreamatt none~add_stream_attribute_1d->proc~dyn_mpas_debug_print none~add_stream_attribute_1d->mpas_writestreamatt

Called by

proc~~dyn_mpas_read_write_stream~~CalledByGraph proc~dyn_mpas_read_write_stream mpas_dynamical_core_type%dyn_mpas_read_write_stream proc~dyn_init dyn_init proc~dyn_init->proc~dyn_mpas_read_write_stream proc~dyn_variable_dump dyn_variable_dump proc~dyn_variable_dump->proc~dyn_mpas_read_write_stream proc~model_grid_init model_grid_init proc~model_grid_init->proc~dyn_mpas_read_write_stream proc~dyn_final dyn_final proc~dyn_final->proc~dyn_variable_dump proc~stepon_final stepon_final proc~stepon_final->proc~dyn_final

Variables

Type Visibility Attributes Name Initial
integer, private :: i
integer, private :: ierr
type(mpas_pool_type), private, pointer :: mpas_pool
type(mpas_stream_type), private, pointer :: mpas_stream
character(len=*), private, parameter :: subname = 'dyn_mpas_subdriver::dyn_mpas_read_write_stream'
type(var_info_type), private, allocatable :: var_info_list(:)

Source Code

    subroutine dyn_mpas_read_write_stream(self, pio_file, stream_mode, stream_name)
        ! Module(s) from external libraries.
        use pio, only: file_desc_t
        ! Module(s) from MPAS.
        use mpas_derived_types, only: mpas_pool_type, mpas_stream_noerr, mpas_stream_type
        use mpas_io_streams, only: mpas_closestream, mpas_readstream, mpas_writestream
        use mpas_pool_routines, only: mpas_pool_destroy_pool
        use mpas_stream_manager, only: postread_reindex, prewrite_reindex, postwrite_reindex

        class(mpas_dynamical_core_type), intent(in) :: self
        type(file_desc_t), pointer, intent(in) :: pio_file
        character(*), intent(in) :: stream_mode
        character(*), intent(in) :: stream_name

        character(*), parameter :: subname = 'dyn_mpas_subdriver::dyn_mpas_read_write_stream'
        integer :: i, ierr
        type(mpas_pool_type), pointer :: mpas_pool
        type(mpas_stream_type), pointer :: mpas_stream
        type(var_info_type), allocatable :: var_info_list(:)

        call self % debug_print(log_level_debug, subname // ' entered')

        nullify(mpas_pool)
        nullify(mpas_stream)

        call self % debug_print(log_level_info, 'Initializing stream "' // trim(adjustl(stream_name)) // '"')

        call self % init_stream_with_pool(mpas_pool, mpas_stream, pio_file, stream_mode, stream_name)

        if (.not. associated(mpas_pool)) then
            call self % model_error('Failed to initialize stream "' // trim(adjustl(stream_name)) // '"', subname, __LINE__)
        end if

        if (.not. associated(mpas_stream)) then
            call self % model_error('Failed to initialize stream "' // trim(adjustl(stream_name)) // '"', subname, __LINE__)
        end if

        select case (trim(adjustl(stream_mode)))
            case ('r', 'read')
                call self % debug_print(log_level_info, 'Reading stream "' // trim(adjustl(stream_name)) // '"')

                call mpas_readstream(mpas_stream, 1, ierr=ierr)

                if (ierr /= mpas_stream_noerr) then
                    call self % model_error('Failed to read stream "' // trim(adjustl(stream_name)) // '"', subname, __LINE__)
                end if

                ! Exchange halo layers because new data have just been read.
                var_info_list = parse_stream_name(stream_name)

                do i = 1, size(var_info_list)
                    call self % exchange_halo(var_info_list(i) % name)
                end do

                ! For any connectivity arrays in this stream, convert global indexes to local indexes.
                call postread_reindex(self % domain_ptr % blocklist % allfields, self % domain_ptr % packages, &
                    mpas_pool, mpas_pool)
            case ('w', 'write')
                call self % debug_print(log_level_info, 'Writing stream "' // trim(adjustl(stream_name)) // '"')

                ! WARNING:
                ! The `{pre,post}write_reindex` subroutines are STATEFUL because they store information inside their module
                ! (i.e., module variables). They MUST be called in pairs, like below, to prevent undefined behaviors.

                ! For any connectivity arrays in this stream, temporarily convert local indexes to global indexes.
                call prewrite_reindex(self % domain_ptr % blocklist % allfields, self % domain_ptr % packages, &
                    mpas_pool, mpas_pool)

                call mpas_writestream(mpas_stream, 1, ierr=ierr)

                if (ierr /= mpas_stream_noerr) then
                    call self % model_error('Failed to write stream "' // trim(adjustl(stream_name)) // '"', subname, __LINE__)
                end if

                ! For any connectivity arrays in this stream, reset global indexes back to local indexes.
                call postwrite_reindex(self % domain_ptr % blocklist % allfields, mpas_pool)
            case default
                call self % model_error('Unsupported stream mode "' // trim(adjustl(stream_mode)) // '"', subname, __LINE__)
        end select

        call self % debug_print(log_level_info, 'Closing stream "' // trim(adjustl(stream_name)) // '"')

        call mpas_closestream(mpas_stream, ierr=ierr)

        if (ierr /= mpas_stream_noerr) then
            call self % model_error('Failed to close stream "' // trim(adjustl(stream_name)) // '"', subname, __LINE__)
        end if

        ! Deallocate temporary pointers to avoid memory leaks.
        call mpas_pool_destroy_pool(mpas_pool)
        nullify(mpas_pool)

        deallocate(mpas_stream)
        nullify(mpas_stream)

        call self % debug_print(log_level_debug, subname // ' completed')
    end subroutine dyn_mpas_read_write_stream