read_in_refs Subroutine

private subroutine read_in_refs(filename, nRead)

Uses

Arguments

Type IntentOptional Attributes Name
character(len=255), intent(in) :: filename
integer, intent(out) :: nRead

Contents

Source Code


Source Code

    subroutine read_in_refs(filename, nRead)
        use util_mod, only: get_free_unit
        implicit none
        integer, intent(out) :: nRead
        character(255), intent(in) :: filename
        integer :: iunit, i, stat
        integer(n_int) :: tmp(0:NIfTot)
        logical :: exists
        character(*), parameter :: this_routine = "read_in_refs"

        ! All procs read in the file, as all need all references
        inquire (file=trim(filename), exist=exists)
        if (.not. exists) call stop_all(this_routine, "No "//trim(filename)//" file detected.")
        iunit = get_free_unit()

        ! If nRefs == 1, we set it to the number of references in the file
        if (nRefs == 1) then
            ! Therefore, we scan the file once
            open(iunit, file=trim(filename), status='old')
            nRefs = 0
            do
                read(iunit, *, iostat=stat) tmp
                if (stat < 0) exit
                nRefs = nRefs + 1
            end do
            close(iunit)

            ! And allocate the ilutRefAdi accordingly
            call reallocate_ilutRefAdi(nRefs)
        end if

        open(iunit, file=trim(filename), status='old')

        ! Read in at most nRefs references
        nRead = 0
        do i = 1, nRefs
            ! Note that read-in references always have precedence over generated references
            read(iunit, *, iostat=stat) ilutRefAdi(:, i)
            ! If there are no more dets to be read, exit
            if (stat < 0) exit
            ! If we successfully read, log it
            nRead = nRead + 1
        end do

        close(iunit)
    end subroutine read_in_refs