 MODULE comMUSTANG



!&E==========================================================================
!&E                   ***  MODULE  comMUSTANG  ***
!&E
!&E
!&E ** Purpose : declare all common variables related to sediment dynamics
!&E              to be used in MARSiAM system
!&E
!&E ** Description :
!&E     subroutine sed_alloc          ! allocates variables in sediment
!&E
!&E ** History :
!&E     ! 2015-12  (B.Thouvenin )    : creation from sedim.F90 -reorganization of module SEDIMARS
!&E
!&E==========================================================================




























































!!! definitions de dimensions, variables, parametres pour MUSTANG
!!! et couplage avec autre modele




!&E==========================================================================
!&E                   ***    coupleur_MUSTANG dimensions hydro ***
!&E==========================================================================




! parametres issus du code Hydrodynamique (dimensions grille et dimensions tableau/proc)
   USE parameters,  ONLY : lchain,rsh,rlg,riosh,kmax,imin,imax,jmin,jmax,ksdmin,ksdmax,  &
                           limin,limax,ljmin,ljmax,liminp1,limaxm1,ljminp1,ljmaxm1,      &
                           liminm1,ljminm1,limaxp1,ljmaxp1,limaxp2,ljmaxp2,              &
                           liminm2,ljminm2,ljminp2
   USE comsubstance, ONLY : nv_tot,nv_adv,nv_state,nvp
! provisoire
   USE comvars2d,    ONLY :  t 
   




!! * Modules used

                                 
   IMPLICIT NONE

!! * Accessibility

! functions & routines of this module, called outside :
   PUBLIC MUSTANG_alloc
   
!! * Shared or public variables for SEDIMARS (common at all threads) but spatialized i,j

   REAL(KIND=rlg),PUBLIC   :: tstart_sed,       &   ! time beginning consolidation/diffusion/bioturbation
                              t_sed,            &   ! time of next dynamic in sediment step
                              dtcal_sed,        &   ! sub time step for consolidation dynamic in sediment
                              dt_sed                ! time step for dynamic in sediment

   LOGICAL,PUBLIC            :: l_fricwave,l_diffused,l_consolid,l_bioturb,l_dyn_insed
   LOGICAL,PUBLIC            :: l_morphocoupl,l_bathy_smoothing,l_erolat_wet_cell
   INTEGER,PUBLIC            :: choice_flxdiss_diffsed,ero_option,E0_sand_option,nv_use
   
   CHARACTER(len=19),PUBLIC  :: date_start_sed
   
   REAL(KIND=rsh),DIMENSION(:),ALLOCATABLE,PUBLIC    :: diam_sed,ros
   REAL(KIND=rsh),PUBLIC                             :: aref_sand  ! parametre used in sandconcextrap



   REAL(KIND=rsh),DIMENSION(:),ALLOCATABLE,PUBLIC   :: diamstar,ws_sand,rosmrowsros,   &
                                                       stresscri0,tetacri0,xnielsen
                                                       
   REAL(KIND=rsh),PUBLIC     :: fricwav,fws2,dzsmin,cfreshmud,csedmin,cmudcr,ros_sand_homogen, &
                                cvolmaxsort,cvolmaxmel,xperm1,xperm2,xsigma1,xsigma1sg, &
                                xsigma2,fimpls,cimpls, xdifs1,xdifs2,xdifsi1,xdifsi2,   &
                                epdifi,fexcs,cexcs,activlayer,frmudcr2,coef_frmudcr1,   &
                                x1toce_mud,x2toce_mud,E0_sand_para,n_eros_sand,E0_mud,       &
                                n_eros_mud,corfluer1,corfluer2,MF,htncrit_eros,         &
                                slopefac,csegreg,csandseg,xexp_ero,         &
                                xbioturbmax,xbioturbk,dbiotu0,dbiotum,frmud_db_max,frmud_db_min
   REAL(KIND=rsh),PUBLIC                                :: hsed_new,coef_erolat,coef_tenfon_lat
   REAL(KIND=rsh),PUBLIC                                :: sed_difint,sed_difsed 
   REAL(KIND=rsh),DIMENSION(:),ALLOCATABLE,PUBLIC       :: alp1,alp2,alp3,alp4,alp5


   INTEGER,DIMENSION(:,:),ALLOCATABLE              :: ksmi,ksma
   INTEGER, DIMENSION(:,:), ALLOCATABLE            :: dry_cell

   REAL(KIND=rsh),DIMENSION(:,:,:,:),ALLOCATABLE   :: cv_sed

   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE     :: c_sedtot,poro,dzs       
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE     :: corflux,corfluy,gradvit       
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE     :: fludif,fluconsol,fluconsol_drycell,flu_dyninsed
   
   REAL(KIND=rsh),DIMENSION(:,:),ALLOCATABLE       :: hsed,z0sed,hsed0,dzsmax
   REAL(KIND=rsh),DIMENSION(:,:),ALLOCATABLE       :: tenfon,tenfonc,tenfonw
   REAL(KIND=rsh),DIMENSION(:,:),ALLOCATABLE       :: raphbx,raphby,frofonx,frofony
   REAL(KIND=rlg),DIMENSION(:,:),ALLOCATABLE       :: phieau_s2w,phieau_s2w_consol,phieau_s2w_drycell
   REAL(KIND=rsh),DIMENSION(:,:),ALLOCATABLE       :: ustarbot,htot,alt_cw1
   REAL(KIND=rsh),DIMENSION(:,:),ALLOCATABLE       :: morphox,morphoy,hxi,hyi,h0i
 
   REAL(KIND=rsh),DIMENSION(:,:),ALLOCATABLE       :: sal_bottom_MUSTANG,temp_bottom_MUSTANG,epn_bottom_MUSTANG
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE     :: cw_bottom_MUSTANG


   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE    :: rouse2D,sum_tmp  ! definition nombre de Rouse2D et SUM(dzcche*((htot-hzed)/hzed)**rouse)




