!===============================================================================
!===============================================================================
!                                                                              |
!              IO_DORURE    Apr. 2008                                          |
!                                                                              |
!===============================================================================
!===============================================================================
subroutine io_DoRuRe2 (params,aktion)

use definitions
!use mpi

! when called with the 'init' argument this routine erases all preexisting output files if irestart=0.
! when called with the 'close' argument, it closes some output files that have to remain open all through the run.

implicit none

include 'mpif.h'

type(parameters) params
character(*) aktion

character*40  :: string
character*2  :: cis
integer iproc,ierr,is,us

!-------------------------------------------------------------------------------
!-------------------------------------------------------------------------------

call mpi_comm_rank (mpi_comm_world,iproc,ierr)
   
if (params%doDoRuRe .and. iproc.eq.0) then
   if (trim(aktion).eq.'init') then
      if (params%irestart == 0) then
         open (8,file='Log.txt',status='replace')
         open (804,file='./DORURE/p_stats.dat',status='replace')              ; close(804)
         open (805,file='./DORURE/q_stats.dat',status='replace')              ; close(805)
         open (806,file='./DORURE/solver_stats.dat',status='replace')         ; close(806)
         open (807,file='./DORURE/octree_stats.dat',status='replace')         ; close(807)
         open (808,file='./DORURE/levels_stats.dat',status='replace')         ; close(808)
         open (810,file='./DORURE/conv_stats.dat',status='replace')           ; close(810)
         open (811,file='./DORURE/levels_volumes_stats.dat',status='replace') ; close(811)
         open (812,file='./DORURE/cloud_stats.dat',status='replace')          ; close(812)
         open (815,file='./DORURE/div_stats.dat',status='replace')            ; close(815)
         open (816,file='./DORURE/e2d_stats.dat',status='replace')            ; close(816)
         open (820,file='./DORURE/diag1_stats.dat',status='replace')          ; close(820)
         open (821,file='./DORURE/diag3_stats.dat',status='replace')          ; close(821)
         open (822,file='./DORURE/nelem1_stats.dat',status='replace')         ; close(822)
         open (823,file='./DORURE/nelem3_stats.dat',status='replace')         ; close(823)
         open (826,file='./DORURE/nonlin_stats.dat',status='replace')         ; close(826)
         open (827,file='./DORURE/vel_stats.dat',status='replace')            ; close(827)
         open (828,file='./DORURE/temp_stats.dat',status='replace')           ; close(828)
         open (829,file='./DORURE/e3d_stats.dat',status='replace')            ; close(829)
         open (830,file='./DORURE/dt_stats.dat',status='replace')             ; close(830)
         do is=1,params%ns
            call int_to_char(cis,2,is)
