












module flocmod
!
! This module manage the flocculation distribution (in one cell "i,j,k").
!
! No use, no commons, no include this module is independant.
!
! Authors : R.Verney et all, 2011
! adaptation from MARS version to an independant module S.Le Gac, 2022
!

implicit none
private
! variables
                                             
! subroutines
                        
      public :: flocmod_alloc
                       
      public :: flocmod_init
                       
      public :: flocmod_main
                         
      public :: flocmod_comp_g

! Declaration

! parameters
                                                                 
                                                                 
          

#include "Module_Declar_flocmod.h"
                                                                 
                                                                 
               
                                                                 
                                                                 
    

! variables of the module
                                        
                               
                                                                 
                                                                 
                               
                                                             

!TODO add units !!
                                                                 
                                                                 
     
                                                          
                                                        
                                                         
                                                      

                                                                 
                                                                 
                            
                                                                 
                                                                 
                                 

                                                                 
                                                                 
           
                                                                 
                                                                 
                           
                                                                 
                                                                 
         
                                                                 
                                                                 
                           
                                                                 
                                                                 
   
                                                                 
                                                                 
 

                                                         
                                                                 
                                                                 
        
                                                                 
                                                                 
   
                                                                 
                                                                 
          
    ! the eroded particle size - typically 1)
                                                               
    ! flocs fragmented as half the size of the initial binary fragments 
    ! (0.0 if full binary fragmentation, 0.5 if ternary fragmentation)
                                                          
    ! (default 10e-6 microns)
                                                           
    ! (default 0.05)
                                                            
    ! (default 2.0)
                                            
    ! flocculation/fragmentation allowed before redistribution 
    ! (default 0.001 g/l)
                                                                 
    ! to fragmentation by collision (default 0.0, must be less than 1.0)
                                                                 
                                                                 
 
    ! by collision .. (default : =3./16.)
                                                              
    ! (default =0.1) (McAnally, 1999)
                                                        
    ! (Winterwerp, 2002)
                                                        
                                                     
    ! (default 0.15)
                                                       
                                                            
    ! fragmentation (default 2.0 as binary fragmentation)
                                                                 
    ! 1.6 to 2.8)
                                                             
    !processes are not calculated

 
      contains


!==============================================================================
subroutine flocmod_alloc(n)      


      use Agrif_Util
#include "Param_BeforeCall_flocmod_alloc.h"
      integer, intent(in) :: n
      

        call Sub_Loop_flocmod_alloc(n, Agrif_tabvars(21)%array1, Agrif_t&
     &abvars(20)%array2, Agrif_tabvars(19)%array2, Agrif_tabvars(18)%arr&
     &ay2, Agrif_tabvars(17)%array3, Agrif_tabvars(16)%array3, Agrif_tab&
     &vars(15)%array2, Agrif_tabvars(14)%array2, Agrif_tabvars(13)%array&
     &1, Agrif_tabvars(12)%array1, Agrif_tabvars(11)%array1, Agrif_tabva&
     &rs(10)%array1, Agrif_tabvars(9)%array1, Agrif_tabvars_i(189)%iarra&
     &y0)

      end subroutine flocmod_alloc


#include "Sub_Loop_flocmod_alloc.h"

!==================================================================================================
subroutine flocmod_init(l_ADS_in, l_ASH_in, l_COLLFRAG_in,       &
    f_dp0_in, f_nf_in, f_nb_frag_in, f_alpha_in, f_beta_in, f_ater_in,    &
    f_ero_frac_in, f_ero_nbfrag_in, f_ero_iv_in, f_mneg_param_in,   &
    f_collfragparam_in, f_dmin_frag_in, f_cfcst_in, f_fp_in, f_fy_in,  &
    f_clim_in, f_diam_in, ros_in, rhoref_in, l_0Dcase_in, ierrorlog_in)      


      use Agrif_Util