! ---------------------------------------------------------------------------
! VARIABLES FLUXES AT THE INTERFACE WATER-SEDIMENT
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE :: flx_s2w,flx_w2s,flx_w2s_sum

! relatif to initialization
   LOGICAL,PUBLIC          :: l_repsed,l_unised,l_initsed_vardiss,l_dzsmaxuni,l_init_hsed
   INTEGER,PUBLIC          :: ksmiuni,ksmauni
   REAL(KIND=rsh),DIMENSION(:),ALLOCATABLE,PUBLIC    :: cini_sed,cv_sedini
   REAL(KIND=rsh),PUBLIC     :: z0seduni,cseduni,hseduni,sini_sed,tini_sed,dzsmaxuni,csed_mud_ini

! relatif to sedim output
   LOGICAL,PUBLIC                 :: l_outsed_saltemp
   CHARACTER(len=lchain),PUBLIC   :: name_out_eptot,name_out_nbniv,name_out_dzs,name_out_tenfon,&
                                     name_out_tenfonc,name_out_tenfonw
   REAL(kind=riosh),PUBLIC      :: riog_valid_min_eptot,riog_valid_max_eptot,riog_valid_min_nbniv, &
                                   riog_valid_max_nbniv,riog_valid_min_dzs,riog_valid_max_dzs,     &
                                   riog_valid_min_tenfon,riog_valid_max_tenfon
   INTEGER,PUBLIC                                    :: nk_nivsed_out,choice_nivsed_out
   REAL(KIND=rsh),DIMENSION(5),PUBLIC                :: ep_nivsed_out
   REAL(KIND=rsh),DIMENSION(:),ALLOCATABLE,PUBLIC    :: ep_nivsed_outp1,nivsed_out
   REAL(KIND=rsh),PUBLIC                             :: epmax_nivsed_out ! epaisseur max depuis la surface du sediment

   CHARACTER(len=lchain),PUBLIC   :: filrepsed,filoutsed

   LOGICAL,DIMENSION(:),ALLOCATABLE,PUBLIC                :: l_outsandrouse


!  used in erosion only bud exchange and dimensions could depend on grid model
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE,PUBLIC :: flx_s2w_corim1
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE,PUBLIC :: flx_s2w_corip1
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE,PUBLIC :: flx_s2w_corjm1
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE,PUBLIC :: flx_s2w_corjp1
!  used in accretion (settling) only bud exchange and dimensions could depend on grid model
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE,PUBLIC :: flx_w2s_corin
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE,PUBLIC :: flx_w2s_corim1
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE,PUBLIC :: flx_w2s_corip1
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE,PUBLIC :: flx_w2s_corjm1
   REAL(KIND=rsh),DIMENSION(:,:,:),ALLOCATABLE,PUBLIC :: flx_w2s_corjp1

 CONTAINS
 
