module INPUT

use VARIAVEIS

  implicit real *8 (a-h,o-z)

  parameter(n2=18)   

  parameter (ctet = 273.15d0)
 
  real*8,dimension(:,:,:),allocatable :: xl

  real*8,dimension(:,:),allocatable :: xm

  real*8,dimension(:),allocatable :: tp, t0, hum, rhofin_i, &
                                     cfin_i, rkfin_i, efin_i

  real*8,dimension(:),allocatable :: dx, dy, dz

  integer,dimension(:),allocatable :: nfin_i, ihp_i

      dimension tpout(5),tw(n2),pw(n2),pw2(n2)
      external fcn,rkqc
      common /prop/ rhoair,gban,cvair,rkair,alfa,pr,cpair,rkwall
      common /geom/ thick
      common /temp/ tpoute,tpoutw,tpouts,tpoutn,tpoutt, &
                    tground
      common /const/ ht2
      common /rad/ tpair,alf,epsi,sig,riavg,rhoal,esp,cal
      common /hwl/ a,b,c
      common /heat/ ct,cq
 
!     insulation thermal conductivity of the 5 walls
!     1-east; 2-west; 3-south; 4-north and 5-top
 
      common /sol2/ rkiso(5)  
      common /velair/ fanvelo,uhp
      common /rad1/ iwe,iww,iwn,iws
      common /mesh/ nel,nlx,nly,nlz,neleqp
      common /pert/ epsilon

      real*8 hum0,tol1,htime,tp0,ht3,tol,time,x_u,y_u,z_u
	  integer ntime,i_u,j_u,k_u,caso,fres
      character*120 nome_com, local_com, com_linha(5), &
                    nome_dados, nome_saida

      integer :: conta ! linhas escritas no arquivo ITERA

      integer maxit,isteady
      real*8 tol2,tol3

      integer iconv, irest
      real*8 fanrate, secarea

      real*8 tcpui,tcpuf

      real*8 tmin_ar, tmax_ar, dtmax_ar, &
             tmin_eq, tmax_eq, dtmax_eq, &
             wmin_ar, wmax_ar, dwmax_ar

contains


