!
!  coefficients_init.f90
!
      subroutine coefficients_init(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(*)
!
      integer :: i,j,icvs,jcvs,kcvs,length,mflength,np,npmax,nzone,ncell
      character (len=80) :: cval,zone,cell
      character (len=4) :: ctype
      integer :: ptr,pnl,pnlx,pnm,pnmx,pnn,pnnx,pzn,pcl
!
! 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)
!
          cval = trim(cell)//'/icvs'         
          call get_int_value('physics',cval,icvs,izp,ierr)
!
          cval = trim(cell)//'/jcvs'         
          call get_int_value('physics',cval,jcvs,izp,ierr)
!
          cval = trim(cell)//'/kcvs'         
          call get_int_value('physics',cval,kcvs,izp,ierr)
!
          call get_next_ptr('coef','real',ptr,ierr) 
          length = icvs*jcvs*kcvs
!
! Discrete equation coefficients       
          cval = trim(cell)//'/energy0'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          ptr = ptr + length
          cval = trim(cell)//'/energy'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          ptr = ptr + length
          cval = trim(cell)//'/energyc'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          ptr = ptr + length      
          cval = trim(cell)//'/ap'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          ptr = ptr + length
          cval = trim(cell)//'/ae'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          ptr = ptr + length
          cval = trim(cell)//'/aw'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          ptr = ptr + length
          cval = trim(cell)//'/an'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          ptr = ptr + length
          cval = trim(cell)//'/as'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          ptr = ptr + length
          cval = trim(cell)//'/at'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          ptr = ptr + length
          cval = trim(cell)//'/ab'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          ptr = ptr + length
!          
          cval = trim(cell)//'/b'           
          call put_mem('coef',cval,'real',ptr,length,ierr)                                   
          call set_a_0(rz(ptr),length)          
          ptr = ptr + length
          cval = trim(cell)//'/r'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          call set_a_0(rz(ptr),length)
          ptr = ptr + length
          cval = trim(cell)//'/rc'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          call set_a_0(rz(ptr),length)
!
! Reusable work space
          ptr = ptr + length  
          cval = trim(cell)//'/wrk1'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          ptr = ptr + length
          cval = trim(cell)//'/wrk2'           
          call put_mem('coef',cval,'real',ptr,length,ierr)
          ptr = ptr + length
          cval = trim(cell)//'/wrk3'           
          call put_mem('coef',cval,'real',ptr,length,ierr)       
!
! Face mass flow variables
          ptr = ptr + length
          mflength = (icvs+1)*(jcvs+1)*(kcvs+1)
          cval = trim(cell)//'/mass/massfl'           
          call put_mem('coef',cval,'real',ptr,mflength,ierr)       
          call set_a_0(rz(ptr),mflength)
!
! MG solver coefficient space
          cval = trim(cell)//'/nl' 
          call get_mem('physics',cval,ctype,pnl,npmax,ierr)
          cval = trim(cell)//'/nlx' 
          call get_mem('physics',cval,ctype,pnlx,npmax,ierr)        
          cval = trim(cell)//'/nm' 
          call get_mem('physics',cval,ctype,pnm,npmax,ierr)
          cval = trim(cell)//'/nmx' 
          call get_mem('physics',cval,ctype,pnmx,npmax,ierr) 
          cval = trim(cell)//'/nn' 
          call get_mem('physics',cval,ctype,pnn,npmax,ierr)
          cval = trim(cell)//'/nnx' 
          call get_mem('physics',cval,ctype,pnnx,npmax,ierr) 
!
          cval = trim(cell)//'/np'
          call get_int_value('physics',cval,np,izp,ierr)
          call mg_init(cell,izp(pnl),izp(pnlx),izp(pnm),izp(pnmx), &
                       izp(pnn),izp(pnnx),np,rz,iz,dz,cz,rzp,izp,czp,ierr)        
!
! Property coefficients
          call get_next_ptr('coef','real',ptr,ierr) 
          length = icvs*jcvs*kcvs
          cval = trim(cell)//'/props/tc0'          
          call put_mem('coef',cval,'real',ptr,length,ierr)
!           
          ptr = ptr + length
          cval = trim(cell)//'/props/cp0'  
          call put_mem('coef',cval,'real',ptr,length,ierr)
!
          ptr = ptr + length
          cval = trim(cell)//'/props/rho0'  
          call put_mem('coef',cval,'real',ptr,length,ierr)
!
        enddo
      enddo
!       
      ierr = 0
!
      end subroutine coefficients_init     
