\hypertarget{namespacemom__set__diffusivity}{}\section{mom\+\_\+set\+\_\+diffusivity Module Reference}
\label{namespacemom__set__diffusivity}\index{mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}}


\subsection{Detailed Description}
Calculate vertical diffusivity from all mixing processes. \subsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__set__diffusivity_1_1diffusivity__diags}{diffusivity\+\_\+diags}}
\begin{DoxyCompactList}\small\item\em This structure has memory for used in calculating diagnostics of diffusivity. \end{DoxyCompactList}\item 
type \mbox{\hyperlink{structmom__set__diffusivity_1_1set__diffusivity__cs}{set\+\_\+diffusivity\+\_\+cs}}
\begin{DoxyCompactList}\small\item\em This control structure contains parameters for M\+O\+M\+\_\+set\+\_\+diffusivity. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \mbox{\hyperlink{namespacemom__set__diffusivity_a87687e318bfe2d594ce1f57cb5d191e4}{set\+\_\+diffusivity}} (u, v, h, u\+\_\+h, v\+\_\+h, tv, fluxes, optics, visc, dt, G, GV, US, CS, Kd\+\_\+lay, Kd\+\_\+int, Kd\+\_\+extra\+\_\+T, Kd\+\_\+extra\+\_\+S)
\begin{DoxyCompactList}\small\item\em Sets the interior vertical diffusion of scalars due to the following processes\+: \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__set__diffusivity_a07c0ab3f141f8f9e057be3150a940a94}{find\+\_\+tke\+\_\+to\+\_\+kd}} (h, tv, d\+Rho\+\_\+int, N2\+\_\+lay, j, dt, G, GV, US, CS, T\+K\+E\+\_\+to\+\_\+\+Kd, max\+T\+KE, kb)
\begin{DoxyCompactList}\small\item\em Convert turbulent kinetic energy to diffusivity. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__set__diffusivity_afef80c2221be24f63f1ca96c2abe6fa9}{find\+\_\+n2}} (h, tv, T\+\_\+f, S\+\_\+f, fluxes, j, G, GV, US, CS, d\+Rho\+\_\+int, N2\+\_\+lay, N2\+\_\+int, N2\+\_\+bot)
\begin{DoxyCompactList}\small\item\em Calculate Brunt-\/\+Vaisala frequency, N$^\wedge$2. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__set__diffusivity_a99d0eb7701f8e04d856b75117fe7b83c}{double\+\_\+diffusion}} (tv, h, T\+\_\+f, S\+\_\+f, j, G, GV, US, CS, Kd\+\_\+\+T\+\_\+dd, Kd\+\_\+\+S\+\_\+dd)
\begin{DoxyCompactList}\small\item\em This subroutine sets the additional diffusivities of temperature and salinity due to double diffusion, using the same functional form as is used in M\+O\+M4.\+1, and taken from an N\+C\+AR technical note (R\+EF?) that updates what was in Large et al. (1994). All the coefficients here should probably be made run-\/time variables rather than hard-\/coded constants. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__set__diffusivity_ac48033315c2bbdb5551b272a235c16e5}{add\+\_\+drag\+\_\+diffusivity}} (h, u, v, tv, fluxes, visc, j, T\+K\+E\+\_\+to\+\_\+\+Kd, max\+T\+KE, kb, G, GV, US, CS, Kd\+\_\+lay, Kd\+\_\+int, Kd\+\_\+\+B\+BL)
\begin{DoxyCompactList}\small\item\em This routine adds diffusion sustained by flow energy extracted by bottom drag. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__set__diffusivity_a968690a1c9efb859ee67965f9032c7d7}{add\+\_\+lotw\+\_\+bbl\+\_\+diffusivity}} (h, u, v, tv, fluxes, visc, j, N2\+\_\+int, G, GV, US, CS, Kd\+\_\+\+B\+BL, Kd\+\_\+lay, Kd\+\_\+int)
\begin{DoxyCompactList}\small\item\em Calculates a B\+BL diffusivity use a Prandtl number 1 diffusivity with a law of the wall turbulent viscosity, up to a B\+BL height where the energy used for mixing has consumed the mechanical T\+KE input. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__set__diffusivity_a21162cb5173d52ce92dc65beee9d0ef1}{add\+\_\+mlrad\+\_\+diffusivity}} (h, fluxes, j, G, GV, US, CS, T\+K\+E\+\_\+to\+\_\+\+Kd, Kd\+\_\+lay, Kd\+\_\+int)
\begin{DoxyCompactList}\small\item\em This routine adds effects of mixed layer radiation to the layer diffusivities. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__set__diffusivity_a66f77b7e2f9c0c8254da4a0acd5a9996}{set\+\_\+bbl\+\_\+tke}} (u, v, h, fluxes, visc, G, GV, US, CS, O\+BC)
\begin{DoxyCompactList}\small\item\em This subroutine calculates several properties related to bottom boundary layer turbulence. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__set__diffusivity_a5ba8a3be6234304aa5f1dfd0b831078a}{set\+\_\+density\+\_\+ratios}} (h, tv, kb, G, GV, US, CS, j, ds\+\_\+dsp1, rho\+\_\+0)
\item 
subroutine, public \mbox{\hyperlink{namespacemom__set__diffusivity_ac0c0f4b8458cb5610514ee068482f4bb}{set\+\_\+diffusivity\+\_\+init}} (Time, G, GV, US, param\+\_\+file, diag, CS, int\+\_\+tide\+\_\+\+C\+Sp, halo\+\_\+\+TS, double\+\_\+diffuse)
\item 
subroutine, public \mbox{\hyperlink{namespacemom__set__diffusivity_ace82f133d3cee42aa36ec10bcce79e75}{set\+\_\+diffusivity\+\_\+end}} (CS)
\begin{DoxyCompactList}\small\item\em Clear pointers and dealocate memory. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Variables}
\textbf{ }\par
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{namespacemom__set__diffusivity_a6b2144f31cb69fe2481815545253d7c9}\label{namespacemom__set__diffusivity_a6b2144f31cb69fe2481815545253d7c9}} 
integer \mbox{\hyperlink{namespacemom__set__diffusivity_a6b2144f31cb69fe2481815545253d7c9}{id\+\_\+clock\+\_\+kappashear}}
\begin{DoxyCompactList}\small\item\em C\+PU time clocks. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__set__diffusivity_a7ceffee2f68b88087dafa02f6235c4fc}\label{namespacemom__set__diffusivity_a7ceffee2f68b88087dafa02f6235c4fc}} 
integer \mbox{\hyperlink{namespacemom__set__diffusivity_a7ceffee2f68b88087dafa02f6235c4fc}{id\+\_\+clock\+\_\+cvmix\+\_\+ddiff}}
\begin{DoxyCompactList}\small\item\em C\+PU time clocks. \end{DoxyCompactList}\end{DoxyCompactItemize}



\subsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__set__diffusivity_ac48033315c2bbdb5551b272a235c16e5}\label{namespacemom__set__diffusivity_ac48033315c2bbdb5551b272a235c16e5}} 
\index{mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}!add\+\_\+drag\+\_\+diffusivity@{add\+\_\+drag\+\_\+diffusivity}}
\index{add\+\_\+drag\+\_\+diffusivity@{add\+\_\+drag\+\_\+diffusivity}!mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}}
\subsubsection{\texorpdfstring{add\+\_\+drag\+\_\+diffusivity()}{add\_drag\_diffusivity()}}
{\footnotesize\ttfamily subroutine mom\+\_\+set\+\_\+diffusivity\+::add\+\_\+drag\+\_\+diffusivity (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb, g \%ke), intent(in)}]{v,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{type(forcing), intent(in)}]{fluxes,  }\item[{type(vertvisc\+\_\+type), intent(in)}]{visc,  }\item[{integer, intent(in)}]{j,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke), intent(in)}]{T\+K\+E\+\_\+to\+\_\+\+Kd,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke), intent(in)}]{max\+T\+KE,  }\item[{integer, dimension( g \%isd\+: g \%ied), intent(in)}]{kb,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__set__diffusivity_1_1set__diffusivity__cs}{set\+\_\+diffusivity\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke), intent(inout)}]{Kd\+\_\+lay,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke+1), intent(inout), optional}]{Kd\+\_\+int,  }\item[{real, dimension(\+:,\+:,\+:), pointer}]{Kd\+\_\+\+B\+BL }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This routine adds diffusion sustained by flow energy extracted by bottom drag. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em u} & The zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em v} & The meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em tv} & Structure containing pointers to any available thermodynamic fields.\\
\hline
\mbox{\tt in}  & {\em fluxes} & A structure of thermodynamic surface fluxes\\
\hline
\mbox{\tt in}  & {\em visc} & Structure containing vertical viscosities, bottom boundary layer properies, and related fields\\
\hline
\mbox{\tt in}  & {\em j} & j-\/index of row to work on\\
\hline
\mbox{\tt in}  & {\em tke\+\_\+to\+\_\+kd} & The conversion rate between the T\+KE T\+KE dissipated within a layer and the diapycnal diffusivity witin that layer, usually ($\sim$\+Rho\+\_\+0 / (G\+\_\+\+Earth $\ast$ d\+Rho\+\_\+lay)) \mbox{[}Z2 T-\/1 / Z3 T-\/3 = T2 Z-\/1 $\sim$$>$ s2 m-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em maxtke} & The energy required to for a layer to entrain to its maximum-\/realizable thickness \mbox{[}Z3 T-\/3 $\sim$$>$ m3 s-\/3\mbox{]}\\
\hline
\mbox{\tt in}  & {\em kb} & Index of lightest layer denser than the buffer layer, or -\/1 without a bulk mixed layer\\
\hline
 & {\em cs} & Diffusivity control structure\\
\hline
\mbox{\tt in,out}  & {\em kd\+\_\+lay} & The diapycnal diffusivity in layers, \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em kd\+\_\+int} & The diapycnal diffusivity at interfaces,\\
\hline
 & {\em kd\+\_\+bbl} & Interface B\+BL diffusivity \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 1177 of file M\+O\+M\+\_\+set\+\_\+diffusivity.\+F90.


\begin{DoxyCode}
1177   \textcolor{keywordtype}{type}(ocean\_grid\_type),            \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure}
1178   \textcolor{keywordtype}{type}(verticalGrid\_type),          \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure}
1179   \textcolor{keywordtype}{type}(unit\_scale\_type),            \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}
1180   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, &
1181                                     \textcolor{keywordtype}{intent(in)}    :: u\textcolor{comment}{    !< The zonal velocity [L T-1 ~> m s-1]}
1182   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, &
1183                                     \textcolor{keywordtype}{intent(in)}    :: v\textcolor{comment}{    !< The meridional velocity [L T-1 ~> m s-1]}
1184   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
1185                                     \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{    !< Layer thicknesses [H ~> m or kg m-2]}
1186   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),            \textcolor{keywordtype}{intent(in)}    :: tv\textcolor{comment}{   !< Structure containing pointers to any available}
1187 \textcolor{comment}{                                                          !! thermodynamic fields.}
1188   \textcolor{keywordtype}{type}(forcing),                    \textcolor{keywordtype}{intent(in)}    :: fluxes\textcolor{comment}{ !< A structure of thermodynamic surface fluxes}
1189   \textcolor{keywordtype}{type}(vertvisc\_type),              \textcolor{keywordtype}{intent(in)}    :: visc\textcolor{comment}{ !< Structure containing vertical viscosities,
       bottom}
1190 \textcolor{comment}{                                                          !! boundary layer properies, and related fields}
1191   \textcolor{keywordtype}{integer},                          \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{    !< j-index of row to work on}
1192   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}    :: TKE\_to\_Kd\textcolor{comment}{ !< The conversion rate between the TKE}
1193 \textcolor{comment}{                                                          !! TKE dissipated within  a layer and the}
1194 \textcolor{comment}{                                                          !! diapycnal diffusivity witin that layer,}
1195 \textcolor{comment}{                                                          !! usually (~Rho\_0 / (G\_Earth * dRho\_lay))}
1196 \textcolor{comment}{                                                          !! [Z2 T-1 / Z3 T-3 = T2 Z-1 ~> s2 m-1]}
1197   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}    :: maxTKE\textcolor{comment}{ !< The energy required to for a layer to
       entrain}
1198 \textcolor{comment}{                                                          !! to its maximum-realizable thickness [Z3 T-3 ~>
       m3 s-3]}
1199   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G))},      \textcolor{keywordtype}{intent(in)}    :: kb\textcolor{comment}{   !< Index of lightest layer denser than the buffer}
1200 \textcolor{comment}{                                                          !! layer, or -1 without a bulk mixed layer}
1201   \textcolor{keywordtype}{type}(set\_diffusivity\_CS),         \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< Diffusivity control structure}
1202   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(inout)} :: Kd\_lay\textcolor{comment}{ !< The diapycnal diffusivity in layers,}
1203 \textcolor{comment}{                                                            !! [Z2 T-1 ~> m2 s-1].}
1204   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)}, &
1205                           \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: Kd\_int\textcolor{comment}{ !< The diapycnal diffusivity at interfaces,}
1206 \textcolor{comment}{                                                            !! [Z2 T-1 ~> m2 s-1].}
1207   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:,:,:)},           \textcolor{keywordtype}{pointer}       :: Kd\_BBL\textcolor{comment}{ !< Interface BBL diffusivity [Z2 T-1 ~> m2
       s-1].}
1208 
1209 \textcolor{comment}{! This routine adds diffusion sustained by flow energy extracted by bottom drag.}
1210 
1211   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZK\_(G)+1)} :: &
1212     Rint          \textcolor{comment}{! coordinate density of an interface [R ~> kg m-3]}
1213   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: &
1214     htot, &       \textcolor{comment}{! total thickness above or below a layer, or the}
1215                   \textcolor{comment}{! integrated thickness in the BBL [Z ~> m].}
1216     rho\_htot, &   \textcolor{comment}{! running integral with depth of density [Z R ~> kg m-2]}
1217     gh\_sum\_top, & \textcolor{comment}{! BBL value of g'h that can be supported by}
1218                   \textcolor{comment}{! the local ustar, times R0\_g [R ~> kg m-2]}
1219     rho\_top, &    \textcolor{comment}{! density at top of the BBL [R ~> kg m-3]}
1220     tke, &        \textcolor{comment}{! turbulent kinetic energy available to drive}
1221                   \textcolor{comment}{! bottom-boundary layer mixing in a layer [Z3 T-3 ~> m3 s-3]}
1222     i2decay       \textcolor{comment}{! inverse of twice the TKE decay scale [Z-1 ~> m-1].}
1223 
1224   \textcolor{keywordtype}{real}    :: TKE\_to\_layer   \textcolor{comment}{! TKE used to drive mixing in a layer [Z3 T-3 ~> m3 s-3]}
1225   \textcolor{keywordtype}{real}    :: TKE\_Ray        \textcolor{comment}{! TKE from layer Rayleigh drag used to drive mixing in layer [Z3 T-3 ~> m3 s-3]}
1226   \textcolor{keywordtype}{real}    :: TKE\_here       \textcolor{comment}{! TKE that goes into mixing in this layer [Z3 T-3 ~> m3 s-3]}
1227   \textcolor{keywordtype}{real}    :: dRl, dRbot     \textcolor{comment}{! temporaries holding density differences [R ~> kg m-3]}
1228   \textcolor{keywordtype}{real}    :: cdrag\_sqrt     \textcolor{comment}{! square root of the drag coefficient [nondim]}
1229   \textcolor{keywordtype}{real}    :: ustar\_h        \textcolor{comment}{! value of ustar at a thickness point [Z T-1 ~> m s-1].}
1230   \textcolor{keywordtype}{real}    :: absf           \textcolor{comment}{! average absolute Coriolis parameter around a thickness point [T-1 ~> s-1]}
1231   \textcolor{keywordtype}{real}    :: R0\_g           \textcolor{comment}{! Rho0 / G\_Earth [R T2 Z-1 m-1 ~> kg s2 m-5]}
1232   \textcolor{keywordtype}{real}    :: I\_rho0         \textcolor{comment}{! 1 / RHO0 [R-1 ~> m3 kg-1]}
1233   \textcolor{keywordtype}{real}    :: delta\_Kd       \textcolor{comment}{! increment to Kd from the bottom boundary layer mixing [Z2 T-1 ~> m2 s-1].}
1234   \textcolor{keywordtype}{logical} :: Rayleigh\_drag  \textcolor{comment}{! Set to true if Rayleigh drag velocities}
1235                             \textcolor{comment}{! defined in visc, on the assumption that this}
1236                             \textcolor{comment}{! extracted energy also drives diapycnal mixing.}
1237 
1238   \textcolor{keywordtype}{logical} :: domore, do\_i(SZI\_(G))
1239   \textcolor{keywordtype}{logical} :: do\_diag\_Kd\_BBL
1240 
1241   \textcolor{keywordtype}{integer} :: i, k, is, ie, nz, i\_rem, kb\_min
1242   is = g%isc ; ie = g%iec ; nz = g%ke
1243 
1244   do\_diag\_kd\_bbl = \textcolor{keyword}{associated}(kd\_bbl)
1245 
1246   \textcolor{keywordflow}{if} (.not.(cs%bottomdraglaw .and. (cs%BBL\_effic>0.0))) \textcolor{keywordflow}{return}
1247 
1248   cdrag\_sqrt = sqrt(cs%cdrag)
1249   tke\_ray = 0.0 ; rayleigh\_drag = .false.
1250   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc%Ray\_u) .and. \textcolor{keyword}{associated}(visc%Ray\_v)) rayleigh\_drag = .true.
1251 
1252   i\_rho0 = 1.0 / (gv%Rho0)
1253   r0\_g = gv%Rho0 / (us%L\_to\_Z**2 * gv%g\_Earth)
1254 
1255   \textcolor{keywordflow}{do} k=2,nz ; rint(k) = 0.5*(gv%Rlay(k-1)+gv%Rlay(k)) ;\textcolor{keywordflow}{ enddo}
1256 
1257   kb\_min = max(gv%nk\_rho\_varies+1,2)
1258 
1259   \textcolor{comment}{! The turbulence decay scale is 0.5*ustar/f from K&E & MOM\_vertvisc.F90}
1260   \textcolor{comment}{! Any turbulence that makes it into the mixed layers is assumed}
1261   \textcolor{comment}{! to be relatively small and is discarded.}
1262   \textcolor{keywordflow}{do} i=is,ie
1263     ustar\_h = visc%ustar\_BBL(i,j)
1264     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%ustar\_tidal)) &
1265       ustar\_h = ustar\_h + fluxes%ustar\_tidal(i,j)
1266     absf = 0.25 * ((abs(g%CoriolisBu(i-1,j-1)) + abs(g%CoriolisBu(i,j))) + &
1267                    (abs(g%CoriolisBu(i-1,j)) + abs(g%CoriolisBu(i,j-1))))
1268     \textcolor{keywordflow}{if} ((ustar\_h > 0.0) .and. (absf > 0.5*cs%IMax\_decay*ustar\_h))  \textcolor{keywordflow}{then}
1269       i2decay(i) = absf / ustar\_h
1270     \textcolor{keywordflow}{else}
1271       \textcolor{comment}{! The maximum decay scale should be something of order 200 m.}
1272       \textcolor{comment}{! If ustar\_h = 0, this is land so this value doesn't matter.}
1273       i2decay(i) = 0.5*cs%IMax\_decay
1274 \textcolor{keywordflow}{    endif}
1275     tke(i) = ((cs%BBL\_effic * cdrag\_sqrt) * exp(-i2decay(i)*(gv%H\_to\_Z*h(i,j,nz))) ) * &
1276              visc%TKE\_BBL(i,j)
1277 
1278     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%TKE\_tidal)) &
1279       tke(i) = tke(i) + fluxes%TKE\_tidal(i,j) * i\_rho0 * &
1280            (cs%BBL\_effic * exp(-i2decay(i)*(gv%H\_to\_Z*h(i,j,nz))))
1281 
1282     \textcolor{comment}{! Distribute the work over a BBL of depth 20^2 ustar^2 / g' following}
1283     \textcolor{comment}{! Killworth & Edwards (1999) and Zilitikevich & Mironov (1996).}
1284     \textcolor{comment}{! Rho\_top is determined by finding the density where}
1285     \textcolor{comment}{! integral(bottom, Z) (rho(z') - rho(Z)) dz' = rho\_0 400 ustar^2 / g}
1286 
1287     gh\_sum\_top(i) = r0\_g * 400.0 * ustar\_h**2
1288 
1289     do\_i(i) = (g%mask2dT(i,j) > 0.5)
1290     htot(i) = gv%H\_to\_Z*h(i,j,nz)
1291     rho\_htot(i) = gv%Rlay(nz)*(gv%H\_to\_Z*h(i,j,nz))
1292     rho\_top(i) = gv%Rlay(1)
1293     \textcolor{keywordflow}{if} (cs%bulkmixedlayer .and. do\_i(i)) rho\_top(i) = gv%Rlay(kb(i)-1)
1294 \textcolor{keywordflow}{  enddo}
1295 
1296   \textcolor{keywordflow}{do} k=nz-1,2,-1 ; domore = .false.
1297     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}
1298       htot(i) = htot(i) + gv%H\_to\_Z*h(i,j,k)
1299       rho\_htot(i) = rho\_htot(i) + gv%Rlay(k)*(gv%H\_to\_Z*h(i,j,k))
1300       \textcolor{keywordflow}{if} (htot(i)*gv%Rlay(k-1) <= (rho\_htot(i) - gh\_sum\_top(i))) \textcolor{keywordflow}{then}
1301         \textcolor{comment}{! The top of the mixing is in the interface atop the current layer.}
1302         rho\_top(i) = (rho\_htot(i) - gh\_sum\_top(i)) / htot(i)
1303         do\_i(i) = .false.
1304       \textcolor{keywordflow}{elseif} (k <= kb(i)) \textcolor{keywordflow}{then} ; do\_i(i) = .false.
1305       \textcolor{keywordflow}{else} ; domore = .true. ;\textcolor{keywordflow}{ endif}
1306 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
1307     \textcolor{keywordflow}{if} (.not.domore) \textcolor{keywordflow}{exit}
1308 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! k-loop}
1309 
1310   \textcolor{keywordflow}{do} i=is,ie ; do\_i(i) = (g%mask2dT(i,j) > 0.5) ;\textcolor{keywordflow}{ enddo}
1311   \textcolor{keywordflow}{do} k=nz-1,kb\_min,-1
1312     i\_rem = 0
1313     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}
1314       \textcolor{keywordflow}{if} (k<kb(i)) \textcolor{keywordflow}{then} ; do\_i(i) = .false. ; cycle ;\textcolor{keywordflow}{ endif}
1315       i\_rem = i\_rem + 1  \textcolor{comment}{! Count the i-rows that are still being worked on.}
1316       \textcolor{comment}{!   Apply vertical decay of the turbulent energy.  This energy is}
1317       \textcolor{comment}{! simply lost.}
1318       tke(i) = tke(i) * exp(-i2decay(i) * (gv%H\_to\_Z*(h(i,j,k) + h(i,j,k+1))))
1319 
1320 \textcolor{comment}{!      if (maxEnt(i,k) <= 0.0) cycle}
1321       \textcolor{keywordflow}{if} (maxtke(i,k) <= 0.0) cycle
1322 
1323   \textcolor{comment}{! This is an analytic integral where diffusity is a quadratic function of}
1324   \textcolor{comment}{! rho that goes asymptotically to 0 at Rho\_top (vaguely following KPP?).}
1325       \textcolor{keywordflow}{if} (tke(i) > 0.0) \textcolor{keywordflow}{then}
1326         \textcolor{keywordflow}{if} (rint(k) <= rho\_top(i)) \textcolor{keywordflow}{then}
1327           tke\_to\_layer = tke(i)
1328         \textcolor{keywordflow}{else}
1329           drl = rint(k+1) - rint(k) ; drbot = rint(k+1) - rho\_top(i)
1330           tke\_to\_layer = tke(i) * drl * &
1331               (3.0*drbot*(rint(k) - rho\_top(i)) + drl**2) / (drbot**3)
1332 \textcolor{keywordflow}{        endif}
1333       \textcolor{keywordflow}{else} ; tke\_to\_layer = 0.0 ;\textcolor{keywordflow}{ endif}
1334 
1335       \textcolor{comment}{! TKE\_Ray has been initialized to 0 above.}
1336       \textcolor{keywordflow}{if} (rayleigh\_drag) tke\_ray = 0.5*cs%BBL\_effic * us%L\_to\_Z**2 * g%IareaT(i,j) * &
1337             ((g%areaCu(i-1,j) * visc%Ray\_u(i-1,j,k) * u(i-1,j,k)**2 + &
1338               g%areaCu(i,j)   * visc%Ray\_u(i,j,k)   * u(i,j,k)**2) + &
1339              (g%areaCv(i,j-1) * visc%Ray\_v(i,j-1,k) * v(i,j-1,k)**2 + &
1340               g%areaCv(i,j)   * visc%Ray\_v(i,j,k)   * v(i,j,k)**2))
1341 
1342       \textcolor{keywordflow}{if} (tke\_to\_layer + tke\_ray > 0.0) \textcolor{keywordflow}{then}
1343         \textcolor{keywordflow}{if} (cs%BBL\_mixing\_as\_max) \textcolor{keywordflow}{then}
1344           \textcolor{keywordflow}{if} (tke\_to\_layer + tke\_ray > maxtke(i,k)) &
1345               tke\_to\_layer = maxtke(i,k) - tke\_ray
1346 
1347           tke(i) = tke(i) - tke\_to\_layer
1348 
1349           \textcolor{keywordflow}{if} (kd\_lay(i,k) < (tke\_to\_layer + tke\_ray) * tke\_to\_kd(i,k)) \textcolor{keywordflow}{then}
1350             delta\_kd = (tke\_to\_layer + tke\_ray) * tke\_to\_kd(i,k) - kd\_lay(i,k)
1351             \textcolor{keywordflow}{if} ((cs%Kd\_max >= 0.0) .and. (delta\_kd > cs%Kd\_max)) \textcolor{keywordflow}{then}
1352               delta\_kd = cs%Kd\_max
1353               kd\_lay(i,k) = kd\_lay(i,k) + delta\_kd
1354             \textcolor{keywordflow}{else}
1355               kd\_lay(i,k) = (tke\_to\_layer + tke\_ray) * tke\_to\_kd(i,k)
1356 \textcolor{keywordflow}{            endif}
1357             \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_int)) \textcolor{keywordflow}{then}
1358               kd\_int(i,k)   = kd\_int(i,k)   + 0.5 * delta\_kd
1359               kd\_int(i,k+1) = kd\_int(i,k+1) + 0.5 * delta\_kd
1360 \textcolor{keywordflow}{            endif}
1361             \textcolor{keywordflow}{if} (do\_diag\_kd\_bbl) \textcolor{keywordflow}{then}
1362               kd\_bbl(i,j,k) = kd\_bbl(i,j,k) + 0.5 * delta\_kd
1363               kd\_bbl(i,j,k+1) = kd\_bbl(i,j,k+1) + 0.5 * delta\_kd
1364 \textcolor{keywordflow}{            endif}
1365 \textcolor{keywordflow}{          endif}
1366         \textcolor{keywordflow}{else}
1367           \textcolor{keywordflow}{if} (kd\_lay(i,k) >= maxtke(i,k) * tke\_to\_kd(i,k)) \textcolor{keywordflow}{then}
1368             tke\_here = 0.0
1369             tke(i) = tke(i) + tke\_ray
1370           \textcolor{keywordflow}{elseif} (kd\_lay(i,k) + (tke\_to\_layer + tke\_ray) * tke\_to\_kd(i,k) > &
1371                   maxtke(i,k) * tke\_to\_kd(i,k)) \textcolor{keywordflow}{then}
1372             tke\_here = ((tke\_to\_layer + tke\_ray) + kd\_lay(i,k) / tke\_to\_kd(i,k)) - maxtke(i,k)
1373             tke(i) = (tke(i) - tke\_here) + tke\_ray
1374           \textcolor{keywordflow}{else}
1375             tke\_here = tke\_to\_layer + tke\_ray
1376             tke(i) = tke(i) - tke\_to\_layer
1377 \textcolor{keywordflow}{          endif}
1378           \textcolor{keywordflow}{if} (tke(i) < 0.0) tke(i) = 0.0 \textcolor{comment}{! This should be unnecessary?}
1379 
1380           \textcolor{keywordflow}{if} (tke\_here > 0.0) \textcolor{keywordflow}{then}
1381             delta\_kd = tke\_here * tke\_to\_kd(i,k)
1382             \textcolor{keywordflow}{if} (cs%Kd\_max >= 0.0) delta\_kd = min(delta\_kd, cs%Kd\_max)
1383             kd\_lay(i,k) = kd\_lay(i,k) + delta\_kd
1384             \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_int)) \textcolor{keywordflow}{then}
1385               kd\_int(i,k)   = kd\_int(i,k)   + 0.5 * delta\_kd
1386               kd\_int(i,k+1) = kd\_int(i,k+1) + 0.5 * delta\_kd
1387 \textcolor{keywordflow}{            endif}
1388             \textcolor{keywordflow}{if} (do\_diag\_kd\_bbl) \textcolor{keywordflow}{then}
1389               kd\_bbl(i,j,k) = kd\_bbl(i,j,k) + 0.5 * delta\_kd
1390               kd\_bbl(i,j,k+1) = kd\_bbl(i,j,k+1) + 0.5 * delta\_kd
1391 \textcolor{keywordflow}{            endif}
1392 \textcolor{keywordflow}{          endif}
1393 \textcolor{keywordflow}{        endif}
1394 \textcolor{keywordflow}{      endif}
1395 
1396       \textcolor{comment}{! This may be risky - in the case that there are exactly zero}
1397       \textcolor{comment}{! velocities at 4 neighboring points, but nonzero velocities}
1398       \textcolor{comment}{! above the iterations would stop too soon. I don't see how this}
1399       \textcolor{comment}{! could happen in practice. RWH}
1400       \textcolor{keywordflow}{if} ((tke(i)<= 0.0) .and. (tke\_ray == 0.0)) \textcolor{keywordflow}{then}
1401         do\_i(i) = .false. ; i\_rem = i\_rem - 1
1402 \textcolor{keywordflow}{      endif}
1403 
1404 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
1405     \textcolor{keywordflow}{if} (i\_rem == 0) \textcolor{keywordflow}{exit}
1406 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! k-loop}
1407 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__diffusivity_a968690a1c9efb859ee67965f9032c7d7}\label{namespacemom__set__diffusivity_a968690a1c9efb859ee67965f9032c7d7}} 
\index{mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}!add\+\_\+lotw\+\_\+bbl\+\_\+diffusivity@{add\+\_\+lotw\+\_\+bbl\+\_\+diffusivity}}
\index{add\+\_\+lotw\+\_\+bbl\+\_\+diffusivity@{add\+\_\+lotw\+\_\+bbl\+\_\+diffusivity}!mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}}
\subsubsection{\texorpdfstring{add\+\_\+lotw\+\_\+bbl\+\_\+diffusivity()}{add\_lotw\_bbl\_diffusivity()}}
{\footnotesize\ttfamily subroutine mom\+\_\+set\+\_\+diffusivity\+::add\+\_\+lotw\+\_\+bbl\+\_\+diffusivity (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb, g \%ke), intent(in)}]{v,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{type(forcing), intent(in)}]{fluxes,  }\item[{type(vertvisc\+\_\+type), intent(in)}]{visc,  }\item[{integer, intent(in)}]{j,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke+1), intent(in)}]{N2\+\_\+int,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__set__diffusivity_1_1set__diffusivity__cs}{set\+\_\+diffusivity\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension(\+:,\+:,\+:), pointer}]{Kd\+\_\+\+B\+BL,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke), intent(inout), optional}]{Kd\+\_\+lay,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke+1), intent(inout), optional}]{Kd\+\_\+int }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Calculates a B\+BL diffusivity use a Prandtl number 1 diffusivity with a law of the wall turbulent viscosity, up to a B\+BL height where the energy used for mixing has consumed the mechanical T\+KE input. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & Grid structure\\
\hline
\mbox{\tt in}  & {\em gv} & Vertical grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em u} & u component of flow \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em v} & v component of flow \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em tv} & Structure containing pointers to any available thermodynamic fields.\\
\hline
\mbox{\tt in}  & {\em fluxes} & Surface fluxes structure\\
\hline
\mbox{\tt in}  & {\em visc} & Structure containing vertical viscosities, bottom boundary layer properies, and related fields.\\
\hline
\mbox{\tt in}  & {\em j} & j-\/index of row to work on\\
\hline
\mbox{\tt in}  & {\em n2\+\_\+int} & Square of Brunt-\/\+Vaisala at interfaces \mbox{[}T-\/2 $\sim$$>$ s-\/2\mbox{]}\\
\hline
 & {\em cs} & Diffusivity control structure\\
