static_data_bnd_flux_s.f90 Source File


Contents


Source Code

submodule (static_data_m) static_data_bnd_flux_s
    !! Handles static data related with boundary flux
    implicit none

contains

    module subroutine static_bnd_flux_init(comm_handler, par_filepath, nf90_filepath)
        type(comm_handler_t), intent(in) :: comm_handler
        character(len=*), intent(in), optional :: par_filepath
        character(len=*), intent(in), optional :: nf90_filepath

        character(len=:), allocatable :: fpath
        logical :: write_to_netcdf =.false.
        integer :: idbg, nf90_stat, nf90_id, nf90_grpid, nf90_subgrpid
        character(len=3) :: plane_c
        character(len=:), allocatable :: nf90_fpath

        type(parallel_map_t) :: map_bilin

        if (is_rank_info_writer) then
            write(get_stdout(),*)''
            write(get_stdout(),*)repeat('-',80)
            write(get_stdout(),*)'Initializing static data: bnd_flux'
        endif

        if (.not.equi_is_initialized) then
            call handle_error('equi must be initialized at bnd_flux initialization', &
                              GRILLIX_ERR_STATIC_DATA, __LINE__, __FILE__)
        endif
        if (.not.multigrid_is_initialized) then
            call handle_error('multigrid must be initialized at bnd_flux initialization', &
                              GRILLIX_ERR_STATIC_DATA, __LINE__, __FILE__)
        endif
        if (.not.parbnd_immersed_is_initialized) then
            call handle_error('parbnd_immersed must be initialized at bnd_flux initialization', &
                              GRILLIX_ERR_STATIC_DATA, __LINE__, __FILE__)
        endif

        ! Set debug output level
        if (enable_debug_output) then
            idbg = 1
        else
            idbg = 0
        endif

        if (present(par_filepath)) then
            fpath = par_filepath
        else
            fpath = static_data_parpath
        endif

        ! Select NetCDF file path and enable writing when required
        write(plane_c,'(I3.3)')comm_handler%get_plane()
        if (present(nf90_filepath)) then
            nf90_fpath = nf90_filepath
        else
            if (equi%is_axisymmetric()) then
                nf90_fpath = trim(static_data_dirpath)//'/bnd_flux.nc'
                if ((cntrl_build_bnd_flux == BUILD_AND_WRITE) .and. (is_rank_info_writer)) then
                    write_to_netcdf = .true.
                endif
            else
                nf90_fpath = trim(static_data_dirpath)//'/bnd_flux_plane'//plane_c//'.nc'
                if (cntrl_build_bnd_flux == BUILD_AND_WRITE) then
                    write_to_netcdf = .true.
                endif
            endif
        endif

        ! Associate staggered types
        if (equi%is_axisymmetric()) then
            parflux_utils_stag => parflux_utils_cano
            perp_bnd_flux_stag => perp_bnd_flux_cano
        else
            allocate(parflux_utils_stag_mem)
            parflux_utils_stag => parflux_utils_stag_mem
            allocate(perp_bnd_flux_stag_mem)
            perp_bnd_flux_stag => perp_bnd_flux_stag_mem
        endif

        select case(cntrl_build_bnd_flux)
            case(BUILD_NOWRITE, BUILD_AND_WRITE)
                if (is_rank_info_writer) then
                    write(get_stdout(),*)'Building bnd_flux from scratch'
                endif

                ! Use linear parallel map for computing parflux values to avoid Runge's phenomena
                call map_bilin%set_parameters(comm_handler, filename=fpath)
                call map_bilin%set_intorder(1)
                call map_bilin%set_xorder(0)
                call map_bilin%build(comm_handler, equi, mesh_cano, mesh_stag, dbgout=idbg)

                call parflux_utils_cano%build(comm_handler, mesh_cano, map_bilin, &
                        parbnd_immersed_cano, staggered=.false., pen_threshold=0.5_GP, dbgout=idbg)

                call perp_bnd_flux_cano%init(equi, mesh_cano, parbnd_immersed_cano, dbgout=idbg)

                if (.not.equi%is_axisymmetric()) then
                    call parflux_utils_stag%build(comm_handler, mesh_stag, map_bilin, &
                            parbnd_immersed_stag, staggered=.true., pen_threshold=0.5_GP, dbgout=idbg)

                    call perp_bnd_flux_stag%init(equi, mesh_stag, parbnd_immersed_stag, dbgout=idbg)
                endif

                bnd_flux_is_initialized = .true.

            case(READ)
                if (is_rank_info_writer) then
                    write(get_stdout(),*)'Reading bnd_flux data from file: ' // nf90_fpath
                endif
                nf90_stat = nf90_open(nf90_fpath, NF90_NETCDF4, nf90_id)
                call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                
                if (equi%is_axisymmetric()) then
                    nf90_stat = nf90_inq_grp_ncid(nf90_id, 'perp', nf90_grpid)
                    call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                    call perp_bnd_flux_cano%read_netcdf(nf90_grpid)
                    
                    nf90_stat = nf90_inq_grp_ncid(nf90_id, 'parallel', nf90_grpid)
                    call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                    call parflux_utils_cano%read_netcdf(nf90_grpid)
                else
                    nf90_stat = nf90_inq_grp_ncid(nf90_id, 'canonical', nf90_grpid)
                    call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                    nf90_stat = nf90_inq_grp_ncid(nf90_grpid, 'perp', nf90_subgrpid)
                    call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                    call perp_bnd_flux_cano%read_netcdf(nf90_subgrpid)
                    nf90_stat = nf90_inq_grp_ncid(nf90_grpid, 'parallel', nf90_subgrpid)
                    call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                    call parflux_utils_cano%read_netcdf(nf90_subgrpid)
                    
                    nf90_stat = nf90_inq_grp_ncid(nf90_id, 'staggered', nf90_grpid)
                    call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                    nf90_stat = nf90_inq_grp_ncid(nf90_grpid, 'perp', nf90_subgrpid)
                    call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                    call perp_bnd_flux_stag%read_netcdf(nf90_subgrpid)
                    nf90_stat = nf90_inq_grp_ncid(nf90_grpid, 'parallel', nf90_subgrpid)
                    call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                    call parflux_utils_stag%read_netcdf(nf90_subgrpid)
                endif

                nf90_stat = nf90_close(nf90_id)
                call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)

                bnd_flux_is_initialized = .true.

            case(NONE)
                if (is_rank_info_writer) then
                    write(get_stdout(),*) 'bnd_flux not built because it was not requested by the user.'
                endif
            case default
                call handle_error('cntrl_build_bnd_flux not valid', &
                                  GRILLIX_ERR_STATIC_DATA, __LINE__, __FILE__)
        end select

        if (write_to_netcdf) then
            if (is_rank_info_writer) then
                write(get_stdout(),*)'Writing bnd_flux to file (series): ' // nf90_fpath
            endif

            nf90_stat = nf90_create(nf90_fpath, NF90_NETCDF4, nf90_id)
            call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)

            if (equi%is_axisymmetric()) then
                nf90_stat = nf90_def_grp(nf90_id, 'perp', nf90_grpid)
                call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                call perp_bnd_flux_cano%write_netcdf(nf90_grpid)

                nf90_stat = nf90_def_grp(nf90_id, 'parallel', nf90_grpid)
                call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                call parflux_utils_cano%write_netcdf(nf90_grpid)
            else

                nf90_stat = nf90_def_grp(nf90_id, 'canonical', nf90_grpid)
                call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                nf90_stat = nf90_def_grp(nf90_grpid, 'perp', nf90_subgrpid)
                call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                call perp_bnd_flux_cano%write_netcdf(nf90_subgrpid)
                nf90_stat = nf90_def_grp(nf90_grpid, 'parallel', nf90_subgrpid)
                call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                call parflux_utils_cano%write_netcdf(nf90_subgrpid)

                nf90_stat = nf90_def_grp(nf90_id, 'staggered', nf90_grpid)
                call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                nf90_stat = nf90_def_grp(nf90_grpid, 'perp', nf90_subgrpid)
                call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)    
                call perp_bnd_flux_stag%write_netcdf(nf90_subgrpid)      
                nf90_stat = nf90_def_grp(nf90_grpid, 'parallel', nf90_subgrpid)
                call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)
                call parflux_utils_stag%write_netcdf(nf90_subgrpid)
            endif

            nf90_stat = nf90_close(nf90_id)
            call handle_error_netcdf(nf90_stat, __LINE__, __FILE__)

        endif

        if (is_rank_info_writer) then
            write(get_stdout(),*)repeat('-',80)
            write(get_stdout(),*)''
        endif
    
    end subroutine

end submodule