!!===========================================================================
 
  SUBROUTINE MUSTANG_alloc(l_filesubs)
 
!&E--------------------------------------------------------------------------
!&E                 ***  ROUTINE MUSTANG_alloc  ***
!&E
!&E ** Purpose : allocation of arrays relative to sediment
!&E
!&E ** Description :
!&E
!&E ** Called by : main, after call traceur and sed_param
!&E
!&E ** External calls :
!&E
!&E ** Reference :
!&E
!&E ** History :
!&E       !  2008-09  (Pierre Le Hir) extracted from traceur.F90
!&E       !  2008-10  (F. Cayocca)
!&E
!&E--------------------------------------------------------------------------
!! * Modules used
!!! definitions de dimensions, variables, parametres pour MUSTANG
!!! et couplage avec autre modele




!&E==========================================================================
!&E                   ***    coupleur_MUSTANG  ***
!&E==========================================================================

!/* Spatial MPI limits grid
!*/





!/* Spatial Grid limits definition  of loops
!*/







!/* dimensions table definition
!*/







!/* general variable hydro , bathy, time ... defined in hydro model but using by MUSTANG
!*/


























!/* number of advected and total substances
!*/
! nombre de variables transportees

! nombre de variables totales (variable transportees,+variables fixees, +variables forcantes etc..)


!/* name of variables in MUSTANG (dimensions order could changed)
!*/
! layers thickness (ordre des dimensions differentes)

! vertical eddy viscosity  (ordre des dimensions differentes)


!/* name of fluxes exchange between MUSTANG and hydro model
!*/











!/* dimensions of variables in hydro modele
!*/!! dimensions des variables 3D ou 4D dans le modele hydro !! (si pas MARS)

!!!#define DIM_MODELE_HYDRO_cw_bottom nv_tot,liminm1:limaxp2,ljminm1:ljmaxp2
!#define DIM_COORD_SIG 0:kmax+1





!/* Laterale Erosion
!*/
! Erosion laterale dans les mailles voisines (pourrait dependre architecture grille)













!/* sliding proces of fluid mud
!*/
! glissement depot : pente du fond dans les mailles voisines  (pourrait dependre architecture grille)










   USE parameters,   ONLY : l_testcase
   USE comvars2d,    ONLY : iscreenlog
   USE comsubstance, ONLY : irk_fil,diam_r,nvpc,   &
                            cini_sed_r, irkm_var_assoc,ros_r,unit_modif_mudbio_N2dw




!! * Arguments
   LOGICAL, INTENT(IN), OPTIONAL   :: l_filesubs

!! * Local declarations
   INTEGER               :: iv
   CHARACTER(len=lchain) :: filepc
   LOGICAL               :: l_filesubsr

!!--------------------------------------------------------------------------
!! * Executable part
   IF(PRESENT(l_filesubs)) THEN
     l_filesubsr=l_filesubs
   ELSE
     l_filesubsr=.FALSE.
   ENDIF

!  allocation des variables 1DV
   ALLOCATE(ros(nvp))
   ALLOCATE(diam_sed(nvp))
   
   ros(1:nvp)=0.0_rsh
   diam_sed(1:nvp)=0.0_rsh
   
   ALLOCATE(ws_sand(nvp))        
   ALLOCATE(diamstar(nvp))
   ALLOCATE(rosmrowsros(nvp))
   ALLOCATE(stresscri0(nvp))
   ALLOCATE(tetacri0(nvp))
   ALLOCATE(xnielsen(nvp))
   
      

   