\hline
 & {\em kd\+\_\+bbl} & Interface B\+BL diffusivity \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em kd\+\_\+lay} & Layer net diffusivity \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em kd\+\_\+int} & Interface net diffusivity \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 1415 of file M\+O\+M\+\_\+set\+\_\+diffusivity.\+F90.


\begin{DoxyCode}
1415   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{  !< Grid structure}
1416   \textcolor{keywordtype}{type}(verticalGrid\_type),  \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{ !< Vertical grid structure}
1417   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{ !< A dimensional unit scaling type}
1418   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, &
1419                             \textcolor{keywordtype}{intent(in)}    :: u\textcolor{comment}{  !< u component of flow [L T-1 ~> m s-1]}
1420   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, &
1421                             \textcolor{keywordtype}{intent(in)}    :: v\textcolor{comment}{  !< v component of flow [L T-1 ~> m s-1]}
1422   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
1423                             \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{  !< Layer thickness [H ~> m or kg m-2]}
1424   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),    \textcolor{keywordtype}{intent(in)}    :: tv\textcolor{comment}{ !< Structure containing pointers to any available}
1425 \textcolor{comment}{                                                !! thermodynamic fields.}
1426   \textcolor{keywordtype}{type}(forcing),            \textcolor{keywordtype}{intent(in)}    :: fluxes\textcolor{comment}{ !< Surface fluxes structure}
1427   \textcolor{keywordtype}{type}(vertvisc\_type),      \textcolor{keywordtype}{intent(in)}    :: visc\textcolor{comment}{ !< Structure containing vertical viscosities, bottom}
1428 \textcolor{comment}{                                                  !! boundary layer properies, and related fields.}
1429   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{  !< j-index of row to work on}
1430   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)}, &
1431                             \textcolor{keywordtype}{intent(in)}    :: N2\_int\textcolor{comment}{ !< Square of Brunt-Vaisala at interfaces [T-2 ~> s-2]}
1432   \textcolor{keywordtype}{type}(set\_diffusivity\_CS), \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{ !< Diffusivity control structure}
1433   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:,:,:)},   \textcolor{keywordtype}{pointer}       :: Kd\_BBL\textcolor{comment}{ !< Interface BBL diffusivity [Z2 T-1 ~> m2 s-1]}
1434   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, &
1435                   \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: Kd\_lay\textcolor{comment}{ !< Layer net diffusivity [Z2 T-1 ~> m2 s-1]}
1436   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)}, &
1437                   \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: Kd\_int\textcolor{comment}{ !< Interface net diffusivity [Z2 T-1 ~> m2 s-1]}
1438 
1439   \textcolor{comment}{! Local variables}
1440   \textcolor{keywordtype}{real} :: TKE\_column       \textcolor{comment}{! net TKE input into the column [Z3 T-3 ~> m3 s-3]}
1441   \textcolor{keywordtype}{real} :: TKE\_remaining    \textcolor{comment}{! remaining TKE available for mixing in this layer and above [Z3 T-3 ~> m3 s-3]}
1442   \textcolor{keywordtype}{real} :: TKE\_consumed     \textcolor{comment}{! TKE used for mixing in this layer [Z3 T-3 ~> m3 s-3]}
1443   \textcolor{keywordtype}{real} :: TKE\_Kd\_wall      \textcolor{comment}{! TKE associated with unlimited law of the wall mixing [Z3 T-3 ~> m3 s-3]}
1444   \textcolor{keywordtype}{real} :: cdrag\_sqrt       \textcolor{comment}{! square root of the drag coefficient [nondim]}
1445   \textcolor{keywordtype}{real} :: ustar            \textcolor{comment}{! value of ustar at a thickness point [Z T-1 ~> m s-1].}
1446   \textcolor{keywordtype}{real} :: ustar2           \textcolor{comment}{! square of ustar, for convenience [Z2 T-2 ~> m2 s-2]}
1447   \textcolor{keywordtype}{real} :: absf             \textcolor{comment}{! average absolute value of Coriolis parameter around a thickness point [T-1 ~>
       s-1]}
1448   \textcolor{keywordtype}{real} :: dh, dhm1         \textcolor{comment}{! thickness of layers k and k-1, respecitvely [Z ~> m].}
1449   \textcolor{keywordtype}{real} :: z\_bot            \textcolor{comment}{! distance to interface k from bottom [Z ~> m].}
1450   \textcolor{keywordtype}{real} :: D\_minus\_z        \textcolor{comment}{! distance to interface k from surface [Z ~> m].}
1451   \textcolor{keywordtype}{real} :: total\_thickness  \textcolor{comment}{! total thickness of water column [Z ~> m].}
1452   \textcolor{keywordtype}{real} :: Idecay           \textcolor{comment}{! inverse of decay scale used for "Joule heating" loss of TKE with height [Z-1
       ~> m-1].}
1453   \textcolor{keywordtype}{real} :: Kd\_wall          \textcolor{comment}{! Law of the wall diffusivity [Z2 T-1 ~> m2 s-1].}
1454   \textcolor{keywordtype}{real} :: Kd\_lower         \textcolor{comment}{! diffusivity for lower interface [Z2 T-1 ~> m2 s-1]}
1455   \textcolor{keywordtype}{real} :: ustar\_D          \textcolor{comment}{! u* x D  [Z2 T-1 ~> m2 s-1].}
1456   \textcolor{keywordtype}{real} :: I\_Rho0           \textcolor{comment}{! 1 / rho0 [R-1  ~> m3 kg-1]}
1457   \textcolor{keywordtype}{real} :: N2\_min           \textcolor{comment}{! Minimum value of N2 to use in calculation of TKE\_Kd\_wall [T-2 ~> s-2]}
1458   \textcolor{keywordtype}{logical} :: Rayleigh\_drag \textcolor{comment}{! Set to true if there are Rayleigh drag velocities defined in visc, on}
1459                            \textcolor{comment}{! the assumption that this extracted energy also drives diapycnal mixing.}
1460   \textcolor{keywordtype}{integer} :: i, k, km1
1461   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{parameter} :: von\_karm = 0.41 \textcolor{comment}{! Von Karman constant
       (http://en.wikipedia.org/wiki/Von\_Karman\_constant)}
1462   \textcolor{keywordtype}{logical} :: do\_diag\_Kd\_BBL
1463 
1464   \textcolor{keywordflow}{if} (.not.(cs%bottomdraglaw .and. (cs%BBL\_effic>0.0))) \textcolor{keywordflow}{return}
1465   do\_diag\_kd\_bbl = \textcolor{keyword}{associated}(kd\_bbl)
1466 
1467   n2\_min = 0.
1468   \textcolor{keywordflow}{if} (cs%LOTW\_BBL\_use\_omega) n2\_min = cs%omega**2
1469 
1470   \textcolor{comment}{! Determine whether to add Rayleigh drag contribution to TKE}
1471   rayleigh\_drag = .false.
1472   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc%Ray\_u) .and. \textcolor{keyword}{associated}(visc%Ray\_v)) rayleigh\_drag = .true.
1473   i\_rho0 = 1.0 / (gv%Rho0)
1474   cdrag\_sqrt = sqrt(cs%cdrag)
1475 
1476   \textcolor{keywordflow}{do} i=g%isc,g%iec \textcolor{comment}{! Developed in single-column mode}
1477 
1478     \textcolor{comment}{! Column-wise parameters.}
1479     absf = 0.25 * ((abs(g%CoriolisBu(i-1,j-1)) + abs(g%CoriolisBu(i,j))) + &
1480                    (abs(g%CoriolisBu(i-1,j)) + abs(g%CoriolisBu(i,j-1)))) \textcolor{comment}{! Non-zero on equator!}
1481 
1482     \textcolor{comment}{! u* at the bottom [Z T-1 ~> m s-1].}
1483     ustar = visc%ustar\_BBL(i,j)
1484     ustar2 = ustar**2
1485     \textcolor{comment}{! In add\_drag\_diffusivity(), fluxes%ustar\_tidal is added in. This might be double counting}
1486     \textcolor{comment}{! since ustar\_BBL should already include all contributions to u*? -AJA}
1487     \textcolor{comment}{!### Examine the question of whether there is double counting of fluxes%ustar\_tidal.}
1488     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%ustar\_tidal)) ustar = ustar + fluxes%ustar\_tidal(i,j)
1489 
1490     \textcolor{comment}{! The maximum decay scale should be something of order 200 m. We use the smaller of u*/f and}
1491     \textcolor{comment}{! (IMax\_decay)^-1 as the decay scale. If ustar = 0, this is land so this value doesn't matter.}
1492     idecay = cs%IMax\_decay
1493     \textcolor{keywordflow}{if} ((ustar > 0.0) .and. (absf > cs%IMax\_decay * ustar)) idecay = absf / ustar
1494 
1495     \textcolor{comment}{! Energy input at the bottom [Z3 T-3 ~> m3 s-3].}
1496     \textcolor{comment}{! (Note that visc%TKE\_BBL is in [Z3 T-3 ~> m3 s-3], set in set\_BBL\_TKE().)}
1497     \textcolor{comment}{! I am still unsure about sqrt(cdrag) in this expressions - AJA}
1498     tke\_column = cdrag\_sqrt * visc%TKE\_BBL(i,j)
1499     \textcolor{comment}{! Add in tidal dissipation energy at the bottom [R Z3 T-3 ~> m3 s-3].}
1500     \textcolor{comment}{! Note that TKE\_tidal is in [R Z3 T-3 ~> W m-2].}
1501     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%TKE\_tidal)) &
1502       tke\_column = tke\_column + fluxes%TKE\_tidal(i,j) * i\_rho0
1503     tke\_column = cs%BBL\_effic * tke\_column \textcolor{comment}{! Only use a fraction of the mechanical dissipation for mixing.}
1504 
1505     tke\_remaining = tke\_column
1506     total\_thickness = ( sum(h(i,j,:)) + gv%H\_subroundoff )* gv%H\_to\_Z \textcolor{comment}{! Total column thickness [Z ~> m].}
1507     ustar\_d = ustar * total\_thickness
1508     z\_bot = 0.
1509     kd\_lower = 0. \textcolor{comment}{! Diffusivity on bottom boundary.}
1510 
1511     \textcolor{comment}{! Work upwards from the bottom, accumulating work used until it exceeds the available TKE input}
1512     \textcolor{comment}{! at the bottom.}
1513     \textcolor{keywordflow}{do} k=g%ke,2,-1
1514       dh = gv%H\_to\_Z * h(i,j,k) \textcolor{comment}{! Thickness of this level [Z ~> m].}
1515       km1 = max(k-1, 1)
1516       dhm1 = gv%H\_to\_Z * h(i,j,km1) \textcolor{comment}{! Thickness of level above [Z ~> m].}
1517 
1518       \textcolor{comment}{! Add in additional energy input from bottom-drag against slopes (sides)}
1519       \textcolor{keywordflow}{if} (rayleigh\_drag) tke\_remaining = tke\_remaining + &
1520             0.5*cs%BBL\_effic * us%L\_to\_Z**2 * g%IareaT(i,j) * &
1521             ((g%areaCu(i-1,j) * visc%Ray\_u(i-1,j,k) * u(i-1,j,k)**2 + &
1522               g%areaCu(i,j)   * visc%Ray\_u(i,j,k)   * u(i,j,k)**2) + &
1523              (g%areaCv(i,j-1) * visc%Ray\_v(i,j-1,k) * v(i,j-1,k)**2 + &
1524               g%areaCv(i,j)   * visc%Ray\_v(i,j,k)   * v(i,j,k)**2))
1525 
1526       \textcolor{comment}{! Exponentially decay TKE across the thickness of the layer.}
1527       \textcolor{comment}{! This is energy loss in addition to work done as mixing, apparently to Joule heating.}
1528       tke\_remaining = exp(-idecay*dh) * tke\_remaining
1529 
1530       z\_bot = z\_bot + h(i,j,k)*gv%H\_to\_Z \textcolor{comment}{! Distance between upper interface of layer and the bottom [Z ~>
       m].}
1531       d\_minus\_z = max(total\_thickness - z\_bot, 0.) \textcolor{comment}{! Thickness above layer [Z ~> m].}
1532 
1533       \textcolor{comment}{! Diffusivity using law of the wall, limited by rotation, at height z [Z2 T-1 ~> m2 s-1].}
1534       \textcolor{comment}{! This calculation is at the upper interface of the layer}
1535       \textcolor{keywordflow}{if} ( ustar\_d + absf * ( z\_bot * d\_minus\_z ) == 0.) \textcolor{keywordflow}{then}
1536         kd\_wall = 0.
1537       \textcolor{keywordflow}{else}
1538         kd\_wall = ((von\_karm * ustar2) * (z\_bot * d\_minus\_z)) &
1539                   / (ustar\_d + absf * (z\_bot * d\_minus\_z))
1540 \textcolor{keywordflow}{      endif}
1541 
1542       \textcolor{comment}{! TKE associated with Kd\_wall [Z3 T-3 ~> m3 s-3].}
1543       \textcolor{comment}{! This calculation if for the volume spanning the interface.}
1544       tke\_kd\_wall = kd\_wall * 0.5 * (dh + dhm1) * max(n2\_int(i,k), n2\_min)
1545 
1546       \textcolor{comment}{! Now bound Kd such that the associated TKE is no greater than available TKE for mixing.}
1547       \textcolor{keywordflow}{if} (tke\_kd\_wall > 0.) \textcolor{keywordflow}{then}
1548         tke\_consumed = min(tke\_kd\_wall, tke\_remaining)
1549         kd\_wall = (tke\_consumed / tke\_kd\_wall) * kd\_wall \textcolor{comment}{! Scale Kd so that only TKE\_consumed is used.}
1550       \textcolor{keywordflow}{else}
1551         \textcolor{comment}{! Either N2=0 or dh = 0.}
1552         \textcolor{keywordflow}{if} (tke\_remaining > 0.) \textcolor{keywordflow}{then}
1553           kd\_wall = cs%Kd\_max
1554         \textcolor{keywordflow}{else}
1555           kd\_wall = 0.
1556 \textcolor{keywordflow}{        endif}
1557         tke\_consumed = 0.
1558 \textcolor{keywordflow}{      endif}
1559 
1560       \textcolor{comment}{! Now use up the appropriate about of TKE associated with the diffusivity chosen}
1561       tke\_remaining = tke\_remaining - tke\_consumed \textcolor{comment}{! Note this will be non-negative}
1562 
1563       \textcolor{comment}{! Add this BBL diffusivity to the model net diffusivity.}
1564       \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_int)) kd\_int(i,k) = kd\_int(i,k) + kd\_wall
1565       \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_lay)) kd\_lay(i,k) = kd\_lay(i,k) + 0.5 * (kd\_wall + kd\_lower)
1566       kd\_lower = kd\_wall \textcolor{comment}{! Store for next layer up.}
1567       \textcolor{keywordflow}{if} (do\_diag\_kd\_bbl) kd\_bbl(i,j,k) = kd\_wall
1568 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! k}
1569 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! i}
1570 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__diffusivity_a21162cb5173d52ce92dc65beee9d0ef1}\label{namespacemom__set__diffusivity_a21162cb5173d52ce92dc65beee9d0ef1}} 
\index{mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}!add\+\_\+mlrad\+\_\+diffusivity@{add\+\_\+mlrad\+\_\+diffusivity}}
\index{add\+\_\+mlrad\+\_\+diffusivity@{add\+\_\+mlrad\+\_\+diffusivity}!mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}}
\subsubsection{\texorpdfstring{add\+\_\+mlrad\+\_\+diffusivity()}{add\_mlrad\_diffusivity()}}
{\footnotesize\ttfamily subroutine mom\+\_\+set\+\_\+diffusivity\+::add\+\_\+mlrad\+\_\+diffusivity (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{type(forcing), intent(in)}]{fluxes,  }\item[{integer, intent(in)}]{j,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__set__diffusivity_1_1set__diffusivity__cs}{set\+\_\+diffusivity\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{T\+K\+E\+\_\+to\+\_\+\+Kd,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(inout), optional}]{Kd\+\_\+lay,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)+1), intent(inout), optional}]{Kd\+\_\+int }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This routine adds effects of mixed layer radiation to the layer diffusivities. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em fluxes} & Surface fluxes structure\\
\hline
\mbox{\tt in}  & {\em j} & The j-\/index to work on\\
\hline
 & {\em cs} & Diffusivity control structure\\
\hline
\mbox{\tt in}  & {\em tke\+\_\+to\+\_\+kd} & The conversion rate between the T\+KE T\+KE dissipated within a layer and the diapycnal diffusivity witin that layer, usually ($\sim$\+Rho\+\_\+0 / (G\+\_\+\+Earth $\ast$ d\+Rho\+\_\+lay)) \mbox{[}Z2 T-\/1 / Z3 T-\/3 = T2 Z-\/1 $\sim$$>$ s2 m-\/1\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em kd\+\_\+lay} & The diapycnal diffusivity in layers \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em kd\+\_\+int} & The diapycnal diffusivity at interfaces \\
\hline
\end{DoxyParams}


Definition at line 1575 of file M\+O\+M\+\_\+set\+\_\+diffusivity.\+F90.


\begin{DoxyCode}
1575   \textcolor{keywordtype}{type}(ocean\_grid\_type),            \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{      !< The ocean's grid structure}
1576   \textcolor{keywordtype}{type}(verticalGrid\_type),          \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{     !< The ocean's vertical grid structure}
1577   \textcolor{keywordtype}{type}(unit\_scale\_type),            \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{     !< A dimensional unit scaling type}
1578   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
1579                                     \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{      !< Layer thicknesses [H ~> m or kg m-2]}
1580   \textcolor{keywordtype}{type}(forcing),                    \textcolor{keywordtype}{intent(in)}    :: fluxes\textcolor{comment}{ !< Surface fluxes structure}
1581   \textcolor{keywordtype}{integer},                          \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{      !< The j-index to work on}
1582   \textcolor{keywordtype}{type}(set\_diffusivity\_CS),         \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{     !< Diffusivity control structure}
1583   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}    :: TKE\_to\_Kd\textcolor{comment}{ !< The conversion rate between the TKE}
1584 \textcolor{comment}{                                                            !! TKE dissipated within  a layer and the}
1585 \textcolor{comment}{                                                            !! diapycnal diffusivity witin that layer,}
1586 \textcolor{comment}{                                                            !! usually (~Rho\_0 / (G\_Earth * dRho\_lay))}
1587 \textcolor{comment}{                                                            !! [Z2 T-1 / Z3 T-3 = T2 Z-1 ~> s2 m-1]}
1588   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, &
1589                           \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: Kd\_lay\textcolor{comment}{ !< The diapycnal diffusivity in layers [Z2 T-1
       ~> m2 s-1].}
1590   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)}, &
1591                           \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: Kd\_int\textcolor{comment}{ !< The diapycnal diffusivity at interfaces}
1592 \textcolor{comment}{                                                            !! [Z2 T-1 ~> m2 s-1].}
1593 
1594 \textcolor{comment}{! This routine adds effects of mixed layer radiation to the layer diffusivities.}
1595 
1596   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: h\_ml  \textcolor{comment}{! Mixed layer thickness [Z ~> m].}
1597   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: TKE\_ml\_flux \textcolor{comment}{! Mixed layer TKE flux [Z3 T-3 ~> m3 s-3]}
1598   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: I\_decay \textcolor{comment}{! A decay rate [Z-1 ~> m-1].}
1599   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: Kd\_mlr\_ml \textcolor{comment}{! Diffusivities associated with mixed layer radiation [Z2 T-1 ~> m2
       s-1].}
1600 
1601   \textcolor{keywordtype}{real} :: f\_sq              \textcolor{comment}{! The square of the local Coriolis parameter or a related variable [T-2 ~>
       s-2].}
1602   \textcolor{keywordtype}{real} :: h\_ml\_sq           \textcolor{comment}{! The square of the mixed layer thickness [Z2 ~> m2].}
1603   \textcolor{keywordtype}{real} :: ustar\_sq          \textcolor{comment}{! ustar squared [Z2 T-2 ~> m2 s-2]}
1604   \textcolor{keywordtype}{real} :: Kd\_mlr            \textcolor{comment}{! A diffusivity associated with mixed layer turbulence radiation [Z2 T-1 ~> m2
       s-1].}