#include "Param_BeforeCall_flocmod_init.h"
      logical, intent(in) :: l_ASH_in
      logical, intent(in) :: l_ADS_in
      logical, intent(in) :: l_COLLFRAG_in
      integer, intent(in) :: f_ero_iv_in
      real, intent(in) :: f_ater_in
      real, intent(in) :: f_dmin_frag_in
      real, intent(in) :: f_ero_frac_in
      real, intent(in) :: f_ero_nbfrag_in
      real, intent(in) :: f_mneg_param_in
      real, intent(in) :: f_collfragparam_in
      real, intent(in) :: f_cfcst_in
      real, intent(in) :: f_fp_in
      real, intent(in) :: f_fy_in
      real, intent(in) :: f_dp0_in
      real, intent(in) :: f_alpha_in
      real, intent(in) :: f_beta_in
      real, intent(in) :: f_nb_frag_in
      real, intent(in) :: f_nf_in
      real, intent(in) :: f_clim_in
      real, intent(in), dimension(1: Agrif_tabvars_i(189)%iarray0) :: f_&
     &diam_in
      real, intent(in), dimension(1: Agrif_tabvars_i(189)%iarray0) :: ro&
     &s_in
      real, intent(in) :: rhoref_in
      logical, intent(in) :: l_0Dcase_in
      integer, intent(in) :: ierrorlog_in
      

        call Sub_Loop_flocmod_init(l_ADS_in,l_ASH_in,l_COLLFRAG_in,f_dp0&
     &_in,f_nf_in,f_nb_frag_in,f_alpha_in,f_beta_in,f_ater_in,f_ero_frac&
     &_in,f_ero_nbfrag_in,f_ero_iv_in,f_mneg_param_in,f_collfragparam_in&
     &,f_dmin_frag_in,f_cfcst_in,f_fp_in,f_fy_in,f_clim_in,f_diam_in,ros&
     &_in,rhoref_in,l_0Dcase_in,ierrorlog_in, Agrif_tabvars(9)%array1, A&
     &grif_tabvars(13)%array1, Agrif_tabvars(12)%array1, Agrif_tabvars(1&
     &1)%array1, Agrif_tabvars(10)%array1, Agrif_tabvars_i(190)%iarray0,&
     & Agrif_tabvars_l(19)%larray0, Agrif_tabvars_r(55)%array0, Agrif_ta&
     &bvars_r(70)%array0, Agrif_tabvars_r(63)%array0, Agrif_tabvars_r(62&
     &)%array0, Agrif_tabvars_r(57)%array0, Agrif_tabvars_r(61)%array0, &
     &Agrif_tabvars_r(60)%array0, Agrif_tabvars_i(191)%iarray0, Agrif_ta&
     &bvars_r(59)%array0, Agrif_tabvars_r(58)%array0, Agrif_tabvars_r(56&
     &)%array0, Agrif_tabvars_r(67)%array0, Agrif_tabvars_r(66)%array0, &
     &Agrif_tabvars_r(68)%array0, Agrif_tabvars_r(69)%array0, Agrif_tabv&
     &ars_r(65)%array0, Agrif_tabvars_l(22)%larray0, Agrif_tabvars_l(20)&
     &%larray0, Agrif_tabvars_l(21)%larray0, Agrif_tabvars_i(189)%iarray&
     &0)

      end subroutine flocmod_init


#include "Sub_Loop_flocmod_init.h"


!==================================================================================================
subroutine flocmod_kernels()      


      use Agrif_Util
#include "Param_BeforeCall_flocmod_kernels.h"
      

        call Sub_Loop_flocmod_kernels( Agrif_tabvars(21)%array1, Agrif_t&
     &abvars(19)%array2, Agrif_tabvars(18)%array2, Agrif_tabvars_r(59)%a&
     &rray0, Agrif_tabvars_i(191)%iarray0, Agrif_tabvars_r(69)%array0, A&
     &grif_tabvars_r(65)%array0, Agrif_tabvars_r(67)%array0, Agrif_tabva&
     &rs_r(56)%array0, Agrif_tabvars_r(58)%array0, Agrif_tabvars(20)%arr&
     &ay2, Agrif_tabvars_r(68)%array0, Agrif_tabvars_r(57)%array0, Agrif&
     &_tabvars(10)%array1, Agrif_tabvars(15)%array2, Agrif_tabvars(17)%a&
     &rray3, Agrif_tabvars(14)%array2, Agrif_tabvars_r(66)%array0, Agrif&
     &_tabvars(16)%array3, Agrif_tabvars(13)%array1, Agrif_tabvars_i(189&
     &)%iarray0)

      end subroutine flocmod_kernels