!            us=600+is
!            open(us,file='./DORURE/work_'//cis//'_stats.dat',status='replace') ; close(us)
!            us=650+is
!            open(us,file='./DORURE/vol_'//cis//'_stats.dat',status='replace') ; close(us)
            us=700+is
            open(us,file='./DORURE/surf_'//cis//'_stats.dat',status='replace') ; close(us)
         end do
         open (99,file='screen.txt',status='replace')                         
      else
         open (8,file='Log.txt',position='append',status='old')
         open (99,file='screen.txt',position='append',status='old')
      end if   
   elseif (trim(aktion).eq.'close' ) then 
      close(99)
      close(8)
   else
      call stop_run ('pb in the DoRuRe subroutine')
   end if
end if

end subroutine io_DoRuRe2







!===============================================================================
!===============================================================================
!                                                                              |
!              DORURE_DT_STATS    Feb. 2008                                    |
!                                                                              |
!===============================================================================
!===============================================================================
subroutine DoRuRe_dt_stats (doDoRuRe,istep,dt)

!use mpi

implicit none

include 'mpif.h'

logical doDoRuRe
integer istep
double precision dt
integer ierr, iproc
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (doDoRuRe .and. iproc.eq.0) then
   open (830,file    = 'DORURE/dt_stats.dat',&
             position= 'append',              &
             form    = 'formatted',           &
             access  = 'sequential',          &
             status  = 'old')
   write(830,'(i9,es11.4)') istep,dt
   close(830)
end if

return
end subroutine DoRuRe_dt_stats


!===============================================================================
!===============================================================================
!                                                                              |
!              DORURE_CLOUD_STATS    Feb. 2008                                 |
!                                                                              |
!===============================================================================
!===============================================================================

subroutine DoRuRe_cloud_stats (doDoRuRe,istep,iter,clnp,nremove,ninject,s,t,p)

!use mpi

implicit none

include 'mpif.h'

logical doDoRuRe
integer istep,iter,clnp,nremove,ninject
double precision s(clnp),t(clnp),p(clnp)
integer ierr, iproc
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (doDoRuRe .and. iproc.eq.0) then
   open (812,file     ='./DORURE/cloud_stats.dat',&
             position ='append',                   &
             form     ='formatted',                &
             access   ='sequential',               &
             status   ='old')
   write(812,'(es11.4,i10,2i5,9es11.4)') istep+dble(iter)*.01d0,clnp,nremove,ninject, &
                                     minval(s),sum(s)/clnp,maxval(s),             &
                                     minval(t),sum(t)/clnp,maxval(t),             &
                                     minval(p),sum(p)/clnp,maxval(p)
   close(812)
end if

return
end subroutine DoRuRe_cloud_stats

!===============================================================================
!===============================================================================
!                                                                              |
!              DORURE_DIAG_STATS    Feb. 2008                                  |
!                                                                              |
!===============================================================================
!===============================================================================
subroutine DoRuRe_diag_stats (doDoRuRe,sizediag,diag,ndof,istep,iter,iter_nl)

!use mpi

implicit none

include 'mpif.h'

logical doDoRuRe
integer sizediag
double precision diag(sizediag)
integer ndof,istep,iter,iter_nl
integer ierr,iproc,nproc
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (doDoRuRe .and. iproc.eq.0) then

   if (ndof==1) then 
      open (820,file    ='./DORURE/diag1_stats.dat',&
                position='append',                  &
                form    ='formatted',               &
                access  ='sequential',              &
                status  ='old')
      write(820,'(i5,2f25.8)') istep,minval(diag),maxval(diag)
      close(820)
   end if

   if (ndof==3) then
      open (821,file    ='./DORURE/diag3_stats.dat', &
                position='append',                   &
                form    ='formatted',                &
                access  ='sequential',               &
                status  ='old')
      write(821,'(3es11.4)') istep+dble(iter)*.01d0+dble(iter_nl)*.0001d0,minval(diag),maxval(diag)
      close(821)
   end if

end if

return
end subroutine DoRuRe_diag_stats

!===============================================================================
!===============================================================================
!                                                                              |
!              DORURE_LEAF_STATS    Feb. 2008                                 |
!                                                                              |
!===============================================================================
!===============================================================================
subroutine DoRuRe_leaf_stats (doDoRuRe,nleaves,e2d,e3d,p,q,dilatrtemp,istep,iter,iter_nl)

!use mpi

implicit none

include 'mpif.h'

logical doDoRuRe
integer nleaves
double precision :: e2d(nleaves),p(nleaves),q(nleaves),e3d(nleaves),dilatrtemp(nleaves)
integer istep,iter,iter_nl
integer ierr,iproc
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (doDoRuRe .and. iproc.eq.0) then

   open (816,file    ='./DORURE/e2d_stats.dat',&
             position='append',                &
             form    ='formatted',             &
             access  ='sequential',            &
             status  ='old')
   write(816,'(3es13.4)') istep+dble(iter)*.01d0+dble(iter_nl)*.0001d0,minval(e2d),maxval(e2d) 
   close(816)

   open (829,file    ='./DORURE/e3d_stats.dat', &
             position='append',                 &
             form    ='formatted',              &
             access  ='sequential',             &
             status  ='old')
   write(829,'(3es13.4)') istep+dble(iter)*.01d0+dble(iter_nl)*.0001d0,minval(e3d),maxval(e3d) 
   close(829)

   open (804,file    ='./DORURE/p_stats.dat', &
             position='append',               &
             form    ='formatted',            &
             access  ='sequential',           &
             status  ='old')
   write(804,'(3es13.4)') istep+dble(iter)*.01d0+dble(iter_nl)*.0001d0,minval(p),maxval(p) 
   close(804)

   open (805,file    ='./DORURE/q_stats.dat', &
             position='append',               &
             form    ='formatted',            &
             access  ='sequential',           &
             status  ='old')
   write(805,'(3es13.4)') istep+dble(iter)*.01d0+dble(iter_nl)*.0001d0,minval(q),maxval(q) 
   close(805)

end if

end subroutine DoRuRe_leaf_stats




!===============================================================================
!===============================================================================
!                                                                              |
!              DORURE_NELEM_STATS    Feb. 2008                                 |
!                                                                              |
!===============================================================================
!===============================================================================
subroutine DoRuRe_nelem_stats (doDoRuRe,nbproc,nelem,ndof,nleaves,istep,iter,iter_nl)

!use mpi

implicit none

include 'mpif.h'

logical doDoRuRe
integer nbproc,nelem(nbproc),ndof,nleaves,istep,iter,iter_nl
integer ierr,iproc,nproc,jproc
call mpi_comm_size (mpi_comm_world,nproc,ierr)
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (doDoRuRe .and. iproc.eq.0) then

   if (ndof==1) then 
      open (822,file    ='./DORURE/nelem1_stats.dat', &
                position='append',                    &
                form    ='formatted',                 &
                access  ='sequential',                &
                status  ='old')
      write(822,'(3i10)') istep,minval(nelem),maxval(nelem)
      close(822)
   end if

   if (ndof==3) then
      open (823,file    ='./DORURE/nelem3_stats.dat', &
                position='append',                    &
                form    ='formatted',                 &
                access  ='sequential',                &
                status='old')
      write(823,'(es11.4,2i10)') istep+dble(iter)*.01d0+dble(iter_nl)*.0001d0,minval(nelem),maxval(nelem)
      close(823)
   end if

   write(8,*) '(processor,nb of built elements by this processor):'
   write(8,*) ('(',jproc,',',nelem(jproc),')',jproc=1,nproc)
   write(8,*) 'Redundancy ',(float(sum(nelem))/nleaves-1.)*100.,'%'

end if

return
end subroutine DoRuRe_nelem_stats

!===============================================================================
!===============================================================================
!                                                                              |
!              DORURE_SOLVER_STATS    Feb. 2008                                |
!                                                                              |
!===============================================================================
!===============================================================================
subroutine DoRuRe_solver_stats (doDoRuRe,istep,iter,iter_nl,n,nz,rinfog,infog,time1,time2,time3,nb)

!use mpi

implicit none

include 'mpif.h'

logical doDoRuRe
integer istep,iter,iter_nl,nl,n,nz,infog,nb
double precision rinfog
real  time1,time2,time3
integer iproc,ierr
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (doDoRuRe .and. iproc.eq.0) then
   open (806,file    ='./DORURE/solver_stats.dat', & 
             position='append',                    &
             form    ='formatted',                 &
             access  ='sequential',                &
             status  ='old')
   write(806,'(es11.4,2i10,4es11.4,i9)') istep+dble(iter)*.01d0+dble(iter_nl)*.0001d0,&
                                         n,nz, rinfog/1.d9,infog/1073741824.d0*8.d0,  &
                                         time2-time1,time3-time2,nb
   close(806)
end if

end subroutine DoRuRe_solver_stats


!===============================================================================
!===============================================================================
!                                                                              |
!              DORURE_CONV_STATS    Feb. 2008                                  |
!                                                                              |
!===============================================================================
!===============================================================================
subroutine DoRuRe_conv_stats (doDoRuRe,istep,iter,iter_nl,velocity_diff_norm,tol,maxu,maxv,maxw)

!use mpi

implicit none

include 'mpif.h'

logical doDoRuRe
integer istep,iter,iter_nl
double precision velocity_diff_norm,tol,maxu,maxv,maxw
integer iproc,ierr
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (doDoRuRe .and. iproc.eq.0) then
   open (810,file    ='./DORURE/conv_stats.dat', &
             position='append',                   &
             form    ='formatted',                &
             access  ='sequential',               &
             status  ='old')
   write(810,'(6es15.6)') istep+dble(iter)*.01d0+dble(iter_nl)*.0001d0,velocity_diff_norm,tol,maxu,maxv,maxw
   close(810)
end if

return
end subroutine DoRuRe_conv_stats

!===============================================================================
!===============================================================================
!                                                                              |
!              DORURE_TEMP_STATS    Feb. 2008                                  |
!                                                                              |
!===============================================================================
!===============================================================================
subroutine DoRuRe_temp_stats(doDoRuRe,istep,nnode,temp)

!use mpi

implicit none

include 'mpif.h'

logical doDoRuRe
integer istep,nnode
double precision temp(nnode)
integer iproc,ierr
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (doDoRuRe .and. iproc.eq.0) then 
   open (828,file    ='./DORURE/temp_stats.dat', &
             position='append',                   &
             form    ='formatted',                &
             access  ='sequential',               &
             status  ='old')
   write(828,'(i5,2es11.4)') istep,minval(temp),maxval(temp)
   close(828)
end if

return
end subroutine DoRuRe_temp_stats

!===============================================================================
!===============================================================================
!                                                                              |
!              DORURE_VEL_STATS    Feb. 2008                                    |
!                                                                              |
!===============================================================================
!===============================================================================
subroutine DoRuRe_vel_stats(doDoRuRe,istep,iter,iter_nl,nnode,unode,vnode,wnode)

!use mpi

implicit none

include 'mpif.h'

logical doDoRuRe
integer istep,iter,iter_nl,nnode
double precision unode(nnode),vnode(nnode),wnode(nnode)
integer iproc,ierr
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (doDoRuRe .and. iproc.eq.0) then
   open (827,file    ='./DORURE/vel_stats.dat', &
             position='append',                      &
             form='formatted',                       &
             access='sequential',                    &
             status='old')
   write(827,'(7es15.5)') istep+iter*0.01d0+iter_nl*0.0001d0, &
                          minval(unode),maxval(unode),&
                          minval(vnode),maxval(vnode),&
                          minval(wnode),maxval(wnode)
   close(827)
end if

return
end subroutine DoRuRe_vel_stats


!===============================================================================
!===============================================================================
!                                                                              |
!              DORUREE_SURF_STATS    Feb. 2008                                 |
!                                                                              |
!===============================================================================
!===============================================================================

subroutine DoRuRe_surf_stats (doDoRuRe,istep,ref_count,is,nt,nsurface,nedge,nadd)

!use mpi

implicit none

include 'mpif.h'

logical doDoRuRe
integer istep,ref_count,is,nt,nsurface,nedge,nadd
integer us,iproc,ierr
character(len=2) cis
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (doDoRuRe .and. iproc.eq.0) then
   call int_to_char(cis,2,is)
   us=700+is
   open(us,file    ='./DORURE/surf_'//cis//'_stats.dat',    &
           position='append',                                &
           form    ='formatted',                             &
           access  ='sequential',                            &
           status  ='old') 
   write(us,'(es11.4,4i7)') real(istep)+real(ref_count)/100.d0,nt,nsurface,nedge,nadd
   close(us)
end if

return
end subroutine DoRuRe_surf_stats 





!===============================================================================
!===============================================================================
!                                                                              |
!              DORURE_OCTREE_STATS    Feb. 2008                                |
!                                                                              |
!===============================================================================
!===============================================================================
subroutine DoRuRe_octree_stats (osolve,params,istep,iter,iter_nl)

use definitions
!use mpi

implicit none

include 'mpif.h'

type (octreesolve) osolve
type (parameters) params
integer istep,iter,iter_nl
integer ierr,iproc,i,ie,k,il,nlev,err
integer,dimension(:),allocatable::levs
integer,dimension(:),allocatable::levelstat
double precision phi(8),vol,voltot
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (params%doDoRuRe .and. iproc.eq.0) then

   open (807,file    ='./DORURE/octree_stats.dat', &
             position='append',                    &
             form    ='formatted',                 &
             access  ='sequential',                &
             status  ='old')
   write(807,'(es11.4,3i8)') istep+dble(iter)*.01d0+dble(iter_nl)*.0001d0,osolve%nleaves,osolve%nnode,osolve%nface
   close(807)

   allocate (levelstat(4:10),stat=err) ; if (err.ne.0) call stop_run ('Error alloc levelstat in write_octree_stats$')
   allocate (levs(osolve%nleaves),stat=err) ; if (err.ne.0) call stop_run ('Error alloc levs in write_octree_stats$')

   call octree_find_element_level (osolve%octree,osolve%noctree,levs,osolve%nleaves)

   levelstat( 4)=count(levs== 4)
   levelstat( 5)=count(levs== 5)
   levelstat( 6)=count(levs== 6)
   levelstat( 7)=count(levs== 7)
   levelstat( 8)=count(levs== 8)
   levelstat( 9)=count(levs== 9)
   levelstat(10)=count(levs==10)

   voltot=0.d0
   do i=1,osolve%nleaves
      do k=1,8
         phi(k)=osolve%lsf(osolve%icon(k,i),1)
      end do
      call compute_positive_volume (phi,vol,3)
      voltot=voltot+vol/(2.d0**levs(i))**3
   end do 

   open (808,file    ='./DORURE/levels_stats.dat', &
             position='append',                    &
             form    ='formatted',                 &
             access  ='sequential',                &
             status  ='old')
   write(808,'(es11.4,7i7)') istep+dble(iter)*.01d0+dble(iter_nl)*.0001d0,levelstat(4:10)
   close(808)

   open (811,file    ='./DORURE/levels_volumes_stats.dat', &
             position='append',                            &
             form    ='formatted',                         &
             access  ='sequential',                        &
             status  ='old')
   write(811,'(9es11.4)') istep+dble(iter)*.01d0+dble(iter_nl)*.0001d0,(levelstat(i)/8.d0**i,i=4,10),1.d0-voltot
   close(811)
   
   write (8,*) 'Levs',levelstat(4:10)
   write (8,*) 'Total fluid volume ',1.d0-voltot

   deallocate (levs)
   deallocate (levelstat)


end if

return
end subroutine DoRuRe_octree_stats


!===============================================================================
!===============================================================================
!                                                                              |
!              DORURE_DIV_STATS    Feb. 2008                                   |
!                                                                              |
!===============================================================================
!===============================================================================

subroutine DoRuRe_div_stats (doDoRuRe,istep,iter,nleaves,divergence)

!use mpi

implicit none

include 'mpif.h'

logical doDoRuRe
integer istep,iter,nleaves
double precision divergence(nleaves)
integer iproc,ierr
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (doDoRuRe .and. iproc.eq.0) then
   open (815,file    ='./DORURE/div_stats.dat', &
             position='append',                        &
             form    ='formatted',                     &
             access  ='sequential',                    &
             status  ='old')
   write(815,'(4es13.4)') istep+dble(iter)*.01d0,minval(divergence),sum(divergence)/nleaves,maxval(divergence)
   close(815)
end if

end subroutine DoRuRe_div_stats

!===============================================================================
!===============================================================================
!                                                                              |
!              DORURE_NONLIN_STATS    Feb. 2008                                |
!                                                                              |
!===============================================================================
!===============================================================================
subroutine DoRuRe_nonlin_stats(doDoRuRe,istep,iter,iter_nl)

!use mpi

implicit none

include 'mpif.h'

logical doDoRuRe
integer istep,iter,iter_nl
integer iproc,ierr
call mpi_comm_rank (mpi_comm_world,iproc,ierr)

if (doDoRuRe .and. iproc.eq.0) then
   open (826,file    ='./DORURE/nonlin_stats.dat', &
             position='append',                    &
             form    ='formatted',                 &
             access  ='sequential',                &
             status  ='old')
   write(826,'(es11.4,i5)') istep+iter/100.d0,iter_nl
   close(826)
end if

return
end subroutine DoRuRe_nonlin_stats

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