1605   \textcolor{keywordtype}{real} :: C1\_6              \textcolor{comment}{! 1/6}
1606   \textcolor{keywordtype}{real} :: Omega2            \textcolor{comment}{! rotation rate squared [T-2 ~> s-2].}
1607   \textcolor{keywordtype}{real} :: z1                \textcolor{comment}{! layer thickness times I\_decay [nondim]}
1608   \textcolor{keywordtype}{real} :: dzL               \textcolor{comment}{! thickness converted to heights [Z ~> m].}
1609   \textcolor{keywordtype}{real} :: I\_decay\_len2\_TKE  \textcolor{comment}{! squared inverse decay lengthscale for}
1610                             \textcolor{comment}{! TKE, as used in the mixed layer code [Z-2 ~> m-2].}
1611   \textcolor{keywordtype}{real} :: h\_neglect         \textcolor{comment}{! negligibly small thickness [Z ~> m].}
1612 
1613   \textcolor{keywordtype}{logical} :: do\_any, do\_i(SZI\_(G))
1614   \textcolor{keywordtype}{integer} :: i, k, is, ie, nz, kml
1615   is = g%isc ; ie = g%iec ; nz = g%ke
1616 
1617   omega2    = cs%omega**2
1618   c1\_6      = 1.0 / 6.0
1619   kml       = gv%nkml
1620   h\_neglect = gv%H\_subroundoff*gv%H\_to\_Z
1621 
1622   \textcolor{keywordflow}{if} (.not.cs%ML\_radiation) \textcolor{keywordflow}{return}
1623 
1624   \textcolor{keywordflow}{do} i=is,ie ; h\_ml(i) = 0.0 ; do\_i(i) = (g%mask2dT(i,j) > 0.5) ;\textcolor{keywordflow}{ enddo}
1625   \textcolor{keywordflow}{do} k=1,kml ; \textcolor{keywordflow}{do} i=is,ie ; h\_ml(i) = h\_ml(i) + gv%H\_to\_Z*h(i,j,k) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
1626 
1627   \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}
1628     \textcolor{keywordflow}{if} (cs%ML\_omega\_frac >= 1.0) \textcolor{keywordflow}{then}
1629       f\_sq = 4.0 * omega2
1630     \textcolor{keywordflow}{else}
1631       f\_sq = 0.25 * ((g%CoriolisBu(i,j)**2 + g%CoriolisBu(i-1,j-1)**2) + &
1632                      (g%CoriolisBu(i,j-1)**2 + g%CoriolisBu(i-1,j)**2))
1633       \textcolor{keywordflow}{if} (cs%ML\_omega\_frac > 0.0) &
1634         f\_sq = cs%ML\_omega\_frac * 4.0 * omega2 + (1.0 - cs%ML\_omega\_frac) * f\_sq
1635 \textcolor{keywordflow}{    endif}
1636 
1637     ustar\_sq = max(fluxes%ustar(i,j), cs%ustar\_min)**2
1638 
1639     tke\_ml\_flux(i) = (cs%mstar * cs%ML\_rad\_coeff) * (ustar\_sq * (fluxes%ustar(i,j)))
1640     i\_decay\_len2\_tke = cs%TKE\_decay**2 * (f\_sq / ustar\_sq)
1641 
1642     \textcolor{keywordflow}{if} (cs%ML\_rad\_TKE\_decay) &
1643       tke\_ml\_flux(i) = tke\_ml\_flux(i) * exp(-h\_ml(i) * sqrt(i\_decay\_len2\_tke))
1644 
1645     \textcolor{comment}{! Calculate the inverse decay scale}
1646     h\_ml\_sq = (cs%ML\_rad\_efold\_coeff * (h\_ml(i)+h\_neglect))**2
1647     i\_decay(i) = sqrt((i\_decay\_len2\_tke * h\_ml\_sq + 1.0) / h\_ml\_sq)
1648 
1649     \textcolor{comment}{! Average the dissipation layer kml+1, using}
1650     \textcolor{comment}{! a more accurate Taylor series approximations for very thin layers.}
1651     z1 = (gv%H\_to\_Z*h(i,j,kml+1)) * i\_decay(i)
1652     \textcolor{keywordflow}{if} (z1 > 1e-5) \textcolor{keywordflow}{then}
1653       kd\_mlr = tke\_ml\_flux(i) * tke\_to\_kd(i,kml+1) * (1.0 - exp(-z1))
1654     \textcolor{keywordflow}{else}
1655       kd\_mlr = tke\_ml\_flux(i) * tke\_to\_kd(i,kml+1) * (z1 * (1.0 - z1 * (0.5 - c1\_6 * z1)))
1656 \textcolor{keywordflow}{    endif}
1657     kd\_mlr\_ml(i) = min(kd\_mlr, cs%ML\_rad\_kd\_max)
1658     tke\_ml\_flux(i) = tke\_ml\_flux(i) * exp(-z1)
1659 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}
1660 
1661   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_lay)) \textcolor{keywordflow}{then}
1662     \textcolor{keywordflow}{do} k=1,kml+1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}
1663       kd\_lay(i,k) = kd\_lay(i,k) + kd\_mlr\_ml(i)
1664 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
1665 \textcolor{keywordflow}{  endif}
1666   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_int)) \textcolor{keywordflow}{then}
1667     \textcolor{keywordflow}{do} k=2,kml+1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}
1668       kd\_int(i,k) = kd\_int(i,k) + kd\_mlr\_ml(i)
1669 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
1670     \textcolor{keywordflow}{if} (kml<=nz-1) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}
1671       kd\_int(i,kml+2) = kd\_int(i,kml+2) + 0.5 * kd\_mlr\_ml(i)
1672 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
1673 \textcolor{keywordflow}{  endif}
1674 
1675   \textcolor{keywordflow}{do} k=kml+2,nz-1
1676     do\_any = .false.
1677     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}
1678       dzl = gv%H\_to\_Z*h(i,j,k) ;  z1 = dzl*i\_decay(i)
1679       \textcolor{keywordflow}{if} (cs%ML\_Rad\_bug) \textcolor{keywordflow}{then}
1680         \textcolor{comment}{! These expresssions are dimensionally inconsistent. -RWH}
1681         \textcolor{comment}{! This is supposed to be the integrated energy deposited in the layer,}
1682         \textcolor{comment}{! not the average over the layer as in these expressions.}
1683         \textcolor{keywordflow}{if} (z1 > 1e-5) \textcolor{keywordflow}{then}
1684           kd\_mlr = (tke\_ml\_flux(i) * tke\_to\_kd(i,k)) * & \textcolor{comment}{! Units of Z2 T-1}
1685                    us%m\_to\_Z * ((1.0 - exp(-z1)) / dzl)  \textcolor{comment}{! Units of m-1}
1686         \textcolor{keywordflow}{else}
1687           kd\_mlr = (tke\_ml\_flux(i) * tke\_to\_kd(i,k)) * &  \textcolor{comment}{! Units of Z2 T-1}
1688                    us%m\_to\_Z * (i\_decay(i) * (1.0 - z1 * (0.5 - c1\_6*z1))) \textcolor{comment}{! Units of m-1}
1689 \textcolor{keywordflow}{        endif}
1690       \textcolor{keywordflow}{else}
1691         \textcolor{keywordflow}{if} (z1 > 1e-5) \textcolor{keywordflow}{then}
1692           kd\_mlr = (tke\_ml\_flux(i) * tke\_to\_kd(i,k)) * (1.0 - exp(-z1))
1693         \textcolor{keywordflow}{else}
1694           kd\_mlr = (tke\_ml\_flux(i) * tke\_to\_kd(i,k)) * (z1 * (1.0 - z1 * (0.5 - c1\_6*z1)))
1695 \textcolor{keywordflow}{        endif}
1696 \textcolor{keywordflow}{      endif}
1697       kd\_mlr = min(kd\_mlr, cs%ML\_rad\_kd\_max)
1698       \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_lay)) \textcolor{keywordflow}{then}
1699         kd\_lay(i,k) = kd\_lay(i,k) + kd\_mlr
1700 \textcolor{keywordflow}{      endif}
1701       \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_int)) \textcolor{keywordflow}{then}
1702         kd\_int(i,k)   = kd\_int(i,k)   + 0.5 * kd\_mlr
1703         kd\_int(i,k+1) = kd\_int(i,k+1) + 0.5 * kd\_mlr
1704 \textcolor{keywordflow}{      endif}
1705 
1706       tke\_ml\_flux(i) = tke\_ml\_flux(i) * exp(-z1)
1707       \textcolor{keywordflow}{if} (tke\_ml\_flux(i) * i\_decay(i) < 0.1 * cs%Kd\_min * omega2) \textcolor{keywordflow}{then}
1708         do\_i(i) = .false.
1709       \textcolor{keywordflow}{else} ; do\_any = .true. ;\textcolor{keywordflow}{ endif}
1710 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
1711     \textcolor{keywordflow}{if} (.not.do\_any) \textcolor{keywordflow}{exit}
1712 \textcolor{keywordflow}{  enddo}
1713 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__diffusivity_a99d0eb7701f8e04d856b75117fe7b83c}\label{namespacemom__set__diffusivity_a99d0eb7701f8e04d856b75117fe7b83c}} 
\index{mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}!double\+\_\+diffusion@{double\+\_\+diffusion}}
\index{double\+\_\+diffusion@{double\+\_\+diffusion}!mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}}
\subsubsection{\texorpdfstring{double\+\_\+diffusion()}{double\_diffusion()}}
{\footnotesize\ttfamily subroutine mom\+\_\+set\+\_\+diffusivity\+::double\+\_\+diffusion (\begin{DoxyParamCaption}\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{T\+\_\+f,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{S\+\_\+f,  }\item[{integer, intent(in)}]{j,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__set__diffusivity_1_1set__diffusivity__cs}{set\+\_\+diffusivity\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke+1), intent(out)}]{Kd\+\_\+\+T\+\_\+dd,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke+1), intent(out)}]{Kd\+\_\+\+S\+\_\+dd }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine sets the additional diffusivities of temperature and salinity due to double diffusion, using the same functional form as is used in M\+O\+M4.\+1, and taken from an N\+C\+AR technical note (R\+EF?) that updates what was in Large et al. (1994). All the coefficients here should probably be made run-\/time variables rather than hard-\/coded constants. 

\begin{DoxyRefDesc}{Todo}
\item[\mbox{\hyperlink{todo__todo000006}{Todo}}]Find reference for N\+C\+AR tech note above. \end{DoxyRefDesc}



\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em tv} & Structure containing pointers to any available thermodynamic fields; absent fields have N\+U\+LL ptrs.\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em t\+\_\+f} & layer temperatures with the values in massless layers\\
\hline
\mbox{\tt in}  & {\em s\+\_\+f} & Layer salinities with values in massless\\
\hline
\mbox{\tt in}  & {\em j} & Meridional index upon which to work.\\
\hline
 & {\em cs} & Module control structure.\\
\hline
\mbox{\tt out}  & {\em kd\+\_\+t\+\_\+dd} & Interface double diffusion diapycnal\\
\hline
\mbox{\tt out}  & {\em kd\+\_\+s\+\_\+dd} & Interface double diffusion diapycnal \\
\hline
\end{DoxyParams}


Definition at line 1089 of file M\+O\+M\+\_\+set\+\_\+diffusivity.\+F90.


\begin{DoxyCode}
1089   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(in)}  :: G\textcolor{comment}{   !< The ocean's grid structure.}
1090   \textcolor{keywordtype}{type}(verticalGrid\_type),  \textcolor{keywordtype}{intent(in)}  :: GV\textcolor{comment}{  !< The ocean's vertical grid structure.}
1091   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}  :: US\textcolor{comment}{  !< A dimensional unit scaling type}
1092   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),    \textcolor{keywordtype}{intent(in)}  :: tv\textcolor{comment}{  !< Structure containing pointers to any available}
1093 \textcolor{comment}{                                               !! thermodynamic fields; absent fields have NULL ptrs.}
1094   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
1095                             \textcolor{keywordtype}{intent(in)}  :: h\textcolor{comment}{   !< Layer thicknesses [H ~> m or kg m-2].}
1096   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
1097                             \textcolor{keywordtype}{intent(in)}  :: T\_f\textcolor{comment}{ !< layer temperatures with the values in massless layers}
1098 \textcolor{comment}{                                               !! filled vertically by diffusion [degC].}
1099   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
1100                             \textcolor{keywordtype}{intent(in)}  :: S\_f\textcolor{comment}{ !< Layer salinities with values in massless}
1101 \textcolor{comment}{                                               !! layers filled vertically by diffusion [ppt].}
1102   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}  :: j\textcolor{comment}{   !< Meridional index upon which to work.}
1103   \textcolor{keywordtype}{type}(set\_diffusivity\_CS), \textcolor{keywordtype}{pointer}     :: CS\textcolor{comment}{  !< Module control structure.}
1104   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)},       &
1105                             \textcolor{keywordtype}{intent(out)} :: Kd\_T\_dd\textcolor{comment}{ !< Interface double diffusion diapycnal}
1106 \textcolor{comment}{                                               !! diffusivity for temp [Z2 T-1 ~> m2 s-1].}
1107   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)},       &
1108                             \textcolor{keywordtype}{intent(out)} :: Kd\_S\_dd\textcolor{comment}{ !< Interface double diffusion diapycnal}
1109 \textcolor{comment}{                                               !! diffusivity for saln [Z2 T-1 ~> m2 s-1].}
1110 
1111   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: &
1112     dRho\_dT,  &    \textcolor{comment}{! partial derivatives of density wrt temp [R degC-1 ~> kg m-3 degC-1]}
1113     dRho\_dS,  &    \textcolor{comment}{! partial derivatives of density wrt saln [R ppt-1 ~> kg m-3 ppt-1]}
1114     pres,     &    \textcolor{comment}{! pressure at each interface [R L2 T-2 ~> Pa]}
1115     Temp\_int, &    \textcolor{comment}{! temperature at interfaces [degC]}
1116     Salin\_int      \textcolor{comment}{! Salinity at interfaces [ppt]}
1117 
1118   \textcolor{keywordtype}{real} ::  alpha\_dT \textcolor{comment}{! density difference between layers due to temp diffs [R ~> kg m-3]}
1119   \textcolor{keywordtype}{real} ::  beta\_dS  \textcolor{comment}{! density difference between layers due to saln diffs [R ~> kg m-3]}
1120 
1121   \textcolor{keywordtype}{real} :: Rrho    \textcolor{comment}{! vertical density ratio [nondim]}
1122   \textcolor{keywordtype}{real} :: diff\_dd \textcolor{comment}{! factor for double-diffusion [nondim]}
1123   \textcolor{keywordtype}{real} :: Kd\_dd   \textcolor{comment}{! The dominant double diffusive diffusivity [Z2 T-1 ~> m2 s-1]}
1124   \textcolor{keywordtype}{real} :: prandtl \textcolor{comment}{! flux ratio for diffusive convection regime}
1125 
1126   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{parameter} :: Rrho0  = 1.9 \textcolor{comment}{! limit for double-diffusive density ratio [nondim]}
1127 
1128   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: EOSdom \textcolor{comment}{! The i-computational domain for the equation of state}
1129   \textcolor{keywordtype}{integer} :: i, k, is, ie, nz
1130   is = g%isc ; ie = g%iec ; nz = g%ke
1131 
1132   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv%eqn\_of\_state)) \textcolor{keywordflow}{then}
1133     \textcolor{keywordflow}{do} i=is,ie
1134       pres(i) = 0.0 ; kd\_t\_dd(i,1) = 0.0 ; kd\_s\_dd(i,1) = 0.0
1135       kd\_t\_dd(i,nz+1) = 0.0 ; kd\_s\_dd(i,nz+1) = 0.0
1136 \textcolor{keywordflow}{    enddo}
1137     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv%p\_surf)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie ; pres(i) = tv%p\_surf(i,j) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
1138     eosdom(:) = eos\_domain(g%HI)
1139     \textcolor{keywordflow}{do} k=2,nz
1140       \textcolor{keywordflow}{do} i=is,ie
1141         pres(i) = pres(i) + (gv%g\_Earth*gv%H\_to\_RZ)*h(i,j,k-1)
1142         temp\_int(i) = 0.5 * (t\_f(i,j,k-1) + t\_f(i,j,k))
1143         salin\_int(i) = 0.5 * (s\_f(i,j,k-1) + s\_f(i,j,k))
1144 \textcolor{keywordflow}{      enddo}
1145       \textcolor{keyword}{call }calculate\_density\_derivs(temp\_int, salin\_int, pres, drho\_dt, drho\_ds, &
1146                                     tv%eqn\_of\_state, eosdom)
1147 
1148       \textcolor{keywordflow}{do} i=is,ie
1149         alpha\_dt = -1.0*drho\_dt(i) * (t\_f(i,j,k-1) - t\_f(i,j,k))
1150         beta\_ds  = drho\_ds(i) * (s\_f(i,j,k-1) - s\_f(i,j,k))
1151 
1152         \textcolor{keywordflow}{if} ((alpha\_dt > beta\_ds) .and. (beta\_ds > 0.0)) \textcolor{keywordflow}{then}  \textcolor{comment}{! salt finger case}
1153           rrho = min(alpha\_dt / beta\_ds, rrho0)
1154           diff\_dd = 1.0 - ((rrho-1.0)/(rrho0-1.0))
1155           kd\_dd = cs%Max\_salt\_diff\_salt\_fingers * diff\_dd*diff\_dd*diff\_dd
1156           kd\_t\_dd(i,k) = 0.7 * kd\_dd
1157           kd\_s\_dd(i,k) = kd\_dd
1158         \textcolor{keywordflow}{elseif} ((alpha\_dt < 0.) .and. (beta\_ds < 0.) .and. (alpha\_dt > beta\_ds)) \textcolor{keywordflow}{then} \textcolor{comment}{! diffusive
       convection}
1159           rrho = alpha\_dt / beta\_ds
1160           kd\_dd = cs%Kv\_molecular * 0.909 * exp(4.6 * exp(-0.54 * (1/rrho - 1)))
1161           prandtl = 0.15*rrho
1162           \textcolor{keywordflow}{if} (rrho > 0.5) prandtl = (1.85-0.85/rrho)*rrho
1163           kd\_t\_dd(i,k) = kd\_dd
1164           kd\_s\_dd(i,k) = prandtl * kd\_dd
1165         \textcolor{keywordflow}{else}
1166           kd\_t\_dd(i,k) = 0.0 ; kd\_s\_dd(i,k) = 0.0
1167 \textcolor{keywordflow}{        endif}
1168 \textcolor{keywordflow}{      enddo}
1169 \textcolor{keywordflow}{    enddo}
1170 \textcolor{keywordflow}{  endif}
1171 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__diffusivity_afef80c2221be24f63f1ca96c2abe6fa9}\label{namespacemom__set__diffusivity_afef80c2221be24f63f1ca96c2abe6fa9}} 
\index{mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}!find\+\_\+n2@{find\+\_\+n2}}
\index{find\+\_\+n2@{find\+\_\+n2}!mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}}
\subsubsection{\texorpdfstring{find\+\_\+n2()}{find\_n2()}}
{\footnotesize\ttfamily subroutine mom\+\_\+set\+\_\+diffusivity\+::find\+\_\+n2 (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{T\+\_\+f,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{S\+\_\+f,  }\item[{type(forcing), intent(in)}]{fluxes,  }\item[{integer, intent(in)}]{j,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__set__diffusivity_1_1set__diffusivity__cs}{set\+\_\+diffusivity\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke+1), intent(out)}]{d\+Rho\+\_\+int,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke), intent(out)}]{N2\+\_\+lay,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke+1), intent(out)}]{N2\+\_\+int,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out)}]{N2\+\_\+bot }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Calculate Brunt-\/\+Vaisala frequency, N$^\wedge$2. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em tv} & Structure containing pointers to any available thermodynamic fields.\\
\hline
\mbox{\tt in}  & {\em t\+\_\+f} & layer temperature with the values in massless layers\\
\hline
\mbox{\tt in}  & {\em s\+\_\+f} & Layer salinities with values in massless\\
\hline
\mbox{\tt in}  & {\em fluxes} & A structure of thermodynamic surface fluxes\\
\hline
\mbox{\tt in}  & {\em j} & j-\/index of row to work on\\
\hline
 & {\em cs} & Diffusivity control structure\\
\hline
\mbox{\tt out}  & {\em drho\+\_\+int} & Change in locally referenced potential density\\
\hline
\mbox{\tt out}  & {\em n2\+\_\+int} & The squared buoyancy frequency at the interfaces \mbox{[}T-\/2 $\sim$$>$ s-\/2\mbox{]}.\\
\hline
\mbox{\tt out}  & {\em n2\+\_\+lay} & The squared buoyancy frequency of the layers \mbox{[}T-\/2 $\sim$$>$ s-\/2\mbox{]}.\\
\hline
\mbox{\tt out}  & {\em n2\+\_\+bot} & The near-\/bottom squared buoyancy frequency \mbox{[}T-\/2 $\sim$$>$ s-\/2\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 918 of file M\+O\+M\+\_\+set\+\_\+diffusivity.\+F90.


\begin{DoxyCode}
918   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(in)}  :: G\textcolor{comment}{    !< The ocean's grid structure}
919   \textcolor{keywordtype}{type}(verticalGrid\_type),  \textcolor{keywordtype}{intent(in)}  :: GV\textcolor{comment}{   !< The ocean's vertical grid structure}
920   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}  :: US\textcolor{comment}{   !< A dimensional unit scaling type}
921   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
922                             \textcolor{keywordtype}{intent(in)}  :: h\textcolor{comment}{    !< Layer thicknesses [H ~> m or kg m-2]}
923   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),    \textcolor{keywordtype}{intent(in)}  :: tv\textcolor{comment}{   !< Structure containing pointers to any available}
924 \textcolor{comment}{                                                !! thermodynamic fields.}
925   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
926                             \textcolor{keywordtype}{intent(in)}  :: T\_f\textcolor{comment}{  !< layer temperature with the values in massless layers}
927 \textcolor{comment}{                                                !! filled vertically by diffusion [degC].}
928   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
929                             \textcolor{keywordtype}{intent(in)}  :: S\_f\textcolor{comment}{  !< Layer salinities with values in massless}
930 \textcolor{comment}{                                                !! layers filled vertically by diffusion [ppt].}
931   \textcolor{keywordtype}{type}(forcing),            \textcolor{keywordtype}{intent(in)}  :: fluxes\textcolor{comment}{ !< A structure of thermodynamic surface fluxes}
932   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}  :: j\textcolor{comment}{    !< j-index of row to work on}
933   \textcolor{keywordtype}{type}(set\_diffusivity\_CS), \textcolor{keywordtype}{pointer}     :: CS\textcolor{comment}{   !< Diffusivity control structure}
934   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)}, &
935                             \textcolor{keywordtype}{intent(out)} :: dRho\_int\textcolor{comment}{ !< Change in locally referenced potential density}
936 \textcolor{comment}{                                                !! across each interface [R ~> kg m-3].}
937   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)}, &
938                             \textcolor{keywordtype}{intent(out)} :: N2\_int\textcolor{comment}{ !< The squared buoyancy frequency at the interfaces [T-2
       ~> s-2].}
939   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, &
940                             \textcolor{keywordtype}{intent(out)} :: N2\_lay\textcolor{comment}{ !< The squared buoyancy frequency of the layers [T-2 ~>
       s-2].}
941   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)} :: N2\_bot\textcolor{comment}{ !< The near-bottom squared buoyancy frequency [T-2 ~>
       s-2].}