subroutine dados

  character*120 fora2

  fora2 = trim(adjustl(nome_dados))//'.man'

  open(7,file=fora2)

      read (7,*)
      read (7,*) a
      read (7,*) b
      read (7,*) c
      read (7,*) tp0
      read (7,*) neleqp
      read (7,*) nel

      nto = nel + 5

      if ( nto .ne. nto_a ) then

        deallocate ( xl,       xm      )
        deallocate ( tp,       t0,     hum,     rkeqp, &
                     rhoeqp,   ceqp,   qgeqp,   atc,   &
                     rhofin_i, cfin_i, rkfin_i, efin_i )
        deallocate ( ifhp,     itypel, nfin_i,  ihp_i  )

        allocate ( xl(nto,8,3), xm(nto,3) )
        allocate ( tp(nto),       t0(nto),     hum(nto),     rkeqp(nto), &
                   rhoeqp(nto),   ceqp(nto),   qgeqp(nto),   atc(nto),   &
                   rhofin_i(nto),	cfin_i(nto), rkfin_i(nto), efin_i(nto) )
        allocate ( ifhp(nto), itypel(nto), nfin_i(nto), ihp_i(nto) )

        nto_a = nto

      end if

      read (7,*) 
      read (7,*) 
      read (7,*) esp
      read (7,*) rhoal
      read (7,*) rkwall
      read (7,*) cal
      read (7,*) tground
      read (7,*) tpoute
      read (7,*) tpoutw
      read (7,*) tpouts
      read (7,*) tpoutn
      read (7,*) tpoutt

      read (7,*) 
      read (7,*) 
      read (7,*) thick
      read (7,*) rkiso(1)
      read (7,*) rkiso(2)
      read (7,*) rkiso(3)
      read (7,*) rkiso(4)
      read (7,*) rkiso(5)

      read (7,*) 
      read (7,*) 
      read (7,*) tpair
      read (7,*) hum0
      read (7,*) rhoair
      read (7,*) rkair
      read (7,*) alfa
      read (7,*) cpair
      read (7,*) cvair
      read (7,*) pr
      read (7,*) gban

      read (7,*) 
      read (7,*) 
      read (7,*) nlx
      read (7,*) nly
      read (7,*) nlz

      if ( nlx .ne. nlx_a ) then
        deallocate ( dx )
        allocate ( dx(nlx) )
        nlx_a = nlx
      end if

      if ( nly .ne. nly_a ) then
        deallocate ( dy )
        allocate ( dy(nly) )
        nly_a = nly
      end if

      if ( nlz .ne. nlz_a ) then
        deallocate ( dz )
        allocate ( dz(nlz) )
        nlz_a = nlz
      end if

      read (7,*) 
      read (7,*)
      read (7,*) riavg
      read (7,*) iwe
      read (7,*) iww
      read (7,*) iwn
      read (7,*) iws
      read (7,*) alf
      read (7,*) epsi

      read (7,*) 
      read (7,*)
      read (7,*) uhp

      read (7,*) 
      read (7,*)
      read (7,*) iconv
      read (7,*) fanrate
      read (7,*) secarea

      if ( iconv == 0 ) fanrate = -1.0d0

      read (7,*) 
      read (7,*) 
      read (7,*) htime
      read (7,*) ntime
      read (7,*) tol1
      read (7,*) tol
      read (7,*) ht2
      read (7,*) ht3
      read (7,*) maxit
      read (7,*) tol2
      read (7,*) tol3
      read (7,*) epsilon
      read (7,*) ct
      read (7,*) cq
      read (7,*) fres
      read (7,*) isteady
      read (7,*) irest

      read (7,*) 
      read (7,*) 
      read (7,*) nome_com
      read (7,*) local_com
      read (7,*) com_linha(1)
      read (7,*) com_linha(2)
      read (7,*) com_linha(3)
      read (7,*) com_linha(4)
      read (7,*) com_linha(5)

      close(7)

      tp0 = tp0 + ctet
      tpoute = tpoute + ctet
      tpoutw = tpoutw + ctet
      tpoutn = tpoutn + ctet
      tpouts = tpouts + ctet
      tpoutt = tpoutt + ctet
      tground = tground + ctet
      tpair = tpair + ctet
      hum0 = hum0 / 1.0d+2

      fora2 = trim(adjustl(nome_dados))//'.geo'

      open(7,file=fora2)

      read(7,*) (dx(i),i=1,nlx)         
      read(7,*) (dy(i),i=1,nly)  
      read(7,*) (dz(i),i=1,nlz) 

      xl(1,1,1)=0.d0
      xl(1,1,2)=0.d0
      xl(1,1,3)=0.d0
      iel=0
      do k=1,nlz
         if(k.ne.1) then
            xl(iel+1,1,1)=0.d0
            xl(iel+1,1,2)=0.d0
            xl(iel+1,1,3)=xl(iel-nlx*nly+1,1,3)+dz(k-1)
         endif
      do j=1,nly
         if(j.ne.1) then
            xl(iel+1,1,1)=xl(iel-nlx+1,1,1)
            xl(iel+1,1,2)=xl(iel-nlx+1,1,2)+dy(j-1)
            xl(iel+1,1,3)=xl(iel-nlx+1,1,3)
         endif
      do i=1,nlx
         if(i.ne.1) then
            xl(iel+i,1,1)=xl(iel+i-1,2,1)
            xl(iel+i,1,2)=xl(iel+i-1,2,2)
            xl(iel+i,1,3)=xl(iel+i-1,2,3)
         endif
         xl(iel+i,2,1)=xl(iel+i,1,1)+dx(i)
         xl(iel+i,2,2)=xl(iel+i,1,2)
         xl(iel+i,2,3)=xl(iel+i,1,3)
         xl(iel+i,3,1)=xl(iel+i,1,1)+dx(i) 
         xl(iel+i,3,2)=xl(iel+i,1,2)+dy(j) 
         xl(iel+i,3,3)=xl(iel+i,1,3)
         xl(iel+i,4,1)=xl(iel+i,1,1)
         xl(iel+i,4,2)=xl(iel+i,1,2)+dy(j) 
         xl(iel+i,4,3)=xl(iel+i,1,3)
         xl(iel+i,5,1)=xl(iel+i,1,1)
         xl(iel+i,5,2)=xl(iel+i,1,2)
         xl(iel+i,5,3)=xl(iel+i,1,3)+dz(k)
         xl(iel+i,6,1)=xl(iel+i,1,1)+dx(i)
         xl(iel+i,6,2)=xl(iel+i,1,2)
         xl(iel+i,6,3)=xl(iel+i,1,3)+dz(k)
         xl(iel+i,7,1)=xl(iel+i,1,1)+dx(i)
         xl(iel+i,7,2)=xl(iel+i,1,2)+dy(j)
         xl(iel+i,7,3)=xl(iel+i,1,3)+dz(k)
         xl(iel+i,8,1)=xl(iel+i,1,1)
         xl(iel+i,8,2)=xl(iel+i,1,2)+dy(j)
         xl(iel+i,8,3)=xl(iel+i,1,3)+dz(k)
      enddo
         iel=iel+nlx
      enddo
      enddo
      auxa=abs(xl(nel,7,1)-a)
      auxb=abs(xl(nel,7,2)-b)
      auxc=abs(xl(nel,7,3)-c)
      if(auxa.gt.1.d-10.and.auxb.gt.1.d-10.and.auxc.gt.1.d-10) then
         write(*,*)xl(nel,7,1),xl(nel,7,2), xl(nel,7,3)
         write(*,*)'mesh is not correct - review input file inpmesh'
         stop
         else
            a=xl(nel,7,1)
            b=xl(nel,7,2)
            c=xl(nel,7,3)
      endif

      rhoeqp=0.d0
      ceqp=0.d0
      qgeqp=0.d0
      rkeqp=0.d0
      itypel=0
      ifhp=0
      atc=0.0d0

      ihp_i = 0
      nfin_i = 0
      rhofin_i=0.d0
      cfin_i=0.d0
      rkfin_i=0.d0
      efin_i=0.d0