#include "Sub_Loop_flocmod_kernels.h"


!==================================================================================================
subroutine flocmod_agregation_statistics
      


      use Agrif_Util
#include "Param_BeforeCall_flocmod_agregation_statistics.h"
      

        call Sub_Loop_flocmod_agregation_statistics( Agrif_tabvars(9)%ar&
     &ray1, Agrif_tabvars(15)%array2, Agrif_tabvars(10)%array1, Agrif_ta&
     &bvars(14)%array2, Agrif_tabvars_i(189)%iarray0)

      end subroutine flocmod_agregation_statistics


#include "Sub_Loop_flocmod_agregation_statistics.h"


!==================================================================================================
subroutine flocmod_comp_g(Gval, time)
      use Agrif_Types, only : Agrif_tabvars

      implicit none

#include "Param_BeforeCall_flocmod_comp_g.h"
      real, intent(out) :: Gval
      real(rlg), intent(in) :: time

! Purpose : compute shear rate to estimate shear aggregation and erosion  
! reproducing flocculation experiment Verney et al., 2011
             
 
    
                            
                                                        

Gval=0.0
if (time .lt. 7201.0) then
    Gval=1.0
elseif (time .lt. 8401.0) then
    Gval=2.0
elseif (time .lt. 9601.0) then
    Gval=3.0  
elseif (time .lt. 10801.0) then
    Gval=4.0
elseif (time .lt. 12601.0) then
    Gval=12.0
elseif (time .lt. 13801.0) then
    Gval=4.0
elseif (time .lt. 15001.0) then
    Gval=3.0
elseif (time .lt. 16201.0) then
    Gval=2.0
elseif (time .lt. 21601.0) then
    Gval=1.0
elseif (time .lt. 25201.0) then
    Gval=0.0
elseif (time .lt. 30601.0) then
    Gval=1.0
elseif (time .lt. 31801.0) then
    Gval=2.0                     
elseif (time .lt. 33001.0) then
    Gval=3.0       
elseif (time .lt. 34201.0) then
    Gval=4.0
elseif (time .lt. 36001.0) then
    Gval=12.0
elseif (time .lt. 37201.0) then
    Gval=4.0
elseif (time .lt. 38401.0) then
    Gval=3.0
elseif (time .lt. 39601.0) then
    Gval=2.0
elseif (time .lt. 45001.0) then
    Gval=1.0
elseif (time .lt. 48601.0) then
    Gval=0.0
elseif (time .lt. 54001.0) then
    Gval=1.0
elseif (time .lt. 55201.0) then
    Gval=2.0                     
elseif (time .lt. 56401.0) then
    Gval=3.0       
elseif (time .lt. 57601.0) then
    Gval=4.0
else 
    Gval=12.0
endif

return
end subroutine flocmod_comp_g

!==================================================================================================
subroutine flocmod_main(dt, cwater, gradvit)      


      use Agrif_Util
#include "Param_BeforeCall_flocmod_main.h"
      real(rlg), intent(in) :: dt
      real, intent(in) :: gradvit
      real, intent(inout), dimension(1: Agrif_tabvars_i(189)%iarray0) ::&
     & cwater
      

        call Sub_Loop_flocmod_main(dt,cwater,gradvit, Agrif_tabvars_i(19&
     &0)%iarray0, Agrif_tabvars_r(60)%array0, Agrif_tabvars_r(70)%array0&
     &, Agrif_tabvars(13)%array1, Agrif_tabvars_i(189)%iarray0)

      end subroutine flocmod_main