942   \textcolor{comment}{! Local variables}
943   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)} :: &
944     dRho\_int\_unfilt, & \textcolor{comment}{! unfiltered density differences across interfaces [R ~> kg m-3]}
945     dRho\_dT,         & \textcolor{comment}{! partial derivative of density wrt temp [R degC-1 ~> kg m-3 degC-1]}
946     dRho\_dS            \textcolor{comment}{! partial derivative of density wrt saln [R ppt-1 ~> kg m-3 ppt-1]}
947 
948   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: &
949     pres,      &  \textcolor{comment}{! pressure at each interface [R L2 T-2 ~> Pa]}
950     Temp\_int,  &  \textcolor{comment}{! temperature at each interface [degC]}
951     Salin\_int, &  \textcolor{comment}{! salinity at each interface [ppt]}
952     drho\_bot,  &  \textcolor{comment}{! A density difference [R ~> kg m-3]}
953     h\_amp,     &  \textcolor{comment}{! The topographic roughness amplitude [Z ~> m].}
954     hb,        &  \textcolor{comment}{! The thickness of the bottom layer [Z ~> m].}
955     z\_from\_bot    \textcolor{comment}{! The hieght above the bottom [Z ~> m].}
956 
957   \textcolor{keywordtype}{real} :: Rml\_base  \textcolor{comment}{! density of the deepest variable density layer}
958   \textcolor{keywordtype}{real} :: dz\_int    \textcolor{comment}{! thickness associated with an interface [Z ~> m].}
959   \textcolor{keywordtype}{real} :: G\_Rho0    \textcolor{comment}{! gravitation acceleration divided by Bouss reference density}
960                     \textcolor{comment}{! times some unit conversion factors [Z T-2 R-1 ~> m4 s-2 kg-1].}
961   \textcolor{keywordtype}{real} :: H\_neglect \textcolor{comment}{! negligibly small thickness, in the same units as h.}
962 
963   \textcolor{keywordtype}{logical} :: do\_i(SZI\_(G)), do\_any
964   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: EOSdom \textcolor{comment}{! The i-computational domain for the equation of state}
965   \textcolor{keywordtype}{integer} :: i, k, is, ie, nz
966 
967   is = g%isc ; ie = g%iec ; nz = g%ke
968   g\_rho0    = (us%L\_to\_Z**2 * gv%g\_Earth) / (gv%Rho0)
969   h\_neglect = gv%H\_subroundoff
970 
971   \textcolor{comment}{! Find the (limited) density jump across each interface.}
972   \textcolor{keywordflow}{do} i=is,ie
973     drho\_int(i,1) = 0.0 ; drho\_int(i,nz+1) = 0.0
974     drho\_int\_unfilt(i,1) = 0.0 ; drho\_int\_unfilt(i,nz+1) = 0.0
975 \textcolor{keywordflow}{  enddo}
976   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv%eqn\_of\_state)) \textcolor{keywordflow}{then}
977     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%p\_surf)) \textcolor{keywordflow}{then}
978       \textcolor{keywordflow}{do} i=is,ie ; pres(i) = fluxes%p\_surf(i,j) ;\textcolor{keywordflow}{ enddo}
979     \textcolor{keywordflow}{else}
980       \textcolor{keywordflow}{do} i=is,ie ; pres(i) = 0.0 ;\textcolor{keywordflow}{ enddo}
981 \textcolor{keywordflow}{    endif}
982     eosdom(:) = eos\_domain(g%HI)
983     \textcolor{keywordflow}{do} k=2,nz
984       \textcolor{keywordflow}{do} i=is,ie
985         pres(i) = pres(i) + (gv%g\_Earth*gv%H\_to\_RZ)*h(i,j,k-1)
986         temp\_int(i) = 0.5 * (t\_f(i,j,k) + t\_f(i,j,k-1))
987         salin\_int(i) = 0.5 * (s\_f(i,j,k) + s\_f(i,j,k-1))
988 \textcolor{keywordflow}{      enddo}
989       \textcolor{keyword}{call }calculate\_density\_derivs(temp\_int, salin\_int, pres, drho\_dt(:,k), drho\_ds(:,k), &
990                                     tv%eqn\_of\_state, eosdom)
991       \textcolor{keywordflow}{do} i=is,ie
992         drho\_int(i,k) = max(drho\_dt(i,k)*(t\_f(i,j,k) - t\_f(i,j,k-1)) + &
993                             drho\_ds(i,k)*(s\_f(i,j,k) - s\_f(i,j,k-1)), 0.0)
994         drho\_int\_unfilt(i,k) = max(drho\_dt(i,k)*(tv%T(i,j,k) - tv%T(i,j,k-1)) + &
995                             drho\_ds(i,k)*(tv%S(i,j,k) - tv%S(i,j,k-1)), 0.0)
996 \textcolor{keywordflow}{      enddo}
997 \textcolor{keywordflow}{    enddo}
998   \textcolor{keywordflow}{else}
999     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie
1000       drho\_int(i,k) = gv%Rlay(k) - gv%Rlay(k-1)
1001 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
1002 \textcolor{keywordflow}{  endif}
1003 
1004   \textcolor{comment}{! Set the buoyancy frequencies.}
1005   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie
1006     n2\_lay(i,k) = g\_rho0 * 0.5*(drho\_int(i,k) + drho\_int(i,k+1)) / &
1007                   (gv%H\_to\_Z*(h(i,j,k) + h\_neglect))
1008 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
1009   \textcolor{keywordflow}{do} i=is,ie ; n2\_int(i,1) = 0.0 ; n2\_int(i,nz+1) = 0.0 ;\textcolor{keywordflow}{ enddo}
1010   \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie
1011     n2\_int(i,k) = g\_rho0 * drho\_int(i,k) / &
1012                   (0.5*gv%H\_to\_Z*(h(i,j,k-1) + h(i,j,k) + h\_neglect))
1013 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
1014 
1015   \textcolor{comment}{! Find the bottom boundary layer stratification, and use this in the deepest layers.}
1016   \textcolor{keywordflow}{do} i=is,ie
1017     hb(i) = 0.0 ; drho\_bot(i) = 0.0 ; h\_amp(i) = 0.0
1018     z\_from\_bot(i) = 0.5*gv%H\_to\_Z*h(i,j,nz)
1019     do\_i(i) = (g%mask2dT(i,j) > 0.5)
1020 \textcolor{keywordflow}{  enddo}
1021   \textcolor{keywordflow}{if} (cs%use\_tidal\_mixing) \textcolor{keyword}{call }tidal\_mixing\_h\_amp(h\_amp, g, j, cs%tidal\_mixing\_CSp)
1022 
1023   \textcolor{keywordflow}{do} k=nz,2,-1
1024     do\_any = .false.
1025     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}
1026       dz\_int = 0.5*gv%H\_to\_Z*(h(i,j,k) + h(i,j,k-1))
1027       z\_from\_bot(i) = z\_from\_bot(i) + dz\_int \textcolor{comment}{! middle of the layer above}
1028 
1029       hb(i) = hb(i) + dz\_int
1030       drho\_bot(i) = drho\_bot(i) + drho\_int(i,k)
1031 
1032       \textcolor{keywordflow}{if} (z\_from\_bot(i) > h\_amp(i)) \textcolor{keywordflow}{then}
1033         \textcolor{keywordflow}{if} (k>2) \textcolor{keywordflow}{then}
1034           \textcolor{comment}{! Always include at least one full layer.}
1035           hb(i) = hb(i) + 0.5*gv%H\_to\_Z*(h(i,j,k-1) + h(i,j,k-2))
1036           drho\_bot(i) = drho\_bot(i) + drho\_int(i,k-1)
1037 \textcolor{keywordflow}{        endif}
1038         do\_i(i) = .false.
1039       \textcolor{keywordflow}{else}
1040         do\_any = .true.
1041 \textcolor{keywordflow}{      endif}
1042 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
1043     \textcolor{keywordflow}{if} (.not.do\_any) \textcolor{keywordflow}{exit}
1044 \textcolor{keywordflow}{  enddo}
1045 
1046   \textcolor{keywordflow}{do} i=is,ie
1047     \textcolor{keywordflow}{if} (hb(i) > 0.0) \textcolor{keywordflow}{then}
1048       n2\_bot(i) = (g\_rho0 * drho\_bot(i)) / hb(i)
1049     \textcolor{keywordflow}{else} ;  n2\_bot(i) = 0.0 ;\textcolor{keywordflow}{ endif}
1050     z\_from\_bot(i) = 0.5*gv%H\_to\_Z*h(i,j,nz)
1051     do\_i(i) = (g%mask2dT(i,j) > 0.5)
1052 \textcolor{keywordflow}{  enddo}
1053 
1054   \textcolor{keywordflow}{do} k=nz,2,-1
1055     do\_any = .false.
1056     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}
1057       dz\_int = 0.5*gv%H\_to\_Z*(h(i,j,k) + h(i,j,k-1))
1058       z\_from\_bot(i) = z\_from\_bot(i) + dz\_int \textcolor{comment}{! middle of the layer above}
1059 
1060       n2\_int(i,k) = n2\_bot(i)
1061       \textcolor{keywordflow}{if} (k>2) n2\_lay(i,k-1) = n2\_bot(i)
1062 
1063       \textcolor{keywordflow}{if} (z\_from\_bot(i) > h\_amp(i)) \textcolor{keywordflow}{then}
1064         \textcolor{keywordflow}{if} (k>2) n2\_int(i,k-1) = n2\_bot(i)
1065         do\_i(i) = .false.
1066       \textcolor{keywordflow}{else}
1067         do\_any = .true.
1068 \textcolor{keywordflow}{      endif}
1069 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
1070     \textcolor{keywordflow}{if} (.not.do\_any) \textcolor{keywordflow}{exit}
1071 \textcolor{keywordflow}{  enddo}
1072 
1073   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv%eqn\_of\_state)) \textcolor{keywordflow}{then}
1074     \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie
1075       drho\_int(i,k) = drho\_int\_unfilt(i,k)
1076 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
1077 \textcolor{keywordflow}{  endif}
1078 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__diffusivity_a07c0ab3f141f8f9e057be3150a940a94}\label{namespacemom__set__diffusivity_a07c0ab3f141f8f9e057be3150a940a94}} 
\index{mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}!find\+\_\+tke\+\_\+to\+\_\+kd@{find\+\_\+tke\+\_\+to\+\_\+kd}}
\index{find\+\_\+tke\+\_\+to\+\_\+kd@{find\+\_\+tke\+\_\+to\+\_\+kd}!mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}}
\subsubsection{\texorpdfstring{find\+\_\+tke\+\_\+to\+\_\+kd()}{find\_tke\_to\_kd()}}
{\footnotesize\ttfamily subroutine mom\+\_\+set\+\_\+diffusivity\+::find\+\_\+tke\+\_\+to\+\_\+kd (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke+1), intent(in)}]{d\+Rho\+\_\+int,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke), intent(in)}]{N2\+\_\+lay,  }\item[{integer, intent(in)}]{j,  }\item[{real, intent(in)}]{dt,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__set__diffusivity_1_1set__diffusivity__cs}{set\+\_\+diffusivity\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke), intent(out)}]{T\+K\+E\+\_\+to\+\_\+\+Kd,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke), intent(out)}]{max\+T\+KE,  }\item[{integer, dimension( g \%isd\+: g \%ied), intent(out)}]{kb }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Convert turbulent kinetic energy to diffusivity. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em tv} & Structure containing pointers to any available thermodynamic fields.\\
\hline
\mbox{\tt in}  & {\em drho\+\_\+int} & Change in locally referenced potential density across each interface \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em n2\+\_\+lay} & The squared buoyancy frequency of the layers \mbox{[}T-\/2 $\sim$$>$ s-\/2\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em j} & j-\/index of row to work on\\
\hline
\mbox{\tt in}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}.\\
\hline
 & {\em cs} & Diffusivity control structure\\
\hline
\mbox{\tt out}  & {\em tke\+\_\+to\+\_\+kd} & The conversion rate between the T\+KE dissipated within a layer and the diapycnal diffusivity witin that layer, usually ($\sim$\+Rho\+\_\+0 / (G\+\_\+\+Earth $\ast$ d\+Rho\+\_\+lay)) \mbox{[}Z2 T-\/1 / Z3 T-\/3 = T2 Z-\/1 $\sim$$>$ s2 m-\/1\mbox{]}\\
\hline
\mbox{\tt out}  & {\em maxtke} & The energy required to for a layer to entrain to its maximum realizable thickness \mbox{[}Z3 T-\/3 $\sim$$>$ m3 s-\/3\mbox{]}\\
\hline
\mbox{\tt out}  & {\em kb} & Index of lightest layer denser than the buffer layer, or -\/1 without a bulk mixed layer. \\
\hline
\end{DoxyParams}


Definition at line 704 of file M\+O\+M\+\_\+set\+\_\+diffusivity.\+F90.


\begin{DoxyCode}
704   \textcolor{keywordtype}{type}(ocean\_grid\_type),            \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure}
705   \textcolor{keywordtype}{type}(verticalGrid\_type),          \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure}
706   \textcolor{keywordtype}{type}(unit\_scale\_type),            \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}
707   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
708                                     \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{    !< Layer thicknesses [H ~> m or kg m-2]}
709   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),            \textcolor{keywordtype}{intent(in)}    :: tv\textcolor{comment}{   !< Structure containing pointers to any available}
710 \textcolor{comment}{                                                          !! thermodynamic fields.}
711   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)}, \textcolor{keywordtype}{intent(in)}  :: dRho\_int\textcolor{comment}{ !< Change in locally referenced potential
       density}
712 \textcolor{comment}{                                                          !! across each interface [R ~> kg m-3].}
713   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}    :: N2\_lay\textcolor{comment}{ !< The squared buoyancy frequency of the}
714 \textcolor{comment}{                                                          !! layers [T-2 ~> s-2].}
715   \textcolor{keywordtype}{integer},                          \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{    !< j-index of row to work on}
716   \textcolor{keywordtype}{real},                             \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{   !< Time increment [T ~> s].}
717   \textcolor{keywordtype}{type}(set\_diffusivity\_CS),         \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< Diffusivity control structure}
718   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(out)}   :: TKE\_to\_Kd\textcolor{comment}{ !< The conversion rate between the}
719 \textcolor{comment}{                                                          !! TKE dissipated within a layer and the}
720 \textcolor{comment}{                                                          !! diapycnal diffusivity witin that layer,}
721 \textcolor{comment}{                                                          !! usually (~Rho\_0 / (G\_Earth * dRho\_lay))}
722 \textcolor{comment}{                                                          !! [Z2 T-1 / Z3 T-3 = T2 Z-1 ~> s2 m-1]}
723   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(out)}   :: maxTKE\textcolor{comment}{ !< The energy required to for a layer to
       entrain}
724 \textcolor{comment}{                                                          !! to its maximum realizable thickness [Z3 T-3 ~>
       m3 s-3]}
725   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G))},      \textcolor{keywordtype}{intent(out)}   :: kb\textcolor{comment}{   !< Index of lightest layer denser than the buffer}
726 \textcolor{comment}{                                                          !! layer, or -1 without a bulk mixed layer.}
727   \textcolor{comment}{! Local variables}
728   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))} :: &
729     ds\_dsp1, &    \textcolor{comment}{! coordinate variable (sigma-2) difference across an}
730                   \textcolor{comment}{! interface divided by the difference across the interface}
731                   \textcolor{comment}{! below it [nondim]}
732     dsp1\_ds, &    \textcolor{comment}{! inverse coordinate variable (sigma-2) difference}
733                   \textcolor{comment}{! across an interface times the difference across the}
734                   \textcolor{comment}{! interface above it [nondim]}
735     rho\_0,   &    \textcolor{comment}{! Layer potential densities relative to surface pressure [R ~> kg m-3]}
736     maxent        \textcolor{comment}{! maxEnt is the maximum value of entrainment from below (with}
737                   \textcolor{comment}{! compensating entrainment from above to keep the layer}
738                   \textcolor{comment}{! density from changing) that will not deplete all of the}
739                   \textcolor{comment}{! layers above or below a layer within a timestep [Z ~> m].}
740   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: &
741     htot,    &    \textcolor{comment}{! total thickness above or below a layer, or the}
742                   \textcolor{comment}{! integrated thickness in the BBL [Z ~> m].}
743     mfkb,    &    \textcolor{comment}{! total thickness in the mixed and buffer layers times ds\_dsp1 [Z ~> m].}
744     p\_ref,   &    \textcolor{comment}{! array of tv%P\_Ref pressures [R L2 T-2 ~> Pa]}
745     rcv\_kmb, &    \textcolor{comment}{! coordinate density in the lowest buffer layer [R ~> kg m-3]}
746     p\_0           \textcolor{comment}{! An array of 0 pressures [R L2 T-2 ~> Pa]}
747 
748   \textcolor{keywordtype}{real} :: dh\_max      \textcolor{comment}{! maximum amount of entrainment a layer could}
749                       \textcolor{comment}{! undergo before entraining all fluid in the layers}
750                       \textcolor{comment}{! above or below [Z ~> m].}
751   \textcolor{keywordtype}{real} :: dRho\_lay    \textcolor{comment}{! density change across a layer [R ~> kg m-3]}
752   \textcolor{keywordtype}{real} :: Omega2      \textcolor{comment}{! rotation rate squared [T-2 ~> s-2]}
753   \textcolor{keywordtype}{real} :: G\_Rho0      \textcolor{comment}{! gravitation accel divided by Bouss ref density [Z T-2 R-1 ~> m4 s-2 kg-1]}
754   \textcolor{keywordtype}{real} :: G\_IRho0     \textcolor{comment}{! Alternate calculation of G\_Rho0 for reproducibility [Z T-2 R-1 ~> m4 s-2 kg-1]}
755   \textcolor{keywordtype}{real} :: I\_Rho0      \textcolor{comment}{! inverse of Boussinesq reference density [R-1 ~> m3 kg-1]}
756   \textcolor{keywordtype}{real} :: I\_dt        \textcolor{comment}{! 1/dt [T-1 ~> s-1]}
757   \textcolor{keywordtype}{real} :: H\_neglect   \textcolor{comment}{! negligibly small thickness [H ~> m or kg m-2]}
758   \textcolor{keywordtype}{real} :: hN2pO2      \textcolor{comment}{! h (N^2 + Omega^2), in [Z T-2 ~> m s-2].}
759   \textcolor{keywordtype}{logical} :: do\_i(SZI\_(G))
760 
761   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: EOSdom \textcolor{comment}{! The i-computational domain for the equation of state}
762   \textcolor{keywordtype}{integer} :: i, k, is, ie, nz, i\_rem, kmb, kb\_min
763   is = g%isc ; ie = g%iec ; nz = g%ke
764 
765   i\_dt      = 1.0 / dt
766   omega2    = cs%omega**2
767   h\_neglect = gv%H\_subroundoff
768   g\_rho0    = (us%L\_to\_Z**2 * gv%g\_Earth) / (gv%Rho0)
769   \textcolor{keywordflow}{if} (cs%answers\_2018) \textcolor{keywordflow}{then}
770     i\_rho0    = 1.0 / (gv%Rho0)
771     g\_irho0 = (us%L\_to\_Z**2 * gv%g\_Earth) * i\_rho0
772   \textcolor{keywordflow}{else}
773     g\_irho0 = g\_rho0
774 \textcolor{keywordflow}{  endif}
775 
776   \textcolor{comment}{! Simple but coordinate-independent estimate of Kd/TKE}
777   \textcolor{keywordflow}{if} (cs%simple\_TKE\_to\_Kd) \textcolor{keywordflow}{then}
778     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie
779       hn2po2 = (gv%H\_to\_Z * h(i,j,k)) * (n2\_lay(i,k) + omega2) \textcolor{comment}{! Units of Z T-2.}
780       \textcolor{keywordflow}{if} (hn2po2>0.) \textcolor{keywordflow}{then}
781         tke\_to\_kd(i,k) = 1.0 / hn2po2 \textcolor{comment}{! Units of T2 Z-1.}
782       else; tke\_to\_kd(i,k) = 0.;\textcolor{keywordflow}{ endif}
783       \textcolor{comment}{! The maximum TKE conversion we allow is really a statement}
784       \textcolor{comment}{! about the upper diffusivity we allow. Kd\_max must be set.}
785       maxtke(i,k) = hn2po2 * cs%Kd\_max \textcolor{comment}{! Units of Z3 T-3.}
786 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
787     kb(is:ie) = -1 \textcolor{comment}{! kb should not be used by any code in non-layered mode -AJA}
788     \textcolor{keywordflow}{return}
789 \textcolor{keywordflow}{  endif}
790 
791   \textcolor{comment}{! Determine kb - the index of the shallowest active interior layer.}
792   \textcolor{keywordflow}{if} (cs%bulkmixedlayer) \textcolor{keywordflow}{then}
793     kmb = gv%nk\_rho\_varies
794     \textcolor{keywordflow}{do} i=is,ie ; p\_0(i) = 0.0 ; p\_ref(i) = tv%P\_Ref ;\textcolor{keywordflow}{ enddo}
795     eosdom(:) = eos\_domain(g%HI)
796     \textcolor{keywordflow}{do} k=1,nz
797       \textcolor{keyword}{call }calculate\_density(tv%T(:,j,k), tv%S(:,j,k), p\_0, rho\_0(:,k), tv%eqn\_of\_state, eosdom)
798 \textcolor{keywordflow}{    enddo}
799     \textcolor{keyword}{call }calculate\_density(tv%T(:,j,kmb), tv%S(:,j,kmb), p\_ref, rcv\_kmb, tv%eqn\_of\_state, eosdom)
800 
801     kb\_min = kmb+1
802     \textcolor{keywordflow}{do} i=is,ie
803       \textcolor{comment}{!   Determine the next denser layer than the buffer layer in the}
804       \textcolor{comment}{! coordinate density (sigma-2).}
805       \textcolor{keywordflow}{do} k=kmb+1,nz-1 ; \textcolor{keywordflow}{if} (rcv\_kmb(i) <= gv%Rlay(k)) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}
806       kb(i) = k
807 
808     \textcolor{comment}{!   Backtrack, in case there are massive layers above that are stable}
809     \textcolor{comment}{! in sigma-0.}
810       \textcolor{keywordflow}{do} k=kb(i)-1,kmb+1,-1
811         \textcolor{keywordflow}{if} (rho\_0(i,kmb) > rho\_0(i,k)) \textcolor{keywordflow}{exit}
812         \textcolor{keywordflow}{if} (h(i,j,k)>2.0*gv%Angstrom\_H) kb(i) = k
813 \textcolor{keywordflow}{      enddo}
814 \textcolor{keywordflow}{    enddo}
815 
816     \textcolor{keyword}{call }set\_density\_ratios(h, tv, kb, g, gv, us, cs, j, ds\_dsp1, rho\_0)
817   \textcolor{keywordflow}{else} \textcolor{comment}{! not bulkmixedlayer}
818     kb\_min = 2 ; kmb = 0
819     \textcolor{keywordflow}{do} i=is,ie ; kb(i) = 1 ;\textcolor{keywordflow}{ enddo}
820     \textcolor{keyword}{call }set\_density\_ratios(h, tv, kb, g, gv, us, cs, j, ds\_dsp1)
821 \textcolor{keywordflow}{  endif}
822 
823   \textcolor{comment}{! Determine maxEnt - the maximum permitted entrainment from below by each}
824   \textcolor{comment}{! interior layer.}
825   \textcolor{keywordflow}{do} k=2,nz-1 ; \textcolor{keywordflow}{do} i=is,ie
826     dsp1\_ds(i,k) = 1.0 / ds\_dsp1(i,k)
827 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
828   \textcolor{keywordflow}{do} i=is,ie ; dsp1\_ds(i,nz) = 0.0 ;\textcolor{keywordflow}{ enddo}
829 
830   \textcolor{keywordflow}{if} (cs%bulkmixedlayer) \textcolor{keywordflow}{then}
831     kmb = gv%nk\_rho\_varies
832     \textcolor{keywordflow}{do} i=is,ie
833       htot(i) = gv%H\_to\_Z*h(i,j,kmb)
834       mfkb(i) = 0.0
835       \textcolor{keywordflow}{if} (kb(i) < nz) &
836         mfkb(i) = ds\_dsp1(i,kb(i)) * (gv%H\_to\_Z*(h(i,j,kmb) - gv%Angstrom\_H))
837 \textcolor{keywordflow}{    enddo}
838     \textcolor{keywordflow}{do} k=1,kmb-1 ; \textcolor{keywordflow}{do} i=is,ie
839       htot(i) = htot(i) + gv%H\_to\_Z*h(i,j,k)
840       mfkb(i) = mfkb(i) + ds\_dsp1(i,k+1)*(gv%H\_to\_Z*(h(i,j,k) - gv%Angstrom\_H))
841 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
842   \textcolor{keywordflow}{else}
843     \textcolor{keywordflow}{do} i=is,i
844       maxent(i,1) = 0.0 ; htot(i) = gv%H\_to\_Z*(h(i,j,1) - gv%Angstrom\_H)
845 \textcolor{keywordflow}{    enddo}
846 \textcolor{keywordflow}{  endif}
847   \textcolor{keywordflow}{do} k=kb\_min,nz-1 ; \textcolor{keywordflow}{do} i=is,ie
848     \textcolor{keywordflow}{if} (k == kb(i)) \textcolor{keywordflow}{then}
849       maxent(i,kb(i)) = mfkb(i)
850     \textcolor{keywordflow}{elseif} (k > kb(i)) \textcolor{keywordflow}{then}
851       \textcolor{keywordflow}{if} (cs%answers\_2018) \textcolor{keywordflow}{then}
852         maxent(i,k) = (1.0/dsp1\_ds(i,k))*(maxent(i,k-1) + htot(i))
853       \textcolor{keywordflow}{else}
854         maxent(i,k) = ds\_dsp1(i,k)*(maxent(i,k-1) + htot(i))
855 \textcolor{keywordflow}{      endif}
856       htot(i) = htot(i) + gv%H\_to\_Z*(h(i,j,k) - gv%Angstrom\_H)
857 \textcolor{keywordflow}{    endif}
858 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
859 
860   \textcolor{keywordflow}{do} i=is,ie
861     htot(i) = gv%H\_to\_Z*(h(i,j,nz) - gv%Angstrom\_H) ; maxent(i,nz) = 0.0
862     do\_i(i) = (g%mask2dT(i,j) > 0.5)
863 \textcolor{keywordflow}{  enddo}
864   \textcolor{keywordflow}{do} k=nz-1,kb\_min,-1
865     i\_rem = 0
866     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}
867       \textcolor{keywordflow}{if} (k<kb(i)) \textcolor{keywordflow}{then} ; do\_i(i) = .false. ; cycle ;\textcolor{keywordflow}{ endif}
868       i\_rem = i\_rem + 1  \textcolor{comment}{! Count the i-rows that are still being worked on.}
869       maxent(i,k) = min(maxent(i,k),dsp1\_ds(i,k+1)*maxent(i,k+1) + htot(i))
870       htot(i) = htot(i) + gv%H\_to\_Z*(h(i,j,k) - gv%Angstrom\_H)
871 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
872     \textcolor{keywordflow}{if} (i\_rem == 0) \textcolor{keywordflow}{exit}
873 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! k-loop}
874 
875   \textcolor{comment}{! Now set maxTKE and TKE\_to\_Kd.}
876   \textcolor{keywordflow}{do} i=is,ie
877     maxtke(i,1) = 0.0 ; tke\_to\_kd(i,1) = 0.0
878     maxtke(i,nz) = 0.0 ; tke\_to\_kd(i,nz) = 0.0
879 \textcolor{keywordflow}{  enddo}
880   \textcolor{keywordflow}{do} k=2,kmb ; \textcolor{keywordflow}{do} i=is,ie
881     maxtke(i,k) = 0.0
882     tke\_to\_kd(i,k) = 1.0 / ((n2\_lay(i,k) + omega2) * &
883                             (gv%H\_to\_Z*(h(i,j,k) + h\_neglect)))
884 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
885   \textcolor{keywordflow}{do} k=kmb+1,kb\_min-1 ; \textcolor{keywordflow}{do} i=is,ie
886     \textcolor{comment}{!   These are the properties in the deeper mixed and buffer layers, and}
887     \textcolor{comment}{! should perhaps be revisited.}
888     maxtke(i,k) = 0.0 ; tke\_to\_kd(i,k) = 0.0
889 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
890   \textcolor{keywordflow}{do} k=kb\_min,nz-1 ; \textcolor{keywordflow}{do} i=is,ie
891     \textcolor{keywordflow}{if} (k<kb(i)) \textcolor{keywordflow}{then}
892       maxtke(i,k) = 0.0
893       tke\_to\_kd(i,k) = 0.0
894     \textcolor{keywordflow}{else}
895       \textcolor{comment}{! maxTKE is found by determining the kappa that gives maxEnt.}
896       \textcolor{comment}{!  kappa\_max = I\_dt * dRho\_int(i,K+1) * maxEnt(i,k) * &}
897       \textcolor{comment}{!             (GV%H\_to\_Z*h(i,j,k) + dh\_max) / dRho\_lay}
898       \textcolor{comment}{!  maxTKE(i,k) = (GV%g\_Earth*US%L\_to\_Z**2) * dRho\_lay * kappa\_max}
899       \textcolor{comment}{! dRho\_int should already be non-negative, so the max is redundant?}
900       dh\_max = maxent(i,k) * (1.0 + dsp1\_ds(i,k))
901       drho\_lay = 0.5 * max(drho\_int(i,k) + drho\_int(i,k+1), 0.0)
902       maxtke(i,k) = i\_dt * (g\_irho0 * &
903           (0.5*max(drho\_int(i,k+1) + dsp1\_ds(i,k)*drho\_int(i,k), 0.0))) * &
904            ((gv%H\_to\_Z*h(i,j,k) + dh\_max) * maxent(i,k))
905       \textcolor{comment}{! TKE\_to\_Kd should be rho\_InSitu / G\_Earth * (delta rho\_InSitu)}
906       \textcolor{comment}{! The omega^2 term in TKE\_to\_Kd is due to a rescaling of the efficiency of turbulent}
907       \textcolor{comment}{! mixing by a factor of N^2 / (N^2 + Omega^2), as proposed by Melet et al., 2013?}
908       tke\_to\_kd(i,k) = 1.0 / (g\_rho0 * drho\_lay + &
909                               cs%omega**2 * gv%H\_to\_Z*(h(i,j,k) + h\_neglect))
910 \textcolor{keywordflow}{    endif}
911 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
912 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__diffusivity_a66f77b7e2f9c0c8254da4a0acd5a9996}\label{namespacemom__set__diffusivity_a66f77b7e2f9c0c8254da4a0acd5a9996}} 
\index{mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}!set\+\_\+bbl\+\_\+tke@{set\+\_\+bbl\+\_\+tke}}
\index{set\+\_\+bbl\+\_\+tke@{set\+\_\+bbl\+\_\+tke}!mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}}
\subsubsection{\texorpdfstring{set\+\_\+bbl\+\_\+tke()}{set\_bbl\_tke()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+set\+\_\+diffusivity\+::set\+\_\+bbl\+\_\+tke (\begin{DoxyParamCaption}\item[{real, dimension(szib\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{u,  }\item[{real, dimension(szi\+\_\+(g),szjb\+\_\+(g),szk\+\_\+(g)), intent(in)}]{v,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{type(forcing), intent(in)}]{fluxes,  }\item[{type(vertvisc\+\_\+type), intent(in)}]{visc,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__set__diffusivity_1_1set__diffusivity__cs}{set\+\_\+diffusivity\+\_\+cs}}), pointer}]{CS,  }\item[{type(ocean\+\_\+obc\+\_\+type), optional, pointer}]{O\+BC }\end{DoxyParamCaption})}