!     compute average air velocity due to the use of a fan
!     considering an average section area around the heat exchanger
!     side of the heat pipe

      fanvelo=fanrate/secarea

!     reading of equipment properties

!     ine is the element number which is equipment

      do i=1,neleqp
 
         read(7,*) ine, ihp
         
		 itypel(ine)=1

         ihp_i(ine) = ihp
         
		 if(ihp == 0) then

!     the equipment density may be evaluated experimentally
!     by weighing the equipment and dividing by its volume

!     likewise, the equipment thermal conductivity can be evaluated
!     as an equivalent conductivity, such as
!     rkeqp(ine) = sum_i(k_i*V_i)/V_eqp

            read(7,*) rhoeqp(ine),ceqp(ine),qgeqp(ine),rkeqp(ine), &
                      ifhp(ine)

         else

!     properties of the finned side of the heat pipe

               read(7,*) nfin,rhofin,cfin,rkfin,efin

               nfin_i(ine)   = nfin
               rhofin_i(ine) = rhofin
               cfin_i(ine)   = cfin
               rkfin_i(ine)  = rkfin
               efin_i(ine)   = efin

               wid=xl(ine,4,2)-xl(ine,1,2)
               hgt=xl(ine,5,3)-xl(ine,1,3)
               rlg=xl(ine,2,1)-xl(ine,1,1)
               afin=wid*hgt
               atc(ine)=2*nfin*afin
               vfin=afin*efin
               vtfin=nfin*vfin
               vol=hgt*wid*rlg
               volair=vol-vtfin
               rhoeqp(ine)=(rhoair*volair+rhofin*vtfin)/vol
               ceqp(ine)=(cvair*volair+cfin*vtfin)/vol
               rkeqp(ine)=(rkair*volair+rkfin*vtfin)/vol
         endif
      enddo

!     itypel(i) = 0(air), 1(equipament or heat pipe)

      close(7)

end subroutine dados


end module INPUT
