\hypertarget{namespacemom__set__diffusivity}{}\doxysection{mom\+\_\+set\+\_\+diffusivity Module Reference}
\label{namespacemom__set__diffusivity}\index{mom\_set\_diffusivity@{mom\_set\_diffusivity}}


\doxysubsection{Detailed Description}
Calculate vertical diffusivity from all mixing processes. \doxysubsection*{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}
\doxysubsection*{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}


\doxysubsection{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}}
\doxysubsubsection{\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{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em u} & The zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em v} & The meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & Structure containing pointers to any available thermodynamic fields. \\
\hline
\mbox{\texttt{ in}}  & {\em fluxes} & A structure of thermodynamic surface fluxes \\
\hline
\mbox{\texttt{ in}}  & {\em visc} & Structure containing vertical viscosities, bottom boundary layer properies, and related fields \\
\hline
\mbox{\texttt{ in}}  & {\em j} & j-\/index of row to work on \\
\hline
\mbox{\texttt{ 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{\texttt{ 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{\texttt{ 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{\texttt{ in,out}}  & {\em kd\+\_\+lay} & The diapycnal diffusivity in layers, \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ 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 1175 of file M\+O\+M\+\_\+set\+\_\+diffusivity.\+F90.


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


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


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


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


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


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


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


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


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


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


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


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


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

\end{DoxyEnumerate}

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


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


\begin{DoxyCode}{0}
\DoxyCodeLine{2347   \textcolor{keywordtype}{type}(set\_diffusivity\_CS), \textcolor{keywordtype}{pointer} :: CS\textcolor{comment}{ !< Control structure for this module}}
\DoxyCodeLine{2348 }
\DoxyCodeLine{2349   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{return}}
\DoxyCodeLine{2350 }
\DoxyCodeLine{2351   \textcolor{keyword}{call }bkgnd\_mixing\_end(cs\%bkgnd\_mixing\_csp)}
\DoxyCodeLine{2352 }
\DoxyCodeLine{2353   \textcolor{keywordflow}{if} (cs\%use\_tidal\_mixing) \textcolor{keyword}{call }tidal\_mixing\_end(cs\%tidal\_mixing\_CSp)}
\DoxyCodeLine{2354 }
\DoxyCodeLine{2355   \textcolor{keywordflow}{if} (cs\%user\_change\_diff) \textcolor{keyword}{call }user\_change\_diff\_end(cs\%user\_change\_diff\_CSp)}
\DoxyCodeLine{2356 }
\DoxyCodeLine{2357   \textcolor{keywordflow}{if} (cs\%use\_CVMix\_shear)  \textcolor{keyword}{call }cvmix\_shear\_end(cs\%CVMix\_shear\_csp)}
\DoxyCodeLine{2358 }
\DoxyCodeLine{2359   \textcolor{keywordflow}{if} (cs\%use\_CVMix\_ddiff)  \textcolor{keyword}{call }cvmix\_ddiff\_end(cs\%CVMix\_ddiff\_csp)}
\DoxyCodeLine{2360 }
\DoxyCodeLine{2361   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{deallocate}(cs)}
\DoxyCodeLine{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}}
\doxysubsubsection{\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{\texttt{ in}}  & {\em time} & The current model time\\
\hline
\mbox{\texttt{ in,out}}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\texttt{ in}}  & {\em param\+\_\+file} & A structure to parse for run-\/time parameters.\\
\hline
\mbox{\texttt{ 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{\texttt{ out}}  & {\em halo\+\_\+ts} & The halo size of tracer points that must be valid for the calculations in set\+\_\+diffusivity.\\
\hline
\mbox{\texttt{ out}}  & {\em double\+\_\+diffuse} & If present, this indicates whether some version of double diffusion is being used. \\
\hline
\end{DoxyParams}


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


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

\end{DoxyCode}