This subroutine calculates several properties related to bottom boundary layer turbulence. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em u} & The zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em v} & The meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em fluxes} & A structure of thermodynamic surface fluxes\\
\hline
\mbox{\tt in}  & {\em visc} & Structure containing vertical viscosities, bottom boundary layer properies, and related fields.\\
\hline
 & {\em cs} & Diffusivity control structure\\
\hline
 & {\em obc} & Open boundaries control structure. \\
\hline
\end{DoxyParams}


Definition at line 1719 of file M\+O\+M\+\_\+set\+\_\+diffusivity.\+F90.


\begin{DoxyCode}
1719   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure}
1720   \textcolor{keywordtype}{type}(verticalGrid\_type),  \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure}
1721   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}
1722   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, &
1723                             \textcolor{keywordtype}{intent(in)}    :: u\textcolor{comment}{    !< The zonal velocity [L T-1 ~> m s-1]}
1724   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, &
1725                             \textcolor{keywordtype}{intent(in)}    :: v\textcolor{comment}{    !< The meridional velocity [L T-1 ~> m s-1]}
1726   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
1727                             \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{    !< Layer thicknesses [H ~> m or kg m-2]}
1728   \textcolor{keywordtype}{type}(forcing),            \textcolor{keywordtype}{intent(in)}    :: fluxes\textcolor{comment}{ !< A structure of thermodynamic surface fluxes}
1729   \textcolor{keywordtype}{type}(vertvisc\_type),      \textcolor{keywordtype}{intent(in)}    :: visc\textcolor{comment}{ !< Structure containing vertical viscosities, bottom}
1730 \textcolor{comment}{                                                  !! boundary layer properies, and related fields.}
1731   \textcolor{keywordtype}{type}(set\_diffusivity\_CS), \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< Diffusivity control structure}
1732   \textcolor{keywordtype}{type}(ocean\_OBC\_type), \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer} :: OBC\textcolor{comment}{  !< Open boundaries control structure.}
1733 
1734   \textcolor{comment}{! This subroutine calculates several properties related to bottom}
1735   \textcolor{comment}{! boundary layer turbulence.}
1736 
1737   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: &
1738     htot          \textcolor{comment}{! total thickness above or below a layer, or the}
1739                   \textcolor{comment}{! integrated thickness in the BBL [Z ~> m].}
1740 
1741   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G))} :: &
1742     uhtot, &      \textcolor{comment}{! running integral of u in the BBL [Z L T-1 ~> m2 s-1]}
1743     ustar, &      \textcolor{comment}{! bottom boundary layer turbulence speed [Z T-1 ~> m s-1].}
1744     u2\_bbl        \textcolor{comment}{! square of the mean zonal velocity in the BBL [L2 T-2 ~> m2 s-2]}
1745 
1746   \textcolor{keywordtype}{real} :: vhtot(SZI\_(G)) \textcolor{comment}{! running integral of v in the BBL [Z L T-1 ~> m2 s-1]}
1747 
1748   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))} :: &
1749     vstar, & \textcolor{comment}{! ustar at at v-points [Z T-1 ~> m s-1].}
1750     v2\_bbl   \textcolor{comment}{! square of average meridional velocity in BBL [L2 T-2 ~> m2 s-2]}
1751 
1752   \textcolor{keywordtype}{real} :: cdrag\_sqrt  \textcolor{comment}{! square root of the drag coefficient [nondim]}
1753   \textcolor{keywordtype}{real} :: hvel        \textcolor{comment}{! thickness at velocity points [Z ~> m].}
1754 
1755   \textcolor{keywordtype}{logical} :: domore, do\_i(SZI\_(G))
1756   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz
1757   \textcolor{keywordtype}{integer} :: l\_seg
1758   \textcolor{keywordtype}{logical} :: local\_open\_u\_BC, local\_open\_v\_BC
1759   \textcolor{keywordtype}{logical} :: has\_obc
1760 
1761   local\_open\_u\_bc = .false.
1762   local\_open\_v\_bc = .false.
1763   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(obc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(obc)) \textcolor{keywordflow}{then}
1764     local\_open\_u\_bc = obc%open\_u\_BCs\_exist\_globally
1765     local\_open\_v\_bc = obc%open\_v\_BCs\_exist\_globally
1766 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ endif}
1767 
1768   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec ; nz = g%ke
1769 
1770   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"set\_BBL\_TKE: "}//&
1771          \textcolor{stringliteral}{"Module must be initialized before it is used."})
1772 
1773   \textcolor{keywordflow}{if} (.not.cs%bottomdraglaw .or. (cs%BBL\_effic<=0.0)) \textcolor{keywordflow}{then}
1774     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc%ustar\_BBL)) \textcolor{keywordflow}{then}
1775       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie ; visc%ustar\_BBL(i,j) = 0.0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
1776 \textcolor{keywordflow}{    endif}
1777     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc%TKE\_BBL)) \textcolor{keywordflow}{then}
1778       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie ; visc%TKE\_BBL(i,j) = 0.0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
1779 \textcolor{keywordflow}{    endif}
1780     \textcolor{keywordflow}{return}
1781 \textcolor{keywordflow}{  endif}
1782 
1783   cdrag\_sqrt = sqrt(cs%cdrag)
1784 
1785   \textcolor{comment}{!$OMP parallel default(shared) private(do\_i,vhtot,htot,domore,hvel,uhtot,ustar,u2\_bbl)}
1786   \textcolor{comment}{!$OMP do}
1787   \textcolor{keywordflow}{do} j=js-1,je
1788     \textcolor{comment}{! Determine ustar and the square magnitude of the velocity in the}
1789     \textcolor{comment}{! bottom boundary layer. Together these give the TKE source and}
1790     \textcolor{comment}{! vertical decay scale.}
1791     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} ((g%mask2dCv(i,j) > 0.5) .and. (cdrag\_sqrt*visc%bbl\_thick\_v(i,j) > 0.0)) \textcolor{keywordflow}{then}
1792       do\_i(i) = .true. ; vhtot(i) = 0.0 ; htot(i) = 0.0
1793       vstar(i,j) = visc%Kv\_bbl\_v(i,j) / (cdrag\_sqrt*visc%bbl\_thick\_v(i,j))
1794     \textcolor{keywordflow}{else}
1795       do\_i(i) = .false. ; vstar(i,j) = 0.0 ; htot(i) = 0.0
1796 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
1797     \textcolor{keywordflow}{do} k=nz,1,-1
1798       domore = .false.
1799       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}
1800         \textcolor{comment}{! Determine if grid point is an OBC}
1801         has\_obc = .false.
1802         \textcolor{keywordflow}{if} (local\_open\_v\_bc) \textcolor{keywordflow}{then}
1803           l\_seg = obc%segnum\_v(i,j)
1804           \textcolor{keywordflow}{if} (l\_seg /= obc\_none) \textcolor{keywordflow}{then}
1805             has\_obc = obc%segment(l\_seg)%open
1806 \textcolor{keywordflow}{          endif}
1807 \textcolor{keywordflow}{        endif}
1808 
1809         \textcolor{comment}{! Compute h based on OBC state}
1810         \textcolor{keywordflow}{if} (has\_obc) \textcolor{keywordflow}{then}
1811           \textcolor{keywordflow}{if} (obc%segment(l\_seg)%direction == obc\_direction\_n) \textcolor{keywordflow}{then}
1812             hvel = gv%H\_to\_Z*h(i,j,k)
1813           \textcolor{keywordflow}{else}
1814             hvel = gv%H\_to\_Z*h(i,j+1,k)
1815 \textcolor{keywordflow}{          endif}
1816         \textcolor{keywordflow}{else}
1817           hvel = 0.5*gv%H\_to\_Z*(h(i,j,k) + h(i,j+1,k))
1818 \textcolor{keywordflow}{        endif}
1819 
1820         \textcolor{keywordflow}{if} ((htot(i) + hvel) >= visc%bbl\_thick\_v(i,j)) \textcolor{keywordflow}{then}
1821           vhtot(i) = vhtot(i) + (visc%bbl\_thick\_v(i,j) - htot(i))*v(i,j,k)
1822           htot(i) = visc%bbl\_thick\_v(i,j)
1823           do\_i(i) = .false.
1824         \textcolor{keywordflow}{else}
1825           vhtot(i) = vhtot(i) + hvel*v(i,j,k)
1826           htot(i) = htot(i) + hvel
1827           domore = .true.
1828 \textcolor{keywordflow}{        endif}
1829 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}
1830       \textcolor{keywordflow}{if} (.not.domore) \textcolor{keywordflow}{exit}
1831 \textcolor{keywordflow}{    enddo}
1832     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} ((g%mask2dCv(i,j) > 0.5) .and. (htot(i) > 0.0)) \textcolor{keywordflow}{then}
1833       v2\_bbl(i,j) = (vhtot(i)*vhtot(i))/(htot(i)*htot(i))
1834     \textcolor{keywordflow}{else}
1835       v2\_bbl(i,j) = 0.0
1836 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
1837 \textcolor{keywordflow}{  enddo}
1838   \textcolor{comment}{!$OMP do}
1839   \textcolor{keywordflow}{do} j=js,je
1840     \textcolor{keywordflow}{do} i=is-1,ie ; \textcolor{keywordflow}{if} ((g%mask2dCu(i,j) > 0.5) .and. (cdrag\_sqrt*visc%bbl\_thick\_u(i,j) > 0.0))  \textcolor{keywordflow}{then}
1841       do\_i(i) = .true. ; uhtot(i) = 0.0 ; htot(i) = 0.0
1842       ustar(i) = visc%Kv\_bbl\_u(i,j) / (cdrag\_sqrt*visc%bbl\_thick\_u(i,j))
1843     \textcolor{keywordflow}{else}
1844       do\_i(i) = .false. ; ustar(i) = 0.0 ; htot(i) = 0.0
1845 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
1846     \textcolor{keywordflow}{do} k=nz,1,-1 ; domore = .false.
1847       \textcolor{keywordflow}{do} i=is-1,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}
1848         \textcolor{comment}{! Determine if grid point is an OBC}
1849         has\_obc = .false.
1850         \textcolor{keywordflow}{if} (local\_open\_u\_bc) \textcolor{keywordflow}{then}
1851           l\_seg = obc%segnum\_u(i,j)
1852           \textcolor{keywordflow}{if} (l\_seg /= obc\_none) \textcolor{keywordflow}{then}
1853             has\_obc = obc%segment(l\_seg)%open
1854 \textcolor{keywordflow}{          endif}
1855 \textcolor{keywordflow}{        endif}
1856 
1857         \textcolor{comment}{! Compute h based on OBC state}
1858         \textcolor{keywordflow}{if} (has\_obc) \textcolor{keywordflow}{then}
1859           \textcolor{keywordflow}{if} (obc%segment(l\_seg)%direction == obc\_direction\_e) \textcolor{keywordflow}{then}
1860             hvel = gv%H\_to\_Z*h(i,j,k)
1861           \textcolor{keywordflow}{else} \textcolor{comment}{! OBC\_DIRECTION\_W}
1862             hvel = gv%H\_to\_Z*h(i+1,j,k)
1863 \textcolor{keywordflow}{          endif}
1864         \textcolor{keywordflow}{else}
1865           hvel = 0.5*gv%H\_to\_Z*(h(i,j,k) + h(i+1,j,k))
1866 \textcolor{keywordflow}{        endif}
1867 
1868         \textcolor{keywordflow}{if} ((htot(i) + hvel) >= visc%bbl\_thick\_u(i,j)) \textcolor{keywordflow}{then}
1869           uhtot(i) = uhtot(i) + (visc%bbl\_thick\_u(i,j) - htot(i))*u(i,j,k)
1870           htot(i) = visc%bbl\_thick\_u(i,j)
1871           do\_i(i) = .false.
1872         \textcolor{keywordflow}{else}
1873           uhtot(i) = uhtot(i) + hvel*u(i,j,k)
1874           htot(i) = htot(i) + hvel
1875           domore = .true.
1876 \textcolor{keywordflow}{        endif}
1877 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}
1878       \textcolor{keywordflow}{if} (.not.domore) \textcolor{keywordflow}{exit}
1879 \textcolor{keywordflow}{    enddo}
1880     \textcolor{keywordflow}{do} i=is-1,ie ; \textcolor{keywordflow}{if} ((g%mask2dCu(i,j) > 0.5) .and. (htot(i) > 0.0)) \textcolor{keywordflow}{then}
1881       u2\_bbl(i) = (uhtot(i)*uhtot(i))/(htot(i)*htot(i))
1882     \textcolor{keywordflow}{else}
1883       u2\_bbl(i) = 0.0
1884 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
1885 
1886     \textcolor{keywordflow}{do} i=is,ie
1887       visc%ustar\_BBL(i,j) = sqrt(0.5*g%IareaT(i,j) * &
1888                 ((g%areaCu(i-1,j)*(ustar(i-1)*ustar(i-1)) + &
1889                   g%areaCu(i,j)*(ustar(i)*ustar(i))) + &
1890                  (g%areaCv(i,j-1)*(vstar(i,j-1)*vstar(i,j-1)) + &
1891                   g%areaCv(i,j)*(vstar(i,j)*vstar(i,j))) ) )
1892       visc%TKE\_BBL(i,j) = us%L\_to\_Z**2 * &
1893                  (((g%areaCu(i-1,j)*(ustar(i-1)*u2\_bbl(i-1)) + &
1894                     g%areaCu(i,j) * (ustar(i)*u2\_bbl(i))) + &
1895                    (g%areaCv(i,j-1)*(vstar(i,j-1)*v2\_bbl(i,j-1)) + &
1896                     g%areaCv(i,j) * (vstar(i,j)*v2\_bbl(i,j))) )*g%IareaT(i,j))
1897 \textcolor{keywordflow}{    enddo}
1898 \textcolor{keywordflow}{  enddo}
1899   \textcolor{comment}{!$OMP end parallel}
1900 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__diffusivity_a5ba8a3be6234304aa5f1dfd0b831078a}\label{namespacemom__set__diffusivity_a5ba8a3be6234304aa5f1dfd0b831078a}} 
\index{mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}!set\+\_\+density\+\_\+ratios@{set\+\_\+density\+\_\+ratios}}
\index{set\+\_\+density\+\_\+ratios@{set\+\_\+density\+\_\+ratios}!mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}}
\subsubsection{\texorpdfstring{set\+\_\+density\+\_\+ratios()}{set\_density\_ratios()}}
{\footnotesize\ttfamily subroutine mom\+\_\+set\+\_\+diffusivity\+::set\+\_\+density\+\_\+ratios (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{integer, dimension(szi\+\_\+(g)), intent(in)}]{kb,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__set__diffusivity_1_1set__diffusivity__cs}{set\+\_\+diffusivity\+\_\+cs}}), pointer}]{CS,  }\item[{integer, intent(in)}]{j,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(out)}]{ds\+\_\+dsp1,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in), optional}]{rho\+\_\+0 }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em tv} & Structure containing pointers to any available thermodynamic fields; absent fields have N\+U\+LL ptrs.\\
\hline
\mbox{\tt in}  & {\em kb} & Index of lightest layer denser than the buffer layer, or -\/1 without a bulk mixed layer.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em cs} & Control structure returned by previous call to diabatic\+\_\+entrain\+\_\+init.\\
\hline
\mbox{\tt in}  & {\em j} & Meridional index upon which to work.\\
\hline
\mbox{\tt out}  & {\em ds\+\_\+dsp1} & Coordinate variable (sigma-\/2) difference across an interface divided by the difference across the interface below it \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em rho\+\_\+0} & Layer potential densities relative to \\
\hline
\end{DoxyParams}


Definition at line 1904 of file M\+O\+M\+\_\+set\+\_\+diffusivity.\+F90.


\begin{DoxyCode}
1904   \textcolor{keywordtype}{type}(ocean\_grid\_type),            \textcolor{keywordtype}{intent(in)}   :: G\textcolor{comment}{  !< The ocean's grid structure.}
1905   \textcolor{keywordtype}{type}(verticalGrid\_type),          \textcolor{keywordtype}{intent(in)}   :: GV\textcolor{comment}{ !< The ocean's vertical grid structure.}
1906   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
1907                                     \textcolor{keywordtype}{intent(in)}   :: h\textcolor{comment}{  !< Layer thicknesses [H ~> m or kg m-2].}
1908   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),            \textcolor{keywordtype}{intent(in)}   :: tv\textcolor{comment}{ !< Structure containing pointers to any}
1909 \textcolor{comment}{                                                       !! available thermodynamic fields; absent}
1910 \textcolor{comment}{                                                       !! fields have NULL ptrs.}
1911   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G))},      \textcolor{keywordtype}{intent(in)}   :: kb\textcolor{comment}{ !< Index of lightest layer denser than the buffer}
1912 \textcolor{comment}{                                                       !! layer, or -1 without a bulk mixed layer.}
1913   \textcolor{keywordtype}{type}(unit\_scale\_type),            \textcolor{keywordtype}{intent(in)}   :: US\textcolor{comment}{ !< A dimensional unit scaling type}
1914   \textcolor{keywordtype}{type}(set\_diffusivity\_CS),         \textcolor{keywordtype}{pointer}      :: CS\textcolor{comment}{ !< Control structure returned by previous}
1915 \textcolor{comment}{                                                       !! call to diabatic\_entrain\_init.}
1916   \textcolor{keywordtype}{integer},                          \textcolor{keywordtype}{intent(in)}   :: j\textcolor{comment}{  !< Meridional index upon which to work.}
1917   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(out)}  :: ds\_dsp1\textcolor{comment}{ !< Coordinate variable (sigma-2)}
1918 \textcolor{comment}{                                                       !! difference across an interface divided by}
1919 \textcolor{comment}{                                                       !! the difference across the interface below}
1920 \textcolor{comment}{                                                       !! it [nondim]}
1921   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, &
1922                           \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}   :: rho\_0\textcolor{comment}{ !< Layer potential densities relative to}
1923 \textcolor{comment}{                                                       !! surface press [R ~> kg m-3].}
1924 
1925   \textcolor{comment}{! Local variables}
1926   \textcolor{keywordtype}{real} :: g\_R0                     \textcolor{comment}{! g\_R0 is a rescaled version of g/Rho [L2 Z-1 R-1 T-2 ~> m4 kg-1 s-2]}
1927   \textcolor{keywordtype}{real} :: eps, tmp                 \textcolor{comment}{! nondimensional temproray variables}
1928   \textcolor{keywordtype}{real} :: a(SZK\_(G)), a\_0(SZK\_(G)) \textcolor{comment}{! nondimensional temporary variables}
1929   \textcolor{keywordtype}{real} :: p\_ref(SZI\_(G))           \textcolor{comment}{! an array of tv%P\_Ref pressures [R L2 T-2 ~> Pa]}
1930   \textcolor{keywordtype}{real} :: Rcv(SZI\_(G),SZK\_(G))     \textcolor{comment}{! coordinate density in the mixed and buffer layers [R ~> kg m-3]}
1931   \textcolor{keywordtype}{real} :: I\_Drho                   \textcolor{comment}{! temporary variable [R-1 ~> m3 kg-1]}
1932 
1933   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: EOSdom \textcolor{comment}{! The i-computational domain for the equation of state}
1934   \textcolor{keywordtype}{integer} :: i, k, k3, is, ie, nz, kmb
1935   is = g%isc ; ie = g%iec ; nz = g%ke
1936 
1937   \textcolor{keywordflow}{do} k=2,nz-1
1938     \textcolor{keywordflow}{if} (gv%g\_prime(k+1) /= 0.0) \textcolor{keywordflow}{then}
1939       \textcolor{keywordflow}{do} i=is,ie
1940         ds\_dsp1(i,k) = gv%g\_prime(k) / gv%g\_prime(k+1)
1941 \textcolor{keywordflow}{      enddo}
1942     \textcolor{keywordflow}{else}
1943       \textcolor{keywordflow}{do} i=is,ie
1944         ds\_dsp1(i,k) = 1.
1945 \textcolor{keywordflow}{      enddo}
1946 \textcolor{keywordflow}{    endif}
1947 \textcolor{keywordflow}{  enddo}
1948 
1949   \textcolor{keywordflow}{if} (cs%bulkmixedlayer) \textcolor{keywordflow}{then}
1950     g\_r0 = gv%g\_Earth / (gv%Rho0)
1951     kmb = gv%nk\_rho\_varies
1952     eps = 0.1
1953     \textcolor{keywordflow}{do} i=is,ie ; p\_ref(i) = tv%P\_Ref ;\textcolor{keywordflow}{ enddo}
1954     eosdom(:) = eos\_domain(g%HI)
1955     \textcolor{keywordflow}{do} k=1,kmb
1956       \textcolor{keyword}{call }calculate\_density(tv%T(:,j,k), tv%S(:,j,k), p\_ref, rcv(:,k), tv%eqn\_of\_state, eosdom)
1957 \textcolor{keywordflow}{    enddo}
1958     \textcolor{keywordflow}{do} i=is,ie
1959       \textcolor{keywordflow}{if} (kb(i) <= nz-1) \textcolor{keywordflow}{then}
1960 \textcolor{comment}{!   Set up appropriately limited ratios of the reduced gravities of the}
1961 \textcolor{comment}{! interfaces above and below the buffer layer and the next denser layer.}
1962         k = kb(i)
1963 
1964         i\_drho = g\_r0 / gv%g\_prime(k+1)
1965         \textcolor{comment}{! The indexing convention for a is appropriate for the interfaces.}
1966         \textcolor{keywordflow}{do} k3=1,kmb
1967           a(k3+1) = (gv%Rlay(k) - rcv(i,k3)) * i\_drho
1968 \textcolor{keywordflow}{        enddo}
1969         \textcolor{keywordflow}{if} ((\textcolor{keyword}{present}(rho\_0)) .and. (a(kmb+1) < 2.0*eps*ds\_dsp1(i,k))) \textcolor{keywordflow}{then}
1970 \textcolor{comment}{!   If the buffer layer nearly matches the density of the layer below in the}
1971 \textcolor{comment}{! coordinate variable (sigma-2), use the sigma-0-based density ratio if it is}
1972 \textcolor{comment}{! greater (and stable).}
1973           \textcolor{keywordflow}{if} ((rho\_0(i,k) > rho\_0(i,kmb)) .and. &
1974               (rho\_0(i,k+1) > rho\_0(i,k))) \textcolor{keywordflow}{then}
1975             i\_drho = 1.0 / (rho\_0(i,k+1)-rho\_0(i,k))
1976             a\_0(kmb+1) = min((rho\_0(i,k)-rho\_0(i,kmb)) * i\_drho, ds\_dsp1(i,k))
1977             \textcolor{keywordflow}{if} (a\_0(kmb+1) > a(kmb+1)) \textcolor{keywordflow}{then}
1978               \textcolor{keywordflow}{do} k3=2,kmb
1979                 a\_0(k3) = a\_0(kmb+1) + (rho\_0(i,kmb)-rho\_0(i,k3-1)) * i\_drho
1980 \textcolor{keywordflow}{              enddo}
1981               \textcolor{keywordflow}{if} (a(kmb+1) <= eps*ds\_dsp1(i,k)) \textcolor{keywordflow}{then}
1982                 \textcolor{keywordflow}{do} k3=2,kmb+1 ; a(k3) = a\_0(k3) ;\textcolor{keywordflow}{ enddo}
1983               \textcolor{keywordflow}{else}
1984 \textcolor{comment}{! Alternative...  tmp = 0.5*(1.0 - cos(PI*(a(K2+1)/(eps*ds\_dsp1(i,k)) - 1.0)) )}
1985                 tmp = a(kmb+1)/(eps*ds\_dsp1(i,k)) - 1.0
1986                 \textcolor{keywordflow}{do} k3=2,kmb+1 ; a(k3) = tmp*a(k3) + (1.0-tmp)*a\_0(k3) ;\textcolor{keywordflow}{ enddo}
1987 \textcolor{keywordflow}{              endif}
1988 \textcolor{keywordflow}{            endif}
1989 \textcolor{keywordflow}{          endif}
1990 \textcolor{keywordflow}{        endif}
1991 
1992         ds\_dsp1(i,k) = max(a(kmb+1),1e-5)
1993 
1994         \textcolor{keywordflow}{do} k3=2,kmb
1995 \textcolor{comment}{!           ds\_dsp1(i,k3) = MAX(a(k3),1e-5)}
1996           \textcolor{comment}{! Deliberately treat convective instabilies of the upper mixed}
1997           \textcolor{comment}{! and buffer layers with respect to the deepest buffer layer as}
1998           \textcolor{comment}{! though they don't exist.  They will be eliminated by the upcoming}
1999           \textcolor{comment}{! call to the mixedlayer code anyway.}
2000           \textcolor{comment}{! The indexing convention is appropriate for the interfaces.}
2001           ds\_dsp1(i,k3) = max(a(k3),ds\_dsp1(i,k))
2002 \textcolor{keywordflow}{        enddo}
2003 \textcolor{keywordflow}{      endif} \textcolor{comment}{! (kb(i) <= nz-1)}
2004 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! I-loop.}
2005 \textcolor{keywordflow}{  endif} \textcolor{comment}{! bulkmixedlayer}
2006 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__diffusivity_a87687e318bfe2d594ce1f57cb5d191e4}\label{namespacemom__set__diffusivity_a87687e318bfe2d594ce1f57cb5d191e4}} 
\index{mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}!set\+\_\+diffusivity@{set\+\_\+diffusivity}}
\index{set\+\_\+diffusivity@{set\+\_\+diffusivity}!mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}}
\subsubsection{\texorpdfstring{set\+\_\+diffusivity()}{set\_diffusivity()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+set\+\_\+diffusivity\+::set\+\_\+diffusivity (\begin{DoxyParamCaption}\item[{real, dimension(szib\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{u,  }\item[{real, dimension(szi\+\_\+(g),szjb\+\_\+(g),szk\+\_\+(g)), intent(in)}]{v,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{u\+\_\+h,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{v\+\_\+h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(inout)}]{tv,  }\item[{type(forcing), intent(in)}]{fluxes,  }\item[{type(optics\+\_\+type), pointer}]{optics,  }\item[{type(vertvisc\+\_\+type), intent(inout)}]{visc,  }\item[{real, intent(in)}]{dt,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__set__diffusivity_1_1set__diffusivity__cs}{set\+\_\+diffusivity\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(out), optional}]{Kd\+\_\+lay,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)+1), intent(out), optional}]{Kd\+\_\+int,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)+1), intent(out), optional}]{Kd\+\_\+extra\+\_\+T,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)+1), intent(out), optional}]{Kd\+\_\+extra\+\_\+S }\end{DoxyParamCaption})}



Sets the interior vertical diffusion of scalars due to the following processes\+: 


