!
!  mg_init.f90
!
       subroutine mg_init(cell,nl,nlx,nm,nmx,nn,nnx,np, &
                          rz,iz,dz,cz,rzp,izp,czp,ierr)      
!
       implicit none
       real :: rzp(*),rz(*)
       integer :: izp(*),iz(*)
       character (len=*) :: czp(*),cz(*)
       real (selected_int_kind (5)) :: dz(*)       
!       
       character (len=*) :: cell
       integer :: np,ierr  
       integer :: nl(np),nlx(np),nm(np),nmx(np),nn(np),nnx(np)
!
       integer :: p,length
       integer, dimension(1:10,1:3) :: lmn
       character (len=80) :: cval,cbase
       character (len=2), dimension(1:10) :: cnum
       data cnum/'1 ','2 ','3 ','4 ','5 ','6 ','7 ','8 ','9 ','10'/
       integer :: ptr,pti
!
       do p=1,np
         lmn(p,1)= nl(p)
         if (nlx(p) > 0) lmn(p,1)=lmn(p,1)+1    
         lmn(p,2)= nm(p)
         if (nmx(p) > 0) lmn(p,2)=lmn(p,2)+1
         lmn(p,3)= nn(p)
         if (nnx(p) > 0) lmn(p,3)=lmn(p,3)+1
       enddo
!
       call get_next_ptr('coef','real',ptr,ierr)
       call get_next_ptr('coef','int',pti,ierr)
!
       do p = 1,np
         length = lmn(p,1)*lmn(p,2)*lmn(p,3)
         cbase= trim(cell)//'/level_'//cnum(p)
         cval = trim(cbase)//'/ap' 
         call put_mem('coef',cval,'real',ptr,length,ierr)
         ptr = ptr + length
         cval =trim(cbase)//'/ae' 
         call put_mem('coef',cval,'real',ptr,length,ierr)
         ptr = ptr + length
         cval = trim(cbase)//'/aw' 
         call put_mem('coef',cval,'real',ptr,length,ierr)
         ptr = ptr + length
         cval = trim(cbase)//'/an' 
         call put_mem('coef',cval,'real',ptr,length,ierr)
         ptr = ptr + length
         cval = trim(cbase)//'/as' 
         call put_mem('coef',cval,'real',ptr,length,ierr)
         ptr = ptr + length
         cval = trim(cbase)//'/at' 
         call put_mem('coef',cval,'real',ptr,length,ierr)
         ptr = ptr + length
         cval = trim(cbase)//'/ab' 
         call put_mem('coef',cval,'real',ptr,length,ierr)
         ptr = ptr + length
         cval = trim(cbase)//'/b' 
         call put_mem('coef',cval,'real',ptr,length,ierr)
         ptr = ptr + length
         cval = trim(cbase)//'/r' 
         call put_mem('coef',cval,'real',ptr,length,ierr)
         ptr = ptr + length                                                               
         cval = trim(cbase)//'/rc' 
         call put_mem('coef',cval,'real',ptr,length,ierr)
         ptr = ptr + length 
         cval = trim(cbase)//'/phic' 
         call put_mem('coef',cval,'real',ptr,length,ierr)
         ptr = ptr + length
!
! Write the level mesh size
         iz(pti) = lmn(p,1)         
         cval = trim(cbase)//'/icvs' 
         call put_mem('coef',cval,'int',pti,1,ierr)
         pti = pti + 1
!
         iz(pti) = lmn(p,2)                    
         cval = trim(cbase)//'/jcvs' 
         call put_mem('coef',cval,'int',pti,1,ierr)
         pti = pti + 1
!          
         iz(pti) = lmn(p,3)         
         cval = trim(cbase)//'/kcvs' 
         call put_mem('coef',cval,'int',pti,1,ierr)
         pti = pti + 1          
       enddo
!       
       end subroutine mg_init       
       