!  allocation des variables spatiales
!  dimenions definies dans couplour_MUSTANG.h
!  dimension pour MARS : limin  :limax   , ljmin  :ljmax       = limin:limax,ljmin:ljmax
!                        liminm1:limaxp2 , ljminm1:ljmaxp2  = liminm1:limaxp2,ljminm1:ljmaxp2
!                        liminm1:limaxp1 , ljminm1:ljmaxp1  = liminm1:limaxp1,ljminm1:ljmaxp1
!                        limin  :limaxp1 , ljmin  :ljmaxp1   = limin:limaxp1,ljmin:ljmaxp1

   ALLOCATE(ksmi(limin  :limax   , ljmin  :ljmax))
   ALLOCATE(ksma(limin  :limax   , ljmin  :ljmax))
   ALLOCATE(hsed(liminm1:limaxp2 , ljminm1:ljmaxp2))  ! extend limits because of morpho
   ALLOCATE(hsed0(liminm1:limaxp2 , ljminm1:ljmaxp2))
   ALLOCATE(z0sed(limin  :limax   , ljmin  :ljmax))
   ALLOCATE(tenfon(limin  :limax   , ljmin  :ljmax))
   ALLOCATE(tenfonc(limin  :limax   , ljmin  :ljmax))
   ALLOCATE(tenfonw(limin  :limax   , ljmin  :ljmax))
   ALLOCATE(ustarbot(limin  :limax   , ljmin  :ljmax))
   ALLOCATE(dzsmax(limin  :limax   , ljmin  :ljmax))
   ALLOCATE(htot(liminm2:limaxp2 , ljminm2:ljmaxp2))
   ALLOCATE(alt_cw1(limin  :limax   , ljmin  :ljmax))
   ALLOCATE(epn_bottom_MUSTANG(liminm1:limaxp2 , ljminm1:ljmaxp2))  
   ALLOCATE(sal_bottom_MUSTANG(liminm1:limaxp2 , ljminm1:ljmaxp2))  
   ALLOCATE(temp_bottom_MUSTANG(liminm1:limaxp2 , ljminm1:ljmaxp2))
   ALLOCATE(cw_bottom_MUSTANG(nv_tot,liminm1:limaxp2 , ljminm1:ljmaxp2))


   ALLOCATE(rouse2D(nv_adv,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(sum_tmp(nv_adv,limin  :limax   , ljmin  :ljmax))
   rouse2D(1:nv_adv,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   sum_tmp(1:nv_adv,limin  :limax   , ljmin  :ljmax)=0.0_rsh


   ksmi(limin  :limax   , ljmin  :ljmax)=0
   ksma(limin  :limax   , ljmin  :ljmax)=0
   hsed(liminm1:limaxp2 , ljminm1:ljmaxp2)=0.0_rsh
   hsed0(liminm1:limaxp2 , ljminm1:ljmaxp2)=0.0_rsh
   z0sed(limin  :limax   , ljmin  :ljmax)=0.0_rsh
   tenfon(limin  :limax   , ljmin  :ljmax)=0.0_rsh
   tenfonc(limin  :limax   , ljmin  :ljmax)=0.0_rsh
   tenfonw(limin  :limax   , ljmin  :ljmax)=0.0_rsh
   ustarbot(limin  :limax   , ljmin  :ljmax)=0.0_rsh
   dzsmax(limin  :limax   , ljmin  :ljmax)=0.0_rsh
   htot(liminm2:limaxp2 , ljminm2:ljmaxp2)=0.0_rsh
   alt_cw1(limin  :limax   , ljmin  :ljmax)=0.0_rsh
   epn_bottom_MUSTANG(liminm1:limaxp2 , ljminm1:ljmaxp2)=0.0_rsh
   sal_bottom_MUSTANG(liminm1:limaxp2 , ljminm1:ljmaxp2)=0.0_rsh
   temp_bottom_MUSTANG(liminm1:limaxp2 , ljminm1:ljmaxp2)=0.0_rsh
   cw_bottom_MUSTANG(nv_tot,liminm1:limaxp2 , ljminm1:ljmaxp2)=0.0_rsh

   ALLOCATE(cv_sed(-1:nv_tot,ksdmin:ksdmax,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(c_sedtot(ksdmin:ksdmax,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(poro(ksdmin:ksdmax,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(dzs(ksdmin:ksdmax,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(flx_s2w(-1:nv_adv,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(flx_w2s(-1:nv_adv,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(flx_w2s_sum(-1:nv_adv,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(corflux(nv_adv,liminm1:limaxp1 , ljminm1:ljmaxp1  ))
   ALLOCATE(corfluy(nv_adv,liminm1:limaxp1 , ljminm1:ljmaxp1))
   ALLOCATE(fludif(-1:nv_adv,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(fluconsol(-1:nv_adv,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(fluconsol_drycell(-1:nv_adv,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(flu_dyninsed(-1:nv_adv,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(gradvit(kmax,limin  :limax   , ljmin  :ljmax))

   cv_sed(-1:nv_tot,ksdmin:ksdmax,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   c_sedtot(ksdmin:ksdmax,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   poro(ksdmin:ksdmax,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   dzs(ksdmin:ksdmax,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   flx_s2w(-1:nv_adv,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   flx_w2s(-1:nv_adv,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   flx_w2s_sum(-1:nv_adv,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   corflux(1:nv_adv,liminm1:limaxp1 , ljminm1:ljmaxp1  )=1.0_rsh
   corfluy(1:nv_adv,liminm1:limaxp1 , ljminm1:ljmaxp1)=1.0_rsh
   fludif(-1:nv_adv,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   fluconsol(-1:nv_adv,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   fluconsol_drycell(-1:nv_adv,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   flu_dyninsed(-1:nv_adv,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   gradvit(kmax,limin  :limax   , ljmin  :ljmax)=0.0_rsh
   
   ALLOCATE(phieau_s2w(limin  :limax   , ljmin  :ljmax))
   ALLOCATE(phieau_s2w_drycell(limin  :limax   , ljmin  :ljmax))
   ALLOCATE(phieau_s2w_consol(limin  :limax   , ljmin  :ljmax))
   phieau_s2w(limin  :limax   , ljmin  :ljmax)=0.0_rlg
   phieau_s2w_drycell(limin  :limax   , ljmin  :ljmax)=0.0_rlg
   phieau_s2w_consol(limin  :limax   , ljmin  :ljmax)=0.0_rlg

   ALLOCATE( raphbx(liminm1:limaxp1 , ljminm1:ljmaxp1),raphby(liminm1:limaxp1 , ljminm1:ljmaxp1) )
   ALLOCATE( frofonx(liminm1:limaxp1 , ljminm1:ljmaxp1),frofony(liminm1:limaxp1 , ljminm1:ljmaxp1) )
   ALLOCATE( dry_cell(limin  :limax   , ljmin  :ljmax))
   raphbx(liminm1:limaxp1 , ljminm1:ljmaxp1)=0.0_rsh
   raphby(liminm1:limaxp1 , ljminm1:ljmaxp1)=0.0_rsh
   dry_cell(limin  :limax   , ljmin  :ljmax)=0

   ALLOCATE(flx_s2w_corim1(-1:nv_adv,liminm1:limaxp1 , ljminm1:ljmaxp1))
   ALLOCATE(flx_s2w_corip1(-1:nv_adv,liminm1:limaxp1 , ljminm1:ljmaxp1))
   ALLOCATE(flx_s2w_corjm1(-1:nv_adv,liminm1:limaxp1 , ljminm1:ljmaxp1))
   ALLOCATE(flx_s2w_corjp1(-1:nv_adv,liminm1:limaxp1 , ljminm1:ljmaxp1))
   ALLOCATE(flx_w2s_corin(nvp,limin  :limax   , ljmin  :ljmax))
   ALLOCATE(flx_w2s_corim1(nvp,liminm1:limaxp1 , ljminm1:ljmaxp1))
   ALLOCATE(flx_w2s_corip1(nvp,liminm1:limaxp1 , ljminm1:ljmaxp1))
   ALLOCATE(flx_w2s_corjm1(nvp,liminm1:limaxp1 , ljminm1:ljmaxp1))
   ALLOCATE(flx_w2s_corjp1(nvp,liminm1:limaxp1 , ljminm1:ljmaxp1))
   
! relatif to initialization
   ALLOCATE(cini_sed(nv_state))
   

   IF ((.NOT.l_testcase) .OR. (l_testcase .AND. l_filesubsr)) THEN
      DO iv=1,nvp
        ros(iv)=ros_r(irk_fil(iv))
        diam_sed(iv)=diam_r(irk_fil(iv))
      ENDDO
! def of inital concentrations in sediment and for particulate variable MUDB
! conversion of initial value in sediment expressed in mmole/kg (as NoCP variable)
! in kg/kg of sediment
      DO iv=1,nv_state
        cini_sed(iv)=cini_sed_r(irk_fil(iv))*unit_modif_mudbio_N2dw(irk_fil(iv))
      ENDDO
   ENDIF



      nv_use=nvp


   
   !PRINT*, 'FIN MUSTANG_ALLOC'
   
   END SUBROUTINE MUSTANG_alloc




END MODULE comMUSTANG