\begin{DoxyEnumerate}
\item Shear-\/driven mixing\+: two options, Jackson et at. and K\+PP interior;
\item Background mixing via C\+V\+Mix (Bryan-\/\+Lewis profile) or the scheme described by Harrison \& Hallberg, J\+PO 2008;
\item Double-\/diffusion, old method and new method via C\+V\+Mix;
\item Tidal mixing\+: many options available, see \mbox{\hyperlink{MOM__tidal__mixing_8F90_source}{M\+O\+M\+\_\+tidal\+\_\+mixing.\+F90}}; In addition, this subroutine has the option to set the interior vertical viscosity associated with processes 1,2 and 4 listed above, which is stored in viscKv\+\_\+slow. Vertical viscosity due to shear-\/driven mixing is passed via viscKv\+\_\+shear
\end{DoxyEnumerate}


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em u} & The zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em v} & The meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em u\+\_\+h} & Zonal velocity interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em v\+\_\+h} & Meridional velocity interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em tv} & Structure with pointers to thermodynamic fields. Out is for tvTempx\+PmE.\\
\hline
\mbox{\tt in}  & {\em fluxes} & A structure of thermodynamic surface fluxes\\
\hline
 & {\em optics} & A structure describing the optical properties of the ocean.\\
\hline
\mbox{\tt in,out}  & {\em visc} & Structure containing vertical viscosities, bottom boundary layer properies, and related fields.\\
\hline
\mbox{\tt in}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}.\\
\hline
 & {\em cs} & Module control structure.\\
\hline
\mbox{\tt out}  & {\em kd\+\_\+lay} & Diapycnal diffusivity of each layer \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}.\\
\hline
\mbox{\tt out}  & {\em kd\+\_\+int} & Diapycnal diffusivity at each interface \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}.\\
\hline
\mbox{\tt out}  & {\em kd\+\_\+extra\+\_\+t} & The extra diffusivity at interfaces of\\
\hline
\mbox{\tt out}  & {\em kd\+\_\+extra\+\_\+s} & The extra diffusivity at interfaces of \\
\hline
\end{DoxyParams}


Definition at line 213 of file M\+O\+M\+\_\+set\+\_\+diffusivity.\+F90.


\begin{DoxyCode}
213   \textcolor{keywordtype}{type}(ocean\_grid\_type),     \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}
214   \textcolor{keywordtype}{type}(verticalGrid\_type),   \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}
215   \textcolor{keywordtype}{type}(unit\_scale\_type),     \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}
216   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, &
217                              \textcolor{keywordtype}{intent(in)}    :: u\textcolor{comment}{    !< The zonal velocity [L T-1 ~> m s-1].}
218   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, &
219                              \textcolor{keywordtype}{intent(in)}    :: v\textcolor{comment}{    !< The meridional velocity [L T-1 ~> m s-1].}
220   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  &
221                              \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{    !< Layer thicknesses [H ~> m or kg m-2].}
222   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  &
223                              \textcolor{keywordtype}{intent(in)}    :: u\_h\textcolor{comment}{  !< Zonal velocity interpolated to h points [L T-1 ~> m
       s-1].}
224   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  &
225                              \textcolor{keywordtype}{intent(in)}    :: v\_h\textcolor{comment}{  !< Meridional velocity interpolated to h points [L T-1
       ~> m s-1].}
226   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),     \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{   !< Structure with pointers to thermodynamic}
227 \textcolor{comment}{                                                   !! fields. Out is for tv%TempxPmE.}
228   \textcolor{keywordtype}{type}(forcing),             \textcolor{keywordtype}{intent(in)}    :: fluxes\textcolor{comment}{ !< A structure of thermodynamic surface fluxes}
229   \textcolor{keywordtype}{type}(optics\_type),         \textcolor{keywordtype}{pointer}       :: optics\textcolor{comment}{ !< A structure describing the optical}
230 \textcolor{comment}{                                                   !!  properties of the ocean.}
231   \textcolor{keywordtype}{type}(vertvisc\_type),       \textcolor{keywordtype}{intent(inout)} :: visc\textcolor{comment}{ !< Structure containing vertical viscosities, bottom}
232 \textcolor{comment}{                                                   !! boundary layer properies, and related fields.}
233   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{   !< Time increment [T ~> s].}
234   \textcolor{keywordtype}{type}(set\_diffusivity\_CS),  \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< Module control structure.}
235   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
236                    \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)}   :: Kd\_lay\textcolor{comment}{ !< Diapycnal diffusivity of each layer [Z2 T-1 ~> m2
       s-1].}
237   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G)+1)}, &
238                    \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)}   :: Kd\_int\textcolor{comment}{ !< Diapycnal diffusivity at each interface [Z2 T-1 ~>
       m2 s-1].}
239   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G)+1)}, &
240                    \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)}   :: Kd\_extra\_T\textcolor{comment}{ !< The extra diffusivity at interfaces of}
241 \textcolor{comment}{                                                     !! temperature due to double diffusion relative to}
242 \textcolor{comment}{                                                     !! the diffusivity of density [Z2 T-1 ~> m2 s-1].}
243   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G)+1)}, &
244                    \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)}   :: Kd\_extra\_S\textcolor{comment}{ !< The extra diffusivity at interfaces of}
245 \textcolor{comment}{                                                     !! salinity due to double diffusion relative to}
246 \textcolor{comment}{                                                     !! the diffusivity of density [Z2 T-1 ~> m2 s-1].}
247 
248   \textcolor{comment}{! local variables}
249   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: &
250     N2\_bot        \textcolor{comment}{! bottom squared buoyancy frequency [T-2 ~> s-2]}
251 
252   \textcolor{keywordtype}{type}(diffusivity\_diags)  :: dd \textcolor{comment}{! structure with arrays of available diags}
253 
254   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))} :: &
255     T\_f, S\_f      \textcolor{comment}{! Temperature and salinity [degC] and [ppt] with properties in massless layers}
256                   \textcolor{comment}{! filled vertically by diffusion or the properties after full convective adjustment.}
257 
258   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))} :: &
259     N2\_lay, &     !< Squared buoyancy frequency associated with layers [T-2 ~> s-2]
260     Kd\_lay\_2d, &  !< The layer diffusivities [Z2 T-1 ~> m2 s-1]
261     maxTKE, &     !< Energy required to entrain to h\_max [Z3 T-3 ~> m3 s-3]
262     TKE\_to\_Kd\textcolor{comment}{     !< Conversion rate (~1.0 / (G\_Earth + dRho\_lay)) between}
263 \textcolor{comment}{                  !< TKE dissipated within a layer and Kd in that layer}
264 \textcolor{comment}{                  !< [Z2 T-1 / Z3 T-3 = T2 Z-1 ~> s2 m-1]}
265 
266   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)} :: &
267     N2\_int,   &   !< squared buoyancy frequency associated at interfaces [T-2 ~> s-2]
268     Kd\_int\_2d, &  !< The interface diffusivities [Z2 T-1 ~> m2 s-1]
269     Kv\_bkgnd, &   !< The background diffusion related interface viscosities [Z2 T-1 ~> m2 s-1]
270     dRho\_int, &   !< Locally referenced potential density difference across interfaces [R ~> kg m-3]
271     KT\_extra, &   !< Double difusion diffusivity of temperature [Z2 T-1 ~> m2 s-1]
272     KS\_extra\textcolor{comment}{      !< Double difusion diffusivity of salinity [Z2 T-1 ~> m2 s-1]}
273 
274   \textcolor{keywordtype}{real} :: dissip        \textcolor{comment}{! local variable for dissipation calculations [Z2 R T-3 ~> W m-3]}
275   \textcolor{keywordtype}{real} :: Omega2        \textcolor{comment}{! squared absolute rotation rate [T-2 ~> s-2]}
276 
277   \textcolor{keywordtype}{logical}   :: use\_EOS      \textcolor{comment}{! If true, compute density from T/S using equation of state.}
278   \textcolor{keywordtype}{logical}   :: TKE\_to\_Kd\_used \textcolor{comment}{! If true, TKE\_to\_Kd and maxTKE need to be calculated.}
279   \textcolor{keywordtype}{integer}   :: kb(SZI\_(G))  \textcolor{comment}{! The index of the lightest layer denser than the}
280                             \textcolor{comment}{! buffer layer, or -1 without a bulk mixed layer.}
281   \textcolor{keywordtype}{logical}   :: showCallTree \textcolor{comment}{! If true, show the call tree.}
282 
283   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz, isd, ied, jsd, jed
284 
285   \textcolor{keywordtype}{real}      :: kappa\_dt\_fill \textcolor{comment}{! diffusivity times a timestep used to fill massless layers [Z2 ~> m2]}
286 
287   is  = g%isc ; ie  = g%iec ; js  = g%jsc ; je  = g%jec ; nz = g%ke
288   isd = g%isd ; ied = g%ied ; jsd = g%jsd ; jed = g%jed
289   showcalltree = calltree\_showquery()
290   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_enter(\textcolor{stringliteral}{"set\_diffusivity(), MOM\_set\_diffusivity.F90"})
291 
292   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"set\_diffusivity: "}//&
293          \textcolor{stringliteral}{"Module must be initialized before it is used."})
294 
295   \textcolor{keywordflow}{if} (cs%answers\_2018) \textcolor{keywordflow}{then}
296     \textcolor{comment}{! These hard-coded dimensional parameters are being replaced.}
297     kappa\_dt\_fill = us%m\_to\_Z**2 * 1.e-3 * 7200.
298   \textcolor{keywordflow}{else}
299     kappa\_dt\_fill = cs%Kd\_smooth * dt
300 \textcolor{keywordflow}{  endif}
301   omega2 = cs%omega * cs%omega
302 
303   use\_eos = \textcolor{keyword}{associated}(tv%eqn\_of\_state)
304 
305   \textcolor{keywordflow}{if} ((cs%use\_CVMix\_ddiff .or. cs%double\_diffusion) .and. &
306       .not.(\textcolor{keyword}{present}(kd\_extra\_t) .and. \textcolor{keyword}{present}(kd\_extra\_s))) &
307      \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"set\_diffusivity: both Kd\_extra\_T and Kd\_extra\_S must be present "}//&
308                            \textcolor{stringliteral}{"when USE\_CVMIX\_DDIFF or DOUBLE\_DIFFUSION are true."})
309 
310   tke\_to\_kd\_used = (cs%use\_tidal\_mixing .or. cs%ML\_radiation .or. &
311                    (cs%bottomdraglaw .and. .not.cs%use\_LOTW\_BBL\_diffusivity))
312 
313   \textcolor{comment}{! Set Kd\_lay, Kd\_int and Kv\_slow to constant values, mostly to fill the halos.}
314   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_lay)) kd\_lay(:,:,:) = cs%Kd
315   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_int)) kd\_int(:,:,:) = cs%Kd
316   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_extra\_t)) kd\_extra\_t(:,:,:) = 0.0
317   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_extra\_s)) kd\_extra\_s(:,:,:) = 0.0
318   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc%Kv\_slow)) visc%Kv\_slow(:,:,:) = cs%Kv
319 
320   \textcolor{comment}{! Set up arrays for diagnostics.}
321 
322   \textcolor{keywordflow}{if} (cs%id\_N2 > 0) \textcolor{keywordflow}{then}
323     \textcolor{keyword}{allocate}(dd%N2\_3d(isd:ied,jsd:jed,nz+1)) ; dd%N2\_3d(:,:,:) = 0.0
324 \textcolor{keywordflow}{  endif}
325   \textcolor{keywordflow}{if} (cs%id\_Kd\_user > 0) \textcolor{keywordflow}{then}
326     \textcolor{keyword}{allocate}(dd%Kd\_user(isd:ied,jsd:jed,nz+1)) ; dd%Kd\_user(:,:,:) = 0.0
327 \textcolor{keywordflow}{  endif}
328   \textcolor{keywordflow}{if} (cs%id\_Kd\_work > 0) \textcolor{keywordflow}{then}
329     \textcolor{keyword}{allocate}(dd%Kd\_work(isd:ied,jsd:jed,nz)) ; dd%Kd\_work(:,:,:) = 0.0
330 \textcolor{keywordflow}{  endif}
331   \textcolor{keywordflow}{if} (cs%id\_maxTKE > 0) \textcolor{keywordflow}{then}
332     \textcolor{keyword}{allocate}(dd%maxTKE(isd:ied,jsd:jed,nz)) ; dd%maxTKE(:,:,:) = 0.0
333 \textcolor{keywordflow}{  endif}
334   \textcolor{keywordflow}{if} (cs%id\_TKE\_to\_Kd > 0) \textcolor{keywordflow}{then}
335     \textcolor{keyword}{allocate}(dd%TKE\_to\_Kd(isd:ied,jsd:jed,nz)) ; dd%TKE\_to\_Kd(:,:,:) = 0.0
336 \textcolor{keywordflow}{  endif}
337   \textcolor{keywordflow}{if} ((cs%double\_diffusion) .and. (cs%id\_KT\_extra > 0)) \textcolor{keywordflow}{then}
338     \textcolor{keyword}{allocate}(dd%KT\_extra(isd:ied,jsd:jed,nz+1)) ; dd%KT\_extra(:,:,:) = 0.0
339 \textcolor{keywordflow}{  endif}
340   \textcolor{keywordflow}{if} ((cs%double\_diffusion) .and. (cs%id\_KS\_extra > 0)) \textcolor{keywordflow}{then}
341     \textcolor{keyword}{allocate}(dd%KS\_extra(isd:ied,jsd:jed,nz+1)) ; dd%KS\_extra(:,:,:) = 0.0
342 \textcolor{keywordflow}{  endif}
343   \textcolor{keywordflow}{if} (cs%id\_R\_rho > 0) \textcolor{keywordflow}{then}
344     \textcolor{keyword}{allocate}(dd%drho\_rat(isd:ied,jsd:jed,nz+1)) ; dd%drho\_rat(:,:,:) = 0.0
345 \textcolor{keywordflow}{  endif}
346   \textcolor{keywordflow}{if} (cs%id\_Kd\_BBL > 0) \textcolor{keywordflow}{then}
347     \textcolor{keyword}{allocate}(dd%Kd\_BBL(isd:ied,jsd:jed,nz+1)) ; dd%Kd\_BBL(:,:,:) = 0.0
348 \textcolor{keywordflow}{  endif}
349 
350   \textcolor{keywordflow}{if} (cs%id\_Kd\_bkgnd > 0) \textcolor{keywordflow}{then}
351     \textcolor{keyword}{allocate}(dd%Kd\_bkgnd(isd:ied,jsd:jed,nz+1)) ; dd%Kd\_bkgnd(:,:,:) = 0.
352 \textcolor{keywordflow}{  endif}
353   \textcolor{keywordflow}{if} (cs%id\_Kv\_bkgnd > 0) \textcolor{keywordflow}{then}
354     \textcolor{keyword}{allocate}(dd%Kv\_bkgnd(isd:ied,jsd:jed,nz+1)) ; dd%Kv\_bkgnd(:,:,:) = 0.
355 \textcolor{keywordflow}{  endif}
356 
357   \textcolor{comment}{! set up arrays for tidal mixing diagnostics}
358   \textcolor{keyword}{call }setup\_tidal\_diagnostics(g, cs%tidal\_mixing\_CSp)
359 
360   \textcolor{keywordflow}{if} (cs%useKappaShear) \textcolor{keywordflow}{then}
361     \textcolor{keywordflow}{if} (cs%debug) \textcolor{keywordflow}{then}
362       \textcolor{keyword}{call }hchksum\_pair(\textcolor{stringliteral}{"before calc\_KS [uv]\_h"}, u\_h, v\_h, g%HI, scale=us%L\_T\_to\_m\_s)
363 \textcolor{keywordflow}{    endif}
364     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_kappashear)
365     \textcolor{keywordflow}{if} (cs%Vertex\_shear) \textcolor{keywordflow}{then}
366       \textcolor{keyword}{call }full\_convection(g, gv, us, h, tv, t\_f, s\_f, fluxes%p\_surf, &
367                            (gv%Z\_to\_H**2)*kappa\_dt\_fill, halo=1)
368 
369       \textcolor{keyword}{call }calc\_kappa\_shear\_vertex(u, v, h, t\_f, s\_f, tv, fluxes%p\_surf, visc%Kd\_shear, &
370                                    visc%TKE\_turb, visc%Kv\_shear\_Bu, dt, g, gv, us, cs%kappaShear\_CSp)
371       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc%Kv\_shear)) visc%Kv\_shear(:,:,:) = 0.0 \textcolor{comment}{! needed for other parameterizations}
372       \textcolor{keywordflow}{if} (cs%debug) \textcolor{keywordflow}{then}
373         \textcolor{keyword}{call }hchksum(visc%Kd\_shear, \textcolor{stringliteral}{"after calc\_KS\_vert visc%Kd\_shear"}, g%HI, scale=us%Z2\_T\_to\_m2\_s)
374         \textcolor{keyword}{call }bchksum(visc%Kv\_shear\_Bu, \textcolor{stringliteral}{"after calc\_KS\_vert visc%Kv\_shear\_Bu"}, g%HI, scale=us%Z2\_T\_to\_m2\_s)
375         \textcolor{keyword}{call }bchksum(visc%TKE\_turb, \textcolor{stringliteral}{"after calc\_KS\_vert visc%TKE\_turb"}, g%HI, scale=us%Z\_to\_m**2*us%s\_to\_T*
      *2)
376 \textcolor{keywordflow}{      endif}
377     \textcolor{keywordflow}{else}
378       \textcolor{comment}{! Changes: visc%Kd\_shear ;  Sets: visc%Kv\_shear and visc%TKE\_turb}
379       \textcolor{keyword}{call }calculate\_kappa\_shear(u\_h, v\_h, h, tv, fluxes%p\_surf, visc%Kd\_shear, visc%TKE\_turb, &
380                                  visc%Kv\_shear, dt, g, gv, us, cs%kappaShear\_CSp)
381       \textcolor{keywordflow}{if} (cs%debug) \textcolor{keywordflow}{then}
382         \textcolor{keyword}{call }hchksum(visc%Kd\_shear, \textcolor{stringliteral}{"after calc\_KS visc%Kd\_shear"}, g%HI, scale=us%Z2\_T\_to\_m2\_s)
383         \textcolor{keyword}{call }hchksum(visc%Kv\_shear, \textcolor{stringliteral}{"after calc\_KS visc%Kv\_shear"}, g%HI, scale=us%Z2\_T\_to\_m2\_s)
384         \textcolor{keyword}{call }hchksum(visc%TKE\_turb, \textcolor{stringliteral}{"after calc\_KS visc%TKE\_turb"}, g%HI, scale=us%Z\_to\_m**2*us%s\_to\_T**2)
385 \textcolor{keywordflow}{      endif}
386 \textcolor{keywordflow}{    endif}
387     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_kappashear)
388     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with calculate\_kappa\_shear (set\_diffusivity)"})
389   \textcolor{keywordflow}{elseif} (cs%use\_CVMix\_shear) \textcolor{keywordflow}{then}
390     \textcolor{comment}{!NOTE\{BGR\}: this needs to be cleaned up.  It works in 1D case, but has not been tested outside.}
391     \textcolor{keyword}{call }calculate\_cvmix\_shear(u\_h, v\_h, h, tv, visc%Kd\_shear, visc%Kv\_shear, g, gv, us, cs%CVMix\_shear\_CSp
      )
392     \textcolor{keywordflow}{if} (cs%debug) \textcolor{keywordflow}{then}
393       \textcolor{keyword}{call }hchksum(visc%Kd\_shear, \textcolor{stringliteral}{"after CVMix\_shear visc%Kd\_shear"}, g%HI, scale=us%Z2\_T\_to\_m2\_s)
394       \textcolor{keyword}{call }hchksum(visc%Kv\_shear, \textcolor{stringliteral}{"after CVMix\_shear visc%Kv\_shear"}, g%HI, scale=us%Z2\_T\_to\_m2\_s)
395 \textcolor{keywordflow}{    endif}
396   \textcolor{keywordflow}{elseif} (\textcolor{keyword}{associated}(visc%Kv\_shear)) \textcolor{keywordflow}{then}
397     visc%Kv\_shear(:,:,:) = 0.0 \textcolor{comment}{! needed if calculate\_kappa\_shear is not enabled}
398 \textcolor{keywordflow}{  endif}
399 
400   \textcolor{comment}{! Smooth the properties through massless layers.}
401   \textcolor{keywordflow}{if} (use\_eos) \textcolor{keywordflow}{then}
402     \textcolor{keywordflow}{if} (cs%debug) \textcolor{keywordflow}{then}
403       \textcolor{keyword}{call }hchksum(tv%T, \textcolor{stringliteral}{"before vert\_fill\_TS tv%T"},g%HI)
404       \textcolor{keyword}{call }hchksum(tv%S, \textcolor{stringliteral}{"before vert\_fill\_TS tv%S"},g%HI)
405       \textcolor{keyword}{call }hchksum(h, \textcolor{stringliteral}{"before vert\_fill\_TS h"},g%HI, scale=gv%H\_to\_m)
406 \textcolor{keywordflow}{    endif}
407     \textcolor{keyword}{call }vert\_fill\_ts(h, tv%T, tv%S, kappa\_dt\_fill, t\_f, s\_f, g, gv, larger\_h\_denom=.true.)
408     \textcolor{keywordflow}{if} (cs%debug) \textcolor{keywordflow}{then}
409       \textcolor{keyword}{call }hchksum(tv%T, \textcolor{stringliteral}{"after vert\_fill\_TS tv%T"},g%HI)
410       \textcolor{keyword}{call }hchksum(tv%S, \textcolor{stringliteral}{"after vert\_fill\_TS tv%S"},g%HI)
411       \textcolor{keyword}{call }hchksum(h, \textcolor{stringliteral}{"after vert\_fill\_TS h"},g%HI, scale=gv%H\_to\_m)
412 \textcolor{keywordflow}{    endif}
413 \textcolor{keywordflow}{  endif}
414 
415   \textcolor{comment}{!   Calculate the diffusivities, Kd\_lay and Kd\_int, for each layer and interface.  This would}
416   \textcolor{comment}{! be an appropriate place to add a depth-dependent parameterization or another explicit}
417   \textcolor{comment}{! parameterization of Kd.}
418 
419   \textcolor{comment}{!$OMP parallel do default(shared) private(dRho\_int,N2\_lay,Kd\_lay\_2d,Kd\_int\_2d,Kv\_bkgnd,N2\_int,&}
420   \textcolor{comment}{!$OMP                                     N2\_bot,KT\_extra,KS\_extra,TKE\_to\_Kd,maxTKE,dissip,kb)}
421   \textcolor{keywordflow}{do} j=js,je
422 
423     \textcolor{comment}{! Set up variables related to the stratification.}
424     \textcolor{keyword}{call }find\_n2(h, tv, t\_f, s\_f, fluxes, j, g, gv, us, cs, drho\_int, n2\_lay, n2\_int, n2\_bot)
425 
426     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%N2\_3d)) \textcolor{keywordflow}{then}
427       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie ; dd%N2\_3d(i,j,k) = n2\_int(i,k) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
428 \textcolor{keywordflow}{    endif}
429 
430     \textcolor{comment}{! Add background mixing}
431     \textcolor{keyword}{call }calculate\_bkgnd\_mixing(h, tv, n2\_lay, kd\_lay\_2d, kd\_int\_2d, kv\_bkgnd, j, g, gv, us, cs
      %bkgnd\_mixing\_csp)
