\hypertarget{namespacemom__bulk__mixed__layer}{}\doxysection{mom\+\_\+bulk\+\_\+mixed\+\_\+layer Module Reference}
\label{namespacemom__bulk__mixed__layer}\index{mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}}


\doxysubsection{Detailed Description}
Build mixed layer parameterization. 

By Robert Hallberg, 1997 -\/ 2005.

This file contains the subroutine (bulkmixedlayer) that implements a Kraus-\/\+Turner-\/like bulk mixed layer, based on the work of various people, as described in the review paper by Niiler and Kraus (1979), with particular attention to the form proposed by Oberhuber (J\+PO, 1993, 808-\/829), with an extension to a refied bulk mixed layer as described in Hallberg (Aha Huliko\textquotesingle{}a, 2003). The physical processes portrayed in this subroutine include convective adjustment and mixed layer entrainment and detrainment. Penetrating shortwave radiation and an exponential decay of T\+KE fluxes are also supported by this subroutine. Several constants can alternately be set to give a traditional Kraus-\/\+Turner mixed layer scheme, although that is not the preferred option. The physical processes and arguments are described in detail below. \doxysubsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__bulk__mixed__layer_1_1bulkmixedlayer__cs}{bulkmixedlayer\+\_\+cs}}
\begin{DoxyCompactList}\small\item\em The control structure with parameters for the M\+O\+M\+\_\+bulk\+\_\+mixed\+\_\+layer module. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \mbox{\hyperlink{namespacemom__bulk__mixed__layer_ad6b69cad68bd88aa1deee0481fd3cc59}{bulkmixedlayer}} (h\+\_\+3d, u\+\_\+3d, v\+\_\+3d, tv, fluxes, dt, ea, eb, G, GV, US, CS, optics, Hml, aggregate\+\_\+\+F\+W\+\_\+forcing, dt\+\_\+diag, last\+\_\+call)
\begin{DoxyCompactList}\small\item\em This subroutine partially steps the bulk mixed layer model. The following processes are executed, in the order listed. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__bulk__mixed__layer_a64f3bec37c0a6dfba1400d03c61399e7}{convective\+\_\+adjustment}} (h, u, v, R0, Rcv, T, S, eps, d\+\_\+eb, d\+K\+E\+\_\+\+CA, c\+T\+KE, j, G, GV, US, CS, nz\+\_\+conv)
\begin{DoxyCompactList}\small\item\em This subroutine does instantaneous convective entrainment into the buffer layers and mixed layers to remove hydrostatic instabilities. Any water that is lighter than currently in the mixed-\/ or buffer-\/ layer is entrained. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__bulk__mixed__layer_a0f75ed48f800138d458b5654d151fe50}{mixedlayer\+\_\+convection}} (h, d\+\_\+eb, htot, Ttot, Stot, uhtot, vhtot, R0\+\_\+tot, Rcv\+\_\+tot, u, v, T, S, R0, Rcv, eps, d\+R0\+\_\+dT, d\+Rcv\+\_\+dT, d\+R0\+\_\+dS, d\+Rcv\+\_\+dS, net\+Mass\+In\+Out, net\+Mass\+Out, Net\+\_\+heat, Net\+\_\+salt, nsw, Pen\+\_\+\+S\+W\+\_\+bnd, opacity\+\_\+band, Conv\+\_\+\+En, d\+K\+E\+\_\+\+FC, j, ksort, G, GV, US, CS, tv, fluxes, dt, aggregate\+\_\+\+F\+W\+\_\+forcing)
\begin{DoxyCompactList}\small\item\em This subroutine causes the mixed layer to entrain to the depth of free convection. The depth of free convection is the shallowest depth at which the fluid is denser than the average of the fluid above. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__bulk__mixed__layer_a8ab429f040caadc340609ca16aca2e29}{find\+\_\+starting\+\_\+tke}} (htot, h\+\_\+\+CA, fluxes, Conv\+\_\+\+En, c\+T\+KE, d\+K\+E\+\_\+\+FC, d\+K\+E\+\_\+\+CA, T\+KE, T\+K\+E\+\_\+river, Idecay\+\_\+len\+\_\+\+T\+KE, c\+M\+KE, dt, Idt\+\_\+diag, j, ksort, G, GV, US, CS)
\begin{DoxyCompactList}\small\item\em This subroutine determines the T\+KE available at the depth of free convection to drive mechanical entrainment. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__bulk__mixed__layer_aae11f02b6b843d50866b7e259a7d468a}{mechanical\+\_\+entrainment}} (h, d\+\_\+eb, htot, Ttot, Stot, uhtot, vhtot, R0\+\_\+tot, Rcv\+\_\+tot, u, v, T, S, R0, Rcv, eps, d\+R0\+\_\+dT, d\+Rcv\+\_\+dT, c\+M\+KE, Idt\+\_\+diag, nsw, Pen\+\_\+\+S\+W\+\_\+bnd, opacity\+\_\+band, T\+KE, Idecay\+\_\+len\+\_\+\+T\+KE, j, ksort, G, GV, US, CS)
\begin{DoxyCompactList}\small\item\em This subroutine calculates mechanically driven entrainment. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__bulk__mixed__layer_ae4325155d260533b923ba910557945f3}{sort\+\_\+ml}} (h, R0, eps, G, GV, CS, ksort)
\begin{DoxyCompactList}\small\item\em This subroutine generates an array of indices that are sorted by layer density. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__bulk__mixed__layer_a1378659bc97b52e065a7cfe44166504d}{resort\+\_\+ml}} (h, T, S, R0, Rcv, Rcv\+Tgt, eps, d\+\_\+ea, d\+\_\+eb, ksort, G, GV, CS, d\+R0\+\_\+dT, d\+R0\+\_\+dS, d\+Rcv\+\_\+dT, d\+Rcv\+\_\+dS)
\begin{DoxyCompactList}\small\item\em This subroutine actually moves properties between layers to achieve a resorted state, with all of the resorted water either moved into the correct interior layers or in the top nkmb layers. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__bulk__mixed__layer_a5f7d06425d0395a7fd4b94942c6465d0}{mixedlayer\+\_\+detrain\+\_\+2}} (h, T, S, R0, Rcv, Rcv\+Tgt, dt, dt\+\_\+diag, d\+\_\+ea, j, G, GV, US, CS, d\+R0\+\_\+dT, d\+R0\+\_\+dS, d\+Rcv\+\_\+dT, d\+Rcv\+\_\+dS, max\+\_\+\+B\+L\+\_\+det)
\begin{DoxyCompactList}\small\item\em This subroutine moves any water left in the former mixed layers into the two buffer layers and may also move buffer layer water into the interior isopycnal layers. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__bulk__mixed__layer_aa33a3e7c5e1b18444bf54d37f1c00ad3}{mixedlayer\+\_\+detrain\+\_\+1}} (h, T, S, R0, Rcv, Rcv\+Tgt, dt, dt\+\_\+diag, d\+\_\+ea, d\+\_\+eb, j, G, GV, US, CS, d\+Rcv\+\_\+dT, d\+Rcv\+\_\+dS, max\+\_\+\+B\+L\+\_\+det)
\begin{DoxyCompactList}\small\item\em This subroutine moves any water left in the former mixed layers into the single buffer layers and may also move buffer layer water into the interior isopycnal layers. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__bulk__mixed__layer_a14f3b36851c81d60fb31ff86870a7d54}{bulkmixedlayer\+\_\+init}} (Time, G, GV, US, param\+\_\+file, diag, CS)
\begin{DoxyCompactList}\small\item\em This subroutine initializes the M\+OM bulk mixed layer module. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__bulk__mixed__layer_a4ac89b3858f2c7c0ac6f8ac8f93b5e44}{ef4}} (Ht, En, I\+\_\+L, d\+R\+\_\+de)
\begin{DoxyCompactList}\small\item\em This subroutine returns an approximation to the integral R = exp(-\/L$\ast$(H+E)) integral(LH to L(H+E)) L/(1-\/(1+x)exp(-\/x)) dx. The approximation to the integrand is good to within -\/2\% at x$\sim$.3 and +25\% at x$\sim$3.5, but the exponential deemphasizes the importance of large x. When L=0, E\+F4 returns E/((Ht+E)$\ast$\+Ht). \end{DoxyCompactList}\end{DoxyCompactItemize}


\doxysubsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_ad6b69cad68bd88aa1deee0481fd3cc59}\label{namespacemom__bulk__mixed__layer_ad6b69cad68bd88aa1deee0481fd3cc59}} 
\index{mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}!bulkmixedlayer@{bulkmixedlayer}}
\index{bulkmixedlayer@{bulkmixedlayer}!mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}}
\doxysubsubsection{\texorpdfstring{bulkmixedlayer()}{bulkmixedlayer()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+bulk\+\_\+mixed\+\_\+layer\+::bulkmixedlayer (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, gv \%ke), intent(inout)}]{h\+\_\+3d,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, gv \%ke), intent(in)}]{u\+\_\+3d,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, gv \%ke), intent(in)}]{v\+\_\+3d,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(inout)}]{tv,  }\item[{type(forcing), intent(inout)}]{fluxes,  }\item[{real, intent(in)}]{dt,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, gv \%ke), intent(inout)}]{ea,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, gv \%ke), intent(inout)}]{eb,  }\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(\mbox{\hyperlink{structmom__bulk__mixed__layer_1_1bulkmixedlayer__cs}{bulkmixedlayer\+\_\+cs}}), pointer}]{CS,  }\item[{type(optics\+\_\+type), pointer}]{optics,  }\item[{real, dimension(\+:,\+:), pointer}]{Hml,  }\item[{logical, intent(in)}]{aggregate\+\_\+\+F\+W\+\_\+forcing,  }\item[{real, intent(in), optional}]{dt\+\_\+diag,  }\item[{logical, intent(in), optional}]{last\+\_\+call }\end{DoxyParamCaption})}



This subroutine partially steps the bulk mixed layer model. The following processes are executed, in the order listed. 


\begin{DoxyEnumerate}
\item Undergo convective adjustment into mixed layer.
\item Apply surface heating and cooling.
\item Starting from the top, entrain whatever fluid the T\+KE budget permits. Penetrating shortwave radiation is also applied at this point.
\item If there is any unentrained fluid that was formerly in the mixed layer, detrain this fluid into the buffer layer. This is equivalent to the mixed layer detraining to the Monin-\/ Obukhov depth.
\item Divide the fluid in the mixed layer evenly into CSnkml pieces.
\item Split the buffer layer if appropriate. Layers 1 to nkml are the mixed layer, nkml+1 to nkml+nkbl are the buffer layers. The results of this subroutine are mathematically identical if there are multiple pieces of the mixed layer with the same density or if there is just a single layer. There is no stability limit on the time step.
\end{DoxyEnumerate}

The key parameters for the mixed layer are found in the control structure. These include mstar, nstar, nstar2, pen\+\_\+\+S\+W\+\_\+frac, pen\+\_\+\+S\+W\+\_\+scale, and T\+K\+E\+\_\+decay. For the Oberhuber (1993) mixed layer, the values of these are\+: pen\+\_\+\+S\+W\+\_\+frac = 0.\+42, pen\+\_\+\+S\+W\+\_\+scale = 15.\+0 m, mstar = 1.\+25, nstar = 1, T\+K\+E\+\_\+decay = 2.\+5, conv\+\_\+decay = 0.\+5 T\+K\+E\+\_\+decay is 1/kappa in eq. 28 of Oberhuber (1993), while conv\+\_\+decay is 1/mu. Conv\+\_\+decay has been eliminated in favor of the well-\/calibrated form for the efficiency of penetrating convection from Wang (2003). For a traditional Kraus-\/\+Turner mixed layer, the values are\+: pen\+\_\+\+S\+W\+\_\+frac = 0.\+0, pen\+\_\+\+S\+W\+\_\+scale = 0.\+0 m, mstar = 1.\+25, nstar = 0.\+4, T\+K\+E\+\_\+decay = 0.\+0, conv\+\_\+decay = 0.\+0 
\begin{DoxyParams}[1]{Parameters}
\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,out}}  & {\em h\+\_\+3d} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em u\+\_\+3d} & Zonal velocities interpolated to h points \\
\hline
\mbox{\texttt{ in}}  & {\em v\+\_\+3d} & Zonal velocities interpolated to h points \\
\hline
\mbox{\texttt{ in,out}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs. \\
\hline
\mbox{\texttt{ in,out}}  & {\em fluxes} & A structure containing pointers to any possible forcing fields. Unused fields have N\+U\+LL ptrs. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em ea} & The amount of fluid moved downward into a \\
\hline
\mbox{\texttt{ in,out}}  & {\em eb} & The amount of fluid moved upward into a \\
\hline
 & {\em cs} & The control structure returned by a previous call to mixedlayer\+\_\+init. \\
\hline
 & {\em optics} & The structure containing the inverse of the vertical absorption decay scale for penetrating shortwave radiation \mbox{[}m-\/1\mbox{]}. \\
\hline
 & {\em hml} & Active mixed layer depth \mbox{[}Z $\sim$$>$ m\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em aggregate\+\_\+fw\+\_\+forcing} & If true, the net incoming and outgoing surface freshwater fluxes are combined before being applied, instead of being applied separately. \\
\hline
\mbox{\texttt{ in}}  & {\em dt\+\_\+diag} & The diagnostic time step, which may be less than dt if there are two callse to mixedlayer \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em last\+\_\+call} & if true, this is the last call to mixedlayer in the current time step, so diagnostics will be written. The default is .true. \\
\hline
\end{DoxyParams}


Definition at line 190 of file M\+O\+M\+\_\+bulk\+\_\+mixed\+\_\+layer.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{190   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{      !< The ocean's grid structure.}}
\DoxyCodeLine{191   \textcolor{keywordtype}{type}(verticalGrid\_type),    \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{     !< The ocean's vertical grid structure.}}
\DoxyCodeLine{192   \textcolor{keywordtype}{type}(unit\_scale\_type),      \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{     !< A dimensional unit scaling type}}
\DoxyCodeLine{193 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{194                               \textcolor{keywordtype}{intent(inout)} :: h\_3d\textcolor{comment}{   !< Layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{195 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{196                               \textcolor{keywordtype}{intent(in)}    :: u\_3d\textcolor{comment}{   !< Zonal velocities interpolated to h points}}
\DoxyCodeLine{197 \textcolor{comment}{                                                      !! [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{198 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{199                               \textcolor{keywordtype}{intent(in)}    :: v\_3d\textcolor{comment}{   !< Zonal velocities interpolated to h points}}
\DoxyCodeLine{200 \textcolor{comment}{                                                      !! [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{201   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),      \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{     !< A structure containing pointers to any}}
\DoxyCodeLine{202 \textcolor{comment}{                                                      !! available thermodynamic fields. Absent}}
\DoxyCodeLine{203 \textcolor{comment}{                                                      !! fields have NULL ptrs.}}
\DoxyCodeLine{204   \textcolor{keywordtype}{type}(forcing),              \textcolor{keywordtype}{intent(inout)} :: fluxes\textcolor{comment}{ !< A structure containing pointers to any}}
\DoxyCodeLine{205 \textcolor{comment}{                                                      !! possible forcing fields.  Unused fields}}
\DoxyCodeLine{206 \textcolor{comment}{                                                      !! have NULL ptrs.}}
\DoxyCodeLine{207 \textcolor{keywordtype}{  real},                       \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{     !< Time increment [T \string~> s].}}
\DoxyCodeLine{208 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{209                               \textcolor{keywordtype}{intent(inout)} :: ea\textcolor{comment}{     !< The amount of fluid moved downward into a}}
\DoxyCodeLine{210 \textcolor{comment}{                                                      !! layer; this should be increased due to}}
\DoxyCodeLine{211 \textcolor{comment}{                                                      !! mixed layer detrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{212 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{213                               \textcolor{keywordtype}{intent(inout)} :: eb\textcolor{comment}{     !< The amount of fluid moved upward into a}}
\DoxyCodeLine{214 \textcolor{comment}{                                                      !! layer; this should be increased due to}}
\DoxyCodeLine{215 \textcolor{comment}{                                                      !! mixed layer entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{216   \textcolor{keywordtype}{type}(bulkmixedlayer\_CS),    \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{     !< The control structure returned by a}}
\DoxyCodeLine{217 \textcolor{comment}{                                                      !! previous call to mixedlayer\_init.}}
\DoxyCodeLine{218   \textcolor{keywordtype}{type}(optics\_type),          \textcolor{keywordtype}{pointer}       :: optics\textcolor{comment}{ !< The structure containing the inverse of the}}
\DoxyCodeLine{219 \textcolor{comment}{                                                      !! vertical absorption decay scale for}}
\DoxyCodeLine{220 \textcolor{comment}{                                                      !! penetrating shortwave radiation [m-\/1].}}
\DoxyCodeLine{221 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(:,:)},       \textcolor{keywordtype}{pointer}       :: Hml\textcolor{comment}{    !< Active mixed layer depth [Z \string~> m].}}
\DoxyCodeLine{222   \textcolor{keywordtype}{logical},                    \textcolor{keywordtype}{intent(in)}    :: aggregate\_FW\_forcing\textcolor{comment}{ !< If true, the net incoming and}}
\DoxyCodeLine{223 \textcolor{comment}{                                                     !! outgoing surface freshwater fluxes are}}
\DoxyCodeLine{224 \textcolor{comment}{                                                     !! combined before being applied, instead of}}
\DoxyCodeLine{225 \textcolor{comment}{                                                     !! being applied separately.}}
\DoxyCodeLine{226 \textcolor{keywordtype}{  real},             \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: dt\_diag\textcolor{comment}{  !< The diagnostic time step,}}
\DoxyCodeLine{227 \textcolor{comment}{                                                      !! which may be less than dt if there are}}
\DoxyCodeLine{228 \textcolor{comment}{                                                      !! two callse to mixedlayer [T \string~> s].}}
\DoxyCodeLine{229   \textcolor{keywordtype}{logical},          \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: last\_call\textcolor{comment}{ !< if true, this is the last call}}
\DoxyCodeLine{230 \textcolor{comment}{                                                      !! to mixedlayer in the current time step, so}}
\DoxyCodeLine{231 \textcolor{comment}{                                                      !! diagnostics will be written. The default is}}
\DoxyCodeLine{232 \textcolor{comment}{                                                      !! .true.}}
\DoxyCodeLine{233 }
\DoxyCodeLine{234   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{235 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))} :: \&}
\DoxyCodeLine{236     eaml, \&     \textcolor{comment}{!   The amount of fluid moved downward into a layer due to mixed}}
\DoxyCodeLine{237                 \textcolor{comment}{! layer detrainment [H \string~> m or kg m-\/2]. (I.e. entrainment from above.)}}
\DoxyCodeLine{238     ebml        \textcolor{comment}{!   The amount of fluid moved upward into a layer due to mixed}}
\DoxyCodeLine{239                 \textcolor{comment}{! layer detrainment [H \string~> m or kg m-\/2]. (I.e. entrainment from below.)}}
\DoxyCodeLine{240 }
\DoxyCodeLine{241   \textcolor{comment}{! If there is resorting, the vertical coordinate for these variables is the}}
\DoxyCodeLine{242   \textcolor{comment}{! new, sorted index space.  Here layer 0 is an initially massless layer that}}
\DoxyCodeLine{243   \textcolor{comment}{! will be used to hold the new mixed layer properties.}}
\DoxyCodeLine{244 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))} :: \&}
\DoxyCodeLine{245     h, \&        \textcolor{comment}{!   The layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{246     T, \&        \textcolor{comment}{!   The layer temperatures [degC].}}
\DoxyCodeLine{247     S, \&        \textcolor{comment}{!   The layer salinities [ppt].}}
\DoxyCodeLine{248     R0, \&       \textcolor{comment}{!   The potential density referenced to the surface [R \string~> kg m-\/3].}}
\DoxyCodeLine{249     Rcv         \textcolor{comment}{!   The coordinate variable potential density [R \string~> kg m-\/3].}}
\DoxyCodeLine{250 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))} :: \&}
\DoxyCodeLine{251     u, \&        \textcolor{comment}{!   The zonal velocity [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{252     v, \&        \textcolor{comment}{!   The meridional velocity [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{253     h\_orig, \&   \textcolor{comment}{!   The original thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{254     d\_eb, \&     \textcolor{comment}{!   The downward increase across a layer in the entrainment from}}
\DoxyCodeLine{255                 \textcolor{comment}{! below [H \string~> m or kg m-\/2].  The sign convention is that positive values of}}
\DoxyCodeLine{256                 \textcolor{comment}{! d\_eb correspond to a gain in mass by a layer by upward motion.}}
\DoxyCodeLine{257     d\_ea, \&     \textcolor{comment}{!   The upward increase across a layer in the entrainment from}}
\DoxyCodeLine{258                 \textcolor{comment}{! above [H \string~> m or kg m-\/2].  The sign convention is that positive values of}}
\DoxyCodeLine{259                 \textcolor{comment}{! d\_ea mean a net gain in mass by a layer from downward motion.}}
\DoxyCodeLine{260     eps         \textcolor{comment}{! The (small) thickness that must remain in a layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{261   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))} :: \&}
\DoxyCodeLine{262     ksort       \textcolor{comment}{!   The sorted k-\/index that each original layer goes to.}}
\DoxyCodeLine{263 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: \&}
\DoxyCodeLine{264     h\_miss      \textcolor{comment}{!   The summed absolute mismatch [Z \string~> m].}}
\DoxyCodeLine{265 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: \&}
\DoxyCodeLine{266     TKE, \&      \textcolor{comment}{!   The turbulent kinetic energy available for mixing over a}}
\DoxyCodeLine{267                 \textcolor{comment}{! time step [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{268     conv\_en, \&  \textcolor{comment}{!   The turbulent kinetic energy source due to mixing down to}}
\DoxyCodeLine{269                 \textcolor{comment}{! the depth of free convection [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{270     htot, \&     \textcolor{comment}{!   The total depth of the layers being considered for}}
\DoxyCodeLine{271                 \textcolor{comment}{! entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{272     r0\_tot, \&   \textcolor{comment}{!   The integrated potential density referenced to the surface}}
\DoxyCodeLine{273                 \textcolor{comment}{! of the layers which are fully entrained [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{274     rcv\_tot, \&  \textcolor{comment}{!   The integrated coordinate value potential density of the}}
\DoxyCodeLine{275                 \textcolor{comment}{! layers that are fully entrained [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{276     ttot, \&     \textcolor{comment}{!   The integrated temperature of layers which are fully}}
\DoxyCodeLine{277                 \textcolor{comment}{! entrained [degC H \string~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{278     stot, \&     \textcolor{comment}{!   The integrated salt of layers which are fully entrained}}
\DoxyCodeLine{279                 \textcolor{comment}{! [H ppt \string~> m ppt or ppt kg m-\/2].}}
\DoxyCodeLine{280     uhtot, \&    \textcolor{comment}{!   The depth integrated zonal and meridional velocities in the}}
\DoxyCodeLine{281     vhtot, \&    \textcolor{comment}{! mixed layer [H L T-\/1 \string~> m2 s-\/1 or kg m-\/1 s-\/1].}}
\DoxyCodeLine{282 }
\DoxyCodeLine{283     netmassinout, \&  \textcolor{comment}{! The net mass flux (if non-\/Boussinsq) or volume flux (if}}
\DoxyCodeLine{284                      \textcolor{comment}{! Boussinesq -\/ i.e. the fresh water flux (P+R-\/E)) into the}}
\DoxyCodeLine{285                      \textcolor{comment}{! ocean over a time step [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{286     netmassout,   \&  \textcolor{comment}{! The mass flux (if non-\/Boussinesq) or volume flux (if Boussinesq)}}
\DoxyCodeLine{287                      \textcolor{comment}{! over a time step from evaporating fresh water [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{288     net\_heat, \& \textcolor{comment}{!   The net heating at the surface over a time step [degC H \string~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{289                 \textcolor{comment}{! Any penetrating shortwave radiation is not included in Net\_heat.}}
\DoxyCodeLine{290     net\_salt, \& \textcolor{comment}{! The surface salt flux into the ocean over a time step, ppt H.}}
\DoxyCodeLine{291     idecay\_len\_tke, \&  \textcolor{comment}{! The inverse of a turbulence decay length scale [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{292     p\_ref, \&    \textcolor{comment}{!   Reference pressure for the potential density governing mixed}}
\DoxyCodeLine{293                 \textcolor{comment}{! layer dynamics, almost always 0 (or 1e5) [R L2 T-\/2 \string~> Pa].}}
\DoxyCodeLine{294     p\_ref\_cv, \& \textcolor{comment}{!   Reference pressure for the potential density which defines}}
\DoxyCodeLine{295                 \textcolor{comment}{! the coordinate variable, set to P\_Ref [R L2 T-\/2 \string~> Pa].}}
\DoxyCodeLine{296     dr0\_dt, \&   \textcolor{comment}{!   Partial derivative of the mixed layer potential density with}}
\DoxyCodeLine{297                 \textcolor{comment}{! temperature [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{298     drcv\_dt, \&  \textcolor{comment}{!   Partial derivative of the coordinate variable potential}}
\DoxyCodeLine{299                 \textcolor{comment}{! density in the mixed layer with temperature [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{300     dr0\_ds, \&   \textcolor{comment}{!   Partial derivative of the mixed layer potential density with}}
\DoxyCodeLine{301                 \textcolor{comment}{! salinity [R ppt-\/1 \string~> kg m-\/3 ppt-\/1].}}
\DoxyCodeLine{302     drcv\_ds, \&  \textcolor{comment}{!   Partial derivative of the coordinate variable potential}}
\DoxyCodeLine{303                 \textcolor{comment}{! density in the mixed layer with salinity [R ppt-\/1 \string~> kg m-\/3 ppt-\/1].}}
\DoxyCodeLine{304     tke\_river   \textcolor{comment}{! The source of turbulent kinetic energy available for mixing}}
\DoxyCodeLine{305                 \textcolor{comment}{! at rivermouths [Z L2 T-\/3 \string~> m3 s-\/3].}}
\DoxyCodeLine{306 }
\DoxyCodeLine{307 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(max(CS\%nsw,1),SZI\_(G))} :: \&}
\DoxyCodeLine{308     Pen\_SW\_bnd  \textcolor{comment}{!   The penetrating fraction of the shortwave heating integrated}}
\DoxyCodeLine{309                 \textcolor{comment}{! over a time step in each band [degC H \string~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{310 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(max(CS\%nsw,1),SZI\_(G),SZK\_(GV))} :: \&}
\DoxyCodeLine{311     opacity\_band \textcolor{comment}{! The opacity in each band [H-\/1 \string~> m-\/1 or m2 kg-\/1]. The indicies are band, i, k.}}
\DoxyCodeLine{312 }
\DoxyCodeLine{313 \textcolor{keywordtype}{  real} :: cMKE(2,SZI\_(G)) \textcolor{comment}{! Coefficients of HpE and HpE\string^2 used in calculating the}}
\DoxyCodeLine{314                           \textcolor{comment}{! denominator of MKE\_rate; the two elements have differing}}
\DoxyCodeLine{315                           \textcolor{comment}{! units of [H-\/1 \string~> m-\/1 or m2 kg-\/1] and [H-\/2 \string~> m-\/2 or m4 kg-\/2].}}
\DoxyCodeLine{316 \textcolor{keywordtype}{  real} :: Irho0         \textcolor{comment}{! 1.0 / rho\_0 [R-\/1 \string~> m3 kg-\/1]}}
\DoxyCodeLine{317 \textcolor{keywordtype}{  real} :: Inkml, Inkmlm1\textcolor{comment}{!  1.0 / REAL(nkml) and  1.0 / REAL(nkml-\/1)}}
\DoxyCodeLine{318 \textcolor{keywordtype}{  real} :: Ih            \textcolor{comment}{!   The inverse of a thickness [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{319 \textcolor{keywordtype}{  real} :: Idt\_diag      \textcolor{comment}{!   The inverse of the timestep used for diagnostics [T-\/1 \string~> s-\/1].}}
\DoxyCodeLine{320 \textcolor{keywordtype}{  real} :: RmixConst}
\DoxyCodeLine{321 }
\DoxyCodeLine{322 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: \&}
\DoxyCodeLine{323     dKE\_FC, \&   \textcolor{comment}{!   The change in mean kinetic energy due to free convection}}
\DoxyCodeLine{324                 \textcolor{comment}{! [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{325     h\_ca        \textcolor{comment}{!   The depth to which convective adjustment has gone [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{326 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))} :: \&}
\DoxyCodeLine{327     dKE\_CA, \&   \textcolor{comment}{!   The change in mean kinetic energy due to convective}}
\DoxyCodeLine{328                 \textcolor{comment}{! adjustment [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{329     ctke        \textcolor{comment}{!   The turbulent kinetic energy source due to convective}}
\DoxyCodeLine{330                 \textcolor{comment}{! adjustment [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{331 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: \&}
\DoxyCodeLine{332     Hsfc\_max, \& \textcolor{comment}{! The thickness of the surface region (mixed and buffer layers)}}
\DoxyCodeLine{333                 \textcolor{comment}{! after entrainment but before any buffer layer detrainment [Z \string~> m].}}
\DoxyCodeLine{334     hsfc\_used, \& \textcolor{comment}{! The thickness of the surface region after buffer layer}}
\DoxyCodeLine{335                 \textcolor{comment}{! detrainment [Z \string~> m].}}
\DoxyCodeLine{336     hsfc\_min, \& \textcolor{comment}{! The minimum thickness of the surface region based on the}}
\DoxyCodeLine{337                 \textcolor{comment}{! new mixed layer depth and the previous thickness of the}}
\DoxyCodeLine{338                 \textcolor{comment}{! neighboring water columns [Z \string~> m].}}
\DoxyCodeLine{339     h\_sum, \&    \textcolor{comment}{! The total thickness of the water column [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{340     hmbl\_prev   \textcolor{comment}{! The previous thickness of the mixed and buffer layers [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{341 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: \&}
\DoxyCodeLine{342     Hsfc, \&     \textcolor{comment}{!   The thickness of the surface region (mixed and buffer}}
\DoxyCodeLine{343                 \textcolor{comment}{! layers before detrainment in to the interior [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{344     max\_bl\_det  \textcolor{comment}{!   If non-\/negative, the maximum amount of entrainment from}}
\DoxyCodeLine{345                 \textcolor{comment}{! the buffer layers that will be allowed this time step [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{346 \textcolor{keywordtype}{  real} :: dHsfc, dHD \textcolor{comment}{! Local copies of nondimensional parameters.}}
\DoxyCodeLine{347 \textcolor{keywordtype}{  real} :: H\_nbr \textcolor{comment}{! A minimum thickness based on neighboring thicknesses [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{348 }
\DoxyCodeLine{349 \textcolor{keywordtype}{  real} :: absf\_x\_H  \textcolor{comment}{! The absolute value of f times the mixed layer thickness [Z T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{350 \textcolor{keywordtype}{  real} :: kU\_star   \textcolor{comment}{! Ustar times the Von Karmen constant [Z T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{351 \textcolor{keywordtype}{  real} :: dt\_\_diag  \textcolor{comment}{! A recaled copy of dt\_diag (if present) or dt [T \string~> s].}}
\DoxyCodeLine{352   \textcolor{keywordtype}{logical} :: write\_diags  \textcolor{comment}{! If true, write out diagnostics with this step.}}
\DoxyCodeLine{353   \textcolor{keywordtype}{logical} :: reset\_diags  \textcolor{comment}{! If true, zero out the accumulated diagnostics.}}
\DoxyCodeLine{354   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: EOSdom \textcolor{comment}{! The i-\/computational domain for the equation of state}}
\DoxyCodeLine{355   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz, nkmb, n}
\DoxyCodeLine{356   \textcolor{keywordtype}{integer} :: nsw    \textcolor{comment}{! The number of bands of penetrating shortwave radiation.}}
\DoxyCodeLine{357 }
\DoxyCodeLine{358   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = gv\%ke}
\DoxyCodeLine{359 }
\DoxyCodeLine{360   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_mixed\_layer: "}//\&}
\DoxyCodeLine{361          \textcolor{stringliteral}{"Module must be initialized before it is used."})}
\DoxyCodeLine{362   \textcolor{keywordflow}{if} (gv\%nkml < 1) \textcolor{keywordflow}{return}}
\DoxyCodeLine{363 }
\DoxyCodeLine{364   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(tv\%eqn\_of\_state)) \textcolor{keyword}{call }mom\_error(fatal, \&}
\DoxyCodeLine{365       \textcolor{stringliteral}{"MOM\_mixed\_layer: Temperature, salinity and an equation of state "}//\&}
\DoxyCodeLine{366       \textcolor{stringliteral}{"must now be used."})}
\DoxyCodeLine{367   \textcolor{keywordflow}{if} (.NOT. \textcolor{keyword}{associated}(fluxes\%ustar)) \textcolor{keyword}{call }mom\_error(fatal, \&}
\DoxyCodeLine{368       \textcolor{stringliteral}{"MOM\_mixed\_layer: No surface TKE fluxes (ustar) defined in mixedlayer!"})}
\DoxyCodeLine{369 }
\DoxyCodeLine{370   nkmb = cs\%nkml+cs\%nkbl}
\DoxyCodeLine{371   inkml = 1.0 / real(cs\%nkml)}
\DoxyCodeLine{372   \textcolor{keywordflow}{if} (cs\%nkml > 1) inkmlm1 = 1.0 / real(cs\%nkml-\/1)}
\DoxyCodeLine{373 }
\DoxyCodeLine{374   irho0 = 1.0 / (gv\%Rho0)}
\DoxyCodeLine{375   dt\_\_diag = dt ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dt\_diag)) dt\_\_diag = dt\_diag}
\DoxyCodeLine{376   idt\_diag = 1.0 / (dt\_\_diag)}
\DoxyCodeLine{377   write\_diags = .true. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(last\_call)) write\_diags = last\_call}
\DoxyCodeLine{378 }
\DoxyCodeLine{379   p\_ref(:) = 0.0 ; p\_ref\_cv(:) = tv\%P\_Ref}
\DoxyCodeLine{380 }
\DoxyCodeLine{381   nsw = cs\%nsw}
\DoxyCodeLine{382 }
\DoxyCodeLine{383   \textcolor{keywordflow}{if} (cs\%limit\_det .or. (cs\%id\_Hsfc\_min > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{384     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{385     \textcolor{keywordflow}{do} j=js-\/1,je+1 ; \textcolor{keywordflow}{do} i=is-\/1,ie+1}
\DoxyCodeLine{386       h\_sum(i,j) = 0.0 ; hmbl\_prev(i,j) = 0.0}
\DoxyCodeLine{387 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{388     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{389     \textcolor{keywordflow}{do} j=js-\/1,je+1}
\DoxyCodeLine{390       \textcolor{keywordflow}{do} k=1,nkmb ; \textcolor{keywordflow}{do} i=is-\/1,ie+1}
\DoxyCodeLine{391         h\_sum(i,j) = h\_sum(i,j) + h\_3d(i,j,k)}
\DoxyCodeLine{392         hmbl\_prev(i,j) = hmbl\_prev(i,j) + h\_3d(i,j,k)}
\DoxyCodeLine{393 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{394       \textcolor{keywordflow}{do} k=nkmb+1,nz ; \textcolor{keywordflow}{do} i=is-\/1,ie+1}
\DoxyCodeLine{395         h\_sum(i,j) = h\_sum(i,j) + h\_3d(i,j,k)}
\DoxyCodeLine{396 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{397 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{398 }
\DoxyCodeLine{399     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_pass)}
\DoxyCodeLine{400     \textcolor{keyword}{call }create\_group\_pass(cs\%pass\_h\_sum\_hmbl\_prev, h\_sum,g\%Domain)}
\DoxyCodeLine{401     \textcolor{keyword}{call }create\_group\_pass(cs\%pass\_h\_sum\_hmbl\_prev, hmbl\_prev,g\%Domain)}
\DoxyCodeLine{402     \textcolor{keyword}{call }do\_group\_pass(cs\%pass\_h\_sum\_hmbl\_prev, g\%Domain)}
\DoxyCodeLine{403     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_pass)}
\DoxyCodeLine{404 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{405 }
\DoxyCodeLine{406   \textcolor{comment}{! Determine whether to zero out diagnostics before accumulation.}}
\DoxyCodeLine{407   reset\_diags = .true.}
\DoxyCodeLine{408   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dt\_diag) .and. write\_diags .and. (dt\_\_diag > dt)) \&}
\DoxyCodeLine{409     reset\_diags = .false.  \textcolor{comment}{! This is the second call to mixedlayer.}}
\DoxyCodeLine{410 }
\DoxyCodeLine{411   \textcolor{keywordflow}{if} (reset\_diags) \textcolor{keywordflow}{then}}
\DoxyCodeLine{412     \textcolor{keywordflow}{if} (cs\%TKE\_diagnostics) \textcolor{keywordflow}{then}}
\DoxyCodeLine{413       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{414       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{415         cs\%diag\_TKE\_wind(i,j) = 0.0 ; cs\%diag\_TKE\_RiBulk(i,j) = 0.0}
\DoxyCodeLine{416         cs\%diag\_TKE\_conv(i,j) = 0.0 ; cs\%diag\_TKE\_pen\_SW(i,j) = 0.0}
\DoxyCodeLine{417         cs\%diag\_TKE\_mixing(i,j) = 0.0 ; cs\%diag\_TKE\_mech\_decay(i,j) = 0.0}
\DoxyCodeLine{418         cs\%diag\_TKE\_conv\_decay(i,j) = 0.0 ; cs\%diag\_TKE\_conv\_s2(i,j) = 0.0}
\DoxyCodeLine{419 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{420 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{421     \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{422       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{423       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{424         cs\%diag\_PE\_detrain(i,j) = 0.0}
\DoxyCodeLine{425 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{426 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{427     \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain2)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{428       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{429       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{430         cs\%diag\_PE\_detrain2(i,j) = 0.0}
\DoxyCodeLine{431 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{432 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{433 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{434 }
\DoxyCodeLine{435   \textcolor{keywordflow}{if} (cs\%ML\_resort) \textcolor{keywordflow}{then}}
\DoxyCodeLine{436     \textcolor{keywordflow}{do} i=is,ie ; h\_ca(i) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{437     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; dke\_ca(i,k) = 0.0 ; ctke(i,k) = 0.0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{438 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{439   max\_bl\_det(:) = -\/1}
\DoxyCodeLine{440   eosdom(:) = eos\_domain(g\%HI)}
\DoxyCodeLine{441 }
\DoxyCodeLine{442   \textcolor{comment}{!\$OMP parallel default(shared) firstprivate(dKE\_CA,cTKE,h\_CA,max\_BL\_det,p\_ref,p\_ref\_cv) \&}}
\DoxyCodeLine{443   \textcolor{comment}{!\$OMP                 private(h,u,v,h\_orig,eps,T,S,opacity\_band,d\_ea,d\_eb,R0,Rcv,ksort, \&}}
\DoxyCodeLine{444   \textcolor{comment}{!\$OMP                         dR0\_dT,dR0\_dS,dRcv\_dT,dRcv\_dS,htot,Ttot,Stot,TKE,Conv\_en, \&}}
\DoxyCodeLine{445   \textcolor{comment}{!\$OMP                         RmixConst,TKE\_river,Pen\_SW\_bnd,netMassInOut,NetMassOut,   \&}}
\DoxyCodeLine{446   \textcolor{comment}{!\$OMP                         Net\_heat,Net\_salt,uhtot,vhtot,R0\_tot,Rcv\_tot,dKE\_FC,      \&}}
\DoxyCodeLine{447   \textcolor{comment}{!\$OMP                         Idecay\_len\_TKE,cMKE,Hsfc,dHsfc,dHD,H\_nbr,kU\_Star,         \&}}
\DoxyCodeLine{448   \textcolor{comment}{!\$OMP                         absf\_x\_H,ebml,eaml)}}
\DoxyCodeLine{449   \textcolor{comment}{!\$OMP do}}
\DoxyCodeLine{450   \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{451     \textcolor{comment}{! Copy the thicknesses and other fields to 2-\/d arrays.}}
\DoxyCodeLine{452     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{453       h(i,k) = h\_3d(i,j,k) ; u(i,k) = u\_3d(i,j,k) ; v(i,k) = v\_3d(i,j,k)}
\DoxyCodeLine{454       h\_orig(i,k) = h\_3d(i,j,k)}
\DoxyCodeLine{455       eps(i,k) = 0.0 ; \textcolor{keywordflow}{if} (k > nkmb) eps(i,k) = gv\%Angstrom\_H}
\DoxyCodeLine{456       t(i,k) = tv\%T(i,j,k) ; s(i,k) = tv\%S(i,j,k)}
\DoxyCodeLine{457 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{458     \textcolor{keywordflow}{if} (nsw>0) \textcolor{keyword}{call }extract\_optics\_slice(optics, j, g, gv, opacity=opacity\_band, opacity\_scale=gv\%H\_to\_m)}
\DoxyCodeLine{459 }
\DoxyCodeLine{460     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{461       d\_ea(i,k) = 0.0 ; d\_eb(i,k) = 0.0}
\DoxyCodeLine{462 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{463 }
\DoxyCodeLine{464     \textcolor{keywordflow}{if} (id\_clock\_eos>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_eos)}
\DoxyCodeLine{465     \textcolor{comment}{! Calculate an estimate of the mid-\/mixed layer pressure [R L2 T-\/2 \string~> Pa]}}
\DoxyCodeLine{466     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%p\_surf)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{467       \textcolor{keywordflow}{do} i=is,ie ; p\_ref(i) = tv\%p\_surf(i,j) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{468     \textcolor{keywordflow}{else}}
\DoxyCodeLine{469       \textcolor{keywordflow}{do} i=is,ie ; p\_ref(i) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{470 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{471     \textcolor{keywordflow}{do} k=1,cs\%nkml ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{472       p\_ref(i) = p\_ref(i) + 0.5*(gv\%H\_to\_RZ*gv\%g\_Earth)*h(i,k)}
\DoxyCodeLine{473 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{474     \textcolor{keyword}{call }calculate\_density\_derivs(t(:,1), s(:,1), p\_ref, dr0\_dt, dr0\_ds, tv\%eqn\_of\_state, eosdom)}
\DoxyCodeLine{475     \textcolor{keyword}{call }calculate\_density\_derivs(t(:,1), s(:,1), p\_ref\_cv, drcv\_dt, drcv\_ds, tv\%eqn\_of\_state, eosdom)}
\DoxyCodeLine{476     \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{477       \textcolor{keyword}{call }calculate\_density(t(:,k), s(:,k), p\_ref, r0(:,k), tv\%eqn\_of\_state, eosdom)}
\DoxyCodeLine{478       \textcolor{keyword}{call }calculate\_density(t(:,k), s(:,k), p\_ref\_cv, rcv(:,k), tv\%eqn\_of\_state, eosdom)}
\DoxyCodeLine{479 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{480     \textcolor{keywordflow}{if} (id\_clock\_eos>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_eos)}
\DoxyCodeLine{481 }
\DoxyCodeLine{482     \textcolor{keywordflow}{if} (cs\%ML\_resort) \textcolor{keywordflow}{then}}
\DoxyCodeLine{483       \textcolor{keywordflow}{if} (id\_clock\_resort>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_resort)}
\DoxyCodeLine{484       \textcolor{keywordflow}{if} (cs\%ML\_presort\_nz\_conv\_adj > 0) \&}
\DoxyCodeLine{485         \textcolor{keyword}{call }convective\_adjustment(h(:,1:), u, v, r0(:,1:), rcv(:,1:), t(:,1:), \&}
\DoxyCodeLine{486                                    s(:,1:), eps, d\_eb, dke\_ca, ctke, j, g, gv, us, cs, \&}
\DoxyCodeLine{487                                    cs\%ML\_presort\_nz\_conv\_adj)}
\DoxyCodeLine{488 }
\DoxyCodeLine{489       \textcolor{keyword}{call }sort\_ml(h(:,1:), r0(:,1:), eps, g, gv, cs, ksort)}
\DoxyCodeLine{490       \textcolor{keywordflow}{if} (id\_clock\_resort>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_resort)}
\DoxyCodeLine{491     \textcolor{keywordflow}{else}}
\DoxyCodeLine{492       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; ksort(i,k) = k ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{493 }
\DoxyCodeLine{494       \textcolor{keywordflow}{if} (id\_clock\_adjustment>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_adjustment)}
\DoxyCodeLine{495       \textcolor{comment}{!  Undergo instantaneous entrainment into the buffer layers and mixed layers}}
\DoxyCodeLine{496       \textcolor{comment}{! to remove hydrostatic instabilities.  Any water that is lighter than}}
\DoxyCodeLine{497       \textcolor{comment}{! currently in the mixed or buffer layer is entrained.}}
\DoxyCodeLine{498       \textcolor{keyword}{call }convective\_adjustment(h(:,1:), u, v, r0(:,1:), rcv(:,1:), t(:,1:), \&}
\DoxyCodeLine{499                                  s(:,1:), eps, d\_eb, dke\_ca, ctke, j, g, gv, us, cs)}
\DoxyCodeLine{500       \textcolor{keywordflow}{do} i=is,ie ; h\_ca(i) = h(i,1) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{501 }
\DoxyCodeLine{502       \textcolor{keywordflow}{if} (id\_clock\_adjustment>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_adjustment)}
\DoxyCodeLine{503 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{504 }
\DoxyCodeLine{505     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes\%lrunoff) .and. cs\%do\_rivermix) \textcolor{keywordflow}{then}}
\DoxyCodeLine{506 }
\DoxyCodeLine{507       \textcolor{comment}{! Here we add an additional source of TKE to the mixed layer where river}}
\DoxyCodeLine{508       \textcolor{comment}{! is present to simulate unresolved estuaries. The TKE input is diagnosed}}
\DoxyCodeLine{509       \textcolor{comment}{! as follows:}}
\DoxyCodeLine{510       \textcolor{comment}{!   TKE\_river[m3 s-\/3] = 0.5*rivermix\_depth*g*Irho0*drho\_ds*}}
\DoxyCodeLine{511       \textcolor{comment}{!                       River*(Samb -\/ Sriver) = CS\%mstar*U\_star\string^3}}
\DoxyCodeLine{512       \textcolor{comment}{! where River is in units of [m s-\/1].}}
\DoxyCodeLine{513       \textcolor{comment}{! Samb = Ambient salinity at the mouth of the estuary}}
\DoxyCodeLine{514       \textcolor{comment}{! rivermix\_depth =  The prescribed depth over which to mix river inflow}}
\DoxyCodeLine{515       \textcolor{comment}{! drho\_ds = The gradient of density wrt salt at the ambient surface salinity.}}
\DoxyCodeLine{516       \textcolor{comment}{! Sriver = 0 (i.e. rivers are assumed to be pure freshwater)}}
\DoxyCodeLine{517       rmixconst = 0.5*cs\%rivermix\_depth * gv\%g\_Earth * irho0**2}
\DoxyCodeLine{518       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{519         tke\_river(i) = max(0.0, rmixconst*dr0\_ds(i)* \&}
\DoxyCodeLine{520                       (fluxes\%lrunoff(i,j) + fluxes\%frunoff(i,j)) * s(i,1))}
\DoxyCodeLine{521 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{522     \textcolor{keywordflow}{else}}
\DoxyCodeLine{523       \textcolor{keywordflow}{do} i=is,ie ; tke\_river(i) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{524 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{525 }
\DoxyCodeLine{526 }
\DoxyCodeLine{527     \textcolor{keywordflow}{if} (id\_clock\_conv>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_conv)}
\DoxyCodeLine{528 }
\DoxyCodeLine{529     \textcolor{comment}{! The surface forcing is contained in the fluxes type.}}
\DoxyCodeLine{530     \textcolor{comment}{! We aggregate the thermodynamic forcing for a time step into the following:}}
\DoxyCodeLine{531     \textcolor{comment}{! netMassInOut = water [H \string~> m or kg m-\/2] added/removed via surface fluxes}}
\DoxyCodeLine{532     \textcolor{comment}{! netMassOut   = water [H \string~> m or kg m-\/2] removed via evaporating surface fluxes}}
\DoxyCodeLine{533     \textcolor{comment}{! net\_heat     = heat via surface fluxes [degC H \string~> degC m or degC kg m-\/2]}}
\DoxyCodeLine{534     \textcolor{comment}{! net\_salt     = salt via surface fluxes [ppt H \string~> dppt m or gSalt m-\/2]}}
\DoxyCodeLine{535     \textcolor{comment}{! Pen\_SW\_bnd   = components to penetrative shortwave radiation}}
\DoxyCodeLine{536     \textcolor{keyword}{call }extractfluxes1d(g, gv, us, fluxes, optics, nsw, j, dt, \&}
\DoxyCodeLine{537                   cs\%H\_limit\_fluxes, cs\%use\_river\_heat\_content, cs\%use\_calving\_heat\_content, \&}
\DoxyCodeLine{538                   h(:,1:), t(:,1:), netmassinout, netmassout, net\_heat, net\_salt, pen\_sw\_bnd,\&}
\DoxyCodeLine{539                   tv, aggregate\_fw\_forcing)}
\DoxyCodeLine{540 }
\DoxyCodeLine{541     \textcolor{comment}{! This subroutine causes the mixed layer to entrain to depth of free convection.}}
\DoxyCodeLine{542     \textcolor{keyword}{call }mixedlayer\_convection(h(:,1:), d\_eb, htot, ttot, stot, uhtot, vhtot, \&}
\DoxyCodeLine{543                                r0\_tot, rcv\_tot, u, v, t(:,1:), s(:,1:),       \&}
\DoxyCodeLine{544                                r0(:,1:), rcv(:,1:), eps,                      \&}
\DoxyCodeLine{545                                dr0\_dt, drcv\_dt, dr0\_ds, drcv\_ds,              \&}
\DoxyCodeLine{546                                netmassinout, netmassout, net\_heat, net\_salt,  \&}
\DoxyCodeLine{547                                nsw, pen\_sw\_bnd, opacity\_band, conv\_en,        \&}
\DoxyCodeLine{548                                dke\_fc, j, ksort, g, gv, us, cs, tv, fluxes, dt, \&}
\DoxyCodeLine{549                                aggregate\_fw\_forcing)}
\DoxyCodeLine{550 }
\DoxyCodeLine{551     \textcolor{keywordflow}{if} (id\_clock\_conv>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_conv)}
\DoxyCodeLine{552 }
\DoxyCodeLine{553     \textcolor{comment}{!   Now the mixed layer undergoes mechanically forced entrainment.}}
\DoxyCodeLine{554     \textcolor{comment}{! The mixed layer may entrain down to the Monin-\/Obukhov depth if the}}
\DoxyCodeLine{555     \textcolor{comment}{! surface is becoming lighter, and is effecti1336vely detraining.}}
\DoxyCodeLine{556 }
\DoxyCodeLine{557     \textcolor{comment}{!    First the TKE at the depth of free convection that is available}}
\DoxyCodeLine{558     \textcolor{comment}{!  to drive mixing is calculated.}}
\DoxyCodeLine{559     \textcolor{keywordflow}{if} (id\_clock\_mech>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_mech)}
\DoxyCodeLine{560 }
\DoxyCodeLine{561     \textcolor{keyword}{call }find\_starting\_tke(htot, h\_ca, fluxes, conv\_en, ctke, dke\_fc, dke\_ca, \&}
\DoxyCodeLine{562                            tke, tke\_river, idecay\_len\_tke, cmke, dt, idt\_diag, \&}
\DoxyCodeLine{563                            j, ksort, g, gv, us, cs)}
\DoxyCodeLine{564 }
\DoxyCodeLine{565     \textcolor{comment}{! Here the mechanically driven entrainment occurs.}}
\DoxyCodeLine{566     \textcolor{keyword}{call }mechanical\_entrainment(h(:,1:), d\_eb, htot, ttot, stot, uhtot, vhtot, \&}
\DoxyCodeLine{567                                 r0\_tot, rcv\_tot, u, v, t(:,1:), s(:,1:), r0(:,1:), rcv(:,1:), eps, dr0\_dt, drcv\_dt, \&}
\DoxyCodeLine{568                                 cmke, idt\_diag, nsw, pen\_sw\_bnd, opacity\_band, tke, \&}
\DoxyCodeLine{569                                 idecay\_len\_tke, j, ksort, g, gv, us, cs)}
\DoxyCodeLine{570 }
\DoxyCodeLine{571     \textcolor{keyword}{call }absorbremainingsw(g, gv, us, h(:,1:), opacity\_band, nsw, optics, j, dt, \&}
\DoxyCodeLine{572                            cs\%H\_limit\_fluxes, cs\%correct\_absorption, cs\%absorb\_all\_SW, \&}
\DoxyCodeLine{573                            t(:,1:), pen\_sw\_bnd, eps, ksort, htot, ttot)}
\DoxyCodeLine{574 }
\DoxyCodeLine{575     \textcolor{keywordflow}{if} (cs\%TKE\_diagnostics) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{576       cs\%diag\_TKE\_mech\_decay(i,j) = cs\%diag\_TKE\_mech\_decay(i,j) -\/ idt\_diag * tke(i)}
\DoxyCodeLine{577 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{578     \textcolor{keywordflow}{if} (id\_clock\_mech>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_mech)}
\DoxyCodeLine{579 }
\DoxyCodeLine{580     \textcolor{comment}{! Calculate the homogeneous mixed layer properties and store them in layer 0.}}
\DoxyCodeLine{581     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (htot(i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{582       ih = 1.0 / htot(i)}
\DoxyCodeLine{583       r0(i,0) = r0\_tot(i) * ih ; rcv(i,0) = rcv\_tot(i) * ih}
\DoxyCodeLine{584       t(i,0) = ttot(i) * ih ; s(i,0) = stot(i) * ih}
\DoxyCodeLine{585       h(i,0) = htot(i)}
\DoxyCodeLine{586     \textcolor{keywordflow}{else} \textcolor{comment}{! This may not ever be needed?}}
\DoxyCodeLine{587       t(i,0) = t(i,1) ; s(i,0) = s(i,1) ; r0(i,0) = r0(i,1) ; rcv(i,0) = rcv(i,1)}
\DoxyCodeLine{588       h(i,0) = htot(i)}
\DoxyCodeLine{589 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{590     \textcolor{keywordflow}{if} (write\_diags .and. \textcolor{keyword}{allocated}(cs\%ML\_depth)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{591       cs\%ML\_depth(i,j) = h(i,0) * gv\%H\_to\_m  \textcolor{comment}{! Rescale the diagnostic.}}
\DoxyCodeLine{592 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{593     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(hml)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{594       hml(i,j) = g\%mask2dT(i,j) * (h(i,0) * gv\%H\_to\_Z) \textcolor{comment}{! Rescale the diagnostic for output.}}
\DoxyCodeLine{595 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{596 }
\DoxyCodeLine{597 \textcolor{comment}{! At this point, return water to the original layers, but constrained to}}
\DoxyCodeLine{598 \textcolor{comment}{! still be sorted.  After this point, all the water that is in massive}}
\DoxyCodeLine{599 \textcolor{comment}{! interior layers will be denser than water remaining in the mixed-\/ and}}
\DoxyCodeLine{600 \textcolor{comment}{! buffer-\/layers.  To achieve this, some of these variable density layers}}
\DoxyCodeLine{601 \textcolor{comment}{! might be split between two isopycnal layers that are denser than new}}
\DoxyCodeLine{602 \textcolor{comment}{! mixed layer or any remaining water from the old mixed-\/ or buffer-\/layers.}}
\DoxyCodeLine{603 \textcolor{comment}{! Alternately, if there are fewer than nkbl of the old buffer or mixed layers}}
\DoxyCodeLine{604 \textcolor{comment}{! with any mass, relatively light interior layers might be transferred to}}
\DoxyCodeLine{605 \textcolor{comment}{! these unused layers (but not currently in the code).}}
\DoxyCodeLine{606 }
\DoxyCodeLine{607     \textcolor{keywordflow}{if} (cs\%ML\_resort) \textcolor{keywordflow}{then}}
\DoxyCodeLine{608       \textcolor{keywordflow}{if} (id\_clock\_resort>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_resort)}
\DoxyCodeLine{609       \textcolor{keyword}{call }resort\_ml(h(:,0:), t(:,0:), s(:,0:), r0(:,0:), rcv(:,0:), gv\%Rlay(:), eps, \&}
\DoxyCodeLine{610                      d\_ea, d\_eb, ksort, g, gv, cs, dr0\_dt, dr0\_ds, drcv\_dt, drcv\_ds)}
\DoxyCodeLine{611       \textcolor{keywordflow}{if} (id\_clock\_resort>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_resort)}
\DoxyCodeLine{612 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{613 }
\DoxyCodeLine{614     \textcolor{keywordflow}{if} (cs\%limit\_det .or. (cs\%id\_Hsfc\_max > 0) .or. (cs\%id\_Hsfc\_min > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{615       \textcolor{keywordflow}{do} i=is,ie ; hsfc(i) = h(i,0) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{616       \textcolor{keywordflow}{do} k=1,nkmb ; \textcolor{keywordflow}{do} i=is,ie ; hsfc(i) = hsfc(i) + h(i,k) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{617 }
\DoxyCodeLine{618       \textcolor{keywordflow}{if} (cs\%limit\_det .or. (cs\%id\_Hsfc\_min > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{619         dhsfc = cs\%lim\_det\_dH\_sfc ; dhd = cs\%lim\_det\_dH\_bathy}
\DoxyCodeLine{620         \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{621           h\_nbr = min(dhsfc*max(hmbl\_prev(i-\/1,j), hmbl\_prev(i+1,j), \&}
\DoxyCodeLine{622                                 hmbl\_prev(i,j-\/1), hmbl\_prev(i,j+1)), \&}
\DoxyCodeLine{623                       max(hmbl\_prev(i-\/1,j) -\/ dhd*min(h\_sum(i,j),h\_sum(i-\/1,j)), \&}
\DoxyCodeLine{624                           hmbl\_prev(i+1,j) -\/ dhd*min(h\_sum(i,j),h\_sum(i+1,j)), \&}
\DoxyCodeLine{625                           hmbl\_prev(i,j-\/1) -\/ dhd*min(h\_sum(i,j),h\_sum(i,j-\/1)), \&}
\DoxyCodeLine{626                           hmbl\_prev(i,j+1) -\/ dhd*min(h\_sum(i,j),h\_sum(i,j+1))) )}
\DoxyCodeLine{627 }
\DoxyCodeLine{628           hsfc\_min(i,j) = gv\%H\_to\_Z * max(h(i,0), min(hsfc(i), h\_nbr))}
\DoxyCodeLine{629 }
\DoxyCodeLine{630           \textcolor{keywordflow}{if} (cs\%limit\_det) max\_bl\_det(i) = max(0.0, hsfc(i)-\/h\_nbr)}
\DoxyCodeLine{631 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{632 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{633 }
\DoxyCodeLine{634       \textcolor{keywordflow}{if} (cs\%id\_Hsfc\_max > 0) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{635         hsfc\_max(i,j) = gv\%H\_to\_Z * hsfc(i)}
\DoxyCodeLine{636 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{637 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{638 }
\DoxyCodeLine{639 \textcolor{comment}{! Move water left in the former mixed layer into the buffer layer and}}
\DoxyCodeLine{640 \textcolor{comment}{! from the buffer layer into the interior.  These steps might best be}}
\DoxyCodeLine{641 \textcolor{comment}{! treated in conjuction.}}
\DoxyCodeLine{642     \textcolor{keywordflow}{if} (id\_clock\_detrain>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_detrain)}
\DoxyCodeLine{643     \textcolor{keywordflow}{if} (cs\%nkbl == 1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{644       \textcolor{keyword}{call }mixedlayer\_detrain\_1(h(:,0:), t(:,0:), s(:,0:), r0(:,0:), rcv(:,0:), \&}
\DoxyCodeLine{645                                 gv\%Rlay(:), dt, dt\_\_diag, d\_ea, d\_eb, j, g, gv, us, cs, \&}
\DoxyCodeLine{646                                 drcv\_dt, drcv\_ds, max\_bl\_det)}
\DoxyCodeLine{647     \textcolor{keywordflow}{elseif} (cs\%nkbl == 2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{648       \textcolor{keyword}{call }mixedlayer\_detrain\_2(h(:,0:), t(:,0:), s(:,0:), r0(:,0:), rcv(:,0:), \&}
\DoxyCodeLine{649                                 gv\%Rlay(:), dt, dt\_\_diag, d\_ea, j, g, gv, us, cs, \&}
\DoxyCodeLine{650                                 dr0\_dt, dr0\_ds, drcv\_dt, drcv\_ds, max\_bl\_det)}
\DoxyCodeLine{651     \textcolor{keywordflow}{else} \textcolor{comment}{! CS\%nkbl not = 1 or 2}}
\DoxyCodeLine{652       \textcolor{comment}{! This code only works with 1 or 2 buffer layers.}}
\DoxyCodeLine{653       \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_mixed\_layer: CS\%nkbl must be 1 or 2 for now."})}
\DoxyCodeLine{654 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{655     \textcolor{keywordflow}{if} (id\_clock\_detrain>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_detrain)}
\DoxyCodeLine{656 }
\DoxyCodeLine{657 }
\DoxyCodeLine{658     \textcolor{keywordflow}{if} (cs\%id\_Hsfc\_used > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{659       \textcolor{keywordflow}{do} i=is,ie ; hsfc\_used(i,j) = gv\%H\_to\_Z * h(i,0) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{660       \textcolor{keywordflow}{do} k=cs\%nkml+1,nkmb ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{661         hsfc\_used(i,j) = hsfc\_used(i,j) + gv\%H\_to\_Z * h(i,k)}
\DoxyCodeLine{662 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{663 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{664 }
\DoxyCodeLine{665 \textcolor{comment}{! Now set the properties of the layers in the mixed layer in the original}}
\DoxyCodeLine{666 \textcolor{comment}{! 3-\/d variables.}}
\DoxyCodeLine{667     \textcolor{keywordflow}{if} (cs\%Resolve\_Ekman .and. (cs\%nkml>1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{668       \textcolor{comment}{! The thickness of the topmost piece of the mixed layer is given by}}
\DoxyCodeLine{669       \textcolor{comment}{! h\_1 = H / (3 + sqrt(|f|*H\string^2/2*nu\_max)), which asymptotes to the Ekman}}
\DoxyCodeLine{670       \textcolor{comment}{! layer depth and 1/3 of the mixed layer depth.  This curve has been}}
\DoxyCodeLine{671       \textcolor{comment}{! determined to maximize the impact of the Ekman transport in the mixed}}
\DoxyCodeLine{672       \textcolor{comment}{! layer TKE budget with nkml=2.  With nkml=3, this should also be used,}}
\DoxyCodeLine{673       \textcolor{comment}{! as the third piece will then optimally describe mixed layer}}
\DoxyCodeLine{674       \textcolor{comment}{! restratification.  For nkml>=4 the whole strategy should be revisited.}}
\DoxyCodeLine{675       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{676         ku\_star = 0.41*fluxes\%ustar(i,j) \textcolor{comment}{! Maybe could be replaced with u*+w*?}}
\DoxyCodeLine{677         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes\%ustar\_shelf) .and. \&}
\DoxyCodeLine{678             \textcolor{keyword}{associated}(fluxes\%frac\_shelf\_h)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{679           \textcolor{keywordflow}{if} (fluxes\%frac\_shelf\_h(i,j) > 0.0) \&}
\DoxyCodeLine{680             ku\_star = (1.0 -\/ fluxes\%frac\_shelf\_h(i,j)) * ku\_star + \&}
\DoxyCodeLine{681                       fluxes\%frac\_shelf\_h(i,j) * (0.41*fluxes\%ustar\_shelf(i,j))}
\DoxyCodeLine{682 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{683         absf\_x\_h = 0.25 * gv\%H\_to\_Z * h(i,0) * \&}
\DoxyCodeLine{684             ((abs(g\%CoriolisBu(i,j)) + abs(g\%CoriolisBu(i-\/1,j-\/1))) + \&}
\DoxyCodeLine{685              (abs(g\%CoriolisBu(i,j-\/1)) + abs(g\%CoriolisBu(i-\/1,j))))}
\DoxyCodeLine{686         \textcolor{comment}{! If the mixed layer vertical viscosity specification is changed in}}
\DoxyCodeLine{687         \textcolor{comment}{! MOM\_vert\_friction.F90, this line will have to be modified accordingly.}}
\DoxyCodeLine{688         h\_3d(i,j,1) = h(i,0) / (3.0 + sqrt(absf\_x\_h*(absf\_x\_h + 2.0*ku\_star) / ku\_star**2))}
\DoxyCodeLine{689         \textcolor{keywordflow}{do} k=2,cs\%nkml}
\DoxyCodeLine{690           \textcolor{comment}{! The other layers are evenly distributed through the mixed layer.}}
\DoxyCodeLine{691           h\_3d(i,j,k) = (h(i,0)-\/h\_3d(i,j,1)) * inkmlm1}
\DoxyCodeLine{692           d\_ea(i,k) = d\_ea(i,k) + h\_3d(i,j,k)}
\DoxyCodeLine{693           d\_ea(i,1) = d\_ea(i,1) -\/ h\_3d(i,j,k)}
\DoxyCodeLine{694 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{695 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{696     \textcolor{keywordflow}{else}}
\DoxyCodeLine{697       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{698         h\_3d(i,j,1) = h(i,0) * inkml}
\DoxyCodeLine{699 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{700       \textcolor{keywordflow}{do} k=2,cs\%nkml ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{701         h\_3d(i,j,k) = h(i,0) * inkml}
\DoxyCodeLine{702         d\_ea(i,k) = d\_ea(i,k) + h\_3d(i,j,k)}
\DoxyCodeLine{703         d\_ea(i,1) = d\_ea(i,1) -\/ h\_3d(i,j,k)}
\DoxyCodeLine{704 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{705 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{706     \textcolor{keywordflow}{do} i=is,ie ; h(i,0) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{707     \textcolor{keywordflow}{do} k=1,cs\%nkml ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{708       tv\%T(i,j,k) = t(i,0) ; tv\%S(i,j,k) = s(i,0)}
\DoxyCodeLine{709 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{710 }
\DoxyCodeLine{711     \textcolor{comment}{! These sum needs to be done in the original layer space.}}
\DoxyCodeLine{712 }
\DoxyCodeLine{713     \textcolor{comment}{! The treatment of layer 1 is atypical because evaporation shows up as}}
\DoxyCodeLine{714     \textcolor{comment}{! negative ea(i,1), and because all precipitation goes straight into layer 1.}}
\DoxyCodeLine{715     \textcolor{comment}{! The code is ordered so that any roundoff errors in ea are lost the surface.}}
\DoxyCodeLine{716 \textcolor{comment}{!    do i=is,ie ; eaml(i,1) = 0.0 ; enddo}}
\DoxyCodeLine{717 \textcolor{comment}{!    do k=2,nz ; do i=is,ie ; eaml(i,k) = eaml(i,k-\/1) -\/ d\_ea(i,k-\/1) ; enddo ; enddo}}
\DoxyCodeLine{718 \textcolor{comment}{!    do i=is,ie ; eaml(i,1) = netMassInOut(i) ; enddo}}
\DoxyCodeLine{719 }
\DoxyCodeLine{720 }
\DoxyCodeLine{721     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{722 \textcolor{comment}{! eaml(i,nz) is derived from  h(i,nz) -\/ h\_orig(i,nz) = eaml(i,nz) -\/ ebml(i,nz-\/1)}}
\DoxyCodeLine{723       ebml(i,nz) = 0.0}
\DoxyCodeLine{724       eaml(i,nz) = (h(i,nz) -\/ h\_orig(i,nz)) -\/ d\_eb(i,nz)}
\DoxyCodeLine{725 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{726     \textcolor{keywordflow}{do} k=nz-\/1,1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{727       ebml(i,k) = ebml(i,k+1) -\/ d\_eb(i,k+1)}
\DoxyCodeLine{728       eaml(i,k) = eaml(i,k+1) + d\_ea(i,k)}
\DoxyCodeLine{729 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{730     \textcolor{keywordflow}{do} i=is,ie ; eaml(i,1) = netmassinout(i) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{731 }
\DoxyCodeLine{732     \textcolor{comment}{! Copy the interior thicknesses and other fields back to the 3-\/d arrays.}}
\DoxyCodeLine{733     \textcolor{keywordflow}{do} k=cs\%nkml+1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{734       h\_3d(i,j,k) = h(i,k); tv\%T(i,j,k) = t(i,k) ; tv\%S(i,j,k) = s(i,k)}
\DoxyCodeLine{735 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{736 }
\DoxyCodeLine{737     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{738       ea(i,j,k) = ea(i,j,k) + eaml(i,k)}
\DoxyCodeLine{739       eb(i,j,k) = eb(i,j,k) + ebml(i,k)}
\DoxyCodeLine{740 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{741 }
\DoxyCodeLine{742     \textcolor{keywordflow}{if} (cs\%id\_h\_mismatch > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{743       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{744         h\_miss(i,j) = gv\%H\_to\_Z * abs(h\_3d(i,j,1) -\/ (h\_orig(i,1) + \&}
\DoxyCodeLine{745           (eaml(i,1) + (ebml(i,1) -\/ eaml(i,1+1)))))}
\DoxyCodeLine{746 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{747       \textcolor{keywordflow}{do} k=2,nz-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{748         h\_miss(i,j) = h\_miss(i,j) + gv\%H\_to\_Z * abs(h\_3d(i,j,k) -\/ (h\_orig(i,k) + \&}
\DoxyCodeLine{749           ((eaml(i,k) -\/ ebml(i,k-\/1)) + (ebml(i,k) -\/ eaml(i,k+1)))))}
\DoxyCodeLine{750 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{751       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{752         h\_miss(i,j) = h\_miss(i,j) + gv\%H\_to\_Z * abs(h\_3d(i,j,nz) -\/ (h\_orig(i,nz) + \&}
\DoxyCodeLine{753           ((eaml(i,nz) -\/ ebml(i,nz-\/1)) + ebml(i,nz))))}
\DoxyCodeLine{754 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{755 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{756 }
\DoxyCodeLine{757 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! j loop}}
\DoxyCodeLine{758   \textcolor{comment}{!\$OMP end parallel}}
\DoxyCodeLine{759 }
\DoxyCodeLine{760   \textcolor{comment}{! Whenever thickness changes let the diag manager know, target grids}}
\DoxyCodeLine{761   \textcolor{comment}{! for vertical remapping may need to be regenerated.}}
\DoxyCodeLine{762   \textcolor{comment}{! This needs to happen after the H update and before the next post\_data.}}
\DoxyCodeLine{763   \textcolor{keyword}{call }diag\_update\_remap\_grids(cs\%diag)}
\DoxyCodeLine{764 }
\DoxyCodeLine{765 }
\DoxyCodeLine{766   \textcolor{keywordflow}{if} (write\_diags) \textcolor{keywordflow}{then}}
\DoxyCodeLine{767     \textcolor{keywordflow}{if} (cs\%id\_ML\_depth > 0) \&}
\DoxyCodeLine{768       \textcolor{keyword}{call }post\_data(cs\%id\_ML\_depth, cs\%ML\_depth, cs\%diag)}
\DoxyCodeLine{769     \textcolor{keywordflow}{if} (cs\%id\_TKE\_wind > 0) \&}
\DoxyCodeLine{770       \textcolor{keyword}{call }post\_data(cs\%id\_TKE\_wind, cs\%diag\_TKE\_wind, cs\%diag)}
\DoxyCodeLine{771     \textcolor{keywordflow}{if} (cs\%id\_TKE\_RiBulk > 0) \&}
\DoxyCodeLine{772       \textcolor{keyword}{call }post\_data(cs\%id\_TKE\_RiBulk, cs\%diag\_TKE\_RiBulk, cs\%diag)}
\DoxyCodeLine{773     \textcolor{keywordflow}{if} (cs\%id\_TKE\_conv > 0) \&}
\DoxyCodeLine{774       \textcolor{keyword}{call }post\_data(cs\%id\_TKE\_conv, cs\%diag\_TKE\_conv, cs\%diag)}
\DoxyCodeLine{775     \textcolor{keywordflow}{if} (cs\%id\_TKE\_pen\_SW > 0) \&}
\DoxyCodeLine{776       \textcolor{keyword}{call }post\_data(cs\%id\_TKE\_pen\_SW, cs\%diag\_TKE\_pen\_SW, cs\%diag)}
\DoxyCodeLine{777     \textcolor{keywordflow}{if} (cs\%id\_TKE\_mixing > 0) \&}
\DoxyCodeLine{778       \textcolor{keyword}{call }post\_data(cs\%id\_TKE\_mixing, cs\%diag\_TKE\_mixing, cs\%diag)}
\DoxyCodeLine{779     \textcolor{keywordflow}{if} (cs\%id\_TKE\_mech\_decay > 0) \&}
\DoxyCodeLine{780       \textcolor{keyword}{call }post\_data(cs\%id\_TKE\_mech\_decay, cs\%diag\_TKE\_mech\_decay, cs\%diag)}
\DoxyCodeLine{781     \textcolor{keywordflow}{if} (cs\%id\_TKE\_conv\_decay > 0) \&}
\DoxyCodeLine{782       \textcolor{keyword}{call }post\_data(cs\%id\_TKE\_conv\_decay, cs\%diag\_TKE\_conv\_decay, cs\%diag)}
\DoxyCodeLine{783     \textcolor{keywordflow}{if} (cs\%id\_TKE\_conv\_s2 > 0) \&}
\DoxyCodeLine{784       \textcolor{keyword}{call }post\_data(cs\%id\_TKE\_conv\_s2, cs\%diag\_TKE\_conv\_s2, cs\%diag)}
\DoxyCodeLine{785     \textcolor{keywordflow}{if} (cs\%id\_PE\_detrain > 0) \&}
\DoxyCodeLine{786       \textcolor{keyword}{call }post\_data(cs\%id\_PE\_detrain, cs\%diag\_PE\_detrain, cs\%diag)}
\DoxyCodeLine{787     \textcolor{keywordflow}{if} (cs\%id\_PE\_detrain2 > 0) \&}
\DoxyCodeLine{788       \textcolor{keyword}{call }post\_data(cs\%id\_PE\_detrain2, cs\%diag\_PE\_detrain2, cs\%diag)}
\DoxyCodeLine{789     \textcolor{keywordflow}{if} (cs\%id\_h\_mismatch > 0) \&}
\DoxyCodeLine{790       \textcolor{keyword}{call }post\_data(cs\%id\_h\_mismatch, h\_miss, cs\%diag)}
\DoxyCodeLine{791     \textcolor{keywordflow}{if} (cs\%id\_Hsfc\_used > 0) \&}
\DoxyCodeLine{792       \textcolor{keyword}{call }post\_data(cs\%id\_Hsfc\_used, hsfc\_used, cs\%diag)}
\DoxyCodeLine{793     \textcolor{keywordflow}{if} (cs\%id\_Hsfc\_max > 0) \&}
\DoxyCodeLine{794       \textcolor{keyword}{call }post\_data(cs\%id\_Hsfc\_max, hsfc\_max, cs\%diag)}
\DoxyCodeLine{795     \textcolor{keywordflow}{if} (cs\%id\_Hsfc\_min > 0) \&}
\DoxyCodeLine{796       \textcolor{keyword}{call }post\_data(cs\%id\_Hsfc\_min, hsfc\_min, cs\%diag)}
\DoxyCodeLine{797 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{798 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_a14f3b36851c81d60fb31ff86870a7d54}\label{namespacemom__bulk__mixed__layer_a14f3b36851c81d60fb31ff86870a7d54}} 
\index{mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}!bulkmixedlayer\_init@{bulkmixedlayer\_init}}
\index{bulkmixedlayer\_init@{bulkmixedlayer\_init}!mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}}
\doxysubsubsection{\texorpdfstring{bulkmixedlayer\_init()}{bulkmixedlayer\_init()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+bulk\+\_\+mixed\+\_\+layer\+::bulkmixedlayer\+\_\+init (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), intent(in), target}]{Time,  }\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(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file,  }\item[{type(diag\+\_\+ctrl), intent(inout), target}]{diag,  }\item[{type(\mbox{\hyperlink{structmom__bulk__mixed__layer_1_1bulkmixedlayer__cs}{bulkmixedlayer\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



This subroutine initializes the M\+OM bulk mixed layer module. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em time} & The model\textquotesingle{}s clock with the current time. \\
\hline
\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 param\+\_\+file} & A structure to parse for run-\/time parameters. \\
\hline
\mbox{\texttt{ in,out}}  & {\em diag} & A structure that is used to regulate diagnostic output. \\
\hline
 & {\em cs} & A pointer that is set to point to the control structure for this module. \\
\hline
\end{DoxyParams}


Definition at line 3390 of file M\+O\+M\+\_\+bulk\+\_\+mixed\+\_\+layer.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{3390   \textcolor{keywordtype}{type}(time\_type), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{ !< The model's clock with the current time.}}
\DoxyCodeLine{3391   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{3392   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{3393   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}}
\DoxyCodeLine{3394   \textcolor{keywordtype}{type}(param\_file\_type),   \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< A structure to parse for run-\/time}}
\DoxyCodeLine{3395 \textcolor{comment}{                                                 !! parameters.}}
\DoxyCodeLine{3396   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{ !< A structure that is used to regulate diagnostic}}
\DoxyCodeLine{3397 \textcolor{comment}{                                                 !! output.}}
\DoxyCodeLine{3398   \textcolor{keywordtype}{type}(bulkmixedlayer\_CS), \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< A pointer that is set to point to the control}}
\DoxyCodeLine{3399 \textcolor{comment}{                                                 !! structure for this module.}}
\DoxyCodeLine{3400 \textcolor{comment}{! Arguments: Time -\/ The current model time.}}
\DoxyCodeLine{3401 \textcolor{comment}{!  (in)      G -\/ The ocean's grid structure.}}
\DoxyCodeLine{3402 \textcolor{comment}{!  (in)      GV -\/ The ocean's vertical grid structure.}}
\DoxyCodeLine{3403 \textcolor{comment}{!  (in)      param\_file -\/ A structure indicating the open file to parse for}}
\DoxyCodeLine{3404 \textcolor{comment}{!                         model parameter values.}}
\DoxyCodeLine{3405 \textcolor{comment}{!  (in)      diag -\/ A structure that is used to regulate diagnostic output.}}
\DoxyCodeLine{3406 \textcolor{comment}{!  (in/out)  CS -\/ A pointer that is set to point to the control structure}}
\DoxyCodeLine{3407 \textcolor{comment}{!                  for this module}}
\DoxyCodeLine{3408 \textcolor{comment}{! This include declares and sets the variable "version".}}
\DoxyCodeLine{3409 \textcolor{preprocessor}{\#include "version\_variable.h"}}
\DoxyCodeLine{3410 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_mixed\_layer"}  \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{3411 \textcolor{keywordtype}{  real} :: BL\_detrain\_time\_dflt \textcolor{comment}{! The default value for BUFFER\_LAY\_DETRAIN\_TIME [s]}}
\DoxyCodeLine{3412 \textcolor{keywordtype}{  real} :: omega\_frac\_dflt, ustar\_min\_dflt, Hmix\_min\_m}
\DoxyCodeLine{3413   \textcolor{keywordtype}{integer} :: isd, ied, jsd, jed}
\DoxyCodeLine{3414   \textcolor{keywordtype}{logical} :: use\_temperature, use\_omega}
\DoxyCodeLine{3415   isd = g\%isd ; ied = g\%ied ; jsd = g\%jsd ; jed = g\%jed}
\DoxyCodeLine{3416 }
\DoxyCodeLine{3417   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3418     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"mixedlayer\_init called with an associated"}// \&}
\DoxyCodeLine{3419                             \textcolor{stringliteral}{"associated control structure."})}
\DoxyCodeLine{3420     \textcolor{keywordflow}{return}}
\DoxyCodeLine{3421   \textcolor{keywordflow}{else} ; \textcolor{keyword}{allocate}(cs) ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{3422 }
\DoxyCodeLine{3423   cs\%diag => diag}
\DoxyCodeLine{3424   cs\%Time => time}
\DoxyCodeLine{3425 }
\DoxyCodeLine{3426   \textcolor{keywordflow}{if} (gv\%nkml < 1) \textcolor{keywordflow}{return}}
\DoxyCodeLine{3427 }
\DoxyCodeLine{3428 \textcolor{comment}{! Set default, read and log parameters}}
\DoxyCodeLine{3429   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \textcolor{stringliteral}{""})}
\DoxyCodeLine{3430 }
\DoxyCodeLine{3431   cs\%nkml = gv\%nkml}
\DoxyCodeLine{3432   \textcolor{keyword}{call }log\_param(param\_file, mdl, \textcolor{stringliteral}{"NKML"}, cs\%nkml, \&}
\DoxyCodeLine{3433                  \textcolor{stringliteral}{"The number of sublayers within the mixed layer if "}//\&}
\DoxyCodeLine{3434                  \textcolor{stringliteral}{"BULKMIXEDLAYER is true."}, units=\textcolor{stringliteral}{"nondim"}, default=2)}
\DoxyCodeLine{3435   cs\%nkbl = gv\%nk\_rho\_varies -\/ gv\%nkml}
\DoxyCodeLine{3436   \textcolor{keyword}{call }log\_param(param\_file, mdl, \textcolor{stringliteral}{"NKBL"}, cs\%nkbl, \&}
\DoxyCodeLine{3437                  \textcolor{stringliteral}{"The number of variable density buffer layers if "}//\&}
\DoxyCodeLine{3438                  \textcolor{stringliteral}{"BULKMIXEDLAYER is true."}, units=\textcolor{stringliteral}{"nondim"}, default=2)}
\DoxyCodeLine{3439   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MSTAR"}, cs\%mstar, \&}
\DoxyCodeLine{3440                  \textcolor{stringliteral}{"The ratio of the friction velocity cubed to the TKE "}//\&}
\DoxyCodeLine{3441                  \textcolor{stringliteral}{"input to the mixed layer."}, units=\textcolor{stringliteral}{"nondim"}, default=1.2)}
\DoxyCodeLine{3442   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NSTAR"}, cs\%nstar, \&}
\DoxyCodeLine{3443                  \textcolor{stringliteral}{"The portion of the buoyant potential energy imparted by "}//\&}
\DoxyCodeLine{3444                  \textcolor{stringliteral}{"surface fluxes that is available to drive entrainment "}//\&}
\DoxyCodeLine{3445                  \textcolor{stringliteral}{"at the base of mixed layer when that energy is positive."}, \&}
\DoxyCodeLine{3446                  units=\textcolor{stringliteral}{"nondim"}, default=0.15)}
\DoxyCodeLine{3447   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BULK\_RI\_ML"}, cs\%bulk\_Ri\_ML, \&}
\DoxyCodeLine{3448                  \textcolor{stringliteral}{"The efficiency with which mean kinetic energy released "}//\&}
\DoxyCodeLine{3449                  \textcolor{stringliteral}{"by mechanically forced entrainment of the mixed layer "}//\&}
\DoxyCodeLine{3450                  \textcolor{stringliteral}{"is converted to turbulent kinetic energy."}, units=\textcolor{stringliteral}{"nondim"},\&}
\DoxyCodeLine{3451                  fail\_if\_missing=.true.)}
\DoxyCodeLine{3452   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ABSORB\_ALL\_SW"}, cs\%absorb\_all\_sw, \&}
\DoxyCodeLine{3453                  \textcolor{stringliteral}{"If true,  all shortwave radiation is absorbed by the "}//\&}
\DoxyCodeLine{3454                  \textcolor{stringliteral}{"ocean, instead of passing through to the bottom mud."}, \&}
\DoxyCodeLine{3455                  default=.false.)}
\DoxyCodeLine{3456   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"TKE\_DECAY"}, cs\%TKE\_decay, \&}
\DoxyCodeLine{3457                  \textcolor{stringliteral}{"TKE\_DECAY relates the vertical rate of decay of the "}//\&}
\DoxyCodeLine{3458                  \textcolor{stringliteral}{"TKE available for mechanical entrainment to the natural "}//\&}
\DoxyCodeLine{3459                  \textcolor{stringliteral}{"Ekman depth."}, units=\textcolor{stringliteral}{"nondim"}, default=2.5)}
\DoxyCodeLine{3460   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NSTAR2"}, cs\%nstar2, \&}
\DoxyCodeLine{3461                  \textcolor{stringliteral}{"The portion of any potential energy released by "}//\&}
\DoxyCodeLine{3462                  \textcolor{stringliteral}{"convective adjustment that is available to drive "}//\&}
\DoxyCodeLine{3463                  \textcolor{stringliteral}{"entrainment at the base of mixed layer. By default "}//\&}
\DoxyCodeLine{3464                  \textcolor{stringliteral}{"NSTAR2=NSTAR."}, units=\textcolor{stringliteral}{"nondim"}, default=cs\%nstar)}
\DoxyCodeLine{3465   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BULK\_RI\_CONVECTIVE"}, cs\%bulk\_Ri\_convective, \&}
\DoxyCodeLine{3466                  \textcolor{stringliteral}{"The efficiency with which convectively released mean "}//\&}
\DoxyCodeLine{3467                  \textcolor{stringliteral}{"kinetic energy is converted to turbulent kinetic "}//\&}
\DoxyCodeLine{3468                  \textcolor{stringliteral}{"energy.  By default BULK\_RI\_CONVECTIVE=BULK\_RI\_ML."}, \&}
\DoxyCodeLine{3469                  units=\textcolor{stringliteral}{"nondim"}, default=cs\%bulk\_Ri\_ML)}
\DoxyCodeLine{3470   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HMIX\_MIN"}, cs\%Hmix\_min, \&}
\DoxyCodeLine{3471                  \textcolor{stringliteral}{"The minimum mixed layer depth if the mixed layer depth "}//\&}
\DoxyCodeLine{3472                  \textcolor{stringliteral}{"is determined dynamically."}, units=\textcolor{stringliteral}{"m"}, default=0.0, scale=gv\%m\_to\_H, \&}
\DoxyCodeLine{3473                  unscaled=hmix\_min\_m)}
\DoxyCodeLine{3474 }
\DoxyCodeLine{3475   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"LIMIT\_BUFFER\_DETRAIN"}, cs\%limit\_det, \&}
\DoxyCodeLine{3476                  \textcolor{stringliteral}{"If true, limit the detrainment from the buffer layers "}//\&}
\DoxyCodeLine{3477                  \textcolor{stringliteral}{"to not be too different from the neighbors."}, default=.false.)}
\DoxyCodeLine{3478   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ALLOWED\_DETRAIN\_TEMP\_CHG"}, cs\%Allowed\_T\_chg, \&}
\DoxyCodeLine{3479                  \textcolor{stringliteral}{"The amount by which temperature is allowed to exceed "}//\&}
\DoxyCodeLine{3480                  \textcolor{stringliteral}{"previous values during detrainment."}, units=\textcolor{stringliteral}{"K"}, default=0.5)}
\DoxyCodeLine{3481   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ALLOWED\_DETRAIN\_SALT\_CHG"}, cs\%Allowed\_S\_chg, \&}
\DoxyCodeLine{3482                  \textcolor{stringliteral}{"The amount by which salinity is allowed to exceed "}//\&}
\DoxyCodeLine{3483                  \textcolor{stringliteral}{"previous values during detrainment."}, units=\textcolor{stringliteral}{"PSU"}, default=0.1)}
\DoxyCodeLine{3484   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_DT\_DS\_WEIGHT"}, cs\%dT\_dS\_wt, \&}
\DoxyCodeLine{3485                  \textcolor{stringliteral}{"When forced to extrapolate T \& S to match the layer "}//\&}
\DoxyCodeLine{3486                  \textcolor{stringliteral}{"densities, this factor (in deg C / PSU) is combined "}//\&}
\DoxyCodeLine{3487                  \textcolor{stringliteral}{"with the derivatives of density with T \& S to determine "}//\&}
\DoxyCodeLine{3488                  \textcolor{stringliteral}{"what direction is orthogonal to density contours. It "}//\&}
\DoxyCodeLine{3489                  \textcolor{stringliteral}{"should be a typical value of (dR/dS) / (dR/dT) in "}//\&}
\DoxyCodeLine{3490                  \textcolor{stringliteral}{"oceanic profiles."}, units=\textcolor{stringliteral}{"degC PSU-\/1"}, default=6.0)}
\DoxyCodeLine{3491   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BUFFER\_LAYER\_EXTRAP\_LIMIT"}, cs\%BL\_extrap\_lim, \&}
\DoxyCodeLine{3492                  \textcolor{stringliteral}{"A limit on the density range over which extrapolation "}//\&}
\DoxyCodeLine{3493                  \textcolor{stringliteral}{"can occur when detraining from the buffer layers, "}//\&}
\DoxyCodeLine{3494                  \textcolor{stringliteral}{"relative to the density range within the mixed and "}//\&}
\DoxyCodeLine{3495                  \textcolor{stringliteral}{"buffer layers, when the detrainment is going into the "}//\&}
\DoxyCodeLine{3496                  \textcolor{stringliteral}{"lightest interior layer, nondimensional, or a negative "}//\&}
\DoxyCodeLine{3497                  \textcolor{stringliteral}{"value not to apply this limit."}, units=\textcolor{stringliteral}{"nondim"}, default = -\/1.0)}
\DoxyCodeLine{3498   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BUFFER\_LAYER\_HMIN\_THICK"}, cs\%Hbuffer\_min, \&}
\DoxyCodeLine{3499                  \textcolor{stringliteral}{"The minimum buffer layer thickness when the mixed layer is very thick."}, \&}
\DoxyCodeLine{3500                  units=\textcolor{stringliteral}{"m"}, default=5.0, scale=gv\%m\_to\_H)}
\DoxyCodeLine{3501   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BUFFER\_LAYER\_HMIN\_REL"}, cs\%Hbuffer\_rel\_min, \&}
\DoxyCodeLine{3502                  \textcolor{stringliteral}{"The minimum buffer layer thickness relative to the combined mixed "}//\&}
\DoxyCodeLine{3503                  \textcolor{stringliteral}{"land buffer ayer thicknesses when they are thin."}, \&}
\DoxyCodeLine{3504                  units=\textcolor{stringliteral}{"nondim"}, default=0.1/cs\%nkbl)}
\DoxyCodeLine{3505   bl\_detrain\_time\_dflt = 4.0*3600.0 ; \textcolor{keywordflow}{if} (cs\%nkbl==1) bl\_detrain\_time\_dflt = 86400.0*30.0}
\DoxyCodeLine{3506   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BUFFER\_LAY\_DETRAIN\_TIME"}, cs\%BL\_detrain\_time, \&}
\DoxyCodeLine{3507                  \textcolor{stringliteral}{"A timescale that characterizes buffer layer detrainment events."}, \&}
\DoxyCodeLine{3508                  units=\textcolor{stringliteral}{"s"}, default=bl\_detrain\_time\_dflt, scale=us\%s\_to\_T)}
\DoxyCodeLine{3509   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BUFFER\_SPLIT\_RHO\_TOL"}, cs\%BL\_split\_rho\_tol, \&}
\DoxyCodeLine{3510                  \textcolor{stringliteral}{"The fractional tolerance for matching layer target densities when splitting "}//\&}
\DoxyCodeLine{3511                  \textcolor{stringliteral}{"layers to deal with massive interior layers that are lighter than one of the "}//\&}
\DoxyCodeLine{3512                  \textcolor{stringliteral}{"mixed or buffer layers."}, units=\textcolor{stringliteral}{"nondim"}, default=0.1)}
\DoxyCodeLine{3513 }
\DoxyCodeLine{3514   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEPTH\_LIMIT\_FLUXES"}, cs\%H\_limit\_fluxes, \&}
\DoxyCodeLine{3515                  \textcolor{stringliteral}{"The surface fluxes are scaled away when the total ocean "}//\&}
\DoxyCodeLine{3516                  \textcolor{stringliteral}{"depth is less than DEPTH\_LIMIT\_FLUXES."}, \&}
\DoxyCodeLine{3517                  units=\textcolor{stringliteral}{"m"}, default=0.1*hmix\_min\_m, scale=gv\%m\_to\_H)}
\DoxyCodeLine{3518   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"OMEGA"}, cs\%omega, \&}
\DoxyCodeLine{3519                  \textcolor{stringliteral}{"The rotation rate of the earth."}, \&}
\DoxyCodeLine{3520                  default=7.2921e-\/5, units=\textcolor{stringliteral}{"s-\/1"}, scale=us\%T\_to\_s)}
\DoxyCodeLine{3521   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_USE\_OMEGA"}, use\_omega, \&}
\DoxyCodeLine{3522                  \textcolor{stringliteral}{"If true, use the absolute rotation rate instead of the "}//\&}
\DoxyCodeLine{3523                  \textcolor{stringliteral}{"vertical component of rotation when setting the decay "}//\&}
\DoxyCodeLine{3524                  \textcolor{stringliteral}{"scale for turbulence."}, default=.false., do\_not\_log=.true.)}
\DoxyCodeLine{3525   omega\_frac\_dflt = 0.0}
\DoxyCodeLine{3526   \textcolor{keywordflow}{if} (use\_omega) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3527     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"ML\_USE\_OMEGA is depricated; use ML\_OMEGA\_FRAC=1.0 instead."})}
\DoxyCodeLine{3528     omega\_frac\_dflt = 1.0}
\DoxyCodeLine{3529 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3530   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_OMEGA\_FRAC"}, cs\%omega\_frac, \&}
\DoxyCodeLine{3531                  \textcolor{stringliteral}{"When setting the decay scale for turbulence, use this "}//\&}
\DoxyCodeLine{3532                  \textcolor{stringliteral}{"fraction of the absolute rotation rate blended with the "}//\&}
\DoxyCodeLine{3533                  \textcolor{stringliteral}{"local value of f, as sqrt((1-\/of)*f\string^2 + of*4*omega\string^2)."}, \&}
\DoxyCodeLine{3534                  units=\textcolor{stringliteral}{"nondim"}, default=omega\_frac\_dflt)}
\DoxyCodeLine{3535   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_RESORT"}, cs\%ML\_resort, \&}
\DoxyCodeLine{3536                  \textcolor{stringliteral}{"If true, resort the topmost layers by potential density "}//\&}
\DoxyCodeLine{3537                  \textcolor{stringliteral}{"before the mixed layer calculations."}, default=.false.)}
\DoxyCodeLine{3538   \textcolor{keywordflow}{if} (cs\%ML\_resort) \&}
\DoxyCodeLine{3539     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_PRESORT\_NK\_CONV\_ADJ"}, cs\%ML\_presort\_nz\_conv\_adj, \&}
\DoxyCodeLine{3540                  \textcolor{stringliteral}{"Convectively mix the first ML\_PRESORT\_NK\_CONV\_ADJ "}//\&}
\DoxyCodeLine{3541                  \textcolor{stringliteral}{"layers before sorting when ML\_RESORT is true."}, \&}
\DoxyCodeLine{3542                  units=\textcolor{stringliteral}{"nondim"}, default=0, fail\_if\_missing=.true.) \textcolor{comment}{! Fail added by AJA.}}
\DoxyCodeLine{3543   \textcolor{comment}{! This gives a minimum decay scale that is typically much less than Angstrom.}}
\DoxyCodeLine{3544   ustar\_min\_dflt = 2e-\/4*us\%s\_to\_T*cs\%omega*(gv\%Angstrom\_m + gv\%H\_to\_m*gv\%H\_subroundoff)}
\DoxyCodeLine{3545   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BML\_USTAR\_MIN"}, cs\%ustar\_min, \&}
\DoxyCodeLine{3546                  \textcolor{stringliteral}{"The minimum value of ustar that should be used by the "}//\&}
\DoxyCodeLine{3547                  \textcolor{stringliteral}{"bulk mixed layer model in setting vertical TKE decay "}//\&}
\DoxyCodeLine{3548                  \textcolor{stringliteral}{"scales. This must be greater than 0."}, units=\textcolor{stringliteral}{"m s-\/1"}, \&}
\DoxyCodeLine{3549                  default=ustar\_min\_dflt, scale=us\%m\_to\_Z*us\%T\_to\_s)}
\DoxyCodeLine{3550   \textcolor{keywordflow}{if} (cs\%ustar\_min<=0.0) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"BML\_USTAR\_MIN must be positive."})}
\DoxyCodeLine{3551 }
\DoxyCodeLine{3552   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"RESOLVE\_EKMAN"}, cs\%Resolve\_Ekman, \&}
\DoxyCodeLine{3553                  \textcolor{stringliteral}{"If true, the NKML>1 layers in the mixed layer are "}//\&}
\DoxyCodeLine{3554                  \textcolor{stringliteral}{"chosen to optimally represent the impact of the Ekman "}//\&}
\DoxyCodeLine{3555                  \textcolor{stringliteral}{"transport on the mixed layer TKE budget.  Otherwise, "}//\&}
\DoxyCodeLine{3556                  \textcolor{stringliteral}{"the sublayers are distributed uniformly through the "}//\&}
\DoxyCodeLine{3557                  \textcolor{stringliteral}{"mixed layer."}, default=.false.)}
\DoxyCodeLine{3558   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CORRECT\_ABSORPTION\_DEPTH"}, cs\%correct\_absorption, \&}
\DoxyCodeLine{3559                  \textcolor{stringliteral}{"If true, the average depth at which penetrating shortwave "}//\&}
\DoxyCodeLine{3560                  \textcolor{stringliteral}{"radiation is absorbed is adjusted to match the average "}//\&}
\DoxyCodeLine{3561                  \textcolor{stringliteral}{"heating depth of an exponential profile by moving some "}//\&}
\DoxyCodeLine{3562                  \textcolor{stringliteral}{"of the heating upward in the water column."}, default=.false.)}
\DoxyCodeLine{3563   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DO\_RIVERMIX"}, cs\%do\_rivermix, \&}
\DoxyCodeLine{3564                  \textcolor{stringliteral}{"If true, apply additional mixing wherever there is "}//\&}
\DoxyCodeLine{3565                  \textcolor{stringliteral}{"runoff, so that it is mixed down to RIVERMIX\_DEPTH, "}//\&}
\DoxyCodeLine{3566                  \textcolor{stringliteral}{"if the ocean is that deep."}, default=.false.)}
\DoxyCodeLine{3567   \textcolor{keywordflow}{if} (cs\%do\_rivermix) \&}
\DoxyCodeLine{3568     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"RIVERMIX\_DEPTH"}, cs\%rivermix\_depth, \&}
\DoxyCodeLine{3569                  \textcolor{stringliteral}{"The depth to which rivers are mixed if DO\_RIVERMIX is "}//\&}
\DoxyCodeLine{3570                  \textcolor{stringliteral}{"defined."}, units=\textcolor{stringliteral}{"m"}, default=0.0, scale=us\%m\_to\_Z)}
\DoxyCodeLine{3571   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_RIVER\_HEAT\_CONTENT"}, cs\%use\_river\_heat\_content, \&}
\DoxyCodeLine{3572                  \textcolor{stringliteral}{"If true, use the fluxes\%runoff\_Hflx field to set the "}//\&}
\DoxyCodeLine{3573                  \textcolor{stringliteral}{"heat carried by runoff, instead of using SST*CP*liq\_runoff."}, \&}
\DoxyCodeLine{3574                  default=.false.)}
\DoxyCodeLine{3575   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_CALVING\_HEAT\_CONTENT"}, cs\%use\_calving\_heat\_content, \&}
\DoxyCodeLine{3576                  \textcolor{stringliteral}{"If true, use the fluxes\%calving\_Hflx field to set the "}//\&}
\DoxyCodeLine{3577                  \textcolor{stringliteral}{"heat carried by runoff, instead of using SST*CP*froz\_runoff."}, \&}
\DoxyCodeLine{3578                  default=.false.)}
\DoxyCodeLine{3579   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BULKML\_CONV\_MOMENTUM\_BUG"}, cs\%convect\_mom\_bug, \&}
\DoxyCodeLine{3580                  \textcolor{stringliteral}{"If true, use code with a bug that causes a loss of momentum conservation "}//\&}
\DoxyCodeLine{3581                  \textcolor{stringliteral}{"during mixedlayer convection."}, default=.false.)}
\DoxyCodeLine{3582 }
\DoxyCodeLine{3583   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ALLOW\_CLOCKS\_IN\_OMP\_LOOPS"}, \&}
\DoxyCodeLine{3584                  cs\%allow\_clocks\_in\_omp\_loops, \&}
\DoxyCodeLine{3585                  \textcolor{stringliteral}{"If true, clocks can be called from inside loops that can "}//\&}
\DoxyCodeLine{3586                  \textcolor{stringliteral}{"be threaded. To run with multiple threads, set to False."}, \&}
\DoxyCodeLine{3587                  default=.true.)}
\DoxyCodeLine{3588 }
\DoxyCodeLine{3589   cs\%id\_ML\_depth = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'h\_ML'}, diag\%axesT1, \&}
\DoxyCodeLine{3590       time, \textcolor{stringliteral}{'Surface mixed layer depth'}, \textcolor{stringliteral}{'m'})}
\DoxyCodeLine{3591   cs\%id\_TKE\_wind = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_wind'}, diag\%axesT1, \&}
\DoxyCodeLine{3592       time, \textcolor{stringliteral}{'Wind-\/stirring source of mixed layer TKE'}, \&}
\DoxyCodeLine{3593       \textcolor{stringliteral}{'m3 s-\/3'}, conversion=us\%Z\_to\_m*(us\%L\_to\_m**2)*(us\%s\_to\_T**3))}
\DoxyCodeLine{3594   cs\%id\_TKE\_RiBulk = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_RiBulk'}, diag\%axesT1, \&}
\DoxyCodeLine{3595       time, \textcolor{stringliteral}{'Mean kinetic energy source of mixed layer TKE'}, \&}
\DoxyCodeLine{3596       \textcolor{stringliteral}{'m3 s-\/3'}, conversion=us\%Z\_to\_m*(us\%L\_to\_m**2)*(us\%s\_to\_T**3))}
\DoxyCodeLine{3597   cs\%id\_TKE\_conv = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_conv'}, diag\%axesT1, \&}
\DoxyCodeLine{3598       time, \textcolor{stringliteral}{'Convective source of mixed layer TKE'}, \&}
\DoxyCodeLine{3599       \textcolor{stringliteral}{'m3 s-\/3'}, conversion=us\%Z\_to\_m*(us\%L\_to\_m**2)*(us\%s\_to\_T**3))}
\DoxyCodeLine{3600   cs\%id\_TKE\_pen\_SW = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_pen\_SW'}, diag\%axesT1, \&}
\DoxyCodeLine{3601       time, \textcolor{stringliteral}{'TKE consumed by mixing penetrative shortwave radation through the mixed layer'}, \&}
\DoxyCodeLine{3602       \textcolor{stringliteral}{'m3 s-\/3'}, conversion=us\%Z\_to\_m*(us\%L\_to\_m**2)*(us\%s\_to\_T**3))}
\DoxyCodeLine{3603   cs\%id\_TKE\_mixing = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_mixing'}, diag\%axesT1, \&}
\DoxyCodeLine{3604       time, \textcolor{stringliteral}{'TKE consumed by mixing that deepens the mixed layer'}, \&}
\DoxyCodeLine{3605       \textcolor{stringliteral}{'m3 s-\/3'}, conversion=us\%Z\_to\_m*(us\%L\_to\_m**2)*(us\%s\_to\_T**3))}
\DoxyCodeLine{3606   cs\%id\_TKE\_mech\_decay = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_mech\_decay'}, diag\%axesT1, \&}
\DoxyCodeLine{3607       time, \textcolor{stringliteral}{'Mechanical energy decay sink of mixed layer TKE'}, \&}
\DoxyCodeLine{3608       \textcolor{stringliteral}{'m3 s-\/3'}, conversion=us\%Z\_to\_m*(us\%L\_to\_m**2)*(us\%s\_to\_T**3))}
\DoxyCodeLine{3609   cs\%id\_TKE\_conv\_decay = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_conv\_decay'}, diag\%axesT1, \&}
\DoxyCodeLine{3610       time, \textcolor{stringliteral}{'Convective energy decay sink of mixed layer TKE'}, \&}
\DoxyCodeLine{3611       \textcolor{stringliteral}{'m3 s-\/3'}, conversion=us\%Z\_to\_m*(us\%L\_to\_m**2)*(us\%s\_to\_T**3))}
\DoxyCodeLine{3612   cs\%id\_TKE\_conv\_s2 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_conv\_s2'}, diag\%axesT1, \&}
\DoxyCodeLine{3613       time, \textcolor{stringliteral}{'Spurious source of mixed layer TKE from sigma2'}, \&}
\DoxyCodeLine{3614       \textcolor{stringliteral}{'m3 s-\/3'}, conversion=us\%Z\_to\_m*(us\%L\_to\_m**2)*(us\%s\_to\_T**3))}
\DoxyCodeLine{3615   cs\%id\_PE\_detrain = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'PE\_detrain'}, diag\%axesT1, \&}
\DoxyCodeLine{3616       time, \textcolor{stringliteral}{'Spurious source of potential energy from mixed layer detrainment'}, \&}
\DoxyCodeLine{3617       \textcolor{stringliteral}{'W m-\/2'}, conversion=us\%RZ3\_T3\_to\_W\_m2*us\%L\_to\_Z**2)}
\DoxyCodeLine{3618   cs\%id\_PE\_detrain2 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'PE\_detrain2'}, diag\%axesT1, \&}
\DoxyCodeLine{3619       time, \textcolor{stringliteral}{'Spurious source of potential energy from mixed layer only detrainment'}, \&}
\DoxyCodeLine{3620       \textcolor{stringliteral}{'W m-\/2'}, conversion=us\%RZ3\_T3\_to\_W\_m2*us\%L\_to\_Z**2)}
\DoxyCodeLine{3621   cs\%id\_h\_mismatch = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'h\_miss\_ML'}, diag\%axesT1, \&}
\DoxyCodeLine{3622       time, \textcolor{stringliteral}{'Summed absolute mismatch in entrainment terms'}, \textcolor{stringliteral}{'m'}, conversion=us\%Z\_to\_m)}
\DoxyCodeLine{3623   cs\%id\_Hsfc\_used = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Hs\_used'}, diag\%axesT1, \&}
\DoxyCodeLine{3624       time, \textcolor{stringliteral}{'Surface region thickness that is used'}, \textcolor{stringliteral}{'m'}, conversion=us\%Z\_to\_m)}
\DoxyCodeLine{3625   cs\%id\_Hsfc\_max = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Hs\_max'}, diag\%axesT1, \&}
\DoxyCodeLine{3626       time, \textcolor{stringliteral}{'Maximum surface region thickness'}, \textcolor{stringliteral}{'m'}, conversion=us\%Z\_to\_m)}
\DoxyCodeLine{3627   cs\%id\_Hsfc\_min = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Hs\_min'}, diag\%axesT1, \&}
\DoxyCodeLine{3628       time, \textcolor{stringliteral}{'Minimum surface region thickness'}, \textcolor{stringliteral}{'m'}, conversion=us\%Z\_to\_m)}
\DoxyCodeLine{3629  \textcolor{comment}{!CS\%lim\_det\_dH\_sfc = 0.5 ; CS\%lim\_det\_dH\_bathy = 0.2 ! Technically these should not get used if limit\_det is false?}}
\DoxyCodeLine{3630   \textcolor{keywordflow}{if} (cs\%limit\_det .or. (cs\%id\_Hsfc\_min > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3631     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"LIMIT\_BUFFER\_DET\_DH\_SFC"}, cs\%lim\_det\_dH\_sfc, \&}
\DoxyCodeLine{3632                  \textcolor{stringliteral}{"The fractional limit in the change between grid points "}//\&}
\DoxyCodeLine{3633                  \textcolor{stringliteral}{"of the surface region (mixed \& buffer layer) thickness."}, \&}
\DoxyCodeLine{3634                  units=\textcolor{stringliteral}{"nondim"}, default=0.5)}
\DoxyCodeLine{3635     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"LIMIT\_BUFFER\_DET\_DH\_BATHY"}, cs\%lim\_det\_dH\_bathy, \&}
\DoxyCodeLine{3636                  \textcolor{stringliteral}{"The fraction of the total depth by which the thickness "}//\&}
\DoxyCodeLine{3637                  \textcolor{stringliteral}{"of the surface region (mixed \& buffer layer) is allowed "}//\&}
\DoxyCodeLine{3638                  \textcolor{stringliteral}{"to change between grid points."}, units=\textcolor{stringliteral}{"nondim"}, default=0.2)}
\DoxyCodeLine{3639 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3640 }
\DoxyCodeLine{3641   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENABLE\_THERMODYNAMICS"}, use\_temperature, \&}
\DoxyCodeLine{3642                  \textcolor{stringliteral}{"If true, temperature and salinity are used as state "}//\&}
\DoxyCodeLine{3643                  \textcolor{stringliteral}{"variables."}, default=.true.)}
\DoxyCodeLine{3644   cs\%nsw = 0}
\DoxyCodeLine{3645   \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3646     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"PEN\_SW\_NBANDS"}, cs\%nsw, default=1)}
\DoxyCodeLine{3647 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3648 }
\DoxyCodeLine{3649 }
\DoxyCodeLine{3650   \textcolor{keywordflow}{if} (max(cs\%id\_TKE\_wind, cs\%id\_TKE\_RiBulk, cs\%id\_TKE\_conv, cs\%id\_TKE\_mixing, \&}
\DoxyCodeLine{3651           cs\%id\_TKE\_pen\_SW, cs\%id\_TKE\_mech\_decay, cs\%id\_TKE\_conv\_decay) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3652     \textcolor{keyword}{call }safe\_alloc\_alloc(cs\%diag\_TKE\_wind, isd, ied, jsd, jed)}
\DoxyCodeLine{3653     \textcolor{keyword}{call }safe\_alloc\_alloc(cs\%diag\_TKE\_RiBulk, isd, ied, jsd, jed)}
\DoxyCodeLine{3654     \textcolor{keyword}{call }safe\_alloc\_alloc(cs\%diag\_TKE\_conv, isd, ied, jsd, jed)}
\DoxyCodeLine{3655     \textcolor{keyword}{call }safe\_alloc\_alloc(cs\%diag\_TKE\_pen\_SW, isd, ied, jsd, jed)}
\DoxyCodeLine{3656     \textcolor{keyword}{call }safe\_alloc\_alloc(cs\%diag\_TKE\_mixing, isd, ied, jsd, jed)}
\DoxyCodeLine{3657     \textcolor{keyword}{call }safe\_alloc\_alloc(cs\%diag\_TKE\_mech\_decay, isd, ied, jsd, jed)}
\DoxyCodeLine{3658     \textcolor{keyword}{call }safe\_alloc\_alloc(cs\%diag\_TKE\_conv\_decay, isd, ied, jsd, jed)}
\DoxyCodeLine{3659     \textcolor{keyword}{call }safe\_alloc\_alloc(cs\%diag\_TKE\_conv\_s2, isd, ied, jsd, jed)}
\DoxyCodeLine{3660 }
\DoxyCodeLine{3661     cs\%TKE\_diagnostics = .true.}
\DoxyCodeLine{3662 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3663   \textcolor{keywordflow}{if} (cs\%id\_PE\_detrain > 0) \textcolor{keyword}{call }safe\_alloc\_alloc(cs\%diag\_PE\_detrain, isd, ied, jsd, jed)}
\DoxyCodeLine{3664   \textcolor{keywordflow}{if} (cs\%id\_PE\_detrain2 > 0) \textcolor{keyword}{call }safe\_alloc\_alloc(cs\%diag\_PE\_detrain2, isd, ied, jsd, jed)}
\DoxyCodeLine{3665   \textcolor{keywordflow}{if} (cs\%id\_ML\_depth > 0) \textcolor{keyword}{call }safe\_alloc\_alloc(cs\%ML\_depth, isd, ied, jsd, jed)}
\DoxyCodeLine{3666 }
\DoxyCodeLine{3667   \textcolor{keywordflow}{if} (cs\%allow\_clocks\_in\_omp\_loops) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3668     id\_clock\_detrain = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer detrain)'}, grain=clock\_routine)}
\DoxyCodeLine{3669     id\_clock\_mech = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer mechanical entrainment)'}, grain=clock\_routine)}
\DoxyCodeLine{3670     id\_clock\_conv = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer convection)'}, grain=clock\_routine)}
\DoxyCodeLine{3671     \textcolor{keywordflow}{if} (cs\%ML\_resort) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3672       id\_clock\_resort = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer resorting)'}, grain=clock\_routine)}
\DoxyCodeLine{3673     \textcolor{keywordflow}{else}}
\DoxyCodeLine{3674       id\_clock\_adjustment = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer convective adjustment)'}, grain=clock\_routine)}
\DoxyCodeLine{3675 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3676     id\_clock\_eos = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer EOS)'}, grain=clock\_routine)}
\DoxyCodeLine{3677 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3678 }
\DoxyCodeLine{3679   \textcolor{keywordflow}{if} (cs\%limit\_det .or. (cs\%id\_Hsfc\_min > 0)) \&}
\DoxyCodeLine{3680       id\_clock\_pass = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer halo updates)'}, grain=clock\_routine)}
\DoxyCodeLine{3681 }
\DoxyCodeLine{3682 }
\DoxyCodeLine{3683 \textcolor{comment}{!  if (CS\%limit\_det) then}}
\DoxyCodeLine{3684 \textcolor{comment}{!  endif}}
\DoxyCodeLine{3685 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_a64f3bec37c0a6dfba1400d03c61399e7}\label{namespacemom__bulk__mixed__layer_a64f3bec37c0a6dfba1400d03c61399e7}} 
\index{mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}!convective\_adjustment@{convective\_adjustment}}
\index{convective\_adjustment@{convective\_adjustment}!mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}}
\doxysubsubsection{\texorpdfstring{convective\_adjustment()}{convective\_adjustment()}}
{\footnotesize\ttfamily subroutine mom\+\_\+bulk\+\_\+mixed\+\_\+layer\+::convective\+\_\+adjustment (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{h,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{u,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{v,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{R0,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{Rcv,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{T,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{S,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{eps,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{d\+\_\+eb,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(out)}]{d\+K\+E\+\_\+\+CA,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(out)}]{c\+T\+KE,  }\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__bulk__mixed__layer_1_1bulkmixedlayer__cs}{bulkmixedlayer\+\_\+cs}}), pointer}]{CS,  }\item[{integer, intent(in), optional}]{nz\+\_\+conv }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine does instantaneous convective entrainment into the buffer layers and mixed layers to remove hydrostatic instabilities. Any water that is lighter than currently in the mixed-\/ or buffer-\/ layer is entrained. 


\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,out}}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. The units of h are referred to as H below. \\
\hline
\mbox{\texttt{ in,out}}  & {\em u} & Zonal velocities interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em v} & Zonal velocities interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em t} & Layer temperatures \mbox{[}degC\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em s} & Layer salinities \mbox{[}ppt\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em r0} & Potential density referenced to surface pressure \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em rcv} & The coordinate defining potential density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em d\+\_\+eb} & The downward increase across a layer in the entrainment from below \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. Positive values go with mass gain by a layer. \\
\hline
\mbox{\texttt{ in}}  & {\em eps} & The negligibly small amount of water that will be left in each layer \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em dke\+\_\+ca} & The vertically integrated change in kinetic energy due to convective adjustment \mbox{[}Z L2 T-\/2 $\sim$$>$ m3 s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em ctke} & The buoyant turbulent kinetic energy source due to convective adjustment \mbox{[}Z L2 T-\/2 $\sim$$>$ m3 s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em j} & The j-\/index to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & The control structure for this module. \\
\hline
\mbox{\texttt{ in}}  & {\em nz\+\_\+conv} & If present, the number of layers over which to do convective adjustment (perhaps CSnkml). \\
\hline
\end{DoxyParams}


Definition at line 806 of file M\+O\+M\+\_\+bulk\+\_\+mixed\+\_\+layer.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{806   \textcolor{keywordtype}{type}(ocean\_grid\_type),             \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{807   \textcolor{keywordtype}{type}(verticalGrid\_type),           \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{808 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{    !< Layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{809 \textcolor{comment}{                                                           !! The units of h are referred to as H below.}}
\DoxyCodeLine{810 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: u\textcolor{comment}{    !< Zonal velocities interpolated to h}}
\DoxyCodeLine{811 \textcolor{comment}{                                                           !! points [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{812 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: v\textcolor{comment}{    !< Zonal velocities interpolated to h}}
\DoxyCodeLine{813 \textcolor{comment}{                                                           !! points [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{814 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: T\textcolor{comment}{    !< Layer temperatures [degC].}}
\DoxyCodeLine{815 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: S\textcolor{comment}{    !< Layer salinities [ppt].}}
\DoxyCodeLine{816 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: R0\textcolor{comment}{   !< Potential density referenced to}}
\DoxyCodeLine{817 \textcolor{comment}{                                                           !! surface pressure [R \string~> kg m-\/3].}}
\DoxyCodeLine{818 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: Rcv\textcolor{comment}{  !< The coordinate defining potential}}
\DoxyCodeLine{819 \textcolor{comment}{                                                           !! density [R \string~> kg m-\/3].}}
\DoxyCodeLine{820 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: d\_eb\textcolor{comment}{ !< The downward increase across a layer}}
\DoxyCodeLine{821 \textcolor{comment}{                                                           !! in the entrainment from below [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{822 \textcolor{comment}{                                                           !! Positive values go with mass gain by}}
\DoxyCodeLine{823 \textcolor{comment}{                                                           !! a layer.}}
\DoxyCodeLine{824 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(in)}    :: eps\textcolor{comment}{  !< The negligibly small amount of water}}
\DoxyCodeLine{825 \textcolor{comment}{                                                           !! that will be left in each layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{826 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(out)}   :: dKE\_CA\textcolor{comment}{ !< The vertically integrated change in}}
\DoxyCodeLine{827 \textcolor{comment}{                                                           !! kinetic energy due to convective}}
\DoxyCodeLine{828 \textcolor{comment}{                                                           !! adjustment [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{829 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(out)}   :: cTKE\textcolor{comment}{ !< The buoyant turbulent kinetic energy}}
\DoxyCodeLine{830 \textcolor{comment}{                                                           !! source due to convective adjustment}}
\DoxyCodeLine{831 \textcolor{comment}{                                                           !! [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{832   \textcolor{keywordtype}{integer},                           \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{    !< The j-\/index to work on.}}
\DoxyCodeLine{833   \textcolor{keywordtype}{type}(unit\_scale\_type),             \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}}
\DoxyCodeLine{834   \textcolor{keywordtype}{type}(bulkmixedlayer\_CS),           \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< The control structure for this module.}}
\DoxyCodeLine{835   \textcolor{keywordtype}{integer},                 \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: nz\_conv\textcolor{comment}{ !< If present, the number of layers}}
\DoxyCodeLine{836 \textcolor{comment}{                                                           !! over which to do convective adjustment}}
\DoxyCodeLine{837 \textcolor{comment}{                                                           !! (perhaps CS\%nkml).}}
\DoxyCodeLine{838 }
\DoxyCodeLine{839 \textcolor{comment}{!   This subroutine does instantaneous convective entrainment into the buffer}}
\DoxyCodeLine{840 \textcolor{comment}{! layers and mixed layers to remove hydrostatic instabilities.  Any water that}}
\DoxyCodeLine{841 \textcolor{comment}{! is lighter than currently in the mixed-\/ or buffer-\/ layer is entrained.}}
\DoxyCodeLine{842 }
\DoxyCodeLine{843   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{844 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: \&}
\DoxyCodeLine{845     htot, \&     \textcolor{comment}{!   The total depth of the layers being considered for}}
\DoxyCodeLine{846                 \textcolor{comment}{! entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{847     r0\_tot, \&   \textcolor{comment}{!   The integrated potential density referenced to the surface}}
\DoxyCodeLine{848                 \textcolor{comment}{! of the layers which are fully entrained [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{849     rcv\_tot, \&  \textcolor{comment}{!   The integrated coordinate value potential density of the}}
\DoxyCodeLine{850                 \textcolor{comment}{! layers that are fully entrained [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{851     ttot, \&     \textcolor{comment}{!   The integrated temperature of layers which are fully}}
\DoxyCodeLine{852                 \textcolor{comment}{! entrained [degC H \string~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{853     stot, \&     \textcolor{comment}{!   The integrated salt of layers which are fully entrained}}
\DoxyCodeLine{854                 \textcolor{comment}{! [H ppt \string~> m ppt or ppt kg m-\/2].}}
\DoxyCodeLine{855     uhtot, \&    \textcolor{comment}{!   The depth integrated zonal and meridional velocities in}}
\DoxyCodeLine{856     vhtot, \&    \textcolor{comment}{! the mixed layer [H L T-\/1 \string~> m2 s-\/1 or kg m-\/1 s-\/1].}}
\DoxyCodeLine{857     ke\_orig, \&  \textcolor{comment}{!   The total mean kinetic energy in the mixed layer before}}
\DoxyCodeLine{858                 \textcolor{comment}{! convection, [H L2 T-\/2 \string~> H m2 s-\/2].}}
\DoxyCodeLine{859     h\_orig\_k1   \textcolor{comment}{!   The depth of layer k1 before convective adjustment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{860 \textcolor{keywordtype}{  real} :: h\_ent \textcolor{comment}{!   The thickness from a layer that is entrained [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{861 \textcolor{keywordtype}{  real} :: Ih    \textcolor{comment}{!   The inverse of a thickness [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{862 \textcolor{keywordtype}{  real} :: g\_H2\_2Rho0  \textcolor{comment}{!   Half the gravitational acceleration times the square of}}
\DoxyCodeLine{863                       \textcolor{comment}{! the conversion from H to Z divided by the mean density,}}
\DoxyCodeLine{864                       \textcolor{comment}{! in [L2 Z T-\/3 H-\/2 R-\/1 \string~> m4 s-\/3 kg-\/1 or m10 s-\/3 kg-\/3].}}
\DoxyCodeLine{865   \textcolor{keywordtype}{integer} :: is, ie, nz, i, k, k1, nzc, nkmb}
\DoxyCodeLine{866 }
\DoxyCodeLine{867   is = g\%isc ; ie = g\%iec ; nz = gv\%ke}
\DoxyCodeLine{868   g\_h2\_2rho0 = (gv\%g\_Earth * gv\%H\_to\_Z**2) / (2.0 * gv\%Rho0)}
\DoxyCodeLine{869   nzc = nz ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(nz\_conv)) nzc = nz\_conv}
\DoxyCodeLine{870   nkmb = cs\%nkml+cs\%nkbl}
\DoxyCodeLine{871 }
\DoxyCodeLine{872 \textcolor{comment}{!   Undergo instantaneous entrainment into the buffer layers and mixed layers}}
\DoxyCodeLine{873 \textcolor{comment}{! to remove hydrostatic instabilities.  Any water that is lighter than currently}}
\DoxyCodeLine{874 \textcolor{comment}{! in the layer is entrained.}}
\DoxyCodeLine{875   \textcolor{keywordflow}{do} k1=min(nzc-\/1,nkmb),1,-\/1}
\DoxyCodeLine{876     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{877       h\_orig\_k1(i) = h(i,k1)}
\DoxyCodeLine{878       ke\_orig(i) = 0.5*h(i,k1)*(u(i,k1)**2 + v(i,k1)**2)}
\DoxyCodeLine{879       uhtot(i) = h(i,k1)*u(i,k1) ; vhtot(i) = h(i,k1)*v(i,k1)}
\DoxyCodeLine{880       r0\_tot(i) = r0(i,k1) * h(i,k1)}
\DoxyCodeLine{881       ctke(i,k1) = 0.0 ; dke\_ca(i,k1) = 0.0}
\DoxyCodeLine{882 }
\DoxyCodeLine{883       rcv\_tot(i) = rcv(i,k1) * h(i,k1)}
\DoxyCodeLine{884       ttot(i) = t(i,k1) * h(i,k1) ; stot(i) = s(i,k1) * h(i,k1)}
\DoxyCodeLine{885 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{886     \textcolor{keywordflow}{do} k=k1+1,nzc}
\DoxyCodeLine{887       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{888         \textcolor{keywordflow}{if} ((h(i,k) > eps(i,k)) .and. (r0\_tot(i) > h(i,k1)*r0(i,k))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{889           h\_ent = h(i,k)-\/eps(i,k)}
\DoxyCodeLine{890           ctke(i,k1) = ctke(i,k1) + h\_ent * g\_h2\_2rho0 * \&}
\DoxyCodeLine{891                    (r0\_tot(i) -\/ h(i,k1)*r0(i,k)) * cs\%nstar2}
\DoxyCodeLine{892           \textcolor{keywordflow}{if} (k < nkmb) \textcolor{keywordflow}{then}}
\DoxyCodeLine{893             ctke(i,k1) = ctke(i,k1) + ctke(i,k)}
\DoxyCodeLine{894             dke\_ca(i,k1) = dke\_ca(i,k1) + dke\_ca(i,k)}
\DoxyCodeLine{895 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{896           r0\_tot(i) = r0\_tot(i) + h\_ent * r0(i,k)}
\DoxyCodeLine{897           ke\_orig(i) = ke\_orig(i) + 0.5*h\_ent* \&}
\DoxyCodeLine{898               (u(i,k)*u(i,k) + v(i,k)*v(i,k))}
\DoxyCodeLine{899           uhtot(i) = uhtot(i) + h\_ent*u(i,k)}
\DoxyCodeLine{900           vhtot(i) = vhtot(i) + h\_ent*v(i,k)}
\DoxyCodeLine{901 }
\DoxyCodeLine{902           rcv\_tot(i) = rcv\_tot(i) + h\_ent * rcv(i,k)}
\DoxyCodeLine{903           ttot(i) = ttot(i) + h\_ent * t(i,k)}
\DoxyCodeLine{904           stot(i) = stot(i) + h\_ent * s(i,k)}
\DoxyCodeLine{905           h(i,k1) = h(i,k1) + h\_ent ; h(i,k) = eps(i,k)}
\DoxyCodeLine{906 }
\DoxyCodeLine{907           d\_eb(i,k) = d\_eb(i,k) -\/ h\_ent}
\DoxyCodeLine{908           d\_eb(i,k1) = d\_eb(i,k1) + h\_ent}
\DoxyCodeLine{909 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{910 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{911 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{912 \textcolor{comment}{! Determine the temperature, salinity, and velocities of the mixed or buffer}}
\DoxyCodeLine{913 \textcolor{comment}{! layer in question, if it has entrained.}}
\DoxyCodeLine{914     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (h(i,k1) > h\_orig\_k1(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{915       ih = 1.0 / h(i,k1)}
\DoxyCodeLine{916       r0(i,k1) = r0\_tot(i) * ih}
\DoxyCodeLine{917       u(i,k1) = uhtot(i) * ih ; v(i,k1) = vhtot(i) * ih}
\DoxyCodeLine{918       dke\_ca(i,k1) = dke\_ca(i,k1) + gv\%H\_to\_Z * (cs\%bulk\_Ri\_convective * \&}
\DoxyCodeLine{919            (ke\_orig(i) -\/ 0.5*h(i,k1)*(u(i,k1)**2 + v(i,k1)**2)))}
\DoxyCodeLine{920       rcv(i,k1) = rcv\_tot(i) * ih}
\DoxyCodeLine{921       t(i,k1) = ttot(i) * ih ; s(i,k1) = stot(i) * ih}
\DoxyCodeLine{922 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{923 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{924 \textcolor{comment}{! If lower mixed or buffer layers are massless, give them the properties of the}}
\DoxyCodeLine{925 \textcolor{comment}{! layer above.}}
\DoxyCodeLine{926   \textcolor{keywordflow}{do} k=2,min(nzc,nkmb) ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (h(i,k) == 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{927     r0(i,k) = r0(i,k-\/1)}
\DoxyCodeLine{928     rcv(i,k) = rcv(i,k-\/1) ; t(i,k) = t(i,k-\/1) ; s(i,k) = s(i,k-\/1)}
\DoxyCodeLine{929 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{930 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_a4ac89b3858f2c7c0ac6f8ac8f93b5e44}\label{namespacemom__bulk__mixed__layer_a4ac89b3858f2c7c0ac6f8ac8f93b5e44}} 
\index{mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}!ef4@{ef4}}
\index{ef4@{ef4}!mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}}
\doxysubsubsection{\texorpdfstring{ef4()}{ef4()}}
{\footnotesize\ttfamily real function mom\+\_\+bulk\+\_\+mixed\+\_\+layer\+::ef4 (\begin{DoxyParamCaption}\item[{real, intent(in)}]{Ht,  }\item[{real, intent(in)}]{En,  }\item[{real, intent(in)}]{I\+\_\+L,  }\item[{real, intent(inout), optional}]{d\+R\+\_\+de }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine returns an approximation to the integral R = exp(-\/L$\ast$(H+E)) integral(LH to L(H+E)) L/(1-\/(1+x)exp(-\/x)) dx. The approximation to the integrand is good to within -\/2\% at x$\sim$.3 and +25\% at x$\sim$3.5, but the exponential deemphasizes the importance of large x. When L=0, E\+F4 returns E/((Ht+E)$\ast$\+Ht). 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em ht} & Total thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em en} & Entrainment \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em i\+\_\+l} & The e-\/folding scale \mbox{[}H-\/1 $\sim$$>$ m-\/1 or m2 kg-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em dr\+\_\+de} & The partial derivative of the result R with E \mbox{[}H-\/2 $\sim$$>$ m-\/2 or m4 kg-\/2\mbox{]}. \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
The integral \mbox{[}H-\/1 $\sim$$>$ m-\/1 or m2 kg-\/1\mbox{]}. 
\end{DoxyReturn}


Definition at line 3694 of file M\+O\+M\+\_\+bulk\+\_\+mixed\+\_\+layer.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{3694 \textcolor{keywordtype}{  real},           \textcolor{keywordtype}{intent(in)}    :: Ht\textcolor{comment}{  !< Total thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{3695 \textcolor{keywordtype}{  real},           \textcolor{keywordtype}{intent(in)}    :: En\textcolor{comment}{  !< Entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{3696 \textcolor{keywordtype}{  real},           \textcolor{keywordtype}{intent(in)}    :: I\_L\textcolor{comment}{ !< The e-\/folding scale [H-\/1 \string~> m-\/1 or m2 kg-\/1]}}
\DoxyCodeLine{3697 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: dR\_de\textcolor{comment}{ !< The partial derivative of the result R with E [H-\/2 \string~> m-\/2 or m4 kg-\/2].}}
\DoxyCodeLine{3698 \textcolor{keywordtype}{  real} :: EF4\textcolor{comment}{ !< The integral [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{3699 }
\DoxyCodeLine{3700   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{3701 \textcolor{keywordtype}{  real} :: exp\_LHpE \textcolor{comment}{! A nondimensional exponential decay.}}
\DoxyCodeLine{3702 \textcolor{keywordtype}{  real} :: I\_HpE    \textcolor{comment}{! An inverse thickness plus entrainment [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{3703 \textcolor{keywordtype}{  real} :: Res      \textcolor{comment}{! The result of the integral above [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{3704 }
\DoxyCodeLine{3705   exp\_lhpe = exp(-\/i\_l*(en+ht))}
\DoxyCodeLine{3706   i\_hpe = 1.0/(ht+en)}
\DoxyCodeLine{3707   res = exp\_lhpe * (en*i\_hpe/ht -\/ 0.5*i\_l*log(ht*i\_hpe) + 0.5*i\_l*i\_l*en)}
\DoxyCodeLine{3708   \textcolor{keywordflow}{if} (\textcolor{keyword}{PRESENT}(dr\_de)) \&}
\DoxyCodeLine{3709     dr\_de = -\/i\_l*res + exp\_lhpe*(i\_hpe*i\_hpe + 0.5*i\_l*i\_hpe + 0.5*i\_l*i\_l)}
\DoxyCodeLine{3710   ef4 = res}
\DoxyCodeLine{3711 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_a8ab429f040caadc340609ca16aca2e29}\label{namespacemom__bulk__mixed__layer_a8ab429f040caadc340609ca16aca2e29}} 
\index{mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}!find\_starting\_tke@{find\_starting\_tke}}
\index{find\_starting\_tke@{find\_starting\_tke}!mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}}
\doxysubsubsection{\texorpdfstring{find\_starting\_tke()}{find\_starting\_tke()}}
{\footnotesize\ttfamily subroutine mom\+\_\+bulk\+\_\+mixed\+\_\+layer\+::find\+\_\+starting\+\_\+tke (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{htot,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{h\+\_\+\+CA,  }\item[{type(forcing), intent(in)}]{fluxes,  }\item[{real, dimension(szi\+\_\+(g)), intent(inout)}]{Conv\+\_\+\+En,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{c\+T\+KE,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{d\+K\+E\+\_\+\+FC,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{d\+K\+E\+\_\+\+CA,  }\item[{real, dimension(szi\+\_\+(g)), intent(out)}]{T\+KE,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{T\+K\+E\+\_\+river,  }\item[{real, dimension(szi\+\_\+(g)), intent(out)}]{Idecay\+\_\+len\+\_\+\+T\+KE,  }\item[{real, dimension(2,szi\+\_\+(g)), intent(out)}]{c\+M\+KE,  }\item[{real, intent(in)}]{dt,  }\item[{real, intent(in)}]{Idt\+\_\+diag,  }\item[{integer, intent(in)}]{j,  }\item[{integer, dimension(szi\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{ksort,  }\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__bulk__mixed__layer_1_1bulkmixedlayer__cs}{bulkmixedlayer\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine determines the T\+KE available at the depth of free convection to drive mechanical entrainment. 


\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 htot} & The accumulated mixed layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em h\+\_\+ca} & The mixed layer depth after convective adjustment \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em fluxes} & A structure containing pointers to any possible forcing fields. Unused fields have N\+U\+LL ptrs. \\
\hline
\mbox{\texttt{ in,out}}  & {\em conv\+\_\+en} & The buoyant turbulent kinetic energy source due to free convection \mbox{[}Z L2 T-\/2 $\sim$$>$ m3 s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dke\+\_\+fc} & The vertically integrated change in kinetic energy due to free convection \mbox{[}Z L2 T-\/2 $\sim$$>$ m3 s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ctke} & The buoyant turbulent kinetic energy \\
\hline
\mbox{\texttt{ in}}  & {\em dke\+\_\+ca} & The vertically integrated change in \\
\hline
\mbox{\texttt{ out}}  & {\em tke} & The turbulent kinetic energy available for mixing over a time step \mbox{[}Z m2 T-\/2 $\sim$$>$ m3 s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em idecay\+\_\+len\+\_\+tke} & The inverse of the vertical decay scale for T\+KE \mbox{[}H-\/1 $\sim$$>$ m-\/1 or m2 kg-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em tke\+\_\+river} & The source of turbulent kinetic energy available for driving mixing at river mouths \mbox{[}Z L2 T-\/3 $\sim$$>$ m3 s-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em cmke} & Coefficients of HpE and Hp\+E$^\wedge$2 in calculating the denominator of M\+K\+E\+\_\+rate, \mbox{[}H-\/1 $\sim$$>$ m-\/1 or m2 kg-\/1\mbox{]} and \mbox{[}H-\/2 $\sim$$>$ m-\/2 or m4 kg-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & The time step \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em idt\+\_\+diag} & The inverse of the accumulated diagnostic time interval \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em j} & The j-\/index to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em ksort} & The density-\/sorted k-\/indicies. \\
\hline
 & {\em cs} & The control structure for this module. \\
\hline
\end{DoxyParams}


Definition at line 1316 of file M\+O\+M\+\_\+bulk\+\_\+mixed\+\_\+layer.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1316   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{       !< The ocean's grid structure.}}
\DoxyCodeLine{1317   \textcolor{keywordtype}{type}(verticalGrid\_type),    \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{      !< The ocean's vertical grid structure.}}
\DoxyCodeLine{1318   \textcolor{keywordtype}{type}(unit\_scale\_type),      \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{      !< A dimensional unit scaling type}}
\DoxyCodeLine{1319 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(in)}    :: htot\textcolor{comment}{    !< The accumulated mixed layer thickness}}
\DoxyCodeLine{1320 \textcolor{comment}{                                                       !! [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{1321 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(in)}    :: h\_CA\textcolor{comment}{    !< The mixed layer depth after convective}}
\DoxyCodeLine{1322 \textcolor{comment}{                                                       !! adjustment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1323   \textcolor{keywordtype}{type}(forcing),              \textcolor{keywordtype}{intent(in)}    :: fluxes\textcolor{comment}{  !< A structure containing pointers to any}}
\DoxyCodeLine{1324 \textcolor{comment}{                                                       !! possible forcing fields.  Unused fields}}
\DoxyCodeLine{1325 \textcolor{comment}{                                                       !! have NULL ptrs.}}
\DoxyCodeLine{1326 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(inout)} :: Conv\_En\textcolor{comment}{ !< The buoyant turbulent kinetic energy source}}
\DoxyCodeLine{1327 \textcolor{comment}{                                                       !! due to free convection [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1328 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(in)}    :: dKE\_FC\textcolor{comment}{  !< The vertically integrated change in}}
\DoxyCodeLine{1329 \textcolor{comment}{                                                       !! kinetic energy due to free convection}}
\DoxyCodeLine{1330 \textcolor{comment}{                                                       !! [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1331 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1332                               \textcolor{keywordtype}{intent(in)}    :: cTKE\textcolor{comment}{    !< The buoyant turbulent kinetic energy}}
\DoxyCodeLine{1333 \textcolor{comment}{                                                       !! source due to convective adjustment}}
\DoxyCodeLine{1334 \textcolor{comment}{                                                       !! [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1335 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1336                               \textcolor{keywordtype}{intent(in)}    :: dKE\_CA\textcolor{comment}{  !< The vertically integrated change in}}
\DoxyCodeLine{1337 \textcolor{comment}{                                                       !! kinetic energy due to convective}}
\DoxyCodeLine{1338 \textcolor{comment}{                                                       !! adjustment [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1339 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(out)}   :: TKE\textcolor{comment}{     !< The turbulent kinetic energy available for}}
\DoxyCodeLine{1340 \textcolor{comment}{                                                       !! mixing over a time step [Z m2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1341 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(out)}   :: Idecay\_len\_TKE\textcolor{comment}{ !< The inverse of the vertical decay}}
\DoxyCodeLine{1342 \textcolor{comment}{                                                       !! scale for TKE [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{1343 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(in)}    :: TKE\_river\textcolor{comment}{ !< The source of turbulent kinetic energy}}
\DoxyCodeLine{1344 \textcolor{comment}{                                                       !! available for driving mixing at river mouths}}
\DoxyCodeLine{1345 \textcolor{comment}{                                                       !! [Z L2 T-\/3 \string~> m3 s-\/3].}}
\DoxyCodeLine{1346 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(2,SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: cMKE\textcolor{comment}{    !< Coefficients of HpE and HpE\string^2 in}}
\DoxyCodeLine{1347 \textcolor{comment}{                                                       !! calculating the denominator of MKE\_rate,}}
\DoxyCodeLine{1348 \textcolor{comment}{                                                       !! [H-\/1 \string~> m-\/1 or m2 kg-\/1] and [H-\/2 \string~> m-\/2 or m4 kg-\/2].}}
\DoxyCodeLine{1349 \textcolor{keywordtype}{  real},                       \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{      !< The time step [T \string~> s].}}
\DoxyCodeLine{1350 \textcolor{keywordtype}{  real},                       \textcolor{keywordtype}{intent(in)}    :: Idt\_diag\textcolor{comment}{ !< The inverse of the accumulated diagnostic}}
\DoxyCodeLine{1351 \textcolor{comment}{                                                       !! time interval [T-\/1 \string~> s-\/1].}}
\DoxyCodeLine{1352   \textcolor{keywordtype}{integer},                    \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{       !< The j-\/index to work on.}}
\DoxyCodeLine{1353   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1354                               \textcolor{keywordtype}{intent(in)}    :: ksort\textcolor{comment}{   !< The density-\/sorted k-\/indicies.}}
\DoxyCodeLine{1355   \textcolor{keywordtype}{type}(bulkmixedlayer\_CS),    \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{      !< The control structure for this module.}}
\DoxyCodeLine{1356 }
\DoxyCodeLine{1357 \textcolor{comment}{!   This subroutine determines the TKE available at the depth of free}}
\DoxyCodeLine{1358 \textcolor{comment}{! convection to drive mechanical entrainment.}}
\DoxyCodeLine{1359 }
\DoxyCodeLine{1360   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1361 \textcolor{keywordtype}{  real} :: dKE\_conv  \textcolor{comment}{! The change in mean kinetic energy due to all convection [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1362 \textcolor{keywordtype}{  real} :: nstar\_FC  \textcolor{comment}{! The effective efficiency with which the energy released by}}
\DoxyCodeLine{1363                     \textcolor{comment}{! free convection is converted to TKE, often \string~0.2 [nondim].}}
\DoxyCodeLine{1364 \textcolor{keywordtype}{  real} :: nstar\_CA  \textcolor{comment}{! The effective efficiency with which the energy released by}}
\DoxyCodeLine{1365                     \textcolor{comment}{! convective adjustment is converted to TKE, often \string~0.2 [nondim].}}
\DoxyCodeLine{1366 \textcolor{keywordtype}{  real} :: TKE\_CA    \textcolor{comment}{! The potential energy released by convective adjustment if}}
\DoxyCodeLine{1367                     \textcolor{comment}{! that release is positive [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1368 \textcolor{keywordtype}{  real} :: MKE\_rate\_CA \textcolor{comment}{! MKE\_rate for convective adjustment [nondim], 0 to 1.}}
\DoxyCodeLine{1369 \textcolor{keywordtype}{  real} :: MKE\_rate\_FC \textcolor{comment}{! MKE\_rate for free convection [nondim], 0 to 1.}}
\DoxyCodeLine{1370 \textcolor{keywordtype}{  real} :: totEn\_Z   \textcolor{comment}{! The total potential energy released by convection, [Z3 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1371 \textcolor{keywordtype}{  real} :: Ih        \textcolor{comment}{! The inverse of a thickness [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{1372 \textcolor{keywordtype}{  real} :: exp\_kh    \textcolor{comment}{! The nondimensional decay of TKE across a layer [nondim].}}
\DoxyCodeLine{1373 \textcolor{keywordtype}{  real} :: absf      \textcolor{comment}{! The absolute value of f averaged to thickness points [T-\/1 \string~> s-\/1].}}
\DoxyCodeLine{1374 \textcolor{keywordtype}{  real} :: U\_star    \textcolor{comment}{! The friction velocity [Z T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{1375 \textcolor{keywordtype}{  real} :: absf\_Ustar  \textcolor{comment}{! The absolute value of f divided by U\_star [Z-\/1 \string~> m-\/1].}}
\DoxyCodeLine{1376 \textcolor{keywordtype}{  real} :: wind\_TKE\_src \textcolor{comment}{! The surface wind source of TKE [Z L2 T-\/3 \string~> m3 s-\/3].}}
\DoxyCodeLine{1377 \textcolor{keywordtype}{  real} :: diag\_wt   \textcolor{comment}{! The ratio of the current timestep to the diagnostic}}
\DoxyCodeLine{1378                     \textcolor{comment}{! timestep (which may include 2 calls) [nondim].}}
\DoxyCodeLine{1379   \textcolor{keywordtype}{integer} :: is, ie, nz, i}
\DoxyCodeLine{1380 }
\DoxyCodeLine{1381   is = g\%isc ; ie = g\%iec ; nz = gv\%ke}
\DoxyCodeLine{1382   diag\_wt = dt * idt\_diag}
\DoxyCodeLine{1383 }
\DoxyCodeLine{1384   \textcolor{keywordflow}{if} (cs\%omega\_frac >= 1.0) absf = 2.0*cs\%omega}
\DoxyCodeLine{1385   \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1386     u\_star = fluxes\%ustar(i,j)}
\DoxyCodeLine{1387     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes\%ustar\_shelf) .and. \textcolor{keyword}{associated}(fluxes\%frac\_shelf\_h)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1388       \textcolor{keywordflow}{if} (fluxes\%frac\_shelf\_h(i,j) > 0.0) \&}
\DoxyCodeLine{1389         u\_star = (1.0 -\/ fluxes\%frac\_shelf\_h(i,j)) * u\_star + \&}
\DoxyCodeLine{1390                   fluxes\%frac\_shelf\_h(i,j) * fluxes\%ustar\_shelf(i,j)}
\DoxyCodeLine{1391 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1392 }
\DoxyCodeLine{1393     \textcolor{keywordflow}{if} (u\_star < cs\%ustar\_min) u\_star = cs\%ustar\_min}
\DoxyCodeLine{1394     \textcolor{keywordflow}{if} (cs\%omega\_frac < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1395       absf = 0.25*((abs(g\%CoriolisBu(i,j)) + abs(g\%CoriolisBu(i-\/1,j-\/1))) + \&}
\DoxyCodeLine{1396                    (abs(g\%CoriolisBu(i,j-\/1)) + abs(g\%CoriolisBu(i-\/1,j))))}
\DoxyCodeLine{1397       \textcolor{keywordflow}{if} (cs\%omega\_frac > 0.0) \&}
\DoxyCodeLine{1398         absf = sqrt(cs\%omega\_frac*4.0*cs\%omega**2 + (1.0-\/cs\%omega\_frac)*absf**2)}
\DoxyCodeLine{1399 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1400     absf\_ustar = absf / u\_star}
\DoxyCodeLine{1401     idecay\_len\_tke(i) = (absf\_ustar * cs\%TKE\_decay) * gv\%H\_to\_Z}
\DoxyCodeLine{1402 }
\DoxyCodeLine{1403 \textcolor{comment}{!    The first number in the denominator could be anywhere up to 16.  The}}
\DoxyCodeLine{1404 \textcolor{comment}{!  value of 3 was chosen to minimize the time-\/step dependence of the amount}}
\DoxyCodeLine{1405 \textcolor{comment}{!  of shear-\/driven mixing in 10 days of a 1-\/degree global model, emphasizing}}
\DoxyCodeLine{1406 \textcolor{comment}{!  the equatorial areas.  Although it is not cast as a parameter, it should}}
\DoxyCodeLine{1407 \textcolor{comment}{!  be considered an empirical parameter, and it might depend strongly on the}}
\DoxyCodeLine{1408 \textcolor{comment}{!  number of sublayers in the mixed layer and their locations.}}
\DoxyCodeLine{1409 \textcolor{comment}{!  The 0.41 is VonKarman's constant.  This equation assumes that small \& large}}
\DoxyCodeLine{1410 \textcolor{comment}{!  scales contribute to mixed layer deepening at similar rates, even though}}
\DoxyCodeLine{1411 \textcolor{comment}{!  small scales are dissipated more rapidly (implying they are less efficient).}}
\DoxyCodeLine{1412 \textcolor{comment}{!     Ih = 1.0/(16.0*0.41*U\_star*dt)}}
\DoxyCodeLine{1413     ih = gv\%H\_to\_Z/(3.0*0.41*u\_star*dt)}
\DoxyCodeLine{1414     cmke(1,i) = 4.0 * ih ; cmke(2,i) = (absf\_ustar*gv\%H\_to\_Z) * ih}
\DoxyCodeLine{1415 }
\DoxyCodeLine{1416     \textcolor{keywordflow}{if} (idecay\_len\_tke(i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1417       exp\_kh = exp(-\/htot(i)*idecay\_len\_tke(i))}
\DoxyCodeLine{1418     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1419       exp\_kh = 1.0}
\DoxyCodeLine{1420 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1421 }
\DoxyCodeLine{1422 \textcolor{comment}{! Here nstar is a function of the natural Rossby number  0.2/(1+0.2/Ro), based}}
\DoxyCodeLine{1423 \textcolor{comment}{! on a curve fit from the data of Wang (GRL, 2003).}}
\DoxyCodeLine{1424 \textcolor{comment}{! Note:         Ro = 1.0/sqrt(0.5 * dt * (absf*htot(i))**3 / totEn)}}
\DoxyCodeLine{1425     \textcolor{keywordflow}{if} (conv\_en(i) < 0.0) conv\_en(i) = 0.0}
\DoxyCodeLine{1426     \textcolor{keywordflow}{if} (ctke(i,1) > 0.0) \textcolor{keywordflow}{then} ; tke\_ca = ctke(i,1) ; \textcolor{keywordflow}{else} ; tke\_ca = 0.0 ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1427     \textcolor{keywordflow}{if} ((htot(i) >= h\_ca(i)) .or. (tke\_ca == 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1428       toten\_z = us\%L\_to\_Z**2 * (conv\_en(i) + tke\_ca)}
\DoxyCodeLine{1429 }
\DoxyCodeLine{1430       \textcolor{keywordflow}{if} (toten\_z > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1431         nstar\_fc = cs\%nstar * toten\_z / (toten\_z + 0.2 * \&}
\DoxyCodeLine{1432                         sqrt(0.5 * dt * (absf*(htot(i)*gv\%H\_to\_Z))**3 * toten\_z))}
\DoxyCodeLine{1433       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1434         nstar\_fc = cs\%nstar}
\DoxyCodeLine{1435 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1436       nstar\_ca = nstar\_fc}
\DoxyCodeLine{1437     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1438       \textcolor{comment}{! This reconstructs the Buoyancy flux within the topmost htot of water.}}
\DoxyCodeLine{1439       \textcolor{keywordflow}{if} (conv\_en(i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1440         toten\_z = us\%L\_to\_Z**2 * (conv\_en(i) + tke\_ca * (htot(i) / h\_ca(i)) )}
\DoxyCodeLine{1441         nstar\_fc = cs\%nstar * toten\_z / (toten\_z + 0.2 * \&}
\DoxyCodeLine{1442                         sqrt(0.5 * dt * (absf*(htot(i)*gv\%H\_to\_Z))**3 * toten\_z))}
\DoxyCodeLine{1443       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1444         nstar\_fc = cs\%nstar}
\DoxyCodeLine{1445 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1446 }
\DoxyCodeLine{1447       toten\_z = us\%L\_to\_Z**2 * (conv\_en(i) + tke\_ca)}
\DoxyCodeLine{1448       \textcolor{keywordflow}{if} (tke\_ca > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1449         nstar\_ca = cs\%nstar * toten\_z / (toten\_z + 0.2 * \&}
\DoxyCodeLine{1450                         sqrt(0.5 * dt * (absf*(h\_ca(i)*gv\%H\_to\_Z))**3 * toten\_z))}
\DoxyCodeLine{1451       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1452         nstar\_ca = cs\%nstar}
\DoxyCodeLine{1453 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1454 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1455 }
\DoxyCodeLine{1456     \textcolor{keywordflow}{if} (dke\_fc(i) + dke\_ca(i,1) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1457       \textcolor{keywordflow}{if} (htot(i) >= h\_ca(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1458         mke\_rate\_fc = 1.0 / (1.0 + htot(i)*(cmke(1,i) + cmke(2,i)*htot(i)) )}
\DoxyCodeLine{1459         mke\_rate\_ca = mke\_rate\_fc}
\DoxyCodeLine{1460       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1461         mke\_rate\_fc = 1.0 / (1.0 + htot(i)*(cmke(1,i) + cmke(2,i)*htot(i)) )}
\DoxyCodeLine{1462         mke\_rate\_ca = 1.0 / (1.0 + h\_ca(i)*(cmke(1,i) + cmke(2,i)*h\_ca(i)) )}
\DoxyCodeLine{1463 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1464     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1465       \textcolor{comment}{! This branch just saves unnecessary calculations.}}
\DoxyCodeLine{1466       mke\_rate\_fc = 1.0 ; mke\_rate\_ca = 1.0}
\DoxyCodeLine{1467 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1468 }
\DoxyCodeLine{1469     dke\_conv = dke\_ca(i,1) * mke\_rate\_ca + dke\_fc(i) * mke\_rate\_fc}
\DoxyCodeLine{1470 \textcolor{comment}{! At this point, it is assumed that cTKE is positive and stored in TKE\_CA!}}
\DoxyCodeLine{1471 \textcolor{comment}{! Note: Removed factor of 2 in u*\string^3 terms.}}
\DoxyCodeLine{1472     tke(i) = (dt*cs\%mstar)*((us\%Z\_to\_L**2*(u\_star*u\_star*u\_star))*exp\_kh) + \&}
\DoxyCodeLine{1473              (exp\_kh * dke\_conv + nstar\_fc*conv\_en(i) + nstar\_ca * tke\_ca)}
\DoxyCodeLine{1474 }
\DoxyCodeLine{1475     \textcolor{keywordflow}{if} (cs\%do\_rivermix) \textcolor{keywordflow}{then} \textcolor{comment}{! Add additional TKE at river mouths}}
\DoxyCodeLine{1476       tke(i) = tke(i) + tke\_river(i)*dt*exp\_kh}
\DoxyCodeLine{1477 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1478 }
\DoxyCodeLine{1479     \textcolor{keywordflow}{if} (cs\%TKE\_diagnostics) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1480       wind\_tke\_src = cs\%mstar*(us\%Z\_to\_L**2*u\_star*u\_star*u\_star) * diag\_wt}
\DoxyCodeLine{1481       cs\%diag\_TKE\_wind(i,j) = cs\%diag\_TKE\_wind(i,j) + \&}
\DoxyCodeLine{1482           ( wind\_tke\_src + tke\_river(i) * diag\_wt )}
\DoxyCodeLine{1483       cs\%diag\_TKE\_RiBulk(i,j) = cs\%diag\_TKE\_RiBulk(i,j) + dke\_conv*idt\_diag}
\DoxyCodeLine{1484       cs\%diag\_TKE\_mech\_decay(i,j) = cs\%diag\_TKE\_mech\_decay(i,j) + \&}
\DoxyCodeLine{1485           (exp\_kh-\/1.0)*(wind\_tke\_src + dke\_conv*idt\_diag)}
\DoxyCodeLine{1486       cs\%diag\_TKE\_conv(i,j) = cs\%diag\_TKE\_conv(i,j) + \&}
\DoxyCodeLine{1487           idt\_diag * (nstar\_fc*conv\_en(i) + nstar\_ca*tke\_ca)}
\DoxyCodeLine{1488       cs\%diag\_TKE\_conv\_decay(i,j) = cs\%diag\_TKE\_conv\_decay(i,j) + \&}
\DoxyCodeLine{1489           idt\_diag * ((cs\%nstar-\/nstar\_fc)*conv\_en(i) + (cs\%nstar-\/nstar\_ca)*tke\_ca)}
\DoxyCodeLine{1490       cs\%diag\_TKE\_conv\_s2(i,j) = cs\%diag\_TKE\_conv\_s2(i,j) + \&}
\DoxyCodeLine{1491           idt\_diag * (ctke(i,1)-\/tke\_ca)}
\DoxyCodeLine{1492 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1493 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1494 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_aae11f02b6b843d50866b7e259a7d468a}\label{namespacemom__bulk__mixed__layer_aae11f02b6b843d50866b7e259a7d468a}} 
\index{mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}!mechanical\_entrainment@{mechanical\_entrainment}}
\index{mechanical\_entrainment@{mechanical\_entrainment}!mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}}
\doxysubsubsection{\texorpdfstring{mechanical\_entrainment()}{mechanical\_entrainment()}}
{\footnotesize\ttfamily subroutine mom\+\_\+bulk\+\_\+mixed\+\_\+layer\+::mechanical\+\_\+entrainment (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{h,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{d\+\_\+eb,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(inout)}]{htot,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(inout)}]{Ttot,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(inout)}]{Stot,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(inout)}]{uhtot,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(inout)}]{vhtot,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(inout)}]{R0\+\_\+tot,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(inout)}]{Rcv\+\_\+tot,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{u,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{v,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{T,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{S,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{R0,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{Rcv,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{eps,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{d\+R0\+\_\+dT,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{d\+Rcv\+\_\+dT,  }\item[{real, dimension(2, g \%isd\+: g \%ied), intent(in)}]{c\+M\+KE,  }\item[{real, intent(in)}]{Idt\+\_\+diag,  }\item[{integer, intent(in)}]{nsw,  }\item[{real, dimension(max(nsw,1), g \%isd\+: g \%ied), intent(inout)}]{Pen\+\_\+\+S\+W\+\_\+bnd,  }\item[{real, dimension(max(nsw,1), g \%isd\+: g \%ied, gv \%ke), intent(in)}]{opacity\+\_\+band,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(inout)}]{T\+KE,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(inout)}]{Idecay\+\_\+len\+\_\+\+T\+KE,  }\item[{integer, intent(in)}]{j,  }\item[{integer, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{ksort,  }\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__bulk__mixed__layer_1_1bulkmixedlayer__cs}{bulkmixedlayer\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine calculates mechanically driven entrainment. 


\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,out}}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em d\+\_\+eb} & The downward increase across a layer in the \\
\hline
\mbox{\texttt{ in,out}}  & {\em htot} & The accumlated mixed layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em ttot} & The depth integrated mixed layer temperature \mbox{[}degC H $\sim$$>$ degC m or degC kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em stot} & The depth integrated mixed layer salinity \mbox{[}ppt H $\sim$$>$ ppt m or ppt kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em uhtot} & The depth integrated mixed layer zonal velocity \mbox{[}H L T-\/1 $\sim$$>$ m2 s-\/1 or kg m-\/1 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em vhtot} & The integrated mixed layer meridional velocity \mbox{[}H L T-\/1 $\sim$$>$ m2 s-\/1 or kg m-\/1 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em r0\+\_\+tot} & The integrated mixed layer potential density referenced to 0 pressure \mbox{[}H R $\sim$$>$ kg m-\/2 or kg2 m-\/5\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em rcv\+\_\+tot} & The integrated mixed layer coordinate variable potential density \mbox{[}H R $\sim$$>$ kg m-\/2 or kg2 m-\/5\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em u} & Zonal velocities interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em v} & Zonal velocities interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em t} & Layer temperatures \mbox{[}degC\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em s} & Layer salinities \mbox{[}ppt\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em r0} & Potential density referenced to \\
\hline
\mbox{\texttt{ in}}  & {\em rcv} & The coordinate defining potential \\
\hline
\mbox{\texttt{ in}}  & {\em eps} & The negligibly small amount of water \\
\hline
\mbox{\texttt{ in}}  & {\em dr0\+\_\+dt} & The partial derivative of R0 with respect to temperature \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em drcv\+\_\+dt} & The partial derivative of Rcv with respect to temperature \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em cmke} & Coefficients of HpE and Hp\+E$^\wedge$2 used in calculating the denominator of M\+K\+E\+\_\+rate; the two elements have differing units of \mbox{[}H-\/1 $\sim$$>$ m-\/1 or m2 kg-\/1\mbox{]} and \mbox{[}H-\/2 $\sim$$>$ m-\/2 or m4 kg-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em idt\+\_\+diag} & The inverse of the accumulated diagnostic time interval \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em nsw} & The number of bands of penetrating shortwave radiation. \\
\hline
\mbox{\texttt{ in,out}}  & {\em pen\+\_\+sw\+\_\+bnd} & The penetrating shortwave heating at the sea surface in each penetrating band \mbox{[}degC H $\sim$$>$ degC m or degC kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em opacity\+\_\+band} & The opacity in each band of penetrating shortwave radiation \mbox{[}H-\/1 $\sim$$>$ m-\/1 or m2 kg-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em tke} & The turbulent kinetic energy available for mixing over a time step \mbox{[}Z m2 T-\/2 $\sim$$>$ m3 s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em idecay\+\_\+len\+\_\+tke} & The vertical T\+KE decay rate \mbox{[}H-\/1 $\sim$$>$ m-\/1 or m2 kg-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em j} & The j-\/index to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em ksort} & The density-\/sorted k-\/indicies. \\
\hline
 & {\em cs} & The control structure for this module. \\
\hline
\end{DoxyParams}


Definition at line 1503 of file M\+O\+M\+\_\+bulk\+\_\+mixed\+\_\+layer.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1503   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{     !< The ocean's grid structure.}}
\DoxyCodeLine{1504   \textcolor{keywordtype}{type}(verticalGrid\_type),  \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{    !< The ocean's vertical grid structure.}}
\DoxyCodeLine{1505   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{    !< A dimensional unit scaling type}}
\DoxyCodeLine{1506 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1507                             \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{     !< Layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1508 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1509                             \textcolor{keywordtype}{intent(inout)} :: d\_eb\textcolor{comment}{  !< The downward increase across a layer in the}}
\DoxyCodeLine{1510 \textcolor{comment}{                                                   !! layer in the entrainment from below [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1511 \textcolor{comment}{                                                   !! Positive values go with mass gain by a layer.}}
\DoxyCodeLine{1512 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: htot\textcolor{comment}{  !< The accumlated mixed layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1513 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: Ttot\textcolor{comment}{  !< The depth integrated mixed layer temperature}}
\DoxyCodeLine{1514 \textcolor{comment}{                                                   !! [degC H \string~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{1515 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: Stot\textcolor{comment}{  !< The depth integrated mixed layer salinity}}
\DoxyCodeLine{1516 \textcolor{comment}{                                                   !! [ppt H \string~> ppt m or ppt kg m-\/2].}}
\DoxyCodeLine{1517 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: uhtot\textcolor{comment}{ !< The depth integrated mixed layer zonal}}
\DoxyCodeLine{1518 \textcolor{comment}{                                                   !! velocity [H L T-\/1 \string~> m2 s-\/1 or kg m-\/1 s-\/1].}}
\DoxyCodeLine{1519 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: vhtot\textcolor{comment}{ !< The integrated mixed layer meridional}}
\DoxyCodeLine{1520 \textcolor{comment}{                                                   !! velocity [H L T-\/1 \string~> m2 s-\/1 or kg m-\/1 s-\/1].}}
\DoxyCodeLine{1521 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: R0\_tot\textcolor{comment}{ !< The integrated mixed layer potential density}}
\DoxyCodeLine{1522 \textcolor{comment}{                                                   !! referenced to 0 pressure [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{1523 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: Rcv\_tot\textcolor{comment}{ !< The integrated mixed layer coordinate variable}}
\DoxyCodeLine{1524 \textcolor{comment}{                                                   !! potential density [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{1525 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1526                             \textcolor{keywordtype}{intent(in)}    :: u\textcolor{comment}{     !< Zonal velocities interpolated to h points [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{1527 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1528                             \textcolor{keywordtype}{intent(in)}    :: v\textcolor{comment}{     !< Zonal velocities interpolated to h points [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{1529 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1530                             \textcolor{keywordtype}{intent(in)}    :: T\textcolor{comment}{     !< Layer temperatures [degC].}}
\DoxyCodeLine{1531 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1532                             \textcolor{keywordtype}{intent(in)}    :: S\textcolor{comment}{     !< Layer salinities [ppt].}}
\DoxyCodeLine{1533 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1534                             \textcolor{keywordtype}{intent(in)}    :: R0\textcolor{comment}{    !< Potential density referenced to}}
\DoxyCodeLine{1535 \textcolor{comment}{                                                   !! surface pressure [R \string~> kg m-\/3].}}
\DoxyCodeLine{1536 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1537                             \textcolor{keywordtype}{intent(in)}    :: Rcv\textcolor{comment}{   !< The coordinate defining potential}}
\DoxyCodeLine{1538 \textcolor{comment}{                                                   !! density [R \string~> kg m-\/3].}}
\DoxyCodeLine{1539 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1540                             \textcolor{keywordtype}{intent(in)}    :: eps\textcolor{comment}{   !< The negligibly small amount of water}}
\DoxyCodeLine{1541 \textcolor{comment}{                                                   !! that will be left in each layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1542 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: dR0\_dT\textcolor{comment}{  !< The partial derivative of R0 with respect to}}
\DoxyCodeLine{1543 \textcolor{comment}{                                                   !! temperature [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{1544 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: dRcv\_dT\textcolor{comment}{ !< The partial derivative of Rcv with respect to}}
\DoxyCodeLine{1545 \textcolor{comment}{                                                   !! temperature [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{1546 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(2,SZI\_(G))}, \textcolor{keywordtype}{intent(in)}  :: cMKE\textcolor{comment}{  !< Coefficients of HpE and HpE\string^2 used in calculating the}}
\DoxyCodeLine{1547 \textcolor{comment}{                                                   !! denominator of MKE\_rate; the two elements have differing}}
\DoxyCodeLine{1548 \textcolor{comment}{                                                   !! units of [H-\/1 \string~> m-\/1 or m2 kg-\/1] and [H-\/2 \string~> m-\/2 or m4 kg-\/2].}}
\DoxyCodeLine{1549 \textcolor{keywordtype}{  real},                     \textcolor{keywordtype}{intent(in)}    :: Idt\_diag\textcolor{comment}{ !< The inverse of the accumulated diagnostic}}
\DoxyCodeLine{1550 \textcolor{comment}{                                                   !! time interval [T-\/1 \string~> s-\/1].}}
\DoxyCodeLine{1551   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}    :: nsw\textcolor{comment}{   !< The number of bands of penetrating}}
\DoxyCodeLine{1552 \textcolor{comment}{                                                   !! shortwave radiation.}}
\DoxyCodeLine{1553 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(max(nsw,1),SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: Pen\_SW\_bnd\textcolor{comment}{ !< The penetrating shortwave}}
\DoxyCodeLine{1554 \textcolor{comment}{                                                   !! heating at the sea surface in each penetrating}}
\DoxyCodeLine{1555 \textcolor{comment}{                                                   !! band [degC H \string~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{1556 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(max(nsw,1),SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(in)} :: opacity\_band\textcolor{comment}{ !< The opacity in each band of}}
\DoxyCodeLine{1557 \textcolor{comment}{                                                   !! penetrating shortwave radiation [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{1558 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: TKE\textcolor{comment}{   !< The turbulent kinetic energy}}
\DoxyCodeLine{1559 \textcolor{comment}{                                                   !! available for mixing over a time}}
\DoxyCodeLine{1560 \textcolor{comment}{                                                   !! step [Z m2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1561 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: Idecay\_len\_TKE\textcolor{comment}{ !< The vertical TKE decay rate [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{1562   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{     !< The j-\/index to work on.}}
\DoxyCodeLine{1563   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1564                             \textcolor{keywordtype}{intent(in)}    :: ksort\textcolor{comment}{ !< The density-\/sorted k-\/indicies.}}
\DoxyCodeLine{1565   \textcolor{keywordtype}{type}(bulkmixedlayer\_CS),  \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{    !< The control structure for this module.}}
\DoxyCodeLine{1566 }
\DoxyCodeLine{1567 \textcolor{comment}{! This subroutine calculates mechanically driven entrainment.}}
\DoxyCodeLine{1568 }
\DoxyCodeLine{1569   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1570 \textcolor{keywordtype}{  real} :: SW\_trans  \textcolor{comment}{!   The fraction of shortwave radiation that is not}}
\DoxyCodeLine{1571                     \textcolor{comment}{! absorbed in a layer, nondimensional.}}
\DoxyCodeLine{1572 \textcolor{keywordtype}{  real} :: Pen\_absorbed  \textcolor{comment}{!   The amount of penetrative shortwave radiation}}
\DoxyCodeLine{1573                         \textcolor{comment}{! that is absorbed in a layer [degC H \string~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{1574 \textcolor{keywordtype}{  real} :: h\_avail   \textcolor{comment}{! The thickness in a layer available for entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1575 \textcolor{keywordtype}{  real} :: h\_ent     \textcolor{comment}{! The thickness from a layer that is entrained [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1576 \textcolor{keywordtype}{  real} :: h\_min, h\_max \textcolor{comment}{! Limits on the solution for h\_ent [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1577 \textcolor{keywordtype}{  real} :: dh\_Newt      \textcolor{comment}{!   The Newton's method estimate of the change in}}
\DoxyCodeLine{1578                        \textcolor{comment}{! h\_ent between iterations [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1579 \textcolor{keywordtype}{  real} :: MKE\_rate  \textcolor{comment}{!   The fraction of the energy in resolved shears}}
\DoxyCodeLine{1580                     \textcolor{comment}{! within the mixed layer that will be eliminated}}
\DoxyCodeLine{1581                     \textcolor{comment}{! within a timestep, nondim, 0 to 1.}}
\DoxyCodeLine{1582 \textcolor{keywordtype}{  real} :: HpE       \textcolor{comment}{!   The current thickness plus entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1583 \textcolor{keywordtype}{  real} :: g\_H\_2Rho0   \textcolor{comment}{!   Half the gravitational acceleration times the}}
\DoxyCodeLine{1584                       \textcolor{comment}{! conversion from H to m divided by the mean density,}}
\DoxyCodeLine{1585                       \textcolor{comment}{! in [L2 T-\/2 H-\/1 R-\/1 \string~> m4 s-\/2 kg-\/1 or m7 s-\/2 kg-\/2].}}
\DoxyCodeLine{1586 \textcolor{keywordtype}{  real} :: TKE\_full\_ent  \textcolor{comment}{! The TKE remaining if a layer is fully entrained}}
\DoxyCodeLine{1587                         \textcolor{comment}{! [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1588 \textcolor{keywordtype}{  real} :: dRL       \textcolor{comment}{! Work required to mix water from the next layer}}
\DoxyCodeLine{1589                     \textcolor{comment}{! across the mixed layer [L2 T-\/2 \string~> L2 s-\/2].}}
\DoxyCodeLine{1590 \textcolor{keywordtype}{  real} :: Pen\_En\_Contrib  \textcolor{comment}{! Penetrating SW contributions to the changes in}}
\DoxyCodeLine{1591                           \textcolor{comment}{! TKE, divided by layer thickness in m [L2 T2 \string~> m2 s-\/2].}}
\DoxyCodeLine{1592 \textcolor{keywordtype}{  real} :: Cpen1     \textcolor{comment}{! A temporary variable [L2 T-\/2 \string~> m2 s-\/2].}}
\DoxyCodeLine{1593 \textcolor{keywordtype}{  real} :: dMKE      \textcolor{comment}{! A temporary variable related to the release of mean}}
\DoxyCodeLine{1594                     \textcolor{comment}{! kinetic energy [H Z L2 T-\/2 \string~> m4 s-\/2 or kg m s-\/2]}}
\DoxyCodeLine{1595 \textcolor{keywordtype}{  real} :: TKE\_ent   \textcolor{comment}{! The TKE that remains if h\_ent were entrained [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1596 \textcolor{keywordtype}{  real} :: TKE\_ent1  \textcolor{comment}{! The TKE that would remain, without considering the}}
\DoxyCodeLine{1597                     \textcolor{comment}{! release of mean kinetic energy [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1598 \textcolor{keywordtype}{  real} :: dTKE\_dh   \textcolor{comment}{! The partial derivative of TKE with h\_ent [Z L2 T-\/2 H-\/1 \string~> m2 s-\/2 or m5 s-\/2 kg-\/1].}}
\DoxyCodeLine{1599 \textcolor{keywordtype}{  real} :: Pen\_dTKE\_dh\_Contrib \textcolor{comment}{! The penetrating shortwave contribution to}}
\DoxyCodeLine{1600                     \textcolor{comment}{! dTKE\_dh [L2 T-\/2 \string~> m2 s-\/2].}}
\DoxyCodeLine{1601 \textcolor{keywordtype}{  real} :: EF4\_val   \textcolor{comment}{! The result of EF4() (see later) [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{1602 \textcolor{keywordtype}{  real} :: h\_neglect \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{1603                     \textcolor{comment}{! in roundoff and can be neglected [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1604 \textcolor{keywordtype}{  real} :: dEF4\_dh   \textcolor{comment}{! The partial derivative of EF4 with h [H-\/2 \string~> m-\/2 or m4 kg-\/2].}}
\DoxyCodeLine{1605 \textcolor{keywordtype}{  real} :: Pen\_En1   \textcolor{comment}{! A nondimensional temporary variable.}}
\DoxyCodeLine{1606 \textcolor{keywordtype}{  real} :: kh, exp\_kh  \textcolor{comment}{! Nondimensional temporary variables related to the}}
\DoxyCodeLine{1607 \textcolor{keywordtype}{  real} :: f1\_kh       \textcolor{comment}{! fractional decay of TKE across a layer.}}
\DoxyCodeLine{1608 \textcolor{keywordtype}{  real} :: x1, e\_x1      \textcolor{comment}{!   Nondimensional temporary variables related to}}
\DoxyCodeLine{1609 \textcolor{keywordtype}{  real} :: f1\_x1, f2\_x1  \textcolor{comment}{! the relative decay of TKE and SW radiation across}}
\DoxyCodeLine{1610 \textcolor{keywordtype}{  real} :: f3\_x1         \textcolor{comment}{! a layer, and exponential-\/related functions of x1.}}
\DoxyCodeLine{1611 \textcolor{keywordtype}{  real} :: E\_HxHpE   \textcolor{comment}{! Entrainment divided by the product of the new and old}}
\DoxyCodeLine{1612                     \textcolor{comment}{! thicknesses [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{1613 \textcolor{keywordtype}{  real} :: Hmix\_min  \textcolor{comment}{! The minimum mixed layer depth [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1614 \textcolor{keywordtype}{  real} :: opacity}
\DoxyCodeLine{1615 \textcolor{keywordtype}{  real} :: C1\_3, C1\_6, C1\_24   \textcolor{comment}{!  1/3, 1/6, and 1/24.}}
\DoxyCodeLine{1616   \textcolor{keywordtype}{integer} :: is, ie, nz, i, k, ks, itt, n}
\DoxyCodeLine{1617 }
\DoxyCodeLine{1618   c1\_3 = 1.0/3.0 ; c1\_6 = 1.0/6.0 ; c1\_24 = 1.0/24.0}
\DoxyCodeLine{1619   g\_h\_2rho0 = (gv\%g\_Earth * gv\%H\_to\_Z) / (2.0 * gv\%Rho0)}
\DoxyCodeLine{1620   hmix\_min = cs\%Hmix\_min}
\DoxyCodeLine{1621   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{1622   is = g\%isc ; ie = g\%iec ; nz = gv\%ke}
\DoxyCodeLine{1623 }
\DoxyCodeLine{1624   \textcolor{keywordflow}{do} ks=1,nz}
\DoxyCodeLine{1625 }
\DoxyCodeLine{1626     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (ksort(i,ks) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1627       k = ksort(i,ks)}
\DoxyCodeLine{1628 }
\DoxyCodeLine{1629       h\_avail = h(i,k) -\/ eps(i,k)}
\DoxyCodeLine{1630       \textcolor{keywordflow}{if} ((h\_avail > 0.) .and. ((tke(i) > 0.) .or. (htot(i) < hmix\_min))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1631         drl = g\_h\_2rho0 * (r0(i,k)*htot(i) -\/ r0\_tot(i) )}
\DoxyCodeLine{1632         dmke = (gv\%H\_to\_Z * cs\%bulk\_Ri\_ML) * 0.5 * \&}
\DoxyCodeLine{1633             ((uhtot(i)-\/u(i,k)*htot(i))**2 + (vhtot(i)-\/v(i,k)*htot(i))**2)}
\DoxyCodeLine{1634 }
\DoxyCodeLine{1635 \textcolor{comment}{! Find the TKE that would remain if the entire layer were entrained.}}
\DoxyCodeLine{1636         kh = idecay\_len\_tke(i)*h\_avail ; exp\_kh = exp(-\/kh)}
\DoxyCodeLine{1637         \textcolor{keywordflow}{if} (kh >= 2.0e-\/5) \textcolor{keywordflow}{then} ; f1\_kh = (1.0-\/exp\_kh) / kh}
\DoxyCodeLine{1638         \textcolor{keywordflow}{else} ; f1\_kh = (1.0 -\/ kh*(0.5 -\/ c1\_6*kh)) ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1639 }
\DoxyCodeLine{1640         pen\_en\_contrib = 0.0}
\DoxyCodeLine{1641         \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1642           opacity = opacity\_band(n,i,k)}
\DoxyCodeLine{1643 \textcolor{comment}{! Two different forms are used here to make sure that only negative}}
\DoxyCodeLine{1644 \textcolor{comment}{! values are taken into exponentials to avoid excessively large}}
\DoxyCodeLine{1645 \textcolor{comment}{! numbers.  They are, of course, mathematically identical.}}
\DoxyCodeLine{1646           \textcolor{keywordflow}{if} (idecay\_len\_tke(i) > opacity) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1647             x1 = (idecay\_len\_tke(i) -\/ opacity) * h\_avail}
\DoxyCodeLine{1648             \textcolor{keywordflow}{if} (x1 >= 2.0e-\/5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1649               e\_x1 = exp(-\/x1) ; f1\_x1 = ((1.0-\/e\_x1)/(x1))}
\DoxyCodeLine{1650               f3\_x1 = ((e\_x1-\/(1.0-\/x1))/(x1*x1))}
\DoxyCodeLine{1651             \textcolor{keywordflow}{else}}
\DoxyCodeLine{1652               f1\_x1 = (1.0 -\/ x1*(0.5 -\/ c1\_6*x1))}
\DoxyCodeLine{1653               f3\_x1 = (0.5 -\/ x1*(c1\_6 -\/ c1\_24*x1))}
\DoxyCodeLine{1654 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1655 }
\DoxyCodeLine{1656             pen\_en1 = exp(-\/opacity*h\_avail) * \&}
\DoxyCodeLine{1657                ((1.0+opacity*htot(i))*f1\_x1 + opacity*h\_avail*f3\_x1)}
\DoxyCodeLine{1658           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1659             x1 = (opacity -\/ idecay\_len\_tke(i)) * h\_avail}
\DoxyCodeLine{1660             \textcolor{keywordflow}{if} (x1 >= 2.0e-\/5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1661               e\_x1 = exp(-\/x1) ; f1\_x1 = ((1.0-\/e\_x1)/(x1))}
\DoxyCodeLine{1662               f2\_x1 = ((1.0-\/(1.0+x1)*e\_x1)/(x1*x1))}
\DoxyCodeLine{1663             \textcolor{keywordflow}{else}}
\DoxyCodeLine{1664               f1\_x1 = (1.0 -\/ x1*(0.5 -\/ c1\_6*x1))}
\DoxyCodeLine{1665               f2\_x1 = (0.5 -\/ x1*(c1\_3 -\/ 0.125*x1))}
\DoxyCodeLine{1666 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1667 }
\DoxyCodeLine{1668             pen\_en1 = exp\_kh * ((1.0+opacity*htot(i))*f1\_x1 + \&}
\DoxyCodeLine{1669                                  opacity*h\_avail*f2\_x1)}
\DoxyCodeLine{1670 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1671           pen\_en\_contrib = pen\_en\_contrib + \&}
\DoxyCodeLine{1672             (g\_h\_2rho0*dr0\_dt(i)*pen\_sw\_bnd(n,i)) * (pen\_en1 -\/ f1\_kh)}
\DoxyCodeLine{1673 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1674 }
\DoxyCodeLine{1675         hpe = htot(i)+h\_avail}
\DoxyCodeLine{1676         mke\_rate = 1.0/(1.0 + (cmke(1,i)*hpe + cmke(2,i)*hpe**2))}
\DoxyCodeLine{1677         ef4\_val = ef4(htot(i)+h\_neglect,h\_avail,idecay\_len\_tke(i))}
\DoxyCodeLine{1678         tke\_full\_ent = (exp\_kh*tke(i) -\/ (h\_avail*gv\%H\_to\_Z)*(drl*f1\_kh + pen\_en\_contrib)) + \&}
\DoxyCodeLine{1679             mke\_rate*dmke*ef4\_val}
\DoxyCodeLine{1680         \textcolor{keywordflow}{if} ((tke\_full\_ent >= 0.0) .or. (h\_avail+htot(i) <= hmix\_min)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1681           \textcolor{comment}{! The layer will be fully entrained.}}
\DoxyCodeLine{1682           h\_ent = h\_avail}
\DoxyCodeLine{1683 }
\DoxyCodeLine{1684           \textcolor{keywordflow}{if} (cs\%TKE\_diagnostics) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1685             e\_hxhpe = h\_ent / ((htot(i)+h\_neglect)*(htot(i)+h\_ent+h\_neglect))}
\DoxyCodeLine{1686             cs\%diag\_TKE\_mech\_decay(i,j) = cs\%diag\_TKE\_mech\_decay(i,j) + \&}
\DoxyCodeLine{1687                 idt\_diag * ((exp\_kh-\/1.0)* tke(i) + (h\_ent*gv\%H\_to\_Z)*drl*(1.0-\/f1\_kh) + \&}
\DoxyCodeLine{1688                             mke\_rate*dmke*(ef4\_val-\/e\_hxhpe))}
\DoxyCodeLine{1689             cs\%diag\_TKE\_mixing(i,j) = cs\%diag\_TKE\_mixing(i,j) -\/ \&}
\DoxyCodeLine{1690                 idt\_diag*(gv\%H\_to\_Z*h\_ent)*drl}
\DoxyCodeLine{1691             cs\%diag\_TKE\_pen\_SW(i,j) = cs\%diag\_TKE\_pen\_SW(i,j) -\/ \&}
\DoxyCodeLine{1692                 idt\_diag*(gv\%H\_to\_Z*h\_ent)*pen\_en\_contrib}
\DoxyCodeLine{1693             cs\%diag\_TKE\_RiBulk(i,j) = cs\%diag\_TKE\_RiBulk(i,j) + \&}
\DoxyCodeLine{1694                 idt\_diag*mke\_rate*dmke*e\_hxhpe}
\DoxyCodeLine{1695 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1696 }
\DoxyCodeLine{1697           tke(i) = tke\_full\_ent}
\DoxyCodeLine{1698           \textcolor{comment}{!\#\#\# The minimum TKE value in this line may be problematically small.}}
\DoxyCodeLine{1699           \textcolor{keywordflow}{if} (tke(i) <= 0.0) tke(i) = 1.0e-\/150*us\%m\_to\_Z*us\%m\_s\_to\_L\_T**2}
\DoxyCodeLine{1700         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1701 \textcolor{comment}{! The layer is only partially entrained.  The amount that will be}}
\DoxyCodeLine{1702 \textcolor{comment}{! entrained is determined iteratively.  No further layers will be}}
\DoxyCodeLine{1703 \textcolor{comment}{! entrained.}}
\DoxyCodeLine{1704           h\_min = 0.0 ; h\_max = h\_avail}
\DoxyCodeLine{1705           \textcolor{keywordflow}{if} (tke(i) <= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1706             h\_ent = 0.0}
\DoxyCodeLine{1707           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1708             h\_ent = h\_avail * tke(i) / (tke(i) -\/ tke\_full\_ent)}
\DoxyCodeLine{1709 }
\DoxyCodeLine{1710             \textcolor{keywordflow}{do} itt=1,15}
\DoxyCodeLine{1711               \textcolor{comment}{! Evaluate the TKE that would remain if h\_ent were entrained.}}
\DoxyCodeLine{1712 }
\DoxyCodeLine{1713               kh = idecay\_len\_tke(i)*h\_ent ; exp\_kh = exp(-\/kh)}
\DoxyCodeLine{1714               \textcolor{keywordflow}{if} (kh >= 2.0e-\/5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1715                 f1\_kh = (1.0-\/exp\_kh) / kh}
\DoxyCodeLine{1716               \textcolor{keywordflow}{else}}
\DoxyCodeLine{1717                 f1\_kh = (1.0 -\/ kh*(0.5 -\/ c1\_6*kh))}
\DoxyCodeLine{1718 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1719 }
\DoxyCodeLine{1720 }
\DoxyCodeLine{1721               pen\_en\_contrib = 0.0 ; pen\_dtke\_dh\_contrib = 0.0}
\DoxyCodeLine{1722               \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1723                 \textcolor{comment}{! Two different forms are used here to make sure that only negative}}
\DoxyCodeLine{1724                 \textcolor{comment}{! values are taken into exponentials to avoid excessively large}}
\DoxyCodeLine{1725                 \textcolor{comment}{! numbers.  They are, of course, mathematically identical.}}
\DoxyCodeLine{1726                 opacity = opacity\_band(n,i,k)}
\DoxyCodeLine{1727                 sw\_trans = exp(-\/h\_ent*opacity)}
\DoxyCodeLine{1728                 \textcolor{keywordflow}{if} (idecay\_len\_tke(i) > opacity) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1729                   x1 = (idecay\_len\_tke(i) -\/ opacity) * h\_ent}
\DoxyCodeLine{1730                   \textcolor{keywordflow}{if} (x1 >= 2.0e-\/5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1731                     e\_x1 = exp(-\/x1) ; f1\_x1 = ((1.0-\/e\_x1)/(x1))}
\DoxyCodeLine{1732                     f3\_x1 = ((e\_x1-\/(1.0-\/x1))/(x1*x1))}
\DoxyCodeLine{1733                   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1734                     f1\_x1 = (1.0 -\/ x1*(0.5 -\/ c1\_6*x1))}
\DoxyCodeLine{1735                     f3\_x1 = (0.5 -\/ x1*(c1\_6 -\/ c1\_24*x1))}
\DoxyCodeLine{1736 \textcolor{keywordflow}{                  endif}}
\DoxyCodeLine{1737                   pen\_en1 = sw\_trans * ((1.0+opacity*htot(i))*f1\_x1 + \&}
\DoxyCodeLine{1738                                           opacity*h\_ent*f3\_x1)}
\DoxyCodeLine{1739                 \textcolor{keywordflow}{else}}
\DoxyCodeLine{1740                   x1 = (opacity -\/ idecay\_len\_tke(i)) * h\_ent}
\DoxyCodeLine{1741                   \textcolor{keywordflow}{if} (x1 >= 2.0e-\/5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1742                     e\_x1 = exp(-\/x1) ; f1\_x1 = ((1.0-\/e\_x1)/(x1))}
\DoxyCodeLine{1743                     f2\_x1 = ((1.0-\/(1.0+x1)*e\_x1)/(x1*x1))}
\DoxyCodeLine{1744                   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1745                     f1\_x1 = (1.0 -\/ x1*(0.5 -\/ c1\_6*x1))}
\DoxyCodeLine{1746                     f2\_x1 = (0.5 -\/ x1*(c1\_3 -\/ 0.125*x1))}
\DoxyCodeLine{1747 \textcolor{keywordflow}{                  endif}}
\DoxyCodeLine{1748 }
\DoxyCodeLine{1749                   pen\_en1 = exp\_kh * ((1.0+opacity*htot(i))*f1\_x1 + \&}
\DoxyCodeLine{1750                                         opacity*h\_ent*f2\_x1)}
\DoxyCodeLine{1751 \textcolor{keywordflow}{                endif}}
\DoxyCodeLine{1752                 cpen1 = g\_h\_2rho0*dr0\_dt(i)*pen\_sw\_bnd(n,i)}
\DoxyCodeLine{1753                 pen\_en\_contrib = pen\_en\_contrib + cpen1*(pen\_en1 -\/ f1\_kh)}
\DoxyCodeLine{1754                 pen\_dtke\_dh\_contrib = pen\_dtke\_dh\_contrib + \&}
\DoxyCodeLine{1755                            cpen1*((1.0-\/sw\_trans) -\/ opacity*(htot(i) + h\_ent)*sw\_trans)}
\DoxyCodeLine{1756 \textcolor{keywordflow}{              endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! (Pen\_SW\_bnd(n,i) > 0.0)}}
\DoxyCodeLine{1757 }
\DoxyCodeLine{1758               tke\_ent1 = exp\_kh* tke(i) -\/ (h\_ent*gv\%H\_to\_Z)*(drl*f1\_kh + pen\_en\_contrib)}
\DoxyCodeLine{1759               ef4\_val = ef4(htot(i)+h\_neglect,h\_ent,idecay\_len\_tke(i),def4\_dh)}
\DoxyCodeLine{1760               hpe = htot(i)+h\_ent}
\DoxyCodeLine{1761               mke\_rate = 1.0/(1.0 + (cmke(1,i)*hpe + cmke(2,i)*hpe**2))}
\DoxyCodeLine{1762               tke\_ent = tke\_ent1 + dmke*ef4\_val*mke\_rate}
\DoxyCodeLine{1763               \textcolor{comment}{! TKE\_ent is the TKE that would remain if h\_ent were entrained.}}
\DoxyCodeLine{1764 }
\DoxyCodeLine{1765               dtke\_dh = ((-\/idecay\_len\_tke(i)*tke\_ent1 -\/ drl*gv\%H\_to\_Z) + \&}
\DoxyCodeLine{1766                          pen\_dtke\_dh\_contrib*gv\%H\_to\_Z) + dmke * mke\_rate* \&}
\DoxyCodeLine{1767                         (def4\_dh -\/ ef4\_val*mke\_rate*(cmke(1,i)+2.0*cmke(2,i)*hpe))}
\DoxyCodeLine{1768               \textcolor{comment}{!  dh\_Newt = -\/TKE\_ent / dTKE\_dh}}
\DoxyCodeLine{1769               \textcolor{comment}{! Bisect if the Newton's method prediction is outside of the bounded range.}}
\DoxyCodeLine{1770               \textcolor{keywordflow}{if} (tke\_ent > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1771                 \textcolor{keywordflow}{if} ((h\_max-\/h\_ent)*(-\/dtke\_dh) > tke\_ent) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1772                   dh\_newt = -\/tke\_ent / dtke\_dh}
\DoxyCodeLine{1773                 \textcolor{keywordflow}{else}}
\DoxyCodeLine{1774                   dh\_newt = 0.5*(h\_max-\/h\_ent)}
\DoxyCodeLine{1775 \textcolor{keywordflow}{                endif}}
\DoxyCodeLine{1776                 h\_min = h\_ent}
\DoxyCodeLine{1777               \textcolor{keywordflow}{else}}
\DoxyCodeLine{1778                 \textcolor{keywordflow}{if} ((h\_min-\/h\_ent)*(-\/dtke\_dh) < tke\_ent) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1779                   dh\_newt = -\/tke\_ent / dtke\_dh}
\DoxyCodeLine{1780                 \textcolor{keywordflow}{else}}
\DoxyCodeLine{1781                   dh\_newt = 0.5*(h\_min-\/h\_ent)}
\DoxyCodeLine{1782 \textcolor{keywordflow}{                endif}}
\DoxyCodeLine{1783                 h\_max = h\_ent}
\DoxyCodeLine{1784 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1785               h\_ent = h\_ent + dh\_newt}
\DoxyCodeLine{1786 }
\DoxyCodeLine{1787               \textcolor{keywordflow}{if} (abs(dh\_newt) < 0.2*gv\%Angstrom\_H) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1788 \textcolor{keywordflow}{            enddo}}
\DoxyCodeLine{1789 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1790 }
\DoxyCodeLine{1791           \textcolor{keywordflow}{if} (h\_ent < hmix\_min-\/htot(i)) h\_ent = hmix\_min -\/ htot(i)}
\DoxyCodeLine{1792 }
\DoxyCodeLine{1793           \textcolor{keywordflow}{if} (cs\%TKE\_diagnostics) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1794             hpe = htot(i)+h\_ent}
\DoxyCodeLine{1795             mke\_rate = 1.0/(1.0 + cmke(1,i)*hpe + cmke(2,i)*hpe**2)}
\DoxyCodeLine{1796             ef4\_val = ef4(htot(i)+h\_neglect,h\_ent,idecay\_len\_tke(i))}
\DoxyCodeLine{1797 }
\DoxyCodeLine{1798             e\_hxhpe = h\_ent / ((htot(i)+h\_neglect)*(hpe+h\_neglect))}
\DoxyCodeLine{1799             cs\%diag\_TKE\_mech\_decay(i,j) = cs\%diag\_TKE\_mech\_decay(i,j) + \&}
\DoxyCodeLine{1800                 idt\_diag * ((exp\_kh-\/1.0)* tke(i) + (h\_ent*gv\%H\_to\_Z)*drl*(1.0-\/f1\_kh) + \&}
\DoxyCodeLine{1801                              dmke*mke\_rate*(ef4\_val-\/e\_hxhpe))}
\DoxyCodeLine{1802             cs\%diag\_TKE\_mixing(i,j) = cs\%diag\_TKE\_mixing(i,j) -\/ \&}
\DoxyCodeLine{1803                 idt\_diag*(h\_ent*gv\%H\_to\_Z)*drl}
\DoxyCodeLine{1804             cs\%diag\_TKE\_pen\_SW(i,j) = cs\%diag\_TKE\_pen\_SW(i,j) -\/ \&}
\DoxyCodeLine{1805                 idt\_diag*(h\_ent*gv\%H\_to\_Z)*pen\_en\_contrib}
\DoxyCodeLine{1806             cs\%diag\_TKE\_RiBulk(i,j) = cs\%diag\_TKE\_RiBulk(i,j) + \&}
\DoxyCodeLine{1807                 idt\_diag*dmke*mke\_rate*e\_hxhpe}
\DoxyCodeLine{1808 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1809 }
\DoxyCodeLine{1810           tke(i) = 0.0}
\DoxyCodeLine{1811 \textcolor{keywordflow}{        endif} \textcolor{comment}{! TKE\_full\_ent > 0.0}}
\DoxyCodeLine{1812 }
\DoxyCodeLine{1813         pen\_absorbed = 0.0}
\DoxyCodeLine{1814         \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1815           sw\_trans = exp(-\/h\_ent*opacity\_band(n,i,k))}
\DoxyCodeLine{1816           pen\_absorbed = pen\_absorbed + pen\_sw\_bnd(n,i) * (1.0 -\/ sw\_trans)}
\DoxyCodeLine{1817           pen\_sw\_bnd(n,i) = pen\_sw\_bnd(n,i) * sw\_trans}
\DoxyCodeLine{1818 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1819 }
\DoxyCodeLine{1820         htot(i)   = htot(i)   + h\_ent}
\DoxyCodeLine{1821         r0\_tot(i) = r0\_tot(i) + (h\_ent * r0(i,k) + pen\_absorbed*dr0\_dt(i))}
\DoxyCodeLine{1822         h(i,k)    = h(i,k)    -\/ h\_ent}
\DoxyCodeLine{1823         d\_eb(i,k) = d\_eb(i,k) -\/ h\_ent}
\DoxyCodeLine{1824 }
\DoxyCodeLine{1825         stot(i)    = stot(i)    + h\_ent * s(i,k)}
\DoxyCodeLine{1826         ttot(i)    = ttot(i)    + (h\_ent * t(i,k) + pen\_absorbed)}
\DoxyCodeLine{1827         rcv\_tot(i) = rcv\_tot(i) + (h\_ent*rcv(i,k) + pen\_absorbed*drcv\_dt(i))}
\DoxyCodeLine{1828 }
\DoxyCodeLine{1829         uhtot(i) = uhtot(i) + u(i,k)*h\_ent}
\DoxyCodeLine{1830         vhtot(i) = vhtot(i) + v(i,k)*h\_ent}
\DoxyCodeLine{1831 \textcolor{keywordflow}{      endif} \textcolor{comment}{! h\_avail > 0.0 .AND TKE(i) > 0.0}}
\DoxyCodeLine{1832 }
\DoxyCodeLine{1833 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i loop}}
\DoxyCodeLine{1834 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! k loop}}
\DoxyCodeLine{1835 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_a0f75ed48f800138d458b5654d151fe50}\label{namespacemom__bulk__mixed__layer_a0f75ed48f800138d458b5654d151fe50}} 
\index{mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}!mixedlayer\_convection@{mixedlayer\_convection}}
\index{mixedlayer\_convection@{mixedlayer\_convection}!mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}}
\doxysubsubsection{\texorpdfstring{mixedlayer\_convection()}{mixedlayer\_convection()}}
{\footnotesize\ttfamily subroutine mom\+\_\+bulk\+\_\+mixed\+\_\+layer\+::mixedlayer\+\_\+convection (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{h,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{d\+\_\+eb,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out)}]{htot,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out)}]{Ttot,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out)}]{Stot,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out)}]{uhtot,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out)}]{vhtot,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out)}]{R0\+\_\+tot,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out)}]{Rcv\+\_\+tot,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{u,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{v,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{T,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{S,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{R0,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{Rcv,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{eps,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{d\+R0\+\_\+dT,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{d\+Rcv\+\_\+dT,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{d\+R0\+\_\+dS,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{d\+Rcv\+\_\+dS,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{net\+Mass\+In\+Out,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{net\+Mass\+Out,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{Net\+\_\+heat,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{Net\+\_\+salt,  }\item[{integer, intent(in)}]{nsw,  }\item[{real, dimension(max(nsw,1), g \%isd\+: g \%ied), intent(inout)}]{Pen\+\_\+\+S\+W\+\_\+bnd,  }\item[{real, dimension(max(nsw,1), g \%isd\+: g \%ied, gv \%ke), intent(in)}]{opacity\+\_\+band,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out)}]{Conv\+\_\+\+En,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out)}]{d\+K\+E\+\_\+\+FC,  }\item[{integer, intent(in)}]{j,  }\item[{integer, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{ksort,  }\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__bulk__mixed__layer_1_1bulkmixedlayer__cs}{bulkmixedlayer\+\_\+cs}}), pointer}]{CS,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(inout)}]{tv,  }\item[{type(forcing), intent(inout)}]{fluxes,  }\item[{real, intent(in)}]{dt,  }\item[{logical, intent(in)}]{aggregate\+\_\+\+F\+W\+\_\+forcing }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine causes the mixed layer to entrain to the depth of free convection. The depth of free convection is the shallowest depth at which the fluid is denser than the average of the fluid above. 


\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,out}}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em d\+\_\+eb} & The downward increase across a layer in the \\
\hline
\mbox{\texttt{ out}}  & {\em htot} & The accumulated mixed layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em ttot} & The depth integrated mixed layer temperature \mbox{[}degC H $\sim$$>$ degC m or degC kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em stot} & The depth integrated mixed layer salinity \mbox{[}ppt H $\sim$$>$ ppt m or ppt kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em uhtot} & The depth integrated mixed layer zonal velocity \mbox{[}H L T-\/1 $\sim$$>$ m2 s-\/1 or kg m-\/1 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em vhtot} & The integrated mixed layer meridional velocity \mbox{[}H L T-\/1 $\sim$$>$ m2 s-\/1 or kg m-\/1 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em r0\+\_\+tot} & The integrated mixed layer potential density referenced to 0 pressure \mbox{[}H R $\sim$$>$ kg m-\/2 or kg2 m-\/5\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em rcv\+\_\+tot} & The integrated mixed layer coordinate variable potential density \mbox{[}H R $\sim$$>$ kg m-\/2 or kg2 m-\/5\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em u} & Zonal velocities interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em v} & Zonal velocities interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em t} & Layer temperatures \mbox{[}degC\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em s} & Layer salinities \mbox{[}ppt\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em r0} & Potential density referenced to \\
\hline
\mbox{\texttt{ in}}  & {\em rcv} & The coordinate defining potential \\
\hline
\mbox{\texttt{ in}}  & {\em eps} & The negligibly small amount of water \\
\hline
\mbox{\texttt{ in}}  & {\em dr0\+\_\+dt} & The partial derivative of R0 with respect to temperature \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em drcv\+\_\+dt} & The partial derivative of Rcv with respect to temperature \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dr0\+\_\+ds} & The partial derivative of R0 with respect to salinity \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em drcv\+\_\+ds} & The partial derivative of Rcv with respect to salinity \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em netmassinout} & The net mass flux (if non-\/\+Boussinesq) or volume flux (if Boussinesq) into the ocean within a time step \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. (I.\+e. P+\+R-\/E.) \\
\hline
\mbox{\texttt{ in}}  & {\em netmassout} & The mass or volume flux out of the ocean within a time step \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em net\+\_\+heat} & The net heating at the surface over a time step \mbox{[}degC H $\sim$$>$ degC m or degC kg m-\/2\mbox{]}. Any penetrating shortwave radiation is not included in Net\+\_\+heat. \\
\hline
\mbox{\texttt{ in}}  & {\em net\+\_\+salt} & The net surface salt flux into the ocean over a time step \mbox{[}ppt H $\sim$$>$ ppt m or ppt kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em nsw} & The number of bands of penetrating shortwave radiation. \\
\hline
\mbox{\texttt{ in,out}}  & {\em pen\+\_\+sw\+\_\+bnd} & The penetrating shortwave heating at the sea surface in each penetrating band \mbox{[}degC H $\sim$$>$ degC m or degC kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em opacity\+\_\+band} & The opacity in each band of penetrating shortwave radiation \mbox{[}H-\/1 $\sim$$>$ m-\/1 or m2 kg-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em conv\+\_\+en} & The buoyant turbulent kinetic energy source due to free convection \mbox{[}Z L2 T-\/2 $\sim$$>$ m3 s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em dke\+\_\+fc} & The vertically integrated change in kinetic energy due to free convection \mbox{[}Z L2 T-\/2 $\sim$$>$ m3 s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em j} & The j-\/index to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em ksort} & The density-\/sorted k-\/indices. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & The control structure for this module. \\
\hline
\mbox{\texttt{ in,out}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs. \\
\hline
\mbox{\texttt{ in,out}}  & {\em fluxes} & A structure containing pointers to any possible forcing fields. Unused fields have N\+U\+LL ptrs. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em aggregate\+\_\+fw\+\_\+forcing} & If true, the net incoming and outgoing surface freshwater fluxes are combined before being applied, instead of being applied separately. \\
\hline
\end{DoxyParams}


Definition at line 943 of file M\+O\+M\+\_\+bulk\+\_\+mixed\+\_\+layer.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{943   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{     !< The ocean's grid structure.}}
\DoxyCodeLine{944   \textcolor{keywordtype}{type}(verticalGrid\_type),  \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{    !< The ocean's vertical grid structure.}}
\DoxyCodeLine{945 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{946                             \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{     !< Layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{947 \textcolor{comment}{                                                   !! The units of h are referred to as H below.}}
\DoxyCodeLine{948 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{949                             \textcolor{keywordtype}{intent(inout)} :: d\_eb\textcolor{comment}{  !< The downward increase across a layer in the}}
\DoxyCodeLine{950 \textcolor{comment}{                                                   !! layer in the entrainment from below [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{951 \textcolor{comment}{                                                   !! Positive values go with mass gain by a layer.}}
\DoxyCodeLine{952 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: htot\textcolor{comment}{  !< The accumulated mixed layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{953 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: Ttot\textcolor{comment}{  !< The depth integrated mixed layer temperature}}
\DoxyCodeLine{954 \textcolor{comment}{                                                   !! [degC H \string~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{955 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: Stot\textcolor{comment}{  !< The depth integrated mixed layer salinity}}
\DoxyCodeLine{956 \textcolor{comment}{                                                   !! [ppt H \string~> ppt m or ppt kg m-\/2].}}
\DoxyCodeLine{957 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: uhtot\textcolor{comment}{ !< The depth integrated mixed layer zonal}}
\DoxyCodeLine{958 \textcolor{comment}{                                                   !! velocity [H L T-\/1 \string~> m2 s-\/1 or kg m-\/1 s-\/1].}}
\DoxyCodeLine{959 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: vhtot\textcolor{comment}{ !< The integrated mixed layer meridional}}
\DoxyCodeLine{960 \textcolor{comment}{                                                   !! velocity [H L T-\/1 \string~> m2 s-\/1 or kg m-\/1 s-\/1].}}
\DoxyCodeLine{961 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: R0\_tot\textcolor{comment}{ !< The integrated mixed layer potential density referenced}}
\DoxyCodeLine{962 \textcolor{comment}{                                                   !! to 0 pressure [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{963 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: Rcv\_tot\textcolor{comment}{ !< The integrated mixed layer coordinate}}
\DoxyCodeLine{964 \textcolor{comment}{                                                   !! variable potential density [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{965 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{966                             \textcolor{keywordtype}{intent(in)}    :: u\textcolor{comment}{     !< Zonal velocities interpolated to h points [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{967 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{968                             \textcolor{keywordtype}{intent(in)}    :: v\textcolor{comment}{     !< Zonal velocities interpolated to h points [L T-\/1 \string~> m s-\/1].}}
\DoxyCodeLine{969 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{970                             \textcolor{keywordtype}{intent(in)}    :: T\textcolor{comment}{     !< Layer temperatures [degC].}}
\DoxyCodeLine{971 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{972                             \textcolor{keywordtype}{intent(in)}    :: S\textcolor{comment}{     !< Layer salinities [ppt].}}
\DoxyCodeLine{973 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{974                             \textcolor{keywordtype}{intent(in)}    :: R0\textcolor{comment}{    !< Potential density referenced to}}
\DoxyCodeLine{975 \textcolor{comment}{                                                   !! surface pressure [R \string~> kg m-\/3].}}
\DoxyCodeLine{976 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{977                             \textcolor{keywordtype}{intent(in)}    :: Rcv\textcolor{comment}{   !< The coordinate defining potential}}
\DoxyCodeLine{978 \textcolor{comment}{                                                   !! density [R \string~> kg m-\/3].}}
\DoxyCodeLine{979 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{980                             \textcolor{keywordtype}{intent(in)}    :: eps\textcolor{comment}{   !< The negligibly small amount of water}}
\DoxyCodeLine{981 \textcolor{comment}{                                                   !! that will be left in each layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{982 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: dR0\_dT\textcolor{comment}{  !< The partial derivative of R0 with respect to}}
\DoxyCodeLine{983 \textcolor{comment}{                                                   !! temperature [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{984 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: dRcv\_dT\textcolor{comment}{ !< The partial derivative of Rcv with respect to}}
\DoxyCodeLine{985 \textcolor{comment}{                                                   !! temperature [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{986 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: dR0\_dS\textcolor{comment}{  !< The partial derivative of R0 with respect to}}
\DoxyCodeLine{987 \textcolor{comment}{                                                   !! salinity [R ppt-\/1 \string~> kg m-\/3 ppt-\/1].}}
\DoxyCodeLine{988 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: dRcv\_dS\textcolor{comment}{ !< The partial derivative of Rcv with respect to}}
\DoxyCodeLine{989 \textcolor{comment}{                                                   !! salinity [R ppt-\/1 \string~> kg m-\/3 ppt-\/1].}}
\DoxyCodeLine{990 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: netMassInOut\textcolor{comment}{ !< The net mass flux (if non-\/Boussinesq)}}
\DoxyCodeLine{991 \textcolor{comment}{                                                   !! or volume flux (if Boussinesq) into the ocean}}
\DoxyCodeLine{992 \textcolor{comment}{                                                   !! within a time step [H \string~> m or kg m-\/2]. (I.e. P+R-\/E.)}}
\DoxyCodeLine{993 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: netMassOut\textcolor{comment}{ !< The mass or volume flux out of the ocean}}
\DoxyCodeLine{994 \textcolor{comment}{                                                   !! within a time step [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{995 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: Net\_heat\textcolor{comment}{ !< The net heating at the surface over a time}}
\DoxyCodeLine{996 \textcolor{comment}{                                                   !! step [degC H \string~> degC m or degC kg m-\/2].  Any penetrating}}
\DoxyCodeLine{997 \textcolor{comment}{                                                   !! shortwave radiation is not included in Net\_heat.}}
\DoxyCodeLine{998 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: Net\_salt\textcolor{comment}{ !< The net surface salt flux into the ocean}}
\DoxyCodeLine{999 \textcolor{comment}{                                                   !! over a time step [ppt H \string~> ppt m or ppt kg m-\/2].}}
\DoxyCodeLine{1000   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}    :: nsw\textcolor{comment}{   !< The number of bands of penetrating}}
\DoxyCodeLine{1001 \textcolor{comment}{                                                   !! shortwave radiation.}}
\DoxyCodeLine{1002 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(max(nsw,1),SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: Pen\_SW\_bnd\textcolor{comment}{ !< The penetrating shortwave}}
\DoxyCodeLine{1003 \textcolor{comment}{                                                   !! heating at the sea surface in each penetrating}}
\DoxyCodeLine{1004 \textcolor{comment}{                                                   !! band [degC H \string~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{1005 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(max(nsw,1),SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(in)} :: opacity\_band\textcolor{comment}{ !< The opacity in each band of}}
\DoxyCodeLine{1006 \textcolor{comment}{                                                   !! penetrating shortwave radiation [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{1007 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: Conv\_En\textcolor{comment}{ !< The buoyant turbulent kinetic energy source}}
\DoxyCodeLine{1008 \textcolor{comment}{                                                   !! due to free convection [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1009 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: dKE\_FC\textcolor{comment}{ !< The vertically integrated change in kinetic}}
\DoxyCodeLine{1010 \textcolor{comment}{                                                   !! energy due to free convection [Z L2 T-\/2 \string~> m3 s-\/2].}}
\DoxyCodeLine{1011   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{     !< The j-\/index to work on.}}
\DoxyCodeLine{1012   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1013                             \textcolor{keywordtype}{intent(in)}    :: ksort\textcolor{comment}{ !< The density-\/sorted k-\/indices.}}
\DoxyCodeLine{1014   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{    !< A dimensional unit scaling type}}
\DoxyCodeLine{1015   \textcolor{keywordtype}{type}(bulkmixedlayer\_CS),  \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{    !< The control structure for this module.}}
\DoxyCodeLine{1016   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),    \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{    !< A structure containing pointers to any}}
\DoxyCodeLine{1017 \textcolor{comment}{                                                   !! available thermodynamic fields. Absent}}
\DoxyCodeLine{1018 \textcolor{comment}{                                                   !! fields have NULL ptrs.}}
\DoxyCodeLine{1019   \textcolor{keywordtype}{type}(forcing),            \textcolor{keywordtype}{intent(inout)} :: fluxes\textcolor{comment}{  !< A structure containing pointers to any}}
\DoxyCodeLine{1020 \textcolor{comment}{                                                   !! possible forcing fields.  Unused fields}}
\DoxyCodeLine{1021 \textcolor{comment}{                                                   !! have NULL ptrs.}}
\DoxyCodeLine{1022 \textcolor{keywordtype}{  real},                     \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{    !< Time increment [T \string~> s].}}
\DoxyCodeLine{1023   \textcolor{keywordtype}{logical},                  \textcolor{keywordtype}{intent(in)}    :: aggregate\_FW\_forcing\textcolor{comment}{ !< If true, the net incoming and}}
\DoxyCodeLine{1024 \textcolor{comment}{                                                   !! outgoing surface freshwater fluxes are}}
\DoxyCodeLine{1025 \textcolor{comment}{                                                   !! combined before being applied, instead of}}
\DoxyCodeLine{1026 \textcolor{comment}{                                                   !! being applied separately.}}
\DoxyCodeLine{1027 }
\DoxyCodeLine{1028 \textcolor{comment}{!   This subroutine causes the mixed layer to entrain to the depth of free}}
\DoxyCodeLine{1029 \textcolor{comment}{! convection.  The depth of free convection is the shallowest depth at which the}}
\DoxyCodeLine{1030 \textcolor{comment}{! fluid is denser than the average of the fluid above.}}
\DoxyCodeLine{1031 }
\DoxyCodeLine{1032   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1033 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: \&}
\DoxyCodeLine{1034     massOutRem, \&      \textcolor{comment}{!   Evaporation that remains to be supplied [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1035     netMassIn          \textcolor{comment}{! mass entering through ocean surface [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{1036 \textcolor{keywordtype}{  real} :: SW\_trans     \textcolor{comment}{!   The fraction of shortwave radiation}}
\DoxyCodeLine{1037                        \textcolor{comment}{! that is not absorbed in a layer [nondim].}}
\DoxyCodeLine{1038 \textcolor{keywordtype}{  real} :: Pen\_absorbed \textcolor{comment}{!   The amount of penetrative shortwave radiation}}
\DoxyCodeLine{1039                        \textcolor{comment}{! that is absorbed in a layer [degC H \string~> degC m or degC kg m-\/2].}}
\DoxyCodeLine{1040 \textcolor{keywordtype}{  real} :: h\_avail      \textcolor{comment}{!   The thickness in a layer available for}}
\DoxyCodeLine{1041                        \textcolor{comment}{! entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1042 \textcolor{keywordtype}{  real} :: h\_ent        \textcolor{comment}{!   The thickness from a layer that is entrained [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1043 \textcolor{keywordtype}{  real} :: T\_precip     \textcolor{comment}{!   The temperature of the precipitation [degC].}}
\DoxyCodeLine{1044 \textcolor{keywordtype}{  real} :: C1\_3, C1\_6   \textcolor{comment}{!  1/3 and 1/6.}}
\DoxyCodeLine{1045 \textcolor{keywordtype}{  real} :: En\_fn, Frac, x1 \textcolor{comment}{!  Nondimensional temporary variables.}}
\DoxyCodeLine{1046 \textcolor{keywordtype}{  real} :: dr, dr0      \textcolor{comment}{! Temporary variables [R H \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{1047 \textcolor{keywordtype}{  real} :: dr\_ent, dr\_comp \textcolor{comment}{! Temporary variables [R H \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{1048 \textcolor{keywordtype}{  real} :: dr\_dh        \textcolor{comment}{! The partial derivative of dr\_ent with h\_ent [R \string~> kg m-\/3].}}
\DoxyCodeLine{1049 \textcolor{keywordtype}{  real} :: h\_min, h\_max \textcolor{comment}{!   The minimum, maximum, and previous estimates for}}
\DoxyCodeLine{1050 \textcolor{keywordtype}{  real} :: h\_prev       \textcolor{comment}{! h\_ent [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1051 \textcolor{keywordtype}{  real} :: h\_evap       \textcolor{comment}{!   The thickness that is evaporated [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1052 \textcolor{keywordtype}{  real} :: dh\_Newt      \textcolor{comment}{!   The Newton's method estimate of the change in}}
\DoxyCodeLine{1053                        \textcolor{comment}{! h\_ent between iterations [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1054 \textcolor{keywordtype}{  real} :: g\_H2\_2Rho0   \textcolor{comment}{!   Half the gravitational acceleration times the square of}}
\DoxyCodeLine{1055                        \textcolor{comment}{! the conversion from H to Z divided by the mean density,}}
\DoxyCodeLine{1056                        \textcolor{comment}{! [L2 Z T-\/3 H-\/2 R-\/1 \string~> m4 s-\/3 kg-\/1 or m10 s-\/3 kg-\/3].}}
\DoxyCodeLine{1057 \textcolor{keywordtype}{  real} :: Angstrom     \textcolor{comment}{!   The minimum layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1058 \textcolor{keywordtype}{  real} :: opacity      \textcolor{comment}{!   The opacity converted to inverse thickness units [H-\/1 \string~> m-\/1 or m2 kg-\/1]}}
\DoxyCodeLine{1059 \textcolor{keywordtype}{  real} :: sum\_Pen\_En   \textcolor{comment}{!   The potential energy change due to penetrating}}
\DoxyCodeLine{1060                        \textcolor{comment}{! shortwave radiation, integrated over a layer}}
\DoxyCodeLine{1061                        \textcolor{comment}{! [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{1062 \textcolor{keywordtype}{  real} :: Idt          \textcolor{comment}{! 1.0/dt [T-\/1 \string~> s-\/1]}}
\DoxyCodeLine{1063 \textcolor{keywordtype}{  real} :: netHeatOut   \textcolor{comment}{! accumulated heat content of mass leaving ocean}}
\DoxyCodeLine{1064   \textcolor{keywordtype}{integer} :: is, ie, nz, i, k, ks, itt, n}
\DoxyCodeLine{1065 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(max(nsw,1))} :: \&}
\DoxyCodeLine{1066     C2, \&              \textcolor{comment}{! Temporary variable R H-\/1 \string~> kg m-\/4 or m-\/1].}}
\DoxyCodeLine{1067     r\_SW\_top           \textcolor{comment}{! Temporary variables [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{1068 }
\DoxyCodeLine{1069   angstrom = gv\%Angstrom\_H}
\DoxyCodeLine{1070   c1\_3 = 1.0/3.0 ; c1\_6 = 1.0/6.0}
\DoxyCodeLine{1071   g\_h2\_2rho0 = (gv\%g\_Earth * gv\%H\_to\_Z**2) / (2.0 * gv\%Rho0)}
\DoxyCodeLine{1072   idt = 1.0 / dt}
\DoxyCodeLine{1073   is = g\%isc ; ie = g\%iec ; nz = gv\%ke}
\DoxyCodeLine{1074 }
\DoxyCodeLine{1075   \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (ksort(i,1) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1076     k = ksort(i,1)}
\DoxyCodeLine{1077 }
\DoxyCodeLine{1078     \textcolor{keywordflow}{if} (aggregate\_fw\_forcing) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1079       massoutrem(i) = 0.0}
\DoxyCodeLine{1080       \textcolor{keywordflow}{if} (netmassinout(i) < 0.0) massoutrem(i) = -\/netmassinout(i)}
\DoxyCodeLine{1081       netmassin(i) = netmassinout(i) + massoutrem(i)}
\DoxyCodeLine{1082     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1083       massoutrem(i) = -\/netmassout(i)}
\DoxyCodeLine{1084       netmassin(i)  = netmassinout(i) -\/ netmassout(i)}
\DoxyCodeLine{1085 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1086 }
\DoxyCodeLine{1087     \textcolor{comment}{! htot is an Angstrom (taken from layer 1) plus any net precipitation.}}
\DoxyCodeLine{1088     h\_ent     = max(min(angstrom,h(i,k)-\/eps(i,k)),0.0)}
\DoxyCodeLine{1089     htot(i)   = h\_ent + netmassin(i)}
\DoxyCodeLine{1090     h(i,k)    = h(i,k) -\/ h\_ent}
\DoxyCodeLine{1091     d\_eb(i,k) = d\_eb(i,k) -\/ h\_ent}
\DoxyCodeLine{1092 }
\DoxyCodeLine{1093     pen\_absorbed = 0.0}
\DoxyCodeLine{1094     \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1095       sw\_trans        = exp(-\/htot(i)*opacity\_band(n,i,k))}
\DoxyCodeLine{1096       pen\_absorbed    = pen\_absorbed + pen\_sw\_bnd(n,i) * (1.0-\/sw\_trans)}
\DoxyCodeLine{1097       pen\_sw\_bnd(n,i) = pen\_sw\_bnd(n,i) * sw\_trans}
\DoxyCodeLine{1098 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1099 }
\DoxyCodeLine{1100     \textcolor{comment}{! Precipitation is assumed to have the same temperature and velocity}}
\DoxyCodeLine{1101     \textcolor{comment}{! as layer 1.  Because layer 1 might not be the topmost layer, this}}
\DoxyCodeLine{1102     \textcolor{comment}{! involves multiple terms.}}
\DoxyCodeLine{1103     t\_precip = t(i,1)}
\DoxyCodeLine{1104     ttot(i) = (net\_heat(i) + (netmassin(i) * t\_precip + h\_ent * t(i,k))) + \&}
\DoxyCodeLine{1105               pen\_absorbed}
\DoxyCodeLine{1106     \textcolor{comment}{! Net\_heat contains both heat fluxes and the heat content of mass fluxes.}}
\DoxyCodeLine{1107  \textcolor{comment}{!! Ttot(i) = netMassIn(i) * T\_precip + h\_ent * T(i,k)}}
\DoxyCodeLine{1108  \textcolor{comment}{!! Ttot(i) = Net\_heat(i) + Ttot(i)}}
\DoxyCodeLine{1109  \textcolor{comment}{!! Ttot(i) = Ttot(i) + Pen\_absorbed}}
\DoxyCodeLine{1110   \textcolor{comment}{! smg:}}
\DoxyCodeLine{1111   \textcolor{comment}{! Ttot(i)   = (Net\_heat(i) + (h\_ent * T(i,k))) + Pen\_absorbed}}
\DoxyCodeLine{1112     stot(i)   = h\_ent*s(i,k) + net\_salt(i)}
\DoxyCodeLine{1113     uhtot(i)  = u(i,1)*netmassin(i) + u(i,k)*h\_ent}
\DoxyCodeLine{1114     vhtot(i)  = v(i,1)*netmassin(i) + v(i,k)*h\_ent}
\DoxyCodeLine{1115     r0\_tot(i) = (h\_ent*r0(i,k) + netmassin(i)*r0(i,1)) + \&}
\DoxyCodeLine{1116 \textcolor{comment}{!                   dR0\_dT(i)*netMassIn(i)*(T\_precip -\/ T(i,1)) + \&}}
\DoxyCodeLine{1117                 (dr0\_dt(i)*(net\_heat(i) + pen\_absorbed) -\/ \&}
\DoxyCodeLine{1118                  dr0\_ds(i) * (netmassin(i) * s(i,1) -\/ net\_salt(i)))}
\DoxyCodeLine{1119     rcv\_tot(i) = (h\_ent*rcv(i,k) + netmassin(i)*rcv(i,1)) + \&}
\DoxyCodeLine{1120 \textcolor{comment}{!                    dRcv\_dT(i)*netMassIn(i)*(T\_precip -\/ T(i,1)) + \&}}
\DoxyCodeLine{1121                  (drcv\_dt(i)*(net\_heat(i) + pen\_absorbed) -\/ \&}
\DoxyCodeLine{1122                   drcv\_ds(i) * (netmassin(i) * s(i,1) -\/ net\_salt(i)))}
\DoxyCodeLine{1123     conv\_en(i) = 0.0 ; dke\_fc(i) = 0.0}
\DoxyCodeLine{1124     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes\%heat\_content\_massin)) \&}
\DoxyCodeLine{1125       fluxes\%heat\_content\_massin(i,j) = fluxes\%heat\_content\_massin(i,j) + \&}
\DoxyCodeLine{1126                          t\_precip * netmassin(i) * gv\%H\_to\_RZ * fluxes\%C\_p * idt}
\DoxyCodeLine{1127     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%TempxPmE)) tv\%TempxPmE(i,j) = tv\%TempxPmE(i,j) + \&}
\DoxyCodeLine{1128                          t\_precip * netmassin(i) * gv\%H\_to\_RZ}
\DoxyCodeLine{1129   \textcolor{keywordflow}{else}  \textcolor{comment}{! This is a massless column, but zero out the summed variables anyway for safety.}}
\DoxyCodeLine{1130     htot(i) = 0.0 ; ttot(i) = 0.0 ; stot(i) = 0.0 ; r0\_tot(i) = 0.0 ; rcv\_tot = 0.0}
\DoxyCodeLine{1131     uhtot(i) = 0.0 ; vhtot(i) = 0.0 ; conv\_en(i) = 0.0 ; dke\_fc(i) = 0.0}
\DoxyCodeLine{1132 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1133 }
\DoxyCodeLine{1134   \textcolor{comment}{! Now do netMassOut case in this block.}}
\DoxyCodeLine{1135   \textcolor{comment}{! At this point htot contains an Angstrom of fluid from layer 0 plus netMassIn.}}
\DoxyCodeLine{1136   \textcolor{keywordflow}{do} ks=1,nz}
\DoxyCodeLine{1137     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (ksort(i,ks) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1138       k = ksort(i,ks)}
\DoxyCodeLine{1139 }
\DoxyCodeLine{1140       \textcolor{keywordflow}{if} ((htot(i) < angstrom) .and. (h(i,k) > eps(i,k))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1141         \textcolor{comment}{! If less than an Angstrom was available from the layers above plus}}
\DoxyCodeLine{1142         \textcolor{comment}{! any precipitation, add more fluid from this layer.}}
\DoxyCodeLine{1143         h\_ent = min(angstrom-\/htot(i), h(i,k)-\/eps(i,k))}
\DoxyCodeLine{1144         htot(i) = htot(i) + h\_ent}
\DoxyCodeLine{1145         h(i,k) = h(i,k) -\/ h\_ent}
\DoxyCodeLine{1146         d\_eb(i,k) = d\_eb(i,k) -\/ h\_ent}
\DoxyCodeLine{1147 }
\DoxyCodeLine{1148         r0\_tot(i) = r0\_tot(i) + h\_ent*r0(i,k)}
\DoxyCodeLine{1149         uhtot(i) = uhtot(i) + h\_ent*u(i,k)}
\DoxyCodeLine{1150         vhtot(i) = vhtot(i) + h\_ent*v(i,k)}
\DoxyCodeLine{1151 }
\DoxyCodeLine{1152         rcv\_tot(i) = rcv\_tot(i) + h\_ent*rcv(i,k)}
\DoxyCodeLine{1153         ttot(i) = ttot(i) + h\_ent*t(i,k)}
\DoxyCodeLine{1154         stot(i) = stot(i) + h\_ent*s(i,k)}
\DoxyCodeLine{1155 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1156 }
\DoxyCodeLine{1157       \textcolor{comment}{! Water is removed from the topmost layers with any mass.}}
\DoxyCodeLine{1158       \textcolor{comment}{! We may lose layers if they are thin enough.}}
\DoxyCodeLine{1159       \textcolor{comment}{! The salt that is left behind goes into Stot.}}
\DoxyCodeLine{1160       \textcolor{keywordflow}{if} ((massoutrem(i) > 0.0) .and. (h(i,k) > eps(i,k))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1161         \textcolor{keywordflow}{if} (massoutrem(i) > (h(i,k) -\/ eps(i,k))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1162           h\_evap = h(i,k) -\/ eps(i,k)}
\DoxyCodeLine{1163           h(i,k) = eps(i,k)}
\DoxyCodeLine{1164           massoutrem(i) = massoutrem(i) -\/ h\_evap}
\DoxyCodeLine{1165         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1166           h\_evap = massoutrem(i)}
\DoxyCodeLine{1167           h(i,k) = h(i,k) -\/ h\_evap}
\DoxyCodeLine{1168           massoutrem(i) = 0.0}
\DoxyCodeLine{1169 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1170 }
\DoxyCodeLine{1171         stot(i) = stot(i) + h\_evap*s(i,k)}
\DoxyCodeLine{1172         r0\_tot(i) = r0\_tot(i) + dr0\_ds(i)*h\_evap*s(i,k)}
\DoxyCodeLine{1173         rcv\_tot(i) = rcv\_tot(i) + drcv\_ds(i)*h\_evap*s(i,k)}
\DoxyCodeLine{1174         d\_eb(i,k) = d\_eb(i,k) -\/ h\_evap}
\DoxyCodeLine{1175 }
\DoxyCodeLine{1176         \textcolor{comment}{! smg: when resolve the A=B code, we will set}}
\DoxyCodeLine{1177         \textcolor{comment}{! heat\_content\_massout = heat\_content\_massout -\/ T(i,k)*h\_evap*GV\%H\_to\_RZ*fluxes\%C\_p*Idt}}
\DoxyCodeLine{1178         \textcolor{comment}{! by uncommenting the lines here.}}
\DoxyCodeLine{1179         \textcolor{comment}{! we will also then completely remove TempXpme from the model.}}
\DoxyCodeLine{1180         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes\%heat\_content\_massout)) \&}
\DoxyCodeLine{1181           fluxes\%heat\_content\_massout(i,j) = fluxes\%heat\_content\_massout(i,j) -\/ \&}
\DoxyCodeLine{1182                                       t(i,k)*h\_evap*gv\%H\_to\_RZ * fluxes\%C\_p * idt}
\DoxyCodeLine{1183         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%TempxPmE)) tv\%TempxPmE(i,j) = tv\%TempxPmE(i,j) -\/ \&}
\DoxyCodeLine{1184                                       t(i,k)*h\_evap*gv\%H\_to\_RZ}
\DoxyCodeLine{1185 }
\DoxyCodeLine{1186 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1187 }
\DoxyCodeLine{1188       \textcolor{comment}{! The following section calculates how much fluid will be entrained.}}
\DoxyCodeLine{1189       h\_avail = h(i,k) -\/ eps(i,k)}
\DoxyCodeLine{1190       \textcolor{keywordflow}{if} (h\_avail > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1191         dr = r0\_tot(i) -\/ htot(i)*r0(i,k)}
\DoxyCodeLine{1192         h\_ent = 0.0}
\DoxyCodeLine{1193 }
\DoxyCodeLine{1194         dr0 = dr}
\DoxyCodeLine{1195         \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1196           dr0 = dr0 -\/ (dr0\_dt(i)*pen\_sw\_bnd(n,i)) * \&}
\DoxyCodeLine{1197                       opacity\_band(n,i,k)*htot(i)}
\DoxyCodeLine{1198 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1199 }
\DoxyCodeLine{1200         \textcolor{comment}{! Some entrainment will occur from this layer.}}
\DoxyCodeLine{1201         \textcolor{keywordflow}{if} (dr0 > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1202           dr\_comp = dr}
\DoxyCodeLine{1203           \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1204             \textcolor{comment}{!   Compare the density at the bottom of a layer with the}}
\DoxyCodeLine{1205             \textcolor{comment}{! density averaged over the mixed layer and that layer.}}
\DoxyCodeLine{1206             opacity = opacity\_band(n,i,k)}
\DoxyCodeLine{1207             sw\_trans = exp(-\/h\_avail*opacity)}
\DoxyCodeLine{1208             dr\_comp = dr\_comp + (dr0\_dt(i)*pen\_sw\_bnd(n,i)) * \&}
\DoxyCodeLine{1209                 ((1.0 -\/ sw\_trans) -\/ opacity*(htot(i)+h\_avail)*sw\_trans)}
\DoxyCodeLine{1210 \textcolor{keywordflow}{          endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1211           \textcolor{keywordflow}{if} (dr\_comp >= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1212             \textcolor{comment}{! The entire layer is entrained.}}
\DoxyCodeLine{1213             h\_ent = h\_avail}
\DoxyCodeLine{1214           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1215             \textcolor{comment}{!  The layer is partially entrained.   Iterate to determine how much}}
\DoxyCodeLine{1216             \textcolor{comment}{!  entrainment occurs.  Solve for the h\_ent at which dr\_ent = 0.}}
\DoxyCodeLine{1217 }
\DoxyCodeLine{1218             \textcolor{comment}{! Instead of assuming that the curve is linear between the two end}}
\DoxyCodeLine{1219             \textcolor{comment}{! points, assume that the change is concentrated near small values}}
\DoxyCodeLine{1220             \textcolor{comment}{! of entrainment.  On average, this saves about 1 iteration.}}
\DoxyCodeLine{1221             frac = dr0 / (dr0 -\/ dr\_comp)}
\DoxyCodeLine{1222             h\_ent = h\_avail * frac*frac}
\DoxyCodeLine{1223             h\_min = 0.0 ; h\_max = h\_avail}
\DoxyCodeLine{1224 }
\DoxyCodeLine{1225             \textcolor{keywordflow}{do} n=1,nsw}
\DoxyCodeLine{1226               r\_sw\_top(n) = dr0\_dt(i) * pen\_sw\_bnd(n,i)}
\DoxyCodeLine{1227               c2(n) = r\_sw\_top(n) * opacity\_band(n,i,k)**2}
\DoxyCodeLine{1228 \textcolor{keywordflow}{            enddo}}
\DoxyCodeLine{1229             \textcolor{keywordflow}{do} itt=1,10}
\DoxyCodeLine{1230               dr\_ent = dr ; dr\_dh = 0.0}
\DoxyCodeLine{1231               \textcolor{keywordflow}{do} n=1,nsw}
\DoxyCodeLine{1232                 opacity = opacity\_band(n,i,k)}
\DoxyCodeLine{1233                 sw\_trans = exp(-\/h\_ent*opacity)}
\DoxyCodeLine{1234                 dr\_ent = dr\_ent + r\_sw\_top(n) * ((1.0 -\/ sw\_trans) -\/ \&}
\DoxyCodeLine{1235                            opacity*(htot(i)+h\_ent)*sw\_trans)}
\DoxyCodeLine{1236                 dr\_dh = dr\_dh + c2(n) * (htot(i)+h\_ent) * sw\_trans}
\DoxyCodeLine{1237 \textcolor{keywordflow}{              enddo}}
\DoxyCodeLine{1238 }
\DoxyCodeLine{1239               \textcolor{keywordflow}{if} (dr\_ent > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1240                 h\_min = h\_ent}
\DoxyCodeLine{1241               \textcolor{keywordflow}{else}}
\DoxyCodeLine{1242                 h\_max = h\_ent}
\DoxyCodeLine{1243 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1244 }
\DoxyCodeLine{1245               dh\_newt = -\/dr\_ent / dr\_dh}
\DoxyCodeLine{1246               h\_prev = h\_ent ; h\_ent = h\_prev+dh\_newt}
\DoxyCodeLine{1247               \textcolor{keywordflow}{if} (h\_ent > h\_max) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1248                 h\_ent = 0.5*(h\_prev+h\_max)}
\DoxyCodeLine{1249               \textcolor{keywordflow}{elseif} (h\_ent < h\_min) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1250                 h\_ent = 0.5*(h\_prev+h\_min)}
\DoxyCodeLine{1251 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1252 }
\DoxyCodeLine{1253               \textcolor{keywordflow}{if} (abs(dh\_newt) < 0.2*angstrom) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1254 \textcolor{keywordflow}{            enddo}}
\DoxyCodeLine{1255 }
\DoxyCodeLine{1256 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1257 }
\DoxyCodeLine{1258           \textcolor{comment}{!  Now that the amount of entrainment (h\_ent) has been determined,}}
\DoxyCodeLine{1259           \textcolor{comment}{!  calculate changes in various terms.}}
\DoxyCodeLine{1260           sum\_pen\_en = 0.0 ; pen\_absorbed = 0.0}
\DoxyCodeLine{1261           \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1262             opacity = opacity\_band(n,i,k)}
\DoxyCodeLine{1263             sw\_trans = exp(-\/h\_ent*opacity)}
\DoxyCodeLine{1264 }
\DoxyCodeLine{1265             x1 = h\_ent*opacity}
\DoxyCodeLine{1266             \textcolor{keywordflow}{if} (x1 < 2.0e-\/5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1267               en\_fn = (opacity*htot(i)*(1.0 -\/ 0.5*(x1 -\/ c1\_3*x1)) + \&}
\DoxyCodeLine{1268                        x1*x1*c1\_6)}
\DoxyCodeLine{1269             \textcolor{keywordflow}{else}}
\DoxyCodeLine{1270               en\_fn = ((opacity*htot(i) + 2.0) * \&}
\DoxyCodeLine{1271                        ((1.0-\/sw\_trans) / x1) -\/ 1.0 + sw\_trans)}
\DoxyCodeLine{1272 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1273             sum\_pen\_en = sum\_pen\_en -\/ (dr0\_dt(i)*pen\_sw\_bnd(n,i)) * en\_fn}
\DoxyCodeLine{1274 }
\DoxyCodeLine{1275             pen\_absorbed = pen\_absorbed + pen\_sw\_bnd(n,i) * (1.0 -\/ sw\_trans)}
\DoxyCodeLine{1276             pen\_sw\_bnd(n,i) = pen\_sw\_bnd(n,i) * sw\_trans}
\DoxyCodeLine{1277 \textcolor{keywordflow}{          endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1278 }
\DoxyCodeLine{1279           conv\_en(i) = conv\_en(i) + g\_h2\_2rho0 * h\_ent * \&}
\DoxyCodeLine{1280                        ( (r0\_tot(i) -\/ r0(i,k)*htot(i)) + sum\_pen\_en )}
\DoxyCodeLine{1281 }
\DoxyCodeLine{1282           r0\_tot(i) = r0\_tot(i) + (h\_ent * r0(i,k) + pen\_absorbed*dr0\_dt(i))}
\DoxyCodeLine{1283           stot(i) = stot(i) + h\_ent * s(i,k)}
\DoxyCodeLine{1284           ttot(i) = ttot(i) + (h\_ent * t(i,k) + pen\_absorbed)}
\DoxyCodeLine{1285           rcv\_tot(i) = rcv\_tot(i) + (h\_ent * rcv(i,k) + pen\_absorbed*drcv\_dt(i))}
\DoxyCodeLine{1286 \textcolor{keywordflow}{        endif} \textcolor{comment}{! dr0 > 0.0}}
\DoxyCodeLine{1287 }
\DoxyCodeLine{1288         \textcolor{keywordflow}{if} (h\_ent > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1289           \textcolor{keywordflow}{if} (htot(i) > 0.0) \&}
\DoxyCodeLine{1290             dke\_fc(i) = dke\_fc(i) + cs\%bulk\_Ri\_convective * 0.5 * \&}
\DoxyCodeLine{1291               ((gv\%H\_to\_Z*h\_ent) / (htot(i)*(h\_ent+htot(i)))) * \&}
\DoxyCodeLine{1292               ((uhtot(i)-\/u(i,k)*htot(i))**2 + (vhtot(i)-\/v(i,k)*htot(i))**2)}
\DoxyCodeLine{1293 }
\DoxyCodeLine{1294           htot(i)  = htot(i)  + h\_ent}
\DoxyCodeLine{1295           h(i,k) = h(i,k) -\/ h\_ent}
\DoxyCodeLine{1296           d\_eb(i,k) = d\_eb(i,k) -\/ h\_ent}
\DoxyCodeLine{1297           \textcolor{keywordflow}{if} (cs\%convect\_mom\_bug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1298             uhtot(i) = u(i,k)*h\_ent ; vhtot(i) = v(i,k)*h\_ent}
\DoxyCodeLine{1299           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1300             uhtot(i) = uhtot(i) + h\_ent*u(i,k) ; vhtot(i) = vhtot(i) + h\_ent*v(i,k)}
\DoxyCodeLine{1301 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1302 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1303 }
\DoxyCodeLine{1304 }
\DoxyCodeLine{1305 \textcolor{keywordflow}{      endif} \textcolor{comment}{! h\_avail>0}}
\DoxyCodeLine{1306 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i loop}}
\DoxyCodeLine{1307 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! k loop}}
\DoxyCodeLine{1308 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_aa33a3e7c5e1b18444bf54d37f1c00ad3}\label{namespacemom__bulk__mixed__layer_aa33a3e7c5e1b18444bf54d37f1c00ad3}} 
\index{mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}!mixedlayer\_detrain\_1@{mixedlayer\_detrain\_1}}
\index{mixedlayer\_detrain\_1@{mixedlayer\_detrain\_1}!mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}}
\doxysubsubsection{\texorpdfstring{mixedlayer\_detrain\_1()}{mixedlayer\_detrain\_1()}}
{\footnotesize\ttfamily subroutine mom\+\_\+bulk\+\_\+mixed\+\_\+layer\+::mixedlayer\+\_\+detrain\+\_\+1 (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szk0\+\_\+(gv)), intent(inout)}]{h,  }\item[{real, dimension(szi\+\_\+(g),szk0\+\_\+(gv)), intent(inout)}]{T,  }\item[{real, dimension(szi\+\_\+(g),szk0\+\_\+(gv)), intent(inout)}]{S,  }\item[{real, dimension(szi\+\_\+(g),szk0\+\_\+(gv)), intent(inout)}]{R0,  }\item[{real, dimension(szi\+\_\+(g),szk0\+\_\+(gv)), intent(inout)}]{Rcv,  }\item[{real, dimension(szk\+\_\+(gv)), intent(in)}]{Rcv\+Tgt,  }\item[{real, intent(in)}]{dt,  }\item[{real, intent(in)}]{dt\+\_\+diag,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(gv)), intent(inout)}]{d\+\_\+ea,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(gv)), intent(inout)}]{d\+\_\+eb,  }\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__bulk__mixed__layer_1_1bulkmixedlayer__cs}{bulkmixedlayer\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{d\+Rcv\+\_\+dT,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{d\+Rcv\+\_\+dS,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{max\+\_\+\+B\+L\+\_\+det }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine moves any water left in the former mixed layers into the single buffer layers and may also move buffer layer water into the interior isopycnal layers. 


\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,out}}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. Layer 0 is the new mixed layer. \\
\hline
\mbox{\texttt{ in,out}}  & {\em t} & Potential temperature \mbox{[}degC\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em s} & Salinity \mbox{[}ppt\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em r0} & Potential density referenced to surface pressure \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em rcv} & The coordinate defining potential density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em rcvtgt} & The target value of Rcv for each layer \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt\+\_\+diag} & The accumulated time interval for diagnostics \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em d\+\_\+ea} & The upward increase across a layer in the entrainment from above \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. Positive d\+\_\+ea goes with layer thickness increases. \\
\hline
\mbox{\texttt{ in,out}}  & {\em d\+\_\+eb} & The downward increase across a layer in the entrainment from below \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. Positive values go with mass gain by a layer. \\
\hline
\mbox{\texttt{ in}}  & {\em j} & The meridional row to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & The control structure returned by a previous call to mixedlayer\+\_\+init. \\
\hline
\mbox{\texttt{ in}}  & {\em drcv\+\_\+dt} & The partial derivative of coordinate defining potential density with potential temperature \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em drcv\+\_\+ds} & The partial derivative of coordinate defining potential density with salinity \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em max\+\_\+bl\+\_\+det} & If non-\/negative, the maximum detrainment permitted from the buffer layers \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 3105 of file M\+O\+M\+\_\+bulk\+\_\+mixed\+\_\+layer.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{3105   \textcolor{keywordtype}{type}(ocean\_grid\_type),              \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{3106   \textcolor{keywordtype}{type}(verticalGrid\_type),            \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{3107 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{    !< Layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{3108 \textcolor{comment}{                                                            !! Layer 0 is the new mixed layer.}}
\DoxyCodeLine{3109 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: T\textcolor{comment}{    !< Potential temperature [degC].}}
\DoxyCodeLine{3110 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: S\textcolor{comment}{    !< Salinity [ppt].}}
\DoxyCodeLine{3111 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: R0\textcolor{comment}{   !< Potential density referenced to}}
\DoxyCodeLine{3112 \textcolor{comment}{                                                            !! surface pressure [R \string~> kg m-\/3].}}
\DoxyCodeLine{3113 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: Rcv\textcolor{comment}{  !< The coordinate defining potential}}
\DoxyCodeLine{3114 \textcolor{comment}{                                                            !! density [R \string~> kg m-\/3].}}
\DoxyCodeLine{3115 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV))},          \textcolor{keywordtype}{intent(in)}    :: RcvTgt\textcolor{comment}{ !< The target value of Rcv for each}}
\DoxyCodeLine{3116 \textcolor{comment}{                                                            !! layer [R \string~> kg m-\/3].}}
\DoxyCodeLine{3117 \textcolor{keywordtype}{  real},                               \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{   !< Time increment [T \string~> s].}}
\DoxyCodeLine{3118 \textcolor{keywordtype}{  real},                               \textcolor{keywordtype}{intent(in)}    :: dt\_diag\textcolor{comment}{ !< The accumulated time interval for}}
\DoxyCodeLine{3119 \textcolor{comment}{                                                            !! diagnostics [T \string~> s].}}
\DoxyCodeLine{3120 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},  \textcolor{keywordtype}{intent(inout)} :: d\_ea\textcolor{comment}{ !< The upward increase across a layer in}}
\DoxyCodeLine{3121 \textcolor{comment}{                                                            !! the entrainment from above}}
\DoxyCodeLine{3122 \textcolor{comment}{                                                            !! [H \string~> m or kg m-\/2]. Positive d\_ea}}
\DoxyCodeLine{3123 \textcolor{comment}{                                                            !! goes with layer thickness increases.}}
\DoxyCodeLine{3124 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},  \textcolor{keywordtype}{intent(inout)} :: d\_eb\textcolor{comment}{ !< The downward increase across a layer}}
\DoxyCodeLine{3125 \textcolor{comment}{                                                            !! in the entrainment from below [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{3126 \textcolor{comment}{                                                            !! Positive values go with mass gain by}}
\DoxyCodeLine{3127 \textcolor{comment}{                                                            !! a layer.}}
\DoxyCodeLine{3128   \textcolor{keywordtype}{integer},                            \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{    !< The meridional row to work on.}}
\DoxyCodeLine{3129   \textcolor{keywordtype}{type}(unit\_scale\_type),              \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}}
\DoxyCodeLine{3130   \textcolor{keywordtype}{type}(bulkmixedlayer\_CS),            \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< The control structure returned by a}}
\DoxyCodeLine{3131 \textcolor{comment}{                                                            !! previous call to mixedlayer\_init.}}
\DoxyCodeLine{3132 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: dRcv\_dT\textcolor{comment}{ !< The partial derivative of}}
\DoxyCodeLine{3133 \textcolor{comment}{                                                            !! coordinate defining potential density}}
\DoxyCodeLine{3134 \textcolor{comment}{                                                            !! with potential temperature}}
\DoxyCodeLine{3135 \textcolor{comment}{                                                            !! [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{3136 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: dRcv\_dS\textcolor{comment}{    !< The partial derivative of}}
\DoxyCodeLine{3137 \textcolor{comment}{                                                            !! coordinate defining potential density}}
\DoxyCodeLine{3138 \textcolor{comment}{                                                            !! with salinity [R ppt-\/1 \string~> kg m-\/3 ppt-\/1].}}
\DoxyCodeLine{3139 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: max\_BL\_det\textcolor{comment}{ !< If non-\/negative, the maximum}}
\DoxyCodeLine{3140 \textcolor{comment}{                                                            !! detrainment permitted from the buffer}}
\DoxyCodeLine{3141 \textcolor{comment}{                                                            !! layers [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{3142 }
\DoxyCodeLine{3143   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{3144 \textcolor{keywordtype}{  real} :: Ih                  \textcolor{comment}{! The inverse of a thickness [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{3145 \textcolor{keywordtype}{  real} :: h\_ent               \textcolor{comment}{! The thickness from a layer that is}}
\DoxyCodeLine{3146                               \textcolor{comment}{! entrained [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{3147 \textcolor{keywordtype}{  real} :: max\_det\_rem(SZI\_(G)) \textcolor{comment}{! Remaining permitted detrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{3148 \textcolor{keywordtype}{  real} :: detrain(SZI\_(G))    \textcolor{comment}{! The thickness of fluid to detrain}}
\DoxyCodeLine{3149                               \textcolor{comment}{! from the mixed layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{3150 \textcolor{keywordtype}{  real} :: dT\_dR, dS\_dR, dRml, dR0\_dRcv, dT\_dS\_wt2}
\DoxyCodeLine{3151 \textcolor{keywordtype}{  real} :: I\_denom             \textcolor{comment}{! A work variable [ppt2 R-\/2 \string~> ppt2 m6 kg-\/2].}}
\DoxyCodeLine{3152 \textcolor{keywordtype}{  real} :: Sdown, Tdown}
\DoxyCodeLine{3153 \textcolor{keywordtype}{  real} :: dt\_Time             \textcolor{comment}{! The timestep divided by the detrainment timescale [nondim].}}
\DoxyCodeLine{3154 \textcolor{keywordtype}{  real} :: g\_H2\_2Rho0dt        \textcolor{comment}{! Half the gravitational acceleration times the square of the}}
\DoxyCodeLine{3155                               \textcolor{comment}{! conversion from H to m divided by the mean density times the time}}
\DoxyCodeLine{3156                               \textcolor{comment}{! step [L2 Z T-\/3 H-\/2 R-\/1 \string~> m4 s-\/3 kg-\/1 or m10 s-\/3 kg-\/3].}}
\DoxyCodeLine{3157 \textcolor{keywordtype}{  real} :: g\_H2\_2dt            \textcolor{comment}{! Half the gravitational acceleration times the square of the}}
\DoxyCodeLine{3158                               \textcolor{comment}{! conversion from H to Z divided by the diagnostic time step}}
\DoxyCodeLine{3159                               \textcolor{comment}{! [L2 Z H-\/2 T-\/3 \string~> m s-\/3 or m7 kg-\/2 s-\/3].}}
\DoxyCodeLine{3160 }
\DoxyCodeLine{3161   \textcolor{keywordtype}{logical} :: splittable\_BL(SZI\_(G)), orthogonal\_extrap}
\DoxyCodeLine{3162 \textcolor{keywordtype}{  real} :: x1}
\DoxyCodeLine{3163 }
\DoxyCodeLine{3164   \textcolor{keywordtype}{integer} :: i, is, ie, k, k1, nkmb, nz}
\DoxyCodeLine{3165   is = g\%isc ; ie = g\%iec ; nz = gv\%ke}
\DoxyCodeLine{3166   nkmb = cs\%nkml+cs\%nkbl}
\DoxyCodeLine{3167   \textcolor{keywordflow}{if} (cs\%nkbl /= 1) \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"MOM\_mixed\_layer: "}// \&}
\DoxyCodeLine{3168                         \textcolor{stringliteral}{"CS\%nkbl must be 1 in mixedlayer\_detrain\_1."})}
\DoxyCodeLine{3169 }
\DoxyCodeLine{3170   dt\_time = dt / cs\%BL\_detrain\_time}
\DoxyCodeLine{3171   g\_h2\_2rho0dt = (gv\%g\_Earth * gv\%H\_to\_Z**2) / (2.0 * gv\%Rho0 * dt\_diag)}
\DoxyCodeLine{3172   g\_h2\_2dt = (gv\%g\_Earth * gv\%H\_to\_Z**2) / (2.0 * dt\_diag)}
\DoxyCodeLine{3173 }
\DoxyCodeLine{3174   \textcolor{comment}{! Move detrained water into the buffer layer.}}
\DoxyCodeLine{3175   \textcolor{keywordflow}{do} k=1,cs\%nkml}
\DoxyCodeLine{3176     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (h(i,k) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3177       ih = 1.0 / (h(i,nkmb) + h(i,k))}
\DoxyCodeLine{3178       \textcolor{keywordflow}{if} (cs\%TKE\_diagnostics) \&}
\DoxyCodeLine{3179         cs\%diag\_TKE\_conv\_s2(i,j) = cs\%diag\_TKE\_conv\_s2(i,j) + \&}
\DoxyCodeLine{3180             g\_h2\_2rho0dt * h(i,k) * h(i,nkmb) * (r0(i,nkmb) -\/ r0(i,k))}
\DoxyCodeLine{3181       \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain)) \&}
\DoxyCodeLine{3182         cs\%diag\_PE\_detrain(i,j) = cs\%diag\_PE\_detrain(i,j) + \&}
\DoxyCodeLine{3183             g\_h2\_2dt * h(i,k) * h(i,nkmb) * (r0(i,nkmb) -\/ r0(i,k))}
\DoxyCodeLine{3184       \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain2)) \&}
\DoxyCodeLine{3185         cs\%diag\_PE\_detrain2(i,j) = cs\%diag\_PE\_detrain2(i,j) + \&}
\DoxyCodeLine{3186             g\_h2\_2dt * h(i,k) * h(i,nkmb) * (r0(i,nkmb) -\/ r0(i,k))}
\DoxyCodeLine{3187 }
\DoxyCodeLine{3188       r0(i,nkmb) = (r0(i,nkmb)*h(i,nkmb) + r0(i,k)*h(i,k)) * ih}
\DoxyCodeLine{3189       rcv(i,nkmb) = (rcv(i,nkmb)*h(i,nkmb) + rcv(i,k)*h(i,k)) * ih}
\DoxyCodeLine{3190       t(i,nkmb) = (t(i,nkmb)*h(i,nkmb) + t(i,k)*h(i,k)) * ih}
\DoxyCodeLine{3191       s(i,nkmb) = (s(i,nkmb)*h(i,nkmb) + s(i,k)*h(i,k)) * ih}
\DoxyCodeLine{3192 }
\DoxyCodeLine{3193       d\_ea(i,k) = d\_ea(i,k) -\/ h(i,k)}
\DoxyCodeLine{3194       d\_ea(i,nkmb) = d\_ea(i,nkmb) + h(i,k)}
\DoxyCodeLine{3195       h(i,nkmb) = h(i,nkmb) + h(i,k)}
\DoxyCodeLine{3196       h(i,k) = 0.0}
\DoxyCodeLine{3197 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{3198 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{3199 }
\DoxyCodeLine{3200   \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3201     max\_det\_rem(i) = 10.0 * h(i,nkmb)}
\DoxyCodeLine{3202     \textcolor{keywordflow}{if} (max\_bl\_det(i) >= 0.0) max\_det\_rem(i) = max\_bl\_det(i)}
\DoxyCodeLine{3203 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{3204 }
\DoxyCodeLine{3205 \textcolor{comment}{!   If the mixed layer was denser than the densest interior layer,}}
\DoxyCodeLine{3206 \textcolor{comment}{! but is now lighter than this layer, leaving a buffer layer that}}
\DoxyCodeLine{3207 \textcolor{comment}{! is denser than this layer, there are problems.  This should prob-\/}}
\DoxyCodeLine{3208 \textcolor{comment}{! ably be considered a case of an inadequate choice of resolution in}}
\DoxyCodeLine{3209 \textcolor{comment}{! density space and should be avoided.  To make the model run sens-\/}}
\DoxyCodeLine{3210 \textcolor{comment}{! ibly in this case, it will make the mixed layer denser while making}}
\DoxyCodeLine{3211 \textcolor{comment}{! the buffer layer the density of the densest interior layer (pro-\/}}
\DoxyCodeLine{3212 \textcolor{comment}{! vided that the this will not make the mixed layer denser than the}}
\DoxyCodeLine{3213 \textcolor{comment}{! interior layer).  Otherwise, make the mixed layer the same density}}
\DoxyCodeLine{3214 \textcolor{comment}{! as the densest interior layer and lighten the buffer layer with}}
\DoxyCodeLine{3215 \textcolor{comment}{! the released buoyancy.  With multiple buffer layers, much more}}
\DoxyCodeLine{3216 \textcolor{comment}{! graceful options are available.}}
\DoxyCodeLine{3217   \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (h(i,nkmb) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3218     \textcolor{keywordflow}{if} ((r0(i,0)<r0(i,nz)) .and. (r0(i,nz)<r0(i,nkmb))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3219       \textcolor{keywordflow}{if} ((r0(i,nz)-\/r0(i,0))*h(i,0) > \&}
\DoxyCodeLine{3220           (r0(i,nkmb)-\/r0(i,nz))*h(i,nkmb)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3221         detrain(i) = (r0(i,nkmb)-\/r0(i,nz))*h(i,nkmb) / (r0(i,nkmb)-\/r0(i,0))}
\DoxyCodeLine{3222       \textcolor{keywordflow}{else}}
\DoxyCodeLine{3223         detrain(i) = (r0(i,nz)-\/r0(i,0))*h(i,0) / (r0(i,nkmb)-\/r0(i,0))}
\DoxyCodeLine{3224 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{3225 }
\DoxyCodeLine{3226       d\_eb(i,cs\%nkml) = d\_eb(i,cs\%nkml) + detrain(i)}
\DoxyCodeLine{3227       d\_ea(i,cs\%nkml) = d\_ea(i,cs\%nkml) -\/ detrain(i)}
\DoxyCodeLine{3228       d\_eb(i,nkmb) = d\_eb(i,nkmb) -\/ detrain(i)}
\DoxyCodeLine{3229       d\_ea(i,nkmb) = d\_ea(i,nkmb) + detrain(i)}
\DoxyCodeLine{3230 }
\DoxyCodeLine{3231       \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain)) cs\%diag\_PE\_detrain(i,j) = \&}
\DoxyCodeLine{3232         cs\%diag\_PE\_detrain(i,j) + g\_h2\_2dt * detrain(i)* \&}
\DoxyCodeLine{3233                      (h(i,0) + h(i,nkmb)) * (r0(i,nkmb) -\/ r0(i,0))}
\DoxyCodeLine{3234       x1 = r0(i,0)}
\DoxyCodeLine{3235       r0(i,0) = r0(i,0) -\/ detrain(i)*(r0(i,0)-\/r0(i,nkmb)) / h(i,0)}
\DoxyCodeLine{3236       r0(i,nkmb) = r0(i,nkmb) -\/ detrain(i)*(r0(i,nkmb)-\/x1) / h(i,nkmb)}
\DoxyCodeLine{3237       x1 = rcv(i,0)}
\DoxyCodeLine{3238       rcv(i,0) = rcv(i,0) -\/ detrain(i)*(rcv(i,0)-\/rcv(i,nkmb)) / h(i,0)}
\DoxyCodeLine{3239       rcv(i,nkmb) = rcv(i,nkmb) -\/ detrain(i)*(rcv(i,nkmb)-\/x1) / h(i,nkmb)}
\DoxyCodeLine{3240       x1 = t(i,0)}
\DoxyCodeLine{3241       t(i,0) = t(i,0) -\/ detrain(i)*(t(i,0)-\/t(i,nkmb)) / h(i,0)}
\DoxyCodeLine{3242       t(i,nkmb) = t(i,nkmb) -\/ detrain(i)*(t(i,nkmb)-\/x1) / h(i,nkmb)}
\DoxyCodeLine{3243       x1 = s(i,0)}
\DoxyCodeLine{3244       s(i,0) = s(i,0) -\/ detrain(i)*(s(i,0)-\/s(i,nkmb)) / h(i,0)}
\DoxyCodeLine{3245       s(i,nkmb) = s(i,nkmb) -\/ detrain(i)*(s(i,nkmb)-\/x1) / h(i,nkmb)}
\DoxyCodeLine{3246 }
\DoxyCodeLine{3247 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3248 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{3249 }
\DoxyCodeLine{3250   \textcolor{comment}{! Move water out of the buffer layer, if convenient.}}
\DoxyCodeLine{3251 \textcolor{comment}{!   Split the buffer layer if possible, and replace the buffer layer}}
\DoxyCodeLine{3252 \textcolor{comment}{! with a small amount of fluid from the mixed layer.}}
\DoxyCodeLine{3253 \textcolor{comment}{! This is the exponential-\/in-\/time splitting, circa 2005.}}
\DoxyCodeLine{3254   \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3255     \textcolor{keywordflow}{if} (h(i,nkmb) > 0.0) \textcolor{keywordflow}{then} ; splittable\_bl(i) = .true.}
\DoxyCodeLine{3256     \textcolor{keywordflow}{else} ; splittable\_bl(i) = .false. ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{3257 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{3258 }
\DoxyCodeLine{3259   dt\_ds\_wt2 = cs\%dT\_dS\_wt**2}
\DoxyCodeLine{3260 }
\DoxyCodeLine{3261   \textcolor{keywordflow}{do} k=nz-\/1,nkmb+1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3262     \textcolor{keywordflow}{if} (splittable\_bl(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3263       \textcolor{keywordflow}{if} (rcvtgt(k)<=rcv(i,nkmb)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3264 \textcolor{comment}{! Estimate dR/drho, dTheta/dR, and dS/dR, where R is the coordinate variable}}
\DoxyCodeLine{3265 \textcolor{comment}{! and rho is in-\/situ (or surface) potential density.}}
\DoxyCodeLine{3266 \textcolor{comment}{! There is no "right" way to do this, so this keeps things reasonable, if}}
\DoxyCodeLine{3267 \textcolor{comment}{! slightly arbitrary.}}
\DoxyCodeLine{3268         splittable\_bl(i) = .false.}
\DoxyCodeLine{3269 }
\DoxyCodeLine{3270         k1 = k+1 ; orthogonal\_extrap = .false.}
\DoxyCodeLine{3271         \textcolor{comment}{! Here we try to find a massive layer to use for interpolating the}}
\DoxyCodeLine{3272         \textcolor{comment}{! temperature and salinity.  If none is available a pseudo-\/orthogonal}}
\DoxyCodeLine{3273         \textcolor{comment}{! extrapolation is used.  The 10.0 and 0.9 in the following are}}
\DoxyCodeLine{3274         \textcolor{comment}{! arbitrary but probably about right.}}
\DoxyCodeLine{3275         \textcolor{keywordflow}{if} ((h(i,k+1) < 10.0*gv\%Angstrom\_H) .or. \&}
\DoxyCodeLine{3276             ((rcvtgt(k+1)-\/rcv(i,nkmb)) >= 0.9*(rcv(i,k1) -\/ rcv(i,0)))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3277           \textcolor{keywordflow}{if} (k>=nz-\/1) \textcolor{keywordflow}{then} ; orthogonal\_extrap = .true.}
\DoxyCodeLine{3278           \textcolor{keywordflow}{elseif} ((h(i,k+2) <= 10.0*gv\%Angstrom\_H) .and. \&}
\DoxyCodeLine{3279               ((rcvtgt(k+1)-\/rcv(i,nkmb)) < 0.9*(rcv(i,k+2)-\/rcv(i,0)))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3280             k1 = k+2}
\DoxyCodeLine{3281           \textcolor{keywordflow}{else} ; orthogonal\_extrap = .true. ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{3282 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{3283 }
\DoxyCodeLine{3284         \textcolor{keywordflow}{if} ((r0(i,0) >= r0(i,k1)) .or. (rcv(i,0) >= rcv(i,nkmb))) cycle}
\DoxyCodeLine{3285           \textcolor{comment}{! In this case there is an inversion of in-\/situ density relative to}}
\DoxyCodeLine{3286           \textcolor{comment}{! the coordinate variable.  Do not detrain from the buffer layer.}}
\DoxyCodeLine{3287 }
\DoxyCodeLine{3288         \textcolor{keywordflow}{if} (orthogonal\_extrap) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3289           \textcolor{comment}{! 36 here is a typical oceanic value of (dR/dS) / (dR/dT) -\/ it says}}
\DoxyCodeLine{3290           \textcolor{comment}{! that the relative weights of T \& S changes is a plausible 6:1.}}
\DoxyCodeLine{3291           \textcolor{comment}{! Also, this was coded on Athena's 6th birthday!}}
\DoxyCodeLine{3292           i\_denom = 1.0 / (drcv\_ds(i)**2 + dt\_ds\_wt2*drcv\_dt(i)**2)}
\DoxyCodeLine{3293           dt\_dr = dt\_ds\_wt2*drcv\_dt(i) * i\_denom}
\DoxyCodeLine{3294           ds\_dr = drcv\_ds(i) * i\_denom}
\DoxyCodeLine{3295         \textcolor{keywordflow}{else}}
\DoxyCodeLine{3296           dt\_dr = (t(i,0) -\/ t(i,k1)) / (rcv(i,0) -\/ rcv(i,k1))}
\DoxyCodeLine{3297           ds\_dr = (s(i,0) -\/ s(i,k1)) / (rcv(i,0) -\/ rcv(i,k1))}
\DoxyCodeLine{3298 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{3299         drml = dt\_time * (r0(i,nkmb) -\/ r0(i,0)) * \&}
\DoxyCodeLine{3300                (rcv(i,0) -\/ rcv(i,k1)) / (r0(i,0) -\/ r0(i,k1))}
\DoxyCodeLine{3301         \textcolor{comment}{! Once again, there is an apparent density inversion in Rcv.}}
\DoxyCodeLine{3302         \textcolor{keywordflow}{if} (drml < 0.0) cycle}
\DoxyCodeLine{3303         dr0\_drcv = (r0(i,0) -\/ r0(i,k1)) / (rcv(i,0) -\/ rcv(i,k1))}
\DoxyCodeLine{3304 }
\DoxyCodeLine{3305         \textcolor{keywordflow}{if} ((rcv(i,nkmb) -\/ drml < rcvtgt(k)) .and. (max\_det\_rem(i) > h(i,nkmb))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3306           \textcolor{comment}{! In this case, the buffer layer is split into two isopycnal layers.}}
\DoxyCodeLine{3307           detrain(i) = h(i,nkmb)*(rcv(i,nkmb) -\/ rcvtgt(k)) / \&}
\DoxyCodeLine{3308                                   (rcvtgt(k+1) -\/ rcvtgt(k))}
\DoxyCodeLine{3309 }
\DoxyCodeLine{3310           \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain)) cs\%diag\_PE\_detrain(i,j) = \&}
\DoxyCodeLine{3311             cs\%diag\_PE\_detrain(i,j) -\/ g\_h2\_2dt * detrain(i) * \&}
\DoxyCodeLine{3312                  (h(i,nkmb)-\/detrain(i)) * (rcvtgt(k+1) -\/ rcvtgt(k)) * dr0\_drcv}
\DoxyCodeLine{3313 }
\DoxyCodeLine{3314           tdown = detrain(i) * (t(i,nkmb) + dt\_dr*(rcvtgt(k+1)-\/rcv(i,nkmb)))}
\DoxyCodeLine{3315           t(i,k) = (h(i,k) * t(i,k) + \&}
\DoxyCodeLine{3316                         (h(i,nkmb) * t(i,nkmb) -\/ tdown)) / \&}
\DoxyCodeLine{3317                        (h(i,k) + (h(i,nkmb) -\/ detrain(i)))}
\DoxyCodeLine{3318           t(i,k+1) = (h(i,k+1) * t(i,k+1) + tdown)/ \&}
\DoxyCodeLine{3319                           (h(i,k+1) + detrain(i))}
\DoxyCodeLine{3320           t(i,nkmb) = t(i,0)}
\DoxyCodeLine{3321           sdown = detrain(i) * (s(i,nkmb) + ds\_dr*(rcvtgt(k+1)-\/rcv(i,nkmb)))}
\DoxyCodeLine{3322           s(i,k) = (h(i,k) * s(i,k) + \&}
\DoxyCodeLine{3323                       (h(i,nkmb) * s(i,nkmb) -\/ sdown)) / \&}
\DoxyCodeLine{3324                       (h(i,k) + (h(i,nkmb) -\/ detrain(i)))}
\DoxyCodeLine{3325           s(i,k+1) = (h(i,k+1) * s(i,k+1) + sdown)/ \&}
\DoxyCodeLine{3326                          (h(i,k+1) + detrain(i))}
\DoxyCodeLine{3327           s(i,nkmb) = s(i,0)}
\DoxyCodeLine{3328           rcv(i,nkmb) = rcv(i,0)}
\DoxyCodeLine{3329 }
\DoxyCodeLine{3330           d\_ea(i,k+1) = d\_ea(i,k+1) + detrain(i)}
\DoxyCodeLine{3331           d\_ea(i,k) = d\_ea(i,k) + (h(i,nkmb) -\/ detrain(i))}
\DoxyCodeLine{3332           d\_ea(i,nkmb) = d\_ea(i,nkmb) -\/ h(i,nkmb)}
\DoxyCodeLine{3333 }
\DoxyCodeLine{3334           h(i,k+1) = h(i,k+1) + detrain(i)}
\DoxyCodeLine{3335           h(i,k) = h(i,k) + h(i,nkmb) -\/ detrain(i)}
\DoxyCodeLine{3336           h(i,nkmb) = 0.0}
\DoxyCodeLine{3337         \textcolor{keywordflow}{else}}
\DoxyCodeLine{3338           \textcolor{comment}{! Here only part of the buffer layer is moved into the interior.}}
\DoxyCodeLine{3339           detrain(i) = h(i,nkmb) * drml / (rcvtgt(k+1) -\/ rcv(i,nkmb) + drml)}
\DoxyCodeLine{3340           \textcolor{keywordflow}{if} (detrain(i) > max\_det\_rem(i)) detrain(i) = max\_det\_rem(i)}
\DoxyCodeLine{3341           ih = 1.0 / (h(i,k+1) + detrain(i))}
\DoxyCodeLine{3342 }
\DoxyCodeLine{3343           tdown = (t(i,nkmb) + dt\_dr*(rcvtgt(k+1)-\/rcv(i,nkmb)))}
\DoxyCodeLine{3344           t(i,nkmb) = t(i,nkmb) -\/ dt\_dr * drml}
\DoxyCodeLine{3345           t(i,k+1) = (h(i,k+1) * t(i,k+1) + detrain(i) * tdown) * ih}
\DoxyCodeLine{3346           sdown = (s(i,nkmb) + ds\_dr*(rcvtgt(k+1)-\/rcv(i,nkmb)))}
\DoxyCodeLine{3347 \textcolor{comment}{!  The following two expressions updating S(nkmb) are mathematically identical.}}
\DoxyCodeLine{3348 \textcolor{comment}{!            S(i,nkmb) = (h(i,nkmb) * S(i,nkmb) -\/ detrain(i) * Sdown) / \&}}
\DoxyCodeLine{3349 \textcolor{comment}{!                           (h(i,nkmb) -\/ detrain(i))}}
\DoxyCodeLine{3350           s(i,nkmb) = s(i,nkmb) -\/ ds\_dr * drml}
\DoxyCodeLine{3351           s(i,k+1) = (h(i,k+1) * s(i,k+1) + detrain(i) * sdown) * ih}
\DoxyCodeLine{3352 }
\DoxyCodeLine{3353           d\_ea(i,k+1) = d\_ea(i,k+1) + detrain(i)}
\DoxyCodeLine{3354           d\_ea(i,nkmb) = d\_ea(i,nkmb) -\/ detrain(i)}
\DoxyCodeLine{3355 }
\DoxyCodeLine{3356           h(i,k+1) = h(i,k+1) + detrain(i)}
\DoxyCodeLine{3357           h(i,nkmb) = h(i,nkmb) -\/ detrain(i)}
\DoxyCodeLine{3358 }
\DoxyCodeLine{3359           \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain)) cs\%diag\_PE\_detrain(i,j) = \&}
\DoxyCodeLine{3360             cs\%diag\_PE\_detrain(i,j) -\/ g\_h2\_2dt * detrain(i) * dr0\_drcv * \&}
\DoxyCodeLine{3361                  (h(i,nkmb)-\/detrain(i)) * (rcvtgt(k+1) -\/ rcv(i,nkmb) + drml)}
\DoxyCodeLine{3362 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{3363 \textcolor{keywordflow}{      endif} \textcolor{comment}{! RcvTgt(k)<=Rcv(i,nkmb)}}
\DoxyCodeLine{3364 \textcolor{keywordflow}{    endif} \textcolor{comment}{! splittable\_BL}}
\DoxyCodeLine{3365 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i \& k loops}}
\DoxyCodeLine{3366 }
\DoxyCodeLine{3367 \textcolor{comment}{!   The numerical behavior of the buffer layer is dramatically improved}}
\DoxyCodeLine{3368 \textcolor{comment}{! if it is always at least a small fraction (say 10\%) of the thickness}}
\DoxyCodeLine{3369 \textcolor{comment}{! of the mixed layer.  As the physical distinction between the mixed}}
\DoxyCodeLine{3370 \textcolor{comment}{! and buffer layers is vague anyway, this seems hard to argue against.}}
\DoxyCodeLine{3371   \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3372     \textcolor{keywordflow}{if} (h(i,nkmb) < 0.1*h(i,0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3373       h\_ent =  0.1*h(i,0) -\/ h(i,nkmb)}
\DoxyCodeLine{3374       ih = 10.0/h(i,0)}
\DoxyCodeLine{3375       t(i,nkmb) = (h(i,nkmb)*t(i,nkmb) + h\_ent*t(i,0)) * ih}
\DoxyCodeLine{3376       s(i,nkmb) = (h(i,nkmb)*s(i,nkmb) + h\_ent*s(i,0)) * ih}
\DoxyCodeLine{3377 }
\DoxyCodeLine{3378       d\_ea(i,1) = d\_ea(i,1) -\/ h\_ent}
\DoxyCodeLine{3379       d\_ea(i,nkmb) = d\_ea(i,nkmb) + h\_ent}
\DoxyCodeLine{3380 }
\DoxyCodeLine{3381       h(i,0) = h(i,0) -\/ h\_ent}
\DoxyCodeLine{3382       h(i,nkmb) = h(i,nkmb) + h\_ent}
\DoxyCodeLine{3383 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3384 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{3385 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_a5f7d06425d0395a7fd4b94942c6465d0}\label{namespacemom__bulk__mixed__layer_a5f7d06425d0395a7fd4b94942c6465d0}} 
\index{mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}!mixedlayer\_detrain\_2@{mixedlayer\_detrain\_2}}
\index{mixedlayer\_detrain\_2@{mixedlayer\_detrain\_2}!mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}}
\doxysubsubsection{\texorpdfstring{mixedlayer\_detrain\_2()}{mixedlayer\_detrain\_2()}}
{\footnotesize\ttfamily subroutine mom\+\_\+bulk\+\_\+mixed\+\_\+layer\+::mixedlayer\+\_\+detrain\+\_\+2 (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szk0\+\_\+(gv)), intent(inout)}]{h,  }\item[{real, dimension(szi\+\_\+(g),szk0\+\_\+(gv)), intent(inout)}]{T,  }\item[{real, dimension(szi\+\_\+(g),szk0\+\_\+(gv)), intent(inout)}]{S,  }\item[{real, dimension(szi\+\_\+(g),szk0\+\_\+(gv)), intent(inout)}]{R0,  }\item[{real, dimension(szi\+\_\+(g),szk0\+\_\+(gv)), intent(inout)}]{Rcv,  }\item[{real, dimension(szk\+\_\+(gv)), intent(in)}]{Rcv\+Tgt,  }\item[{real, intent(in)}]{dt,  }\item[{real, intent(in)}]{dt\+\_\+diag,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(gv)), intent(inout)}]{d\+\_\+ea,  }\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__bulk__mixed__layer_1_1bulkmixedlayer__cs}{bulkmixedlayer\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{d\+R0\+\_\+dT,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{d\+R0\+\_\+dS,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{d\+Rcv\+\_\+dT,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{d\+Rcv\+\_\+dS,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{max\+\_\+\+B\+L\+\_\+det }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine moves any water left in the former mixed layers into the two buffer layers and may also move buffer layer water into the interior isopycnal layers. 


\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,out}}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. Layer 0 is the new mixed layer. \\
\hline
\mbox{\texttt{ in,out}}  & {\em t} & Potential temperature \mbox{[}degC\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em s} & Salinity \mbox{[}ppt\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em r0} & Potential density referenced to surface pressure \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em rcv} & The coordinate defining potential density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em rcvtgt} & The target value of Rcv for each layer \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt\+\_\+diag} & The diagnostic time step \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em d\+\_\+ea} & The upward increase across a layer in the entrainment from above \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. Positive d\+\_\+ea goes with layer thickness increases. \\
\hline
\mbox{\texttt{ in}}  & {\em j} & The meridional row to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & The control structure returned by a previous call to mixedlayer\+\_\+init. \\
\hline
\mbox{\texttt{ in}}  & {\em dr0\+\_\+dt} & The partial derivative of potential density referenced to the surface with potential temperature, \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dr0\+\_\+ds} & The partial derivative of cpotential density referenced to the surface with salinity \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em drcv\+\_\+dt} & The partial derivative of coordinate defining potential density with potential temperature, \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em drcv\+\_\+ds} & The partial derivative of coordinate defining potential density with salinity \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em max\+\_\+bl\+\_\+det} & If non-\/negative, the maximum detrainment permitted from the buffer layers \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 2214 of file M\+O\+M\+\_\+bulk\+\_\+mixed\+\_\+layer.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{2214   \textcolor{keywordtype}{type}(ocean\_grid\_type),              \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{2215   \textcolor{keywordtype}{type}(verticalGrid\_type),            \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{2216 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{    !< Layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2217 \textcolor{comment}{                                                            !!  Layer 0 is the new mixed layer.}}
\DoxyCodeLine{2218 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: T\textcolor{comment}{    !< Potential temperature [degC].}}
\DoxyCodeLine{2219 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: S\textcolor{comment}{    !< Salinity [ppt].}}
\DoxyCodeLine{2220 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: R0\textcolor{comment}{   !< Potential density referenced to}}
\DoxyCodeLine{2221 \textcolor{comment}{                                                            !! surface pressure [R \string~> kg m-\/3].}}
\DoxyCodeLine{2222 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: Rcv\textcolor{comment}{  !< The coordinate defining potential}}
\DoxyCodeLine{2223 \textcolor{comment}{                                                            !! density [R \string~> kg m-\/3].}}
\DoxyCodeLine{2224 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV))},          \textcolor{keywordtype}{intent(in)}    :: RcvTgt\textcolor{comment}{  !< The target value of Rcv for each}}
\DoxyCodeLine{2225 \textcolor{comment}{                                                            !! layer [R \string~> kg m-\/3].}}
\DoxyCodeLine{2226 \textcolor{keywordtype}{  real},                               \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{   !< Time increment [T \string~> s].}}
\DoxyCodeLine{2227 \textcolor{keywordtype}{  real},                               \textcolor{keywordtype}{intent(in)}    :: dt\_diag\textcolor{comment}{ !< The diagnostic time step [T \string~> s].}}
\DoxyCodeLine{2228 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},  \textcolor{keywordtype}{intent(inout)} :: d\_ea\textcolor{comment}{ !< The upward increase across a layer in}}
\DoxyCodeLine{2229 \textcolor{comment}{                                                            !! the entrainment from above}}
\DoxyCodeLine{2230 \textcolor{comment}{                                                            !! [H \string~> m or kg m-\/2]. Positive d\_ea}}
\DoxyCodeLine{2231 \textcolor{comment}{                                                            !! goes with layer thickness increases.}}
\DoxyCodeLine{2232   \textcolor{keywordtype}{integer},                            \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{    !< The meridional row to work on.}}
\DoxyCodeLine{2233   \textcolor{keywordtype}{type}(unit\_scale\_type),              \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}}
\DoxyCodeLine{2234   \textcolor{keywordtype}{type}(bulkmixedlayer\_CS),            \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< The control structure returned by a}}
\DoxyCodeLine{2235 \textcolor{comment}{                                                            !! previous call to mixedlayer\_init.}}
\DoxyCodeLine{2236 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: dR0\_dT\textcolor{comment}{  !< The partial derivative of}}
\DoxyCodeLine{2237 \textcolor{comment}{                                                            !! potential density referenced to the}}
\DoxyCodeLine{2238 \textcolor{comment}{                                                            !! surface with potential temperature,}}
\DoxyCodeLine{2239 \textcolor{comment}{                                                            !! [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{2240 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: dR0\_dS\textcolor{comment}{  !< The partial derivative of}}
\DoxyCodeLine{2241 \textcolor{comment}{                                                            !! cpotential density referenced to the}}
\DoxyCodeLine{2242 \textcolor{comment}{                                                            !! surface with salinity}}
\DoxyCodeLine{2243 \textcolor{comment}{                                                            !! [R ppt-\/1 \string~> kg m-\/3 ppt-\/1].}}
\DoxyCodeLine{2244 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: dRcv\_dT\textcolor{comment}{ !< The partial derivative of}}
\DoxyCodeLine{2245 \textcolor{comment}{                                                            !! coordinate defining potential density}}
\DoxyCodeLine{2246 \textcolor{comment}{                                                            !! with potential temperature,}}
\DoxyCodeLine{2247 \textcolor{comment}{                                                            !! [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{2248 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: dRcv\_dS\textcolor{comment}{ !< The partial derivative of}}
\DoxyCodeLine{2249 \textcolor{comment}{                                                            !! coordinate defining potential density}}
\DoxyCodeLine{2250 \textcolor{comment}{                                                            !! with salinity [R ppt-\/1 \string~> kg m-\/3 ppt-\/1].}}
\DoxyCodeLine{2251 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: max\_BL\_det\textcolor{comment}{ !< If non-\/negative, the maximum}}
\DoxyCodeLine{2252 \textcolor{comment}{                                                            !! detrainment permitted from the buffer}}
\DoxyCodeLine{2253 \textcolor{comment}{                                                            !! layers [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2254 }
\DoxyCodeLine{2255 \textcolor{comment}{! This subroutine moves any water left in the former mixed layers into the}}
\DoxyCodeLine{2256 \textcolor{comment}{! two buffer layers and may also move buffer layer water into the interior}}
\DoxyCodeLine{2257 \textcolor{comment}{! isopycnal layers.}}
\DoxyCodeLine{2258 }
\DoxyCodeLine{2259   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{2260 \textcolor{keywordtype}{  real} :: h\_to\_bl                 \textcolor{comment}{! The total thickness detrained to the buffer}}
\DoxyCodeLine{2261                                   \textcolor{comment}{! layers [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2262 \textcolor{keywordtype}{  real} :: R0\_to\_bl                \textcolor{comment}{! The depth integrated amount of R0 that is detrained to the}}
\DoxyCodeLine{2263                                   \textcolor{comment}{! buffer layer [H R \string~> kg m-\/2 or kg2 m-\/5]}}
\DoxyCodeLine{2264 \textcolor{keywordtype}{  real} :: Rcv\_to\_bl               \textcolor{comment}{! The depth integrated amount of Rcv that is detrained to the}}
\DoxyCodeLine{2265                                   \textcolor{comment}{! buffer layer [H R \string~> kg m-\/2 or kg2 m-\/5]}}
\DoxyCodeLine{2266 \textcolor{keywordtype}{  real} :: T\_to\_bl                 \textcolor{comment}{! The depth integrated amount of T that is detrained to the}}
\DoxyCodeLine{2267                                   \textcolor{comment}{! buffer layer [degC H \string~> degC m or degC kg m-\/2]}}
\DoxyCodeLine{2268 \textcolor{keywordtype}{  real} :: S\_to\_bl                 \textcolor{comment}{! The depth integrated amount of S that is detrained to the}}
\DoxyCodeLine{2269                                   \textcolor{comment}{! buffer layer [ppt H \string~> ppt m or ppt kg m-\/2]}}
\DoxyCodeLine{2270 \textcolor{keywordtype}{  real} :: h\_min\_bl                \textcolor{comment}{! The minimum buffer layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2271 }
\DoxyCodeLine{2272 \textcolor{keywordtype}{  real} :: h1, h2                  \textcolor{comment}{! Scalar variables holding the values of}}
\DoxyCodeLine{2273                                   \textcolor{comment}{! h(i,CS\%nkml+1) and h(i,CS\%nkml+2) [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2274 \textcolor{keywordtype}{  real} :: h1\_avail                \textcolor{comment}{! The thickness of the upper buffer layer}}
\DoxyCodeLine{2275                                   \textcolor{comment}{! available to move into the lower buffer}}
\DoxyCodeLine{2276                                   \textcolor{comment}{! layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2277 \textcolor{keywordtype}{  real} :: stays                   \textcolor{comment}{! stays is the thickness of the upper buffer}}
\DoxyCodeLine{2278                                   \textcolor{comment}{! layer that remains there [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2279 \textcolor{keywordtype}{  real} :: stays\_min, stays\_max    \textcolor{comment}{! The minimum and maximum permitted values of}}
\DoxyCodeLine{2280                                   \textcolor{comment}{! stays [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2281 }
\DoxyCodeLine{2282   \textcolor{keywordtype}{logical} :: mergeable\_bl         \textcolor{comment}{! If true, it is an option to combine the two}}
\DoxyCodeLine{2283                                   \textcolor{comment}{! buffer layers and create water that matches}}
\DoxyCodeLine{2284                                   \textcolor{comment}{! the target density of an interior layer.}}
\DoxyCodeLine{2285 \textcolor{keywordtype}{  real} :: stays\_merge             \textcolor{comment}{! If the two buffer layers can be combined}}
\DoxyCodeLine{2286                                   \textcolor{comment}{! stays\_merge is the thickness of the upper}}
\DoxyCodeLine{2287                                   \textcolor{comment}{! layer that remains [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2288 \textcolor{keywordtype}{  real} :: stays\_min\_merge         \textcolor{comment}{! The minimum allowed value of stays\_merge [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2289 }
\DoxyCodeLine{2290 \textcolor{keywordtype}{  real} :: dR0\_2dz, dRcv\_2dz       \textcolor{comment}{! Half the vertical gradients of R0 and Rcv [R H-\/1 \string~> kg m-\/4 or m-\/1]}}
\DoxyCodeLine{2291 \textcolor{comment}{!  real :: dT\_2dz, dS\_2dz         ! Half the vertical gradients of T and S, in degC H-\/1, and ppt H-\/1.}}
\DoxyCodeLine{2292 \textcolor{keywordtype}{  real} :: scale\_slope             \textcolor{comment}{! A nondimensional number < 1 used to scale down}}
\DoxyCodeLine{2293                                   \textcolor{comment}{! the slope within the upper buffer layer when}}
\DoxyCodeLine{2294                                   \textcolor{comment}{! water MUST be detrained to the lower layer.}}
\DoxyCodeLine{2295 }
\DoxyCodeLine{2296 \textcolor{keywordtype}{  real} :: dPE\_extrap              \textcolor{comment}{! The potential energy change due to dispersive}}
\DoxyCodeLine{2297                                   \textcolor{comment}{! advection or mixing layers, divided by}}
\DoxyCodeLine{2298                                   \textcolor{comment}{! rho\_0*g [H2 \string~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{2299 \textcolor{keywordtype}{  real} :: dPE\_det, dPE\_merge      \textcolor{comment}{! The energy required to mix the detrained water}}
\DoxyCodeLine{2300                                   \textcolor{comment}{! into the buffer layer or the merge the two}}
\DoxyCodeLine{2301                                   \textcolor{comment}{! buffer layers [R H2 L2 Z-\/1 T-\/2 \string~> J m-\/2 or J kg2 m-\/8].}}
\DoxyCodeLine{2302 }
\DoxyCodeLine{2303 \textcolor{keywordtype}{  real} :: h\_from\_ml               \textcolor{comment}{! The amount of additional water that must be}}
\DoxyCodeLine{2304                                   \textcolor{comment}{! drawn from the mixed layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2305 \textcolor{keywordtype}{  real} :: h\_det\_h2                \textcolor{comment}{! The amount of detrained water and mixed layer}}
\DoxyCodeLine{2306                                   \textcolor{comment}{! water that will go directly into the lower}}
\DoxyCodeLine{2307                                   \textcolor{comment}{! buffer layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2308 \textcolor{keywordtype}{  real} :: h\_det\_to\_h2, h\_ml\_to\_h2 \textcolor{comment}{! All of the variables hA\_to\_hB are the thickness fluxes}}
\DoxyCodeLine{2309 \textcolor{keywordtype}{  real} :: h\_det\_to\_h1, h\_ml\_to\_h1 \textcolor{comment}{! from one layer to another [H \string~> m or kg m-\/2],}}
\DoxyCodeLine{2310 \textcolor{keywordtype}{  real} :: h1\_to\_h2, h1\_to\_k0      \textcolor{comment}{! with h\_det the detrained water, h\_ml}}
\DoxyCodeLine{2311 \textcolor{keywordtype}{  real} :: h2\_to\_k1, h2\_to\_k1\_rem  \textcolor{comment}{! the actively mixed layer, h1 and h2 the upper}}
\DoxyCodeLine{2312                                   \textcolor{comment}{! and lower buffer layers, and k0 and k1 the}}
\DoxyCodeLine{2313                                   \textcolor{comment}{! interior layers that are just lighter and}}
\DoxyCodeLine{2314                                   \textcolor{comment}{! just denser than the lower buffer layer.}}
\DoxyCodeLine{2315 }
\DoxyCodeLine{2316 \textcolor{keywordtype}{  real} :: R0\_det, T\_det, S\_det    \textcolor{comment}{! Detrained values of R0 [R \string~> kg m-\/3], T [degC], and S [ppt].}}
\DoxyCodeLine{2317 \textcolor{keywordtype}{  real} :: Rcv\_stays, R0\_stays     \textcolor{comment}{! Values of Rcv and R0 that stay in a layer.}}
\DoxyCodeLine{2318 \textcolor{keywordtype}{  real} :: T\_stays, S\_stays        \textcolor{comment}{! Values of T and S that stay in a layer.}}
\DoxyCodeLine{2319 \textcolor{keywordtype}{  real} :: dSpice\_det, dSpice\_stays\textcolor{comment}{! The spiciness difference between an original}}
\DoxyCodeLine{2320                                   \textcolor{comment}{! buffer layer and the water that moves into}}
\DoxyCodeLine{2321                                   \textcolor{comment}{! an interior layer or that stays in that}}
\DoxyCodeLine{2322                                   \textcolor{comment}{! layer [R \string~> kg m-\/3].}}
\DoxyCodeLine{2323 \textcolor{keywordtype}{  real} :: dSpice\_lim, dSpice\_lim2 \textcolor{comment}{! Limits to the spiciness difference between}}
\DoxyCodeLine{2324                                   \textcolor{comment}{! the lower buffer layer and the water that}}
\DoxyCodeLine{2325                                   \textcolor{comment}{! moves into an interior layer [R \string~> kg m-\/3].}}
\DoxyCodeLine{2326 \textcolor{keywordtype}{  real} :: dSpice\_2dz              \textcolor{comment}{! The vertical gradient of spiciness used for}}
\DoxyCodeLine{2327                                   \textcolor{comment}{! advection [R H-\/1 \string~> kg m-\/4 or m-\/1].}}
\DoxyCodeLine{2328 }
\DoxyCodeLine{2329 \textcolor{keywordtype}{  real} :: dPE\_ratio               \textcolor{comment}{! Multiplier of dPE\_det at which merging is}}
\DoxyCodeLine{2330                                   \textcolor{comment}{! permitted -\/ here (detrainment\_per\_day/dt)*30}}
\DoxyCodeLine{2331                                   \textcolor{comment}{! days?}}
\DoxyCodeLine{2332 \textcolor{keywordtype}{  real} :: num\_events              \textcolor{comment}{! The number of detrainment events over which}}
\DoxyCodeLine{2333                                   \textcolor{comment}{! to prefer merging the buffer layers.}}
\DoxyCodeLine{2334 \textcolor{keywordtype}{  real} :: dPE\_time\_ratio          \textcolor{comment}{! Larger of 1 and the detrainment timescale over dt [nondim].}}
\DoxyCodeLine{2335 \textcolor{keywordtype}{  real} :: dT\_dS\_gauge, dS\_dT\_gauge \textcolor{comment}{! The relative scales of temperature and}}
\DoxyCodeLine{2336                                   \textcolor{comment}{! salinity changes in defining spiciness, in}}
\DoxyCodeLine{2337                                   \textcolor{comment}{! [degC ppt-\/1] and [ppt degC-\/1].}}
\DoxyCodeLine{2338 \textcolor{keywordtype}{  real} :: I\_denom                 \textcolor{comment}{! A work variable with units of [ppt2 R-\/2 \string~> ppt2 m6 kg-\/2].}}
\DoxyCodeLine{2339 }
\DoxyCodeLine{2340 \textcolor{keywordtype}{  real} :: g\_2                     \textcolor{comment}{! 1/2 g\_Earth [L2 Z-\/1 T-\/2 \string~> m s-\/2].}}
\DoxyCodeLine{2341 \textcolor{keywordtype}{  real} :: Rho0xG                  \textcolor{comment}{! Rho0 times G\_Earth [R L2 Z-\/1 T-\/2 \string~> kg m-\/2 s-\/2].}}
\DoxyCodeLine{2342 \textcolor{keywordtype}{  real} :: I2Rho0                  \textcolor{comment}{! 1 / (2 Rho0) [R-\/1 \string~> m3 kg-\/1].}}
\DoxyCodeLine{2343 \textcolor{keywordtype}{  real} :: Idt\_H2                  \textcolor{comment}{! The square of the conversion from thickness to Z}}
\DoxyCodeLine{2344                                   \textcolor{comment}{! divided by the time step [Z2 H-\/2 T-\/1 \string~> s-\/1 or m6 kg-\/2 s-\/1].}}
\DoxyCodeLine{2345   \textcolor{keywordtype}{logical} :: stable\_Rcv           \textcolor{comment}{! If true, the buffer layers are stable with}}
\DoxyCodeLine{2346                                   \textcolor{comment}{! respect to the coordinate potential density.}}
\DoxyCodeLine{2347 \textcolor{keywordtype}{  real} :: h\_neglect \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{2348                     \textcolor{comment}{! in roundoff and can be neglected [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2349 }
\DoxyCodeLine{2350 \textcolor{keywordtype}{  real} :: s1en                    \textcolor{comment}{! A work variable [H2 L2 kg m-\/1 T-\/3 \string~> kg m3 s-\/3 or kg3 m-\/3 s-\/3].}}
\DoxyCodeLine{2351 \textcolor{keywordtype}{  real} :: s1, s2, bh0             \textcolor{comment}{! Work variables [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2352 \textcolor{keywordtype}{  real} :: s3sq                    \textcolor{comment}{! A work variable [H2 \string~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{2353 \textcolor{keywordtype}{  real} :: I\_ya, b1                \textcolor{comment}{! Nondimensional work variables.}}
\DoxyCodeLine{2354 \textcolor{keywordtype}{  real} :: Ih, Ihdet, Ih1f, Ih2f   \textcolor{comment}{! Assorted inverse thickness work variables,}}
\DoxyCodeLine{2355 \textcolor{keywordtype}{  real} :: Ihk0, Ihk1, Ih12        \textcolor{comment}{! all in [H-\/1 \string~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{2356 \textcolor{keywordtype}{  real} :: dR1, dR2, dR2b, dRk1    \textcolor{comment}{! Assorted density difference work variables,}}
\DoxyCodeLine{2357 \textcolor{keywordtype}{  real} :: dR0, dR21, dRcv         \textcolor{comment}{! all in [R \string~> kg m-\/3].}}
\DoxyCodeLine{2358 \textcolor{keywordtype}{  real} :: dRcv\_stays, dRcv\_det, dRcv\_lim}
\DoxyCodeLine{2359 \textcolor{keywordtype}{  real} :: Angstrom                \textcolor{comment}{! The minumum layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{2360 }
\DoxyCodeLine{2361 \textcolor{keywordtype}{  real} :: h2\_to\_k1\_lim, T\_new, S\_new, T\_max, T\_min, S\_max, S\_min}
\DoxyCodeLine{2362   \textcolor{keywordtype}{character(len=200)} :: mesg}
\DoxyCodeLine{2363 }
\DoxyCodeLine{2364   \textcolor{keywordtype}{integer} :: i, k, k0, k1, is, ie, nz, kb1, kb2, nkmb}
\DoxyCodeLine{2365   is = g\%isc ; ie = g\%iec ; nz = gv\%ke}
\DoxyCodeLine{2366   kb1 = cs\%nkml+1; kb2 = cs\%nkml+2}
\DoxyCodeLine{2367   nkmb = cs\%nkml+cs\%nkbl}
\DoxyCodeLine{2368   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{2369   g\_2 = 0.5 * gv\%g\_Earth}
\DoxyCodeLine{2370   rho0xg = gv\%Rho0 * gv\%g\_Earth}
\DoxyCodeLine{2371   idt\_h2 = gv\%H\_to\_Z**2 / dt\_diag}
\DoxyCodeLine{2372   i2rho0 = 0.5 / (gv\%Rho0)}
\DoxyCodeLine{2373   angstrom = gv\%Angstrom\_H}
\DoxyCodeLine{2374 }
\DoxyCodeLine{2375   \textcolor{comment}{! This is hard coding of arbitrary and dimensional numbers.}}
\DoxyCodeLine{2376   dt\_ds\_gauge = cs\%dT\_dS\_wt ; ds\_dt\_gauge = 1.0 / dt\_ds\_gauge}
\DoxyCodeLine{2377   num\_events = 10.0}
\DoxyCodeLine{2378 }
\DoxyCodeLine{2379   \textcolor{keywordflow}{if} (cs\%nkbl /= 2) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_mixed\_layer"}// \&}
\DoxyCodeLine{2380                         \textcolor{stringliteral}{"CS\%nkbl must be 2 in mixedlayer\_detrain\_2."})}
\DoxyCodeLine{2381 }
\DoxyCodeLine{2382   \textcolor{keywordflow}{if} (dt < cs\%BL\_detrain\_time) \textcolor{keywordflow}{then} ; dpe\_time\_ratio = cs\%BL\_detrain\_time / (dt)}
\DoxyCodeLine{2383   \textcolor{keywordflow}{else} ; dpe\_time\_ratio = 1.0 ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{2384 }
\DoxyCodeLine{2385   \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2386 }
\DoxyCodeLine{2387   \textcolor{comment}{! Determine all of the properties being detrained from the mixed layer.}}
\DoxyCodeLine{2388 }
\DoxyCodeLine{2389   \textcolor{comment}{! As coded this has the k and i loop orders switched, but k is CS\%nkml is}}
\DoxyCodeLine{2390   \textcolor{comment}{! often just 1 or 2, so this seems like it should not be a problem, especially}}
\DoxyCodeLine{2391   \textcolor{comment}{! since it means that a number of variables can now be scalars, not arrays.}}
\DoxyCodeLine{2392     h\_to\_bl = 0.0 ; r0\_to\_bl = 0.0}
\DoxyCodeLine{2393     rcv\_to\_bl = 0.0 ; t\_to\_bl = 0.0 ; s\_to\_bl = 0.0}
\DoxyCodeLine{2394 }
\DoxyCodeLine{2395     \textcolor{keywordflow}{do} k=1,cs\%nkml ; \textcolor{keywordflow}{if} (h(i,k) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2396       h\_to\_bl = h\_to\_bl + h(i,k)}
\DoxyCodeLine{2397       r0\_to\_bl = r0\_to\_bl + r0(i,k)*h(i,k)}
\DoxyCodeLine{2398 }
\DoxyCodeLine{2399       rcv\_to\_bl = rcv\_to\_bl + rcv(i,k)*h(i,k)}
\DoxyCodeLine{2400       t\_to\_bl = t\_to\_bl + t(i,k)*h(i,k)}
\DoxyCodeLine{2401       s\_to\_bl = s\_to\_bl + s(i,k)*h(i,k)}
\DoxyCodeLine{2402 }
\DoxyCodeLine{2403       d\_ea(i,k) = d\_ea(i,k) -\/ h(i,k)}
\DoxyCodeLine{2404       h(i,k) = 0.0}
\DoxyCodeLine{2405 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2406     \textcolor{keywordflow}{if} (h\_to\_bl > 0.0) \textcolor{keywordflow}{then} ; r0\_det = r0\_to\_bl / h\_to\_bl}
\DoxyCodeLine{2407     \textcolor{keywordflow}{else} ; r0\_det = r0(i,0) ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{2408 }
\DoxyCodeLine{2409     \textcolor{comment}{! This code does both downward detrainment from both the mixed layer and the}}
\DoxyCodeLine{2410     \textcolor{comment}{! buffer layers.}}
\DoxyCodeLine{2411     \textcolor{comment}{!   Several considerations apply in detraining water into the interior:}}
\DoxyCodeLine{2412     \textcolor{comment}{! (1) Water only moves into the interior from the deeper buffer layer,}}
\DoxyCodeLine{2413     \textcolor{comment}{!     so the deeper buffer layer must have some mass.}}
\DoxyCodeLine{2414     \textcolor{comment}{! (2) The upper buffer layer must have some mass so the extrapolation of}}
\DoxyCodeLine{2415     \textcolor{comment}{!     density is meaningful (i.e. there is not detrainment from the buffer}}
\DoxyCodeLine{2416     \textcolor{comment}{!     layers when there is strong mixed layer entrainment).}}
\DoxyCodeLine{2417     \textcolor{comment}{! (3) The lower buffer layer density extrapolated to its base with a}}
\DoxyCodeLine{2418     \textcolor{comment}{!     linear fit between the two layers must exceed the density of the}}
\DoxyCodeLine{2419     \textcolor{comment}{!     next denser interior layer.}}
\DoxyCodeLine{2420     \textcolor{comment}{! (4) The average extroplated coordinate density that is moved into the}}
\DoxyCodeLine{2421     \textcolor{comment}{!     isopycnal interior matches the target value for that layer.}}
\DoxyCodeLine{2422     \textcolor{comment}{! (5) The potential energy change is calculated and might be used later}}
\DoxyCodeLine{2423     \textcolor{comment}{!     to allow the upper buffer layer to mix more into the lower buffer}}
\DoxyCodeLine{2424     \textcolor{comment}{!     layer.}}
\DoxyCodeLine{2425 }
\DoxyCodeLine{2426     \textcolor{comment}{! Determine whether more must be detrained from the mixed layer to keep a}}
\DoxyCodeLine{2427     \textcolor{comment}{! minimal amount of mass in the buffer layers.  In this case the 5\% of the}}
\DoxyCodeLine{2428     \textcolor{comment}{! mixed layer thickness is hard-\/coded, but probably shouldn't be!}}
\DoxyCodeLine{2429     h\_min\_bl = min(cs\%Hbuffer\_min, cs\%Hbuffer\_rel\_min*h(i,0))}
\DoxyCodeLine{2430 }
\DoxyCodeLine{2431     stable\_rcv = .true.}
\DoxyCodeLine{2432     \textcolor{keywordflow}{if} (((r0(i,kb2)-\/r0(i,kb1)) * (rcv(i,kb2)-\/rcv(i,kb1)) <= 0.0)) \&}
\DoxyCodeLine{2433       stable\_rcv = .false.}
\DoxyCodeLine{2434 }
\DoxyCodeLine{2435     h1 = h(i,kb1) ; h2 = h(i,kb2)}
\DoxyCodeLine{2436 }
\DoxyCodeLine{2437     h2\_to\_k1\_rem = (h1 + h2) + h\_to\_bl}
\DoxyCodeLine{2438     \textcolor{keywordflow}{if} ((max\_bl\_det(i) >= 0.0) .and. (h2\_to\_k1\_rem > max\_bl\_det(i))) \&}
\DoxyCodeLine{2439       h2\_to\_k1\_rem = max\_bl\_det(i)}
\DoxyCodeLine{2440 }
\DoxyCodeLine{2441 }
\DoxyCodeLine{2442     \textcolor{keywordflow}{if} ((h2 == 0.0) .and. (h1 > 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2443       \textcolor{comment}{! The lower buffer layer has been eliminated either by convective}}
\DoxyCodeLine{2444       \textcolor{comment}{! adjustment or entrainment from the interior, and its current properties}}
\DoxyCodeLine{2445       \textcolor{comment}{! are not meaningful, but may later be used to determine the properties of}}
\DoxyCodeLine{2446       \textcolor{comment}{! waters moving into the lower buffer layer.  So the properties of the}}
\DoxyCodeLine{2447       \textcolor{comment}{! lower buffer layer are set to be between those of the upper buffer layer}}
\DoxyCodeLine{2448       \textcolor{comment}{! and the next denser interior layer, measured by R0.  This probably does}}
\DoxyCodeLine{2449       \textcolor{comment}{! not happen very often, so I am not too worried about the inefficiency of}}
\DoxyCodeLine{2450       \textcolor{comment}{! the following loop.}}
\DoxyCodeLine{2451       \textcolor{keywordflow}{do} k1=kb2+1,nz ; \textcolor{keywordflow}{if} (h(i,k1) > 2.0*angstrom) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2452 }
\DoxyCodeLine{2453       r0(i,kb2) = r0(i,kb1)}
\DoxyCodeLine{2454 }
\DoxyCodeLine{2455       rcv(i,kb2)=rcv(i,kb1) ; t(i,kb2)=t(i,kb1) ; s(i,kb2)=s(i,kb1)}
\DoxyCodeLine{2456 }
\DoxyCodeLine{2457 }
\DoxyCodeLine{2458       \textcolor{keywordflow}{if} (k1 <= nz) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (r0(i,k1) >= r0(i,kb1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2459         r0(i,kb2) = 0.5*(r0(i,kb1)+r0(i,k1))}
\DoxyCodeLine{2460 }
\DoxyCodeLine{2461         rcv(i,kb2) = 0.5*(rcv(i,kb1)+rcv(i,k1))}
\DoxyCodeLine{2462         t(i,kb2) = 0.5*(t(i,kb1)+t(i,k1))}
\DoxyCodeLine{2463         s(i,kb2) = 0.5*(s(i,kb1)+s(i,k1))}
\DoxyCodeLine{2464 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{2465 \textcolor{keywordflow}{    endif} \textcolor{comment}{! (h2 = 0 \&\& h1 > 0)}}
\DoxyCodeLine{2466 }
\DoxyCodeLine{2467     dpe\_extrap = 0.0 ; dpe\_merge = 0.0}
\DoxyCodeLine{2468     mergeable\_bl = .false.}
\DoxyCodeLine{2469     \textcolor{keywordflow}{if} ((h1 > 0.0) .and. (h2 > 0.0) .and. (h\_to\_bl > 0.0) .and. \&}
\DoxyCodeLine{2470         (stable\_rcv)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2471       \textcolor{comment}{! Check whether it is permissible for the buffer layers to detrain}}
\DoxyCodeLine{2472       \textcolor{comment}{! into the interior isopycnal layers.}}
\DoxyCodeLine{2473 }
\DoxyCodeLine{2474       \textcolor{comment}{! Determine the layer that has the lightest target density that is}}
\DoxyCodeLine{2475       \textcolor{comment}{! denser than the lowermost buffer layer.}}
\DoxyCodeLine{2476       \textcolor{keywordflow}{do} k1=kb2+1,nz ; \textcolor{keywordflow}{if} (rcvtgt(k1) >= rcv(i,kb2)) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo} ; k0 = k1-\/1}
\DoxyCodeLine{2477       dr1 = rcvtgt(k0)-\/rcv(i,kb1) ; dr2 = rcv(i,kb2)-\/rcvtgt(k0)}
\DoxyCodeLine{2478 }
\DoxyCodeLine{2479       \textcolor{comment}{! Use an energy-\/balanced combination of downwind advection into the next}}
\DoxyCodeLine{2480       \textcolor{comment}{! denser interior layer and upwind advection from the upper buffer layer}}
\DoxyCodeLine{2481       \textcolor{comment}{! into the lower one, each with an energy change that equals that required}}
\DoxyCodeLine{2482       \textcolor{comment}{! to mix the detrained water with the upper buffer layer.}}
\DoxyCodeLine{2483       h1\_avail = h1 -\/ max(0.0,h\_min\_bl-\/h\_to\_bl)}
\DoxyCodeLine{2484       \textcolor{keywordflow}{if} ((k1<=nz) .and. (h2 > h\_min\_bl) .and. (h1\_avail > 0.0) .and. \&}
\DoxyCodeLine{2485           (r0(i,kb1) < r0(i,kb2)) .and. (h\_to\_bl*r0(i,kb1) > r0\_to\_bl)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2486         drk1 = (rcvtgt(k1) -\/ rcv(i,kb2)) * (r0(i,kb2) -\/ r0(i,kb1)) / \&}
\DoxyCodeLine{2487                                            (rcv(i,kb2) -\/ rcv(i,kb1))}
\DoxyCodeLine{2488         b1 = drk1 / (r0(i,kb2) -\/ r0(i,kb1))}
\DoxyCodeLine{2489         \textcolor{comment}{! b1 = RcvTgt(k1) -\/ Rcv(i,kb2)) / (Rcv(i,kb2) -\/ Rcv(i,kb1))}}
\DoxyCodeLine{2490 }
\DoxyCodeLine{2491         \textcolor{comment}{! Apply several limits to the detrainment.}}
\DoxyCodeLine{2492         \textcolor{comment}{! Entrain less than the mass in h2, and keep the base of the buffer}}
\DoxyCodeLine{2493         \textcolor{comment}{! layers from becoming shallower than any neighbors.}}
\DoxyCodeLine{2494         h2\_to\_k1 = min(h2 -\/ h\_min\_bl, h2\_to\_k1\_rem)}
\DoxyCodeLine{2495         \textcolor{comment}{! Balance downwind advection of density into the layer below the}}
\DoxyCodeLine{2496         \textcolor{comment}{! buffer layers with upwind advection from the layer above.}}
\DoxyCodeLine{2497         \textcolor{keywordflow}{if} (h2\_to\_k1*(h1\_avail + b1*(h1\_avail + h2)) > h2*h1\_avail) \&}
\DoxyCodeLine{2498           h2\_to\_k1 = (h2*h1\_avail) / (h1\_avail + b1*(h1\_avail + h2))}
\DoxyCodeLine{2499         \textcolor{keywordflow}{if} (h2\_to\_k1*(drk1 * h2) > (h\_to\_bl*r0(i,kb1) -\/ r0\_to\_bl) * h1) \&}
\DoxyCodeLine{2500           h2\_to\_k1 = (h\_to\_bl*r0(i,kb1) -\/ r0\_to\_bl) * h1 / (drk1 * h2)}
\DoxyCodeLine{2501 }
\DoxyCodeLine{2502         \textcolor{keywordflow}{if} ((k1==kb2+1) .and. (cs\%BL\_extrap\_lim > 0.)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2503           \textcolor{comment}{! Simply do not detrain very light water into the lightest isopycnal}}
\DoxyCodeLine{2504           \textcolor{comment}{! coordinate layers if the density jump is too large.}}
\DoxyCodeLine{2505           drcv\_lim = rcv(i,kb2)-\/rcv(i,0)}
\DoxyCodeLine{2506           \textcolor{keywordflow}{do} k=1,kb2 ; drcv\_lim = max(drcv\_lim, rcv(i,kb2)-\/rcv(i,k)) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2507           drcv\_lim = cs\%BL\_extrap\_lim*drcv\_lim}
\DoxyCodeLine{2508           \textcolor{keywordflow}{if} ((rcvtgt(k1) -\/ rcv(i,kb2)) >= drcv\_lim) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2509             h2\_to\_k1 = 0.0}
\DoxyCodeLine{2510           \textcolor{keywordflow}{elseif} ((rcvtgt(k1) -\/ rcv(i,kb2)) > 0.5*drcv\_lim) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2511             h2\_to\_k1 = h2\_to\_k1 * (2.0 -\/ 2.0*((rcvtgt(k1) -\/ rcv(i,kb2)) / drcv\_lim))}
\DoxyCodeLine{2512 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{2513 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2514 }
\DoxyCodeLine{2515         drcv = (rcvtgt(k1) -\/ rcv(i,kb2))}
\DoxyCodeLine{2516 }
\DoxyCodeLine{2517         \textcolor{comment}{! Use 2nd order upwind advection of spiciness, limited by the values}}
\DoxyCodeLine{2518         \textcolor{comment}{! in deeper thick layers to determine the detrained temperature and}}
\DoxyCodeLine{2519         \textcolor{comment}{! salinity.}}
\DoxyCodeLine{2520         dspice\_det = (ds\_dt\_gauge*drcv\_ds(i)*(t(i,kb2)-\/t(i,kb1)) -\/ \&}
\DoxyCodeLine{2521                       dt\_ds\_gauge*drcv\_dt(i)*(s(i,kb2)-\/s(i,kb1))) * \&}
\DoxyCodeLine{2522                       (h2 -\/ h2\_to\_k1) / (h1 + h2)}
\DoxyCodeLine{2523         dspice\_lim = 0.0}
\DoxyCodeLine{2524         \textcolor{keywordflow}{if} (h(i,k1) > 10.0*angstrom) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2525           dspice\_lim = ds\_dt\_gauge*drcv\_ds(i)*(t(i,k1)-\/t(i,kb2)) -\/ \&}
\DoxyCodeLine{2526                        dt\_ds\_gauge*drcv\_dt(i)*(s(i,k1)-\/s(i,kb2))}
\DoxyCodeLine{2527           \textcolor{keywordflow}{if} (dspice\_det*dspice\_lim <= 0.0) dspice\_lim = 0.0}
\DoxyCodeLine{2528 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2529         \textcolor{keywordflow}{if} (k1<nz) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (h(i,k1+1) > 10.0*angstrom) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2530           dspice\_lim2 = ds\_dt\_gauge*drcv\_ds(i)*(t(i,k1+1)-\/t(i,kb2)) -\/ \&}
\DoxyCodeLine{2531                         dt\_ds\_gauge*drcv\_dt(i)*(s(i,k1+1)-\/s(i,kb2))}
\DoxyCodeLine{2532           \textcolor{keywordflow}{if} ((dspice\_det*dspice\_lim2 > 0.0) .and. \&}
\DoxyCodeLine{2533               (abs(dspice\_lim2) > abs(dspice\_lim))) dspice\_lim = dspice\_lim2}
\DoxyCodeLine{2534 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{2535         \textcolor{keywordflow}{if} (abs(dspice\_det) > abs(dspice\_lim)) dspice\_det = dspice\_lim}
\DoxyCodeLine{2536 }
\DoxyCodeLine{2537         i\_denom = 1.0 / (drcv\_ds(i)**2 + (dt\_ds\_gauge*drcv\_dt(i))**2)}
\DoxyCodeLine{2538         t\_det = t(i,kb2) + dt\_ds\_gauge * i\_denom * \&}
\DoxyCodeLine{2539             (dt\_ds\_gauge * drcv\_dt(i) * drcv + drcv\_ds(i) * dspice\_det)}
\DoxyCodeLine{2540         s\_det = s(i,kb2) + i\_denom * \&}
\DoxyCodeLine{2541             (drcv\_ds(i) * drcv -\/ dt\_ds\_gauge * drcv\_dt(i) * dspice\_det)}
\DoxyCodeLine{2542         \textcolor{comment}{! The detrained values of R0 are based on changes in T and S.}}
\DoxyCodeLine{2543         r0\_det = r0(i,kb2) + (t\_det-\/t(i,kb2)) * dr0\_dt(i) + \&}
\DoxyCodeLine{2544                              (s\_det-\/s(i,kb2)) * dr0\_ds(i)}
\DoxyCodeLine{2545 }
\DoxyCodeLine{2546         \textcolor{keywordflow}{if} (cs\%BL\_extrap\_lim >= 0.) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2547           \textcolor{comment}{! Only do this detrainment if the new layer's temperature and salinity}}
\DoxyCodeLine{2548           \textcolor{comment}{! are not too far outside of the range of previous values.}}
\DoxyCodeLine{2549           \textcolor{keywordflow}{if} (h(i,k1) > 10.0*angstrom) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2550             t\_min = min(t(i,kb1), t(i,kb2), t(i,k1)) -\/ cs\%Allowed\_T\_chg}
\DoxyCodeLine{2551             t\_max = max(t(i,kb1), t(i,kb2), t(i,k1)) + cs\%Allowed\_T\_chg}
\DoxyCodeLine{2552             s\_min = min(s(i,kb1), s(i,kb2), s(i,k1)) -\/ cs\%Allowed\_S\_chg}
\DoxyCodeLine{2553             s\_max = max(s(i,kb1), s(i,kb2), s(i,k1)) + cs\%Allowed\_S\_chg}
\DoxyCodeLine{2554           \textcolor{keywordflow}{else}}
\DoxyCodeLine{2555             t\_min = min(t(i,kb1), t(i,kb2)) -\/ cs\%Allowed\_T\_chg}
\DoxyCodeLine{2556             t\_max = max(t(i,kb1), t(i,kb2)) + cs\%Allowed\_T\_chg}
\DoxyCodeLine{2557             s\_min = min(s(i,kb1), s(i,kb2)) -\/ cs\%Allowed\_S\_chg}
\DoxyCodeLine{2558             s\_max = max(s(i,kb1), s(i,kb2)) + cs\%Allowed\_S\_chg}
\DoxyCodeLine{2559 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{2560           ihk1 = 1.0 / (h(i,k1) + h2\_to\_k1)}
\DoxyCodeLine{2561           t\_new = (h(i,k1)*t(i,k1) + h2\_to\_k1*t\_det) * ihk1}
\DoxyCodeLine{2562           s\_new = (h(i,k1)*s(i,k1) + h2\_to\_k1*s\_det) * ihk1}
\DoxyCodeLine{2563           \textcolor{comment}{! A less restrictive limit might be used here.}}
\DoxyCodeLine{2564           \textcolor{keywordflow}{if} ((t\_new < t\_min) .or. (t\_new > t\_max) .or. \&}
\DoxyCodeLine{2565               (s\_new < s\_min) .or. (s\_new > s\_max)) \&}
\DoxyCodeLine{2566             h2\_to\_k1 = 0.0}
\DoxyCodeLine{2567 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2568 }
\DoxyCodeLine{2569         h1\_to\_h2 = b1*h2*h2\_to\_k1 / (h2 -\/ (1.0+b1)*h2\_to\_k1)}
\DoxyCodeLine{2570 }
\DoxyCodeLine{2571         ihk1 = 1.0 / (h(i,k1) + h\_neglect + h2\_to\_k1)}
\DoxyCodeLine{2572         ih2f = 1.0 / ((h(i,kb2) -\/ h2\_to\_k1) + h1\_to\_h2)}
\DoxyCodeLine{2573 }
\DoxyCodeLine{2574         rcv(i,kb2) = ((h(i,kb2)*rcv(i,kb2) -\/ h2\_to\_k1*rcvtgt(k1)) + \&}
\DoxyCodeLine{2575                       h1\_to\_h2*rcv(i,kb1))*ih2f}
\DoxyCodeLine{2576         rcv(i,k1) = ((h(i,k1)+h\_neglect)*rcv(i,k1) + h2\_to\_k1*rcvtgt(k1)) * ihk1}
\DoxyCodeLine{2577 }
\DoxyCodeLine{2578         t(i,kb2) = ((h(i,kb2)*t(i,kb2) -\/ h2\_to\_k1*t\_det) + \&}
\DoxyCodeLine{2579                     h1\_to\_h2*t(i,kb1)) * ih2f}
\DoxyCodeLine{2580         t(i,k1) = ((h(i,k1)+h\_neglect)*t(i,k1) + h2\_to\_k1*t\_det) * ihk1}
\DoxyCodeLine{2581 }
\DoxyCodeLine{2582         s(i,kb2) = ((h(i,kb2)*s(i,kb2) -\/ h2\_to\_k1*s\_det) + \&}
\DoxyCodeLine{2583                     h1\_to\_h2*s(i,kb1)) * ih2f}
\DoxyCodeLine{2584         s(i,k1) = ((h(i,k1)+h\_neglect)*s(i,k1) + h2\_to\_k1*s\_det) * ihk1}
\DoxyCodeLine{2585 }
\DoxyCodeLine{2586         \textcolor{comment}{! Changes in R0 are based on changes in T and S.}}
\DoxyCodeLine{2587         r0(i,kb2) = ((h(i,kb2)*r0(i,kb2) -\/ h2\_to\_k1*r0\_det) + \&}
\DoxyCodeLine{2588                      h1\_to\_h2*r0(i,kb1)) * ih2f}
\DoxyCodeLine{2589         r0(i,k1) = ((h(i,k1)+h\_neglect)*r0(i,k1) + h2\_to\_k1*r0\_det) * ihk1}
\DoxyCodeLine{2590 }
\DoxyCodeLine{2591         h(i,kb1) = h(i,kb1) -\/ h1\_to\_h2 ; h1 = h(i,kb1)}
\DoxyCodeLine{2592         h(i,kb2) = (h(i,kb2) -\/ h2\_to\_k1) + h1\_to\_h2 ; h2 = h(i,kb2)}
\DoxyCodeLine{2593         h(i,k1) = h(i,k1) + h2\_to\_k1}
\DoxyCodeLine{2594 }
\DoxyCodeLine{2595         d\_ea(i,kb1) = d\_ea(i,kb1) -\/ h1\_to\_h2}
\DoxyCodeLine{2596         d\_ea(i,kb2) = (d\_ea(i,kb2) -\/ h2\_to\_k1) + h1\_to\_h2}
\DoxyCodeLine{2597         d\_ea(i,k1) = d\_ea(i,k1) + h2\_to\_k1}
\DoxyCodeLine{2598         h2\_to\_k1\_rem = max(h2\_to\_k1\_rem -\/ h2\_to\_k1, 0.0)}
\DoxyCodeLine{2599 }
\DoxyCodeLine{2600         \textcolor{comment}{!   The lower buffer layer has become lighter -\/ it may be necessary to}}
\DoxyCodeLine{2601         \textcolor{comment}{! adjust k1 lighter.}}
\DoxyCodeLine{2602         \textcolor{keywordflow}{if} ((k1>kb2+1) .and. (rcvtgt(k1-\/1) >= rcv(i,kb2))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2603           \textcolor{keywordflow}{do} k1=k1,kb2+1,-\/1 ; \textcolor{keywordflow}{if} (rcvtgt(k1-\/1) < rcv(i,kb2)) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2604 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2605 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2606 }
\DoxyCodeLine{2607       k0 = k1-\/1}
\DoxyCodeLine{2608       dr1 = rcvtgt(k0)-\/rcv(i,kb1) ; dr2 = rcv(i,kb2)-\/rcvtgt(k0)}
\DoxyCodeLine{2609 }
\DoxyCodeLine{2610       \textcolor{keywordflow}{if} ((k0>kb2) .and. (dr1 > 0.0) .and. (h1 > h\_min\_bl) .and. \&}
\DoxyCodeLine{2611           (h2*dr2 < h1*dr1) .and. (r0(i,kb2) > r0(i,kb1))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2612         \textcolor{comment}{! An interior isopycnal layer (k0) is intermediate in density between}}
\DoxyCodeLine{2613         \textcolor{comment}{! the two buffer layers, and there can be detrainment. The entire}}
\DoxyCodeLine{2614         \textcolor{comment}{! lower buffer layer is combined with a portion of the upper buffer}}
\DoxyCodeLine{2615         \textcolor{comment}{! layer to match the target density of layer k0.}}
\DoxyCodeLine{2616         stays\_merge = 2.0*(h1+h2)*(h1*dr1 -\/ h2*dr2) / \&}
\DoxyCodeLine{2617                      ((dr1+dr2)*h1 + dr1*(h1+h2) + \&}
\DoxyCodeLine{2618                       sqrt((dr2*h1-\/dr1*h2)**2 + 4*(h1+h2)*h2*(dr1+dr2)*dr2))}
\DoxyCodeLine{2619 }
\DoxyCodeLine{2620         stays\_min\_merge = max(h\_min\_bl, 2.0*h\_min\_bl -\/ h\_to\_bl, \&}
\DoxyCodeLine{2621                   h1 -\/ (h1+h2)*(r0(i,kb1) -\/ r0\_det) / (r0(i,kb2) -\/ r0(i,kb1)))}
\DoxyCodeLine{2622         \textcolor{keywordflow}{if} ((stays\_merge > stays\_min\_merge) .and. \&}
\DoxyCodeLine{2623             (stays\_merge + h2\_to\_k1\_rem >= h1 + h2)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2624           mergeable\_bl = .true.}
\DoxyCodeLine{2625           dpe\_merge = g\_2*(r0(i,kb2)-\/r0(i,kb1))*(h1-\/stays\_merge)*(h2-\/stays\_merge)}
\DoxyCodeLine{2626 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2627 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2628 }
\DoxyCodeLine{2629       \textcolor{keywordflow}{if} ((k1<=nz).and.(.not.mergeable\_bl)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2630         \textcolor{comment}{! Check whether linear extrapolation of density (i.e. 2nd order upwind}}
\DoxyCodeLine{2631         \textcolor{comment}{! advection) will allow some of the lower buffer layer to detrain into}}
\DoxyCodeLine{2632         \textcolor{comment}{! the next denser interior layer (k1).}}
\DoxyCodeLine{2633         dr2b = rcvtgt(k1)-\/rcv(i,kb2) ; dr21 = rcv(i,kb2) -\/ rcv(i,kb1)}
\DoxyCodeLine{2634         \textcolor{keywordflow}{if} (dr2b*(h1+h2) < h2*dr21) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2635           \textcolor{comment}{! Some of layer kb2 is denser than k1.}}
\DoxyCodeLine{2636           h2\_to\_k1 = min(h2 -\/ (h1+h2) * dr2b / dr21, h2\_to\_k1\_rem)}
\DoxyCodeLine{2637 }
\DoxyCodeLine{2638           \textcolor{keywordflow}{if} (h2 > h2\_to\_k1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2639             drcv = (rcvtgt(k1) -\/ rcv(i,kb2))}
\DoxyCodeLine{2640 }
\DoxyCodeLine{2641             \textcolor{comment}{! Use 2nd order upwind advection of spiciness, limited by the values}}
\DoxyCodeLine{2642             \textcolor{comment}{! in deeper thick layers to determine the detrained temperature and}}
\DoxyCodeLine{2643             \textcolor{comment}{! salinity.}}
\DoxyCodeLine{2644             dspice\_det = (ds\_dt\_gauge*drcv\_ds(i)*(t(i,kb2)-\/t(i,kb1)) -\/ \&}
\DoxyCodeLine{2645                           dt\_ds\_gauge*drcv\_dt(i)*(s(i,kb2)-\/s(i,kb1))) * \&}
\DoxyCodeLine{2646                           (h2 -\/ h2\_to\_k1) / (h1 + h2)}
\DoxyCodeLine{2647             dspice\_lim = 0.0}
\DoxyCodeLine{2648             \textcolor{keywordflow}{if} (h(i,k1) > 10.0*angstrom) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2649               dspice\_lim = ds\_dt\_gauge*drcv\_ds(i)*(t(i,k1)-\/t(i,kb2)) -\/ \&}
\DoxyCodeLine{2650                            dt\_ds\_gauge*drcv\_dt(i)*(s(i,k1)-\/s(i,kb2))}
\DoxyCodeLine{2651               \textcolor{keywordflow}{if} (dspice\_det*dspice\_lim <= 0.0) dspice\_lim = 0.0}
\DoxyCodeLine{2652 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{2653             \textcolor{keywordflow}{if} (k1<nz) then; \textcolor{keywordflow}{if} (h(i,k1+1) > 10.0*angstrom) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2654               dspice\_lim2 = ds\_dt\_gauge*drcv\_ds(i)*(t(i,k1+1)-\/t(i,kb2)) -\/ \&}
\DoxyCodeLine{2655                             dt\_ds\_gauge*drcv\_dt(i)*(s(i,k1+1)-\/s(i,kb2))}
\DoxyCodeLine{2656               \textcolor{keywordflow}{if} ((dspice\_det*dspice\_lim2 > 0.0) .and. \&}
\DoxyCodeLine{2657                   (abs(dspice\_lim2) > abs(dspice\_lim))) dspice\_lim = dspice\_lim2}
\DoxyCodeLine{2658             endif;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{2659             \textcolor{keywordflow}{if} (abs(dspice\_det) > abs(dspice\_lim)) dspice\_det = dspice\_lim}
\DoxyCodeLine{2660 }
\DoxyCodeLine{2661             i\_denom = 1.0 / (drcv\_ds(i)**2 + (dt\_ds\_gauge*drcv\_dt(i))**2)}
\DoxyCodeLine{2662             t\_det = t(i,kb2) + dt\_ds\_gauge * i\_denom * \&}
\DoxyCodeLine{2663                 (dt\_ds\_gauge * drcv\_dt(i) * drcv + drcv\_ds(i) * dspice\_det)}
\DoxyCodeLine{2664             s\_det = s(i,kb2) + i\_denom * \&}
\DoxyCodeLine{2665                 (drcv\_ds(i) * drcv -\/ dt\_ds\_gauge * drcv\_dt(i) * dspice\_det)}
\DoxyCodeLine{2666             \textcolor{comment}{! The detrained values of R0 are based on changes in T and S.}}
\DoxyCodeLine{2667             r0\_det = r0(i,kb2) + (t\_det-\/t(i,kb2)) * dr0\_dt(i) + \&}
\DoxyCodeLine{2668                                  (s\_det-\/s(i,kb2)) * dr0\_ds(i)}
\DoxyCodeLine{2669 }
\DoxyCodeLine{2670             \textcolor{comment}{! Now that the properties of the detrained water are known,}}
\DoxyCodeLine{2671             \textcolor{comment}{! potentially limit the amount of water that is detrained to}}
\DoxyCodeLine{2672             \textcolor{comment}{! avoid creating unphysical properties in the remaining water.}}
\DoxyCodeLine{2673             ih2f = 1.0 / (h2 -\/ h2\_to\_k1)}
\DoxyCodeLine{2674 }
\DoxyCodeLine{2675             t\_min = min(t(i,kb2), t(i,kb1)) -\/ cs\%Allowed\_T\_chg}
\DoxyCodeLine{2676             t\_max = max(t(i,kb2), t(i,kb1)) + cs\%Allowed\_T\_chg}
\DoxyCodeLine{2677             t\_new = (h2*t(i,kb2) -\/ h2\_to\_k1*t\_det)*ih2f}
\DoxyCodeLine{2678             \textcolor{keywordflow}{if} (t\_new < t\_min) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2679               h2\_to\_k1\_lim = h2 * (t(i,kb2) -\/ t\_min) / (t\_det -\/ t\_min)}
\DoxyCodeLine{2680 \textcolor{comment}{!               write(mesg,'("Low temperature limits det to ", \&}}
\DoxyCodeLine{2681 \textcolor{comment}{!                    \& 1pe12.5, " from ", 1pe12.5, " at ", 1pg11.4,"E, ",1pg11.4,"N. T=", \&}}
\DoxyCodeLine{2682 \textcolor{comment}{!                    \& 5(1pe12.5))') \&}}
\DoxyCodeLine{2683 \textcolor{comment}{!                    h2\_to\_k1\_lim, h2\_to\_k1, G\%geoLonT(i,j), G\%geoLatT(i,j), \&}}
\DoxyCodeLine{2684 \textcolor{comment}{!                    T\_new, T(i,kb2), T(i,kb1), T\_det, T\_new-\/T\_min}}
\DoxyCodeLine{2685 \textcolor{comment}{!               call MOM\_error(WARNING, mesg)}}
\DoxyCodeLine{2686               h2\_to\_k1 = h2\_to\_k1\_lim}
\DoxyCodeLine{2687               ih2f = 1.0 / (h2 -\/ h2\_to\_k1)}
\DoxyCodeLine{2688             \textcolor{keywordflow}{elseif} (t\_new > t\_max) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2689               h2\_to\_k1\_lim = h2 * (t(i,kb2) -\/ t\_max) / (t\_det -\/ t\_max)}
\DoxyCodeLine{2690 \textcolor{comment}{!               write(mesg,'("High temperature limits det to ", \&}}
\DoxyCodeLine{2691 \textcolor{comment}{!                    \& 1pe12.5, " from ", 1pe12.5, " at ", 1pg11.4,"E, ",1pg11.4,"N. T=", \&}}
\DoxyCodeLine{2692 \textcolor{comment}{!                    \& 5(1pe12.5))') \&}}
\DoxyCodeLine{2693 \textcolor{comment}{!                    h2\_to\_k1\_lim, h2\_to\_k1, G\%geoLonT(i,j), G\%geoLatT(i,j), \&}}
\DoxyCodeLine{2694 \textcolor{comment}{!                    T\_new, T(i,kb2), T(i,kb1), T\_det, T\_new-\/T\_max}}
\DoxyCodeLine{2695 \textcolor{comment}{!               call MOM\_error(WARNING, mesg)}}
\DoxyCodeLine{2696               h2\_to\_k1 = h2\_to\_k1\_lim}
\DoxyCodeLine{2697               ih2f = 1.0 / (h2 -\/ h2\_to\_k1)}
\DoxyCodeLine{2698 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{2699             s\_min = max(min(s(i,kb2), s(i,kb1)) -\/ cs\%Allowed\_S\_chg, 0.0)}
\DoxyCodeLine{2700             s\_max = max(s(i,kb2), s(i,kb1)) + cs\%Allowed\_S\_chg}
\DoxyCodeLine{2701             s\_new = (h2*s(i,kb2) -\/ h2\_to\_k1*s\_det)*ih2f}
\DoxyCodeLine{2702             \textcolor{keywordflow}{if} (s\_new < s\_min) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2703               h2\_to\_k1\_lim = h2 * (s(i,kb2) -\/ s\_min) / (s\_det -\/ s\_min)}
\DoxyCodeLine{2704 \textcolor{comment}{!               write(mesg,'("Low salinity limits det to ", \&}}
\DoxyCodeLine{2705 \textcolor{comment}{!                    \& 1pe12.5, " from ", 1pe12.5, " at ", 1pg11.4,"E, ",1pg11.4,"N. S=", \&}}
\DoxyCodeLine{2706 \textcolor{comment}{!                    \& 5(1pe12.5))') \&}}
\DoxyCodeLine{2707 \textcolor{comment}{!                    h2\_to\_k1\_lim, h2\_to\_k1, G\%geoLonT(i,j), G\%geoLatT(i,j), \&}}
\DoxyCodeLine{2708 \textcolor{comment}{!                    S\_new, S(i,kb2), S(i,kb1), S\_det, S\_new-\/S\_min}}
\DoxyCodeLine{2709 \textcolor{comment}{!               call MOM\_error(WARNING, mesg)}}
\DoxyCodeLine{2710               h2\_to\_k1 = h2\_to\_k1\_lim}
\DoxyCodeLine{2711               ih2f = 1.0 / (h2 -\/ h2\_to\_k1)}
\DoxyCodeLine{2712             \textcolor{keywordflow}{elseif} (s\_new > s\_max) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2713               h2\_to\_k1\_lim = h2 * (s(i,kb2) -\/ s\_max) / (s\_det -\/ s\_max)}
\DoxyCodeLine{2714 \textcolor{comment}{!               write(mesg,'("High salinity limits det to ", \&}}
\DoxyCodeLine{2715 \textcolor{comment}{!                    \& 1pe12.5, " from ", 1pe12.5, " at ", 1pg11.4,"E, ",1pg11.4,"N. S=", \&}}
\DoxyCodeLine{2716 \textcolor{comment}{!                    \& 5(1pe12.5))') \&}}
\DoxyCodeLine{2717 \textcolor{comment}{!                    h2\_to\_k1\_lim, h2\_to\_k1, G\%geoLonT(i,j), G\%geoLatT(i,j), \&}}
\DoxyCodeLine{2718 \textcolor{comment}{!                    S\_new, S(i,kb2), S(i,kb1), S\_det, S\_new-\/S\_max}}
\DoxyCodeLine{2719 \textcolor{comment}{!               call MOM\_error(WARNING, mesg)}}
\DoxyCodeLine{2720               h2\_to\_k1 = h2\_to\_k1\_lim}
\DoxyCodeLine{2721               ih2f = 1.0 / (h2 -\/ h2\_to\_k1)}
\DoxyCodeLine{2722 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{2723 }
\DoxyCodeLine{2724             ihk1 = 1.0 / (h(i,k1) + h\_neglect + h2\_to\_k1)}
\DoxyCodeLine{2725             rcv(i,k1) = ((h(i,k1)+h\_neglect)*rcv(i,k1) + h2\_to\_k1*rcvtgt(k1)) * ihk1}
\DoxyCodeLine{2726             rcv(i,kb2) = rcv(i,kb2) -\/ h2\_to\_k1*drcv*ih2f}
\DoxyCodeLine{2727 }
\DoxyCodeLine{2728             t(i,kb2) = (h2*t(i,kb2) -\/ h2\_to\_k1*t\_det)*ih2f}
\DoxyCodeLine{2729             t(i,k1) = ((h(i,k1)+h\_neglect)*t(i,k1) + h2\_to\_k1*t\_det) * ihk1}
\DoxyCodeLine{2730 }
\DoxyCodeLine{2731             s(i,kb2) = (h2*s(i,kb2) -\/ h2\_to\_k1*s\_det) * ih2f}
\DoxyCodeLine{2732             s(i,k1) = ((h(i,k1)+h\_neglect)*s(i,k1) + h2\_to\_k1*s\_det) * ihk1}
\DoxyCodeLine{2733 }
\DoxyCodeLine{2734             \textcolor{comment}{! Changes in R0 are based on changes in T and S.}}
\DoxyCodeLine{2735             r0(i,kb2) = (h2*r0(i,kb2) -\/ h2\_to\_k1*r0\_det) * ih2f}
\DoxyCodeLine{2736             r0(i,k1) = ((h(i,k1)+h\_neglect)*r0(i,k1) + h2\_to\_k1*r0\_det) * ihk1}
\DoxyCodeLine{2737           \textcolor{keywordflow}{else}}
\DoxyCodeLine{2738             \textcolor{comment}{! h2==h2\_to\_k1 can happen if dR2b = 0 exactly, but this is very}}
\DoxyCodeLine{2739             \textcolor{comment}{! unlikely.  In this case the entirety of layer kb2 is detrained.}}
\DoxyCodeLine{2740             h2\_to\_k1 = h2  \textcolor{comment}{! These 2 lines are probably unnecessary.}}
\DoxyCodeLine{2741             ihk1 = 1.0 / (h(i,k1) + h2)}
\DoxyCodeLine{2742 }
\DoxyCodeLine{2743             rcv(i,k1) = (h(i,k1)*rcv(i,k1) + h2*rcv(i,kb2)) * ihk1}
\DoxyCodeLine{2744             t(i,k1) = (h(i,k1)*t(i,k1) + h2*t(i,kb2)) * ihk1}
\DoxyCodeLine{2745             s(i,k1) = (h(i,k1)*s(i,k1) + h2*s(i,kb2)) * ihk1}
\DoxyCodeLine{2746             r0(i,k1) = (h(i,k1)*r0(i,k1) + h2*r0(i,kb2)) * ihk1}
\DoxyCodeLine{2747 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{2748 }
\DoxyCodeLine{2749           h(i,k1) = h(i,k1) + h2\_to\_k1}
\DoxyCodeLine{2750           h(i,kb2) = h(i,kb2) -\/ h2\_to\_k1 ; h2 = h(i,kb2)}
\DoxyCodeLine{2751           \textcolor{comment}{! dPE\_extrap should be positive here.}}
\DoxyCodeLine{2752           dpe\_extrap = i2rho0*(r0\_det-\/r0(i,kb2))*h2\_to\_k1*h2}
\DoxyCodeLine{2753 }
\DoxyCodeLine{2754           d\_ea(i,kb2) = d\_ea(i,kb2) -\/ h2\_to\_k1}
\DoxyCodeLine{2755           d\_ea(i,k1) = d\_ea(i,k1) + h2\_to\_k1}
\DoxyCodeLine{2756           h2\_to\_k1\_rem = max(h2\_to\_k1\_rem -\/ h2\_to\_k1, 0.0)}
\DoxyCodeLine{2757 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2758 \textcolor{keywordflow}{      endif} \textcolor{comment}{! Detrainment by extrapolation.}}
\DoxyCodeLine{2759 }
\DoxyCodeLine{2760 \textcolor{keywordflow}{    endif} \textcolor{comment}{! Detrainment to the interior at all.}}
\DoxyCodeLine{2761 }
\DoxyCodeLine{2762     \textcolor{comment}{! Does some of the detrained water go into the lower buffer layer?}}
\DoxyCodeLine{2763     h\_det\_h2 = max(h\_min\_bl-\/(h1+h2), 0.0)}
\DoxyCodeLine{2764     \textcolor{keywordflow}{if} (h\_det\_h2 > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2765       \textcolor{comment}{! Detrained water will go into both upper and lower buffer layers.}}
\DoxyCodeLine{2766       \textcolor{comment}{! h(kb2) will be h\_min\_bl, but h(kb1) may be larger if there was already}}
\DoxyCodeLine{2767       \textcolor{comment}{! ample detrainment; all water in layer kb1 moves into layer kb2.}}
\DoxyCodeLine{2768 }
\DoxyCodeLine{2769       \textcolor{comment}{! Determine the fluxes between the various layers.}}
\DoxyCodeLine{2770       h\_det\_to\_h2 = min(h\_to\_bl, h\_det\_h2)}
\DoxyCodeLine{2771       h\_ml\_to\_h2 = h\_det\_h2 -\/ h\_det\_to\_h2}
\DoxyCodeLine{2772       h\_det\_to\_h1 = h\_to\_bl -\/ h\_det\_to\_h2}
\DoxyCodeLine{2773       h\_ml\_to\_h1 = max(h\_min\_bl-\/h\_det\_to\_h1,0.0)}
\DoxyCodeLine{2774 }
\DoxyCodeLine{2775       ih = 1.0/h\_min\_bl}
\DoxyCodeLine{2776       ihdet = 0.0 ; \textcolor{keywordflow}{if} (h\_to\_bl > 0.0) ihdet = 1.0 / h\_to\_bl}
\DoxyCodeLine{2777       ih1f = 1.0 / (h\_det\_to\_h1 + h\_ml\_to\_h1)}
\DoxyCodeLine{2778 }
\DoxyCodeLine{2779       r0(i,kb2) = ((h2*r0(i,kb2) + h1*r0(i,kb1)) + \&}
\DoxyCodeLine{2780                    (h\_det\_to\_h2*r0\_to\_bl*ihdet + h\_ml\_to\_h2*r0(i,0))) * ih}
\DoxyCodeLine{2781       r0(i,kb1) = (h\_det\_to\_h1*r0\_to\_bl*ihdet + h\_ml\_to\_h1*r0(i,0)) * ih1f}
\DoxyCodeLine{2782 }
\DoxyCodeLine{2783       rcv(i,kb2) = ((h2*rcv(i,kb2) + h1*rcv(i,kb1)) + \&}
\DoxyCodeLine{2784                     (h\_det\_to\_h2*rcv\_to\_bl*ihdet + h\_ml\_to\_h2*rcv(i,0))) * ih}
\DoxyCodeLine{2785       rcv(i,kb1) = (h\_det\_to\_h1*rcv\_to\_bl*ihdet + h\_ml\_to\_h1*rcv(i,0)) * ih1f}
\DoxyCodeLine{2786 }
\DoxyCodeLine{2787       t(i,kb2) = ((h2*t(i,kb2) + h1*t(i,kb1)) + \&}
\DoxyCodeLine{2788                   (h\_det\_to\_h2*t\_to\_bl*ihdet + h\_ml\_to\_h2*t(i,0))) * ih}
\DoxyCodeLine{2789       t(i,kb1) = (h\_det\_to\_h1*t\_to\_bl*ihdet + h\_ml\_to\_h1*t(i,0)) * ih1f}
\DoxyCodeLine{2790 }
\DoxyCodeLine{2791       s(i,kb2) = ((h2*s(i,kb2) + h1*s(i,kb1)) + \&}
\DoxyCodeLine{2792                   (h\_det\_to\_h2*s\_to\_bl*ihdet + h\_ml\_to\_h2*s(i,0))) * ih}
\DoxyCodeLine{2793       s(i,kb1) = (h\_det\_to\_h1*s\_to\_bl*ihdet + h\_ml\_to\_h1*s(i,0)) * ih1f}
\DoxyCodeLine{2794 }
\DoxyCodeLine{2795       \textcolor{comment}{! Recall that h1 = h(i,kb1) \& h2 = h(i,kb2).}}
\DoxyCodeLine{2796       d\_ea(i,1) = d\_ea(i,1) -\/ (h\_ml\_to\_h1 + h\_ml\_to\_h2)}
\DoxyCodeLine{2797       d\_ea(i,kb1) = d\_ea(i,kb1) + ((h\_det\_to\_h1 + h\_ml\_to\_h1) -\/ h1)}
\DoxyCodeLine{2798       d\_ea(i,kb2) = d\_ea(i,kb2) + (h\_min\_bl -\/ h2)}
\DoxyCodeLine{2799 }
\DoxyCodeLine{2800       h(i,kb1) = h\_det\_to\_h1 + h\_ml\_to\_h1 ; h(i,kb2) = h\_min\_bl}
\DoxyCodeLine{2801       h(i,0) = h(i,0) -\/ (h\_ml\_to\_h1 + h\_ml\_to\_h2)}
\DoxyCodeLine{2802 }
\DoxyCodeLine{2803 }
\DoxyCodeLine{2804       \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain) .or. \textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain2)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2805         r0\_det = r0\_to\_bl*ihdet}
\DoxyCodeLine{2806         s1en = g\_2 * idt\_h2 * ( ((r0(i,kb2)-\/r0(i,kb1))*h1*h2 + \&}
\DoxyCodeLine{2807             h\_det\_to\_h2*( (r0(i,kb1)-\/r0\_det)*h1 + (r0(i,kb2)-\/r0\_det)*h2 ) + \&}
\DoxyCodeLine{2808             h\_ml\_to\_h2*( (r0(i,kb2)-\/r0(i,0))*h2 + (r0(i,kb1)-\/r0(i,0))*h1 + \&}
\DoxyCodeLine{2809                          (r0\_det-\/r0(i,0))*h\_det\_to\_h2 ) + \&}
\DoxyCodeLine{2810             h\_det\_to\_h1*h\_ml\_to\_h1*(r0\_det-\/r0(i,0))) -\/ 2.0*gv\%Rho0*dpe\_extrap )}
\DoxyCodeLine{2811 }
\DoxyCodeLine{2812         \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain)) \&}
\DoxyCodeLine{2813           cs\%diag\_PE\_detrain(i,j) = cs\%diag\_PE\_detrain(i,j) + s1en}
\DoxyCodeLine{2814 }
\DoxyCodeLine{2815         \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain2)) cs\%diag\_PE\_detrain2(i,j) = \&}
\DoxyCodeLine{2816             cs\%diag\_PE\_detrain2(i,j) + s1en + idt\_h2*rho0xg*dpe\_extrap}
\DoxyCodeLine{2817 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2818 }
\DoxyCodeLine{2819     \textcolor{keywordflow}{elseif} ((h\_to\_bl > 0.0) .or. (h1 < h\_min\_bl) .or. (h2 < h\_min\_bl)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2820     \textcolor{comment}{! Determine how much of the upper buffer layer will be moved into}}
\DoxyCodeLine{2821     \textcolor{comment}{! the lower buffer layer and the properties with which it is moving.}}
\DoxyCodeLine{2822     \textcolor{comment}{! This implementation assumes a 2nd-\/order upwind advection of density}}
\DoxyCodeLine{2823     \textcolor{comment}{! from the uppermost buffer layer into the next one down.}}
\DoxyCodeLine{2824       h\_from\_ml = h\_min\_bl + max(h\_min\_bl-\/h2,0.0) -\/ h1 -\/ h\_to\_bl}
\DoxyCodeLine{2825       \textcolor{keywordflow}{if} (h\_from\_ml > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2826         \textcolor{comment}{! Some water needs to be moved from the mixed layer so that the upper}}
\DoxyCodeLine{2827         \textcolor{comment}{! (and perhaps lower) buffer layers exceed their minimum thicknesses.}}
\DoxyCodeLine{2828         dpe\_extrap = dpe\_extrap -\/ i2rho0*h\_from\_ml*(r0\_to\_bl -\/ r0(i,0)*h\_to\_bl)}
\DoxyCodeLine{2829         r0\_to\_bl = r0\_to\_bl + h\_from\_ml*r0(i,0)}
\DoxyCodeLine{2830         rcv\_to\_bl = rcv\_to\_bl + h\_from\_ml*rcv(i,0)}
\DoxyCodeLine{2831         t\_to\_bl = t\_to\_bl + h\_from\_ml*t(i,0)}
\DoxyCodeLine{2832         s\_to\_bl = s\_to\_bl + h\_from\_ml*s(i,0)}
\DoxyCodeLine{2833 }
\DoxyCodeLine{2834         h\_to\_bl = h\_to\_bl + h\_from\_ml}
\DoxyCodeLine{2835         h(i,0) = h(i,0) -\/ h\_from\_ml}
\DoxyCodeLine{2836         d\_ea(i,1) = d\_ea(i,1) -\/ h\_from\_ml}
\DoxyCodeLine{2837 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2838 }
\DoxyCodeLine{2839       \textcolor{comment}{! The absolute value should be unnecessary and 1e9 is just a large number.}}
\DoxyCodeLine{2840       b1 = 1.0e9}
\DoxyCodeLine{2841       \textcolor{keywordflow}{if} (r0(i,kb2) -\/ r0(i,kb1) > 1.0e-\/9*abs(r0(i,kb1) -\/ r0\_det)) \&}
\DoxyCodeLine{2842         b1 = abs(r0(i,kb1) -\/ r0\_det) / (r0(i,kb2) -\/ r0(i,kb1))}
\DoxyCodeLine{2843       stays\_min = max((1.0-\/b1)*h1 -\/ b1*h2, 0.0, h\_min\_bl -\/ h\_to\_bl)}
\DoxyCodeLine{2844       stays\_max = h1 -\/ max(h\_min\_bl-\/h2,0.0)}
\DoxyCodeLine{2845 }
\DoxyCodeLine{2846       scale\_slope = 1.0}
\DoxyCodeLine{2847       \textcolor{keywordflow}{if} (stays\_max <= stays\_min) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2848         stays = stays\_max}
\DoxyCodeLine{2849         mergeable\_bl = .false.}
\DoxyCodeLine{2850         \textcolor{keywordflow}{if} (stays\_max < h1) scale\_slope = (h1 -\/ stays\_min) / (h1 -\/ stays\_max)}
\DoxyCodeLine{2851       \textcolor{keywordflow}{else}}
\DoxyCodeLine{2852         \textcolor{comment}{! There are numerous temporary variables used here that should not be}}
\DoxyCodeLine{2853         \textcolor{comment}{! used outside of this "else" branch: s1, s2, s3sq, I\_ya, bh0}}
\DoxyCodeLine{2854         bh0 = b1*h\_to\_bl}
\DoxyCodeLine{2855         i\_ya =  (h1 + h2) / ((h1 + h2) + h\_to\_bl)}
\DoxyCodeLine{2856         \textcolor{comment}{! s1 is the amount staying that minimizes the PE increase.}}
\DoxyCodeLine{2857         s1 = 0.5*(h1 + (h2 -\/ bh0) * i\_ya) ; s2 = h1 -\/ s1}
\DoxyCodeLine{2858 }
\DoxyCodeLine{2859         \textcolor{keywordflow}{if} (s2 < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2860           \textcolor{comment}{! The energy released by detrainment from the lower buffer layer can be}}
\DoxyCodeLine{2861           \textcolor{comment}{! used to mix water from the upper buffer layer into the lower one.}}
\DoxyCodeLine{2862           s3sq = i\_ya*max(bh0*h1-\/dpe\_extrap, 0.0)}
\DoxyCodeLine{2863         \textcolor{keywordflow}{else}}
\DoxyCodeLine{2864           s3sq = i\_ya*(bh0*h1-\/min(dpe\_extrap,0.0))}
\DoxyCodeLine{2865 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2866 }
\DoxyCodeLine{2867         \textcolor{keywordflow}{if} (s3sq == 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2868           \textcolor{comment}{! There is a simple, exact solution to the quadratic equation, namely:}}
\DoxyCodeLine{2869           stays = h1 \textcolor{comment}{! This will revert to stays\_max later.}}
\DoxyCodeLine{2870         \textcolor{keywordflow}{elseif} (s2*s2 <= s3sq) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2871           \textcolor{comment}{! There is no solution with 0 PE change -\/ use the minimum energy input.}}
\DoxyCodeLine{2872           stays = s1}
\DoxyCodeLine{2873         \textcolor{keywordflow}{else}}
\DoxyCodeLine{2874           \textcolor{comment}{! The following choose the solutions that are continuous with all water}}
\DoxyCodeLine{2875           \textcolor{comment}{! staying in the upper buffer layer when there is no detrainment,}}
\DoxyCodeLine{2876           \textcolor{comment}{! namely the + root when s2>0 and the -\/ root otherwise. They also}}
\DoxyCodeLine{2877           \textcolor{comment}{! carefully avoid differencing large numbers, using s2 = (h1-\/s).}}
\DoxyCodeLine{2878           \textcolor{keywordflow}{if} (bh0 <= 0.0) \textcolor{keywordflow}{then} ; stays = h1}
\DoxyCodeLine{2879           \textcolor{keywordflow}{elseif} (s2 > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2880   \textcolor{comment}{!         stays = s + sqrt(s2*s2 -\/ s3sq) ! Note that s2 = h1-\/s}}
\DoxyCodeLine{2881             \textcolor{keywordflow}{if} (s1 >= stays\_max) \textcolor{keywordflow}{then} ; stays = stays\_max}
\DoxyCodeLine{2882             \textcolor{keywordflow}{elseif} (s1 >= 0.0) \textcolor{keywordflow}{then} ; stays = s1 + sqrt(s2*s2 -\/ s3sq)}
\DoxyCodeLine{2883             \textcolor{keywordflow}{else} ; stays = (h1*(s2-\/s1) -\/ s3sq) / (-\/s1 + sqrt(s2*s2 -\/ s3sq))}
\DoxyCodeLine{2884 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{2885           \textcolor{keywordflow}{else}}
\DoxyCodeLine{2886   \textcolor{comment}{!         stays = s -\/ sqrt(s2*s2 -\/ s3sq) ! Note that s2 = h1-\/s \& stays\_min >= 0}}
\DoxyCodeLine{2887             \textcolor{keywordflow}{if} (s1 <= stays\_min) \textcolor{keywordflow}{then} ; stays = stays\_min}
\DoxyCodeLine{2888             \textcolor{keywordflow}{else} ; stays = (h1*(s1-\/s2) + s3sq) / (s1 + sqrt(s2*s2 -\/ s3sq))}
\DoxyCodeLine{2889 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{2890 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{2891 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2892 }
\DoxyCodeLine{2893         \textcolor{comment}{! Limit the amount that stays so that the motion of water is from the}}
\DoxyCodeLine{2894         \textcolor{comment}{! upper buffer layer into the lower, but no more than is in the upper}}
\DoxyCodeLine{2895         \textcolor{comment}{! layer, and the water left in the upper layer is no lighter than the}}
\DoxyCodeLine{2896         \textcolor{comment}{! detrained water.}}
\DoxyCodeLine{2897         \textcolor{keywordflow}{if} (stays >= stays\_max) \textcolor{keywordflow}{then} ; stays = stays\_max}
\DoxyCodeLine{2898         \textcolor{keywordflow}{elseif} (stays < stays\_min) \textcolor{keywordflow}{then} ; stays = stays\_min}
\DoxyCodeLine{2899 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2900 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2901 }
\DoxyCodeLine{2902       dpe\_det = g\_2*((r0(i,kb1)*h\_to\_bl -\/ r0\_to\_bl)*stays + \&}
\DoxyCodeLine{2903                      (r0(i,kb2)-\/r0(i,kb1)) * (h1-\/stays) * \&}
\DoxyCodeLine{2904                      (h2 -\/ scale\_slope*stays*((h1+h2)+h\_to\_bl)/(h1+h2)) ) -\/ \&}
\DoxyCodeLine{2905                 rho0xg*dpe\_extrap}
\DoxyCodeLine{2906 }
\DoxyCodeLine{2907       \textcolor{keywordflow}{if} (dpe\_time\_ratio*h\_to\_bl > h\_to\_bl+h(i,0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2908         dpe\_ratio = (h\_to\_bl+h(i,0)) / h\_to\_bl}
\DoxyCodeLine{2909       \textcolor{keywordflow}{else}}
\DoxyCodeLine{2910         dpe\_ratio = dpe\_time\_ratio}
\DoxyCodeLine{2911 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2912 }
\DoxyCodeLine{2913       \textcolor{keywordflow}{if} ((mergeable\_bl) .and. (num\_events*dpe\_ratio*dpe\_det > dpe\_merge)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2914         \textcolor{comment}{! It is energetically preferable to merge the two buffer layers, detrain}}
\DoxyCodeLine{2915         \textcolor{comment}{! them into interior layer (k0), move the remaining upper buffer layer}}
\DoxyCodeLine{2916         \textcolor{comment}{! water into the lower buffer layer, and detrain undiluted into the}}
\DoxyCodeLine{2917         \textcolor{comment}{! upper buffer layer.}}
\DoxyCodeLine{2918         h1\_to\_k0 = (h1-\/stays\_merge)}
\DoxyCodeLine{2919         stays = max(h\_min\_bl-\/h\_to\_bl,0.0)}
\DoxyCodeLine{2920         h1\_to\_h2 = stays\_merge -\/ stays}
\DoxyCodeLine{2921 }
\DoxyCodeLine{2922         ihk0 = 1.0 / ((h1\_to\_k0 + h2) + h(i,k0))}
\DoxyCodeLine{2923         ih1f = 1.0 / (h\_to\_bl + stays); ih2f = 1.0 / h1\_to\_h2}
\DoxyCodeLine{2924         ih12 = 1.0 / (h1 + h2)}
\DoxyCodeLine{2925 }
\DoxyCodeLine{2926         drcv\_2dz = (rcv(i,kb1) -\/ rcv(i,kb2)) * ih12}
\DoxyCodeLine{2927         drcv\_stays = drcv\_2dz*(h1\_to\_k0 + h1\_to\_h2)}
\DoxyCodeLine{2928         drcv\_det = -\/ drcv\_2dz*(stays + h1\_to\_h2)}
\DoxyCodeLine{2929         rcv(i,k0) = ((h1\_to\_k0*(rcv(i,kb1) + drcv\_det) + \&}
\DoxyCodeLine{2930                       h2*rcv(i,kb2)) + h(i,k0)*rcv(i,k0)) * ihk0}
\DoxyCodeLine{2931         rcv(i,kb2) = rcv(i,kb1) + drcv\_2dz*(h1\_to\_k0-\/stays)}
\DoxyCodeLine{2932         rcv(i,kb1) = (rcv\_to\_bl + stays*(rcv(i,kb1) + drcv\_stays)) * ih1f}
\DoxyCodeLine{2933 }
\DoxyCodeLine{2934         \textcolor{comment}{! Use 2nd order upwind advection of spiciness, limited by the value in}}
\DoxyCodeLine{2935         \textcolor{comment}{! the water from the mixed layer to determine the temperature and}}
\DoxyCodeLine{2936         \textcolor{comment}{! salinity of the water that stays in the buffer layers.}}
\DoxyCodeLine{2937         i\_denom = 1.0 / (drcv\_ds(i)**2 + (dt\_ds\_gauge*drcv\_dt(i))**2)}
\DoxyCodeLine{2938         dspice\_2dz = (ds\_dt\_gauge*drcv\_ds(i)*(t(i,kb1)-\/t(i,kb2)) -\/ \&}
\DoxyCodeLine{2939                       dt\_ds\_gauge*drcv\_dt(i)*(s(i,kb1)-\/s(i,kb2))) * ih12}
\DoxyCodeLine{2940         dspice\_lim = (ds\_dt\_gauge*dr0\_ds(i)*(t\_to\_bl-\/t(i,kb1)*h\_to\_bl) -\/ \&}
\DoxyCodeLine{2941                       dt\_ds\_gauge*dr0\_dt(i)*(s\_to\_bl-\/s(i,kb1)*h\_to\_bl)) / h\_to\_bl}
\DoxyCodeLine{2942         \textcolor{keywordflow}{if} (dspice\_lim * dspice\_2dz <= 0.0) dspice\_2dz = 0.0}
\DoxyCodeLine{2943 }
\DoxyCodeLine{2944         \textcolor{keywordflow}{if} (stays > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2945         \textcolor{comment}{! Limit the spiciness of the water that stays in the upper buffer layer.}}
\DoxyCodeLine{2946           \textcolor{keywordflow}{if} (abs(dspice\_lim) < abs(dspice\_2dz*(h1\_to\_k0 + h1\_to\_h2))) \&}
\DoxyCodeLine{2947             dspice\_2dz = dspice\_lim/(h1\_to\_k0 + h1\_to\_h2)}
\DoxyCodeLine{2948 }
\DoxyCodeLine{2949           dspice\_stays = dspice\_2dz*(h1\_to\_k0 + h1\_to\_h2)}
\DoxyCodeLine{2950           t\_stays = t(i,kb1) + dt\_ds\_gauge * i\_denom * \&}
\DoxyCodeLine{2951               (dt\_ds\_gauge * drcv\_dt(i) * drcv\_stays + drcv\_ds(i) * dspice\_stays)}
\DoxyCodeLine{2952           s\_stays = s(i,kb1) + i\_denom * \&}
\DoxyCodeLine{2953               (drcv\_ds(i) * drcv\_stays -\/ dt\_ds\_gauge * drcv\_dt(i) * dspice\_stays)}
\DoxyCodeLine{2954           \textcolor{comment}{! The values of R0 are based on changes in T and S.}}
\DoxyCodeLine{2955           r0\_stays = r0(i,kb1) + (t\_stays-\/t(i,kb1)) * dr0\_dt(i) + \&}
\DoxyCodeLine{2956                                  (s\_stays-\/s(i,kb1)) * dr0\_ds(i)}
\DoxyCodeLine{2957         \textcolor{keywordflow}{else}}
\DoxyCodeLine{2958           \textcolor{comment}{! Limit the spiciness of the water that moves into the lower buffer layer.}}
\DoxyCodeLine{2959           \textcolor{keywordflow}{if} (abs(dspice\_lim) < abs(dspice\_2dz*h1\_to\_k0)) \&}
\DoxyCodeLine{2960             dspice\_2dz = dspice\_lim/h1\_to\_k0}
\DoxyCodeLine{2961           \textcolor{comment}{! These will be multiplied by 0 later.}}
\DoxyCodeLine{2962           t\_stays = 0.0 ; s\_stays = 0.0 ; r0\_stays = 0.0}
\DoxyCodeLine{2963 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2964 }
\DoxyCodeLine{2965         dspice\_det = -\/ dspice\_2dz*(stays + h1\_to\_h2)}
\DoxyCodeLine{2966         t\_det = t(i,kb1) + dt\_ds\_gauge * i\_denom * \&}
\DoxyCodeLine{2967             (dt\_ds\_gauge * drcv\_dt(i) * drcv\_det + drcv\_ds(i) * dspice\_det)}
\DoxyCodeLine{2968         s\_det = s(i,kb1) + i\_denom * \&}
\DoxyCodeLine{2969             (drcv\_ds(i) * drcv\_det -\/ dt\_ds\_gauge * drcv\_dt(i) * dspice\_det)}
\DoxyCodeLine{2970         \textcolor{comment}{! The values of R0 are based on changes in T and S.}}
\DoxyCodeLine{2971         r0\_det = r0(i,kb1) + (t\_det-\/t(i,kb1)) * dr0\_dt(i) + \&}
\DoxyCodeLine{2972                              (s\_det-\/s(i,kb1)) * dr0\_ds(i)}
\DoxyCodeLine{2973 }
\DoxyCodeLine{2974         t(i,k0) = ((h1\_to\_k0*t\_det + h2*t(i,kb2)) + h(i,k0)*t(i,k0)) * ihk0}
\DoxyCodeLine{2975         t(i,kb2) = (h1*t(i,kb1) -\/ stays*t\_stays -\/ h1\_to\_k0*t\_det) * ih2f}
\DoxyCodeLine{2976         t(i,kb1) = (t\_to\_bl + stays*t\_stays) * ih1f}
\DoxyCodeLine{2977 }
\DoxyCodeLine{2978         s(i,k0) = ((h1\_to\_k0*s\_det + h2*s(i,kb2)) + h(i,k0)*s(i,k0)) * ihk0}
\DoxyCodeLine{2979         s(i,kb2) = (h1*s(i,kb1) -\/ stays*s\_stays -\/ h1\_to\_k0*s\_det) * ih2f}
\DoxyCodeLine{2980         s(i,kb1) = (s\_to\_bl + stays*s\_stays) * ih1f}
\DoxyCodeLine{2981 }
\DoxyCodeLine{2982         r0(i,k0) = ((h1\_to\_k0*r0\_det + h2*r0(i,kb2)) + h(i,k0)*r0(i,k0)) * ihk0}
\DoxyCodeLine{2983         r0(i,kb2) = (h1*r0(i,kb1) -\/ stays*r0\_stays -\/ h1\_to\_k0*r0\_det) * ih2f}
\DoxyCodeLine{2984         r0(i,kb1) = (r0\_to\_bl + stays*r0\_stays) * ih1f}
\DoxyCodeLine{2985 }
\DoxyCodeLine{2986 \textcolor{comment}{!        ! The following is 2nd-\/order upwind advection without limiters.}}
\DoxyCodeLine{2987 \textcolor{comment}{!        dT\_2dz = (T(i,kb1) -\/ T(i,kb2)) * Ih12}}
\DoxyCodeLine{2988 \textcolor{comment}{!        T(i,k0) = (h1\_to\_k0*(T(i,kb1) -\/ dT\_2dz*(stays+h1\_to\_h2)) + \&}}
\DoxyCodeLine{2989 \textcolor{comment}{!                     h2*T(i,kb2) + h(i,k0)*T(i,k0)) * Ihk0}}
\DoxyCodeLine{2990 \textcolor{comment}{!        T(i,kb2) = T(i,kb1) + dT\_2dz*(h1\_to\_k0-\/stays)}}
\DoxyCodeLine{2991 \textcolor{comment}{!        T(i,kb1) = (T\_to\_bl + stays*(T(i,kb1) + \&}}
\DoxyCodeLine{2992 \textcolor{comment}{!                      dT\_2dz*(h1\_to\_k0 + h1\_to\_h2))) * Ih1f}}
\DoxyCodeLine{2993 \textcolor{comment}{!        dS\_2dz = (S(i,kb1) -\/ S(i,kb2)) * Ih12}}
\DoxyCodeLine{2994 \textcolor{comment}{!        S(i,k0) = (h1\_to\_k0*(S(i,kb1) -\/ dS\_2dz*(stays+h1\_to\_h2)) + \&}}
\DoxyCodeLine{2995 \textcolor{comment}{!                     h2*S(i,kb2) + h(i,k0)*S(i,k0)) * Ihk0}}
\DoxyCodeLine{2996 \textcolor{comment}{!        S(i,kb2) = S(i,kb1) + dS\_2dz*(h1\_to\_k0-\/stays)}}
\DoxyCodeLine{2997 \textcolor{comment}{!        S(i,kb1) = (S\_to\_bl + stays*(S(i,kb1) + \&}}
\DoxyCodeLine{2998 \textcolor{comment}{!                      dS\_2dz*(h1\_to\_k0 + h1\_to\_h2))) * Ih1f}}
\DoxyCodeLine{2999 \textcolor{comment}{!        dR0\_2dz = (R0(i,kb1) -\/ R0(i,kb2)) * Ih12}}
\DoxyCodeLine{3000 \textcolor{comment}{!        R0(i,k0) = (h1\_to\_k0*(R0(i,kb1) -\/ dR0\_2dz*(stays+h1\_to\_h2)) + \&}}
\DoxyCodeLine{3001 \textcolor{comment}{!                    h2*R0(i,kb2) + h(i,k0)*R0(i,k0)) * Ihk0}}
\DoxyCodeLine{3002 \textcolor{comment}{!        R0(i,kb2) = R0(i,kb1) + dR0\_2dz*(h1\_to\_k0-\/stays)}}
\DoxyCodeLine{3003 \textcolor{comment}{!        R0(i,kb1) = (R0\_to\_bl + stays*(R0(i,kb1) + \&}}
\DoxyCodeLine{3004 \textcolor{comment}{!                     dR0\_2dz*(h1\_to\_k0 + h1\_to\_h2))) * Ih1f}}
\DoxyCodeLine{3005 }
\DoxyCodeLine{3006         d\_ea(i,kb1) = (d\_ea(i,kb1) + h\_to\_bl) + (stays -\/ h1)}
\DoxyCodeLine{3007         d\_ea(i,kb2) = d\_ea(i,kb2) + (h1\_to\_h2 -\/ h2)}
\DoxyCodeLine{3008         d\_ea(i,k0) = d\_ea(i,k0) + (h1\_to\_k0 + h2)}
\DoxyCodeLine{3009 }
\DoxyCodeLine{3010         h(i,kb1) = stays + h\_to\_bl}
\DoxyCodeLine{3011         h(i,kb2) = h1\_to\_h2}
\DoxyCodeLine{3012         h(i,k0) = h(i,k0) + (h1\_to\_k0 + h2)}
\DoxyCodeLine{3013         \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain)) \&}
\DoxyCodeLine{3014           cs\%diag\_PE\_detrain(i,j) = cs\%diag\_PE\_detrain(i,j) + idt\_h2*dpe\_merge}
\DoxyCodeLine{3015         \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain2)) cs\%diag\_PE\_detrain2(i,j) = \&}
\DoxyCodeLine{3016              cs\%diag\_PE\_detrain2(i,j) + idt\_h2*(dpe\_det + rho0xg*dpe\_extrap)}
\DoxyCodeLine{3017       \textcolor{keywordflow}{else} \textcolor{comment}{! Not mergeable\_bl.}}
\DoxyCodeLine{3018         \textcolor{comment}{! There is no further detrainment from the buffer layers, and the}}
\DoxyCodeLine{3019         \textcolor{comment}{! upper buffer layer water is distributed optimally between the}}
\DoxyCodeLine{3020         \textcolor{comment}{! upper and lower buffer layer.}}
\DoxyCodeLine{3021         h1\_to\_h2 = h1 -\/ stays}
\DoxyCodeLine{3022         ih1f = 1.0 / (h\_to\_bl + stays) ; ih2f = 1.0 / (h2 + h1\_to\_h2)}
\DoxyCodeLine{3023         ih = 1.0 / (h1 + h2)}
\DoxyCodeLine{3024         dr0\_2dz = (r0(i,kb1) -\/ r0(i,kb2)) * ih}
\DoxyCodeLine{3025         r0(i,kb2) = (h2*r0(i,kb2) + h1\_to\_h2*(r0(i,kb1) -\/ \&}
\DoxyCodeLine{3026                      scale\_slope*dr0\_2dz*stays)) * ih2f}
\DoxyCodeLine{3027         r0(i,kb1) = (r0\_to\_bl + stays*(r0(i,kb1) + \&}
\DoxyCodeLine{3028                         scale\_slope*dr0\_2dz*h1\_to\_h2)) * ih1f}
\DoxyCodeLine{3029 }
\DoxyCodeLine{3030         \textcolor{comment}{! Use 2nd order upwind advection of spiciness, limited by the value}}
\DoxyCodeLine{3031         \textcolor{comment}{! in the detrained water to determine the detrained temperature and}}
\DoxyCodeLine{3032         \textcolor{comment}{! salinity.}}
\DoxyCodeLine{3033         dr0 = scale\_slope*dr0\_2dz*h1\_to\_h2}
\DoxyCodeLine{3034         dspice\_stays = (ds\_dt\_gauge*dr0\_ds(i)*(t(i,kb1)-\/t(i,kb2)) -\/ \&}
\DoxyCodeLine{3035                         dt\_ds\_gauge*dr0\_dt(i)*(s(i,kb1)-\/s(i,kb2))) * \&}
\DoxyCodeLine{3036                         scale\_slope*h1\_to\_h2 * ih}
\DoxyCodeLine{3037         \textcolor{keywordflow}{if} (h\_to\_bl > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3038           dspice\_lim = (ds\_dt\_gauge*dr0\_ds(i)*(t\_to\_bl-\/t(i,kb1)*h\_to\_bl) -\/ \&}
\DoxyCodeLine{3039                         dt\_ds\_gauge*dr0\_dt(i)*(s\_to\_bl-\/s(i,kb1)*h\_to\_bl)) /\&}
\DoxyCodeLine{3040                         h\_to\_bl}
\DoxyCodeLine{3041         \textcolor{keywordflow}{else}}
\DoxyCodeLine{3042           dspice\_lim = ds\_dt\_gauge*dr0\_ds(i)*(t(i,0)-\/t(i,kb1)) -\/ \&}
\DoxyCodeLine{3043                        dt\_ds\_gauge*dr0\_dt(i)*(s(i,0)-\/s(i,kb1))}
\DoxyCodeLine{3044 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{3045         \textcolor{keywordflow}{if} (dspice\_stays*dspice\_lim <= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3046           dspice\_stays = 0.0}
\DoxyCodeLine{3047         \textcolor{keywordflow}{elseif} (abs(dspice\_stays) > abs(dspice\_lim)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3048           dspice\_stays = dspice\_lim}
\DoxyCodeLine{3049 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{3050         i\_denom = 1.0 / (dr0\_ds(i)**2 + (dt\_ds\_gauge*dr0\_dt(i))**2)}
\DoxyCodeLine{3051         t\_stays = t(i,kb1) + dt\_ds\_gauge * i\_denom * \&}
\DoxyCodeLine{3052             (dt\_ds\_gauge * dr0\_dt(i) * dr0 + dr0\_ds(i) * dspice\_stays)}
\DoxyCodeLine{3053         s\_stays = s(i,kb1) + i\_denom * \&}
\DoxyCodeLine{3054             (dr0\_ds(i) * dr0 -\/ dt\_ds\_gauge * dr0\_dt(i) * dspice\_stays)}
\DoxyCodeLine{3055         \textcolor{comment}{! The detrained values of Rcv are based on changes in T and S.}}
\DoxyCodeLine{3056         rcv\_stays = rcv(i,kb1) + (t\_stays-\/t(i,kb1)) * drcv\_dt(i) + \&}
\DoxyCodeLine{3057                                  (s\_stays-\/s(i,kb1)) * drcv\_ds(i)}
\DoxyCodeLine{3058 }
\DoxyCodeLine{3059         t(i,kb2) = (h2*t(i,kb2) + h1*t(i,kb1) -\/ t\_stays*stays) * ih2f}
\DoxyCodeLine{3060         t(i,kb1) = (t\_to\_bl + stays*t\_stays) * ih1f}
\DoxyCodeLine{3061         s(i,kb2) = (h2*s(i,kb2) + h1*s(i,kb1) -\/ s\_stays*stays) * ih2f}
\DoxyCodeLine{3062         s(i,kb1) = (s\_to\_bl + stays*s\_stays) * ih1f}
\DoxyCodeLine{3063         rcv(i,kb2) = (h2*rcv(i,kb2) + h1*rcv(i,kb1) -\/ rcv\_stays*stays) * ih2f}
\DoxyCodeLine{3064         rcv(i,kb1) = (rcv\_to\_bl + stays*rcv\_stays) * ih1f}
\DoxyCodeLine{3065 }
\DoxyCodeLine{3066 \textcolor{comment}{!        ! The following is 2nd-\/order upwind advection without limiters.}}
\DoxyCodeLine{3067 \textcolor{comment}{!        dRcv\_2dz = (Rcv(i,kb1) -\/ Rcv(i,kb2)) * Ih}}
\DoxyCodeLine{3068 \textcolor{comment}{!        dRcv = scale\_slope*dRcv\_2dz*h1\_to\_h2}}
\DoxyCodeLine{3069 \textcolor{comment}{!        Rcv(i,kb2) = (h2*Rcv(i,kb2) + h1\_to\_h2*(Rcv(i,kb1) -\/ \&}}
\DoxyCodeLine{3070 \textcolor{comment}{!                      scale\_slope*dRcv\_2dz*stays)) * Ih2f}}
\DoxyCodeLine{3071 \textcolor{comment}{!        Rcv(i,kb1) = (Rcv\_to\_bl + stays*(Rcv(i,kb1) + dRcv)) * Ih1f}}
\DoxyCodeLine{3072 \textcolor{comment}{!        dT\_2dz = (T(i,kb1) -\/ T(i,kb2)) * Ih}}
\DoxyCodeLine{3073 \textcolor{comment}{!        T(i,kb2) = (h2*T(i,kb2) + h1\_to\_h2*(T(i,kb1) -\/ \&}}
\DoxyCodeLine{3074 \textcolor{comment}{!                      scale\_slope*dT\_2dz*stays)) * Ih2f}}
\DoxyCodeLine{3075 \textcolor{comment}{!        T(i,kb1) = (T\_to\_bl + stays*(T(i,kb1) + \&}}
\DoxyCodeLine{3076 \textcolor{comment}{!                      scale\_slope*dT\_2dz*h1\_to\_h2)) * Ih1f}}
\DoxyCodeLine{3077 \textcolor{comment}{!        dS\_2dz = (S(i,kb1) -\/ S(i,kb2)) * Ih}}
\DoxyCodeLine{3078 \textcolor{comment}{!        S(i,kb2) = (h2*S(i,kb2) + h1\_to\_h2*(S(i,kb1) -\/ \&}}
\DoxyCodeLine{3079 \textcolor{comment}{!                      scale\_slope*dS\_2dz*stays)) * Ih2f}}
\DoxyCodeLine{3080 \textcolor{comment}{!        S(i,kb1) = (S\_to\_bl + stays*(S(i,kb1) + \&}}
\DoxyCodeLine{3081 \textcolor{comment}{!                      scale\_slope*dS\_2dz*h1\_to\_h2)) * Ih1f}}
\DoxyCodeLine{3082 }
\DoxyCodeLine{3083         d\_ea(i,kb1) = d\_ea(i,kb1) + ((stays -\/ h1) + h\_to\_bl)}
\DoxyCodeLine{3084         d\_ea(i,kb2) = d\_ea(i,kb2) + h1\_to\_h2}
\DoxyCodeLine{3085 }
\DoxyCodeLine{3086         h(i,kb1) = stays + h\_to\_bl}
\DoxyCodeLine{3087         h(i,kb2) = h(i,kb2) + h1\_to\_h2}
\DoxyCodeLine{3088 }
\DoxyCodeLine{3089         \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain)) \&}
\DoxyCodeLine{3090           cs\%diag\_PE\_detrain(i,j) = cs\%diag\_PE\_detrain(i,j) + idt\_h2*dpe\_det}
\DoxyCodeLine{3091         \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs\%diag\_PE\_detrain2)) cs\%diag\_PE\_detrain2(i,j) = \&}
\DoxyCodeLine{3092           cs\%diag\_PE\_detrain2(i,j) + idt\_h2*(dpe\_det + rho0xg*dpe\_extrap)}
\DoxyCodeLine{3093 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{3094 \textcolor{keywordflow}{    endif} \textcolor{comment}{! End of detrainment...}}
\DoxyCodeLine{3095 }
\DoxyCodeLine{3096 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! i loop}}
\DoxyCodeLine{3097 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_a1378659bc97b52e065a7cfe44166504d}\label{namespacemom__bulk__mixed__layer_a1378659bc97b52e065a7cfe44166504d}} 
\index{mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}!resort\_ml@{resort\_ml}}
\index{resort\_ml@{resort\_ml}!mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}}
\doxysubsubsection{\texorpdfstring{resort\_ml()}{resort\_ml()}}
{\footnotesize\ttfamily subroutine mom\+\_\+bulk\+\_\+mixed\+\_\+layer\+::resort\+\_\+ml (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied,0\+: gv \%ke), intent(inout)}]{h,  }\item[{real, dimension( g \%isd\+: g \%ied,0\+: gv \%ke), intent(inout)}]{T,  }\item[{real, dimension( g \%isd\+: g \%ied,0\+: gv \%ke), intent(inout)}]{S,  }\item[{real, dimension( g \%isd\+: g \%ied,0\+: gv \%ke), intent(inout)}]{R0,  }\item[{real, dimension( g \%isd\+: g \%ied,0\+: gv \%ke), intent(inout)}]{Rcv,  }\item[{real, dimension( gv \%ke), intent(in)}]{Rcv\+Tgt,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{eps,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{d\+\_\+ea,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(inout)}]{d\+\_\+eb,  }\item[{integer, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{ksort,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(\mbox{\hyperlink{structmom__bulk__mixed__layer_1_1bulkmixedlayer__cs}{bulkmixedlayer\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{d\+R0\+\_\+dT,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{d\+R0\+\_\+dS,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{d\+Rcv\+\_\+dT,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{d\+Rcv\+\_\+dS }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine actually moves properties between layers to achieve a resorted state, with all of the resorted water either moved into the correct interior layers or in the top nkmb layers. 


\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,out}}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. Layer 0 is the new mixed layer. \\
\hline
\mbox{\texttt{ in,out}}  & {\em t} & Layer temperatures \mbox{[}degC\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em s} & Layer salinities \mbox{[}ppt\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em r0} & Potential density referenced to surface pressure \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em rcv} & The coordinate defining potential density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em rcvtgt} & The target value of Rcv for each layer \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em eps} & The (small) thickness that must remain in each layer \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em d\+\_\+ea} & The upward increase across a layer in the entrainment from above \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. Positive d\+\_\+ea goes with layer thickness increases. \\
\hline
\mbox{\texttt{ in,out}}  & {\em d\+\_\+eb} & The downward increase across a layer in the entrainment from below \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. Positive values go with mass gain by a layer. \\
\hline
\mbox{\texttt{ in}}  & {\em ksort} & The density-\/sorted k-\/indicies. \\
\hline
 & {\em cs} & The control structure for this module. \\
\hline
\mbox{\texttt{ in}}  & {\em dr0\+\_\+dt} & The partial derivative of potential density referenced to the surface with potential temperature \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dr0\+\_\+ds} & The partial derivative of cpotential density referenced to the surface with salinity, \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em drcv\+\_\+dt} & The partial derivative of coordinate defining potential density with potential temperature \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em drcv\+\_\+ds} & The partial derivative of coordinate defining potential density with salinity, \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 1893 of file M\+O\+M\+\_\+bulk\+\_\+mixed\+\_\+layer.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1893   \textcolor{keywordtype}{type}(ocean\_grid\_type),                \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{       !< The ocean's grid structure.}}
\DoxyCodeLine{1894   \textcolor{keywordtype}{type}(verticalGrid\_type),              \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{      !< The ocean's vertical grid}}
\DoxyCodeLine{1895 \textcolor{comment}{                                                                 !! structure.}}
\DoxyCodeLine{1896 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))},   \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{       !< Layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1897 \textcolor{comment}{                                                                 !! Layer 0 is the new mixed layer.}}
\DoxyCodeLine{1898 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))},   \textcolor{keywordtype}{intent(inout)} :: T\textcolor{comment}{       !< Layer temperatures [degC].}}
\DoxyCodeLine{1899 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))},   \textcolor{keywordtype}{intent(inout)} :: S\textcolor{comment}{       !< Layer salinities [ppt].}}
\DoxyCodeLine{1900 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))},   \textcolor{keywordtype}{intent(inout)} :: R0\textcolor{comment}{      !< Potential density referenced to}}
\DoxyCodeLine{1901 \textcolor{comment}{                                                                 !! surface pressure [R \string~> kg m-\/3].}}
\DoxyCodeLine{1902 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))},   \textcolor{keywordtype}{intent(inout)} :: Rcv\textcolor{comment}{     !< The coordinate defining}}
\DoxyCodeLine{1903 \textcolor{comment}{                                                                 !! potential density [R \string~> kg m-\/3].}}
\DoxyCodeLine{1904 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV))},            \textcolor{keywordtype}{intent(in)}    :: RcvTgt\textcolor{comment}{  !< The target value of Rcv for each}}
\DoxyCodeLine{1905 \textcolor{comment}{                                                                 !! layer [R \string~> kg m-\/3].}}
\DoxyCodeLine{1906 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},    \textcolor{keywordtype}{intent(inout)} :: eps\textcolor{comment}{     !< The (small) thickness that must}}
\DoxyCodeLine{1907 \textcolor{comment}{                                                                 !! remain in each layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1908 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},    \textcolor{keywordtype}{intent(inout)} :: d\_ea\textcolor{comment}{    !< The upward increase across a}}
\DoxyCodeLine{1909 \textcolor{comment}{                                                                 !! layer in the entrainment from}}
\DoxyCodeLine{1910 \textcolor{comment}{                                                                 !! above [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1911 \textcolor{comment}{                                                                 !!  Positive d\_ea goes with layer}}
\DoxyCodeLine{1912 \textcolor{comment}{                                                                 !! thickness increases.}}
\DoxyCodeLine{1913 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},    \textcolor{keywordtype}{intent(inout)} :: d\_eb\textcolor{comment}{    !< The downward increase across a}}
\DoxyCodeLine{1914 \textcolor{comment}{                                                                 !! layer in the entrainment from}}
\DoxyCodeLine{1915 \textcolor{comment}{                                                                 !! below [H \string~> m or kg m-\/2]. Positive values go}}
\DoxyCodeLine{1916 \textcolor{comment}{                                                                 !! with mass gain by a layer.}}
\DoxyCodeLine{1917   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(in)}    :: ksort\textcolor{comment}{   !< The density-\/sorted k-\/indicies.}}
\DoxyCodeLine{1918   \textcolor{keywordtype}{type}(bulkmixedlayer\_CS),              \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{      !< The control structure for this}}
\DoxyCodeLine{1919 \textcolor{comment}{                                                                 !! module.}}
\DoxyCodeLine{1920 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},             \textcolor{keywordtype}{intent(in)}    :: dR0\_dT\textcolor{comment}{  !< The partial derivative of}}
\DoxyCodeLine{1921 \textcolor{comment}{                                                                 !! potential density referenced}}
\DoxyCodeLine{1922 \textcolor{comment}{                                                                 !! to the surface with potential}}
\DoxyCodeLine{1923 \textcolor{comment}{                                                                 !! temperature [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{1924 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},             \textcolor{keywordtype}{intent(in)}    :: dR0\_dS\textcolor{comment}{  !< The partial derivative of}}
\DoxyCodeLine{1925 \textcolor{comment}{                                                                 !! cpotential density referenced}}
\DoxyCodeLine{1926 \textcolor{comment}{                                                                 !! to the surface with salinity,}}
\DoxyCodeLine{1927 \textcolor{comment}{                                                                 !! [R ppt-\/1 \string~> kg m-\/3 ppt-\/1].}}
\DoxyCodeLine{1928 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},             \textcolor{keywordtype}{intent(in)}    :: dRcv\_dT\textcolor{comment}{ !< The partial derivative of}}
\DoxyCodeLine{1929 \textcolor{comment}{                                                                 !! coordinate defining potential}}
\DoxyCodeLine{1930 \textcolor{comment}{                                                                 !! density with potential}}
\DoxyCodeLine{1931 \textcolor{comment}{                                                                 !! temperature [R degC-\/1 \string~> kg m-\/3 degC-\/1].}}
\DoxyCodeLine{1932 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},             \textcolor{keywordtype}{intent(in)}    :: dRcv\_dS\textcolor{comment}{ !< The partial derivative of}}
\DoxyCodeLine{1933 \textcolor{comment}{                                                                 !! coordinate defining potential}}
\DoxyCodeLine{1934 \textcolor{comment}{                                                                 !! density with salinity,}}
\DoxyCodeLine{1935 \textcolor{comment}{                                                                 !! [R ppt-\/1 \string~> kg m-\/3 ppt-\/1].}}
\DoxyCodeLine{1936 }
\DoxyCodeLine{1937 \textcolor{comment}{!   If there are no massive light layers above the deepest of the mixed-\/ and}}
\DoxyCodeLine{1938 \textcolor{comment}{! buffer layers, do nothing (except perhaps to reshuffle these layers).}}
\DoxyCodeLine{1939 \textcolor{comment}{!   If there are nkbl or fewer layers above the deepest mixed-\/ or buffer-\/}}
\DoxyCodeLine{1940 \textcolor{comment}{! layers, move them (in sorted order) into the buffer layers, even if they}}
\DoxyCodeLine{1941 \textcolor{comment}{! were previously interior layers.}}
\DoxyCodeLine{1942 \textcolor{comment}{!   If there are interior layers that are intermediate in density (both in-\/situ}}
\DoxyCodeLine{1943 \textcolor{comment}{! and the coordinate density (sigma-\/2)) between the newly forming mixed layer}}
\DoxyCodeLine{1944 \textcolor{comment}{! and a residual buffer-\/ or mixed layer, and the number of massive layers above}}
\DoxyCodeLine{1945 \textcolor{comment}{! the deepest massive buffer or mixed layer is greater than nkbl, then split}}
\DoxyCodeLine{1946 \textcolor{comment}{! those buffer layers into peices that match the target density of the two}}
\DoxyCodeLine{1947 \textcolor{comment}{! nearest interior layers.}}
\DoxyCodeLine{1948 \textcolor{comment}{!   Otherwise, if there are more than nkbl+1 remaining massive layers}}
\DoxyCodeLine{1949 }
\DoxyCodeLine{1950   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1951 \textcolor{keywordtype}{  real}    :: h\_move, h\_tgt\_old, I\_hnew}
\DoxyCodeLine{1952 \textcolor{keywordtype}{  real}    :: dT\_dS\_wt2, dT\_dR, dS\_dR, I\_denom}
\DoxyCodeLine{1953 \textcolor{keywordtype}{  real}    :: Rcv\_int}
\DoxyCodeLine{1954 \textcolor{keywordtype}{  real}    :: T\_up, S\_up, R0\_up, I\_hup, h\_to\_up}
\DoxyCodeLine{1955 \textcolor{keywordtype}{  real}    :: T\_dn, S\_dn, R0\_dn, I\_hdn, h\_to\_dn}
\DoxyCodeLine{1956 \textcolor{keywordtype}{  real}    :: wt\_dn}
\DoxyCodeLine{1957 \textcolor{keywordtype}{  real}    :: dR1, dR2}
\DoxyCodeLine{1958 \textcolor{keywordtype}{  real}    :: dPE, hmin, min\_dPE, min\_hmin}
\DoxyCodeLine{1959 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV))} :: \&}
\DoxyCodeLine{1960     h\_tmp, R0\_tmp, T\_tmp, S\_tmp, Rcv\_tmp}
\DoxyCodeLine{1961   \textcolor{keywordtype}{integer} :: ks\_min}
\DoxyCodeLine{1962   \textcolor{keywordtype}{logical} :: sorted, leave\_in\_layer}
\DoxyCodeLine{1963   \textcolor{keywordtype}{integer} :: ks\_deep(SZI\_(G)), k\_count(SZI\_(G)), ks2\_reverse(SZI\_(G), SZK\_(GV))}
\DoxyCodeLine{1964   \textcolor{keywordtype}{integer} :: ks2(SZK\_(GV))}
\DoxyCodeLine{1965   \textcolor{keywordtype}{integer} :: i, k, ks, is, ie, nz, k1, k2, k\_tgt, k\_src, k\_int\_top}
\DoxyCodeLine{1966   \textcolor{keywordtype}{integer} :: nks, nkmb, num\_interior, top\_interior\_ks}
\DoxyCodeLine{1967 }
\DoxyCodeLine{1968   is = g\%isc ; ie = g\%iec ; nz = gv\%ke}
\DoxyCodeLine{1969   nkmb = cs\%nkml+cs\%nkbl}
\DoxyCodeLine{1970 }
\DoxyCodeLine{1971   dt\_ds\_wt2 = cs\%dT\_dS\_wt**2}
\DoxyCodeLine{1972 }
\DoxyCodeLine{1973 \textcolor{comment}{! Find out how many massive layers are above the deepest buffer or mixed layer.}}
\DoxyCodeLine{1974   \textcolor{keywordflow}{do} i=is,ie ; ks\_deep(i) = -\/1 ; k\_count(i) = 0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1975   \textcolor{keywordflow}{do} ks=nz,1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (ksort(i,ks) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1976     k = ksort(i,ks)}
\DoxyCodeLine{1977 }
\DoxyCodeLine{1978     \textcolor{keywordflow}{if} (h(i,k) > eps(i,k)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1979       \textcolor{keywordflow}{if} (ks\_deep(i) == -\/1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1980         \textcolor{keywordflow}{if} (k <= nkmb) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1981           ks\_deep(i) = ks ; k\_count(i) = k\_count(i) + 1}
\DoxyCodeLine{1982           ks2\_reverse(i,k\_count(i)) = k}
\DoxyCodeLine{1983 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1984       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1985         k\_count(i) = k\_count(i) + 1}
\DoxyCodeLine{1986         ks2\_reverse(i,k\_count(i)) = k}
\DoxyCodeLine{1987 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1988 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1989 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1990 }
\DoxyCodeLine{1991   \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (k\_count(i) > 1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1992     \textcolor{comment}{! This column might need to be reshuffled.}}
\DoxyCodeLine{1993     nks = k\_count(i)}
\DoxyCodeLine{1994 }
\DoxyCodeLine{1995     \textcolor{comment}{! Put ks2 in the right order and check whether reshuffling is needed.}}
\DoxyCodeLine{1996     sorted = .true.}
\DoxyCodeLine{1997     ks2(nks) = ks2\_reverse(i,1)}
\DoxyCodeLine{1998     \textcolor{keywordflow}{do} ks=nks-\/1,1,-\/1}
\DoxyCodeLine{1999       ks2(ks) = ks2\_reverse(i,1+nks-\/ks)}
\DoxyCodeLine{2000       \textcolor{keywordflow}{if} (ks2(ks) > ks2(ks+1)) sorted = .false.}
\DoxyCodeLine{2001 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2002 }
\DoxyCodeLine{2003     \textcolor{comment}{! Go to the next column of no reshuffling is needed.}}
\DoxyCodeLine{2004     \textcolor{keywordflow}{if} (sorted) cycle}
\DoxyCodeLine{2005 }
\DoxyCodeLine{2006     \textcolor{comment}{! Find out how many interior layers are being reshuffled.  If none,}}
\DoxyCodeLine{2007     \textcolor{comment}{! then this is a simple swapping procedure.}}
\DoxyCodeLine{2008     num\_interior = 0 ; top\_interior\_ks = 0}
\DoxyCodeLine{2009     \textcolor{keywordflow}{do} ks=nks,1,-\/1 ; \textcolor{keywordflow}{if} (ks2(ks) > nkmb) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2010       num\_interior = num\_interior+1 ; top\_interior\_ks = ks}
\DoxyCodeLine{2011 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2012 }
\DoxyCodeLine{2013     \textcolor{keywordflow}{if} (num\_interior >= 1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2014       \textcolor{comment}{! Find the lightest interior layer with a target coordinate density}}
\DoxyCodeLine{2015       \textcolor{comment}{! greater than the newly forming mixed layer.}}
\DoxyCodeLine{2016       \textcolor{keywordflow}{do} k=nkmb+1,nz ; \textcolor{keywordflow}{if} (rcv(i,0) < rcvtgt(k)) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2017       k\_int\_top = k ; rcv\_int = rcvtgt(k)}
\DoxyCodeLine{2018 }
\DoxyCodeLine{2019       i\_denom = 1.0 / (drcv\_ds(i)**2 + dt\_ds\_wt2*drcv\_dt(i)**2)}
\DoxyCodeLine{2020       dt\_dr = dt\_ds\_wt2*drcv\_dt(i) * i\_denom}
\DoxyCodeLine{2021       ds\_dr = drcv\_ds(i) * i\_denom}
\DoxyCodeLine{2022 }
\DoxyCodeLine{2023 }
\DoxyCodeLine{2024       \textcolor{comment}{! Examine whether layers can be split out of existence.  Stop when there}}
\DoxyCodeLine{2025       \textcolor{comment}{! is a layer that cannot be handled this way, or when the topmost formerly}}
\DoxyCodeLine{2026       \textcolor{comment}{! interior layer has been dealt with.}}
\DoxyCodeLine{2027       \textcolor{keywordflow}{do} ks = nks,top\_interior\_ks,-\/1}
\DoxyCodeLine{2028         k = ks2(ks)}
\DoxyCodeLine{2029         leave\_in\_layer = .false.}
\DoxyCodeLine{2030         \textcolor{keywordflow}{if} ((k > nkmb) .and. (rcv(i,k) <= rcvtgt(k))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2031           \textcolor{keywordflow}{if} (rcvtgt(k)-\/rcv(i,k) < cs\%BL\_split\_rho\_tol*(rcvtgt(k) -\/ rcvtgt(k-\/1))) \&}
\DoxyCodeLine{2032             leave\_in\_layer = .true.}
\DoxyCodeLine{2033         \textcolor{keywordflow}{elseif} (k > nkmb) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2034           \textcolor{keywordflow}{if} (rcv(i,k)-\/rcvtgt(k) < cs\%BL\_split\_rho\_tol*(rcvtgt(k+1) -\/ rcvtgt(k))) \&}
\DoxyCodeLine{2035             leave\_in\_layer = .true.}
\DoxyCodeLine{2036 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2037 }
\DoxyCodeLine{2038         \textcolor{keywordflow}{if} (leave\_in\_layer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2039           \textcolor{comment}{! Just drop this layer from the sorted list.}}
\DoxyCodeLine{2040           nks = nks-\/1}
\DoxyCodeLine{2041         \textcolor{keywordflow}{elseif} (rcv(i,k) < rcv\_int) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2042           \textcolor{comment}{! There is no interior layer with a target density that is intermediate}}
\DoxyCodeLine{2043           \textcolor{comment}{! between this layer and the mixed layer.}}
\DoxyCodeLine{2044           \textcolor{keywordflow}{exit}}
\DoxyCodeLine{2045         \textcolor{keywordflow}{else}}
\DoxyCodeLine{2046           \textcolor{comment}{! Try splitting the layer between two interior isopycnal layers.}}
\DoxyCodeLine{2047           \textcolor{comment}{! Find the target densities that bracket this layer.}}
\DoxyCodeLine{2048           \textcolor{keywordflow}{do} k2=k\_int\_top+1,nz ; \textcolor{keywordflow}{if} (rcv(i,k) < rcvtgt(k2)) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2049           \textcolor{keywordflow}{if} (k2>nz) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{2050 }
\DoxyCodeLine{2051           \textcolor{comment}{! This layer is bracketed in density between layers k2-\/1 and k2.}}
\DoxyCodeLine{2052 }
\DoxyCodeLine{2053           dr1 = (rcvtgt(k2-\/1) -\/ rcv(i,k)) ; dr2 = (rcvtgt(k2) -\/ rcv(i,k))}
\DoxyCodeLine{2054           t\_up = t(i,k) + dt\_dr * dr1}
\DoxyCodeLine{2055           s\_up = s(i,k) + ds\_dr * dr1}
\DoxyCodeLine{2056           t\_dn = t(i,k) + dt\_dr * dr2}
\DoxyCodeLine{2057           s\_dn = s(i,k) + ds\_dr * dr2}
\DoxyCodeLine{2058 }
\DoxyCodeLine{2059           r0\_up = r0(i,k) + (dt\_dr*dr0\_dt(i) + ds\_dr*dr0\_ds(i)) * dr1}
\DoxyCodeLine{2060           r0\_dn = r0(i,k) + (dt\_dr*dr0\_dt(i) + ds\_dr*dr0\_ds(i)) * dr2}
\DoxyCodeLine{2061 }
\DoxyCodeLine{2062           \textcolor{comment}{! Make sure the new properties are acceptable.}}
\DoxyCodeLine{2063           \textcolor{keywordflow}{if} ((r0\_up > r0(i,0)) .or. (r0\_dn > r0(i,0))) \&}
\DoxyCodeLine{2064             \textcolor{comment}{! Avoid creating obviously unstable profiles.}}
\DoxyCodeLine{2065             \textcolor{keywordflow}{exit}}
\DoxyCodeLine{2066 }
\DoxyCodeLine{2067           wt\_dn = (rcv(i,k) -\/ rcvtgt(k2-\/1)) / (rcvtgt(k2) -\/ rcvtgt(k2-\/1))}
\DoxyCodeLine{2068           h\_to\_up = (h(i,k)-\/eps(i,k)) * (1.0 -\/ wt\_dn)}
\DoxyCodeLine{2069           h\_to\_dn = (h(i,k)-\/eps(i,k)) * wt\_dn}
\DoxyCodeLine{2070 }
\DoxyCodeLine{2071           i\_hup = 1.0 / (h(i,k2-\/1) + h\_to\_up)}
\DoxyCodeLine{2072           i\_hdn = 1.0 / (h(i,k2) + h\_to\_dn)}
\DoxyCodeLine{2073           r0(i,k2-\/1) = (r0(i,k2)*h(i,k2-\/1) + r0\_up*h\_to\_up) * i\_hup}
\DoxyCodeLine{2074           r0(i,k2) = (r0(i,k2)*h(i,k2) + r0\_dn*h\_to\_dn) * i\_hdn}
\DoxyCodeLine{2075 }
\DoxyCodeLine{2076           t(i,k2-\/1) = (t(i,k2)*h(i,k2-\/1) + t\_up*h\_to\_up) * i\_hup}
\DoxyCodeLine{2077           t(i,k2) = (t(i,k2)*h(i,k2) + t\_dn*h\_to\_dn) * i\_hdn}
\DoxyCodeLine{2078           s(i,k2-\/1) = (s(i,k2)*h(i,k2-\/1) + s\_up*h\_to\_up) * i\_hup}
\DoxyCodeLine{2079           s(i,k2) = (s(i,k2)*h(i,k2) + s\_dn*h\_to\_dn) * i\_hdn}
\DoxyCodeLine{2080           rcv(i,k2-\/1) = (rcv(i,k2)*h(i,k2-\/1) + rcvtgt(k2-\/1)*h\_to\_up) * i\_hup}
\DoxyCodeLine{2081           rcv(i,k2) = (rcv(i,k2)*h(i,k2) + rcvtgt(k2)*h\_to\_dn) * i\_hdn}
\DoxyCodeLine{2082 }
\DoxyCodeLine{2083           h(i,k) = eps(i,k)}
\DoxyCodeLine{2084           h(i,k2) = h(i,k2) + h\_to\_dn}
\DoxyCodeLine{2085           h(i,k2-\/1) = h(i,k2-\/1) + h\_to\_up}
\DoxyCodeLine{2086 }
\DoxyCodeLine{2087           \textcolor{keywordflow}{if} (k > k2-\/1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2088             d\_eb(i,k) = d\_eb(i,k) -\/ h\_to\_up}
\DoxyCodeLine{2089             d\_eb(i,k2-\/1) = d\_eb(i,k2-\/1) + h\_to\_up}
\DoxyCodeLine{2090           \textcolor{keywordflow}{elseif} (k < k2-\/1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2091             d\_ea(i,k) = d\_ea(i,k) -\/ h\_to\_up}
\DoxyCodeLine{2092             d\_ea(i,k2-\/1) = d\_ea(i,k2-\/1) + h\_to\_up}
\DoxyCodeLine{2093 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{2094           \textcolor{keywordflow}{if} (k > k2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2095             d\_eb(i,k) = d\_eb(i,k) -\/ h\_to\_dn}
\DoxyCodeLine{2096             d\_eb(i,k2) = d\_eb(i,k2) + h\_to\_dn}
\DoxyCodeLine{2097           \textcolor{keywordflow}{elseif} (k < k2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2098             d\_ea(i,k) = d\_ea(i,k) -\/ h\_to\_dn}
\DoxyCodeLine{2099             d\_ea(i,k2) = d\_ea(i,k2) + h\_to\_dn}
\DoxyCodeLine{2100 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{2101           nks = nks-\/1}
\DoxyCodeLine{2102 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2103 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{2104 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2105 }
\DoxyCodeLine{2106     \textcolor{keywordflow}{do} \textcolor{keywordflow}{while} (nks > nkmb)}
\DoxyCodeLine{2107       \textcolor{comment}{! Having already tried to move surface layers into the interior, there}}
\DoxyCodeLine{2108       \textcolor{comment}{! are still too many layers, and layers must be merged until nks=nkmb.}}
\DoxyCodeLine{2109       \textcolor{comment}{! Examine every merger of a layer with its neighbors, and merge the ones}}
\DoxyCodeLine{2110       \textcolor{comment}{! that increase the potential energy the least.  If there are layers}}
\DoxyCodeLine{2111       \textcolor{comment}{! with (apparently?) negative potential energy change, choose the one}}
\DoxyCodeLine{2112       \textcolor{comment}{! with the smallest total thickness.  Repeat until nkmb layers remain.}}
\DoxyCodeLine{2113       \textcolor{comment}{! Choose the smaller value for the remaining index for convenience.}}
\DoxyCodeLine{2114 }
\DoxyCodeLine{2115       ks\_min = -\/1 ; min\_dpe = 1.0 ; min\_hmin = 0.0}
\DoxyCodeLine{2116       \textcolor{keywordflow}{do} ks=1,nks-\/1}
\DoxyCodeLine{2117         k1 = ks2(ks) ; k2 = ks2(ks+1)}
\DoxyCodeLine{2118         dpe = max(0.0, (r0(i,k2)-\/r0(i,k1)) * h(i,k1) * h(i,k2))}
\DoxyCodeLine{2119         hmin = min(h(i,k1)-\/eps(i,k1), h(i,k2)-\/eps(i,k2))}
\DoxyCodeLine{2120         \textcolor{keywordflow}{if} ((ks\_min < 0) .or. (dpe < min\_dpe) .or. \&}
\DoxyCodeLine{2121             ((dpe <= 0.0) .and. (hmin < min\_hmin))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2122            ks\_min = ks ; min\_dpe = dpe ; min\_hmin = hmin}
\DoxyCodeLine{2123 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2124 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{2125 }
\DoxyCodeLine{2126       \textcolor{comment}{! Now merge the two layers that do the least damage.}}
\DoxyCodeLine{2127       k1 = ks2(ks\_min) ; k2 = ks2(ks\_min+1)}
\DoxyCodeLine{2128       \textcolor{keywordflow}{if} (k1 < k2) \textcolor{keywordflow}{then} ; k\_tgt = k1 ; k\_src = k2}
\DoxyCodeLine{2129       \textcolor{keywordflow}{else} ; k\_tgt = k2 ; k\_src = k1 ; ks2(ks\_min) = k2 ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{2130 }
\DoxyCodeLine{2131       h\_tgt\_old = h(i,k\_tgt)}
\DoxyCodeLine{2132       h\_move = h(i,k\_src)-\/eps(i,k\_src)}
\DoxyCodeLine{2133       h(i,k\_src) = eps(i,k\_src)}
\DoxyCodeLine{2134       h(i,k\_tgt) = h(i,k\_tgt) + h\_move}
\DoxyCodeLine{2135       i\_hnew = 1.0 / (h(i,k\_tgt))}
\DoxyCodeLine{2136       r0(i,k\_tgt) = (r0(i,k\_tgt)*h\_tgt\_old + r0(i,k\_src)*h\_move) * i\_hnew}
\DoxyCodeLine{2137 }
\DoxyCodeLine{2138       t(i,k\_tgt) = (t(i,k\_tgt)*h\_tgt\_old + t(i,k\_src)*h\_move) * i\_hnew}
\DoxyCodeLine{2139       s(i,k\_tgt) = (s(i,k\_tgt)*h\_tgt\_old + s(i,k\_src)*h\_move) * i\_hnew}
\DoxyCodeLine{2140       rcv(i,k\_tgt) = (rcv(i,k\_tgt)*h\_tgt\_old + rcv(i,k\_src)*h\_move) * i\_hnew}
\DoxyCodeLine{2141 }
\DoxyCodeLine{2142       d\_eb(i,k\_src) = d\_eb(i,k\_src) -\/ h\_move}
\DoxyCodeLine{2143       d\_eb(i,k\_tgt) = d\_eb(i,k\_tgt) + h\_move}
\DoxyCodeLine{2144 }
\DoxyCodeLine{2145       \textcolor{comment}{! Remove the newly missing layer from the sorted list.}}
\DoxyCodeLine{2146       \textcolor{keywordflow}{do} ks=ks\_min+1,nks ; ks2(ks) = ks2(ks+1) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2147       nks = nks-\/1}
\DoxyCodeLine{2148 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2149 }
\DoxyCodeLine{2150     \textcolor{comment}{!   Check again whether the layers are sorted, and go on to the next column}}
\DoxyCodeLine{2151     \textcolor{comment}{! if they are.}}
\DoxyCodeLine{2152     sorted = .true.}
\DoxyCodeLine{2153     \textcolor{keywordflow}{do} ks=1,nks-\/1 ; \textcolor{keywordflow}{if} (ks2(ks) > ks2(ks+1)) sorted = .false. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2154     \textcolor{keywordflow}{if} (sorted) cycle}
\DoxyCodeLine{2155 }
\DoxyCodeLine{2156     \textcolor{keywordflow}{if} (nks > 1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2157       \textcolor{comment}{! At this point, actually swap the properties of the layers, and place}}
\DoxyCodeLine{2158       \textcolor{comment}{! the remaining layers in order starting with nkmb.}}
\DoxyCodeLine{2159 }
\DoxyCodeLine{2160       \textcolor{comment}{! Save all the properties of the nkmb layers that might be replaced.}}
\DoxyCodeLine{2161       \textcolor{keywordflow}{do} k=1,nkmb}
\DoxyCodeLine{2162         h\_tmp(k) = h(i,k) ; r0\_tmp(k) = r0(i,k)}
\DoxyCodeLine{2163         t\_tmp(k) = t(i,k) ; s\_tmp(k) = s(i,k) ; rcv\_tmp(k) = rcv(i,k)}
\DoxyCodeLine{2164 }
\DoxyCodeLine{2165         h(i,k) = 0.0}
\DoxyCodeLine{2166 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{2167 }
\DoxyCodeLine{2168       \textcolor{keywordflow}{do} ks=nks,1,-\/1}
\DoxyCodeLine{2169         k\_tgt = nkmb -\/ nks + ks ; k\_src = ks2(ks)}
\DoxyCodeLine{2170         \textcolor{keywordflow}{if} (k\_tgt == k\_src) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2171           h(i,k\_tgt) = h\_tmp(k\_tgt)  \textcolor{comment}{! This layer doesn't move, so put the water back.}}
\DoxyCodeLine{2172           cycle}
\DoxyCodeLine{2173 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2174 }
\DoxyCodeLine{2175         \textcolor{comment}{! Note below that eps=0 for k<=nkmb.}}
\DoxyCodeLine{2176         \textcolor{keywordflow}{if} (k\_src > nkmb) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2177           h\_move = h(i,k\_src)-\/eps(i,k\_src)}
\DoxyCodeLine{2178           h(i,k\_src) = eps(i,k\_src)}
\DoxyCodeLine{2179           h(i,k\_tgt) = h\_move}
\DoxyCodeLine{2180           r0(i,k\_tgt) = r0(i,k\_src)}
\DoxyCodeLine{2181 }
\DoxyCodeLine{2182           t(i,k\_tgt) = t(i,k\_src) ; s(i,k\_tgt) = s(i,k\_src)}
\DoxyCodeLine{2183           rcv(i,k\_tgt) = rcv(i,k\_src)}
\DoxyCodeLine{2184 }
\DoxyCodeLine{2185           d\_eb(i,k\_src) = d\_eb(i,k\_src) -\/ h\_move}
\DoxyCodeLine{2186           d\_eb(i,k\_tgt) = d\_eb(i,k\_tgt) + h\_move}
\DoxyCodeLine{2187         \textcolor{keywordflow}{else}}
\DoxyCodeLine{2188           h(i,k\_tgt) = h\_tmp(k\_src)}
\DoxyCodeLine{2189           r0(i,k\_tgt) = r0\_tmp(k\_src)}
\DoxyCodeLine{2190 }
\DoxyCodeLine{2191           t(i,k\_tgt) = t\_tmp(k\_src) ; s(i,k\_tgt) = s\_tmp(k\_src)}
\DoxyCodeLine{2192           rcv(i,k\_tgt) = rcv\_tmp(k\_src)}
\DoxyCodeLine{2193 }
\DoxyCodeLine{2194           \textcolor{keywordflow}{if} (k\_src > k\_tgt) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2195             d\_eb(i,k\_src) = d\_eb(i,k\_src) -\/ h\_tmp(k\_src)}
\DoxyCodeLine{2196             d\_eb(i,k\_tgt) = d\_eb(i,k\_tgt) + h\_tmp(k\_src)}
\DoxyCodeLine{2197           \textcolor{keywordflow}{else}}
\DoxyCodeLine{2198             d\_ea(i,k\_src) = d\_ea(i,k\_src) -\/ h\_tmp(k\_src)}
\DoxyCodeLine{2199             d\_ea(i,k\_tgt) = d\_ea(i,k\_tgt) + h\_tmp(k\_src)}
\DoxyCodeLine{2200 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{2201 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2202 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{2203 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2204 }
\DoxyCodeLine{2205 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2206 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_ae4325155d260533b923ba910557945f3}\label{namespacemom__bulk__mixed__layer_ae4325155d260533b923ba910557945f3}} 
\index{mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}!sort\_ml@{sort\_ml}}
\index{sort\_ml@{sort\_ml}!mom\_bulk\_mixed\_layer@{mom\_bulk\_mixed\_layer}}
\doxysubsubsection{\texorpdfstring{sort\_ml()}{sort\_ml()}}
{\footnotesize\ttfamily subroutine mom\+\_\+bulk\+\_\+mixed\+\_\+layer\+::sort\+\_\+ml (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{h,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{R0,  }\item[{real, dimension( g \%isd\+: g \%ied, gv \%ke), intent(in)}]{eps,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(\mbox{\hyperlink{structmom__bulk__mixed__layer_1_1bulkmixedlayer__cs}{bulkmixedlayer\+\_\+cs}}), pointer}]{CS,  }\item[{integer, dimension( g \%isd\+: g \%ied, gv \%ke), intent(out)}]{ksort }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine generates an array of indices that are sorted by layer density. 


\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 thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em r0} & The potential density used to sort the layers \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em eps} & The (small) thickness that must remain in each layer \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
 & {\em cs} & The control structure returned by a previous call to mixedlayer\+\_\+init. \\
\hline
\mbox{\texttt{ out}}  & {\em ksort} & The k-\/index to use in the sort. \\
\hline
\end{DoxyParams}


Definition at line 1841 of file M\+O\+M\+\_\+bulk\+\_\+mixed\+\_\+layer.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1841   \textcolor{keywordtype}{type}(ocean\_grid\_type),                \textcolor{keywordtype}{intent(in)}  :: G\textcolor{comment}{     !< The ocean's grid structure.}}
\DoxyCodeLine{1842   \textcolor{keywordtype}{type}(verticalGrid\_type),              \textcolor{keywordtype}{intent(in)}  :: GV\textcolor{comment}{    !< The ocean's vertical grid structure.}}
\DoxyCodeLine{1843 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},    \textcolor{keywordtype}{intent(in)}  :: h\textcolor{comment}{     !< Layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1844 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},    \textcolor{keywordtype}{intent(in)}  :: R0\textcolor{comment}{    !< The potential density used to sort}}
\DoxyCodeLine{1845 \textcolor{comment}{                                                             !! the layers [R \string~> kg m-\/3].}}
\DoxyCodeLine{1846 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},    \textcolor{keywordtype}{intent(in)}  :: eps\textcolor{comment}{   !< The (small) thickness that must}}
\DoxyCodeLine{1847 \textcolor{comment}{                                                             !! remain in each layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1848   \textcolor{keywordtype}{type}(bulkmixedlayer\_CS),              \textcolor{keywordtype}{pointer}     :: CS\textcolor{comment}{    !< The control structure returned by a}}
\DoxyCodeLine{1849 \textcolor{comment}{                                                             !! previous call to mixedlayer\_init.}}
\DoxyCodeLine{1850   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(out)} :: ksort\textcolor{comment}{ !< The k-\/index to use in the sort.}}
\DoxyCodeLine{1851 }
\DoxyCodeLine{1852   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1853 \textcolor{keywordtype}{  real} :: R0sort(SZI\_(G),SZK\_(GV))}
\DoxyCodeLine{1854   \textcolor{keywordtype}{integer} :: nsort(SZI\_(G))}
\DoxyCodeLine{1855   \textcolor{keywordtype}{logical} :: done\_sorting(SZI\_(G))}
\DoxyCodeLine{1856   \textcolor{keywordtype}{integer} :: i, k, ks, is, ie, nz, nkmb}
\DoxyCodeLine{1857 }
\DoxyCodeLine{1858   is = g\%isc ; ie = g\%iec ; nz = gv\%ke}
\DoxyCodeLine{1859   nkmb = cs\%nkml+cs\%nkbl}
\DoxyCodeLine{1860 }
\DoxyCodeLine{1861   \textcolor{comment}{!   Come up with a sorted index list of layers with increasing R0.}}
\DoxyCodeLine{1862   \textcolor{comment}{! Assume that the layers below nkmb are already stably stratified.}}
\DoxyCodeLine{1863   \textcolor{comment}{! Only layers that are thicker than eps are in the list.  Extra elements}}
\DoxyCodeLine{1864   \textcolor{comment}{! have an index of -\/1.}}
\DoxyCodeLine{1865 }
\DoxyCodeLine{1866   \textcolor{comment}{!   This is coded using straight insertion, on the assumption that the}}
\DoxyCodeLine{1867   \textcolor{comment}{! layers are usually in the right order (or massless) anyway.}}
\DoxyCodeLine{1868 }
\DoxyCodeLine{1869   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; ksort(i,k) = -\/1 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1870 }
\DoxyCodeLine{1871   \textcolor{keywordflow}{do} i=is,ie ; nsort(i) = 0 ; done\_sorting(i) = .false. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1872   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (h(i,k) > eps(i,k)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1873     \textcolor{keywordflow}{if} (done\_sorting(i)) \textcolor{keywordflow}{then} ; ks = nsort(i) ; \textcolor{keywordflow}{else}}
\DoxyCodeLine{1874       \textcolor{keywordflow}{do} ks=nsort(i),1,-\/1}
\DoxyCodeLine{1875         \textcolor{keywordflow}{if} (r0(i,k) >= r0sort(i,ks)) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1876         r0sort(i,ks+1) = r0sort(i,ks) ; ksort(i,ks+1) = ksort(i,ks)}
\DoxyCodeLine{1877 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1878       \textcolor{keywordflow}{if} ((k > nkmb) .and. (ks == nsort(i))) done\_sorting(i) = .true.}
\DoxyCodeLine{1879 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1880 }
\DoxyCodeLine{1881     ksort(i,ks+1) = k}
\DoxyCodeLine{1882     r0sort(i,ks+1) = r0(i,k)}
\DoxyCodeLine{1883     nsort(i) = nsort(i) + 1}
\DoxyCodeLine{1884 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1885 }

\end{DoxyCode}
