!
!  cell_input.f90
!
      subroutine cell_input(rz,iz,dz,cz,rzp,izp,czp,ierr)      
!
      implicit none
      real :: rzp(*),rz(*)
      integer :: izp(*),iz(*),ierr
      character (len=*) :: czp(*),cz(*)
      real (selected_int_kind (5)) :: dz(*)
      character (len=80) :: cval,ctype,zone,cell
!
      real :: xlen,ylen,zlen
      integer :: i,j,icvs,jcvs,kcvs,length,vlength,anlength
      integer :: nzone,ncell,norig
      integer :: px,py,pz,pxc,pyc,pzc,pv,pan,pzn,pcl,porig
!
! Zone list       
      cval = '/zone_list'
      call get_mem('physics',cval,ctype,pzn,nzone,ierr)
!
      do i = 1, nzone
!
! Loop over cells in a zone
        zone = czp(pzn + i -1)        
        cval = '/'//trim(zone)//'/cell_list' 
        call get_mem('physics',cval,ctype,pcl,ncell,ierr) 
!
        do j = 1, ncell
!	
          cell = '/'//trim(zone)//'/'//czp(pcl + j -1)
!
! Mesh data for cells
          cval = trim(cell)//'/type'
          call get_char_value('physics',cval,ctype,czp,ierr)
!       
          if (ctype(1:8) == 'internal') then
            cval = trim(cell)//'/internal/origin'  
            call get_mem('physics',cval,ctype,porig,norig,ierr)
!
            cval = trim(cell)//'/internal/xlength'                
            call get_real_value('physics',cval,xlen,rzp,ierr)
!
            cval = trim(cell)//'/icvs'         
            call get_int_value('physics',cval,icvs,izp,ierr)
!
            cval = trim(cell)//'/internal/ylength'                
            call get_real_value('physics',cval,ylen,rzp,ierr)
!
            cval = trim(cell)//'/jcvs'         
            call get_int_value('physics',cval,jcvs,izp,ierr)
!
            cval = trim(cell)//'/internal/zlength'                
            call get_real_value('physics',cval,zlen,rzp,ierr)
!
            cval = trim(cell)//'/kcvs'         
            call get_int_value('physics',cval,kcvs,izp,ierr)
!
            call get_next_ptr('cell','real',px,ierr) 
            length = (icvs+1)*(jcvs+1)*(kcvs+1)
            cval = trim(cell)//'/mesh/x'           
            call put_mem('cell',cval,'real',px,length,ierr)
!
            py = px + length
            cval = trim(cell)//'/mesh/y'          
            call put_mem('cell',cval,'real',py,length,ierr)
!           
            pz = py + length
            cval = trim(cell)//'/mesh/z'  
            call put_mem('cell',cval,'real',pz,length,ierr)
!         
            call generate_grid(rzp(porig),rz(px),rz(py),rz(pz),xlen,ylen,zlen, & 
                               icvs+1,jcvs+1,kcvs+1,rz,iz,dz,cz,ierr) 
!
            call get_next_ptr('cell','real',pxc,ierr) 
            length = icvs*jcvs*kcvs
            cval = trim(cell)//'/mesh/xc'           
            call put_mem('cell',cval,'real',pxc,length,ierr)
!
            pyc = pxc + length
            cval = trim(cell)//'/mesh/yc'          
            call put_mem('cell',cval,'real',pyc,length,ierr)
!           
            pzc = pyc + length
            cval = trim(cell)//'/mesh/zc'  
            call put_mem('cell',cval,'real',pzc,length,ierr)
!
            call vertex_avg(rz(px),rz(pxc),icvs,jcvs,kcvs) 
            call vertex_avg(rz(py),rz(pyc),icvs,jcvs,kcvs) 
            call vertex_avg(rz(pz),rz(pzc),icvs,jcvs,kcvs)
!
! CV volume calculation
            call get_next_ptr('cell','real',pv,ierr) 
            vlength = icvs*jcvs*kcvs         
            cval = trim(cell)//'/mesh/vol'  
            call put_mem('cell',cval,'real',pv,vlength,ierr)
!
            call cal_vol(rz(px),rz(py),rz(pz),rz(pv), & 
                         icvs+1,jcvs+1,kcvs+1,rz,iz,dz,cz,ierr)          
!
! CV face outward area normals (Ax,Ay,Az) 
            pan = pv + vlength
            anlength = (icvs+1)*(jcvs+1)*(kcvs+1)*3          
            cval = trim(cell)//'/mesh/anorm'  
            call put_mem('cell',cval,'real',pan,anlength,ierr)
!
            call cal_anorm(rz(px),rz(py),rz(pz),rz(pan),&
                        icvs+1,jcvs+1,kcvs+1,rz,iz,dz,cz,ierr) 
          else
            print *,'Error in cell input' 
            stop  
          endif
        enddo
      enddo   
!
      ierr = 0
!
      end subroutine cell_input                                        