432     \textcolor{comment}{! Update Kv and 3-d diffusivity diagnostics.}
433     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc%Kv\_slow)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie
434       visc%Kv\_slow(i,j,k) = visc%Kv\_slow(i,j,k) + kv\_bkgnd(i,k)
435 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
436     \textcolor{keywordflow}{if} (cs%id\_Kv\_bkgnd > 0) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie
437       dd%Kv\_bkgnd(i,j,k) = kv\_bkgnd(i,k)
438 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
439     \textcolor{keywordflow}{if} (cs%id\_Kd\_bkgnd > 0) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie
440       dd%Kd\_bkgnd(i,j,k) = kd\_int\_2d(i,k)
441 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
442 
443     \textcolor{comment}{! Double-diffusion (old method)}
444     \textcolor{keywordflow}{if} (cs%double\_diffusion) \textcolor{keywordflow}{then}
445       \textcolor{keyword}{call }double\_diffusion(tv, h, t\_f, s\_f, j, g, gv, us, cs, kt\_extra, ks\_extra)
446       \textcolor{comment}{! One of Kd\_extra\_T and Kd\_extra\_S is always 0. Kd\_extra\_S is positive for salt fingering.}
447       \textcolor{comment}{! Kd\_extra\_T is positive for double diffusive convection.}
448       \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie
449         \textcolor{keywordflow}{if} (ks\_extra(i,k) > kt\_extra(i,k)) \textcolor{keywordflow}{then} \textcolor{comment}{! salt fingering}
450           kd\_lay\_2d(i,k-1) = kd\_lay\_2d(i,k-1) + 0.5 * kt\_extra(i,k)
451           kd\_lay\_2d(i,k)   = kd\_lay\_2d(i,k)   + 0.5 * kt\_extra(i,k)
452           kd\_extra\_s(i,j,k) = (ks\_extra(i,k) - kt\_extra(i,k))
453           kd\_extra\_t(i,j,k) = 0.0
454         \textcolor{keywordflow}{elseif} (kt\_extra(i,k) > 0.0) \textcolor{keywordflow}{then} \textcolor{comment}{! double-diffusive convection}
455           kd\_lay\_2d(i,k-1) = kd\_lay\_2d(i,k-1) + 0.5 * ks\_extra(i,k)
456           kd\_lay\_2d(i,k)   = kd\_lay\_2d(i,k)   + 0.5 * ks\_extra(i,k)
457           kd\_extra\_t(i,j,k) = (kt\_extra(i,k) - ks\_extra(i,k))
458           kd\_extra\_s(i,j,k) = 0.0
459         \textcolor{keywordflow}{else} \textcolor{comment}{! There is no double diffusion at this interface.}
460           kd\_extra\_t(i,j,k) = 0.0
461           kd\_extra\_s(i,j,k) = 0.0
462 \textcolor{keywordflow}{        endif}
463 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
464       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%KT\_extra)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie
465         dd%KT\_extra(i,j,k) = kt\_extra(i,k)
466 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
467 
468       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%KS\_extra)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie
469         dd%KS\_extra(i,j,k) = ks\_extra(i,k)
470 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
471 \textcolor{keywordflow}{    endif}
472 
473     \textcolor{comment}{! Apply double diffusion via CVMix}
474     \textcolor{comment}{! GMM, we need to pass HBL to compute\_ddiff\_coeffs, but it is not yet available.}
475     \textcolor{keywordflow}{if} (cs%use\_CVMix\_ddiff) \textcolor{keywordflow}{then}
476       \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_cvmix\_ddiff)
477       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%drho\_rat)) \textcolor{keywordflow}{then}
478         \textcolor{keyword}{call }compute\_ddiff\_coeffs(h, tv, g, gv, us, j, kd\_extra\_t, kd\_extra\_s, &
479                                   cs%CVMix\_ddiff\_csp, dd%drho\_rat)
480       \textcolor{keywordflow}{else}
481         \textcolor{keyword}{call }compute\_ddiff\_coeffs(h, tv, g, gv, us, j, kd\_extra\_t, kd\_extra\_s, cs%CVMix\_ddiff\_csp)
482 \textcolor{keywordflow}{      endif}
483       \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_cvmix\_ddiff)
484 \textcolor{keywordflow}{    endif}
485 
486     \textcolor{comment}{! Calculate conversion ratios from TKE to layer diffusivities.}
487     \textcolor{keywordflow}{if} (tke\_to\_kd\_used) \textcolor{keywordflow}{then}
488       \textcolor{keyword}{call }find\_tke\_to\_kd(h, tv, drho\_int, n2\_lay, j, dt, g, gv, us, cs, tke\_to\_kd, maxtke, kb)
489       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%maxTKE)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie
490         dd%maxTKE(i,j,k) = maxtke(i,k)
491 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
492       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%TKE\_to\_Kd)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie
493         dd%TKE\_to\_Kd(i,j,k) = tke\_to\_kd(i,k)
494 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
495 \textcolor{keywordflow}{    endif}
496 
497     \textcolor{comment}{! Add the input turbulent diffusivity.}
498     \textcolor{keywordflow}{if} (cs%useKappaShear .or. cs%use\_CVMix\_shear) \textcolor{keywordflow}{then}
499       \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_int)) \textcolor{keywordflow}{then}
500         \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie
501           kd\_int\_2d(i,k) = visc%Kd\_shear(i,j,k) + 0.5 * (kd\_lay\_2d(i,k-1) + kd\_lay\_2d(i,k))
502 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}
503         \textcolor{keywordflow}{do} i=is,ie
504           kd\_int\_2d(i,1) = visc%Kd\_shear(i,j,1) \textcolor{comment}{! This isn't actually used. It could be 0.}
505           kd\_int\_2d(i,nz+1) = 0.0
506 \textcolor{keywordflow}{        enddo}
507 \textcolor{keywordflow}{      endif}
508       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie
509         kd\_lay\_2d(i,k) = kd\_lay\_2d(i,k) + 0.5 * (visc%Kd\_shear(i,j,k) + visc%Kd\_shear(i,j,k+1))
510 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
511     \textcolor{keywordflow}{else}
512       \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_int)) \textcolor{keywordflow}{then}
513         \textcolor{keywordflow}{do} i=is,ie
514           kd\_int\_2d(i,1) = kd\_lay\_2d(i,1) ; kd\_int\_2d(i,nz+1) = 0.0
515 \textcolor{keywordflow}{        enddo}
516         \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie
517           kd\_int\_2d(i,k) = 0.5 * (kd\_lay\_2d(i,k-1) + kd\_lay\_2d(i,k))
518 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}
519 \textcolor{keywordflow}{      endif}
520 \textcolor{keywordflow}{    endif}
521 
522     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_int)) \textcolor{keywordflow}{then}
523       \textcolor{comment}{! Add the ML\_Rad diffusivity.}
524       \textcolor{keywordflow}{if} (cs%ML\_radiation) &
525         \textcolor{keyword}{call }add\_mlrad\_diffusivity(h, fluxes, j, g, gv, us, cs, tke\_to\_kd, kd\_lay\_2d, kd\_int\_2d)
526 
527       \textcolor{comment}{! Add the Nikurashin and / or tidal bottom-driven mixing}
528       \textcolor{keywordflow}{if} (cs%use\_tidal\_mixing) &
529         \textcolor{keyword}{call }calculate\_tidal\_mixing(h, n2\_bot, j, tke\_to\_kd, maxtke, g, gv, us, cs%tidal\_mixing\_CSp, &
530                                     n2\_lay, n2\_int, kd\_lay\_2d, kd\_int\_2d, cs%Kd\_max, visc%Kv\_slow)
531 
532       \textcolor{comment}{! This adds the diffusion sustained by the energy extracted from the flow by the bottom drag.}
533       \textcolor{keywordflow}{if} (cs%bottomdraglaw .and. (cs%BBL\_effic>0.0)) \textcolor{keywordflow}{then}
534         \textcolor{keywordflow}{if} (cs%use\_LOTW\_BBL\_diffusivity) \textcolor{keywordflow}{then}
535           \textcolor{keyword}{call }add\_lotw\_bbl\_diffusivity(h, u, v, tv, fluxes, visc, j, n2\_int, g, gv, us, cs,  &
536                                         dd%Kd\_BBL, kd\_lay\_2d, kd\_int\_2d)
537         \textcolor{keywordflow}{else}
538           \textcolor{keyword}{call }add\_drag\_diffusivity(h, u, v,  tv, fluxes, visc, j, tke\_to\_kd, &
539                                     maxtke, kb, g, gv, us, cs, kd\_lay\_2d, kd\_int\_2d, dd%Kd\_BBL)
540 \textcolor{keywordflow}{        endif}
541 \textcolor{keywordflow}{      endif}
542 
543       \textcolor{keywordflow}{if} (cs%limit\_dissipation) \textcolor{keywordflow}{then}
544         \textcolor{comment}{! This calculates the dissipation ONLY from Kd calculated in this routine}
545         \textcolor{comment}{! dissip has units of W/m3 (= kg/m3 * m2/s * 1/s2)}
546         \textcolor{comment}{!   1) a global constant,}
547         \textcolor{comment}{!   2) a dissipation proportional to N (aka Gargett) and}
548         \textcolor{comment}{!   3) dissipation corresponding to a (nearly) constant diffusivity.}
549         \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie
550           dissip = max( cs%dissip\_min, &   \textcolor{comment}{! Const. floor on dissip.}
551                         cs%dissip\_N0 + cs%dissip\_N1 * sqrt(n2\_int(i,k)), & \textcolor{comment}{! Floor aka Gargett}
552                         cs%dissip\_N2 * n2\_int(i,k)) \textcolor{comment}{! Floor of Kd\_min*rho0/F\_Ri}
553           kd\_int\_2d(i,k) = max(kd\_int\_2d(i,k) , &  \textcolor{comment}{! Apply floor to Kd}
554                               dissip * (cs%FluxRi\_max / (gv%Rho0 * (n2\_int(i,k) + omega2))))
555 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}
556 \textcolor{keywordflow}{      endif}
557 
558       \textcolor{comment}{! Optionally add a uniform diffusivity at the interfaces.}
559       \textcolor{keywordflow}{if} (cs%Kd\_add > 0.0) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie
560         kd\_int\_2d(i,k) = kd\_int\_2d(i,k) + cs%Kd\_add
561 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
562 
563       \textcolor{comment}{! Copy the 2-d slices into the 3-d array that is exported.}
564       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie
565         kd\_int(i,j,k) = kd\_int\_2d(i,k)
566 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
567 
568     \textcolor{keywordflow}{else} \textcolor{comment}{! Kd\_int is not present.}
569 
570       \textcolor{comment}{! Add the ML\_Rad diffusivity.}
571       \textcolor{keywordflow}{if} (cs%ML\_radiation) &
572         \textcolor{keyword}{call }add\_mlrad\_diffusivity(h, fluxes, j, g, gv, us, cs, tke\_to\_kd, kd\_lay\_2d)
573 
574       \textcolor{comment}{! Add the Nikurashin and / or tidal bottom-driven mixing}
575       \textcolor{keywordflow}{if} (cs%use\_tidal\_mixing) &
576         \textcolor{keyword}{call }calculate\_tidal\_mixing(h, n2\_bot, j, tke\_to\_kd, maxtke, g, gv, us, cs%tidal\_mixing\_CSp, &
577                                     n2\_lay, n2\_int, kd\_lay\_2d, kd\_max=cs%Kd\_max, kv=visc%Kv\_slow)
578 
579       \textcolor{comment}{! This adds the diffusion sustained by the energy extracted from the flow by the bottom drag.}
580       \textcolor{keywordflow}{if} (cs%bottomdraglaw .and. (cs%BBL\_effic>0.0)) \textcolor{keywordflow}{then}
581         \textcolor{keywordflow}{if} (cs%use\_LOTW\_BBL\_diffusivity) \textcolor{keywordflow}{then}
582           \textcolor{keyword}{call }add\_lotw\_bbl\_diffusivity(h, u, v, tv, fluxes, visc, j, n2\_int, g, gv, us, cs,  &
583                                         dd%Kd\_BBL, kd\_lay\_2d)
584         \textcolor{keywordflow}{else}
585           \textcolor{keyword}{call }add\_drag\_diffusivity(h, u, v,  tv, fluxes, visc, j, tke\_to\_kd, &
586                                     maxtke, kb, g, gv, us, cs, kd\_lay\_2d, kd\_bbl=dd%Kd\_BBL)
587 \textcolor{keywordflow}{        endif}
588 \textcolor{keywordflow}{      endif}
589 
590 \textcolor{keywordflow}{    endif}
591 
592     \textcolor{keywordflow}{if} (cs%limit\_dissipation) \textcolor{keywordflow}{then}
593       \textcolor{comment}{! This calculates the layer dissipation ONLY from Kd calculated in this routine}
594       \textcolor{comment}{! dissip has units of W/m3 (= kg/m3 * m2/s * 1/s2)}
595       \textcolor{comment}{!   1) a global constant,}
596       \textcolor{comment}{!   2) a dissipation proportional to N (aka Gargett) and}
597       \textcolor{comment}{!   3) dissipation corresponding to a (nearly) constant diffusivity.}
598       \textcolor{keywordflow}{do} k=2,nz-1 ; \textcolor{keywordflow}{do} i=is,ie
599         dissip = max( cs%dissip\_min, &   \textcolor{comment}{! Const. floor on dissip.}
600                       cs%dissip\_N0 + cs%dissip\_N1 * sqrt(n2\_lay(i,k)), & \textcolor{comment}{! Floor aka Gargett}
601                       cs%dissip\_N2 * n2\_lay(i,k)) \textcolor{comment}{! Floor of Kd\_min*rho0/F\_Ri}
602         kd\_lay\_2d(i,k) = max(kd\_lay\_2d(i,k) , &  \textcolor{comment}{! Apply floor to Kd}
603                             dissip * (cs%FluxRi\_max / (gv%Rho0 * (n2\_lay(i,k) + omega2))))
604 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
605 \textcolor{keywordflow}{    endif}
606 
607     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%Kd\_work)) \textcolor{keywordflow}{then}
608       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie
609         dd%Kd\_Work(i,j,k) = gv%Rho0 * kd\_lay\_2d(i,k) * n2\_lay(i,k) * &
610                             gv%H\_to\_Z*h(i,j,k)  \textcolor{comment}{! Watt m-2 s = kg s-3}
611 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
612 \textcolor{keywordflow}{    endif}
613 
614     \textcolor{comment}{! Optionally add a uniform diffusivity to the layers.}
615     \textcolor{keywordflow}{if} ((cs%Kd\_add > 0.0) .and. (\textcolor{keyword}{present}(kd\_lay))) \textcolor{keywordflow}{then}
616       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie
617         kd\_lay\_2d(i,k) = kd\_lay\_2d(i,k) + cs%Kd\_add
618 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
619 \textcolor{keywordflow}{    endif}
620 
621     \textcolor{comment}{! Copy the 2-d slices into the 3-d array that is exported; this was done above for Kd\_int.}
622     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_lay)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie
623       kd\_lay(i,j,k) = kd\_lay\_2d(i,k)
624 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}
625 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! j-loop}
626 
627   \textcolor{keywordflow}{if} (cs%user\_change\_diff) \textcolor{keywordflow}{then}
628     \textcolor{keyword}{call }user\_change\_diff(h, tv, g, gv, us, cs%user\_change\_diff\_CSp, kd\_lay, kd\_int, &
629                           t\_f, s\_f, dd%Kd\_user)
630 \textcolor{keywordflow}{  endif}
631 
632   \textcolor{keywordflow}{if} (cs%debug) \textcolor{keywordflow}{then}
633     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_lay)) \textcolor{keyword}{call }hchksum(kd\_lay, \textcolor{stringliteral}{"Kd\_lay"}, g%HI, haloshift=0, scale=us%Z2\_T\_to\_m2\_s)
634 
635     \textcolor{keywordflow}{if} (cs%useKappaShear) \textcolor{keyword}{call }hchksum(visc%Kd\_shear, \textcolor{stringliteral}{"Turbulent Kd"}, g%HI, haloshift=0, scale=us
      %Z2\_T\_to\_m2\_s)
636 
637     \textcolor{keywordflow}{if} (cs%use\_CVMix\_ddiff) \textcolor{keywordflow}{then}
638       \textcolor{keyword}{call }hchksum(kd\_extra\_t, \textcolor{stringliteral}{"MOM\_set\_diffusivity: Kd\_extra\_T"}, g%HI, haloshift=0, scale=us%Z2\_T\_to\_m2\_s)
639       \textcolor{keyword}{call }hchksum(kd\_extra\_s, \textcolor{stringliteral}{"MOM\_set\_diffusivity: Kd\_extra\_S"}, g%HI, haloshift=0, scale=us%Z2\_T\_to\_m2\_s)
640 \textcolor{keywordflow}{    endif}
641 
642     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc%kv\_bbl\_u) .and. \textcolor{keyword}{associated}(visc%kv\_bbl\_v)) \textcolor{keywordflow}{then}
643       \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"BBL Kv\_bbl\_[uv]"}, visc%kv\_bbl\_u, visc%kv\_bbl\_v, g%HI, &
644                     haloshift=0, symmetric=.true., scale=us%Z2\_T\_to\_m2\_s, &
645                     scalar\_pair=.true.)
646 \textcolor{keywordflow}{    endif}
647 
648     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc%bbl\_thick\_u) .and. \textcolor{keyword}{associated}(visc%bbl\_thick\_v)) \textcolor{keywordflow}{then}
649       \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"BBL bbl\_thick\_[uv]"}, visc%bbl\_thick\_u, visc%bbl\_thick\_v, &
650                     g%HI, haloshift=0, symmetric=.true., scale=us%Z\_to\_m, &
651                     scalar\_pair=.true.)
652 \textcolor{keywordflow}{    endif}
653 
654     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc%Ray\_u) .and. \textcolor{keyword}{associated}(visc%Ray\_v)) \textcolor{keywordflow}{then}
655       \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"Ray\_[uv]"}, visc%Ray\_u, visc%Ray\_v, g%HI, 0, symmetric=.true., scale=us%Z\_to\_m*us
      %s\_to\_T)
656 \textcolor{keywordflow}{    endif}
657 
658 \textcolor{keywordflow}{  endif}
659 
660   \textcolor{comment}{! post diagnostics}
661   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_lay) .and. (cs%id\_Kd\_layer > 0)) \textcolor{keyword}{call }post\_data(cs%id\_Kd\_layer, kd\_lay, cs%diag)
662 
663   \textcolor{comment}{! background mixing}
664   \textcolor{keywordflow}{if} (cs%id\_Kd\_bkgnd > 0) \textcolor{keyword}{call }post\_data(cs%id\_Kd\_bkgnd, dd%Kd\_bkgnd, cs%diag)
665   \textcolor{keywordflow}{if} (cs%id\_Kv\_bkgnd > 0) \textcolor{keyword}{call }post\_data(cs%id\_Kv\_bkgnd, dd%Kv\_bkgnd, cs%diag)
666 
667   \textcolor{comment}{! tidal mixing}
668   \textcolor{keyword}{call }post\_tidal\_diagnostics(g, gv, h, cs%tidal\_mixing\_CSp)
669   \textcolor{keywordflow}{if} (cs%id\_N2 > 0)         \textcolor{keyword}{call }post\_data(cs%id\_N2,        dd%N2\_3d,     cs%diag)
670   \textcolor{keywordflow}{if} (cs%id\_Kd\_Work > 0)    \textcolor{keyword}{call }post\_data(cs%id\_Kd\_Work,   dd%Kd\_Work,   cs%diag)
671   \textcolor{keywordflow}{if} (cs%id\_maxTKE > 0)     \textcolor{keyword}{call }post\_data(cs%id\_maxTKE,    dd%maxTKE,    cs%diag)
672   \textcolor{keywordflow}{if} (cs%id\_TKE\_to\_Kd > 0)  \textcolor{keyword}{call }post\_data(cs%id\_TKE\_to\_Kd, dd%TKE\_to\_Kd, cs%diag)
673 
674   \textcolor{keywordflow}{if} (cs%id\_Kd\_user > 0)    \textcolor{keyword}{call }post\_data(cs%id\_Kd\_user,   dd%Kd\_user,   cs%diag)
675 
676   \textcolor{comment}{! double diffusive mixing}
677   \textcolor{keywordflow}{if} (cs%double\_diffusion) \textcolor{keywordflow}{then}
678     \textcolor{keywordflow}{if} (cs%id\_KT\_extra > 0) \textcolor{keyword}{call }post\_data(cs%id\_KT\_extra, dd%KT\_extra, cs%diag)
679     \textcolor{keywordflow}{if} (cs%id\_KS\_extra > 0) \textcolor{keyword}{call }post\_data(cs%id\_KS\_extra, dd%KS\_extra, cs%diag)
680   \textcolor{keywordflow}{elseif} (cs%use\_CVMix\_ddiff) \textcolor{keywordflow}{then}
681     \textcolor{keywordflow}{if} (cs%id\_KT\_extra > 0) \textcolor{keyword}{call }post\_data(cs%id\_KT\_extra, kd\_extra\_t, cs%diag)
682     \textcolor{keywordflow}{if} (cs%id\_KS\_extra > 0) \textcolor{keyword}{call }post\_data(cs%id\_KS\_extra, kd\_extra\_s, cs%diag)
683     \textcolor{keywordflow}{if} (cs%id\_R\_rho > 0) \textcolor{keyword}{call }post\_data(cs%id\_R\_rho, dd%drho\_rat, cs%diag)
684 \textcolor{keywordflow}{  endif}
685   \textcolor{keywordflow}{if} (cs%id\_Kd\_BBL > 0)   \textcolor{keyword}{call }post\_data(cs%id\_Kd\_BBL, dd%Kd\_BBL, cs%diag)
686 
687   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%N2\_3d)) \textcolor{keyword}{deallocate}(dd%N2\_3d)
688   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%Kd\_work)) \textcolor{keyword}{deallocate}(dd%Kd\_work)
689   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%Kd\_user)) \textcolor{keyword}{deallocate}(dd%Kd\_user)
690   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%maxTKE)) \textcolor{keyword}{deallocate}(dd%maxTKE)
691   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%TKE\_to\_Kd)) \textcolor{keyword}{deallocate}(dd%TKE\_to\_Kd)
692   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%KT\_extra)) \textcolor{keyword}{deallocate}(dd%KT\_extra)
693   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%KS\_extra)) \textcolor{keyword}{deallocate}(dd%KS\_extra)
694   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%drho\_rat)) \textcolor{keyword}{deallocate}(dd%drho\_rat)
695   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(dd%Kd\_BBL)) \textcolor{keyword}{deallocate}(dd%Kd\_BBL)
696 
697   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_leave(\textcolor{stringliteral}{"set\_diffusivity()"})
698 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__diffusivity_ace82f133d3cee42aa36ec10bcce79e75}\label{namespacemom__set__diffusivity_ace82f133d3cee42aa36ec10bcce79e75}} 
\index{mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}!set\+\_\+diffusivity\+\_\+end@{set\+\_\+diffusivity\+\_\+end}}
\index{set\+\_\+diffusivity\+\_\+end@{set\+\_\+diffusivity\+\_\+end}!mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}}
\subsubsection{\texorpdfstring{set\+\_\+diffusivity\+\_\+end()}{set\_diffusivity\_end()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+set\+\_\+diffusivity\+::set\+\_\+diffusivity\+\_\+end (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__set__diffusivity_1_1set__diffusivity__cs}{set\+\_\+diffusivity\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Clear pointers and dealocate memory. 


\begin{DoxyParams}{Parameters}
{\em cs} & Control structure for this module \\
\hline
\end{DoxyParams}


Definition at line 2347 of file M\+O\+M\+\_\+set\+\_\+diffusivity.\+F90.


\begin{DoxyCode}
2347   \textcolor{keywordtype}{type}(set\_diffusivity\_CS), \textcolor{keywordtype}{pointer} :: CS\textcolor{comment}{ !< Control structure for this module}
2348 
2349   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{return}
2350 
2351   \textcolor{keyword}{call }bkgnd\_mixing\_end(cs%bkgnd\_mixing\_csp)
2352 
2353   \textcolor{keywordflow}{if} (cs%use\_tidal\_mixing) \textcolor{keyword}{call }tidal\_mixing\_end(cs%tidal\_mixing\_CSp)
2354 
2355   \textcolor{keywordflow}{if} (cs%user\_change\_diff) \textcolor{keyword}{call }user\_change\_diff\_end(cs%user\_change\_diff\_CSp)
2356 
2357   \textcolor{keywordflow}{if} (cs%use\_CVMix\_shear)  \textcolor{keyword}{call }cvmix\_shear\_end(cs%CVMix\_shear\_csp)
2358 
2359   \textcolor{keywordflow}{if} (cs%use\_CVMix\_ddiff)  \textcolor{keyword}{call }cvmix\_ddiff\_end(cs%CVMix\_ddiff\_csp)
2360 
2361   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{deallocate}(cs)
2362 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__set__diffusivity_ac0c0f4b8458cb5610514ee068482f4bb}\label{namespacemom__set__diffusivity_ac0c0f4b8458cb5610514ee068482f4bb}} 
\index{mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}!set\+\_\+diffusivity\+\_\+init@{set\+\_\+diffusivity\+\_\+init}}
\index{set\+\_\+diffusivity\+\_\+init@{set\+\_\+diffusivity\+\_\+init}!mom\+\_\+set\+\_\+diffusivity@{mom\+\_\+set\+\_\+diffusivity}}
\subsubsection{\texorpdfstring{set\+\_\+diffusivity\+\_\+init()}{set\_diffusivity\_init()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+set\+\_\+diffusivity\+::set\+\_\+diffusivity\+\_\+init (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), intent(in)}]{Time,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(inout)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file,  }\item[{type(diag\+\_\+ctrl), intent(inout), target}]{diag,  }\item[{type(\mbox{\hyperlink{structmom__set__diffusivity_1_1set__diffusivity__cs}{set\+\_\+diffusivity\+\_\+cs}}), pointer}]{CS,  }\item[{type(int\+\_\+tide\+\_\+cs), pointer}]{int\+\_\+tide\+\_\+\+C\+Sp,  }\item[{integer, intent(out), optional}]{halo\+\_\+\+TS,  }\item[{logical, intent(out), optional}]{double\+\_\+diffuse }\end{DoxyParamCaption})}


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em time} & The current model time\\
\hline
\mbox{\tt in,out}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em param\+\_\+file} & A structure to parse for run-\/time parameters.\\
\hline
\mbox{\tt in,out}  & {\em diag} & A structure used to regulate diagnostic output.\\
\hline
 & {\em cs} & pointer set to point to the module control structure.\\
\hline
 & {\em int\+\_\+tide\+\_\+csp} & A pointer to the internal tides control structure\\
\hline
\mbox{\tt out}  & {\em halo\+\_\+ts} & The halo size of tracer points that must be valid for the calculations in set\+\_\+diffusivity.\\
\hline
\mbox{\tt out}  & {\em double\+\_\+diffuse} & If present, this indicates whether some version of double diffusion is being used. \\
\hline
\end{DoxyParams}


Definition at line 2011 of file M\+O\+M\+\_\+set\+\_\+diffusivity.\+F90.


