!|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

 module brine

!BOP
! !MODULE: brine
!
! !DESCRIPTION:
!  This module contains routines for initializing and computing
!  brine parameterizations.  (see Jin, M., Hutchings, J., Kawaguchi, Y. 
!  et al. J. Ocean Univ. China (2012) 11: 473. doi:10.1007/s11802-012-2094-4)
!
! !REVISION HISTORY:
!  SVN:$Id: brine.F90 38648 2012-07-12 19:51:27Z mlevy@ucar.edu $

! !USES:

   use kinds_mod
   use blocks
   use distribution
   use domain_size
   use domain
   use constants
   use grid
   use broadcast
   use io
   use state_mod
   use exit_mod
   use io_types, only: stdout
   use communicate, only: my_task, master_task
   use registry
   use time_management
   use forcing_fields, only: ifrac
   use prognostic, only: UVEL, VVEL, curtime !cro
   
   implicit none
   private
   save


! !PUBLIC MEMBER FUNCTIONS:
   public :: calc_saltfpara

! !PUBLIC DATA MEMBERS:
   real (r8), dimension(nx_block,ny_block,max_blocks_clinic),public ::  &
      SALT_Fpara = c0     ! salt flux for brine rejection parameterization

   integer (int_kind), parameter, public :: &
      brine_type_none    = 1, & ! integer identifiers for desired
      brine_type_jin2012 = 2    ! brine parameterization

   integer (int_kind), public :: &
      brine_itype             ! users choice for brine parameterization

!EOC
!***********************************************************************

 contains

!***********************************************************************

 subroutine calc_saltfpara(k, SALTFK, HMXL, this_block)

 !! Parameterization of brine rejection, Jin et al., 2012

   integer (int_kind), intent(in) :: &
      k                   ! level
   real (r8), dimension(nx_block,ny_block), intent(out) :: &
      SALTFK              ! brine rejection distributed in layer k
   real (r8), dimension(nx_block,ny_block), intent(in) :: &
      HMXL                ! HMXL array from kpp (for now)
   type (block), intent(in) :: &
      this_block          ! block info for current block

   real (r8) :: npower, aaJin, z1Jin, z2Jin, npower1, hmxlJin
   integer (int_kind) :: i, j, ii, jj, bid

   bid = this_block%local_id
   SALTFK = c0

   do j=this_block%jb,this_block%je
   do i=this_block%ib,this_block%ie
      if (SALT_Fpara(i,j,bid) == c0) cycle
      aaJin = c1 - IFRAC(i,j,bid)
      if (aaJin <= 1.0d-6) aaJin=1.0d-6
      npower= 0.360341_r8*aaJin**(-0.364781_r8)-0.605068_r8  !2012
      npower1=npower+c1
      hmxlJin=HMXL(i,j)+9.0_r8
      if (hmxlJin < zw(1)) hmxlJin = zw(1)

      if (IFRAC(i,j,bid) < 0.8_r8) hmxlJin=zw(1) !! Jin 2015-11-29
       if (sqrt((UVEL(i,j,1,curtime,bid)**2+VVEL(i,j,1,curtime,bid)**2)) > 15._r8) hmxlJin=zw(1) !cro limit on ocen vel. 
      aaJin = SALT_Fpara(i,j,bid) * npower1 / hmxlJin**npower1
      if (zw(k)-dz(k) > hmxlJin) cycle
      z1Jin = zw(k) - dz(k)
      z2Jin = zw(k)
      if (z2Jin > hmxlJin) z2Jin=hmxlJin
      SALTFK(i,j) = aaJin * (z2Jin**npower1 - z1Jin**npower1) / npower1
   end do
   end do

 end subroutine calc_saltfpara

!***********************************************************************

 end module brine

!|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