#include "Sub_Loop_flocmod_main.h"


!==================================================================================================
subroutine flocmod_comp_fsd(NNin, NNout, Gval, f_dt)      


      use Agrif_Util
#include "Param_BeforeCall_flocmod_comp_fsd.h"
      real, intent(in) :: Gval
      real(rlg), intent(in) :: f_dt
      real, intent(in), dimension(1: Agrif_tabvars_i(189)%iarray0) :: NN&
     &in
      real, intent(out), dimension(1: Agrif_tabvars_i(189)%iarray0) :: N&
     &Nout
      

        call Sub_Loop_flocmod_comp_fsd(NNin,NNout,Gval,f_dt, Agrif_tabva&
     &rs(21)%array1, Agrif_tabvars(19)%array2, Agrif_tabvars(18)%array2,&
     & Agrif_tabvars(20)%array2, Agrif_tabvars(17)%array3, Agrif_tabvars&
     &_l(21)%larray0, Agrif_tabvars(16)%array3, Agrif_tabvars_l(20)%larr&
     &ay0, Agrif_tabvars_l(22)%larray0, Agrif_tabvars_i(189)%iarray0)

      end subroutine flocmod_comp_fsd


#include "Sub_Loop_flocmod_comp_fsd.h"


!==================================================================================================
subroutine flocmod_collfrag(Gval, f_g4, f_l4)      


      use Agrif_Util
#include "Param_BeforeCall_flocmod_collfrag.h"
      real, intent(in) :: Gval
      real, intent(out), dimension(1: Agrif_tabvars_i(189)%iarray0,1: Ag&
     &rif_tabvars_i(189)%iarray0,1: Agrif_tabvars_i(189)%iarray0) :: f_g&
     &4
      real, intent(out), dimension(1: Agrif_tabvars_i(189)%iarray0,1: Ag&
     &rif_tabvars_i(189)%iarray0) :: f_l4
      

        call Sub_Loop_flocmod_collfrag(Gval,f_g4,f_l4, Agrif_tabvars_r(6&
     &1)%array0, Agrif_tabvars(14)%array2, Agrif_tabvars_r(62)%array0, A&
     &grif_tabvars_r(69)%array0, Agrif_tabvars_r(55)%array0, Agrif_tabva&
     &rs(12)%array1, Agrif_tabvars_r(63)%array0, Agrif_tabvars_r(64)%arr&
     &ay0, Agrif_tabvars(13)%array1, Agrif_tabvars(10)%array1, Agrif_tab&
     &vars_i(189)%iarray0)

      end subroutine flocmod_collfrag


#include "Sub_Loop_flocmod_collfrag.h"


!==================================================================================================
subroutine flocmod_mass_control(NN,mneg)      


      use Agrif_Util
#include "Param_BeforeCall_flocmod_mass_control.h"
      real, intent(in), dimension(1: Agrif_tabvars_i(189)%iarray0) :: NN
      real, intent(out) :: mneg
      

        call Sub_Loop_flocmod_mass_control(NN,mneg, Agrif_tabvars(13)%ar&
     &ray1, Agrif_tabvars_i(189)%iarray0)

      end subroutine flocmod_mass_control


#include "Sub_Loop_flocmod_mass_control.h"


!==================================================================================================
subroutine flocmod_mass_redistribute(NN)      


      use Agrif_Util
#include "Param_BeforeCall_flocmod_mass_redistribute.h"
      real, intent(inout), dimension(1: Agrif_tabvars_i(189)%iarray0) ::&
     & NN
      

        call Sub_Loop_flocmod_mass_redistribute(NN, Agrif_tabvars_i(190)&
     &%iarray0, Agrif_tabvars(13)%array1, Agrif_tabvars_i(189)%iarray0)

      end subroutine flocmod_mass_redistribute


#include "Sub_Loop_flocmod_mass_redistribute.h"


!==================================================================================================  
 
      end module flocmod