\begin{DoxyCode}
2011   \textcolor{keywordtype}{type}(time\_type),          \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{ !< The current model time}
2012   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{    !< The ocean's grid structure.}
2013   \textcolor{keywordtype}{type}(verticalGrid\_type),  \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}
2014   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}
2015   \textcolor{keywordtype}{type}(param\_file\_type),    \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< A structure to parse for run-time}
2016 \textcolor{comment}{                                                  !! parameters.}
2017   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target},  \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{ !< A structure used to regulate diagnostic output.}
2018   \textcolor{keywordtype}{type}(set\_diffusivity\_CS), \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< pointer set to point to the module control}
2019 \textcolor{comment}{                                                  !! structure.}
2020   \textcolor{keywordtype}{type}(int\_tide\_CS),        \textcolor{keywordtype}{pointer}       :: int\_tide\_CSp\textcolor{comment}{ !< A pointer to the internal tides control}
2021 \textcolor{comment}{                                                  !! structure}
2022   \textcolor{keywordtype}{integer},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)}   :: halo\_TS\textcolor{comment}{ !< The halo size of tracer points that must be}
2023 \textcolor{comment}{                                                  !! valid for the calculations in set\_diffusivity.}
2024   \textcolor{keywordtype}{logical},        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)}   :: double\_diffuse\textcolor{comment}{ !< If present, this indicates whether}
2025 \textcolor{comment}{                                                  !! some version of double diffusion is being used.}
2026 
2027   \textcolor{comment}{! Local variables}
2028   \textcolor{keywordtype}{real} :: decay\_length
2029   \textcolor{keywordtype}{logical} :: ML\_use\_omega
2030   \textcolor{keywordtype}{logical} :: default\_2018\_answers
2031   \textcolor{comment}{! This include declares and sets the variable "version".}
2032 \textcolor{preprocessor}{# include "version\_variable.h"}
2033 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_set\_diffusivity"}  \textcolor{comment}{! This module's name.}
2034   \textcolor{keywordtype}{real} :: omega\_frac\_dflt
2035   \textcolor{keywordtype}{logical} :: Bryan\_Lewis\_diffusivity \textcolor{comment}{! If true, the background diapycnal diffusivity uses}
2036                                      \textcolor{comment}{! the Bryan-Lewis (1979) style tanh profile.}
2037   \textcolor{keywordtype}{integer} :: i, j, is, ie, js, je
2038   \textcolor{keywordtype}{integer} :: isd, ied, jsd, jed
2039 
2040   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}
2041     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"diabatic\_entrain\_init called with an associated "}// &
2042                             \textcolor{stringliteral}{"control structure."})
2043     \textcolor{keywordflow}{return}
2044 \textcolor{keywordflow}{  endif}
2045   \textcolor{keyword}{allocate}(cs)
2046 
2047   is  = g%isc ; ie  = g%iec ; js  = g%jsc ; je  = g%jec
2048   isd = g%isd ; ied = g%ied ; jsd = g%jsd ; jed = g%jed
2049 
2050   cs%diag => diag
2051   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(int\_tide\_csp))  cs%int\_tide\_CSp  => int\_tide\_csp
2052 
2053   \textcolor{comment}{! These default values always need to be set.}
2054   cs%BBL\_mixing\_as\_max = .true.
2055   cs%cdrag = 0.003 ; cs%BBL\_effic = 0.0
2056   cs%bulkmixedlayer = (gv%nkml > 0)
2057 
2058   \textcolor{comment}{! Read all relevant parameters and write them to the model log.}
2059   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \textcolor{stringliteral}{""})
2060 
2061   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"INPUTDIR"}, cs%inputdir, default=\textcolor{stringliteral}{"."})
2062   cs%inputdir = slasher(cs%inputdir)
2063   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"FLUX\_RI\_MAX"}, cs%FluxRi\_max, &
2064                  \textcolor{stringliteral}{"The flux Richardson number where the stratification is "}//&
2065                  \textcolor{stringliteral}{"large enough that N2 > omega2.  The full expression for "}//&
2066                  \textcolor{stringliteral}{"the Flux Richardson number is usually "}//&
2067                  \textcolor{stringliteral}{"FLUX\_RI\_MAX*N2/(N2+OMEGA2)."}, units=\textcolor{stringliteral}{"nondim"}, default=0.2)
2068   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"OMEGA"}, cs%omega, &
2069                  \textcolor{stringliteral}{"The rotation rate of the earth."}, units=\textcolor{stringliteral}{"s-1"}, default=7.2921e-5, scale=us%T\_to\_s)
2070 
2071   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEFAULT\_2018\_ANSWERS"}, default\_2018\_answers, &
2072                  \textcolor{stringliteral}{"This sets the default value for the various \_2018\_ANSWERS parameters."}, &
2073                  default=.false.)
2074   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"SET\_DIFF\_2018\_ANSWERS"}, cs%answers\_2018, &
2075                  \textcolor{stringliteral}{"If true, use the order of arithmetic and expressions that recover the "}//&
2076                  \textcolor{stringliteral}{"answers from the end of 2018.  Otherwise, use updated and more robust "}//&
2077                  \textcolor{stringliteral}{"forms of the same expressions."}, default=default\_2018\_answers)
2078 
2079   \textcolor{comment}{! CS%use\_tidal\_mixing is set to True if an internal tidal dissipation scheme is to be used.}
2080   cs%use\_tidal\_mixing = tidal\_mixing\_init(time, g, gv, us, param\_file, diag, &
2081                                           cs%tidal\_mixing\_CSp)
2082 
2083   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_RADIATION"}, cs%ML\_radiation, &
2084                  \textcolor{stringliteral}{"If true, allow a fraction of TKE available from wind "}//&
2085                  \textcolor{stringliteral}{"work to penetrate below the base of the mixed layer "}//&
2086                  \textcolor{stringliteral}{"with a vertical decay scale determined by the minimum "}//&
2087                  \textcolor{stringliteral}{"of: (1) The depth of the mixed layer, (2) an Ekman "}//&
2088                  \textcolor{stringliteral}{"length scale."}, default=.false.)
2089   \textcolor{keywordflow}{if} (cs%ML\_radiation) \textcolor{keywordflow}{then}
2090     \textcolor{comment}{! This give a minimum decay scale that is typically much less than Angstrom.}
2091     cs%ustar\_min = 2e-4 * cs%omega * (gv%Angstrom\_Z + gv%H\_subroundoff*gv%H\_to\_Z)
2092 
2093     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_RAD\_EFOLD\_COEFF"}, cs%ML\_rad\_efold\_coeff, &
2094                  \textcolor{stringliteral}{"A coefficient that is used to scale the penetration "}//&
2095                  \textcolor{stringliteral}{"depth for turbulence below the base of the mixed layer. "}//&
2096                  \textcolor{stringliteral}{"This is only used if ML\_RADIATION is true."}, units=\textcolor{stringliteral}{"nondim"}, default=0.2)
2097     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_RAD\_BUG"}, cs%ML\_rad\_bug, &
2098                  \textcolor{stringliteral}{"If true use code with a bug that reduces the energy available "}//&
2099                  \textcolor{stringliteral}{"in the transition layer by a factor of the inverse of the energy "}//&
2100                  \textcolor{stringliteral}{"deposition lenthscale (in m)."}, default=.false.)
2101     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_RAD\_KD\_MAX"}, cs%ML\_rad\_kd\_max, &
2102                  \textcolor{stringliteral}{"The maximum diapycnal diffusivity due to turbulence "}//&
2103                  \textcolor{stringliteral}{"radiated from the base of the mixed layer. "}//&
2104                  \textcolor{stringliteral}{"This is only used if ML\_RADIATION is true."}, &
2105                  units=\textcolor{stringliteral}{"m2 s-1"}, default=1.0e-3, scale=us%m2\_s\_to\_Z2\_T)
2106     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_RAD\_COEFF"}, cs%ML\_rad\_coeff, &
2107                  \textcolor{stringliteral}{"The coefficient which scales MSTAR*USTAR^3 to obtain "}//&
2108                  \textcolor{stringliteral}{"the energy available for mixing below the base of the "}//&
2109                  \textcolor{stringliteral}{"mixed layer. This is only used if ML\_RADIATION is true."}, &
2110                  units=\textcolor{stringliteral}{"nondim"}, default=0.2)
2111     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_RAD\_APPLY\_TKE\_DECAY"}, cs%ML\_rad\_TKE\_decay, &
2112                  \textcolor{stringliteral}{"If true, apply the same exponential decay to ML\_rad as "}//&
2113                  \textcolor{stringliteral}{"is applied to the other surface sources of TKE in the "}//&
2114                  \textcolor{stringliteral}{"mixed layer code. This is only used if ML\_RADIATION is true."}, default=.true.)
2115     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MSTAR"}, cs%mstar, &
2116                  \textcolor{stringliteral}{"The ratio of the friction velocity cubed to the TKE "}//&
2117                  \textcolor{stringliteral}{"input to the mixed layer."}, units=\textcolor{stringliteral}{"nondim"}, default=1.2)
2118     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"TKE\_DECAY"}, cs%TKE\_decay, &
2119                  \textcolor{stringliteral}{"The ratio of the natural Ekman depth to the TKE decay scale."}, &
2120                  units=\textcolor{stringliteral}{"nondim"}, default=2.5)
2121     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_USE\_OMEGA"}, ml\_use\_omega, &
2122                  \textcolor{stringliteral}{"If true, use the absolute rotation rate instead of the "}//&
2123                  \textcolor{stringliteral}{"vertical component of rotation when setting the decay "}//&
2124                  \textcolor{stringliteral}{"scale for turbulence."}, default=.false., do\_not\_log=.true.)
2125     omega\_frac\_dflt = 0.0
2126     \textcolor{keywordflow}{if} (ml\_use\_omega) \textcolor{keywordflow}{then}
2127       \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"ML\_USE\_OMEGA is depricated; use ML\_OMEGA\_FRAC=1.0 instead."})
2128       omega\_frac\_dflt = 1.0
2129 \textcolor{keywordflow}{    endif}
2130     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_OMEGA\_FRAC"}, cs%ML\_omega\_frac, &
2131                    \textcolor{stringliteral}{"When setting the decay scale for turbulence, use this "}//&
2132                    \textcolor{stringliteral}{"fraction of the absolute rotation rate blended with the "}//&
2133                    \textcolor{stringliteral}{"local value of f, as sqrt((1-of)*f^2 + of*4*omega^2)."}, &
2134                    units=\textcolor{stringliteral}{"nondim"}, default=omega\_frac\_dflt)
2135 \textcolor{keywordflow}{  endif}
2136 
2137   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BOTTOMDRAGLAW"}, cs%bottomdraglaw, &
2138                  \textcolor{stringliteral}{"If true, the bottom stress is calculated with a drag "}//&
2139                  \textcolor{stringliteral}{"law of the form c\_drag*|u|*u. The velocity magnitude "}//&
2140                  \textcolor{stringliteral}{"may be an assumed value or it may be based on the actual "}//&
2141                  \textcolor{stringliteral}{"velocity in the bottommost HBBL, depending on LINEAR\_DRAG."}, default=.true.)
2142   \textcolor{keywordflow}{if}  (cs%bottomdraglaw) \textcolor{keywordflow}{then}
2143     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CDRAG"}, cs%cdrag, &
2144                  \textcolor{stringliteral}{"The drag coefficient relating the magnitude of the "}//&
2145                  \textcolor{stringliteral}{"velocity field to the bottom stress. CDRAG is only used "}//&
2146                  \textcolor{stringliteral}{"if BOTTOMDRAGLAW is true."}, units=\textcolor{stringliteral}{"nondim"}, default=0.003)
2147     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BBL\_EFFIC"}, cs%BBL\_effic, &
2148                  \textcolor{stringliteral}{"The efficiency with which the energy extracted by "}//&
2149                  \textcolor{stringliteral}{"bottom drag drives BBL diffusion.  This is only "}//&
2150                  \textcolor{stringliteral}{"used if BOTTOMDRAGLAW is true."}, units=\textcolor{stringliteral}{"nondim"}, default=0.20)
2151     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BBL\_MIXING\_MAX\_DECAY"}, decay\_length, &
2152                  \textcolor{stringliteral}{"The maximum decay scale for the BBL diffusion, or 0 to allow the mixing "}//&
2153                  \textcolor{stringliteral}{"to penetrate as far as stratification and rotation permit.  The default "}//&
2154                  \textcolor{stringliteral}{"for now is 200 m. This is only used if BOTTOMDRAGLAW is true."}, &
2155                  units=\textcolor{stringliteral}{"m"}, default=200.0, scale=us%m\_to\_Z)
2156 
2157     cs%IMax\_decay = 0.0
2158     \textcolor{keywordflow}{if} (decay\_length > 0.0) cs%IMax\_decay = 1.0/decay\_length
2159     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BBL\_MIXING\_AS\_MAX"}, cs%BBL\_mixing\_as\_max, &
2160                  \textcolor{stringliteral}{"If true, take the maximum of the diffusivity from the "}//&
2161                  \textcolor{stringliteral}{"BBL mixing and the other diffusivities. Otherwise, "}//&
2162                  \textcolor{stringliteral}{"diffusivity from the BBL\_mixing is simply added."}, &
2163                  default=.true.)
2164     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_LOTW\_BBL\_DIFFUSIVITY"}, cs%use\_LOTW\_BBL\_diffusivity, &
2165                  \textcolor{stringliteral}{"If true, uses a simple, imprecise but non-coordinate dependent, model "}//&
2166                  \textcolor{stringliteral}{"of BBL mixing diffusivity based on Law of the Wall. Otherwise, uses "}//&
2167                  \textcolor{stringliteral}{"the original BBL scheme."}, default=.false.)
2168     \textcolor{keywordflow}{if} (cs%use\_LOTW\_BBL\_diffusivity) \textcolor{keywordflow}{then}
2169       \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"LOTW\_BBL\_USE\_OMEGA"}, cs%LOTW\_BBL\_use\_omega, &
2170                  \textcolor{stringliteral}{"If true, use the maximum of Omega and N for the TKE to diffusion "}//&
2171                  \textcolor{stringliteral}{"calculation. Otherwise, N is N."}, default=.true.)
2172 \textcolor{keywordflow}{    endif}
2173   \textcolor{keywordflow}{else}
2174     cs%use\_LOTW\_BBL\_diffusivity = .false. \textcolor{comment}{! This parameterization depends on a u* from viscous BBL}
2175 \textcolor{keywordflow}{  endif}
2176   cs%id\_Kd\_BBL = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kd\_BBL'}, diag%axesTi, time, &
2177                  \textcolor{stringliteral}{'Bottom Boundary Layer Diffusivity'}, \textcolor{stringliteral}{'m2 s-1'}, conversion=us%Z2\_T\_to\_m2\_s)
2178   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"SIMPLE\_TKE\_TO\_KD"}, cs%simple\_TKE\_to\_Kd, &
2179                  \textcolor{stringliteral}{"If true, uses a simple estimate of Kd/TKE that will "}//&
2180                  \textcolor{stringliteral}{"work for arbitrary vertical coordinates. If false, "}//&
2181                  \textcolor{stringliteral}{"calculates Kd/TKE and bounds based on exact energetics "}//&
2182                  \textcolor{stringliteral}{"for an isopycnal layer-formulation."}, default=.false.)
2183 
2184   \textcolor{comment}{! set params related to the background mixing}
2185   \textcolor{keyword}{call }bkgnd\_mixing\_init(time, g, gv, us, param\_file, cs%diag, cs%bkgnd\_mixing\_csp)
2186 
2187   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KV"}, cs%Kv, &
2188                  \textcolor{stringliteral}{"The background kinematic viscosity in the interior. "}//&
2189                  \textcolor{stringliteral}{"The molecular value, ~1e-6 m2 s-1, may be used."}, &
2190                  units=\textcolor{stringliteral}{"m2 s-1"}, scale=us%m2\_s\_to\_Z2\_T, fail\_if\_missing=.true.)
2191 
2192   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KD"}, cs%Kd, &
2193                  \textcolor{stringliteral}{"The background diapycnal diffusivity of density in the "}//&
2194                  \textcolor{stringliteral}{"interior. Zero or the molecular value, ~1e-7 m2 s-1, "}//&
2195                  \textcolor{stringliteral}{"may be used."}, default=0.0, units=\textcolor{stringliteral}{"m2 s-1"}, scale=us%m2\_s\_to\_Z2\_T)
2196   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KD\_MIN"}, cs%Kd\_min, &
2197                  \textcolor{stringliteral}{"The minimum diapycnal diffusivity."}, &
2198                  units=\textcolor{stringliteral}{"m2 s-1"}, default=0.01*cs%Kd*us%Z2\_T\_to\_m2\_s, scale=us%m2\_s\_to\_Z2\_T)
2199   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KD\_MAX"}, cs%Kd\_max, &
2200                  \textcolor{stringliteral}{"The maximum permitted increment for the diapycnal "}//&
2201                  \textcolor{stringliteral}{"diffusivity from TKE-based parameterizations, or a negative "}//&
2202                  \textcolor{stringliteral}{"value for no limit."}, units=\textcolor{stringliteral}{"m2 s-1"}, default=-1.0, scale=us%m2\_s\_to\_Z2\_T)
2203   \textcolor{keywordflow}{if} (cs%simple\_TKE\_to\_Kd .and. cs%Kd\_max<=0.) \textcolor{keyword}{call }mom\_error(fatal, &
2204          \textcolor{stringliteral}{"set\_diffusivity\_init: To use SIMPLE\_TKE\_TO\_KD, KD\_MAX must be set to >0."})
2205   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KD\_ADD"}, cs%Kd\_add, &
2206                  \textcolor{stringliteral}{"A uniform diapycnal diffusivity that is added "}//&
2207                  \textcolor{stringliteral}{"everywhere without any filtering or scaling."}, &
2208                  units=\textcolor{stringliteral}{"m2 s-1"}, default=0.0, scale=us%m2\_s\_to\_Z2\_T)
2209   \textcolor{keywordflow}{if} (cs%use\_LOTW\_BBL\_diffusivity .and. cs%Kd\_max<=0.) \textcolor{keyword}{call }mom\_error(fatal, &
2210                  \textcolor{stringliteral}{"set\_diffusivity\_init: KD\_MAX must be set (positive) when "}// &
2211                  \textcolor{stringliteral}{"USE\_LOTW\_BBL\_DIFFUSIVITY=True."})
2212   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KD\_SMOOTH"}, cs%Kd\_smooth, &
2213                  \textcolor{stringliteral}{"A diapycnal diffusivity that is used to interpolate "}//&
2214                  \textcolor{stringliteral}{"more sensible values of T & S into thin layers."}, &
2215                  units=\textcolor{stringliteral}{"m2 s-1"}, default=1.0e-6, scale=us%m2\_s\_to\_Z2\_T)
2216 
2217   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEBUG"}, cs%debug, &
2218                  \textcolor{stringliteral}{"If true, write out verbose debugging data."}, &
2219                  default=.false., debuggingparam=.true.)
2220 
2221   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USER\_CHANGE\_DIFFUSIVITY"}, cs%user\_change\_diff, &
2222                  \textcolor{stringliteral}{"If true, call user-defined code to change the diffusivity."}, default=.false.)
2223 
2224   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DISSIPATION\_MIN"}, cs%dissip\_min, &
2225                  \textcolor{stringliteral}{"The minimum dissipation by which to determine a lower "}//&
2226                  \textcolor{stringliteral}{"bound of Kd (a floor)."}, &
2227                  units=\textcolor{stringliteral}{"W m-3"}, default=0.0, scale=us%W\_m2\_to\_RZ3\_T3*us%Z\_to\_m)
2228   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DISSIPATION\_N0"}, cs%dissip\_N0, &
2229                  \textcolor{stringliteral}{"The intercept when N=0 of the N-dependent expression "}//&
2230                  \textcolor{stringliteral}{"used to set a minimum dissipation by which to determine "}//&
2231                  \textcolor{stringliteral}{"a lower bound of Kd (a floor): A in eps\_min = A + B*N."}, &
2232                  units=\textcolor{stringliteral}{"W m-3"}, default=0.0, scale=us%W\_m2\_to\_RZ3\_T3*us%Z\_to\_m)
2233   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DISSIPATION\_N1"}, cs%dissip\_N1, &
2234                  \textcolor{stringliteral}{"The coefficient multiplying N, following Gargett, used to "}//&
2235                  \textcolor{stringliteral}{"set a minimum dissipation by which to determine a lower "}//&
2236                  \textcolor{stringliteral}{"bound of Kd (a floor): B in eps\_min = A + B*N"}, &
2237                  units=\textcolor{stringliteral}{"J m-3"}, default=0.0, scale=us%W\_m2\_to\_RZ3\_T3*us%Z\_to\_m*us%s\_to\_T)
2238   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DISSIPATION\_KD\_MIN"}, cs%dissip\_Kd\_min, &
2239                  \textcolor{stringliteral}{"The minimum vertical diffusivity applied as a floor."}, &
2240                  units=\textcolor{stringliteral}{"m2 s-1"}, default=0.0, scale=us%m2\_s\_to\_Z2\_T)
2241 
2242   cs%limit\_dissipation = (cs%dissip\_min>0.) .or. (cs%dissip\_N1>0.) .or. &
2243                          (cs%dissip\_N0>0.) .or. (cs%dissip\_Kd\_min>0.)
2244   cs%dissip\_N2 = 0.0
2245   \textcolor{keywordflow}{if} (cs%FluxRi\_max > 0.0) &
2246     cs%dissip\_N2 = cs%dissip\_Kd\_min * gv%Rho0 / cs%FluxRi\_max
2247 
2248   cs%id\_Kd\_bkgnd = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kd\_bkgnd'}, diag%axesTi, time, &
2249       \textcolor{stringliteral}{'Background diffusivity added by MOM\_bkgnd\_mixing module'}, \textcolor{stringliteral}{'m2/s'}, conversion=us%Z2\_T\_to\_m2\_s)
2250   cs%id\_Kv\_bkgnd = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kv\_bkgnd'}, diag%axesTi, time, &
2251       \textcolor{stringliteral}{'Background viscosity added by MOM\_bkgnd\_mixing module'}, \textcolor{stringliteral}{'m2/s'}, conversion=us%Z2\_T\_to\_m2\_s)
2252 
2253   cs%id\_Kd\_layer = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kd\_layer'}, diag%axesTL, time, &
2254       \textcolor{stringliteral}{'Diapycnal diffusivity of layers (as set)'}, \textcolor{stringliteral}{'m2 s-1'}, conversion=us%Z2\_T\_to\_m2\_s)
2255 
2256   \textcolor{keywordflow}{if} (cs%use\_tidal\_mixing) \textcolor{keywordflow}{then}
2257     cs%id\_Kd\_Work = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kd\_Work'}, diag%axesTL, time, &
2258          \textcolor{stringliteral}{'Work done by Diapycnal Mixing'}, \textcolor{stringliteral}{'W m-2'}, conversion=us%RZ3\_T3\_to\_W\_m2)
2259     cs%id\_maxTKE = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'maxTKE'}, diag%axesTL, time, &
2260            \textcolor{stringliteral}{'Maximum layer TKE'}, \textcolor{stringliteral}{'m3 s-3'}, conversion=(us%Z\_to\_m**3*us%s\_to\_T**3))
2261     cs%id\_TKE\_to\_Kd = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_to\_Kd'}, diag%axesTL, time, &
2262            \textcolor{stringliteral}{'Convert TKE to Kd'}, \textcolor{stringliteral}{'s2 m'}, conversion=us%Z2\_T\_to\_m2\_s*(us%m\_to\_Z**3*us%T\_to\_s**3))
2263     cs%id\_N2 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'N2'}, diag%axesTi, time, &
2264          \textcolor{stringliteral}{'Buoyancy frequency squared'}, \textcolor{stringliteral}{'s-2'}, conversion=us%s\_to\_T**2, cmor\_field\_name=\textcolor{stringliteral}{'obvfsq'}, &
2265           cmor\_long\_name=\textcolor{stringliteral}{'Square of seawater buoyancy frequency'}, &
2266           cmor\_standard\_name=\textcolor{stringliteral}{'square\_of\_brunt\_vaisala\_frequency\_in\_sea\_water'})
2267 \textcolor{keywordflow}{  endif}
2268 
2269   \textcolor{keywordflow}{if} (cs%user\_change\_diff) &
2270     cs%id\_Kd\_user = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kd\_user'}, diag%axesTi, time, &
2271          \textcolor{stringliteral}{'User-specified Extra Diffusivity'}, \textcolor{stringliteral}{'m2 s-1'}, conversion=us%Z2\_T\_to\_m2\_s)
2272 
2273   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DOUBLE\_DIFFUSION"}, cs%double\_diffusion, &
2274                  \textcolor{stringliteral}{"If true, increase diffusivites for temperature or salinity based on the "}//&
2275                  \textcolor{stringliteral}{"double-diffusive parameterization described in Large et al. (1994)."}, &
2276                  default=.false.)
2277 
2278   \textcolor{keywordflow}{if} (cs%double\_diffusion) \textcolor{keywordflow}{then}
2279     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MAX\_RRHO\_SALT\_FINGERS"}, cs%Max\_Rrho\_salt\_fingers, &
2280                  \textcolor{stringliteral}{"Maximum density ratio for salt fingering regime."}, &
2281                  default=2.55, units=\textcolor{stringliteral}{"nondim"})
2282     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MAX\_SALT\_DIFF\_SALT\_FINGERS"}, cs%Max\_salt\_diff\_salt\_fingers, &
2283                  \textcolor{stringliteral}{"Maximum salt diffusivity for salt fingering regime."}, &
2284                  default=1.e-4, units=\textcolor{stringliteral}{"m2 s-1"}, scale=us%m2\_s\_to\_Z2\_T)
2285     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KV\_MOLECULAR"}, cs%Kv\_molecular, &
2286                  \textcolor{stringliteral}{"Molecular viscosity for calculation of fluxes under double-diffusive "}//&
2287                  \textcolor{stringliteral}{"convection."}, default=1.5e-6, units=\textcolor{stringliteral}{"m2 s-1"}, scale=us%m2\_s\_to\_Z2\_T)
2288     \textcolor{comment}{! The default molecular viscosity follows the CCSM4.0 and MOM4p1 defaults.}
2289 \textcolor{keywordflow}{  endif} \textcolor{comment}{! old double-diffusion}
2290 
2291   \textcolor{keywordflow}{if} (cs%user\_change\_diff) \textcolor{keywordflow}{then}
2292     \textcolor{keyword}{call }user\_change\_diff\_init(time, g, gv, us, param\_file, diag, cs%user\_change\_diff\_CSp)
2293 \textcolor{keywordflow}{  endif}
2294 
2295   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BRYAN\_LEWIS\_DIFFUSIVITY"}, bryan\_lewis\_diffusivity, &
2296                  \textcolor{stringliteral}{"If true, use a Bryan & Lewis (JGR 1979) like tanh "}//&
2297                  \textcolor{stringliteral}{"profile of background diapycnal diffusivity with depth. "}//&
2298                  \textcolor{stringliteral}{"This is done via CVMix."}, default=.false., do\_not\_log=.true.)
2299   \textcolor{keywordflow}{if} (cs%use\_tidal\_mixing .and. bryan\_lewis\_diffusivity) &
2300     \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"MOM\_Set\_Diffusivity: "}// &
2301          \textcolor{stringliteral}{"Bryan-Lewis and internal tidal dissipation are both enabled. Choose one."})
2302 
2303   cs%useKappaShear = kappa\_shear\_init(time, g, gv, us, param\_file, cs%diag, cs%kappaShear\_CSp)
2304   cs%Vertex\_Shear = kappa\_shear\_at\_vertex(param\_file)
2305 
2306   \textcolor{keywordflow}{if} (cs%useKappaShear) &
2307     id\_clock\_kappashear = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean kappa\_shear)'}, grain=clock\_module)
2308 
2309   \textcolor{comment}{! CVMix shear-driven mixing}
2310   cs%use\_CVMix\_shear = cvmix\_shear\_init(time, g, gv, us, param\_file, cs%diag, cs%CVMix\_shear\_csp)
2311 
2312   \textcolor{comment}{! CVMix double diffusion mixing}
2313   cs%use\_CVMix\_ddiff = cvmix\_ddiff\_init(time, g, gv, us, param\_file, cs%diag, cs%CVMix\_ddiff\_csp)
2314   \textcolor{keywordflow}{if} (cs%use\_CVMix\_ddiff) &
2315     id\_clock\_cvmix\_ddiff = cpu\_clock\_id(\textcolor{stringliteral}{'(Double diffusion via CVMix)'}, grain=clock\_module)
2316 
2317   \textcolor{keywordflow}{if} (cs%double\_diffusion .and. cs%use\_CVMix\_ddiff) \textcolor{keywordflow}{then}
2318     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{'set\_diffusivity\_init: '}// &
2319            \textcolor{stringliteral}{'Multiple double-diffusion options selected (DOUBLE\_DIFFUSION and'}//&
2320            \textcolor{stringliteral}{'USE\_CVMIX\_DDIFF), please disable all but one option to proceed.'})
2321 \textcolor{keywordflow}{  endif}
2322 
2323   \textcolor{keywordflow}{if} (cs%double\_diffusion .or. cs%use\_CVMix\_ddiff) \textcolor{keywordflow}{then}
2324     cs%id\_KT\_extra = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'KT\_extra'}, diag%axesTi, time, &
2325          \textcolor{stringliteral}{'Double-diffusive diffusivity for temperature'}, \textcolor{stringliteral}{'m2 s-1'}, conversion=us%Z2\_T\_to\_m2\_s)
2326     cs%id\_KS\_extra = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'KS\_extra'}, diag%axesTi, time, &
2327          \textcolor{stringliteral}{'Double-diffusive diffusivity for salinity'}, \textcolor{stringliteral}{'m2 s-1'}, conversion=us%Z2\_T\_to\_m2\_s)
2328 \textcolor{keywordflow}{  endif}
2329   \textcolor{keywordflow}{if} (cs%use\_CVMix\_ddiff) \textcolor{keywordflow}{then}
2330     cs%id\_R\_rho = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'R\_rho'}, diag%axesTi, time, &
2331          \textcolor{stringliteral}{'Double-diffusion density ratio'}, \textcolor{stringliteral}{'nondim'})
2332 \textcolor{keywordflow}{  endif}
2333 
2334   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(halo\_ts)) \textcolor{keywordflow}{then}
2335     halo\_ts = 0
2336     \textcolor{keywordflow}{if} (cs%Vertex\_Shear) halo\_ts = 1
2337 \textcolor{keywordflow}{  endif}
2338 
2339   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(double\_diffuse)) \textcolor{keywordflow}{then}
2340     double\_diffuse = (cs%double\_diffusion .or. cs%use\_CVMix\_ddiff)
2341 \textcolor{keywordflow}{  endif}
2342 
\end{DoxyCode}
