module RESULTADOS


use CALCULOS

!use dflib
use msflib

!---------------------------------------------------------------

implicit none

!---------------------------------------------------------------

contains

!-------------------------------------------------

  subroutine escreve 

     call grafico_ordem_aparente ( head(i), pa(i,:) )

     call grafico_E_U ( head(i), Ud(i,:), Un(i,:),  Ufp(i,:), &
                        En(i,:), Tn(i,:), psi(i,:), pa(i,:),  &
                                 r(i,:),  lam(i,:)  )

     call grafico_fator_U ( head(i), Ud(i,:), Un(i,:), Ufp(i,:), &
                            En(i,:) )

  end subroutine escreve

!-------------------------------------------------

  subroutine grafico_ordem_aparente ( head, pa )

    real*8,intent(in),dimension(ni) :: pa ! ordem aparente de Tn

    character*80,intent(in) :: head ! título da variável
    integer :: k, ji ! auxiliar

    open(7,file='arq1.dat')

    ji = n1

    do j = ji, ni, dn
       write(7,2) j, pa(j)
    end do

    2 format(i8,1pe15.6)

    close(7)

    open(7,file='ordem.gnu')
    do k = 1, 6
       read(7,*)
    end do
    label = "'"//trim(head)//", "//trim(title)//" "//trim(dia)//"'"
    write(7,1) label
    1 format("set title ",a137,/, &
             'replot')
    close(7)

    dos = systemqq('wgnuplot ordem.gnu')

  end subroutine grafico_ordem_aparente

!-------------------------------------------------

  subroutine grafico_fator_U ( head, Ud, Un, Ufp, En )

    real*8,intent(in),dimension(ni) :: Ud  ! incerteza delta de Tn
    real*8,intent(in),dimension(ni) :: Un  ! incerteza de Tn
    real*8,intent(in),dimension(ni) :: Ufp ! incerteza de Tn
    real*8,intent(in),dimension(ni) :: En  ! erro de Tn

    character*80,intent(in) :: head ! título da variável

    real*8  :: f1, f2, f3 ! auxiliar
    integer :: k, ji, j2  ! auxiliar

    open( 7,file='arq1.dat')
    open( 8,file='arq2.dat')
    open( 9,file='arq3.dat')

    write(10,1)
    1 format(/,t1,'Razão entre Incerteza e Erro - 1',/, &
               t1,'Iteração',t12,'Ud / En',t27,'Un / En',t42,'Ufp / En')

    j2 = n1 + 2*dn

    ji = n1

    do j = ji, ni, dn
       f1 = 0.0d0
       f2 = 0.0d0
       f3 = 0.0d0
       If ( En(j) /= 0.0d0 ) then
          f1 = Ud(j)/En(j)
          f2 = Un(j)/En(j)
          f3 = dabs(Ufp(j)/En(j))
       end if
       write( 7,2) j, f1
       write( 8,2) j, f2
       write( 9,2) j, f3
       write(10,2) j, f1-1.0d0, f2-1.0d0, f3-1.0d0
    end do

    2 format(i8,3(1pe15.6))

    close(7)
    close(8)
    close(9)

    open(7,file='fator.gnu')
    do k = 1, 9
       read(7,*)
    end do
    label = "'"//trim(head)//", "//trim(title)//" "//trim(dia)//"'"
    write(7,3) label
    3 format("set title ",a137,/, &
             'replot')
    close(7)

    dos = systemqq('wgnuplot fator.gnu')

  end subroutine grafico_fator_U

!-------------------------------------------------

  subroutine grafico_E_U ( head, Ud, Un, Ufp, En, Tn, &
                           psi,  pa, r,  lam  )

    real*8,intent(in),dimension(ni) :: Ud ! incerteza delta de Tn
    real*8,intent(in),dimension(ni) :: Un ! incerteza de Tn
    real*8,intent(in),dimension(ni) :: En ! erro de Tn
    real*8,intent(in),dimension(ni) :: Tn ! solução numérica
    real*8,intent(in),dimension(ni) :: psi ! razão de convergência de Tn
    real*8,intent(in),dimension(ni) :: pa  ! ordem aparente de Tn
    real*8,intent(in),dimension(ni) :: lam ! fator lambda do Ufp
    real*8,intent(in),dimension(ni) :: r   ! fator r do Ufp
    real*8,intent(in),dimension(ni) :: Ufp ! incerteza de Tn

    character*80,intent(in) :: head ! título da variável

    integer :: k, ji, j2, j3 ! auxiliar

    open( 7,file='arq1.dat')
    open( 8,file='arq2.dat')
    open( 9,file='arq3.dat')
    open(11,file='arq4.dat')

    write(10,1)
    1 format(/,t1,'Iteração',t12,'Tn',t27,'En',t42,'psi', &
                             t57,'pa',t72,'lambda',t87,'r')

    j2 = n1 + dn
    j3 = n1 + 2*dn

    ji = n1
    do j = ji, ni, dn
       write(10,2) j, Tn(j), En(j), psi(j), pa(j), lam(j), r(j)
       write( 7,2) j, dabs(En(j))
       write( 8,2) j, dabs(Ud(j))
       write( 9,2) j, dabs(Un(j))
       write(11,2) j, dabs(Ufp(j))
    end do

    2 format(i8,6(1pe15.6))

    close(7)
    close(8)
    close(9)
    close(11)

    open(7,file='erro.gnu')
    do k = 1, 10
       read(7,*)
    end do
    label = "'"//trim(head)//", "//trim(title)//" "//trim(dia)//"'"
    write(7,3) label
    3 format("set title ",a137,/, &
             'replot')
    close(7)

    dos = systemqq('wgnuplot erro.gnu')

  end subroutine grafico_E_U

!-------------------------------------------------

end module RESULTADOS