!------------------------------------------------------------------------------|
!------------------------------------------------------------------------------|
!                                                                              |
!              ||===\\                                                         | 
!              ||    \\                                                        |
!              ||     ||   //==\\   ||  ||   //==||  ||/==\\                   |
!              ||     ||  ||    ||  ||  ||  ||   ||  ||    ||                  |
!              ||    //   ||    ||  ||  ||  ||   ||  ||                        |
!              ||===//     \\==//    \\==\\  \\==\\  ||                        |
!                                                                              |
!------------------------------------------------------------------------------|
!------------------------------------------------------------------------------|
!                                                                              |
!              DEFINE_CLOUD    Nov. 2006                                       |
!                                                                              |
!------------------------------------------------------------------------------|
!------------------------------------------------------------------------------|

subroutine define_cloud (cl,params,zi)

!------------------------------------------------------------------------------|
!(((((((((((((((( Purpose of the routine  ))))))))))))))))))))))))))))))))))))))
!------------------------------------------------------------------------------|
! if irestart=0, this routine allocates and creates the cloud of points present 
! in the system. Otherwise it reads from a user supplied file name the surfaces 
! as they were at the end of a previous run. In this case, since the run output 
! files contain all the octree+lsf+icloud+surface informations, the routine 
! first reads dummy parameters until it gets to the real interesting 
! cloud information 

!------------------------------------------------------------------------------|
!((((((((((((((((  declaration of the subroutine arguments  ))))))))))))))))))))
!------------------------------------------------------------------------------|

use definitions

implicit none

type (cloud) cl
type (parameters) params
type (ziso) zi
!double precision zisodisp(2**params%levelmax_oct+1,2**params%levelmax_oct+1)

!------------------------------------------------------------------------------|
!(((((((((((((((( declaration of the subroutine internal variables )))))))))))))
!------------------------------------------------------------------------------|
double precision x,y,z,u,v,w,p,r,s,t,e2d,xlsf,xn,yn,zn,don,con,wp,ev,epr,espr
double precision current_time,activation_time
logical inf,wlif,ip
integer err,iproc,nproc,ierr,i,j,k,nl,nf,nn,nr,kx,ky,kz,kt,icon
integer ioc,iface,ilsf,noctree,nnode,nface,nlsf,nleaves,ns,nt

!------------------------------------------------------------------------------|
!------------------------------------------------------------------------------|

INCLUDE 'mpif.h'

call mpi_comm_size (mpi_comm_world,nproc,ierr)
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (params%irestart.eq.0) then
   cl%np=0
   allocate (cl%x(1), stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%x in define_cloud$')
   allocate (cl%y(1), stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%y in define_cloud$')
   allocate (cl%z(1), stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%z in define_cloud$')
   allocate (cl%x0(1), stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%x0 in define_cloud$')
   allocate (cl%y0(1), stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%x0 in define_cloud$')
   allocate (cl%z0(1), stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%x0 in define_cloud$')
   allocate (cl%strain(1), stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%strain in define_cloud$')
   allocate (cl%lsf0(1), stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%lsf0 in define_cloud$')
   allocate (cl%temp(1), stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%temp in define_cloud$')
   allocate (cl%press(1), stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%press in define_cloud$')
   allocate (cl%tag(1), stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%tag in define_cloud$')
   if (params%isobc) then
     allocate (zi%zisodisp(2**params%levelmax_oct+1,2**params%levelmax_oct+1), stat=err); if (err.ne.0) call stop_run ('Error alloc zi%zisodisp in define_cloud$')
     zi%zisodisp=0.d0
   endif

else
   open (19,file=trim(params%restartfile),status='old',form='unformatted')
   read (19) noctree, &
               nnode,   &
               nleaves, &
               nface,   &
               nlsf,    &
               cl%np,   &
               current_time     

   allocate (cl%x(cl%np),stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%x in define_cloud$')
   allocate (cl%y(cl%np),stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%y in define_cloud$')
   allocate (cl%z(cl%np),stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%z in define_cloud$')
   allocate (cl%x0(cl%np),stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%x0 in define_cloud$')
   allocate (cl%y0(cl%np),stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%y0 in define_cloud$')
   allocate (cl%z0(cl%np),stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%z0 in define_cloud$')
   allocate (cl%strain(cl%np),stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%strain in define_cloud$')
   allocate (cl%lsf0(cl%np), stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%lsf0 in define_cloud$')
   allocate (cl%temp(cl%np),stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%temp in define_cloud$')
   allocate (cl%press(cl%np),stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%press in define_cloud$')
   allocate (cl%tag(cl%np),stat=err) ; if (err.ne.0) call stop_run ('Error alloc cl%tag in define_cloud$')
   if (params%isobc) allocate (zi%zisodisp(2**params%levelmax_oct+1,2**params%levelmax_oct+1), stat=err); if (err.ne.0) call stop_run ('Error alloc zi%zisodisp in define_cloud$')

   read (19) (x,               &
                y,               &
                z,               &
                u,               &
                v,               &
                w,               &
                wp,              &
                (xlsf,j=1,nlsf), &
                t,               &
                p,               &
                s,               &
                kx,              &
                ky,              &
                kz,              &
                kt,              &
                i=1,nnode)
   read (19) ((icon,k=1,8),epr,espr,con,e2d,ev,ip,wlif,i=1,nleaves)
   read (19) (ioc,k=1,noctree)                    
   read (19) ((iface,k=1,9),i=1,nface)
   read (19) (nn,nl,nf,nr,inf,i=1,nnode)
   read (19) (nf,i=1,nface)
   do ilsf=1,nlsf                                  
      read (19) ns, &
                  activation_time, &
                  nt
      read (19) (r,  &
                   s,  &
                   x,  &
                   y,  &
                   z,  &
                   xn, &
                   yn, &
                   zn, &
                   u,v,w, &
                   i=1,ns)
      read (19) ((icon,k=1,3),i=1,nt)
   enddo
   
   read (19) (cl%x(i),      & 
                cl%y(i),      &
                cl%z(i),      &
                cl%x0(i),     &
                cl%y0(i),     & 
                cl%z0(i),     &
                cl%strain(i), &
                cl%lsf0(i),   &
                cl%temp(i),   &
                cl%press(i),  &
                cl%tag(i),    &
                i=1,cl%np)

   ! read isostasy basal displacement array - dwhipp 11/09
   if (params%isobc) read (19) ((zi%zisodisp(i,j),j=1,2**params%levelmax_oct+1),&
                               i=1,2**params%levelmax_oct+1)

   close (19)
endif

if (iproc.eq.0) write (8,*) cl%np,' particles in cloud'

return
end

!------------------------------------------------------------------------------|
!------------------------------------------------------------------------------|
