\hypertarget{namespacemom__bulk__mixed__layer}{}\section{mom\+\_\+bulk\+\_\+mixed\+\_\+layer Module Reference}
\label{namespacemom__bulk__mixed__layer}\index{mom\+\_\+bulk\+\_\+mixed\+\_\+layer@{mom\+\_\+bulk\+\_\+mixed\+\_\+layer}}


\subsection{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. \subsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \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}
\subsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \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 \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 \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 \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 \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 \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 \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 \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 \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 \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 \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}
\subsection*{Variables}
\textbf{ }\par
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_a834f8c8f259f53f30654ad9273eee648}\label{namespacemom__bulk__mixed__layer_a834f8c8f259f53f30654ad9273eee648}} 
integer \hyperlink{namespacemom__bulk__mixed__layer_a834f8c8f259f53f30654ad9273eee648}{id\+\_\+clock\+\_\+detrain} =0
\begin{DoxyCompactList}\small\item\em C\+PU clock I\+Ds. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_afadd63132ed4d2785d2771dfe33e64d8}\label{namespacemom__bulk__mixed__layer_afadd63132ed4d2785d2771dfe33e64d8}} 
integer \hyperlink{namespacemom__bulk__mixed__layer_afadd63132ed4d2785d2771dfe33e64d8}{id\+\_\+clock\+\_\+mech} =0
\begin{DoxyCompactList}\small\item\em C\+PU clock I\+Ds. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_aaeabff634c035ff4b91e5ea695123a34}\label{namespacemom__bulk__mixed__layer_aaeabff634c035ff4b91e5ea695123a34}} 
integer \hyperlink{namespacemom__bulk__mixed__layer_aaeabff634c035ff4b91e5ea695123a34}{id\+\_\+clock\+\_\+conv} =0
\begin{DoxyCompactList}\small\item\em C\+PU clock I\+Ds. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_a8e92dc75114e33498ee9318edbb784d4}\label{namespacemom__bulk__mixed__layer_a8e92dc75114e33498ee9318edbb784d4}} 
integer \hyperlink{namespacemom__bulk__mixed__layer_a8e92dc75114e33498ee9318edbb784d4}{id\+\_\+clock\+\_\+adjustment} =0
\begin{DoxyCompactList}\small\item\em C\+PU clock I\+Ds. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_a238ec53eac28b48ba8ab226296b363f0}\label{namespacemom__bulk__mixed__layer_a238ec53eac28b48ba8ab226296b363f0}} 
integer \hyperlink{namespacemom__bulk__mixed__layer_a238ec53eac28b48ba8ab226296b363f0}{id\+\_\+clock\+\_\+eos} =0
\begin{DoxyCompactList}\small\item\em C\+PU clock I\+Ds. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_aa27ed6a6317d7f8031d7c0207fe42c52}\label{namespacemom__bulk__mixed__layer_aa27ed6a6317d7f8031d7c0207fe42c52}} 
integer \hyperlink{namespacemom__bulk__mixed__layer_aa27ed6a6317d7f8031d7c0207fe42c52}{id\+\_\+clock\+\_\+resort} =0
\begin{DoxyCompactList}\small\item\em C\+PU clock I\+Ds. \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__bulk__mixed__layer_af833f821fed6dc29921f131d80a27b6e}\label{namespacemom__bulk__mixed__layer_af833f821fed6dc29921f131d80a27b6e}} 
integer \hyperlink{namespacemom__bulk__mixed__layer_af833f821fed6dc29921f131d80a27b6e}{id\+\_\+clock\+\_\+pass} =0
\begin{DoxyCompactList}\small\item\em C\+PU clock I\+Ds. \end{DoxyCompactList}\end{DoxyCompactItemize}



\subsection{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}}
\subsubsection{\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(\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{\tt in,out}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in,out}  & {\em h\+\_\+3d} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em u\+\_\+3d} & Zonal velocities interpolated to h points\\
\hline
\mbox{\tt in}  & {\em v\+\_\+3d} & Zonal velocities interpolated to h points\\
\hline
\mbox{\tt in,out}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs.\\
\hline
\mbox{\tt in,out}  & {\em fluxes} & A structure containing pointers to any possible forcing fields. Unused fields have N\+U\+LL ptrs.\\
\hline
\mbox{\tt in}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em ea} & The amount of fluid moved downward into a\\
\hline
\mbox{\tt 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{\tt 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{\tt 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{\tt 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}
190   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(inout)} :: g\textcolor{comment}{      !< The ocean's grid structure.}
191   \textcolor{keywordtype}{type}(verticalgrid\_type),    \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{     !< The ocean's vertical grid structure.}
192   \textcolor{keywordtype}{type}(unit\_scale\_type),      \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{     !< A dimensional unit scaling type}
193   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, &
194                               \textcolor{keywordtype}{intent(inout)} :: h\_3d\textcolor{comment}{   !< Layer thickness [H ~> m or kg m-2].}
195   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, &
196                               \textcolor{keywordtype}{intent(in)}    :: u\_3d\textcolor{comment}{   !< Zonal velocities interpolated to h points}
197 \textcolor{comment}{                                                      !! [L T-1 ~> m s-1].}
198   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, &
199                               \textcolor{keywordtype}{intent(in)}    :: v\_3d\textcolor{comment}{   !< Zonal velocities interpolated to h points}
200 \textcolor{comment}{                                                      !! [L T-1 ~> m s-1].}
201   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),      \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{     !< A structure containing pointers to any}
202 \textcolor{comment}{                                                      !! available thermodynamic fields. Absent}
203 \textcolor{comment}{                                                      !! fields have NULL ptrs.}
204   \textcolor{keywordtype}{type}(forcing),              \textcolor{keywordtype}{intent(inout)} :: fluxes\textcolor{comment}{ !< A structure containing pointers to any}
205 \textcolor{comment}{                                                      !! possible forcing fields.  Unused fields}
206 \textcolor{comment}{                                                      !! have NULL ptrs.}
207   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{     !< Time increment [T ~> s].}
208   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, &
209                               \textcolor{keywordtype}{intent(inout)} :: ea\textcolor{comment}{     !< The amount of fluid moved downward into a}
210 \textcolor{comment}{                                                      !! layer; this should be increased due to}
211 \textcolor{comment}{                                                      !! mixed layer detrainment [H ~> m or kg m-2].}
212   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, &
213                               \textcolor{keywordtype}{intent(inout)} :: eb\textcolor{comment}{     !< The amount of fluid moved upward into a}
214 \textcolor{comment}{                                                      !! layer; this should be increased due to}
215 \textcolor{comment}{                                                      !! mixed layer entrainment [H ~> m or kg m-2].}
216   \textcolor{keywordtype}{type}(bulkmixedlayer\_cs),    \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{     !< The control structure returned by a}
217 \textcolor{comment}{                                                      !! previous call to mixedlayer\_init.}
218   \textcolor{keywordtype}{type}(optics\_type),          \textcolor{keywordtype}{pointer}       :: optics\textcolor{comment}{ !< The structure containing the inverse of the}
219 \textcolor{comment}{                                                      !! vertical absorption decay scale for}
220 \textcolor{comment}{                                                      !! penetrating shortwave radiation [m-1].}
221   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:,:)},       \textcolor{keywordtype}{pointer}       :: hml\textcolor{comment}{    !< Active mixed layer depth [Z ~> m].}
222   \textcolor{keywordtype}{logical},                    \textcolor{keywordtype}{intent(in)}    :: aggregate\_fw\_forcing\textcolor{comment}{ !< If true, the net incoming and}
223 \textcolor{comment}{                                                     !! outgoing surface freshwater fluxes are}
224 \textcolor{comment}{                                                     !! combined before being applied, instead of}
225 \textcolor{comment}{                                                     !! being applied separately.}
226   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: dt\_diag\textcolor{comment}{  !< The diagnostic time step,}
227 \textcolor{comment}{                                                      !! which may be less than dt if there are}
228 \textcolor{comment}{                                                      !! two callse to mixedlayer [T ~> s].}
229   \textcolor{keywordtype}{logical},          \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: last\_call\textcolor{comment}{ !< if true, this is the last call}
230 \textcolor{comment}{                                                      !! to mixedlayer in the current time step, so}
231 \textcolor{comment}{                                                      !! diagnostics will be written. The default is}
232 \textcolor{comment}{                                                      !! .true.}
233 
234   \textcolor{comment}{! Local variables}
235   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))} :: &
236     eaml, &     \textcolor{comment}{!   The amount of fluid moved downward into a layer due to mixed}
237                 \textcolor{comment}{! layer detrainment [H ~> m or kg m-2]. (I.e. entrainment from above.)}
238     ebml        \textcolor{comment}{!   The amount of fluid moved upward into a layer due to mixed}
239                 \textcolor{comment}{! layer detrainment [H ~> m or kg m-2]. (I.e. entrainment from below.)}
240 
241   \textcolor{comment}{! If there is resorting, the vertical coordinate for these variables is the}
242   \textcolor{comment}{! new, sorted index space.  Here layer 0 is an initially massless layer that}
243   \textcolor{comment}{! will be used to hold the new mixed layer properties.}
244   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))} :: &
245     h, &        \textcolor{comment}{!   The layer thickness [H ~> m or kg m-2].}
246     t, &        \textcolor{comment}{!   The layer temperatures [degC].}
247     s, &        \textcolor{comment}{!   The layer salinities [ppt].}
248     r0, &       \textcolor{comment}{!   The potential density referenced to the surface [R ~> kg m-3].}
249     rcv         \textcolor{comment}{!   The coordinate variable potential density [R ~> kg m-3].}
250   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))} :: &
251     u, &        \textcolor{comment}{!   The zonal velocity [L T-1 ~> m s-1].}
252     v, &        \textcolor{comment}{!   The meridional velocity [L T-1 ~> m s-1].}
253     h\_orig, &   \textcolor{comment}{!   The original thickness [H ~> m or kg m-2].}
254     d\_eb, &     \textcolor{comment}{!   The downward increase across a layer in the entrainment from}
255                 \textcolor{comment}{! below [H ~> m or kg m-2].  The sign convention is that positive values of}
256                 \textcolor{comment}{! d\_eb correspond to a gain in mass by a layer by upward motion.}
257     d\_ea, &     \textcolor{comment}{!   The upward increase across a layer in the entrainment from}
258                 \textcolor{comment}{! above [H ~> m or kg m-2].  The sign convention is that positive values of}
259                 \textcolor{comment}{! d\_ea mean a net gain in mass by a layer from downward motion.}
260     eps         \textcolor{comment}{! The (small) thickness that must remain in a layer [H ~> m or kg m-2].}
261   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))} :: &
262     ksort       \textcolor{comment}{!   The sorted k-index that each original layer goes to.}
263   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: &
264     h\_miss      \textcolor{comment}{!   The summed absolute mismatch [Z ~> m].}
265   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: &
266     tke, &      \textcolor{comment}{!   The turbulent kinetic energy available for mixing over a}
267                 \textcolor{comment}{! time step [Z L2 T-2 ~> m3 s-2].}
268     conv\_en, &  \textcolor{comment}{!   The turbulent kinetic energy source due to mixing down to}
269                 \textcolor{comment}{! the depth of free convection [Z L2 T-2 ~> m3 s-2].}
270     htot, &     \textcolor{comment}{!   The total depth of the layers being considered for}
271                 \textcolor{comment}{! entrainment [H ~> m or kg m-2].}
272     r0\_tot, &   \textcolor{comment}{!   The integrated potential density referenced to the surface}
273                 \textcolor{comment}{! of the layers which are fully entrained [H R ~> kg m-2 or kg2 m-5].}
274     rcv\_tot, &  \textcolor{comment}{!   The integrated coordinate value potential density of the}
275                 \textcolor{comment}{! layers that are fully entrained [H R ~> kg m-2 or kg2 m-5].}
276     ttot, &     \textcolor{comment}{!   The integrated temperature of layers which are fully}
277                 \textcolor{comment}{! entrained [degC H ~> degC m or degC kg m-2].}
278     stot, &     \textcolor{comment}{!   The integrated salt of layers which are fully entrained}
279                 \textcolor{comment}{! [H ppt ~> m ppt or ppt kg m-2].}
280     uhtot, &    \textcolor{comment}{!   The depth integrated zonal and meridional velocities in the}
281     vhtot, &    \textcolor{comment}{! mixed layer [H L T-1 ~> m2 s-1 or kg m-1 s-1].}
282 
283     netmassinout, &  \textcolor{comment}{! The net mass flux (if non-Boussinsq) or volume flux (if}
284                      \textcolor{comment}{! Boussinesq - i.e. the fresh water flux (P+R-E)) into the}
285                      \textcolor{comment}{! ocean over a time step [H ~> m or kg m-2].}
286     netmassout,   &  \textcolor{comment}{! The mass flux (if non-Boussinesq) or volume flux (if Boussinesq)}
287                      \textcolor{comment}{! over a time step from evaporating fresh water [H ~> m or kg m-2]}
288     net\_heat, & \textcolor{comment}{!   The net heating at the surface over a time step [degC H ~> degC m or degC kg m-2].}
289                 \textcolor{comment}{! Any penetrating shortwave radiation is not included in Net\_heat.}
290     net\_salt, & \textcolor{comment}{! The surface salt flux into the ocean over a time step, ppt H.}
291     idecay\_len\_tke, &  \textcolor{comment}{! The inverse of a turbulence decay length scale [H-1 ~> m-1 or m2 kg-1].}
292     p\_ref, &    \textcolor{comment}{!   Reference pressure for the potential density governing mixed}
293                 \textcolor{comment}{! layer dynamics, almost always 0 (or 1e5) [R L2 T-2 ~> Pa].}
294     p\_ref\_cv, & \textcolor{comment}{!   Reference pressure for the potential density which defines}
295                 \textcolor{comment}{! the coordinate variable, set to P\_Ref [R L2 T-2 ~> Pa].}
296     dr0\_dt, &   \textcolor{comment}{!   Partial derivative of the mixed layer potential density with}
297                 \textcolor{comment}{! temperature [R degC-1 ~> kg m-3 degC-1].}
298     drcv\_dt, &  \textcolor{comment}{!   Partial derivative of the coordinate variable potential}
299                 \textcolor{comment}{! density in the mixed layer with temperature [R degC-1 ~> kg m-3 degC-1].}
300     dr0\_ds, &   \textcolor{comment}{!   Partial derivative of the mixed layer potential density with}
301                 \textcolor{comment}{! salinity [R ppt-1 ~> kg m-3 ppt-1].}
302     drcv\_ds, &  \textcolor{comment}{!   Partial derivative of the coordinate variable potential}
303                 \textcolor{comment}{! density in the mixed layer with salinity [R ppt-1 ~> kg m-3 ppt-1].}
304     tke\_river   \textcolor{comment}{! The source of turbulent kinetic energy available for mixing}
305                 \textcolor{comment}{! at rivermouths [Z L2 T-3 ~> m3 s-3].}
306 
307   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(max(CS%nsw,1),SZI\_(G))} :: &
308     pen\_sw\_bnd  \textcolor{comment}{!   The penetrating fraction of the shortwave heating integrated}
309                 \textcolor{comment}{! over a time step in each band [degC H ~> degC m or degC kg m-2].}
310   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(max(CS%nsw,1),SZI\_(G),SZK\_(GV))} :: &
311     opacity\_band \textcolor{comment}{! The opacity in each band [H-1 ~> m-1 or m2 kg-1]. The indicies are band, i, k.}
312 
313   \textcolor{keywordtype}{real} :: cmke(2,szi\_(g)) \textcolor{comment}{! Coefficients of HpE and HpE^2 used in calculating the}
314                           \textcolor{comment}{! denominator of MKE\_rate; the two elements have differing}
315                           \textcolor{comment}{! units of [H-1 ~> m-1 or m2 kg-1] and [H-2 ~> m-2 or m4 kg-2].}
316   \textcolor{keywordtype}{real} :: irho0         \textcolor{comment}{! 1.0 / rho\_0 [R-1 ~> m3 kg-1]}
317   \textcolor{keywordtype}{real} :: inkml, inkmlm1\textcolor{comment}{!  1.0 / REAL(nkml) and  1.0 / REAL(nkml-1)}
318   \textcolor{keywordtype}{real} :: ih            \textcolor{comment}{!   The inverse of a thickness [H-1 ~> m-1 or m2 kg-1].}
319   \textcolor{keywordtype}{real} :: idt\_diag      \textcolor{comment}{!   The inverse of the timestep used for diagnostics [T-1 ~> s-1].}
320   \textcolor{keywordtype}{real} :: rmixconst
321 
322   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: &
323     dke\_fc, &   \textcolor{comment}{!   The change in mean kinetic energy due to free convection}
324                 \textcolor{comment}{! [Z L2 T-2 ~> m3 s-2].}
325     h\_ca        \textcolor{comment}{!   The depth to which convective adjustment has gone [H ~> m or kg m-2].}
326   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))} :: &
327     dke\_ca, &   \textcolor{comment}{!   The change in mean kinetic energy due to convective}
328                 \textcolor{comment}{! adjustment [Z L2 T-2 ~> m3 s-2].}
329     ctke        \textcolor{comment}{!   The turbulent kinetic energy source due to convective}
330                 \textcolor{comment}{! adjustment [Z L2 T-2 ~> m3 s-2].}
331   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: &
332     hsfc\_max, & \textcolor{comment}{! The thickness of the surface region (mixed and buffer layers)}
333                 \textcolor{comment}{! after entrainment but before any buffer layer detrainment [Z ~> m].}
334     hsfc\_used, & \textcolor{comment}{! The thickness of the surface region after buffer layer}
335                 \textcolor{comment}{! detrainment [Z ~> m].}
336     hsfc\_min, & \textcolor{comment}{! The minimum thickness of the surface region based on the}
337                 \textcolor{comment}{! new mixed layer depth and the previous thickness of the}
338                 \textcolor{comment}{! neighboring water columns [Z ~> m].}
339     h\_sum, &    \textcolor{comment}{! The total thickness of the water column [H ~> m or kg m-2].}
340     hmbl\_prev   \textcolor{comment}{! The previous thickness of the mixed and buffer layers [H ~> m or kg m-2].}
341   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: &
342     hsfc, &     \textcolor{comment}{!   The thickness of the surface region (mixed and buffer}
343                 \textcolor{comment}{! layers before detrainment in to the interior [H ~> m or kg m-2].}
344     max\_bl\_det  \textcolor{comment}{!   If non-negative, the maximum amount of entrainment from}
345                 \textcolor{comment}{! the buffer layers that will be allowed this time step [H ~> m or kg m-2].}
346   \textcolor{keywordtype}{real} :: dhsfc, dhd \textcolor{comment}{! Local copies of nondimensional parameters.}
347   \textcolor{keywordtype}{real} :: h\_nbr \textcolor{comment}{! A minimum thickness based on neighboring thicknesses [H ~> m or kg m-2].}
348 
349   \textcolor{keywordtype}{real} :: absf\_x\_h  \textcolor{comment}{! The absolute value of f times the mixed layer thickness [Z T-1 ~> m s-1].}
350   \textcolor{keywordtype}{real} :: ku\_star   \textcolor{comment}{! Ustar times the Von Karmen constant [Z T-1 ~> m s-1].}
351   \textcolor{keywordtype}{real} :: dt\_\_diag  \textcolor{comment}{! A recaled copy of dt\_diag (if present) or dt [T ~> s].}
352   \textcolor{keywordtype}{logical} :: write\_diags  \textcolor{comment}{! If true, write out diagnostics with this step.}
353   \textcolor{keywordtype}{logical} :: reset\_diags  \textcolor{comment}{! If true, zero out the accumulated diagnostics.}
354   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: eosdom \textcolor{comment}{! The i-computational domain for the equation of state}
355   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz, nkmb, n
356   \textcolor{keywordtype}{integer} :: nsw    \textcolor{comment}{! The number of bands of penetrating shortwave radiation.}
357 
358   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec ; nz = gv%ke
359 
360   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_mixed\_layer: "}//&
361          \textcolor{stringliteral}{"Module must be initialized before it is used."})
362   \textcolor{keywordflow}{if} (gv%nkml < 1) \textcolor{keywordflow}{return}
363 
364   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(tv%eqn\_of\_state)) \textcolor{keyword}{call }mom\_error(fatal, &
365       \textcolor{stringliteral}{"MOM\_mixed\_layer: Temperature, salinity and an equation of state "}//&
366       \textcolor{stringliteral}{"must now be used."})
367   \textcolor{keywordflow}{if} (.NOT. \textcolor{keyword}{associated}(fluxes%ustar)) \textcolor{keyword}{call }mom\_error(fatal, &
368       \textcolor{stringliteral}{"MOM\_mixed\_layer: No surface TKE fluxes (ustar) defined in mixedlayer!"})
369 
370   nkmb = cs%nkml+cs%nkbl
371   inkml = 1.0 / \textcolor{keywordtype}{REAL}(cs%nkml)
372   \textcolor{keywordflow}{if} (cs%nkml > 1) inkmlm1 = 1.0 / \textcolor{keywordtype}{REAL}(cs%nkml-1)
373 
374   irho0 = 1.0 / (gv%Rho0)
375   dt\_\_diag = dt ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dt\_diag)) dt\_\_diag = dt\_diag
376   idt\_diag = 1.0 / (dt\_\_diag)
377   write\_diags = .true. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(last\_call)) write\_diags = last\_call
378 
379   p\_ref(:) = 0.0 ; p\_ref\_cv(:) = tv%P\_Ref
380 
381   nsw = cs%nsw
382 
383   \textcolor{keywordflow}{if} (cs%limit\_det .or. (cs%id\_Hsfc\_min > 0)) \textcolor{keywordflow}{then}
384     \textcolor{comment}{!$OMP parallel do default(shared)}
385     \textcolor{keywordflow}{do} j=js-1,je+1 ; \textcolor{keywordflow}{do} i=is-1,ie+1
386       h\_sum(i,j) = 0.0 ; hmbl\_prev(i,j) = 0.0
387 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
388     \textcolor{comment}{!$OMP parallel do default(shared)}
389     \textcolor{keywordflow}{do} j=js-1,je+1
390       \textcolor{keywordflow}{do} k=1,nkmb ; \textcolor{keywordflow}{do} i=is-1,ie+1
391         h\_sum(i,j) = h\_sum(i,j) + h\_3d(i,j,k)
392         hmbl\_prev(i,j) = hmbl\_prev(i,j) + h\_3d(i,j,k)
393 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
394       \textcolor{keywordflow}{do} k=nkmb+1,nz ; \textcolor{keywordflow}{do} i=is-1,ie+1
395         h\_sum(i,j) = h\_sum(i,j) + h\_3d(i,j,k)
396 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
397 \textcolor{keywordflow}{    enddo}
398 
399     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_pass)
400     \textcolor{keyword}{call }create\_group\_pass(cs%pass\_h\_sum\_hmbl\_prev, h\_sum,g%Domain)
401     \textcolor{keyword}{call }create\_group\_pass(cs%pass\_h\_sum\_hmbl\_prev, hmbl\_prev,g%Domain)
402     \textcolor{keyword}{call }do\_group\_pass(cs%pass\_h\_sum\_hmbl\_prev, g%Domain)
403     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_pass)
404 \textcolor{keywordflow}{  endif}
405 
406   \textcolor{comment}{! Determine whether to zero out diagnostics before accumulation.}
407   reset\_diags = .true.
408   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dt\_diag) .and. write\_diags .and. (dt\_\_diag > dt)) &
409     reset\_diags = .false.  \textcolor{comment}{! This is the second call to mixedlayer.}
410 
411   \textcolor{keywordflow}{if} (reset\_diags) \textcolor{keywordflow}{then}
412     \textcolor{keywordflow}{if} (cs%TKE\_diagnostics) \textcolor{keywordflow}{then}
413       \textcolor{comment}{!$OMP parallel do default(shared)}
414       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
415         cs%diag\_TKE\_wind(i,j) = 0.0 ; cs%diag\_TKE\_RiBulk(i,j) = 0.0
416         cs%diag\_TKE\_conv(i,j) = 0.0 ; cs%diag\_TKE\_pen\_SW(i,j) = 0.0
417         cs%diag\_TKE\_mixing(i,j) = 0.0 ; cs%diag\_TKE\_mech\_decay(i,j) = 0.0
418         cs%diag\_TKE\_conv\_decay(i,j) = 0.0 ; cs%diag\_TKE\_conv\_s2(i,j) = 0.0
419 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
420 \textcolor{keywordflow}{    endif}
421     \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain)) \textcolor{keywordflow}{then}
422       \textcolor{comment}{!$OMP parallel do default(shared)}
423       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
424         cs%diag\_PE\_detrain(i,j) = 0.0
425 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
426 \textcolor{keywordflow}{    endif}
427     \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain2)) \textcolor{keywordflow}{then}
428       \textcolor{comment}{!$OMP parallel do default(shared)}
429       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
430         cs%diag\_PE\_detrain2(i,j) = 0.0
431 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
432 \textcolor{keywordflow}{    endif}
433 \textcolor{keywordflow}{  endif}
434 
435   \textcolor{keywordflow}{if} (cs%ML\_resort) \textcolor{keywordflow}{then}
436     \textcolor{keywordflow}{do} i=is,ie ; h\_ca(i) = 0.0 ;\textcolor{keywordflow}{ enddo}
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}
438 \textcolor{keywordflow}{  endif}
439   max\_bl\_det(:) = -1
440   eosdom(:) = eos\_domain(g%HI)
441 
442   \textcolor{comment}{!$OMP parallel default(shared) firstprivate(dKE\_CA,cTKE,h\_CA,max\_BL\_det,p\_ref,p\_ref\_cv) &}
443   \textcolor{comment}{!$OMP                 private(h,u,v,h\_orig,eps,T,S,opacity\_band,d\_ea,d\_eb,R0,Rcv,ksort, &}
444   \textcolor{comment}{!$OMP                         dR0\_dT,dR0\_dS,dRcv\_dT,dRcv\_dS,htot,Ttot,Stot,TKE,Conv\_en, &}
445   \textcolor{comment}{!$OMP                         RmixConst,TKE\_river,Pen\_SW\_bnd,netMassInOut,NetMassOut,   &}
446   \textcolor{comment}{!$OMP                         Net\_heat,Net\_salt,uhtot,vhtot,R0\_tot,Rcv\_tot,dKE\_FC,      &}
447   \textcolor{comment}{!$OMP                         Idecay\_len\_TKE,cMKE,Hsfc,dHsfc,dHD,H\_nbr,kU\_Star,         &}
448   \textcolor{comment}{!$OMP                         absf\_x\_H,ebml,eaml)}
449   \textcolor{comment}{!$OMP do}
450   \textcolor{keywordflow}{do} j=js,je
451     \textcolor{comment}{! Copy the thicknesses and other fields to 2-d arrays.}
452     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie
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)
454       h\_orig(i,k) = h\_3d(i,j,k)
455       eps(i,k) = 0.0 ; \textcolor{keywordflow}{if} (k > nkmb) eps(i,k) = gv%Angstrom\_H
456       t(i,k) = tv%T(i,j,k) ; s(i,k) = tv%S(i,j,k)
457 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
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)
459 
460     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie
461       d\_ea(i,k) = 0.0 ; d\_eb(i,k) = 0.0
462 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
463 
464     \textcolor{keywordflow}{if} (id\_clock\_eos>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_eos)
465     \textcolor{comment}{! Calculate an estimate of the mid-mixed layer pressure [R L2 T-2 ~> Pa]}
466     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv%p\_surf)) \textcolor{keywordflow}{then}
467       \textcolor{keywordflow}{do} i=is,ie ; p\_ref(i) = tv%p\_surf(i,j) ;\textcolor{keywordflow}{ enddo}
468     \textcolor{keywordflow}{else}
469       \textcolor{keywordflow}{do} i=is,ie ; p\_ref(i) = 0.0 ;\textcolor{keywordflow}{ enddo}
470 \textcolor{keywordflow}{    endif}
471     \textcolor{keywordflow}{do} k=1,cs%nkml ; \textcolor{keywordflow}{do} i=is,ie
472       p\_ref(i) = p\_ref(i) + 0.5*(gv%H\_to\_RZ*gv%g\_Earth)*h(i,k)
473 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
474     \textcolor{keyword}{call }calculate\_density\_derivs(t(:,1), s(:,1), p\_ref, dr0\_dt, dr0\_ds, tv%eqn\_of\_state, eosdom)
475     \textcolor{keyword}{call }calculate\_density\_derivs(t(:,1), s(:,1), p\_ref\_cv, drcv\_dt, drcv\_ds, tv%eqn\_of\_state, eosdom)
476     \textcolor{keywordflow}{do} k=1,nz
477       \textcolor{keyword}{call }calculate\_density(t(:,k), s(:,k), p\_ref, r0(:,k), tv%eqn\_of\_state, eosdom)
478       \textcolor{keyword}{call }calculate\_density(t(:,k), s(:,k), p\_ref\_cv, rcv(:,k), tv%eqn\_of\_state, eosdom)
479 \textcolor{keywordflow}{    enddo}
480     \textcolor{keywordflow}{if} (id\_clock\_eos>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_eos)
481 
482     \textcolor{keywordflow}{if} (cs%ML\_resort) \textcolor{keywordflow}{then}
483       \textcolor{keywordflow}{if} (id\_clock\_resort>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_resort)
484       \textcolor{keywordflow}{if} (cs%ML\_presort\_nz\_conv\_adj > 0) &
485         \textcolor{keyword}{call }convective\_adjustment(h(:,1:), u, v, r0(:,1:), rcv(:,1:), t(:,1:), &
486                                    s(:,1:), eps, d\_eb, dke\_ca, ctke, j, g, gv, us, cs, &
487                                    cs%ML\_presort\_nz\_conv\_adj)
488 
489       \textcolor{keyword}{call }sort\_ml(h(:,1:), r0(:,1:), eps, g, gv, cs, ksort)
490       \textcolor{keywordflow}{if} (id\_clock\_resort>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_resort)
491     \textcolor{keywordflow}{else}
492       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; ksort(i,k) = k ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
493 
494       \textcolor{keywordflow}{if} (id\_clock\_adjustment>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_adjustment)
495       \textcolor{comment}{!  Undergo instantaneous entrainment into the buffer layers and mixed layers}
496       \textcolor{comment}{! to remove hydrostatic instabilities.  Any water that is lighter than}
497       \textcolor{comment}{! currently in the mixed or buffer layer is entrained.}
498       \textcolor{keyword}{call }convective\_adjustment(h(:,1:), u, v, r0(:,1:), rcv(:,1:), t(:,1:), &
499                                  s(:,1:), eps, d\_eb, dke\_ca, ctke, j, g, gv, us, cs)
500       \textcolor{keywordflow}{do} i=is,ie ; h\_ca(i) = h(i,1) ;\textcolor{keywordflow}{ enddo}
501 
502       \textcolor{keywordflow}{if} (id\_clock\_adjustment>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_adjustment)
503 \textcolor{keywordflow}{    endif}
504 
505     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%lrunoff) .and. cs%do\_rivermix) \textcolor{keywordflow}{then}
506 
507       \textcolor{comment}{! Here we add an additional source of TKE to the mixed layer where river}
508       \textcolor{comment}{! is present to simulate unresolved estuaries. The TKE input is diagnosed}
509       \textcolor{comment}{! as follows:}
510       \textcolor{comment}{!   TKE\_river[m3 s-3] = 0.5*rivermix\_depth*g*Irho0*drho\_ds*}
511       \textcolor{comment}{!                       River*(Samb - Sriver) = CS%mstar*U\_star^3}
512       \textcolor{comment}{! where River is in units of [m s-1].}
513       \textcolor{comment}{! Samb = Ambient salinity at the mouth of the estuary}
514       \textcolor{comment}{! rivermix\_depth =  The prescribed depth over which to mix river inflow}
515       \textcolor{comment}{! drho\_ds = The gradient of density wrt salt at the ambient surface salinity.}
516       \textcolor{comment}{! Sriver = 0 (i.e. rivers are assumed to be pure freshwater)}
517       rmixconst = 0.5*cs%rivermix\_depth * gv%g\_Earth * irho0**2
518       \textcolor{keywordflow}{do} i=is,ie
519         tke\_river(i) = max(0.0, rmixconst*dr0\_ds(i)* &
520                       (fluxes%lrunoff(i,j) + fluxes%frunoff(i,j)) * s(i,1))
521 \textcolor{keywordflow}{      enddo}
522     \textcolor{keywordflow}{else}
523       \textcolor{keywordflow}{do} i=is,ie ; tke\_river(i) = 0.0 ;\textcolor{keywordflow}{ enddo}
524 \textcolor{keywordflow}{    endif}
525 
526 
527     \textcolor{keywordflow}{if} (id\_clock\_conv>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_conv)
528 
529     \textcolor{comment}{! The surface forcing is contained in the fluxes type.}
530     \textcolor{comment}{! We aggregate the thermodynamic forcing for a time step into the following:}
531     \textcolor{comment}{! netMassInOut = water [H ~> m or kg m-2] added/removed via surface fluxes}
532     \textcolor{comment}{! netMassOut   = water [H ~> m or kg m-2] removed via evaporating surface fluxes}
533     \textcolor{comment}{! net\_heat     = heat via surface fluxes [degC H ~> degC m or degC kg m-2]}
534     \textcolor{comment}{! net\_salt     = salt via surface fluxes [ppt H ~> dppt m or gSalt m-2]}
535     \textcolor{comment}{! Pen\_SW\_bnd   = components to penetrative shortwave radiation}
536     \textcolor{keyword}{call }extractfluxes1d(g, gv, us, fluxes, optics, nsw, j, dt, &
537                   cs%H\_limit\_fluxes, cs%use\_river\_heat\_content, cs%use\_calving\_heat\_content, &
538                   h(:,1:), t(:,1:), netmassinout, netmassout, net\_heat, net\_salt, pen\_sw\_bnd,&
539                   tv, aggregate\_fw\_forcing)
540 
541     \textcolor{comment}{! This subroutine causes the mixed layer to entrain to depth of free convection.}
542     \textcolor{keyword}{call }mixedlayer\_convection(h(:,1:), d\_eb, htot, ttot, stot, uhtot, vhtot, &
543                                r0\_tot, rcv\_tot, u, v, t(:,1:), s(:,1:),       &
544                                r0(:,1:), rcv(:,1:), eps,                      &
545                                dr0\_dt, drcv\_dt, dr0\_ds, drcv\_ds,              &
546                                netmassinout, netmassout, net\_heat, net\_salt,  &
547                                nsw, pen\_sw\_bnd, opacity\_band, conv\_en,        &
548                                dke\_fc, j, ksort, g, gv, us, cs, tv, fluxes, dt, &
549                                aggregate\_fw\_forcing)
550 
551     \textcolor{keywordflow}{if} (id\_clock\_conv>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_conv)
552 
553     \textcolor{comment}{!   Now the mixed layer undergoes mechanically forced entrainment.}
554     \textcolor{comment}{! The mixed layer may entrain down to the Monin-Obukhov depth if the}
555     \textcolor{comment}{! surface is becoming lighter, and is effecti1336vely detraining.}
556 
557     \textcolor{comment}{!    First the TKE at the depth of free convection that is available}
558     \textcolor{comment}{!  to drive mixing is calculated.}
559     \textcolor{keywordflow}{if} (id\_clock\_mech>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_mech)
560 
561     \textcolor{keyword}{call }find\_starting\_tke(htot, h\_ca, fluxes, conv\_en, ctke, dke\_fc, dke\_ca, &
562                            tke, tke\_river, idecay\_len\_tke, cmke, dt, idt\_diag, &
563                            j, ksort, g, gv, us, cs)
564 
565     \textcolor{comment}{! Here the mechanically driven entrainment occurs.}
566     \textcolor{keyword}{call }mechanical\_entrainment(h(:,1:), d\_eb, htot, ttot, stot, uhtot, vhtot, &
567                                 r0\_tot, rcv\_tot, u, v, t(:,1:), s(:,1:), r0(:,1:), rcv(:,1:), eps, dr0\_dt, 
      drcv\_dt, &
568                                 cmke, idt\_diag, nsw, pen\_sw\_bnd, opacity\_band, tke, &
569                                 idecay\_len\_tke, j, ksort, g, gv, us, cs)
570 
571     \textcolor{keyword}{call }absorbremainingsw(g, gv, us, h(:,1:), opacity\_band, nsw, optics, j, dt, &
572                            cs%H\_limit\_fluxes, cs%correct\_absorption, cs%absorb\_all\_SW, &
573                            t(:,1:), pen\_sw\_bnd, eps, ksort, htot, ttot)
574 
575     \textcolor{keywordflow}{if} (cs%TKE\_diagnostics) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie
576       cs%diag\_TKE\_mech\_decay(i,j) = cs%diag\_TKE\_mech\_decay(i,j) - idt\_diag * tke(i)
577 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif}
578     \textcolor{keywordflow}{if} (id\_clock\_mech>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_mech)
579 
580     \textcolor{comment}{! Calculate the homogeneous mixed layer properties and store them in layer 0.}
581     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (htot(i) > 0.0) \textcolor{keywordflow}{then}
582       ih = 1.0 / htot(i)
583       r0(i,0) = r0\_tot(i) * ih ; rcv(i,0) = rcv\_tot(i) * ih
584       t(i,0) = ttot(i) * ih ; s(i,0) = stot(i) * ih
585       h(i,0) = htot(i)
586     \textcolor{keywordflow}{else} \textcolor{comment}{! This may not ever be needed?}
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)
588       h(i,0) = htot(i)
589 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
590     \textcolor{keywordflow}{if} (write\_diags .and. \textcolor{keyword}{allocated}(cs%ML\_depth)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie
591       cs%ML\_depth(i,j) = h(i,0) * gv%H\_to\_m  \textcolor{comment}{! Rescale the diagnostic.}
592 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif}
593     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(hml)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie
594       hml(i,j) = g%mask2dT(i,j) * (h(i,0) * gv%H\_to\_Z) \textcolor{comment}{! Rescale the diagnostic for output.}
595 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif}
596 
597 \textcolor{comment}{! At this point, return water to the original layers, but constrained to}
598 \textcolor{comment}{! still be sorted.  After this point, all the water that is in massive}
599 \textcolor{comment}{! interior layers will be denser than water remaining in the mixed- and}
600 \textcolor{comment}{! buffer-layers.  To achieve this, some of these variable density layers}
601 \textcolor{comment}{! might be split between two isopycnal layers that are denser than new}
602 \textcolor{comment}{! mixed layer or any remaining water from the old mixed- or buffer-layers.}
603 \textcolor{comment}{! Alternately, if there are fewer than nkbl of the old buffer or mixed layers}
604 \textcolor{comment}{! with any mass, relatively light interior layers might be transferred to}
605 \textcolor{comment}{! these unused layers (but not currently in the code).}
606 
607     \textcolor{keywordflow}{if} (cs%ML\_resort) \textcolor{keywordflow}{then}
608       \textcolor{keywordflow}{if} (id\_clock\_resort>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_resort)
609       \textcolor{keyword}{call }resort\_ml(h(:,0:), t(:,0:), s(:,0:), r0(:,0:), rcv(:,0:), gv%Rlay(:), eps, &
610                      d\_ea, d\_eb, ksort, g, gv, cs, dr0\_dt, dr0\_ds, drcv\_dt, drcv\_ds)
611       \textcolor{keywordflow}{if} (id\_clock\_resort>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_resort)
612 \textcolor{keywordflow}{    endif}
613 
614     \textcolor{keywordflow}{if} (cs%limit\_det .or. (cs%id\_Hsfc\_max > 0) .or. (cs%id\_Hsfc\_min > 0)) \textcolor{keywordflow}{then}
615       \textcolor{keywordflow}{do} i=is,ie ; hsfc(i) = h(i,0) ;\textcolor{keywordflow}{ enddo}
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}
617 
618       \textcolor{keywordflow}{if} (cs%limit\_det .or. (cs%id\_Hsfc\_min > 0)) \textcolor{keywordflow}{then}
619         dhsfc = cs%lim\_det\_dH\_sfc ; dhd = cs%lim\_det\_dH\_bathy
620         \textcolor{keywordflow}{do} i=is,ie
621           h\_nbr = min(dhsfc*max(hmbl\_prev(i-1,j), hmbl\_prev(i+1,j), &
622                                 hmbl\_prev(i,j-1), hmbl\_prev(i,j+1)), &
623                       max(hmbl\_prev(i-1,j) - dhd*min(h\_sum(i,j),h\_sum(i-1,j)), &
624                           hmbl\_prev(i+1,j) - dhd*min(h\_sum(i,j),h\_sum(i+1,j)), &
625                           hmbl\_prev(i,j-1) - dhd*min(h\_sum(i,j),h\_sum(i,j-1)), &
626                           hmbl\_prev(i,j+1) - dhd*min(h\_sum(i,j),h\_sum(i,j+1))) )
627 
628           hsfc\_min(i,j) = gv%H\_to\_Z * max(h(i,0), min(hsfc(i), h\_nbr))
629 
630           \textcolor{keywordflow}{if} (cs%limit\_det) max\_bl\_det(i) = max(0.0, hsfc(i)-h\_nbr)
631 \textcolor{keywordflow}{        enddo}
632 \textcolor{keywordflow}{      endif}
633 
634       \textcolor{keywordflow}{if} (cs%id\_Hsfc\_max > 0) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie
635         hsfc\_max(i,j) = gv%H\_to\_Z * hsfc(i)
636 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ endif}
637 \textcolor{keywordflow}{    endif}
638 
639 \textcolor{comment}{! Move water left in the former mixed layer into the buffer layer and}
640 \textcolor{comment}{! from the buffer layer into the interior.  These steps might best be}
641 \textcolor{comment}{! treated in conjuction.}
642     \textcolor{keywordflow}{if} (id\_clock\_detrain>0) \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_detrain)
643     \textcolor{keywordflow}{if} (cs%nkbl == 1) \textcolor{keywordflow}{then}
644       \textcolor{keyword}{call }mixedlayer\_detrain\_1(h(:,0:), t(:,0:), s(:,0:), r0(:,0:), rcv(:,0:), &
645                                 gv%Rlay(:), dt, dt\_\_diag, d\_ea, d\_eb, j, g, gv, us, cs, &
646                                 drcv\_dt, drcv\_ds, max\_bl\_det)
647     \textcolor{keywordflow}{elseif} (cs%nkbl == 2) \textcolor{keywordflow}{then}
648       \textcolor{keyword}{call }mixedlayer\_detrain\_2(h(:,0:), t(:,0:), s(:,0:), r0(:,0:), rcv(:,0:), &
649                                 gv%Rlay(:), dt, dt\_\_diag, d\_ea, j, g, gv, us, cs, &
650                                 dr0\_dt, dr0\_ds, drcv\_dt, drcv\_ds, max\_bl\_det)
651     \textcolor{keywordflow}{else} \textcolor{comment}{! CS%nkbl not = 1 or 2}
652       \textcolor{comment}{! This code only works with 1 or 2 buffer layers.}
653       \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_mixed\_layer: CS%nkbl must be 1 or 2 for now."})
654 \textcolor{keywordflow}{    endif}
655     \textcolor{keywordflow}{if} (id\_clock\_detrain>0) \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_detrain)
656 
657 
658     \textcolor{keywordflow}{if} (cs%id\_Hsfc\_used > 0) \textcolor{keywordflow}{then}
659       \textcolor{keywordflow}{do} i=is,ie ; hsfc\_used(i,j) = gv%H\_to\_Z * h(i,0) ;\textcolor{keywordflow}{ enddo}
660       \textcolor{keywordflow}{do} k=cs%nkml+1,nkmb ; \textcolor{keywordflow}{do} i=is,ie
661         hsfc\_used(i,j) = hsfc\_used(i,j) + gv%H\_to\_Z * h(i,k)
662 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
663 \textcolor{keywordflow}{    endif}
664 
665 \textcolor{comment}{! Now set the properties of the layers in the mixed layer in the original}
666 \textcolor{comment}{! 3-d variables.}
667     \textcolor{keywordflow}{if} (cs%Resolve\_Ekman .and. (cs%nkml>1)) \textcolor{keywordflow}{then}
668       \textcolor{comment}{! The thickness of the topmost piece of the mixed layer is given by}
669       \textcolor{comment}{! h\_1 = H / (3 + sqrt(|f|*H^2/2*nu\_max)), which asymptotes to the Ekman}
670       \textcolor{comment}{! layer depth and 1/3 of the mixed layer depth.  This curve has been}
671       \textcolor{comment}{! determined to maximize the impact of the Ekman transport in the mixed}
672       \textcolor{comment}{! layer TKE budget with nkml=2.  With nkml=3, this should also be used,}
673       \textcolor{comment}{! as the third piece will then optimally describe mixed layer}
674       \textcolor{comment}{! restratification.  For nkml>=4 the whole strategy should be revisited.}
675       \textcolor{keywordflow}{do} i=is,ie
676         ku\_star = 0.41*fluxes%ustar(i,j) \textcolor{comment}{! Maybe could be replaced with u*+w*?}
677         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%ustar\_shelf) .and. &
678             \textcolor{keyword}{associated}(fluxes%frac\_shelf\_h)) \textcolor{keywordflow}{then}
679           \textcolor{keywordflow}{if} (fluxes%frac\_shelf\_h(i,j) > 0.0) &
680             ku\_star = (1.0 - fluxes%frac\_shelf\_h(i,j)) * ku\_star + &
681                       fluxes%frac\_shelf\_h(i,j) * (0.41*fluxes%ustar\_shelf(i,j))
682 \textcolor{keywordflow}{        endif}
683         absf\_x\_h = 0.25 * gv%H\_to\_Z * h(i,0) * &
684             ((abs(g%CoriolisBu(i,j)) + abs(g%CoriolisBu(i-1,j-1))) + &
685              (abs(g%CoriolisBu(i,j-1)) + abs(g%CoriolisBu(i-1,j))))
686         \textcolor{comment}{! If the mixed layer vertical viscosity specification is changed in}
687         \textcolor{comment}{! MOM\_vert\_friction.F90, this line will have to be modified accordingly.}
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))
689         \textcolor{keywordflow}{do} k=2,cs%nkml
690           \textcolor{comment}{! The other layers are evenly distributed through the mixed layer.}
691           h\_3d(i,j,k) = (h(i,0)-h\_3d(i,j,1)) * inkmlm1
692           d\_ea(i,k) = d\_ea(i,k) + h\_3d(i,j,k)
693           d\_ea(i,1) = d\_ea(i,1) - h\_3d(i,j,k)
694 \textcolor{keywordflow}{        enddo}
695 \textcolor{keywordflow}{      enddo}
696     \textcolor{keywordflow}{else}
697       \textcolor{keywordflow}{do} i=is,ie
698         h\_3d(i,j,1) = h(i,0) * inkml
699 \textcolor{keywordflow}{      enddo}
700       \textcolor{keywordflow}{do} k=2,cs%nkml ; \textcolor{keywordflow}{do} i=is,ie
701         h\_3d(i,j,k) = h(i,0) * inkml
702         d\_ea(i,k) = d\_ea(i,k) + h\_3d(i,j,k)
703         d\_ea(i,1) = d\_ea(i,1) - h\_3d(i,j,k)
704 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
705 \textcolor{keywordflow}{    endif}
706     \textcolor{keywordflow}{do} i=is,ie ; h(i,0) = 0.0 ;\textcolor{keywordflow}{ enddo}
707     \textcolor{keywordflow}{do} k=1,cs%nkml ; \textcolor{keywordflow}{do} i=is,ie
708       tv%T(i,j,k) = t(i,0) ; tv%S(i,j,k) = s(i,0)
709 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
710 
711     \textcolor{comment}{! These sum needs to be done in the original layer space.}
712 
713     \textcolor{comment}{! The treatment of layer 1 is atypical because evaporation shows up as}
714     \textcolor{comment}{! negative ea(i,1), and because all precipitation goes straight into layer 1.}
715     \textcolor{comment}{! The code is ordered so that any roundoff errors in ea are lost the surface.}
716 \textcolor{comment}{!    do i=is,ie ; eaml(i,1) = 0.0 ; enddo}
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}
718 \textcolor{comment}{!    do i=is,ie ; eaml(i,1) = netMassInOut(i) ; enddo}
719 
720 
721     \textcolor{keywordflow}{do} i=is,ie
722 \textcolor{comment}{! eaml(i,nz) is derived from  h(i,nz) - h\_orig(i,nz) = eaml(i,nz) - ebml(i,nz-1)}
723       ebml(i,nz) = 0.0
724       eaml(i,nz) = (h(i,nz) - h\_orig(i,nz)) - d\_eb(i,nz)
725 \textcolor{keywordflow}{    enddo}
726     \textcolor{keywordflow}{do} k=nz-1,1,-1 ; \textcolor{keywordflow}{do} i=is,ie
727       ebml(i,k) = ebml(i,k+1) - d\_eb(i,k+1)
728       eaml(i,k) = eaml(i,k+1) + d\_ea(i,k)
729 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
730     \textcolor{keywordflow}{do} i=is,ie ; eaml(i,1) = netmassinout(i) ;\textcolor{keywordflow}{ enddo}
731 
732     \textcolor{comment}{! Copy the interior thicknesses and other fields back to the 3-d arrays.}
733     \textcolor{keywordflow}{do} k=cs%nkml+1,nz ; \textcolor{keywordflow}{do} i=is,ie
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)
735 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
736 
737     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie
738       ea(i,j,k) = ea(i,j,k) + eaml(i,k)
739       eb(i,j,k) = eb(i,j,k) + ebml(i,k)
740 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
741 
742     \textcolor{keywordflow}{if} (cs%id\_h\_mismatch > 0) \textcolor{keywordflow}{then}
743       \textcolor{keywordflow}{do} i=is,ie
744         h\_miss(i,j) = gv%H\_to\_Z * abs(h\_3d(i,j,1) - (h\_orig(i,1) + &
745           (eaml(i,1) + (ebml(i,1) - eaml(i,1+1)))))
746 \textcolor{keywordflow}{      enddo}
747       \textcolor{keywordflow}{do} k=2,nz-1 ; \textcolor{keywordflow}{do} i=is,ie
748         h\_miss(i,j) = h\_miss(i,j) + gv%H\_to\_Z * abs(h\_3d(i,j,k) - (h\_orig(i,k) + &
749           ((eaml(i,k) - ebml(i,k-1)) + (ebml(i,k) - eaml(i,k+1)))))
750 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
751       \textcolor{keywordflow}{do} i=is,ie
752         h\_miss(i,j) = h\_miss(i,j) + gv%H\_to\_Z * abs(h\_3d(i,j,nz) - (h\_orig(i,nz) + &
753           ((eaml(i,nz) - ebml(i,nz-1)) + ebml(i,nz))))
754 \textcolor{keywordflow}{      enddo}
755 \textcolor{keywordflow}{    endif}
756 
757 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! j loop}
758   \textcolor{comment}{!$OMP end parallel}
759 
760   \textcolor{comment}{! Whenever thickness changes let the diag manager know, target grids}
761   \textcolor{comment}{! for vertical remapping may need to be regenerated.}
762   \textcolor{comment}{! This needs to happen after the H update and before the next post\_data.}
763   \textcolor{keyword}{call }diag\_update\_remap\_grids(cs%diag)
764 
765 
766   \textcolor{keywordflow}{if} (write\_diags) \textcolor{keywordflow}{then}
767     \textcolor{keywordflow}{if} (cs%id\_ML\_depth > 0) &
768       \textcolor{keyword}{call }post\_data(cs%id\_ML\_depth, cs%ML\_depth, cs%diag)
769     \textcolor{keywordflow}{if} (cs%id\_TKE\_wind > 0) &
770       \textcolor{keyword}{call }post\_data(cs%id\_TKE\_wind, cs%diag\_TKE\_wind, cs%diag)
771     \textcolor{keywordflow}{if} (cs%id\_TKE\_RiBulk > 0) &
772       \textcolor{keyword}{call }post\_data(cs%id\_TKE\_RiBulk, cs%diag\_TKE\_RiBulk, cs%diag)
773     \textcolor{keywordflow}{if} (cs%id\_TKE\_conv > 0) &
774       \textcolor{keyword}{call }post\_data(cs%id\_TKE\_conv, cs%diag\_TKE\_conv, cs%diag)
775     \textcolor{keywordflow}{if} (cs%id\_TKE\_pen\_SW > 0) &
776       \textcolor{keyword}{call }post\_data(cs%id\_TKE\_pen\_SW, cs%diag\_TKE\_pen\_SW, cs%diag)
777     \textcolor{keywordflow}{if} (cs%id\_TKE\_mixing > 0) &
778       \textcolor{keyword}{call }post\_data(cs%id\_TKE\_mixing, cs%diag\_TKE\_mixing, cs%diag)
779     \textcolor{keywordflow}{if} (cs%id\_TKE\_mech\_decay > 0) &
780       \textcolor{keyword}{call }post\_data(cs%id\_TKE\_mech\_decay, cs%diag\_TKE\_mech\_decay, cs%diag)
781     \textcolor{keywordflow}{if} (cs%id\_TKE\_conv\_decay > 0) &
782       \textcolor{keyword}{call }post\_data(cs%id\_TKE\_conv\_decay, cs%diag\_TKE\_conv\_decay, cs%diag)
783     \textcolor{keywordflow}{if} (cs%id\_TKE\_conv\_s2 > 0) &
784       \textcolor{keyword}{call }post\_data(cs%id\_TKE\_conv\_s2, cs%diag\_TKE\_conv\_s2, cs%diag)
785     \textcolor{keywordflow}{if} (cs%id\_PE\_detrain > 0) &
786       \textcolor{keyword}{call }post\_data(cs%id\_PE\_detrain, cs%diag\_PE\_detrain, cs%diag)
787     \textcolor{keywordflow}{if} (cs%id\_PE\_detrain2 > 0) &
788       \textcolor{keyword}{call }post\_data(cs%id\_PE\_detrain2, cs%diag\_PE\_detrain2, cs%diag)
789     \textcolor{keywordflow}{if} (cs%id\_h\_mismatch > 0) &
790       \textcolor{keyword}{call }post\_data(cs%id\_h\_mismatch, h\_miss, cs%diag)
791     \textcolor{keywordflow}{if} (cs%id\_Hsfc\_used > 0) &
792       \textcolor{keyword}{call }post\_data(cs%id\_Hsfc\_used, hsfc\_used, cs%diag)
793     \textcolor{keywordflow}{if} (cs%id\_Hsfc\_max > 0) &
794       \textcolor{keyword}{call }post\_data(cs%id\_Hsfc\_max, hsfc\_max, cs%diag)
795     \textcolor{keywordflow}{if} (cs%id\_Hsfc\_min > 0) &
796       \textcolor{keyword}{call }post\_data(cs%id\_Hsfc\_min, hsfc\_min, cs%diag)
797 \textcolor{keywordflow}{  endif}
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}}
\subsubsection{\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(\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{\tt in}  & {\em time} & The model\textquotesingle{}s clock with the current time.\\
\hline
\mbox{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em param\+\_\+file} & A structure to parse for run-\/time parameters.\\
\hline
\mbox{\tt 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}
3390   \textcolor{keywordtype}{type}(time\_type), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(in)}    :: time\textcolor{comment}{ !< The model's clock with the current time.}
3391   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: g\textcolor{comment}{    !< The ocean's grid structure.}
3392   \textcolor{keywordtype}{type}(verticalgrid\_type), \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{   !< The ocean's vertical grid structure.}
3393   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{   !< A dimensional unit scaling type}
3394   \textcolor{keywordtype}{type}(param\_file\_type),   \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< A structure to parse for run-time}
3395 \textcolor{comment}{                                                 !! parameters.}
3396   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{ !< A structure that is used to regulate diagnostic}
3397 \textcolor{comment}{                                                 !! output.}
3398   \textcolor{keywordtype}{type}(bulkmixedlayer\_cs), \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{   !< A pointer that is set to point to the control}
3399 \textcolor{comment}{                                                 !! structure for this module.}
3400 \textcolor{comment}{! Arguments: Time - The current model time.}
3401 \textcolor{comment}{!  (in)      G - The ocean's grid structure.}
3402 \textcolor{comment}{!  (in)      GV - The ocean's vertical grid structure.}
3403 \textcolor{comment}{!  (in)      param\_file - A structure indicating the open file to parse for}
3404 \textcolor{comment}{!                         model parameter values.}
3405 \textcolor{comment}{!  (in)      diag - A structure that is used to regulate diagnostic output.}
3406 \textcolor{comment}{!  (in/out)  CS - A pointer that is set to point to the control structure}
3407 \textcolor{comment}{!                  for this module}
3408 \textcolor{comment}{! This include declares and sets the variable "version".}
3409 \textcolor{preprocessor}{#include "version\_variable.h"}
3410 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_mixed\_layer"}  \textcolor{comment}{! This module's name.}
3411   \textcolor{keywordtype}{real} :: bl\_detrain\_time\_dflt \textcolor{comment}{! The default value for BUFFER\_LAY\_DETRAIN\_TIME [s]}
3412   \textcolor{keywordtype}{real} :: omega\_frac\_dflt, ustar\_min\_dflt, hmix\_min\_m
3413   \textcolor{keywordtype}{integer} :: isd, ied, jsd, jed
3414   \textcolor{keywordtype}{logical} :: use\_temperature, use\_omega
3415   isd = g%isd ; ied = g%ied ; jsd = g%jsd ; jed = g%jed
3416 
3417   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}
3418     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"mixedlayer\_init called with an associated"}// &
3419                             \textcolor{stringliteral}{"associated control structure."})
3420     \textcolor{keywordflow}{return}
3421   \textcolor{keywordflow}{else} ; \textcolor{keyword}{allocate}(cs) ;\textcolor{keywordflow}{ endif}
3422 
3423   cs%diag => diag
3424   cs%Time => time
3425 
3426   \textcolor{keywordflow}{if} (gv%nkml < 1) \textcolor{keywordflow}{return}
3427 
3428 \textcolor{comment}{! Set default, read and log parameters}
3429   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \textcolor{stringliteral}{""})
3430 
3431   cs%nkml = gv%nkml
3432   \textcolor{keyword}{call }log\_param(param\_file, mdl, \textcolor{stringliteral}{"NKML"}, cs%nkml, &
3433                  \textcolor{stringliteral}{"The number of sublayers within the mixed layer if "}//&
3434                  \textcolor{stringliteral}{"BULKMIXEDLAYER is true."}, units=\textcolor{stringliteral}{"nondim"}, default=2)
3435   cs%nkbl = gv%nk\_rho\_varies - gv%nkml
3436   \textcolor{keyword}{call }log\_param(param\_file, mdl, \textcolor{stringliteral}{"NKBL"}, cs%nkbl, &
3437                  \textcolor{stringliteral}{"The number of variable density buffer layers if "}//&
3438                  \textcolor{stringliteral}{"BULKMIXEDLAYER is true."}, units=\textcolor{stringliteral}{"nondim"}, default=2)
3439   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MSTAR"}, cs%mstar, &
3440                  \textcolor{stringliteral}{"The ratio of the friction velocity cubed to the TKE "}//&
3441                  \textcolor{stringliteral}{"input to the mixed layer."}, units=\textcolor{stringliteral}{"nondim"}, default=1.2)
3442   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NSTAR"}, cs%nstar, &
3443                  \textcolor{stringliteral}{"The portion of the buoyant potential energy imparted by "}//&
3444                  \textcolor{stringliteral}{"surface fluxes that is available to drive entrainment "}//&
3445                  \textcolor{stringliteral}{"at the base of mixed layer when that energy is positive."}, &
3446                  units=\textcolor{stringliteral}{"nondim"}, default=0.15)
3447   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BULK\_RI\_ML"}, cs%bulk\_Ri\_ML, &
3448                  \textcolor{stringliteral}{"The efficiency with which mean kinetic energy released "}//&
3449                  \textcolor{stringliteral}{"by mechanically forced entrainment of the mixed layer "}//&
3450                  \textcolor{stringliteral}{"is converted to turbulent kinetic energy."}, units=\textcolor{stringliteral}{"nondim"},&
3451                  fail\_if\_missing=.true.)
3452   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ABSORB\_ALL\_SW"}, cs%absorb\_all\_sw, &
3453                  \textcolor{stringliteral}{"If true,  all shortwave radiation is absorbed by the "}//&
3454                  \textcolor{stringliteral}{"ocean, instead of passing through to the bottom mud."}, &
3455                  default=.false.)
3456   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"TKE\_DECAY"}, cs%TKE\_decay, &
3457                  \textcolor{stringliteral}{"TKE\_DECAY relates the vertical rate of decay of the "}//&
3458                  \textcolor{stringliteral}{"TKE available for mechanical entrainment to the natural "}//&
3459                  \textcolor{stringliteral}{"Ekman depth."}, units=\textcolor{stringliteral}{"nondim"}, default=2.5)
3460   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NSTAR2"}, cs%nstar2, &
3461                  \textcolor{stringliteral}{"The portion of any potential energy released by "}//&
3462                  \textcolor{stringliteral}{"convective adjustment that is available to drive "}//&
3463                  \textcolor{stringliteral}{"entrainment at the base of mixed layer. By default "}//&
3464                  \textcolor{stringliteral}{"NSTAR2=NSTAR."}, units=\textcolor{stringliteral}{"nondim"}, default=cs%nstar)
3465   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BULK\_RI\_CONVECTIVE"}, cs%bulk\_Ri\_convective, &
3466                  \textcolor{stringliteral}{"The efficiency with which convectively released mean "}//&
3467                  \textcolor{stringliteral}{"kinetic energy is converted to turbulent kinetic "}//&
3468                  \textcolor{stringliteral}{"energy.  By default BULK\_RI\_CONVECTIVE=BULK\_RI\_ML."}, &
3469                  units=\textcolor{stringliteral}{"nondim"}, default=cs%bulk\_Ri\_ML)
3470   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HMIX\_MIN"}, cs%Hmix\_min, &
3471                  \textcolor{stringliteral}{"The minimum mixed layer depth if the mixed layer depth "}//&
3472                  \textcolor{stringliteral}{"is determined dynamically."}, units=\textcolor{stringliteral}{"m"}, default=0.0, scale=gv%m\_to\_H, &
3473                  unscaled=hmix\_min\_m)
3474 
3475   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"LIMIT\_BUFFER\_DETRAIN"}, cs%limit\_det, &
3476                  \textcolor{stringliteral}{"If true, limit the detrainment from the buffer layers "}//&
3477                  \textcolor{stringliteral}{"to not be too different from the neighbors."}, default=.false.)
3478   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ALLOWED\_DETRAIN\_TEMP\_CHG"}, cs%Allowed\_T\_chg, &
3479                  \textcolor{stringliteral}{"The amount by which temperature is allowed to exceed "}//&
3480                  \textcolor{stringliteral}{"previous values during detrainment."}, units=\textcolor{stringliteral}{"K"}, default=0.5)
3481   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ALLOWED\_DETRAIN\_SALT\_CHG"}, cs%Allowed\_S\_chg, &
3482                  \textcolor{stringliteral}{"The amount by which salinity is allowed to exceed "}//&
3483                  \textcolor{stringliteral}{"previous values during detrainment."}, units=\textcolor{stringliteral}{"PSU"}, default=0.1)
3484   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_DT\_DS\_WEIGHT"}, cs%dT\_dS\_wt, &
3485                  \textcolor{stringliteral}{"When forced to extrapolate T & S to match the layer "}//&
3486                  \textcolor{stringliteral}{"densities, this factor (in deg C / PSU) is combined "}//&
3487                  \textcolor{stringliteral}{"with the derivatives of density with T & S to determine "}//&
3488                  \textcolor{stringliteral}{"what direction is orthogonal to density contours. It "}//&
3489                  \textcolor{stringliteral}{"should be a typical value of (dR/dS) / (dR/dT) in "}//&
3490                  \textcolor{stringliteral}{"oceanic profiles."}, units=\textcolor{stringliteral}{"degC PSU-1"}, default=6.0)
3491   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BUFFER\_LAYER\_EXTRAP\_LIMIT"}, cs%BL\_extrap\_lim, &
3492                  \textcolor{stringliteral}{"A limit on the density range over which extrapolation "}//&
3493                  \textcolor{stringliteral}{"can occur when detraining from the buffer layers, "}//&
3494                  \textcolor{stringliteral}{"relative to the density range within the mixed and "}//&
3495                  \textcolor{stringliteral}{"buffer layers, when the detrainment is going into the "}//&
3496                  \textcolor{stringliteral}{"lightest interior layer, nondimensional, or a negative "}//&
3497                  \textcolor{stringliteral}{"value not to apply this limit."}, units=\textcolor{stringliteral}{"nondim"}, default = -1.0)
3498   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BUFFER\_LAYER\_HMIN\_THICK"}, cs%Hbuffer\_min, &
3499                  \textcolor{stringliteral}{"The minimum buffer layer thickness when the mixed layer is very thick."}, &
3500                  units=\textcolor{stringliteral}{"m"}, default=5.0, scale=gv%m\_to\_H)
3501   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BUFFER\_LAYER\_HMIN\_REL"}, cs%Hbuffer\_rel\_min, &
3502                  \textcolor{stringliteral}{"The minimum buffer layer thickness relative to the combined mixed "}//&
3503                  \textcolor{stringliteral}{"land buffer ayer thicknesses when they are thin."}, &
3504                  units=\textcolor{stringliteral}{"nondim"}, default=0.1/cs%nkbl)
3505   bl\_detrain\_time\_dflt = 4.0*3600.0 ; \textcolor{keywordflow}{if} (cs%nkbl==1) bl\_detrain\_time\_dflt = 86400.0*30.0
3506   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BUFFER\_LAY\_DETRAIN\_TIME"}, cs%BL\_detrain\_time, &
3507                  \textcolor{stringliteral}{"A timescale that characterizes buffer layer detrainment events."}, &
3508                  units=\textcolor{stringliteral}{"s"}, default=bl\_detrain\_time\_dflt, scale=us%s\_to\_T)
3509   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BUFFER\_SPLIT\_RHO\_TOL"}, cs%BL\_split\_rho\_tol, &
3510                  \textcolor{stringliteral}{"The fractional tolerance for matching layer target densities when splitting "}//&
3511                  \textcolor{stringliteral}{"layers to deal with massive interior layers that are lighter than one of the "}//&
3512                  \textcolor{stringliteral}{"mixed or buffer layers."}, units=\textcolor{stringliteral}{"nondim"}, default=0.1)
3513 
3514   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEPTH\_LIMIT\_FLUXES"}, cs%H\_limit\_fluxes, &
3515                  \textcolor{stringliteral}{"The surface fluxes are scaled away when the total ocean "}//&
3516                  \textcolor{stringliteral}{"depth is less than DEPTH\_LIMIT\_FLUXES."}, &
3517                  units=\textcolor{stringliteral}{"m"}, default=0.1*hmix\_min\_m, scale=gv%m\_to\_H)
3518   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"OMEGA"}, cs%omega, &
3519                  \textcolor{stringliteral}{"The rotation rate of the earth."}, &
3520                  default=7.2921e-5, units=\textcolor{stringliteral}{"s-1"}, scale=us%T\_to\_s)
3521   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_USE\_OMEGA"}, use\_omega, &
3522                  \textcolor{stringliteral}{"If true, use the absolute rotation rate instead of the "}//&
3523                  \textcolor{stringliteral}{"vertical component of rotation when setting the decay "}//&
3524                  \textcolor{stringliteral}{"scale for turbulence."}, default=.false., do\_not\_log=.true.)
3525   omega\_frac\_dflt = 0.0
3526   \textcolor{keywordflow}{if} (use\_omega) \textcolor{keywordflow}{then}
3527     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"ML\_USE\_OMEGA is depricated; use ML\_OMEGA\_FRAC=1.0 instead."})
3528     omega\_frac\_dflt = 1.0
3529 \textcolor{keywordflow}{  endif}
3530   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_OMEGA\_FRAC"}, cs%omega\_frac, &
3531                  \textcolor{stringliteral}{"When setting the decay scale for turbulence, use this "}//&
3532                  \textcolor{stringliteral}{"fraction of the absolute rotation rate blended with the "}//&
3533                  \textcolor{stringliteral}{"local value of f, as sqrt((1-of)*f^2 + of*4*omega^2)."}, &
3534                  units=\textcolor{stringliteral}{"nondim"}, default=omega\_frac\_dflt)
3535   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_RESORT"}, cs%ML\_resort, &
3536                  \textcolor{stringliteral}{"If true, resort the topmost layers by potential density "}//&
3537                  \textcolor{stringliteral}{"before the mixed layer calculations."}, default=.false.)
3538   \textcolor{keywordflow}{if} (cs%ML\_resort) &
3539     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_PRESORT\_NK\_CONV\_ADJ"}, cs%ML\_presort\_nz\_conv\_adj, &
3540                  \textcolor{stringliteral}{"Convectively mix the first ML\_PRESORT\_NK\_CONV\_ADJ "}//&
3541                  \textcolor{stringliteral}{"layers before sorting when ML\_RESORT is true."}, &
3542                  units=\textcolor{stringliteral}{"nondim"}, default=0, fail\_if\_missing=.true.) \textcolor{comment}{! Fail added by AJA.}
3543   \textcolor{comment}{! This gives a minimum decay scale that is typically much less than Angstrom.}
3544   ustar\_min\_dflt = 2e-4*us%s\_to\_T*cs%omega*(gv%Angstrom\_m + gv%H\_to\_m*gv%H\_subroundoff)
3545   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BML\_USTAR\_MIN"}, cs%ustar\_min, &
3546                  \textcolor{stringliteral}{"The minimum value of ustar that should be used by the "}//&
3547                  \textcolor{stringliteral}{"bulk mixed layer model in setting vertical TKE decay "}//&
3548                  \textcolor{stringliteral}{"scales. This must be greater than 0."}, units=\textcolor{stringliteral}{"m s-1"}, &
3549                  default=ustar\_min\_dflt, scale=us%m\_to\_Z*us%T\_to\_s)
3550   \textcolor{keywordflow}{if} (cs%ustar\_min<=0.0) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"BML\_USTAR\_MIN must be positive."})
3551 
3552   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"RESOLVE\_EKMAN"}, cs%Resolve\_Ekman, &
3553                  \textcolor{stringliteral}{"If true, the NKML>1 layers in the mixed layer are "}//&
3554                  \textcolor{stringliteral}{"chosen to optimally represent the impact of the Ekman "}//&
3555                  \textcolor{stringliteral}{"transport on the mixed layer TKE budget.  Otherwise, "}//&
3556                  \textcolor{stringliteral}{"the sublayers are distributed uniformly through the "}//&
3557                  \textcolor{stringliteral}{"mixed layer."}, default=.false.)
3558   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CORRECT\_ABSORPTION\_DEPTH"}, cs%correct\_absorption, &
3559                  \textcolor{stringliteral}{"If true, the average depth at which penetrating shortwave "}//&
3560                  \textcolor{stringliteral}{"radiation is absorbed is adjusted to match the average "}//&
3561                  \textcolor{stringliteral}{"heating depth of an exponential profile by moving some "}//&
3562                  \textcolor{stringliteral}{"of the heating upward in the water column."}, default=.false.)
3563   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DO\_RIVERMIX"}, cs%do\_rivermix, &
3564                  \textcolor{stringliteral}{"If true, apply additional mixing wherever there is "}//&
3565                  \textcolor{stringliteral}{"runoff, so that it is mixed down to RIVERMIX\_DEPTH, "}//&
3566                  \textcolor{stringliteral}{"if the ocean is that deep."}, default=.false.)
3567   \textcolor{keywordflow}{if} (cs%do\_rivermix) &
3568     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"RIVERMIX\_DEPTH"}, cs%rivermix\_depth, &
3569                  \textcolor{stringliteral}{"The depth to which rivers are mixed if DO\_RIVERMIX is "}//&
3570                  \textcolor{stringliteral}{"defined."}, units=\textcolor{stringliteral}{"m"}, default=0.0, scale=us%m\_to\_Z)
3571   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_RIVER\_HEAT\_CONTENT"}, cs%use\_river\_heat\_content, &
3572                  \textcolor{stringliteral}{"If true, use the fluxes%runoff\_Hflx field to set the "}//&
3573                  \textcolor{stringliteral}{"heat carried by runoff, instead of using SST*CP*liq\_runoff."}, &
3574                  default=.false.)
3575   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_CALVING\_HEAT\_CONTENT"}, cs%use\_calving\_heat\_content, &
3576                  \textcolor{stringliteral}{"If true, use the fluxes%calving\_Hflx field to set the "}//&
3577                  \textcolor{stringliteral}{"heat carried by runoff, instead of using SST*CP*froz\_runoff."}, &
3578                  default=.false.)
3579   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BULKML\_CONV\_MOMENTUM\_BUG"}, cs%convect\_mom\_bug, &
3580                  \textcolor{stringliteral}{"If true, use code with a bug that causes a loss of momentum conservation "}//&
3581                  \textcolor{stringliteral}{"during mixedlayer convection."}, default=.false.)
3582 
3583   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ALLOW\_CLOCKS\_IN\_OMP\_LOOPS"}, &
3584                  cs%allow\_clocks\_in\_omp\_loops, &
3585                  \textcolor{stringliteral}{"If true, clocks can be called from inside loops that can "}//&
3586                  \textcolor{stringliteral}{"be threaded. To run with multiple threads, set to False."}, &
3587                  default=.true.)
3588 
3589   cs%id\_ML\_depth = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'h\_ML'}, diag%axesT1, &
3590       time, \textcolor{stringliteral}{'Surface mixed layer depth'}, \textcolor{stringliteral}{'m'})
3591   cs%id\_TKE\_wind = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_wind'}, diag%axesT1, &
3592       time, \textcolor{stringliteral}{'Wind-stirring source of mixed layer TKE'}, &
3593       \textcolor{stringliteral}{'m3 s-3'}, conversion=us%Z\_to\_m*(us%L\_to\_m**2)*(us%s\_to\_T**3))
3594   cs%id\_TKE\_RiBulk = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_RiBulk'}, diag%axesT1, &
3595       time, \textcolor{stringliteral}{'Mean kinetic energy source of mixed layer TKE'}, &
3596       \textcolor{stringliteral}{'m3 s-3'}, conversion=us%Z\_to\_m*(us%L\_to\_m**2)*(us%s\_to\_T**3))
3597   cs%id\_TKE\_conv = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_conv'}, diag%axesT1, &
3598       time, \textcolor{stringliteral}{'Convective source of mixed layer TKE'}, &
3599       \textcolor{stringliteral}{'m3 s-3'}, conversion=us%Z\_to\_m*(us%L\_to\_m**2)*(us%s\_to\_T**3))
3600   cs%id\_TKE\_pen\_SW = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_pen\_SW'}, diag%axesT1, &
3601       time, \textcolor{stringliteral}{'TKE consumed by mixing penetrative shortwave radation through the mixed layer'}, &
3602       \textcolor{stringliteral}{'m3 s-3'}, conversion=us%Z\_to\_m*(us%L\_to\_m**2)*(us%s\_to\_T**3))
3603   cs%id\_TKE\_mixing = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_mixing'}, diag%axesT1, &
3604       time, \textcolor{stringliteral}{'TKE consumed by mixing that deepens the mixed layer'}, &
3605       \textcolor{stringliteral}{'m3 s-3'}, conversion=us%Z\_to\_m*(us%L\_to\_m**2)*(us%s\_to\_T**3))
3606   cs%id\_TKE\_mech\_decay = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_mech\_decay'}, diag%axesT1, &
3607       time, \textcolor{stringliteral}{'Mechanical energy decay sink of mixed layer TKE'}, &
3608       \textcolor{stringliteral}{'m3 s-3'}, conversion=us%Z\_to\_m*(us%L\_to\_m**2)*(us%s\_to\_T**3))
3609   cs%id\_TKE\_conv\_decay = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_conv\_decay'}, diag%axesT1, &
3610       time, \textcolor{stringliteral}{'Convective energy decay sink of mixed layer TKE'}, &
3611       \textcolor{stringliteral}{'m3 s-3'}, conversion=us%Z\_to\_m*(us%L\_to\_m**2)*(us%s\_to\_T**3))
3612   cs%id\_TKE\_conv\_s2 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'TKE\_conv\_s2'}, diag%axesT1, &
3613       time, \textcolor{stringliteral}{'Spurious source of mixed layer TKE from sigma2'}, &
3614       \textcolor{stringliteral}{'m3 s-3'}, conversion=us%Z\_to\_m*(us%L\_to\_m**2)*(us%s\_to\_T**3))
3615   cs%id\_PE\_detrain = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'PE\_detrain'}, diag%axesT1, &
3616       time, \textcolor{stringliteral}{'Spurious source of potential energy from mixed layer detrainment'}, &
3617       \textcolor{stringliteral}{'W m-2'}, conversion=us%RZ3\_T3\_to\_W\_m2*us%L\_to\_Z**2)
3618   cs%id\_PE\_detrain2 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'PE\_detrain2'}, diag%axesT1, &
3619       time, \textcolor{stringliteral}{'Spurious source of potential energy from mixed layer only detrainment'}, &
3620       \textcolor{stringliteral}{'W m-2'}, conversion=us%RZ3\_T3\_to\_W\_m2*us%L\_to\_Z**2)
3621   cs%id\_h\_mismatch = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'h\_miss\_ML'}, diag%axesT1, &
3622       time, \textcolor{stringliteral}{'Summed absolute mismatch in entrainment terms'}, \textcolor{stringliteral}{'m'}, conversion=us%Z\_to\_m)
3623   cs%id\_Hsfc\_used = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Hs\_used'}, diag%axesT1, &
3624       time, \textcolor{stringliteral}{'Surface region thickness that is used'}, \textcolor{stringliteral}{'m'}, conversion=us%Z\_to\_m)
3625   cs%id\_Hsfc\_max = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Hs\_max'}, diag%axesT1, &
3626       time, \textcolor{stringliteral}{'Maximum surface region thickness'}, \textcolor{stringliteral}{'m'}, conversion=us%Z\_to\_m)
3627   cs%id\_Hsfc\_min = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Hs\_min'}, diag%axesT1, &
3628       time, \textcolor{stringliteral}{'Minimum surface region thickness'}, \textcolor{stringliteral}{'m'}, conversion=us%Z\_to\_m)
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?}
3630   \textcolor{keywordflow}{if} (cs%limit\_det .or. (cs%id\_Hsfc\_min > 0)) \textcolor{keywordflow}{then}
3631     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"LIMIT\_BUFFER\_DET\_DH\_SFC"}, cs%lim\_det\_dH\_sfc, &
3632                  \textcolor{stringliteral}{"The fractional limit in the change between grid points "}//&
3633                  \textcolor{stringliteral}{"of the surface region (mixed & buffer layer) thickness."}, &
3634                  units=\textcolor{stringliteral}{"nondim"}, default=0.5)
3635     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"LIMIT\_BUFFER\_DET\_DH\_BATHY"}, cs%lim\_det\_dH\_bathy, &
3636                  \textcolor{stringliteral}{"The fraction of the total depth by which the thickness "}//&
3637                  \textcolor{stringliteral}{"of the surface region (mixed & buffer layer) is allowed "}//&
3638                  \textcolor{stringliteral}{"to change between grid points."}, units=\textcolor{stringliteral}{"nondim"}, default=0.2)
3639 \textcolor{keywordflow}{  endif}
3640 
3641   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENABLE\_THERMODYNAMICS"}, use\_temperature, &
3642                  \textcolor{stringliteral}{"If true, temperature and salinity are used as state "}//&
3643                  \textcolor{stringliteral}{"variables."}, default=.true.)
3644   cs%nsw = 0
3645   \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then}
3646     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"PEN\_SW\_NBANDS"}, cs%nsw, default=1)
3647 \textcolor{keywordflow}{  endif}
3648 
3649 
3650   \textcolor{keywordflow}{if} (max(cs%id\_TKE\_wind, cs%id\_TKE\_RiBulk, cs%id\_TKE\_conv, cs%id\_TKE\_mixing, &
3651           cs%id\_TKE\_pen\_SW, cs%id\_TKE\_mech\_decay, cs%id\_TKE\_conv\_decay) > 0) \textcolor{keywordflow}{then}
3652     \textcolor{keyword}{call }safe\_alloc\_alloc(cs%diag\_TKE\_wind, isd, ied, jsd, jed)
3653     \textcolor{keyword}{call }safe\_alloc\_alloc(cs%diag\_TKE\_RiBulk, isd, ied, jsd, jed)
3654     \textcolor{keyword}{call }safe\_alloc\_alloc(cs%diag\_TKE\_conv, isd, ied, jsd, jed)
3655     \textcolor{keyword}{call }safe\_alloc\_alloc(cs%diag\_TKE\_pen\_SW, isd, ied, jsd, jed)
3656     \textcolor{keyword}{call }safe\_alloc\_alloc(cs%diag\_TKE\_mixing, isd, ied, jsd, jed)
3657     \textcolor{keyword}{call }safe\_alloc\_alloc(cs%diag\_TKE\_mech\_decay, isd, ied, jsd, jed)
3658     \textcolor{keyword}{call }safe\_alloc\_alloc(cs%diag\_TKE\_conv\_decay, isd, ied, jsd, jed)
3659     \textcolor{keyword}{call }safe\_alloc\_alloc(cs%diag\_TKE\_conv\_s2, isd, ied, jsd, jed)
3660 
3661     cs%TKE\_diagnostics = .true.
3662 \textcolor{keywordflow}{  endif}
3663   \textcolor{keywordflow}{if} (cs%id\_PE\_detrain > 0) \textcolor{keyword}{call }safe\_alloc\_alloc(cs%diag\_PE\_detrain, isd, ied, jsd, jed)
3664   \textcolor{keywordflow}{if} (cs%id\_PE\_detrain2 > 0) \textcolor{keyword}{call }safe\_alloc\_alloc(cs%diag\_PE\_detrain2, isd, ied, jsd, jed)
3665   \textcolor{keywordflow}{if} (cs%id\_ML\_depth > 0) \textcolor{keyword}{call }safe\_alloc\_alloc(cs%ML\_depth, isd, ied, jsd, jed)
3666 
3667   \textcolor{keywordflow}{if} (cs%allow\_clocks\_in\_omp\_loops) \textcolor{keywordflow}{then}
3668     id\_clock\_detrain = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer detrain)'}, grain=clock\_routine)
3669     id\_clock\_mech = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer mechanical entrainment)'}, grain=clock\_routine)
3670     id\_clock\_conv = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer convection)'}, grain=clock\_routine)
3671     \textcolor{keywordflow}{if} (cs%ML\_resort) \textcolor{keywordflow}{then}
3672       id\_clock\_resort = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer resorting)'}, grain=clock\_routine)
3673     \textcolor{keywordflow}{else}
3674       id\_clock\_adjustment = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer convective adjustment)'}, grain=clock\_routine)
3675 \textcolor{keywordflow}{    endif}
3676     id\_clock\_eos = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer EOS)'}, grain=clock\_routine)
3677 \textcolor{keywordflow}{  endif}
3678 
3679   \textcolor{keywordflow}{if} (cs%limit\_det .or. (cs%id\_Hsfc\_min > 0)) &
3680       id\_clock\_pass = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer halo updates)'}, grain=clock\_routine)
3681 
3682 
3683 \textcolor{comment}{!  if (CS%limit\_det) then}
3684 \textcolor{comment}{!  endif}
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}}
\subsubsection{\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(\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{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in,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{\tt in,out}  & {\em u} & Zonal velocities interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em v} & Zonal velocities interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em t} & Layer temperatures \mbox{[}degC\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em s} & Layer salinities \mbox{[}ppt\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em r0} & Potential density referenced to surface pressure \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em rcv} & The coordinate defining potential density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt 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{\tt 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{\tt 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{\tt 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{\tt in}  & {\em j} & The j-\/index to work on.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em cs} & The control structure for this module.\\
\hline
\mbox{\tt 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}
806   \textcolor{keywordtype}{type}(ocean\_grid\_type),             \textcolor{keywordtype}{intent(in)}    :: g\textcolor{comment}{    !< The ocean's grid structure.}
807   \textcolor{keywordtype}{type}(verticalgrid\_type),           \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{   !< The ocean's vertical grid structure.}
808   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{    !< Layer thickness [H ~> m or kg m-2].}
809 \textcolor{comment}{                                                           !! The units of h are referred to as H below.}
810   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: u\textcolor{comment}{    !< Zonal velocities interpolated to h}
811 \textcolor{comment}{                                                           !! points [L T-1 ~> m s-1].}
812   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: v\textcolor{comment}{    !< Zonal velocities interpolated to h}
813 \textcolor{comment}{                                                           !! points [L T-1 ~> m s-1].}
814   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: t\textcolor{comment}{    !< Layer temperatures [degC].}
815   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: s\textcolor{comment}{    !< Layer salinities [ppt].}
816   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: r0\textcolor{comment}{   !< Potential density referenced to}
817 \textcolor{comment}{                                                           !! surface pressure [R ~> kg m-3].}
818   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: rcv\textcolor{comment}{  !< The coordinate defining potential}
819 \textcolor{comment}{                                                           !! density [R ~> kg m-3].}
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}
821 \textcolor{comment}{                                                           !! in the entrainment from below [H ~> m or kg
       m-2].}
822 \textcolor{comment}{                                                           !! Positive values go with mass gain by}
823 \textcolor{comment}{                                                           !! a layer.}
824   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(in)}    :: eps\textcolor{comment}{  !< The negligibly small amount of water}
825 \textcolor{comment}{                                                           !! that will be left in each layer [H ~> m or kg
       m-2].}
826   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(out)}   :: dke\_ca\textcolor{comment}{ !< The vertically integrated change in}
827 \textcolor{comment}{                                                           !! kinetic energy due to convective}
828 \textcolor{comment}{                                                           !! adjustment [Z L2 T-2 ~> m3 s-2].}
829   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(out)}   :: ctke\textcolor{comment}{ !< The buoyant turbulent kinetic energy}
830 \textcolor{comment}{                                                           !! source due to convective adjustment}
831 \textcolor{comment}{                                                           !! [Z L2 T-2 ~> m3 s-2].}
832   \textcolor{keywordtype}{integer},                           \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{    !< The j-index to work on.}
833   \textcolor{keywordtype}{type}(unit\_scale\_type),             \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{   !< A dimensional unit scaling type}
834   \textcolor{keywordtype}{type}(bulkmixedlayer\_cs),           \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{   !< The control structure for this module.}
835   \textcolor{keywordtype}{integer},                 \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: nz\_conv\textcolor{comment}{ !< If present, the number of layers}
836 \textcolor{comment}{                                                           !! over which to do convective adjustment}
837 \textcolor{comment}{                                                           !! (perhaps CS%nkml).}
838 
839 \textcolor{comment}{!   This subroutine does instantaneous convective entrainment into the buffer}
840 \textcolor{comment}{! layers and mixed layers to remove hydrostatic instabilities.  Any water that}
841 \textcolor{comment}{! is lighter than currently in the mixed- or buffer- layer is entrained.}
842 
843   \textcolor{comment}{! Local variables}
844   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: &
845     htot, &     \textcolor{comment}{!   The total depth of the layers being considered for}
846                 \textcolor{comment}{! entrainment [H ~> m or kg m-2].}
847     r0\_tot, &   \textcolor{comment}{!   The integrated potential density referenced to the surface}
848                 \textcolor{comment}{! of the layers which are fully entrained [H R ~> kg m-2 or kg2 m-5].}
849     rcv\_tot, &  \textcolor{comment}{!   The integrated coordinate value potential density of the}
850                 \textcolor{comment}{! layers that are fully entrained [H R ~> kg m-2 or kg2 m-5].}
851     ttot, &     \textcolor{comment}{!   The integrated temperature of layers which are fully}
852                 \textcolor{comment}{! entrained [degC H ~> degC m or degC kg m-2].}
853     stot, &     \textcolor{comment}{!   The integrated salt of layers which are fully entrained}
854                 \textcolor{comment}{! [H ppt ~> m ppt or ppt kg m-2].}
855     uhtot, &    \textcolor{comment}{!   The depth integrated zonal and meridional velocities in}
856     vhtot, &    \textcolor{comment}{! the mixed layer [H L T-1 ~> m2 s-1 or kg m-1 s-1].}
857     ke\_orig, &  \textcolor{comment}{!   The total mean kinetic energy in the mixed layer before}
858                 \textcolor{comment}{! convection, [H L2 T-2 ~> H m2 s-2].}
859     h\_orig\_k1   \textcolor{comment}{!   The depth of layer k1 before convective adjustment [H ~> m or kg m-2].}
860   \textcolor{keywordtype}{real} :: h\_ent \textcolor{comment}{!   The thickness from a layer that is entrained [H ~> m or kg m-2].}
861   \textcolor{keywordtype}{real} :: ih    \textcolor{comment}{!   The inverse of a thickness [H-1 ~> m-1 or m2 kg-1].}
862   \textcolor{keywordtype}{real} :: g\_h2\_2rho0  \textcolor{comment}{!   Half the gravitational acceleration times the square of}
863                       \textcolor{comment}{! the conversion from H to Z divided by the mean density,}
864                       \textcolor{comment}{! in [L2 Z T-3 H-2 R-1 ~> m4 s-3 kg-1 or m10 s-3 kg-3].}
865   \textcolor{keywordtype}{integer} :: is, ie, nz, i, k, k1, nzc, nkmb
866 
867   is = g%isc ; ie = g%iec ; nz = gv%ke
868   g\_h2\_2rho0 = (gv%g\_Earth * gv%H\_to\_Z**2) / (2.0 * gv%Rho0)
869   nzc = nz ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(nz\_conv)) nzc = nz\_conv
870   nkmb = cs%nkml+cs%nkbl
871 
872 \textcolor{comment}{!   Undergo instantaneous entrainment into the buffer layers and mixed layers}
873 \textcolor{comment}{! to remove hydrostatic instabilities.  Any water that is lighter than currently}
874 \textcolor{comment}{! in the layer is entrained.}
875   \textcolor{keywordflow}{do} k1=min(nzc-1,nkmb),1,-1
876     \textcolor{keywordflow}{do} i=is,ie
877       h\_orig\_k1(i) = h(i,k1)
878       ke\_orig(i) = 0.5*h(i,k1)*(u(i,k1)**2 + v(i,k1)**2)
879       uhtot(i) = h(i,k1)*u(i,k1) ; vhtot(i) = h(i,k1)*v(i,k1)
880       r0\_tot(i) = r0(i,k1) * h(i,k1)
881       ctke(i,k1) = 0.0 ; dke\_ca(i,k1) = 0.0
882 
883       rcv\_tot(i) = rcv(i,k1) * h(i,k1)
884       ttot(i) = t(i,k1) * h(i,k1) ; stot(i) = s(i,k1) * h(i,k1)
885 \textcolor{keywordflow}{    enddo}
886     \textcolor{keywordflow}{do} k=k1+1,nzc
887       \textcolor{keywordflow}{do} i=is,ie
888         \textcolor{keywordflow}{if} ((h(i,k) > eps(i,k)) .and. (r0\_tot(i) > h(i,k1)*r0(i,k))) \textcolor{keywordflow}{then}
889           h\_ent = h(i,k)-eps(i,k)
890           ctke(i,k1) = ctke(i,k1) + h\_ent * g\_h2\_2rho0 * &
891                    (r0\_tot(i) - h(i,k1)*r0(i,k)) * cs%nstar2
892           \textcolor{keywordflow}{if} (k < nkmb) \textcolor{keywordflow}{then}
893             ctke(i,k1) = ctke(i,k1) + ctke(i,k)
894             dke\_ca(i,k1) = dke\_ca(i,k1) + dke\_ca(i,k)
895 \textcolor{keywordflow}{          endif}
896           r0\_tot(i) = r0\_tot(i) + h\_ent * r0(i,k)
897           ke\_orig(i) = ke\_orig(i) + 0.5*h\_ent* &
898               (u(i,k)*u(i,k) + v(i,k)*v(i,k))
899           uhtot(i) = uhtot(i) + h\_ent*u(i,k)
900           vhtot(i) = vhtot(i) + h\_ent*v(i,k)
901 
902           rcv\_tot(i) = rcv\_tot(i) + h\_ent * rcv(i,k)
903           ttot(i) = ttot(i) + h\_ent * t(i,k)
904           stot(i) = stot(i) + h\_ent * s(i,k)
905           h(i,k1) = h(i,k1) + h\_ent ; h(i,k) = eps(i,k)
906 
907           d\_eb(i,k) = d\_eb(i,k) - h\_ent
908           d\_eb(i,k1) = d\_eb(i,k1) + h\_ent
909 \textcolor{keywordflow}{        endif}
910 \textcolor{keywordflow}{      enddo}
911 \textcolor{keywordflow}{    enddo}
912 \textcolor{comment}{! Determine the temperature, salinity, and velocities of the mixed or buffer}
913 \textcolor{comment}{! layer in question, if it has entrained.}
914     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (h(i,k1) > h\_orig\_k1(i)) \textcolor{keywordflow}{then}
915       ih = 1.0 / h(i,k1)
916       r0(i,k1) = r0\_tot(i) * ih
917       u(i,k1) = uhtot(i) * ih ; v(i,k1) = vhtot(i) * ih
918       dke\_ca(i,k1) = dke\_ca(i,k1) + gv%H\_to\_Z * (cs%bulk\_Ri\_convective * &
919            (ke\_orig(i) - 0.5*h(i,k1)*(u(i,k1)**2 + v(i,k1)**2)))
920       rcv(i,k1) = rcv\_tot(i) * ih
921       t(i,k1) = ttot(i) * ih ; s(i,k1) = stot(i) * ih
922 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
923 \textcolor{keywordflow}{  enddo}
924 \textcolor{comment}{! If lower mixed or buffer layers are massless, give them the properties of the}
925 \textcolor{comment}{! layer above.}
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}
927     r0(i,k) = r0(i,k-1)
928     rcv(i,k) = rcv(i,k-1) ; t(i,k) = t(i,k-1) ; s(i,k) = s(i,k-1)
929 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
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}}
\subsubsection{\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{\tt in}  & {\em ht} & Total thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em en} & Entrainment \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em i\+\_\+l} & The e-\/folding scale \mbox{[}H-\/1 $\sim$$>$ m-\/1 or m2 kg-\/1\mbox{]}\\
\hline
\mbox{\tt 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}
3694   \textcolor{keywordtype}{real},           \textcolor{keywordtype}{intent(in)}    :: ht\textcolor{comment}{  !< Total thickness [H ~> m or kg m-2].}
3695   \textcolor{keywordtype}{real},           \textcolor{keywordtype}{intent(in)}    :: en\textcolor{comment}{  !< Entrainment [H ~> m or kg m-2].}
3696   \textcolor{keywordtype}{real},           \textcolor{keywordtype}{intent(in)}    :: i\_l\textcolor{comment}{ !< The e-folding scale [H-1 ~> m-1 or m2 kg-1]}
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 ~> m-2 or m4
       kg-2].}
3698   \textcolor{keywordtype}{real} :: ef4\textcolor{comment}{ !< The integral [H-1 ~> m-1 or m2 kg-1].}
3699 
3700   \textcolor{comment}{! Local variables}
3701   \textcolor{keywordtype}{real} :: exp\_lhpe \textcolor{comment}{! A nondimensional exponential decay.}
3702   \textcolor{keywordtype}{real} :: i\_hpe    \textcolor{comment}{! An inverse thickness plus entrainment [H-1 ~> m-1 or m2 kg-1].}
3703   \textcolor{keywordtype}{real} :: res      \textcolor{comment}{! The result of the integral above [H-1 ~> m-1 or m2 kg-1].}
3704 
3705   exp\_lhpe = exp(-i\_l*(en+ht))
3706   i\_hpe = 1.0/(ht+en)
3707   res = exp\_lhpe * (en*i\_hpe/ht - 0.5*i\_l*log(ht*i\_hpe) + 0.5*i\_l*i\_l*en)
3708   \textcolor{keywordflow}{if} (\textcolor{keyword}{PRESENT}(dr\_de)) &
3709     dr\_de = -i\_l*res + exp\_lhpe*(i\_hpe*i\_hpe + 0.5*i\_l*i\_hpe + 0.5*i\_l*i\_l)
3710   ef4 = res
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}}
\subsubsection{\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(\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{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em htot} & The accumulated mixed layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em h\+\_\+ca} & The mixed layer depth after convective adjustment \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em fluxes} & A structure containing pointers to any possible forcing fields. Unused fields have N\+U\+LL ptrs.\\
\hline
\mbox{\tt 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{\tt 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{\tt in}  & {\em ctke} & The buoyant turbulent kinetic energy\\
\hline
\mbox{\tt in}  & {\em dke\+\_\+ca} & The vertically integrated change in\\
\hline
\mbox{\tt 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{\tt 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{\tt 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{\tt 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{\tt in}  & {\em dt} & The time step \mbox{[}T $\sim$$>$ s\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em idt\+\_\+diag} & The inverse of the accumulated diagnostic time interval \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em j} & The j-\/index to work on.\\
\hline
\mbox{\tt 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}
1316   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(in)}    :: g\textcolor{comment}{       !< The ocean's grid structure.}
1317   \textcolor{keywordtype}{type}(verticalgrid\_type),    \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{      !< The ocean's vertical grid structure.}
1318   \textcolor{keywordtype}{type}(unit\_scale\_type),      \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{      !< A dimensional unit scaling type}
1319   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(in)}    :: htot\textcolor{comment}{    !< The accumulated mixed layer thickness}
1320 \textcolor{comment}{                                                       !! [H ~> m or kg m-2]}
1321   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(in)}    :: h\_ca\textcolor{comment}{    !< The mixed layer depth after convective}
1322 \textcolor{comment}{                                                       !! adjustment [H ~> m or kg m-2].}
1323   \textcolor{keywordtype}{type}(forcing),              \textcolor{keywordtype}{intent(in)}    :: fluxes\textcolor{comment}{  !< A structure containing pointers to any}
1324 \textcolor{comment}{                                                       !! possible forcing fields.  Unused fields}
1325 \textcolor{comment}{                                                       !! have NULL ptrs.}
1326   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(inout)} :: conv\_en\textcolor{comment}{ !< The buoyant turbulent kinetic energy source}
1327 \textcolor{comment}{                                                       !! due to free convection [Z L2 T-2 ~> m3 s-2].}
1328   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(in)}    :: dke\_fc\textcolor{comment}{  !< The vertically integrated change in}
1329 \textcolor{comment}{                                                       !! kinetic energy due to free convection}
1330 \textcolor{comment}{                                                       !! [Z L2 T-2 ~> m3 s-2].}
1331   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1332                               \textcolor{keywordtype}{intent(in)}    :: ctke\textcolor{comment}{    !< The buoyant turbulent kinetic energy}
1333 \textcolor{comment}{                                                       !! source due to convective adjustment}
1334 \textcolor{comment}{                                                       !! [Z L2 T-2 ~> m3 s-2].}
1335   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1336                               \textcolor{keywordtype}{intent(in)}    :: dke\_ca\textcolor{comment}{  !< The vertically integrated change in}
1337 \textcolor{comment}{                                                       !! kinetic energy due to convective}
1338 \textcolor{comment}{                                                       !! adjustment [Z L2 T-2 ~> m3 s-2].}
1339   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(out)}   :: tke\textcolor{comment}{     !< The turbulent kinetic energy available for}
1340 \textcolor{comment}{                                                       !! mixing over a time step [Z m2 T-2 ~> m3 s-2].}
1341   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(out)}   :: idecay\_len\_tke\textcolor{comment}{ !< The inverse of the vertical decay}
1342 \textcolor{comment}{                                                       !! scale for TKE [H-1 ~> m-1 or m2 kg-1].}
1343   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(in)}    :: tke\_river\textcolor{comment}{ !< The source of turbulent kinetic energy}
1344 \textcolor{comment}{                                                       !! available for driving mixing at river mouths}
1345 \textcolor{comment}{                                                       !! [Z L2 T-3 ~> m3 s-3].}
1346   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(2,SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: cmke\textcolor{comment}{    !< Coefficients of HpE and HpE^2 in}
1347 \textcolor{comment}{                                                       !! calculating the denominator of MKE\_rate,}
1348 \textcolor{comment}{                                                       !! [H-1 ~> m-1 or m2 kg-1] and [H-2 ~> m-2 or m4
       kg-2].}
1349   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{      !< The time step [T ~> s].}
1350   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in)}    :: idt\_diag\textcolor{comment}{ !< The inverse of the accumulated diagnostic}
1351 \textcolor{comment}{                                                       !! time interval [T-1 ~> s-1].}
1352   \textcolor{keywordtype}{integer},                    \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{       !< The j-index to work on.}
1353   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1354                               \textcolor{keywordtype}{intent(in)}    :: ksort\textcolor{comment}{   !< The density-sorted k-indicies.}
1355   \textcolor{keywordtype}{type}(bulkmixedlayer\_cs),    \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{      !< The control structure for this module.}
1356 
1357 \textcolor{comment}{!   This subroutine determines the TKE available at the depth of free}
1358 \textcolor{comment}{! convection to drive mechanical entrainment.}
1359 
1360   \textcolor{comment}{! Local variables}
1361   \textcolor{keywordtype}{real} :: dke\_conv  \textcolor{comment}{! The change in mean kinetic energy due to all convection [Z L2 T-2 ~> m3 s-2].}
1362   \textcolor{keywordtype}{real} :: nstar\_fc  \textcolor{comment}{! The effective efficiency with which the energy released by}
1363                     \textcolor{comment}{! free convection is converted to TKE, often ~0.2 [nondim].}
1364   \textcolor{keywordtype}{real} :: nstar\_ca  \textcolor{comment}{! The effective efficiency with which the energy released by}
1365                     \textcolor{comment}{! convective adjustment is converted to TKE, often ~0.2 [nondim].}
1366   \textcolor{keywordtype}{real} :: tke\_ca    \textcolor{comment}{! The potential energy released by convective adjustment if}
1367                     \textcolor{comment}{! that release is positive [Z L2 T-2 ~> m3 s-2].}
1368   \textcolor{keywordtype}{real} :: mke\_rate\_ca \textcolor{comment}{! MKE\_rate for convective adjustment [nondim], 0 to 1.}
1369   \textcolor{keywordtype}{real} :: mke\_rate\_fc \textcolor{comment}{! MKE\_rate for free convection [nondim], 0 to 1.}
1370   \textcolor{keywordtype}{real} :: toten\_z   \textcolor{comment}{! The total potential energy released by convection, [Z3 T-2 ~> m3 s-2].}
1371   \textcolor{keywordtype}{real} :: ih        \textcolor{comment}{! The inverse of a thickness [H-1 ~> m-1 or m2 kg-1].}
1372   \textcolor{keywordtype}{real} :: exp\_kh    \textcolor{comment}{! The nondimensional decay of TKE across a layer [nondim].}
1373   \textcolor{keywordtype}{real} :: absf      \textcolor{comment}{! The absolute value of f averaged to thickness points [T-1 ~> s-1].}
1374   \textcolor{keywordtype}{real} :: u\_star    \textcolor{comment}{! The friction velocity [Z T-1 ~> m s-1].}
1375   \textcolor{keywordtype}{real} :: absf\_ustar  \textcolor{comment}{! The absolute value of f divided by U\_star [Z-1 ~> m-1].}
1376   \textcolor{keywordtype}{real} :: wind\_tke\_src \textcolor{comment}{! The surface wind source of TKE [Z L2 T-3 ~> m3 s-3].}
1377   \textcolor{keywordtype}{real} :: diag\_wt   \textcolor{comment}{! The ratio of the current timestep to the diagnostic}
1378                     \textcolor{comment}{! timestep (which may include 2 calls) [nondim].}
1379   \textcolor{keywordtype}{integer} :: is, ie, nz, i
1380 
1381   is = g%isc ; ie = g%iec ; nz = gv%ke
1382   diag\_wt = dt * idt\_diag
1383 
1384   \textcolor{keywordflow}{if} (cs%omega\_frac >= 1.0) absf = 2.0*cs%omega
1385   \textcolor{keywordflow}{do} i=is,ie
1386     u\_star = fluxes%ustar(i,j)
1387     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%ustar\_shelf) .and. \textcolor{keyword}{associated}(fluxes%frac\_shelf\_h)) \textcolor{keywordflow}{then}
1388       \textcolor{keywordflow}{if} (fluxes%frac\_shelf\_h(i,j) > 0.0) &
1389         u\_star = (1.0 - fluxes%frac\_shelf\_h(i,j)) * u\_star + &
1390                   fluxes%frac\_shelf\_h(i,j) * fluxes%ustar\_shelf(i,j)
1391 \textcolor{keywordflow}{    endif}
1392 
1393     \textcolor{keywordflow}{if} (u\_star < cs%ustar\_min) u\_star = cs%ustar\_min
1394     \textcolor{keywordflow}{if} (cs%omega\_frac < 1.0) \textcolor{keywordflow}{then}
1395       absf = 0.25*((abs(g%CoriolisBu(i,j)) + abs(g%CoriolisBu(i-1,j-1))) + &
1396                    (abs(g%CoriolisBu(i,j-1)) + abs(g%CoriolisBu(i-1,j))))
1397       \textcolor{keywordflow}{if} (cs%omega\_frac > 0.0) &
1398         absf = sqrt(cs%omega\_frac*4.0*cs%omega**2 + (1.0-cs%omega\_frac)*absf**2)
1399 \textcolor{keywordflow}{    endif}
1400     absf\_ustar = absf / u\_star
1401     idecay\_len\_tke(i) = (absf\_ustar * cs%TKE\_decay) * gv%H\_to\_Z
1402 
1403 \textcolor{comment}{!    The first number in the denominator could be anywhere up to 16.  The}
1404 \textcolor{comment}{!  value of 3 was chosen to minimize the time-step dependence of the amount}
1405 \textcolor{comment}{!  of shear-driven mixing in 10 days of a 1-degree global model, emphasizing}
1406 \textcolor{comment}{!  the equatorial areas.  Although it is not cast as a parameter, it should}
1407 \textcolor{comment}{!  be considered an empirical parameter, and it might depend strongly on the}
1408 \textcolor{comment}{!  number of sublayers in the mixed layer and their locations.}
1409 \textcolor{comment}{!  The 0.41 is VonKarman's constant.  This equation assumes that small & large}
1410 \textcolor{comment}{!  scales contribute to mixed layer deepening at similar rates, even though}
1411 \textcolor{comment}{!  small scales are dissipated more rapidly (implying they are less efficient).}
1412 \textcolor{comment}{!     Ih = 1.0/(16.0*0.41*U\_star*dt)}
1413     ih = gv%H\_to\_Z/(3.0*0.41*u\_star*dt)
1414     cmke(1,i) = 4.0 * ih ; cmke(2,i) = (absf\_ustar*gv%H\_to\_Z) * ih
1415 
1416     \textcolor{keywordflow}{if} (idecay\_len\_tke(i) > 0.0) \textcolor{keywordflow}{then}
1417       exp\_kh = exp(-htot(i)*idecay\_len\_tke(i))
1418     \textcolor{keywordflow}{else}
1419       exp\_kh = 1.0
1420 \textcolor{keywordflow}{    endif}
1421 
1422 \textcolor{comment}{! Here nstar is a function of the natural Rossby number  0.2/(1+0.2/Ro), based}
1423 \textcolor{comment}{! on a curve fit from the data of Wang (GRL, 2003).}
1424 \textcolor{comment}{! Note:         Ro = 1.0/sqrt(0.5 * dt * (absf*htot(i))**3 / totEn)}
1425     \textcolor{keywordflow}{if} (conv\_en(i) < 0.0) conv\_en(i) = 0.0
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}
1427     \textcolor{keywordflow}{if} ((htot(i) >= h\_ca(i)) .or. (tke\_ca == 0.0)) \textcolor{keywordflow}{then}
1428       toten\_z = us%L\_to\_Z**2 * (conv\_en(i) + tke\_ca)
1429 
1430       \textcolor{keywordflow}{if} (toten\_z > 0.0) \textcolor{keywordflow}{then}
1431         nstar\_fc = cs%nstar * toten\_z / (toten\_z + 0.2 * &
1432                         sqrt(0.5 * dt * (absf*(htot(i)*gv%H\_to\_Z))**3 * toten\_z))
1433       \textcolor{keywordflow}{else}
1434         nstar\_fc = cs%nstar
1435 \textcolor{keywordflow}{      endif}
1436       nstar\_ca = nstar\_fc
1437     \textcolor{keywordflow}{else}
1438       \textcolor{comment}{! This reconstructs the Buoyancy flux within the topmost htot of water.}
1439       \textcolor{keywordflow}{if} (conv\_en(i) > 0.0) \textcolor{keywordflow}{then}
1440         toten\_z = us%L\_to\_Z**2 * (conv\_en(i) + tke\_ca * (htot(i) / h\_ca(i)) )
1441         nstar\_fc = cs%nstar * toten\_z / (toten\_z + 0.2 * &
1442                         sqrt(0.5 * dt * (absf*(htot(i)*gv%H\_to\_Z))**3 * toten\_z))
1443       \textcolor{keywordflow}{else}
1444         nstar\_fc = cs%nstar
1445 \textcolor{keywordflow}{      endif}
1446 
1447       toten\_z = us%L\_to\_Z**2 * (conv\_en(i) + tke\_ca)
1448       \textcolor{keywordflow}{if} (tke\_ca > 0.0) \textcolor{keywordflow}{then}
1449         nstar\_ca = cs%nstar * toten\_z / (toten\_z + 0.2 * &
1450                         sqrt(0.5 * dt * (absf*(h\_ca(i)*gv%H\_to\_Z))**3 * toten\_z))
1451       \textcolor{keywordflow}{else}
1452         nstar\_ca = cs%nstar
1453 \textcolor{keywordflow}{      endif}
1454 \textcolor{keywordflow}{    endif}
1455 
1456     \textcolor{keywordflow}{if} (dke\_fc(i) + dke\_ca(i,1) > 0.0) \textcolor{keywordflow}{then}
1457       \textcolor{keywordflow}{if} (htot(i) >= h\_ca(i)) \textcolor{keywordflow}{then}
1458         mke\_rate\_fc = 1.0 / (1.0 + htot(i)*(cmke(1,i) + cmke(2,i)*htot(i)) )
1459         mke\_rate\_ca = mke\_rate\_fc
1460       \textcolor{keywordflow}{else}
1461         mke\_rate\_fc = 1.0 / (1.0 + htot(i)*(cmke(1,i) + cmke(2,i)*htot(i)) )
1462         mke\_rate\_ca = 1.0 / (1.0 + h\_ca(i)*(cmke(1,i) + cmke(2,i)*h\_ca(i)) )
1463 \textcolor{keywordflow}{      endif}
1464     \textcolor{keywordflow}{else}
1465       \textcolor{comment}{! This branch just saves unnecessary calculations.}
1466       mke\_rate\_fc = 1.0 ; mke\_rate\_ca = 1.0
1467 \textcolor{keywordflow}{    endif}
1468 
1469     dke\_conv = dke\_ca(i,1) * mke\_rate\_ca + dke\_fc(i) * mke\_rate\_fc
1470 \textcolor{comment}{! At this point, it is assumed that cTKE is positive and stored in TKE\_CA!}
1471 \textcolor{comment}{! Note: Removed factor of 2 in u*^3 terms.}
1472     tke(i) = (dt*cs%mstar)*((us%Z\_to\_L**2*(u\_star*u\_star*u\_star))*exp\_kh) + &
1473              (exp\_kh * dke\_conv + nstar\_fc*conv\_en(i) + nstar\_ca * tke\_ca)
1474 
1475     \textcolor{keywordflow}{if} (cs%do\_rivermix) \textcolor{keywordflow}{then} \textcolor{comment}{! Add additional TKE at river mouths}
1476       tke(i) = tke(i) + tke\_river(i)*dt*exp\_kh
1477 \textcolor{keywordflow}{    endif}
1478 
1479     \textcolor{keywordflow}{if} (cs%TKE\_diagnostics) \textcolor{keywordflow}{then}
1480       wind\_tke\_src = cs%mstar*(us%Z\_to\_L**2*u\_star*u\_star*u\_star) * diag\_wt
1481       cs%diag\_TKE\_wind(i,j) = cs%diag\_TKE\_wind(i,j) + &
1482           ( wind\_tke\_src + tke\_river(i) * diag\_wt )
1483       cs%diag\_TKE\_RiBulk(i,j) = cs%diag\_TKE\_RiBulk(i,j) + dke\_conv*idt\_diag
1484       cs%diag\_TKE\_mech\_decay(i,j) = cs%diag\_TKE\_mech\_decay(i,j) + &
1485           (exp\_kh-1.0)*(wind\_tke\_src + dke\_conv*idt\_diag)
1486       cs%diag\_TKE\_conv(i,j) = cs%diag\_TKE\_conv(i,j) + &
1487           idt\_diag * (nstar\_fc*conv\_en(i) + nstar\_ca*tke\_ca)
1488       cs%diag\_TKE\_conv\_decay(i,j) = cs%diag\_TKE\_conv\_decay(i,j) + &
1489           idt\_diag * ((cs%nstar-nstar\_fc)*conv\_en(i) + (cs%nstar-nstar\_ca)*tke\_ca)
1490       cs%diag\_TKE\_conv\_s2(i,j) = cs%diag\_TKE\_conv\_s2(i,j) + &
1491           idt\_diag * (ctke(i,1)-tke\_ca)
1492 \textcolor{keywordflow}{    endif}
1493 \textcolor{keywordflow}{  enddo}
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}}
\subsubsection{\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(\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{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in,out}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em d\+\_\+eb} & The downward increase across a layer in the\\
\hline
\mbox{\tt in,out}  & {\em htot} & The accumlated mixed layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em ttot} & The depth integrated mixed layer temperature \mbox{[}degC H $\sim$$>$ degC m or degC kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em stot} & The depth integrated mixed layer salinity \mbox{[}ppt H $\sim$$>$ ppt m or ppt kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt 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{\tt 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{\tt 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{\tt 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{\tt in}  & {\em u} & Zonal velocities interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em v} & Zonal velocities interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em t} & Layer temperatures \mbox{[}degC\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em s} & Layer salinities \mbox{[}ppt\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em r0} & Potential density referenced to\\
\hline
\mbox{\tt in}  & {\em rcv} & The coordinate defining potential\\
\hline
\mbox{\tt in}  & {\em eps} & The negligibly small amount of water\\
\hline
\mbox{\tt 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{\tt 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{\tt 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{\tt in}  & {\em idt\+\_\+diag} & The inverse of the accumulated diagnostic time interval \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em nsw} & The number of bands of penetrating shortwave radiation.\\
\hline
\mbox{\tt 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{\tt 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{\tt 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{\tt 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{\tt in}  & {\em j} & The j-\/index to work on.\\
\hline
\mbox{\tt 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}
1503   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(in)}    :: g\textcolor{comment}{     !< The ocean's grid structure.}
1504   \textcolor{keywordtype}{type}(verticalgrid\_type),  \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{    !< The ocean's vertical grid structure.}
1505   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{    !< A dimensional unit scaling type}
1506   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1507                             \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{     !< Layer thickness [H ~> m or kg m-2].}
1508   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1509                             \textcolor{keywordtype}{intent(inout)} :: d\_eb\textcolor{comment}{  !< The downward increase across a layer in the}
1510 \textcolor{comment}{                                                   !! layer in the entrainment from below [H ~> m or kg
       m-2].}
1511 \textcolor{comment}{                                                   !! Positive values go with mass gain by a layer.}
1512   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: htot\textcolor{comment}{  !< The accumlated mixed layer thickness [H ~> m or kg
       m-2].}
1513   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: ttot\textcolor{comment}{  !< The depth integrated mixed layer temperature}
1514 \textcolor{comment}{                                                   !! [degC H ~> degC m or degC kg m-2].}
1515   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: stot\textcolor{comment}{  !< The depth integrated mixed layer salinity}
1516 \textcolor{comment}{                                                   !! [ppt H ~> ppt m or ppt kg m-2].}
1517   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: uhtot\textcolor{comment}{ !< The depth integrated mixed layer zonal}
1518 \textcolor{comment}{                                                   !! velocity [H L T-1 ~> m2 s-1 or kg m-1 s-1].}
1519   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: vhtot\textcolor{comment}{ !< The integrated mixed layer meridional}
1520 \textcolor{comment}{                                                   !! velocity [H L T-1 ~> m2 s-1 or kg m-1 s-1].}
1521   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: r0\_tot\textcolor{comment}{ !< The integrated mixed layer potential density}
1522 \textcolor{comment}{                                                   !! referenced to 0 pressure [H R ~> kg m-2 or kg2 m-5].}
1523   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: rcv\_tot\textcolor{comment}{ !< The integrated mixed layer coordinate variable}
1524 \textcolor{comment}{                                                   !! potential density [H R ~> kg m-2 or kg2 m-5].}
1525   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1526                             \textcolor{keywordtype}{intent(in)}    :: u\textcolor{comment}{     !< Zonal velocities interpolated to h points [L T-1 ~> m
       s-1].}
1527   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1528                             \textcolor{keywordtype}{intent(in)}    :: v\textcolor{comment}{     !< Zonal velocities interpolated to h points [L T-1 ~> m
       s-1].}
1529   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1530                             \textcolor{keywordtype}{intent(in)}    :: t\textcolor{comment}{     !< Layer temperatures [degC].}
1531   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1532                             \textcolor{keywordtype}{intent(in)}    :: s\textcolor{comment}{     !< Layer salinities [ppt].}
1533   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1534                             \textcolor{keywordtype}{intent(in)}    :: r0\textcolor{comment}{    !< Potential density referenced to}
1535 \textcolor{comment}{                                                   !! surface pressure [R ~> kg m-3].}
1536   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1537                             \textcolor{keywordtype}{intent(in)}    :: rcv\textcolor{comment}{   !< The coordinate defining potential}
1538 \textcolor{comment}{                                                   !! density [R ~> kg m-3].}
1539   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1540                             \textcolor{keywordtype}{intent(in)}    :: eps\textcolor{comment}{   !< The negligibly small amount of water}
1541 \textcolor{comment}{                                                   !! that will be left in each layer [H ~> m or kg m-2].}
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}
1543 \textcolor{comment}{                                                   !! temperature [R degC-1 ~> kg m-3 degC-1].}
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}
1545 \textcolor{comment}{                                                   !! temperature [R degC-1 ~> kg m-3 degC-1].}
1546   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(2,SZI\_(G))}, \textcolor{keywordtype}{intent(in)}  :: cmke\textcolor{comment}{  !< Coefficients of HpE and HpE^2 used in calculating the}
1547 \textcolor{comment}{                                                   !! denominator of MKE\_rate; the two elements have
       differing}
1548 \textcolor{comment}{                                                   !! units of [H-1 ~> m-1 or m2 kg-1] and [H-2 ~> m-2 or
       m4 kg-2].}
1549   \textcolor{keywordtype}{real},                     \textcolor{keywordtype}{intent(in)}    :: idt\_diag\textcolor{comment}{ !< The inverse of the accumulated diagnostic}
1550 \textcolor{comment}{                                                   !! time interval [T-1 ~> s-1].}
1551   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}    :: nsw\textcolor{comment}{   !< The number of bands of penetrating}
1552 \textcolor{comment}{                                                   !! shortwave radiation.}
1553   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(max(nsw,1),SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: pen\_sw\_bnd\textcolor{comment}{ !< The penetrating shortwave}
1554 \textcolor{comment}{                                                   !! heating at the sea surface in each penetrating}
1555 \textcolor{comment}{                                                   !! band [degC H ~> degC m or degC kg m-2].}
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}
1557 \textcolor{comment}{                                                   !! penetrating shortwave radiation [H-1 ~> m-1 or m2
       kg-1].}
1558   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: tke\textcolor{comment}{   !< The turbulent kinetic energy}
1559 \textcolor{comment}{                                                   !! available for mixing over a time}
1560 \textcolor{comment}{                                                   !! step [Z m2 T-2 ~> m3 s-2].}
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 ~> m-1 or
       m2 kg-1].}
1562   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{     !< The j-index to work on.}
1563   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1564                             \textcolor{keywordtype}{intent(in)}    :: ksort\textcolor{comment}{ !< The density-sorted k-indicies.}
1565   \textcolor{keywordtype}{type}(bulkmixedlayer\_cs),  \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{    !< The control structure for this module.}
1566 
1567 \textcolor{comment}{! This subroutine calculates mechanically driven entrainment.}
1568 
1569   \textcolor{comment}{! Local variables}
1570   \textcolor{keywordtype}{real} :: sw\_trans  \textcolor{comment}{!   The fraction of shortwave radiation that is not}
1571                     \textcolor{comment}{! absorbed in a layer, nondimensional.}
1572   \textcolor{keywordtype}{real} :: pen\_absorbed  \textcolor{comment}{!   The amount of penetrative shortwave radiation}
1573                         \textcolor{comment}{! that is absorbed in a layer [degC H ~> degC m or degC kg m-2].}
1574   \textcolor{keywordtype}{real} :: h\_avail   \textcolor{comment}{! The thickness in a layer available for entrainment [H ~> m or kg m-2].}
1575   \textcolor{keywordtype}{real} :: h\_ent     \textcolor{comment}{! The thickness from a layer that is entrained [H ~> m or kg m-2].}
1576   \textcolor{keywordtype}{real} :: h\_min, h\_max \textcolor{comment}{! Limits on the solution for h\_ent [H ~> m or kg m-2].}
1577   \textcolor{keywordtype}{real} :: dh\_newt      \textcolor{comment}{!   The Newton's method estimate of the change in}
1578                        \textcolor{comment}{! h\_ent between iterations [H ~> m or kg m-2].}
1579   \textcolor{keywordtype}{real} :: mke\_rate  \textcolor{comment}{!   The fraction of the energy in resolved shears}
1580                     \textcolor{comment}{! within the mixed layer that will be eliminated}
1581                     \textcolor{comment}{! within a timestep, nondim, 0 to 1.}
1582   \textcolor{keywordtype}{real} :: hpe       \textcolor{comment}{!   The current thickness plus entrainment [H ~> m or kg m-2].}
1583   \textcolor{keywordtype}{real} :: g\_h\_2rho0   \textcolor{comment}{!   Half the gravitational acceleration times the}
1584                       \textcolor{comment}{! conversion from H to m divided by the mean density,}
1585                       \textcolor{comment}{! in [L2 T-2 H-1 R-1 ~> m4 s-2 kg-1 or m7 s-2 kg-2].}
1586   \textcolor{keywordtype}{real} :: tke\_full\_ent  \textcolor{comment}{! The TKE remaining if a layer is fully entrained}
1587                         \textcolor{comment}{! [Z L2 T-2 ~> m3 s-2].}
1588   \textcolor{keywordtype}{real} :: drl       \textcolor{comment}{! Work required to mix water from the next layer}
1589                     \textcolor{comment}{! across the mixed layer [L2 T-2 ~> L2 s-2].}
1590   \textcolor{keywordtype}{real} :: pen\_en\_contrib  \textcolor{comment}{! Penetrating SW contributions to the changes in}
1591                           \textcolor{comment}{! TKE, divided by layer thickness in m [L2 T2 ~> m2 s-2].}
1592   \textcolor{keywordtype}{real} :: cpen1     \textcolor{comment}{! A temporary variable [L2 T-2 ~> m2 s-2].}
1593   \textcolor{keywordtype}{real} :: dmke      \textcolor{comment}{! A temporary variable related to the release of mean}
1594                     \textcolor{comment}{! kinetic energy [H Z L2 T-2 ~> m4 s-2 or kg m s-2]}
1595   \textcolor{keywordtype}{real} :: tke\_ent   \textcolor{comment}{! The TKE that remains if h\_ent were entrained [Z L2 T-2 ~> m3 s-2].}
1596   \textcolor{keywordtype}{real} :: tke\_ent1  \textcolor{comment}{! The TKE that would remain, without considering the}
1597                     \textcolor{comment}{! release of mean kinetic energy [Z L2 T-2 ~> m3 s-2].}
1598   \textcolor{keywordtype}{real} :: dtke\_dh   \textcolor{comment}{! The partial derivative of TKE with h\_ent [Z L2 T-2 H-1 ~> m2 s-2 or m5 s-2 kg-1].}
1599   \textcolor{keywordtype}{real} :: pen\_dtke\_dh\_contrib \textcolor{comment}{! The penetrating shortwave contribution to}
1600                     \textcolor{comment}{! dTKE\_dh [L2 T-2 ~> m2 s-2].}
1601   \textcolor{keywordtype}{real} :: ef4\_val   \textcolor{comment}{! The result of EF4() (see later) [H-1 ~> m-1 or m2 kg-1].}
1602   \textcolor{keywordtype}{real} :: h\_neglect \textcolor{comment}{! A thickness that is so small it is usually lost}
1603                     \textcolor{comment}{! in roundoff and can be neglected [H ~> m or kg m-2].}
1604   \textcolor{keywordtype}{real} :: def4\_dh   \textcolor{comment}{! The partial derivative of EF4 with h [H-2 ~> m-2 or m4 kg-2].}
1605   \textcolor{keywordtype}{real} :: pen\_en1   \textcolor{comment}{! A nondimensional temporary variable.}
1606   \textcolor{keywordtype}{real} :: kh, exp\_kh  \textcolor{comment}{! Nondimensional temporary variables related to the}
1607   \textcolor{keywordtype}{real} :: f1\_kh       \textcolor{comment}{! fractional decay of TKE across a layer.}
1608   \textcolor{keywordtype}{real} :: x1, e\_x1      \textcolor{comment}{!   Nondimensional temporary variables related to}
1609   \textcolor{keywordtype}{real} :: f1\_x1, f2\_x1  \textcolor{comment}{! the relative decay of TKE and SW radiation across}
1610   \textcolor{keywordtype}{real} :: f3\_x1         \textcolor{comment}{! a layer, and exponential-related functions of x1.}
1611   \textcolor{keywordtype}{real} :: e\_hxhpe   \textcolor{comment}{! Entrainment divided by the product of the new and old}
1612                     \textcolor{comment}{! thicknesses [H-1 ~> m-1 or m2 kg-1].}
1613   \textcolor{keywordtype}{real} :: hmix\_min  \textcolor{comment}{! The minimum mixed layer depth [H ~> m or kg m-2].}
1614   \textcolor{keywordtype}{real} :: opacity
1615   \textcolor{keywordtype}{real} :: c1\_3, c1\_6, c1\_24   \textcolor{comment}{!  1/3, 1/6, and 1/24.}
1616   \textcolor{keywordtype}{integer} :: is, ie, nz, i, k, ks, itt, n
1617 
1618   c1\_3 = 1.0/3.0 ; c1\_6 = 1.0/6.0 ; c1\_24 = 1.0/24.0
1619   g\_h\_2rho0 = (gv%g\_Earth * gv%H\_to\_Z) / (2.0 * gv%Rho0)
1620   hmix\_min = cs%Hmix\_min
1621   h\_neglect = gv%H\_subroundoff
1622   is = g%isc ; ie = g%iec ; nz = gv%ke
1623 
1624   \textcolor{keywordflow}{do} ks=1,nz
1625 
1626     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (ksort(i,ks) > 0) \textcolor{keywordflow}{then}
1627       k = ksort(i,ks)
1628 
1629       h\_avail = h(i,k) - eps(i,k)
1630       \textcolor{keywordflow}{if} ((h\_avail > 0.) .and. ((tke(i) > 0.) .or. (htot(i) < hmix\_min))) \textcolor{keywordflow}{then}
1631         drl = g\_h\_2rho0 * (r0(i,k)*htot(i) - r0\_tot(i) )
1632         dmke = (gv%H\_to\_Z * cs%bulk\_Ri\_ML) * 0.5 * &
1633             ((uhtot(i)-u(i,k)*htot(i))**2 + (vhtot(i)-v(i,k)*htot(i))**2)
1634 
1635 \textcolor{comment}{! Find the TKE that would remain if the entire layer were entrained.}
1636         kh = idecay\_len\_tke(i)*h\_avail ; exp\_kh = exp(-kh)
1637         \textcolor{keywordflow}{if} (kh >= 2.0e-5) \textcolor{keywordflow}{then} ; f1\_kh = (1.0-exp\_kh) / kh
1638         \textcolor{keywordflow}{else} ; f1\_kh = (1.0 - kh*(0.5 - c1\_6*kh)) ;\textcolor{keywordflow}{ endif}
1639 
1640         pen\_en\_contrib = 0.0
1641         \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}
1642           opacity = opacity\_band(n,i,k)
1643 \textcolor{comment}{! Two different forms are used here to make sure that only negative}
1644 \textcolor{comment}{! values are taken into exponentials to avoid excessively large}
1645 \textcolor{comment}{! numbers.  They are, of course, mathematically identical.}
1646           \textcolor{keywordflow}{if} (idecay\_len\_tke(i) > opacity) \textcolor{keywordflow}{then}
1647             x1 = (idecay\_len\_tke(i) - opacity) * h\_avail
1648             \textcolor{keywordflow}{if} (x1 >= 2.0e-5) \textcolor{keywordflow}{then}
1649               e\_x1 = exp(-x1) ; f1\_x1 = ((1.0-e\_x1)/(x1))
1650               f3\_x1 = ((e\_x1-(1.0-x1))/(x1*x1))
1651             \textcolor{keywordflow}{else}
1652               f1\_x1 = (1.0 - x1*(0.5 - c1\_6*x1))
1653               f3\_x1 = (0.5 - x1*(c1\_6 - c1\_24*x1))
1654 \textcolor{keywordflow}{            endif}
1655 
1656             pen\_en1 = exp(-opacity*h\_avail) * &
1657                ((1.0+opacity*htot(i))*f1\_x1 + opacity*h\_avail*f3\_x1)
1658           \textcolor{keywordflow}{else}
1659             x1 = (opacity - idecay\_len\_tke(i)) * h\_avail
1660             \textcolor{keywordflow}{if} (x1 >= 2.0e-5) \textcolor{keywordflow}{then}
1661               e\_x1 = exp(-x1) ; f1\_x1 = ((1.0-e\_x1)/(x1))
1662               f2\_x1 = ((1.0-(1.0+x1)*e\_x1)/(x1*x1))
1663             \textcolor{keywordflow}{else}
1664               f1\_x1 = (1.0 - x1*(0.5 - c1\_6*x1))
1665               f2\_x1 = (0.5 - x1*(c1\_3 - 0.125*x1))
1666 \textcolor{keywordflow}{            endif}
1667 
1668             pen\_en1 = exp\_kh * ((1.0+opacity*htot(i))*f1\_x1 + &
1669                                  opacity*h\_avail*f2\_x1)
1670 \textcolor{keywordflow}{          endif}
1671           pen\_en\_contrib = pen\_en\_contrib + &
1672             (g\_h\_2rho0*dr0\_dt(i)*pen\_sw\_bnd(n,i)) * (pen\_en1 - f1\_kh)
1673 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}
1674 
1675         hpe = htot(i)+h\_avail
1676         mke\_rate = 1.0/(1.0 + (cmke(1,i)*hpe + cmke(2,i)*hpe**2))
1677         ef4\_val = ef4(htot(i)+h\_neglect,h\_avail,idecay\_len\_tke(i))
1678         tke\_full\_ent = (exp\_kh*tke(i) - (h\_avail*gv%H\_to\_Z)*(drl*f1\_kh + pen\_en\_contrib)) + &
1679             mke\_rate*dmke*ef4\_val
1680         \textcolor{keywordflow}{if} ((tke\_full\_ent >= 0.0) .or. (h\_avail+htot(i) <= hmix\_min)) \textcolor{keywordflow}{then}
1681           \textcolor{comment}{! The layer will be fully entrained.}
1682           h\_ent = h\_avail
1683 
1684           \textcolor{keywordflow}{if} (cs%TKE\_diagnostics) \textcolor{keywordflow}{then}
1685             e\_hxhpe = h\_ent / ((htot(i)+h\_neglect)*(htot(i)+h\_ent+h\_neglect))
1686             cs%diag\_TKE\_mech\_decay(i,j) = cs%diag\_TKE\_mech\_decay(i,j) + &
1687                 idt\_diag * ((exp\_kh-1.0)* tke(i) + (h\_ent*gv%H\_to\_Z)*drl*(1.0-f1\_kh) + &
1688                             mke\_rate*dmke*(ef4\_val-e\_hxhpe))
1689             cs%diag\_TKE\_mixing(i,j) = cs%diag\_TKE\_mixing(i,j) - &
1690                 idt\_diag*(gv%H\_to\_Z*h\_ent)*drl
1691             cs%diag\_TKE\_pen\_SW(i,j) = cs%diag\_TKE\_pen\_SW(i,j) - &
1692                 idt\_diag*(gv%H\_to\_Z*h\_ent)*pen\_en\_contrib
1693             cs%diag\_TKE\_RiBulk(i,j) = cs%diag\_TKE\_RiBulk(i,j) + &
1694                 idt\_diag*mke\_rate*dmke*e\_hxhpe
1695 \textcolor{keywordflow}{          endif}
1696 
1697           tke(i) = tke\_full\_ent
1698           \textcolor{comment}{!### The minimum TKE value in this line may be problematically small.}
1699           \textcolor{keywordflow}{if} (tke(i) <= 0.0) tke(i) = 1.0e-150*us%m\_to\_Z*us%m\_s\_to\_L\_T**2
1700         \textcolor{keywordflow}{else}
1701 \textcolor{comment}{! The layer is only partially entrained.  The amount that will be}
1702 \textcolor{comment}{! entrained is determined iteratively.  No further layers will be}
1703 \textcolor{comment}{! entrained.}
1704           h\_min = 0.0 ; h\_max = h\_avail
1705           \textcolor{keywordflow}{if} (tke(i) <= 0.0) \textcolor{keywordflow}{then}
1706             h\_ent = 0.0
1707           \textcolor{keywordflow}{else}
1708             h\_ent = h\_avail * tke(i) / (tke(i) - tke\_full\_ent)
1709 
1710             \textcolor{keywordflow}{do} itt=1,15
1711               \textcolor{comment}{! Evaluate the TKE that would remain if h\_ent were entrained.}
1712 
1713               kh = idecay\_len\_tke(i)*h\_ent ; exp\_kh = exp(-kh)
1714               \textcolor{keywordflow}{if} (kh >= 2.0e-5) \textcolor{keywordflow}{then}
1715                 f1\_kh = (1.0-exp\_kh) / kh
1716               \textcolor{keywordflow}{else}
1717                 f1\_kh = (1.0 - kh*(0.5 - c1\_6*kh))
1718 \textcolor{keywordflow}{              endif}
1719 
1720 
1721               pen\_en\_contrib = 0.0 ; pen\_dtke\_dh\_contrib = 0.0
1722               \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}
1723                 \textcolor{comment}{! Two different forms are used here to make sure that only negative}
1724                 \textcolor{comment}{! values are taken into exponentials to avoid excessively large}
1725                 \textcolor{comment}{! numbers.  They are, of course, mathematically identical.}
1726                 opacity = opacity\_band(n,i,k)
1727                 sw\_trans = exp(-h\_ent*opacity)
1728                 \textcolor{keywordflow}{if} (idecay\_len\_tke(i) > opacity) \textcolor{keywordflow}{then}
1729                   x1 = (idecay\_len\_tke(i) - opacity) * h\_ent
1730                   \textcolor{keywordflow}{if} (x1 >= 2.0e-5) \textcolor{keywordflow}{then}
1731                     e\_x1 = exp(-x1) ; f1\_x1 = ((1.0-e\_x1)/(x1))
1732                     f3\_x1 = ((e\_x1-(1.0-x1))/(x1*x1))
1733                   \textcolor{keywordflow}{else}
1734                     f1\_x1 = (1.0 - x1*(0.5 - c1\_6*x1))
1735                     f3\_x1 = (0.5 - x1*(c1\_6 - c1\_24*x1))
1736 \textcolor{keywordflow}{                  endif}
1737                   pen\_en1 = sw\_trans * ((1.0+opacity*htot(i))*f1\_x1 + &
1738                                           opacity*h\_ent*f3\_x1)
1739                 \textcolor{keywordflow}{else}
1740                   x1 = (opacity - idecay\_len\_tke(i)) * h\_ent
1741                   \textcolor{keywordflow}{if} (x1 >= 2.0e-5) \textcolor{keywordflow}{then}
1742                     e\_x1 = exp(-x1) ; f1\_x1 = ((1.0-e\_x1)/(x1))
1743                     f2\_x1 = ((1.0-(1.0+x1)*e\_x1)/(x1*x1))
1744                   \textcolor{keywordflow}{else}
1745                     f1\_x1 = (1.0 - x1*(0.5 - c1\_6*x1))
1746                     f2\_x1 = (0.5 - x1*(c1\_3 - 0.125*x1))
1747 \textcolor{keywordflow}{                  endif}
1748 
1749                   pen\_en1 = exp\_kh * ((1.0+opacity*htot(i))*f1\_x1 + &
1750                                         opacity*h\_ent*f2\_x1)
1751 \textcolor{keywordflow}{                endif}
1752                 cpen1 = g\_h\_2rho0*dr0\_dt(i)*pen\_sw\_bnd(n,i)
1753                 pen\_en\_contrib = pen\_en\_contrib + cpen1*(pen\_en1 - f1\_kh)
1754                 pen\_dtke\_dh\_contrib = pen\_dtke\_dh\_contrib + &
1755                            cpen1*((1.0-sw\_trans) - opacity*(htot(i) + h\_ent)*sw\_trans)
1756 \textcolor{keywordflow}{              endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! (Pen\_SW\_bnd(n,i) > 0.0)}
1757 
1758               tke\_ent1 = exp\_kh* tke(i) - (h\_ent*gv%H\_to\_Z)*(drl*f1\_kh + pen\_en\_contrib)
1759               ef4\_val = ef4(htot(i)+h\_neglect,h\_ent,idecay\_len\_tke(i),def4\_dh)
1760               hpe = htot(i)+h\_ent
1761               mke\_rate = 1.0/(1.0 + (cmke(1,i)*hpe + cmke(2,i)*hpe**2))
1762               tke\_ent = tke\_ent1 + dmke*ef4\_val*mke\_rate
1763               \textcolor{comment}{! TKE\_ent is the TKE that would remain if h\_ent were entrained.}
1764 
1765               dtke\_dh = ((-idecay\_len\_tke(i)*tke\_ent1 - drl*gv%H\_to\_Z) + &
1766                          pen\_dtke\_dh\_contrib*gv%H\_to\_Z) + dmke * mke\_rate* &
1767                         (def4\_dh - ef4\_val*mke\_rate*(cmke(1,i)+2.0*cmke(2,i)*hpe))
1768               \textcolor{comment}{!  dh\_Newt = -TKE\_ent / dTKE\_dh}
1769               \textcolor{comment}{! Bisect if the Newton's method prediction is outside of the bounded range.}
1770               \textcolor{keywordflow}{if} (tke\_ent > 0.0) \textcolor{keywordflow}{then}
1771                 \textcolor{keywordflow}{if} ((h\_max-h\_ent)*(-dtke\_dh) > tke\_ent) \textcolor{keywordflow}{then}
1772                   dh\_newt = -tke\_ent / dtke\_dh
1773                 \textcolor{keywordflow}{else}
1774                   dh\_newt = 0.5*(h\_max-h\_ent)
1775 \textcolor{keywordflow}{                endif}
1776                 h\_min = h\_ent
1777               \textcolor{keywordflow}{else}
1778                 \textcolor{keywordflow}{if} ((h\_min-h\_ent)*(-dtke\_dh) < tke\_ent) \textcolor{keywordflow}{then}
1779                   dh\_newt = -tke\_ent / dtke\_dh
1780                 \textcolor{keywordflow}{else}
1781                   dh\_newt = 0.5*(h\_min-h\_ent)
1782 \textcolor{keywordflow}{                endif}
1783                 h\_max = h\_ent
1784 \textcolor{keywordflow}{              endif}
1785               h\_ent = h\_ent + dh\_newt
1786 
1787               \textcolor{keywordflow}{if} (abs(dh\_newt) < 0.2*gv%Angstrom\_H) \textcolor{keywordflow}{exit}
1788 \textcolor{keywordflow}{            enddo}
1789 \textcolor{keywordflow}{          endif}
1790 
1791           \textcolor{keywordflow}{if} (h\_ent < hmix\_min-htot(i)) h\_ent = hmix\_min - htot(i)
1792 
1793           \textcolor{keywordflow}{if} (cs%TKE\_diagnostics) \textcolor{keywordflow}{then}
1794             hpe = htot(i)+h\_ent
1795             mke\_rate = 1.0/(1.0 + cmke(1,i)*hpe + cmke(2,i)*hpe**2)
1796             ef4\_val = ef4(htot(i)+h\_neglect,h\_ent,idecay\_len\_tke(i))
1797 
1798             e\_hxhpe = h\_ent / ((htot(i)+h\_neglect)*(hpe+h\_neglect))
1799             cs%diag\_TKE\_mech\_decay(i,j) = cs%diag\_TKE\_mech\_decay(i,j) + &
1800                 idt\_diag * ((exp\_kh-1.0)* tke(i) + (h\_ent*gv%H\_to\_Z)*drl*(1.0-f1\_kh) + &
1801                              dmke*mke\_rate*(ef4\_val-e\_hxhpe))
1802             cs%diag\_TKE\_mixing(i,j) = cs%diag\_TKE\_mixing(i,j) - &
1803                 idt\_diag*(h\_ent*gv%H\_to\_Z)*drl
1804             cs%diag\_TKE\_pen\_SW(i,j) = cs%diag\_TKE\_pen\_SW(i,j) - &
1805                 idt\_diag*(h\_ent*gv%H\_to\_Z)*pen\_en\_contrib
1806             cs%diag\_TKE\_RiBulk(i,j) = cs%diag\_TKE\_RiBulk(i,j) + &
1807                 idt\_diag*dmke*mke\_rate*e\_hxhpe
1808 \textcolor{keywordflow}{          endif}
1809 
1810           tke(i) = 0.0
1811 \textcolor{keywordflow}{        endif} \textcolor{comment}{! TKE\_full\_ent > 0.0}
1812 
1813         pen\_absorbed = 0.0
1814         \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}
1815           sw\_trans = exp(-h\_ent*opacity\_band(n,i,k))
1816           pen\_absorbed = pen\_absorbed + pen\_sw\_bnd(n,i) * (1.0 - sw\_trans)
1817           pen\_sw\_bnd(n,i) = pen\_sw\_bnd(n,i) * sw\_trans
1818 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}
1819 
1820         htot(i)   = htot(i)   + h\_ent
1821         r0\_tot(i) = r0\_tot(i) + (h\_ent * r0(i,k) + pen\_absorbed*dr0\_dt(i))
1822         h(i,k)    = h(i,k)    - h\_ent
1823         d\_eb(i,k) = d\_eb(i,k) - h\_ent
1824 
1825         stot(i)    = stot(i)    + h\_ent * s(i,k)
1826         ttot(i)    = ttot(i)    + (h\_ent * t(i,k) + pen\_absorbed)
1827         rcv\_tot(i) = rcv\_tot(i) + (h\_ent*rcv(i,k) + pen\_absorbed*drcv\_dt(i))
1828 
1829         uhtot(i) = uhtot(i) + u(i,k)*h\_ent
1830         vhtot(i) = vhtot(i) + v(i,k)*h\_ent
1831 \textcolor{keywordflow}{      endif} \textcolor{comment}{! h\_avail > 0.0 .AND TKE(i) > 0.0}
1832 
1833 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i loop}
1834 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! k loop}
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}}
\subsubsection{\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(\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{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in,out}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em d\+\_\+eb} & The downward increase across a layer in the\\
\hline
\mbox{\tt out}  & {\em htot} & The accumulated mixed layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt out}  & {\em ttot} & The depth integrated mixed layer temperature \mbox{[}degC H $\sim$$>$ degC m or degC kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt out}  & {\em stot} & The depth integrated mixed layer salinity \mbox{[}ppt H $\sim$$>$ ppt m or ppt kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt 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{\tt 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{\tt 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{\tt 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{\tt in}  & {\em u} & Zonal velocities interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em v} & Zonal velocities interpolated to h points \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em t} & Layer temperatures \mbox{[}degC\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em s} & Layer salinities \mbox{[}ppt\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em r0} & Potential density referenced to\\
\hline
\mbox{\tt in}  & {\em rcv} & The coordinate defining potential\\
\hline
\mbox{\tt in}  & {\em eps} & The negligibly small amount of water\\
\hline
\mbox{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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{\tt in}  & {\em nsw} & The number of bands of penetrating shortwave radiation.\\
\hline
\mbox{\tt 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{\tt 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{\tt 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{\tt 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{\tt in}  & {\em j} & The j-\/index to work on.\\
\hline
\mbox{\tt in}  & {\em ksort} & The density-\/sorted k-\/indices.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em cs} & The control structure for this module.\\
\hline
\mbox{\tt in,out}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs.\\
\hline
\mbox{\tt in,out}  & {\em fluxes} & A structure containing pointers to any possible forcing fields. Unused fields have N\+U\+LL ptrs.\\
\hline
\mbox{\tt in}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}.\\
\hline
\mbox{\tt 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}
943   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(in)}    :: g\textcolor{comment}{     !< The ocean's grid structure.}
944   \textcolor{keywordtype}{type}(verticalgrid\_type),  \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{    !< The ocean's vertical grid structure.}
945   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
946                             \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{     !< Layer thickness [H ~> m or kg m-2].}
947 \textcolor{comment}{                                                   !! The units of h are referred to as H below.}
948   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
949                             \textcolor{keywordtype}{intent(inout)} :: d\_eb\textcolor{comment}{  !< The downward increase across a layer in the}
950 \textcolor{comment}{                                                   !! layer in the entrainment from below [H ~> m or kg
       m-2].}
951 \textcolor{comment}{                                                   !! Positive values go with mass gain by a layer.}
952   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: htot\textcolor{comment}{  !< The accumulated mixed layer thickness [H ~> m or kg
       m-2].}
953   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: ttot\textcolor{comment}{  !< The depth integrated mixed layer temperature}
954 \textcolor{comment}{                                                   !! [degC H ~> degC m or degC kg m-2].}
955   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: stot\textcolor{comment}{  !< The depth integrated mixed layer salinity}
956 \textcolor{comment}{                                                   !! [ppt H ~> ppt m or ppt kg m-2].}
957   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: uhtot\textcolor{comment}{ !< The depth integrated mixed layer zonal}
958 \textcolor{comment}{                                                   !! velocity [H L T-1 ~> m2 s-1 or kg m-1 s-1].}
959   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: vhtot\textcolor{comment}{ !< The integrated mixed layer meridional}
960 \textcolor{comment}{                                                   !! velocity [H L T-1 ~> m2 s-1 or kg m-1 s-1].}
961   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: r0\_tot\textcolor{comment}{ !< The integrated mixed layer potential density
       referenced}
962 \textcolor{comment}{                                                   !! to 0 pressure [H R ~> kg m-2 or kg2 m-5].}
963   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: rcv\_tot\textcolor{comment}{ !< The integrated mixed layer coordinate}
964 \textcolor{comment}{                                                   !! variable potential density [H R ~> kg m-2 or kg2
       m-5].}
965   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
966                             \textcolor{keywordtype}{intent(in)}    :: u\textcolor{comment}{     !< Zonal velocities interpolated to h points [L T-1 ~> m
       s-1].}
967   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
968                             \textcolor{keywordtype}{intent(in)}    :: v\textcolor{comment}{     !< Zonal velocities interpolated to h points [L T-1 ~> m
       s-1].}
969   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
970                             \textcolor{keywordtype}{intent(in)}    :: t\textcolor{comment}{     !< Layer temperatures [degC].}
971   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
972                             \textcolor{keywordtype}{intent(in)}    :: s\textcolor{comment}{     !< Layer salinities [ppt].}
973   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
974                             \textcolor{keywordtype}{intent(in)}    :: r0\textcolor{comment}{    !< Potential density referenced to}
975 \textcolor{comment}{                                                   !! surface pressure [R ~> kg m-3].}
976   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
977                             \textcolor{keywordtype}{intent(in)}    :: rcv\textcolor{comment}{   !< The coordinate defining potential}
978 \textcolor{comment}{                                                   !! density [R ~> kg m-3].}
979   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
980                             \textcolor{keywordtype}{intent(in)}    :: eps\textcolor{comment}{   !< The negligibly small amount of water}
981 \textcolor{comment}{                                                   !! that will be left in each layer [H ~> m or kg m-2].}
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}
983 \textcolor{comment}{                                                   !! temperature [R degC-1 ~> kg m-3 degC-1].}
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}
985 \textcolor{comment}{                                                   !! temperature [R degC-1 ~> kg m-3 degC-1].}
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}
987 \textcolor{comment}{                                                   !! salinity [R ppt-1 ~> kg m-3 ppt-1].}
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}
989 \textcolor{comment}{                                                   !! salinity [R ppt-1 ~> kg m-3 ppt-1].}
990   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: netmassinout\textcolor{comment}{ !< The net mass flux (if non-Boussinesq)}
991 \textcolor{comment}{                                                   !! or volume flux (if Boussinesq) into the ocean}
992 \textcolor{comment}{                                                   !! within a time step [H ~> m or kg m-2]. (I.e. P+R-E.)}
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}
994 \textcolor{comment}{                                                   !! within a time step [H ~> m or kg m-2].}
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}
996 \textcolor{comment}{                                                   !! step [degC H ~> degC m or degC kg m-2].  Any
       penetrating}
997 \textcolor{comment}{                                                   !! shortwave radiation is not included in Net\_heat.}
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}
999 \textcolor{comment}{                                                   !! over a time step [ppt H ~> ppt m or ppt kg m-2].}
1000   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}    :: nsw\textcolor{comment}{   !< The number of bands of penetrating}
1001 \textcolor{comment}{                                                   !! shortwave radiation.}
1002   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(max(nsw,1),SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: pen\_sw\_bnd\textcolor{comment}{ !< The penetrating shortwave}
1003 \textcolor{comment}{                                                   !! heating at the sea surface in each penetrating}
1004 \textcolor{comment}{                                                   !! band [degC H ~> degC m or degC kg m-2].}
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}
1006 \textcolor{comment}{                                                   !! penetrating shortwave radiation [H-1 ~> m-1 or m2
       kg-1].}
1007   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: conv\_en\textcolor{comment}{ !< The buoyant turbulent kinetic energy source}
1008 \textcolor{comment}{                                                   !! due to free convection [Z L2 T-2 ~> m3 s-2].}
1009   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(out)}   :: dke\_fc\textcolor{comment}{ !< The vertically integrated change in kinetic}
1010 \textcolor{comment}{                                                   !! energy due to free convection [Z L2 T-2 ~> m3 s-2].}
1011   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{     !< The j-index to work on.}
1012   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, &
1013                             \textcolor{keywordtype}{intent(in)}    :: ksort\textcolor{comment}{ !< The density-sorted k-indices.}
1014   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{    !< A dimensional unit scaling type}
1015   \textcolor{keywordtype}{type}(bulkmixedlayer\_cs),  \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{    !< The control structure for this module.}
1016   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),    \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{    !< A structure containing pointers to any}
1017 \textcolor{comment}{                                                   !! available thermodynamic fields. Absent}
1018 \textcolor{comment}{                                                   !! fields have NULL ptrs.}
1019   \textcolor{keywordtype}{type}(forcing),            \textcolor{keywordtype}{intent(inout)} :: fluxes\textcolor{comment}{  !< A structure containing pointers to any}
1020 \textcolor{comment}{                                                   !! possible forcing fields.  Unused fields}
1021 \textcolor{comment}{                                                   !! have NULL ptrs.}
1022   \textcolor{keywordtype}{real},                     \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{    !< Time increment [T ~> s].}
1023   \textcolor{keywordtype}{logical},                  \textcolor{keywordtype}{intent(in)}    :: aggregate\_fw\_forcing\textcolor{comment}{ !< If true, the net incoming and}
1024 \textcolor{comment}{                                                   !! outgoing surface freshwater fluxes are}
1025 \textcolor{comment}{                                                   !! combined before being applied, instead of}
1026 \textcolor{comment}{                                                   !! being applied separately.}
1027 
1028 \textcolor{comment}{!   This subroutine causes the mixed layer to entrain to the depth of free}
1029 \textcolor{comment}{! convection.  The depth of free convection is the shallowest depth at which the}
1030 \textcolor{comment}{! fluid is denser than the average of the fluid above.}
1031 
1032   \textcolor{comment}{! Local variables}
1033   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: &
1034     massoutrem, &      \textcolor{comment}{!   Evaporation that remains to be supplied [H ~> m or kg m-2].}
1035     netmassin          \textcolor{comment}{! mass entering through ocean surface [H ~> m or kg m-2]}
1036   \textcolor{keywordtype}{real} :: sw\_trans     \textcolor{comment}{!   The fraction of shortwave radiation}
1037                        \textcolor{comment}{! that is not absorbed in a layer [nondim].}
1038   \textcolor{keywordtype}{real} :: pen\_absorbed \textcolor{comment}{!   The amount of penetrative shortwave radiation}
1039                        \textcolor{comment}{! that is absorbed in a layer [degC H ~> degC m or degC kg m-2].}
1040   \textcolor{keywordtype}{real} :: h\_avail      \textcolor{comment}{!   The thickness in a layer available for}
1041                        \textcolor{comment}{! entrainment [H ~> m or kg m-2].}
1042   \textcolor{keywordtype}{real} :: h\_ent        \textcolor{comment}{!   The thickness from a layer that is entrained [H ~> m or kg m-2].}
1043   \textcolor{keywordtype}{real} :: t\_precip     \textcolor{comment}{!   The temperature of the precipitation [degC].}
1044   \textcolor{keywordtype}{real} :: c1\_3, c1\_6   \textcolor{comment}{!  1/3 and 1/6.}
1045   \textcolor{keywordtype}{real} :: en\_fn, frac, x1 \textcolor{comment}{!  Nondimensional temporary variables.}
1046   \textcolor{keywordtype}{real} :: dr, dr0      \textcolor{comment}{! Temporary variables [R H ~> kg m-2 or kg2 m-5].}
1047   \textcolor{keywordtype}{real} :: dr\_ent, dr\_comp \textcolor{comment}{! Temporary variables [R H ~> kg m-2 or kg2 m-5].}
1048   \textcolor{keywordtype}{real} :: dr\_dh        \textcolor{comment}{! The partial derivative of dr\_ent with h\_ent [R ~> kg m-3].}
1049   \textcolor{keywordtype}{real} :: h\_min, h\_max \textcolor{comment}{!   The minimum, maximum, and previous estimates for}
1050   \textcolor{keywordtype}{real} :: h\_prev       \textcolor{comment}{! h\_ent [H ~> m or kg m-2].}
1051   \textcolor{keywordtype}{real} :: h\_evap       \textcolor{comment}{!   The thickness that is evaporated [H ~> m or kg m-2].}
1052   \textcolor{keywordtype}{real} :: dh\_newt      \textcolor{comment}{!   The Newton's method estimate of the change in}
1053                        \textcolor{comment}{! h\_ent between iterations [H ~> m or kg m-2].}
1054   \textcolor{keywordtype}{real} :: g\_h2\_2rho0   \textcolor{comment}{!   Half the gravitational acceleration times the square of}
1055                        \textcolor{comment}{! the conversion from H to Z divided by the mean density,}
1056                        \textcolor{comment}{! [L2 Z T-3 H-2 R-1 ~> m4 s-3 kg-1 or m10 s-3 kg-3].}
1057   \textcolor{keywordtype}{real} :: angstrom     \textcolor{comment}{!   The minimum layer thickness [H ~> m or kg m-2].}
1058   \textcolor{keywordtype}{real} :: opacity      \textcolor{comment}{!   The opacity converted to inverse thickness units [H-1 ~> m-1 or m2 kg-1]}
1059   \textcolor{keywordtype}{real} :: sum\_pen\_en   \textcolor{comment}{!   The potential energy change due to penetrating}
1060                        \textcolor{comment}{! shortwave radiation, integrated over a layer}
1061                        \textcolor{comment}{! [H R ~> kg m-2 or kg2 m-5].}
1062   \textcolor{keywordtype}{real} :: idt          \textcolor{comment}{! 1.0/dt [T-1 ~> s-1]}
1063   \textcolor{keywordtype}{real} :: netheatout   \textcolor{comment}{! accumulated heat content of mass leaving ocean}
1064   \textcolor{keywordtype}{integer} :: is, ie, nz, i, k, ks, itt, n
1065   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(max(nsw,1))} :: &
1066     c2, &              \textcolor{comment}{! Temporary variable R H-1 ~> kg m-4 or m-1].}
1067     r\_sw\_top           \textcolor{comment}{! Temporary variables [H R ~> kg m-2 or kg2 m-5].}
1068 
1069   angstrom = gv%Angstrom\_H
1070   c1\_3 = 1.0/3.0 ; c1\_6 = 1.0/6.0
1071   g\_h2\_2rho0 = (gv%g\_Earth * gv%H\_to\_Z**2) / (2.0 * gv%Rho0)
1072   idt = 1.0 / dt
1073   is = g%isc ; ie = g%iec ; nz = gv%ke
1074 
1075   \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (ksort(i,1) > 0) \textcolor{keywordflow}{then}
1076     k = ksort(i,1)
1077 
1078     \textcolor{keywordflow}{if} (aggregate\_fw\_forcing) \textcolor{keywordflow}{then}
1079       massoutrem(i) = 0.0
1080       \textcolor{keywordflow}{if} (netmassinout(i) < 0.0) massoutrem(i) = -netmassinout(i)
1081       netmassin(i) = netmassinout(i) + massoutrem(i)
1082     \textcolor{keywordflow}{else}
1083       massoutrem(i) = -netmassout(i)
1084       netmassin(i)  = netmassinout(i) - netmassout(i)
1085 \textcolor{keywordflow}{    endif}
1086 
1087     \textcolor{comment}{! htot is an Angstrom (taken from layer 1) plus any net precipitation.}
1088     h\_ent     = max(min(angstrom,h(i,k)-eps(i,k)),0.0)
1089     htot(i)   = h\_ent + netmassin(i)
1090     h(i,k)    = h(i,k) - h\_ent
1091     d\_eb(i,k) = d\_eb(i,k) - h\_ent
1092 
1093     pen\_absorbed = 0.0
1094     \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}
1095       sw\_trans        = exp(-htot(i)*opacity\_band(n,i,k))
1096       pen\_absorbed    = pen\_absorbed + pen\_sw\_bnd(n,i) * (1.0-sw\_trans)
1097       pen\_sw\_bnd(n,i) = pen\_sw\_bnd(n,i) * sw\_trans
1098 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
1099 
1100     \textcolor{comment}{! Precipitation is assumed to have the same temperature and velocity}
1101     \textcolor{comment}{! as layer 1.  Because layer 1 might not be the topmost layer, this}
1102     \textcolor{comment}{! involves multiple terms.}
1103     t\_precip = t(i,1)
1104     ttot(i) = (net\_heat(i) + (netmassin(i) * t\_precip + h\_ent * t(i,k))) + &
1105               pen\_absorbed
1106     \textcolor{comment}{! Net\_heat contains both heat fluxes and the heat content of mass fluxes.}
1107  \textcolor{comment}{!! Ttot(i) = netMassIn(i) * T\_precip + h\_ent * T(i,k)}
1108  \textcolor{comment}{!! Ttot(i) = Net\_heat(i) + Ttot(i)}
1109  \textcolor{comment}{!! Ttot(i) = Ttot(i) + Pen\_absorbed}
1110   \textcolor{comment}{! smg:}
1111   \textcolor{comment}{! Ttot(i)   = (Net\_heat(i) + (h\_ent * T(i,k))) + Pen\_absorbed}
1112     stot(i)   = h\_ent*s(i,k) + net\_salt(i)
1113     uhtot(i)  = u(i,1)*netmassin(i) + u(i,k)*h\_ent
1114     vhtot(i)  = v(i,1)*netmassin(i) + v(i,k)*h\_ent
1115     r0\_tot(i) = (h\_ent*r0(i,k) + netmassin(i)*r0(i,1)) + &
1116 \textcolor{comment}{!                   dR0\_dT(i)*netMassIn(i)*(T\_precip - T(i,1)) + &}
1117                 (dr0\_dt(i)*(net\_heat(i) + pen\_absorbed) - &
1118                  dr0\_ds(i) * (netmassin(i) * s(i,1) - net\_salt(i)))
1119     rcv\_tot(i) = (h\_ent*rcv(i,k) + netmassin(i)*rcv(i,1)) + &
1120 \textcolor{comment}{!                    dRcv\_dT(i)*netMassIn(i)*(T\_precip - T(i,1)) + &}
1121                  (drcv\_dt(i)*(net\_heat(i) + pen\_absorbed) - &
1122                   drcv\_ds(i) * (netmassin(i) * s(i,1) - net\_salt(i)))
1123     conv\_en(i) = 0.0 ; dke\_fc(i) = 0.0
1124     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%heat\_content\_massin)) &
1125       fluxes%heat\_content\_massin(i,j) = fluxes%heat\_content\_massin(i,j) + &
1126                          t\_precip * netmassin(i) * gv%H\_to\_RZ * fluxes%C\_p * idt
1127     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv%TempxPmE)) tv%TempxPmE(i,j) = tv%TempxPmE(i,j) + &
1128                          t\_precip * netmassin(i) * gv%H\_to\_RZ
1129   \textcolor{keywordflow}{else}  \textcolor{comment}{! This is a massless column, but zero out the summed variables anyway for safety.}
1130     htot(i) = 0.0 ; ttot(i) = 0.0 ; stot(i) = 0.0 ; r0\_tot(i) = 0.0 ; rcv\_tot = 0.0
1131     uhtot(i) = 0.0 ; vhtot(i) = 0.0 ; conv\_en(i) = 0.0 ; dke\_fc(i) = 0.0
1132 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}
1133 
1134   \textcolor{comment}{! Now do netMassOut case in this block.}
1135   \textcolor{comment}{! At this point htot contains an Angstrom of fluid from layer 0 plus netMassIn.}
1136   \textcolor{keywordflow}{do} ks=1,nz
1137     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (ksort(i,ks) > 0) \textcolor{keywordflow}{then}
1138       k = ksort(i,ks)
1139 
1140       \textcolor{keywordflow}{if} ((htot(i) < angstrom) .and. (h(i,k) > eps(i,k))) \textcolor{keywordflow}{then}
1141         \textcolor{comment}{! If less than an Angstrom was available from the layers above plus}
1142         \textcolor{comment}{! any precipitation, add more fluid from this layer.}
1143         h\_ent = min(angstrom-htot(i), h(i,k)-eps(i,k))
1144         htot(i) = htot(i) + h\_ent
1145         h(i,k) = h(i,k) - h\_ent
1146         d\_eb(i,k) = d\_eb(i,k) - h\_ent
1147 
1148         r0\_tot(i) = r0\_tot(i) + h\_ent*r0(i,k)
1149         uhtot(i) = uhtot(i) + h\_ent*u(i,k)
1150         vhtot(i) = vhtot(i) + h\_ent*v(i,k)
1151 
1152         rcv\_tot(i) = rcv\_tot(i) + h\_ent*rcv(i,k)
1153         ttot(i) = ttot(i) + h\_ent*t(i,k)
1154         stot(i) = stot(i) + h\_ent*s(i,k)
1155 \textcolor{keywordflow}{      endif}
1156 
1157       \textcolor{comment}{! Water is removed from the topmost layers with any mass.}
1158       \textcolor{comment}{! We may lose layers if they are thin enough.}
1159       \textcolor{comment}{! The salt that is left behind goes into Stot.}
1160       \textcolor{keywordflow}{if} ((massoutrem(i) > 0.0) .and. (h(i,k) > eps(i,k))) \textcolor{keywordflow}{then}
1161         \textcolor{keywordflow}{if} (massoutrem(i) > (h(i,k) - eps(i,k))) \textcolor{keywordflow}{then}
1162           h\_evap = h(i,k) - eps(i,k)
1163           h(i,k) = eps(i,k)
1164           massoutrem(i) = massoutrem(i) - h\_evap
1165         \textcolor{keywordflow}{else}
1166           h\_evap = massoutrem(i)
1167           h(i,k) = h(i,k) - h\_evap
1168           massoutrem(i) = 0.0
1169 \textcolor{keywordflow}{        endif}
1170 
1171         stot(i) = stot(i) + h\_evap*s(i,k)
1172         r0\_tot(i) = r0\_tot(i) + dr0\_ds(i)*h\_evap*s(i,k)
1173         rcv\_tot(i) = rcv\_tot(i) + drcv\_ds(i)*h\_evap*s(i,k)
1174         d\_eb(i,k) = d\_eb(i,k) - h\_evap
1175 
1176         \textcolor{comment}{! smg: when resolve the A=B code, we will set}
1177         \textcolor{comment}{! heat\_content\_massout = heat\_content\_massout - T(i,k)*h\_evap*GV%H\_to\_RZ*fluxes%C\_p*Idt}
1178         \textcolor{comment}{! by uncommenting the lines here.}
1179         \textcolor{comment}{! we will also then completely remove TempXpme from the model.}
1180         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes%heat\_content\_massout)) &
1181           fluxes%heat\_content\_massout(i,j) = fluxes%heat\_content\_massout(i,j) - &
1182                                       t(i,k)*h\_evap*gv%H\_to\_RZ * fluxes%C\_p * idt
1183         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv%TempxPmE)) tv%TempxPmE(i,j) = tv%TempxPmE(i,j) - &
1184                                       t(i,k)*h\_evap*gv%H\_to\_RZ
1185 
1186 \textcolor{keywordflow}{      endif}
1187 
1188       \textcolor{comment}{! The following section calculates how much fluid will be entrained.}
1189       h\_avail = h(i,k) - eps(i,k)
1190       \textcolor{keywordflow}{if} (h\_avail > 0.0) \textcolor{keywordflow}{then}
1191         dr = r0\_tot(i) - htot(i)*r0(i,k)
1192         h\_ent = 0.0
1193 
1194         dr0 = dr
1195         \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}
1196           dr0 = dr0 - (dr0\_dt(i)*pen\_sw\_bnd(n,i)) * &
1197                       opacity\_band(n,i,k)*htot(i)
1198 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}
1199 
1200         \textcolor{comment}{! Some entrainment will occur from this layer.}
1201         \textcolor{keywordflow}{if} (dr0 > 0.0) \textcolor{keywordflow}{then}
1202           dr\_comp = dr
1203           \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}
1204             \textcolor{comment}{!   Compare the density at the bottom of a layer with the}
1205             \textcolor{comment}{! density averaged over the mixed layer and that layer.}
1206             opacity = opacity\_band(n,i,k)
1207             sw\_trans = exp(-h\_avail*opacity)
1208             dr\_comp = dr\_comp + (dr0\_dt(i)*pen\_sw\_bnd(n,i)) * &
1209                 ((1.0 - sw\_trans) - opacity*(htot(i)+h\_avail)*sw\_trans)
1210 \textcolor{keywordflow}{          endif} ;\textcolor{keywordflow}{ enddo}
1211           \textcolor{keywordflow}{if} (dr\_comp >= 0.0) \textcolor{keywordflow}{then}
1212             \textcolor{comment}{! The entire layer is entrained.}
1213             h\_ent = h\_avail
1214           \textcolor{keywordflow}{else}
1215             \textcolor{comment}{!  The layer is partially entrained.   Iterate to determine how much}
1216             \textcolor{comment}{!  entrainment occurs.  Solve for the h\_ent at which dr\_ent = 0.}
1217 
1218             \textcolor{comment}{! Instead of assuming that the curve is linear between the two end}
1219             \textcolor{comment}{! points, assume that the change is concentrated near small values}
1220             \textcolor{comment}{! of entrainment.  On average, this saves about 1 iteration.}
1221             frac = dr0 / (dr0 - dr\_comp)
1222             h\_ent = h\_avail * frac*frac
1223             h\_min = 0.0 ; h\_max = h\_avail
1224 
1225             \textcolor{keywordflow}{do} n=1,nsw
1226               r\_sw\_top(n) = dr0\_dt(i) * pen\_sw\_bnd(n,i)
1227               c2(n) = r\_sw\_top(n) * opacity\_band(n,i,k)**2
1228 \textcolor{keywordflow}{            enddo}
1229             \textcolor{keywordflow}{do} itt=1,10
1230               dr\_ent = dr ; dr\_dh = 0.0
1231               \textcolor{keywordflow}{do} n=1,nsw
1232                 opacity = opacity\_band(n,i,k)
1233                 sw\_trans = exp(-h\_ent*opacity)
1234                 dr\_ent = dr\_ent + r\_sw\_top(n) * ((1.0 - sw\_trans) - &
1235                            opacity*(htot(i)+h\_ent)*sw\_trans)
1236                 dr\_dh = dr\_dh + c2(n) * (htot(i)+h\_ent) * sw\_trans
1237 \textcolor{keywordflow}{              enddo}
1238 
1239               \textcolor{keywordflow}{if} (dr\_ent > 0.0) \textcolor{keywordflow}{then}
1240                 h\_min = h\_ent
1241               \textcolor{keywordflow}{else}
1242                 h\_max = h\_ent
1243 \textcolor{keywordflow}{              endif}
1244 
1245               dh\_newt = -dr\_ent / dr\_dh
1246               h\_prev = h\_ent ; h\_ent = h\_prev+dh\_newt
1247               \textcolor{keywordflow}{if} (h\_ent > h\_max) \textcolor{keywordflow}{then}
1248                 h\_ent = 0.5*(h\_prev+h\_max)
1249               \textcolor{keywordflow}{elseif} (h\_ent < h\_min) \textcolor{keywordflow}{then}
1250                 h\_ent = 0.5*(h\_prev+h\_min)
1251 \textcolor{keywordflow}{              endif}
1252 
1253               \textcolor{keywordflow}{if} (abs(dh\_newt) < 0.2*angstrom) \textcolor{keywordflow}{exit}
1254 \textcolor{keywordflow}{            enddo}
1255 
1256 \textcolor{keywordflow}{          endif}
1257 
1258           \textcolor{comment}{!  Now that the amount of entrainment (h\_ent) has been determined,}
1259           \textcolor{comment}{!  calculate changes in various terms.}
1260           sum\_pen\_en = 0.0 ; pen\_absorbed = 0.0
1261           \textcolor{keywordflow}{do} n=1,nsw ; \textcolor{keywordflow}{if} (pen\_sw\_bnd(n,i) > 0.0) \textcolor{keywordflow}{then}
1262             opacity = opacity\_band(n,i,k)
1263             sw\_trans = exp(-h\_ent*opacity)
1264 
1265             x1 = h\_ent*opacity
1266             \textcolor{keywordflow}{if} (x1 < 2.0e-5) \textcolor{keywordflow}{then}
1267               en\_fn = (opacity*htot(i)*(1.0 - 0.5*(x1 - c1\_3*x1)) + &
1268                        x1*x1*c1\_6)
1269             \textcolor{keywordflow}{else}
1270               en\_fn = ((opacity*htot(i) + 2.0) * &
1271                        ((1.0-sw\_trans) / x1) - 1.0 + sw\_trans)
1272 \textcolor{keywordflow}{            endif}
1273             sum\_pen\_en = sum\_pen\_en - (dr0\_dt(i)*pen\_sw\_bnd(n,i)) * en\_fn
1274 
1275             pen\_absorbed = pen\_absorbed + pen\_sw\_bnd(n,i) * (1.0 - sw\_trans)
1276             pen\_sw\_bnd(n,i) = pen\_sw\_bnd(n,i) * sw\_trans
1277 \textcolor{keywordflow}{          endif} ;\textcolor{keywordflow}{ enddo}
1278 
1279           conv\_en(i) = conv\_en(i) + g\_h2\_2rho0 * h\_ent * &
1280                        ( (r0\_tot(i) - r0(i,k)*htot(i)) + sum\_pen\_en )
1281 
1282           r0\_tot(i) = r0\_tot(i) + (h\_ent * r0(i,k) + pen\_absorbed*dr0\_dt(i))
1283           stot(i) = stot(i) + h\_ent * s(i,k)
1284           ttot(i) = ttot(i) + (h\_ent * t(i,k) + pen\_absorbed)
1285           rcv\_tot(i) = rcv\_tot(i) + (h\_ent * rcv(i,k) + pen\_absorbed*drcv\_dt(i))
1286 \textcolor{keywordflow}{        endif} \textcolor{comment}{! dr0 > 0.0}
1287 
1288         \textcolor{keywordflow}{if} (h\_ent > 0.0) \textcolor{keywordflow}{then}
1289           \textcolor{keywordflow}{if} (htot(i) > 0.0) &
1290             dke\_fc(i) = dke\_fc(i) + cs%bulk\_Ri\_convective * 0.5 * &
1291               ((gv%H\_to\_Z*h\_ent) / (htot(i)*(h\_ent+htot(i)))) * &
1292               ((uhtot(i)-u(i,k)*htot(i))**2 + (vhtot(i)-v(i,k)*htot(i))**2)
1293 
1294           htot(i)  = htot(i)  + h\_ent
1295           h(i,k) = h(i,k) - h\_ent
1296           d\_eb(i,k) = d\_eb(i,k) - h\_ent
1297           \textcolor{keywordflow}{if} (cs%convect\_mom\_bug) \textcolor{keywordflow}{then}
1298             uhtot(i) = u(i,k)*h\_ent ; vhtot(i) = v(i,k)*h\_ent
1299           \textcolor{keywordflow}{else}
1300             uhtot(i) = uhtot(i) + h\_ent*u(i,k) ; vhtot(i) = vhtot(i) + h\_ent*v(i,k)
1301 \textcolor{keywordflow}{          endif}
1302 \textcolor{keywordflow}{        endif}
1303 
1304 
1305 \textcolor{keywordflow}{      endif} \textcolor{comment}{! h\_avail>0}
1306 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i loop}
1307 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! k loop}
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}}
\subsubsection{\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(\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{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in,out}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. Layer 0 is the new mixed layer.\\
\hline
\mbox{\tt in,out}  & {\em t} & Potential temperature \mbox{[}degC\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em s} & Salinity \mbox{[}ppt\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em r0} & Potential density referenced to surface pressure \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em rcv} & The coordinate defining potential density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em rcvtgt} & The target value of Rcv for each layer \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em dt\+\_\+diag} & The accumulated time interval for diagnostics \mbox{[}T $\sim$$>$ s\mbox{]}.\\
\hline
\mbox{\tt 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{\tt 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{\tt in}  & {\em j} & The meridional row to work on.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em cs} & The control structure returned by a previous call to mixedlayer\+\_\+init.\\
\hline
\mbox{\tt 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{\tt 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{\tt 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}
3105   \textcolor{keywordtype}{type}(ocean\_grid\_type),              \textcolor{keywordtype}{intent(in)}    :: g\textcolor{comment}{    !< The ocean's grid structure.}
3106   \textcolor{keywordtype}{type}(verticalgrid\_type),            \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{   !< The ocean's vertical grid structure.}
3107   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{    !< Layer thickness [H ~> m or kg m-2].}
3108 \textcolor{comment}{                                                            !! Layer 0 is the new mixed layer.}
3109   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: t\textcolor{comment}{    !< Potential temperature [degC].}
3110   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: s\textcolor{comment}{    !< Salinity [ppt].}
3111   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: r0\textcolor{comment}{   !< Potential density referenced to}
3112 \textcolor{comment}{                                                            !! surface pressure [R ~> kg m-3].}
3113   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: rcv\textcolor{comment}{  !< The coordinate defining potential}
3114 \textcolor{comment}{                                                            !! density [R ~> kg m-3].}
3115   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZK\_(GV))},          \textcolor{keywordtype}{intent(in)}    :: rcvtgt\textcolor{comment}{ !< The target value of Rcv for each}
3116 \textcolor{comment}{                                                            !! layer [R ~> kg m-3].}
3117   \textcolor{keywordtype}{real},                               \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{   !< Time increment [T ~> s].}
3118   \textcolor{keywordtype}{real},                               \textcolor{keywordtype}{intent(in)}    :: dt\_diag\textcolor{comment}{ !< The accumulated time interval for}
3119 \textcolor{comment}{                                                            !! diagnostics [T ~> s].}
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}
3121 \textcolor{comment}{                                                            !! the entrainment from above}
3122 \textcolor{comment}{                                                            !! [H ~> m or kg m-2]. Positive d\_ea}
3123 \textcolor{comment}{                                                            !! goes with layer thickness increases.}
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}
3125 \textcolor{comment}{                                                            !! in the entrainment from below [H ~> m or kg
       m-2].}
3126 \textcolor{comment}{                                                            !! Positive values go with mass gain by}
3127 \textcolor{comment}{                                                            !! a layer.}
3128   \textcolor{keywordtype}{integer},                            \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{    !< The meridional row to work on.}
3129   \textcolor{keywordtype}{type}(unit\_scale\_type),              \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{   !< A dimensional unit scaling type}
3130   \textcolor{keywordtype}{type}(bulkmixedlayer\_cs),            \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{   !< The control structure returned by a}
3131 \textcolor{comment}{                                                            !! previous call to mixedlayer\_init.}
3132   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: drcv\_dt\textcolor{comment}{ !< The partial derivative of}
3133 \textcolor{comment}{                                                            !! coordinate defining potential density}
3134 \textcolor{comment}{                                                            !! with potential temperature}
3135 \textcolor{comment}{                                                            !! [R degC-1 ~> kg m-3 degC-1].}
3136   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: drcv\_ds\textcolor{comment}{    !< The partial derivative of}
3137 \textcolor{comment}{                                                            !! coordinate defining potential density}
3138 \textcolor{comment}{                                                            !! with salinity [R ppt-1 ~> kg m-3 ppt-1].}
3139   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: max\_bl\_det\textcolor{comment}{ !< If non-negative, the maximum}
3140 \textcolor{comment}{                                                            !! detrainment permitted from the buffer}
3141 \textcolor{comment}{                                                            !! layers [H ~> m or kg m-2].}
3142 
3143   \textcolor{comment}{! Local variables}
3144   \textcolor{keywordtype}{real} :: ih                  \textcolor{comment}{! The inverse of a thickness [H-1 ~> m-1 or m2 kg-1].}
3145   \textcolor{keywordtype}{real} :: h\_ent               \textcolor{comment}{! The thickness from a layer that is}
3146                               \textcolor{comment}{! entrained [H ~> m or kg m-2].}
3147   \textcolor{keywordtype}{real} :: max\_det\_rem(szi\_(g)) \textcolor{comment}{! Remaining permitted detrainment [H ~> m or kg m-2].}
3148   \textcolor{keywordtype}{real} :: detrain(szi\_(g))    \textcolor{comment}{! The thickness of fluid to detrain}
3149                               \textcolor{comment}{! from the mixed layer [H ~> m or kg m-2].}
3150   \textcolor{keywordtype}{real} :: dt\_dr, ds\_dr, drml, dr0\_drcv, dt\_ds\_wt2
3151   \textcolor{keywordtype}{real} :: i\_denom             \textcolor{comment}{! A work variable [ppt2 R-2 ~> ppt2 m6 kg-2].}
3152   \textcolor{keywordtype}{real} :: sdown, tdown
3153   \textcolor{keywordtype}{real} :: dt\_time             \textcolor{comment}{! The timestep divided by the detrainment timescale [nondim].}
3154   \textcolor{keywordtype}{real} :: g\_h2\_2rho0dt        \textcolor{comment}{! Half the gravitational acceleration times the square of the}
3155                               \textcolor{comment}{! conversion from H to m divided by the mean density times the time}
3156                               \textcolor{comment}{! step [L2 Z T-3 H-2 R-1 ~> m4 s-3 kg-1 or m10 s-3 kg-3].}
3157   \textcolor{keywordtype}{real} :: g\_h2\_2dt            \textcolor{comment}{! Half the gravitational acceleration times the square of the}
3158                               \textcolor{comment}{! conversion from H to Z divided by the diagnostic time step}
3159                               \textcolor{comment}{! [L2 Z H-2 T-3 ~> m s-3 or m7 kg-2 s-3].}
3160 
3161   \textcolor{keywordtype}{logical} :: splittable\_bl(szi\_(g)), orthogonal\_extrap
3162   \textcolor{keywordtype}{real} :: x1
3163 
3164   \textcolor{keywordtype}{integer} :: i, is, ie, k, k1, nkmb, nz
3165   is = g%isc ; ie = g%iec ; nz = gv%ke
3166   nkmb = cs%nkml+cs%nkbl
3167   \textcolor{keywordflow}{if} (cs%nkbl /= 1) \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"MOM\_mixed\_layer: "}// &
3168                         \textcolor{stringliteral}{"CS%nkbl must be 1 in mixedlayer\_detrain\_1."})
3169 
3170   dt\_time = dt / cs%BL\_detrain\_time
3171   g\_h2\_2rho0dt = (gv%g\_Earth * gv%H\_to\_Z**2) / (2.0 * gv%Rho0 * dt\_diag)
3172   g\_h2\_2dt = (gv%g\_Earth * gv%H\_to\_Z**2) / (2.0 * dt\_diag)
3173 
3174   \textcolor{comment}{! Move detrained water into the buffer layer.}
3175   \textcolor{keywordflow}{do} k=1,cs%nkml
3176     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (h(i,k) > 0.0) \textcolor{keywordflow}{then}
3177       ih = 1.0 / (h(i,nkmb) + h(i,k))
3178       \textcolor{keywordflow}{if} (cs%TKE\_diagnostics) &
3179         cs%diag\_TKE\_conv\_s2(i,j) = cs%diag\_TKE\_conv\_s2(i,j) + &
3180             g\_h2\_2rho0dt * h(i,k) * h(i,nkmb) * (r0(i,nkmb) - r0(i,k))
3181       \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain)) &
3182         cs%diag\_PE\_detrain(i,j) = cs%diag\_PE\_detrain(i,j) + &
3183             g\_h2\_2dt * h(i,k) * h(i,nkmb) * (r0(i,nkmb) - r0(i,k))
3184       \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain2)) &
3185         cs%diag\_PE\_detrain2(i,j) = cs%diag\_PE\_detrain2(i,j) + &
3186             g\_h2\_2dt * h(i,k) * h(i,nkmb) * (r0(i,nkmb) - r0(i,k))
3187 
3188       r0(i,nkmb) = (r0(i,nkmb)*h(i,nkmb) + r0(i,k)*h(i,k)) * ih
3189       rcv(i,nkmb) = (rcv(i,nkmb)*h(i,nkmb) + rcv(i,k)*h(i,k)) * ih
3190       t(i,nkmb) = (t(i,nkmb)*h(i,nkmb) + t(i,k)*h(i,k)) * ih
3191       s(i,nkmb) = (s(i,nkmb)*h(i,nkmb) + s(i,k)*h(i,k)) * ih
3192 
3193       d\_ea(i,k) = d\_ea(i,k) - h(i,k)
3194       d\_ea(i,nkmb) = d\_ea(i,nkmb) + h(i,k)
3195       h(i,nkmb) = h(i,nkmb) + h(i,k)
3196       h(i,k) = 0.0
3197 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
3198 \textcolor{keywordflow}{  enddo}
3199 
3200   \textcolor{keywordflow}{do} i=is,ie
3201     max\_det\_rem(i) = 10.0 * h(i,nkmb)
3202     \textcolor{keywordflow}{if} (max\_bl\_det(i) >= 0.0) max\_det\_rem(i) = max\_bl\_det(i)
3203 \textcolor{keywordflow}{  enddo}
3204 
3205 \textcolor{comment}{!   If the mixed layer was denser than the densest interior layer,}
3206 \textcolor{comment}{! but is now lighter than this layer, leaving a buffer layer that}
3207 \textcolor{comment}{! is denser than this layer, there are problems.  This should prob-}
3208 \textcolor{comment}{! ably be considered a case of an inadequate choice of resolution in}
3209 \textcolor{comment}{! density space and should be avoided.  To make the model run sens-}
3210 \textcolor{comment}{! ibly in this case, it will make the mixed layer denser while making}
3211 \textcolor{comment}{! the buffer layer the density of the densest interior layer (pro-}
3212 \textcolor{comment}{! vided that the this will not make the mixed layer denser than the}
3213 \textcolor{comment}{! interior layer).  Otherwise, make the mixed layer the same density}
3214 \textcolor{comment}{! as the densest interior layer and lighten the buffer layer with}
3215 \textcolor{comment}{! the released buoyancy.  With multiple buffer layers, much more}
3216 \textcolor{comment}{! graceful options are available.}
3217   \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (h(i,nkmb) > 0.0) \textcolor{keywordflow}{then}
3218     \textcolor{keywordflow}{if} ((r0(i,0)<r0(i,nz)) .and. (r0(i,nz)<r0(i,nkmb))) \textcolor{keywordflow}{then}
3219       \textcolor{keywordflow}{if} ((r0(i,nz)-r0(i,0))*h(i,0) > &
3220           (r0(i,nkmb)-r0(i,nz))*h(i,nkmb)) \textcolor{keywordflow}{then}
3221         detrain(i) = (r0(i,nkmb)-r0(i,nz))*h(i,nkmb) / (r0(i,nkmb)-r0(i,0))
3222       \textcolor{keywordflow}{else}
3223         detrain(i) = (r0(i,nz)-r0(i,0))*h(i,0) / (r0(i,nkmb)-r0(i,0))
3224 \textcolor{keywordflow}{      endif}
3225 
3226       d\_eb(i,cs%nkml) = d\_eb(i,cs%nkml) + detrain(i)
3227       d\_ea(i,cs%nkml) = d\_ea(i,cs%nkml) - detrain(i)
3228       d\_eb(i,nkmb) = d\_eb(i,nkmb) - detrain(i)
3229       d\_ea(i,nkmb) = d\_ea(i,nkmb) + detrain(i)
3230 
3231       \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain)) cs%diag\_PE\_detrain(i,j) = &
3232         cs%diag\_PE\_detrain(i,j) + g\_h2\_2dt * detrain(i)* &
3233                      (h(i,0) + h(i,nkmb)) * (r0(i,nkmb) - r0(i,0))
3234       x1 = r0(i,0)
3235       r0(i,0) = r0(i,0) - detrain(i)*(r0(i,0)-r0(i,nkmb)) / h(i,0)
3236       r0(i,nkmb) = r0(i,nkmb) - detrain(i)*(r0(i,nkmb)-x1) / h(i,nkmb)
3237       x1 = rcv(i,0)
3238       rcv(i,0) = rcv(i,0) - detrain(i)*(rcv(i,0)-rcv(i,nkmb)) / h(i,0)
3239       rcv(i,nkmb) = rcv(i,nkmb) - detrain(i)*(rcv(i,nkmb)-x1) / h(i,nkmb)
3240       x1 = t(i,0)
3241       t(i,0) = t(i,0) - detrain(i)*(t(i,0)-t(i,nkmb)) / h(i,0)
3242       t(i,nkmb) = t(i,nkmb) - detrain(i)*(t(i,nkmb)-x1) / h(i,nkmb)
3243       x1 = s(i,0)
3244       s(i,0) = s(i,0) - detrain(i)*(s(i,0)-s(i,nkmb)) / h(i,0)
3245       s(i,nkmb) = s(i,nkmb) - detrain(i)*(s(i,nkmb)-x1) / h(i,nkmb)
3246 
3247 \textcolor{keywordflow}{    endif}
3248 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}
3249 
3250   \textcolor{comment}{! Move water out of the buffer layer, if convenient.}
3251 \textcolor{comment}{!   Split the buffer layer if possible, and replace the buffer layer}
3252 \textcolor{comment}{! with a small amount of fluid from the mixed layer.}
3253 \textcolor{comment}{! This is the exponential-in-time splitting, circa 2005.}
3254   \textcolor{keywordflow}{do} i=is,ie
3255     \textcolor{keywordflow}{if} (h(i,nkmb) > 0.0) \textcolor{keywordflow}{then} ; splittable\_bl(i) = .true.
3256     \textcolor{keywordflow}{else} ; splittable\_bl(i) = .false. ;\textcolor{keywordflow}{ endif}
3257 \textcolor{keywordflow}{  enddo}
3258 
3259   dt\_ds\_wt2 = cs%dT\_dS\_wt**2
3260 
3261   \textcolor{keywordflow}{do} k=nz-1,nkmb+1,-1 ; \textcolor{keywordflow}{do} i=is,ie
3262     \textcolor{keywordflow}{if} (splittable\_bl(i)) \textcolor{keywordflow}{then}
3263       \textcolor{keywordflow}{if} (rcvtgt(k)<=rcv(i,nkmb)) \textcolor{keywordflow}{then}
3264 \textcolor{comment}{! Estimate dR/drho, dTheta/dR, and dS/dR, where R is the coordinate variable}
3265 \textcolor{comment}{! and rho is in-situ (or surface) potential density.}
3266 \textcolor{comment}{! There is no "right" way to do this, so this keeps things reasonable, if}
3267 \textcolor{comment}{! slightly arbitrary.}
3268         splittable\_bl(i) = .false.
3269 
3270         k1 = k+1 ; orthogonal\_extrap = .false.
3271         \textcolor{comment}{! Here we try to find a massive layer to use for interpolating the}
3272         \textcolor{comment}{! temperature and salinity.  If none is available a pseudo-orthogonal}
3273         \textcolor{comment}{! extrapolation is used.  The 10.0 and 0.9 in the following are}
3274         \textcolor{comment}{! arbitrary but probably about right.}
3275         \textcolor{keywordflow}{if} ((h(i,k+1) < 10.0*gv%Angstrom\_H) .or. &
3276             ((rcvtgt(k+1)-rcv(i,nkmb)) >= 0.9*(rcv(i,k1) - rcv(i,0)))) \textcolor{keywordflow}{then}
3277           \textcolor{keywordflow}{if} (k>=nz-1) \textcolor{keywordflow}{then} ; orthogonal\_extrap = .true.
3278           \textcolor{keywordflow}{elseif} ((h(i,k+2) <= 10.0*gv%Angstrom\_H) .and. &
3279               ((rcvtgt(k+1)-rcv(i,nkmb)) < 0.9*(rcv(i,k+2)-rcv(i,0)))) \textcolor{keywordflow}{then}
3280             k1 = k+2
3281           \textcolor{keywordflow}{else} ; orthogonal\_extrap = .true. ;\textcolor{keywordflow}{ endif}
3282 \textcolor{keywordflow}{        endif}
3283 
3284         \textcolor{keywordflow}{if} ((r0(i,0) >= r0(i,k1)) .or. (rcv(i,0) >= rcv(i,nkmb))) cycle
3285           \textcolor{comment}{! In this case there is an inversion of in-situ density relative to}
3286           \textcolor{comment}{! the coordinate variable.  Do not detrain from the buffer layer.}
3287 
3288         \textcolor{keywordflow}{if} (orthogonal\_extrap) \textcolor{keywordflow}{then}
3289           \textcolor{comment}{! 36 here is a typical oceanic value of (dR/dS) / (dR/dT) - it says}
3290           \textcolor{comment}{! that the relative weights of T & S changes is a plausible 6:1.}
3291           \textcolor{comment}{! Also, this was coded on Athena's 6th birthday!}
3292           i\_denom = 1.0 / (drcv\_ds(i)**2 + dt\_ds\_wt2*drcv\_dt(i)**2)
3293           dt\_dr = dt\_ds\_wt2*drcv\_dt(i) * i\_denom
3294           ds\_dr = drcv\_ds(i) * i\_denom
3295         \textcolor{keywordflow}{else}
3296           dt\_dr = (t(i,0) - t(i,k1)) / (rcv(i,0) - rcv(i,k1))
3297           ds\_dr = (s(i,0) - s(i,k1)) / (rcv(i,0) - rcv(i,k1))
3298 \textcolor{keywordflow}{        endif}
3299         drml = dt\_time * (r0(i,nkmb) - r0(i,0)) * &
3300                (rcv(i,0) - rcv(i,k1)) / (r0(i,0) - r0(i,k1))
3301         \textcolor{comment}{! Once again, there is an apparent density inversion in Rcv.}
3302         \textcolor{keywordflow}{if} (drml < 0.0) cycle
3303         dr0\_drcv = (r0(i,0) - r0(i,k1)) / (rcv(i,0) - rcv(i,k1))
3304 
3305         \textcolor{keywordflow}{if} ((rcv(i,nkmb) - drml < rcvtgt(k)) .and. (max\_det\_rem(i) > h(i,nkmb))) \textcolor{keywordflow}{then}
3306           \textcolor{comment}{! In this case, the buffer layer is split into two isopycnal layers.}
3307           detrain(i) = h(i,nkmb)*(rcv(i,nkmb) - rcvtgt(k)) / &
3308                                   (rcvtgt(k+1) - rcvtgt(k))
3309 
3310           \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain)) cs%diag\_PE\_detrain(i,j) = &
3311             cs%diag\_PE\_detrain(i,j) - g\_h2\_2dt * detrain(i) * &
3312                  (h(i,nkmb)-detrain(i)) * (rcvtgt(k+1) - rcvtgt(k)) * dr0\_drcv
3313 
3314           tdown = detrain(i) * (t(i,nkmb) + dt\_dr*(rcvtgt(k+1)-rcv(i,nkmb)))
3315           t(i,k) = (h(i,k) * t(i,k) + &
3316                         (h(i,nkmb) * t(i,nkmb) - tdown)) / &
3317                        (h(i,k) + (h(i,nkmb) - detrain(i)))
3318           t(i,k+1) = (h(i,k+1) * t(i,k+1) + tdown)/ &
3319                           (h(i,k+1) + detrain(i))
3320           t(i,nkmb) = t(i,0)
3321           sdown = detrain(i) * (s(i,nkmb) + ds\_dr*(rcvtgt(k+1)-rcv(i,nkmb)))
3322           s(i,k) = (h(i,k) * s(i,k) + &
3323                       (h(i,nkmb) * s(i,nkmb) - sdown)) / &
3324                       (h(i,k) + (h(i,nkmb) - detrain(i)))
3325           s(i,k+1) = (h(i,k+1) * s(i,k+1) + sdown)/ &
3326                          (h(i,k+1) + detrain(i))
3327           s(i,nkmb) = s(i,0)
3328           rcv(i,nkmb) = rcv(i,0)
3329 
3330           d\_ea(i,k+1) = d\_ea(i,k+1) + detrain(i)
3331           d\_ea(i,k) = d\_ea(i,k) + (h(i,nkmb) - detrain(i))
3332           d\_ea(i,nkmb) = d\_ea(i,nkmb) - h(i,nkmb)
3333 
3334           h(i,k+1) = h(i,k+1) + detrain(i)
3335           h(i,k) = h(i,k) + h(i,nkmb) - detrain(i)
3336           h(i,nkmb) = 0.0
3337         \textcolor{keywordflow}{else}
3338           \textcolor{comment}{! Here only part of the buffer layer is moved into the interior.}
3339           detrain(i) = h(i,nkmb) * drml / (rcvtgt(k+1) - rcv(i,nkmb) + drml)
3340           \textcolor{keywordflow}{if} (detrain(i) > max\_det\_rem(i)) detrain(i) = max\_det\_rem(i)
3341           ih = 1.0 / (h(i,k+1) + detrain(i))
3342 
3343           tdown = (t(i,nkmb) + dt\_dr*(rcvtgt(k+1)-rcv(i,nkmb)))
3344           t(i,nkmb) = t(i,nkmb) - dt\_dr * drml
3345           t(i,k+1) = (h(i,k+1) * t(i,k+1) + detrain(i) * tdown) * ih
3346           sdown = (s(i,nkmb) + ds\_dr*(rcvtgt(k+1)-rcv(i,nkmb)))
3347 \textcolor{comment}{!  The following two expressions updating S(nkmb) are mathematically identical.}
3348 \textcolor{comment}{!            S(i,nkmb) = (h(i,nkmb) * S(i,nkmb) - detrain(i) * Sdown) / &}
3349 \textcolor{comment}{!                           (h(i,nkmb) - detrain(i))}
3350           s(i,nkmb) = s(i,nkmb) - ds\_dr * drml
3351           s(i,k+1) = (h(i,k+1) * s(i,k+1) + detrain(i) * sdown) * ih
3352 
3353           d\_ea(i,k+1) = d\_ea(i,k+1) + detrain(i)
3354           d\_ea(i,nkmb) = d\_ea(i,nkmb) - detrain(i)
3355 
3356           h(i,k+1) = h(i,k+1) + detrain(i)
3357           h(i,nkmb) = h(i,nkmb) - detrain(i)
3358 
3359           \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain)) cs%diag\_PE\_detrain(i,j) = &
3360             cs%diag\_PE\_detrain(i,j) - g\_h2\_2dt * detrain(i) * dr0\_drcv * &
3361                  (h(i,nkmb)-detrain(i)) * (rcvtgt(k+1) - rcv(i,nkmb) + drml)
3362 \textcolor{keywordflow}{        endif}
3363 \textcolor{keywordflow}{      endif} \textcolor{comment}{! RcvTgt(k)<=Rcv(i,nkmb)}
3364 \textcolor{keywordflow}{    endif} \textcolor{comment}{! splittable\_BL}
3365 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i & k loops}
3366 
3367 \textcolor{comment}{!   The numerical behavior of the buffer layer is dramatically improved}
3368 \textcolor{comment}{! if it is always at least a small fraction (say 10%) of the thickness}
3369 \textcolor{comment}{! of the mixed layer.  As the physical distinction between the mixed}
3370 \textcolor{comment}{! and buffer layers is vague anyway, this seems hard to argue against.}
3371   \textcolor{keywordflow}{do} i=is,ie
3372     \textcolor{keywordflow}{if} (h(i,nkmb) < 0.1*h(i,0)) \textcolor{keywordflow}{then}
3373       h\_ent =  0.1*h(i,0) - h(i,nkmb)
3374       ih = 10.0/h(i,0)
3375       t(i,nkmb) = (h(i,nkmb)*t(i,nkmb) + h\_ent*t(i,0)) * ih
3376       s(i,nkmb) = (h(i,nkmb)*s(i,nkmb) + h\_ent*s(i,0)) * ih
3377 
3378       d\_ea(i,1) = d\_ea(i,1) - h\_ent
3379       d\_ea(i,nkmb) = d\_ea(i,nkmb) + h\_ent
3380 
3381       h(i,0) = h(i,0) - h\_ent
3382       h(i,nkmb) = h(i,nkmb) + h\_ent
3383 \textcolor{keywordflow}{    endif}
3384 \textcolor{keywordflow}{  enddo}
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}}
\subsubsection{\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(\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{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in,out}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. Layer 0 is the new mixed layer.\\
\hline
\mbox{\tt in,out}  & {\em t} & Potential temperature \mbox{[}degC\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em s} & Salinity \mbox{[}ppt\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em r0} & Potential density referenced to surface pressure \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em rcv} & The coordinate defining potential density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em rcvtgt} & The target value of Rcv for each layer \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em dt\+\_\+diag} & The diagnostic time step \mbox{[}T $\sim$$>$ s\mbox{]}.\\
\hline
\mbox{\tt 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{\tt in}  & {\em j} & The meridional row to work on.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em cs} & The control structure returned by a previous call to mixedlayer\+\_\+init.\\
\hline
\mbox{\tt 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{\tt 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{\tt 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{\tt 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{\tt 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}
2214   \textcolor{keywordtype}{type}(ocean\_grid\_type),              \textcolor{keywordtype}{intent(in)}    :: g\textcolor{comment}{    !< The ocean's grid structure.}
2215   \textcolor{keywordtype}{type}(verticalgrid\_type),            \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{   !< The ocean's vertical grid structure.}
2216   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{    !< Layer thickness [H ~> m or kg m-2].}
2217 \textcolor{comment}{                                                            !!  Layer 0 is the new mixed layer.}
2218   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: t\textcolor{comment}{    !< Potential temperature [degC].}
2219   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: s\textcolor{comment}{    !< Salinity [ppt].}
2220   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: r0\textcolor{comment}{   !< Potential density referenced to}
2221 \textcolor{comment}{                                                            !! surface pressure [R ~> kg m-3].}
2222   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))}, \textcolor{keywordtype}{intent(inout)} :: rcv\textcolor{comment}{  !< The coordinate defining potential}
2223 \textcolor{comment}{                                                            !! density [R ~> kg m-3].}
2224   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZK\_(GV))},          \textcolor{keywordtype}{intent(in)}    :: rcvtgt\textcolor{comment}{  !< The target value of Rcv for each}
2225 \textcolor{comment}{                                                            !! layer [R ~> kg m-3].}
2226   \textcolor{keywordtype}{real},                               \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{   !< Time increment [T ~> s].}
2227   \textcolor{keywordtype}{real},                               \textcolor{keywordtype}{intent(in)}    :: dt\_diag\textcolor{comment}{ !< The diagnostic time step [T ~> s].}
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}
2229 \textcolor{comment}{                                                            !! the entrainment from above}
2230 \textcolor{comment}{                                                            !! [H ~> m or kg m-2]. Positive d\_ea}
2231 \textcolor{comment}{                                                            !! goes with layer thickness increases.}
2232   \textcolor{keywordtype}{integer},                            \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{    !< The meridional row to work on.}
2233   \textcolor{keywordtype}{type}(unit\_scale\_type),              \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{   !< A dimensional unit scaling type}
2234   \textcolor{keywordtype}{type}(bulkmixedlayer\_cs),            \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{   !< The control structure returned by a}
2235 \textcolor{comment}{                                                            !! previous call to mixedlayer\_init.}
2236   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: dr0\_dt\textcolor{comment}{  !< The partial derivative of}
2237 \textcolor{comment}{                                                            !! potential density referenced to the}
2238 \textcolor{comment}{                                                            !! surface with potential temperature,}
2239 \textcolor{comment}{                                                            !! [R degC-1 ~> kg m-3 degC-1].}
2240   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: dr0\_ds\textcolor{comment}{  !< The partial derivative of}
2241 \textcolor{comment}{                                                            !! cpotential density referenced to the}
2242 \textcolor{comment}{                                                            !! surface with salinity}
2243 \textcolor{comment}{                                                            !! [R ppt-1 ~> kg m-3 ppt-1].}
2244   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: drcv\_dt\textcolor{comment}{ !< The partial derivative of}
2245 \textcolor{comment}{                                                            !! coordinate defining potential density}
2246 \textcolor{comment}{                                                            !! with potential temperature,}
2247 \textcolor{comment}{                                                            !! [R degC-1 ~> kg m-3 degC-1].}
2248   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: drcv\_ds\textcolor{comment}{ !< The partial derivative of}
2249 \textcolor{comment}{                                                            !! coordinate defining potential density}
2250 \textcolor{comment}{                                                            !! with salinity [R ppt-1 ~> kg m-3 ppt-1].}
2251   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: max\_bl\_det\textcolor{comment}{ !< If non-negative, the maximum}
2252 \textcolor{comment}{                                                            !! detrainment permitted from the buffer}
2253 \textcolor{comment}{                                                            !! layers [H ~> m or kg m-2].}
2254 
2255 \textcolor{comment}{! This subroutine moves any water left in the former mixed layers into the}
2256 \textcolor{comment}{! two buffer layers and may also move buffer layer water into the interior}
2257 \textcolor{comment}{! isopycnal layers.}
2258 
2259   \textcolor{comment}{! Local variables}
2260   \textcolor{keywordtype}{real} :: h\_to\_bl                 \textcolor{comment}{! The total thickness detrained to the buffer}
2261                                   \textcolor{comment}{! layers [H ~> m or kg m-2].}
2262   \textcolor{keywordtype}{real} :: r0\_to\_bl                \textcolor{comment}{! The depth integrated amount of R0 that is detrained to the}
2263                                   \textcolor{comment}{! buffer layer [H R ~> kg m-2 or kg2 m-5]}
2264   \textcolor{keywordtype}{real} :: rcv\_to\_bl               \textcolor{comment}{! The depth integrated amount of Rcv that is detrained to the}
2265                                   \textcolor{comment}{! buffer layer [H R ~> kg m-2 or kg2 m-5]}
2266   \textcolor{keywordtype}{real} :: t\_to\_bl                 \textcolor{comment}{! The depth integrated amount of T that is detrained to the}
2267                                   \textcolor{comment}{! buffer layer [degC H ~> degC m or degC kg m-2]}
2268   \textcolor{keywordtype}{real} :: s\_to\_bl                 \textcolor{comment}{! The depth integrated amount of S that is detrained to the}
2269                                   \textcolor{comment}{! buffer layer [ppt H ~> ppt m or ppt kg m-2]}
2270   \textcolor{keywordtype}{real} :: h\_min\_bl                \textcolor{comment}{! The minimum buffer layer thickness [H ~> m or kg m-2].}
2271 
2272   \textcolor{keywordtype}{real} :: h1, h2                  \textcolor{comment}{! Scalar variables holding the values of}
2273                                   \textcolor{comment}{! h(i,CS%nkml+1) and h(i,CS%nkml+2) [H ~> m or kg m-2].}
2274   \textcolor{keywordtype}{real} :: h1\_avail                \textcolor{comment}{! The thickness of the upper buffer layer}
2275                                   \textcolor{comment}{! available to move into the lower buffer}
2276                                   \textcolor{comment}{! layer [H ~> m or kg m-2].}
2277   \textcolor{keywordtype}{real} :: stays                   \textcolor{comment}{! stays is the thickness of the upper buffer}
2278                                   \textcolor{comment}{! layer that remains there [H ~> m or kg m-2].}
2279   \textcolor{keywordtype}{real} :: stays\_min, stays\_max    \textcolor{comment}{! The minimum and maximum permitted values of}
2280                                   \textcolor{comment}{! stays [H ~> m or kg m-2].}
2281 
2282   \textcolor{keywordtype}{logical} :: mergeable\_bl         \textcolor{comment}{! If true, it is an option to combine the two}
2283                                   \textcolor{comment}{! buffer layers and create water that matches}
2284                                   \textcolor{comment}{! the target density of an interior layer.}
2285   \textcolor{keywordtype}{real} :: stays\_merge             \textcolor{comment}{! If the two buffer layers can be combined}
2286                                   \textcolor{comment}{! stays\_merge is the thickness of the upper}
2287                                   \textcolor{comment}{! layer that remains [H ~> m or kg m-2].}
2288   \textcolor{keywordtype}{real} :: stays\_min\_merge         \textcolor{comment}{! The minimum allowed value of stays\_merge [H ~> m or kg m-2].}
2289 
2290   \textcolor{keywordtype}{real} :: dr0\_2dz, drcv\_2dz       \textcolor{comment}{! Half the vertical gradients of R0 and Rcv [R H-1 ~> kg m-4 or m-1]}
2291 \textcolor{comment}{!  real :: dT\_2dz, dS\_2dz         ! Half the vertical gradients of T and S, in degC H-1, and ppt H-1.}
2292   \textcolor{keywordtype}{real} :: scale\_slope             \textcolor{comment}{! A nondimensional number < 1 used to scale down}
2293                                   \textcolor{comment}{! the slope within the upper buffer layer when}
2294                                   \textcolor{comment}{! water MUST be detrained to the lower layer.}
2295 
2296   \textcolor{keywordtype}{real} :: dpe\_extrap              \textcolor{comment}{! The potential energy change due to dispersive}
2297                                   \textcolor{comment}{! advection or mixing layers, divided by}
2298                                   \textcolor{comment}{! rho\_0*g [H2 ~> m2 or kg2 m-4].}
2299   \textcolor{keywordtype}{real} :: dpe\_det, dpe\_merge      \textcolor{comment}{! The energy required to mix the detrained water}
2300                                   \textcolor{comment}{! into the buffer layer or the merge the two}
2301                                   \textcolor{comment}{! buffer layers [R H2 L2 Z-1 T-2 ~> J m-2 or J kg2 m-8].}
2302 
2303   \textcolor{keywordtype}{real} :: h\_from\_ml               \textcolor{comment}{! The amount of additional water that must be}
2304                                   \textcolor{comment}{! drawn from the mixed layer [H ~> m or kg m-2].}
2305   \textcolor{keywordtype}{real} :: h\_det\_h2                \textcolor{comment}{! The amount of detrained water and mixed layer}
2306                                   \textcolor{comment}{! water that will go directly into the lower}
2307                                   \textcolor{comment}{! buffer layer [H ~> m or kg m-2].}
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}
2309   \textcolor{keywordtype}{real} :: h\_det\_to\_h1, h\_ml\_to\_h1 \textcolor{comment}{! from one layer to another [H ~> m or kg m-2],}
2310   \textcolor{keywordtype}{real} :: h1\_to\_h2, h1\_to\_k0      \textcolor{comment}{! with h\_det the detrained water, h\_ml}
2311   \textcolor{keywordtype}{real} :: h2\_to\_k1, h2\_to\_k1\_rem  \textcolor{comment}{! the actively mixed layer, h1 and h2 the upper}
2312                                   \textcolor{comment}{! and lower buffer layers, and k0 and k1 the}
2313                                   \textcolor{comment}{! interior layers that are just lighter and}
2314                                   \textcolor{comment}{! just denser than the lower buffer layer.}
2315 
2316   \textcolor{keywordtype}{real} :: r0\_det, t\_det, s\_det    \textcolor{comment}{! Detrained values of R0 [R ~> kg m-3], T [degC], and S [ppt].}
2317   \textcolor{keywordtype}{real} :: rcv\_stays, r0\_stays     \textcolor{comment}{! Values of Rcv and R0 that stay in a layer.}
2318   \textcolor{keywordtype}{real} :: t\_stays, s\_stays        \textcolor{comment}{! Values of T and S that stay in a layer.}
2319   \textcolor{keywordtype}{real} :: dspice\_det, dspice\_stays\textcolor{comment}{! The spiciness difference between an original}
2320                                   \textcolor{comment}{! buffer layer and the water that moves into}
2321                                   \textcolor{comment}{! an interior layer or that stays in that}
2322                                   \textcolor{comment}{! layer [R ~> kg m-3].}
2323   \textcolor{keywordtype}{real} :: dspice\_lim, dspice\_lim2 \textcolor{comment}{! Limits to the spiciness difference between}
2324                                   \textcolor{comment}{! the lower buffer layer and the water that}
2325                                   \textcolor{comment}{! moves into an interior layer [R ~> kg m-3].}
2326   \textcolor{keywordtype}{real} :: dspice\_2dz              \textcolor{comment}{! The vertical gradient of spiciness used for}
2327                                   \textcolor{comment}{! advection [R H-1 ~> kg m-4 or m-1].}
2328 
2329   \textcolor{keywordtype}{real} :: dpe\_ratio               \textcolor{comment}{! Multiplier of dPE\_det at which merging is}
2330                                   \textcolor{comment}{! permitted - here (detrainment\_per\_day/dt)*30}
2331                                   \textcolor{comment}{! days?}
2332   \textcolor{keywordtype}{real} :: num\_events              \textcolor{comment}{! The number of detrainment events over which}
2333                                   \textcolor{comment}{! to prefer merging the buffer layers.}
2334   \textcolor{keywordtype}{real} :: dpe\_time\_ratio          \textcolor{comment}{! Larger of 1 and the detrainment timescale over dt [nondim].}
2335   \textcolor{keywordtype}{real} :: dt\_ds\_gauge, ds\_dt\_gauge \textcolor{comment}{! The relative scales of temperature and}
2336                                   \textcolor{comment}{! salinity changes in defining spiciness, in}
2337                                   \textcolor{comment}{! [degC ppt-1] and [ppt degC-1].}
2338   \textcolor{keywordtype}{real} :: i\_denom                 \textcolor{comment}{! A work variable with units of [ppt2 R-2 ~> ppt2 m6 kg-2].}
2339 
2340   \textcolor{keywordtype}{real} :: g\_2                     \textcolor{comment}{! 1/2 g\_Earth [L2 Z-1 T-2 ~> m s-2].}
2341   \textcolor{keywordtype}{real} :: rho0xg                  \textcolor{comment}{! Rho0 times G\_Earth [R L2 Z-1 T-2 ~> kg m-2 s-2].}
2342   \textcolor{keywordtype}{real} :: i2rho0                  \textcolor{comment}{! 1 / (2 Rho0) [R-1 ~> m3 kg-1].}
2343   \textcolor{keywordtype}{real} :: idt\_h2                  \textcolor{comment}{! The square of the conversion from thickness to Z}
2344                                   \textcolor{comment}{! divided by the time step [Z2 H-2 T-1 ~> s-1 or m6 kg-2 s-1].}
2345   \textcolor{keywordtype}{logical} :: stable\_rcv           \textcolor{comment}{! If true, the buffer layers are stable with}
2346                                   \textcolor{comment}{! respect to the coordinate potential density.}
2347   \textcolor{keywordtype}{real} :: h\_neglect \textcolor{comment}{! A thickness that is so small it is usually lost}
2348                     \textcolor{comment}{! in roundoff and can be neglected [H ~> m or kg m-2].}
2349 
2350   \textcolor{keywordtype}{real} :: s1en                    \textcolor{comment}{! A work variable [H2 L2 kg m-1 T-3 ~> kg m3 s-3 or kg3 m-3 s-3].}
2351   \textcolor{keywordtype}{real} :: s1, s2, bh0             \textcolor{comment}{! Work variables [H ~> m or kg m-2].}
2352   \textcolor{keywordtype}{real} :: s3sq                    \textcolor{comment}{! A work variable [H2 ~> m2 or kg2 m-4].}
2353   \textcolor{keywordtype}{real} :: i\_ya, b1                \textcolor{comment}{! Nondimensional work variables.}
2354   \textcolor{keywordtype}{real} :: ih, ihdet, ih1f, ih2f   \textcolor{comment}{! Assorted inverse thickness work variables,}
2355   \textcolor{keywordtype}{real} :: ihk0, ihk1, ih12        \textcolor{comment}{! all in [H-1 ~> m-1 or m2 kg-1].}
2356   \textcolor{keywordtype}{real} :: dr1, dr2, dr2b, drk1    \textcolor{comment}{! Assorted density difference work variables,}
2357   \textcolor{keywordtype}{real} :: dr0, dr21, drcv         \textcolor{comment}{! all in [R ~> kg m-3].}
2358   \textcolor{keywordtype}{real} :: drcv\_stays, drcv\_det, drcv\_lim
2359   \textcolor{keywordtype}{real} :: angstrom                \textcolor{comment}{! The minumum layer thickness [H ~> m or kg m-2].}
2360 
2361   \textcolor{keywordtype}{real} :: h2\_to\_k1\_lim, t\_new, s\_new, t\_max, t\_min, s\_max, s\_min
2362   \textcolor{keywordtype}{character(len=200)} :: mesg
2363 
2364   \textcolor{keywordtype}{integer} :: i, k, k0, k1, is, ie, nz, kb1, kb2, nkmb
2365   is = g%isc ; ie = g%iec ; nz = gv%ke
2366   kb1 = cs%nkml+1; kb2 = cs%nkml+2
2367   nkmb = cs%nkml+cs%nkbl
2368   h\_neglect = gv%H\_subroundoff
2369   g\_2 = 0.5 * gv%g\_Earth
2370   rho0xg = gv%Rho0 * gv%g\_Earth
2371   idt\_h2 = gv%H\_to\_Z**2 / dt\_diag
2372   i2rho0 = 0.5 / (gv%Rho0)
2373   angstrom = gv%Angstrom\_H
2374 
2375   \textcolor{comment}{! This is hard coding of arbitrary and dimensional numbers.}
2376   dt\_ds\_gauge = cs%dT\_dS\_wt ; ds\_dt\_gauge = 1.0 / dt\_ds\_gauge
2377   num\_events = 10.0
2378 
2379   \textcolor{keywordflow}{if} (cs%nkbl /= 2) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_mixed\_layer"}// &
2380                         \textcolor{stringliteral}{"CS%nkbl must be 2 in mixedlayer\_detrain\_2."})
2381 
2382   \textcolor{keywordflow}{if} (dt < cs%BL\_detrain\_time) \textcolor{keywordflow}{then} ; dpe\_time\_ratio = cs%BL\_detrain\_time / (dt)
2383   \textcolor{keywordflow}{else} ; dpe\_time\_ratio = 1.0 ;\textcolor{keywordflow}{ endif}
2384 
2385   \textcolor{keywordflow}{do} i=is,ie
2386 
2387   \textcolor{comment}{! Determine all of the properties being detrained from the mixed layer.}
2388 
2389   \textcolor{comment}{! As coded this has the k and i loop orders switched, but k is CS%nkml is}
2390   \textcolor{comment}{! often just 1 or 2, so this seems like it should not be a problem, especially}
2391   \textcolor{comment}{! since it means that a number of variables can now be scalars, not arrays.}
2392     h\_to\_bl = 0.0 ; r0\_to\_bl = 0.0
2393     rcv\_to\_bl = 0.0 ; t\_to\_bl = 0.0 ; s\_to\_bl = 0.0
2394 
2395     \textcolor{keywordflow}{do} k=1,cs%nkml ; \textcolor{keywordflow}{if} (h(i,k) > 0.0) \textcolor{keywordflow}{then}
2396       h\_to\_bl = h\_to\_bl + h(i,k)
2397       r0\_to\_bl = r0\_to\_bl + r0(i,k)*h(i,k)
2398 
2399       rcv\_to\_bl = rcv\_to\_bl + rcv(i,k)*h(i,k)
2400       t\_to\_bl = t\_to\_bl + t(i,k)*h(i,k)
2401       s\_to\_bl = s\_to\_bl + s(i,k)*h(i,k)
2402 
2403       d\_ea(i,k) = d\_ea(i,k) - h(i,k)
2404       h(i,k) = 0.0
2405 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
2406     \textcolor{keywordflow}{if} (h\_to\_bl > 0.0) \textcolor{keywordflow}{then} ; r0\_det = r0\_to\_bl / h\_to\_bl
2407     \textcolor{keywordflow}{else} ; r0\_det = r0(i,0) ;\textcolor{keywordflow}{ endif}
2408 
2409     \textcolor{comment}{! This code does both downward detrainment from both the mixed layer and the}
2410     \textcolor{comment}{! buffer layers.}
2411     \textcolor{comment}{!   Several considerations apply in detraining water into the interior:}
2412     \textcolor{comment}{! (1) Water only moves into the interior from the deeper buffer layer,}
2413     \textcolor{comment}{!     so the deeper buffer layer must have some mass.}
2414     \textcolor{comment}{! (2) The upper buffer layer must have some mass so the extrapolation of}
2415     \textcolor{comment}{!     density is meaningful (i.e. there is not detrainment from the buffer}
2416     \textcolor{comment}{!     layers when there is strong mixed layer entrainment).}
2417     \textcolor{comment}{! (3) The lower buffer layer density extrapolated to its base with a}
2418     \textcolor{comment}{!     linear fit between the two layers must exceed the density of the}
2419     \textcolor{comment}{!     next denser interior layer.}
2420     \textcolor{comment}{! (4) The average extroplated coordinate density that is moved into the}
2421     \textcolor{comment}{!     isopycnal interior matches the target value for that layer.}
2422     \textcolor{comment}{! (5) The potential energy change is calculated and might be used later}
2423     \textcolor{comment}{!     to allow the upper buffer layer to mix more into the lower buffer}
2424     \textcolor{comment}{!     layer.}
2425 
2426     \textcolor{comment}{! Determine whether more must be detrained from the mixed layer to keep a}
2427     \textcolor{comment}{! minimal amount of mass in the buffer layers.  In this case the 5% of the}
2428     \textcolor{comment}{! mixed layer thickness is hard-coded, but probably shouldn't be!}
2429     h\_min\_bl = min(cs%Hbuffer\_min, cs%Hbuffer\_rel\_min*h(i,0))
2430 
2431     stable\_rcv = .true.
2432     \textcolor{keywordflow}{if} (((r0(i,kb2)-r0(i,kb1)) * (rcv(i,kb2)-rcv(i,kb1)) <= 0.0)) &
2433       stable\_rcv = .false.
2434 
2435     h1 = h(i,kb1) ; h2 = h(i,kb2)
2436 
2437     h2\_to\_k1\_rem = (h1 + h2) + h\_to\_bl
2438     \textcolor{keywordflow}{if} ((max\_bl\_det(i) >= 0.0) .and. (h2\_to\_k1\_rem > max\_bl\_det(i))) &
2439       h2\_to\_k1\_rem = max\_bl\_det(i)
2440 
2441 
2442     \textcolor{keywordflow}{if} ((h2 == 0.0) .and. (h1 > 0.0)) \textcolor{keywordflow}{then}
2443       \textcolor{comment}{! The lower buffer layer has been eliminated either by convective}
2444       \textcolor{comment}{! adjustment or entrainment from the interior, and its current properties}
2445       \textcolor{comment}{! are not meaningful, but may later be used to determine the properties of}
2446       \textcolor{comment}{! waters moving into the lower buffer layer.  So the properties of the}
2447       \textcolor{comment}{! lower buffer layer are set to be between those of the upper buffer layer}
2448       \textcolor{comment}{! and the next denser interior layer, measured by R0.  This probably does}
2449       \textcolor{comment}{! not happen very often, so I am not too worried about the inefficiency of}
2450       \textcolor{comment}{! the following loop.}
2451       \textcolor{keywordflow}{do} k1=kb2+1,nz ; \textcolor{keywordflow}{if} (h(i,k1) > 2.0*angstrom) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}
2452 
2453       r0(i,kb2) = r0(i,kb1)
2454 
2455       rcv(i,kb2)=rcv(i,kb1) ; t(i,kb2)=t(i,kb1) ; s(i,kb2)=s(i,kb1)
2456 
2457 
2458       \textcolor{keywordflow}{if} (k1 <= nz) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (r0(i,k1) >= r0(i,kb1)) \textcolor{keywordflow}{then}
2459         r0(i,kb2) = 0.5*(r0(i,kb1)+r0(i,k1))
2460 
2461         rcv(i,kb2) = 0.5*(rcv(i,kb1)+rcv(i,k1))
2462         t(i,kb2) = 0.5*(t(i,kb1)+t(i,k1))
2463         s(i,kb2) = 0.5*(s(i,kb1)+s(i,k1))
2464 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ endif}
2465 \textcolor{keywordflow}{    endif} \textcolor{comment}{! (h2 = 0 && h1 > 0)}
2466 
2467     dpe\_extrap = 0.0 ; dpe\_merge = 0.0
2468     mergeable\_bl = .false.
2469     \textcolor{keywordflow}{if} ((h1 > 0.0) .and. (h2 > 0.0) .and. (h\_to\_bl > 0.0) .and. &
2470         (stable\_rcv)) \textcolor{keywordflow}{then}
2471       \textcolor{comment}{! Check whether it is permissible for the buffer layers to detrain}
2472       \textcolor{comment}{! into the interior isopycnal layers.}
2473 
2474       \textcolor{comment}{! Determine the layer that has the lightest target density that is}
2475       \textcolor{comment}{! denser than the lowermost buffer layer.}
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
2477       dr1 = rcvtgt(k0)-rcv(i,kb1) ; dr2 = rcv(i,kb2)-rcvtgt(k0)
2478 
2479       \textcolor{comment}{! Use an energy-balanced combination of downwind advection into the next}
2480       \textcolor{comment}{! denser interior layer and upwind advection from the upper buffer layer}
2481       \textcolor{comment}{! into the lower one, each with an energy change that equals that required}
2482       \textcolor{comment}{! to mix the detrained water with the upper buffer layer.}
2483       h1\_avail = h1 - max(0.0,h\_min\_bl-h\_to\_bl)
2484       \textcolor{keywordflow}{if} ((k1<=nz) .and. (h2 > h\_min\_bl) .and. (h1\_avail > 0.0) .and. &
2485           (r0(i,kb1) < r0(i,kb2)) .and. (h\_to\_bl*r0(i,kb1) > r0\_to\_bl)) \textcolor{keywordflow}{then}
2486         drk1 = (rcvtgt(k1) - rcv(i,kb2)) * (r0(i,kb2) - r0(i,kb1)) / &
2487                                            (rcv(i,kb2) - rcv(i,kb1))
2488         b1 = drk1 / (r0(i,kb2) - r0(i,kb1))
2489         \textcolor{comment}{! b1 = RcvTgt(k1) - Rcv(i,kb2)) / (Rcv(i,kb2) - Rcv(i,kb1))}
2490 
2491         \textcolor{comment}{! Apply several limits to the detrainment.}
2492         \textcolor{comment}{! Entrain less than the mass in h2, and keep the base of the buffer}
2493         \textcolor{comment}{! layers from becoming shallower than any neighbors.}
2494         h2\_to\_k1 = min(h2 - h\_min\_bl, h2\_to\_k1\_rem)
2495         \textcolor{comment}{! Balance downwind advection of density into the layer below the}
2496         \textcolor{comment}{! buffer layers with upwind advection from the layer above.}
2497         \textcolor{keywordflow}{if} (h2\_to\_k1*(h1\_avail + b1*(h1\_avail + h2)) > h2*h1\_avail) &
2498           h2\_to\_k1 = (h2*h1\_avail) / (h1\_avail + b1*(h1\_avail + h2))
2499         \textcolor{keywordflow}{if} (h2\_to\_k1*(drk1 * h2) > (h\_to\_bl*r0(i,kb1) - r0\_to\_bl) * h1) &
2500           h2\_to\_k1 = (h\_to\_bl*r0(i,kb1) - r0\_to\_bl) * h1 / (drk1 * h2)
2501 
2502         \textcolor{keywordflow}{if} ((k1==kb2+1) .and. (cs%BL\_extrap\_lim > 0.)) \textcolor{keywordflow}{then}
2503           \textcolor{comment}{! Simply do not detrain very light water into the lightest isopycnal}
2504           \textcolor{comment}{! coordinate layers if the density jump is too large.}
2505           drcv\_lim = rcv(i,kb2)-rcv(i,0)
2506           \textcolor{keywordflow}{do} k=1,kb2 ; drcv\_lim = max(drcv\_lim, rcv(i,kb2)-rcv(i,k)) ;\textcolor{keywordflow}{ enddo}
2507           drcv\_lim = cs%BL\_extrap\_lim*drcv\_lim
2508           \textcolor{keywordflow}{if} ((rcvtgt(k1) - rcv(i,kb2)) >= drcv\_lim) \textcolor{keywordflow}{then}
2509             h2\_to\_k1 = 0.0
2510           \textcolor{keywordflow}{elseif} ((rcvtgt(k1) - rcv(i,kb2)) > 0.5*drcv\_lim) \textcolor{keywordflow}{then}
2511             h2\_to\_k1 = h2\_to\_k1 * (2.0 - 2.0*((rcvtgt(k1) - rcv(i,kb2)) / drcv\_lim))
2512 \textcolor{keywordflow}{          endif}
2513 \textcolor{keywordflow}{        endif}
2514 
2515         drcv = (rcvtgt(k1) - rcv(i,kb2))
2516 
2517         \textcolor{comment}{! Use 2nd order upwind advection of spiciness, limited by the values}
2518         \textcolor{comment}{! in deeper thick layers to determine the detrained temperature and}
2519         \textcolor{comment}{! salinity.}
2520         dspice\_det = (ds\_dt\_gauge*drcv\_ds(i)*(t(i,kb2)-t(i,kb1)) - &
2521                       dt\_ds\_gauge*drcv\_dt(i)*(s(i,kb2)-s(i,kb1))) * &
2522                       (h2 - h2\_to\_k1) / (h1 + h2)
2523         dspice\_lim = 0.0
2524         \textcolor{keywordflow}{if} (h(i,k1) > 10.0*angstrom) \textcolor{keywordflow}{then}
2525           dspice\_lim = ds\_dt\_gauge*drcv\_ds(i)*(t(i,k1)-t(i,kb2)) - &
2526                        dt\_ds\_gauge*drcv\_dt(i)*(s(i,k1)-s(i,kb2))
2527           \textcolor{keywordflow}{if} (dspice\_det*dspice\_lim <= 0.0) dspice\_lim = 0.0
2528 \textcolor{keywordflow}{        endif}
2529         \textcolor{keywordflow}{if} (k1<nz) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (h(i,k1+1) > 10.0*angstrom) \textcolor{keywordflow}{then}
2530           dspice\_lim2 = ds\_dt\_gauge*drcv\_ds(i)*(t(i,k1+1)-t(i,kb2)) - &
2531                         dt\_ds\_gauge*drcv\_dt(i)*(s(i,k1+1)-s(i,kb2))
2532           \textcolor{keywordflow}{if} ((dspice\_det*dspice\_lim2 > 0.0) .and. &
2533               (abs(dspice\_lim2) > abs(dspice\_lim))) dspice\_lim = dspice\_lim2
2534 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ endif}
2535         \textcolor{keywordflow}{if} (abs(dspice\_det) > abs(dspice\_lim)) dspice\_det = dspice\_lim
2536 
2537         i\_denom = 1.0 / (drcv\_ds(i)**2 + (dt\_ds\_gauge*drcv\_dt(i))**2)
2538         t\_det = t(i,kb2) + dt\_ds\_gauge * i\_denom * &
2539             (dt\_ds\_gauge * drcv\_dt(i) * drcv + drcv\_ds(i) * dspice\_det)
2540         s\_det = s(i,kb2) + i\_denom * &
2541             (drcv\_ds(i) * drcv - dt\_ds\_gauge * drcv\_dt(i) * dspice\_det)
2542         \textcolor{comment}{! The detrained values of R0 are based on changes in T and S.}
2543         r0\_det = r0(i,kb2) + (t\_det-t(i,kb2)) * dr0\_dt(i) + &
2544                              (s\_det-s(i,kb2)) * dr0\_ds(i)
2545 
2546         \textcolor{keywordflow}{if} (cs%BL\_extrap\_lim >= 0.) \textcolor{keywordflow}{then}
2547           \textcolor{comment}{! Only do this detrainment if the new layer's temperature and salinity}
2548           \textcolor{comment}{! are not too far outside of the range of previous values.}
2549           \textcolor{keywordflow}{if} (h(i,k1) > 10.0*angstrom) \textcolor{keywordflow}{then}
2550             t\_min = min(t(i,kb1), t(i,kb2), t(i,k1)) - cs%Allowed\_T\_chg
2551             t\_max = max(t(i,kb1), t(i,kb2), t(i,k1)) + cs%Allowed\_T\_chg
2552             s\_min = min(s(i,kb1), s(i,kb2), s(i,k1)) - cs%Allowed\_S\_chg
2553             s\_max = max(s(i,kb1), s(i,kb2), s(i,k1)) + cs%Allowed\_S\_chg
2554           \textcolor{keywordflow}{else}
2555             t\_min = min(t(i,kb1), t(i,kb2)) - cs%Allowed\_T\_chg
2556             t\_max = max(t(i,kb1), t(i,kb2)) + cs%Allowed\_T\_chg
2557             s\_min = min(s(i,kb1), s(i,kb2)) - cs%Allowed\_S\_chg
2558             s\_max = max(s(i,kb1), s(i,kb2)) + cs%Allowed\_S\_chg
2559 \textcolor{keywordflow}{          endif}
2560           ihk1 = 1.0 / (h(i,k1) + h2\_to\_k1)
2561           t\_new = (h(i,k1)*t(i,k1) + h2\_to\_k1*t\_det) * ihk1
2562           s\_new = (h(i,k1)*s(i,k1) + h2\_to\_k1*s\_det) * ihk1
2563           \textcolor{comment}{! A less restrictive limit might be used here.}
2564           \textcolor{keywordflow}{if} ((t\_new < t\_min) .or. (t\_new > t\_max) .or. &
2565               (s\_new < s\_min) .or. (s\_new > s\_max)) &
2566             h2\_to\_k1 = 0.0
2567 \textcolor{keywordflow}{        endif}
2568 
2569         h1\_to\_h2 = b1*h2*h2\_to\_k1 / (h2 - (1.0+b1)*h2\_to\_k1)
2570 
2571         ihk1 = 1.0 / (h(i,k1) + h\_neglect + h2\_to\_k1)
2572         ih2f = 1.0 / ((h(i,kb2) - h2\_to\_k1) + h1\_to\_h2)
2573 
2574         rcv(i,kb2) = ((h(i,kb2)*rcv(i,kb2) - h2\_to\_k1*rcvtgt(k1)) + &
2575                       h1\_to\_h2*rcv(i,kb1))*ih2f
2576         rcv(i,k1) = ((h(i,k1)+h\_neglect)*rcv(i,k1) + h2\_to\_k1*rcvtgt(k1)) * ihk1
2577 
2578         t(i,kb2) = ((h(i,kb2)*t(i,kb2) - h2\_to\_k1*t\_det) + &
2579                     h1\_to\_h2*t(i,kb1)) * ih2f
2580         t(i,k1) = ((h(i,k1)+h\_neglect)*t(i,k1) + h2\_to\_k1*t\_det) * ihk1
2581 
2582         s(i,kb2) = ((h(i,kb2)*s(i,kb2) - h2\_to\_k1*s\_det) + &
2583                     h1\_to\_h2*s(i,kb1)) * ih2f
2584         s(i,k1) = ((h(i,k1)+h\_neglect)*s(i,k1) + h2\_to\_k1*s\_det) * ihk1
2585 
2586         \textcolor{comment}{! Changes in R0 are based on changes in T and S.}
2587         r0(i,kb2) = ((h(i,kb2)*r0(i,kb2) - h2\_to\_k1*r0\_det) + &
2588                      h1\_to\_h2*r0(i,kb1)) * ih2f
2589         r0(i,k1) = ((h(i,k1)+h\_neglect)*r0(i,k1) + h2\_to\_k1*r0\_det) * ihk1
2590 
2591         h(i,kb1) = h(i,kb1) - h1\_to\_h2 ; h1 = h(i,kb1)
2592         h(i,kb2) = (h(i,kb2) - h2\_to\_k1) + h1\_to\_h2 ; h2 = h(i,kb2)
2593         h(i,k1) = h(i,k1) + h2\_to\_k1
2594 
2595         d\_ea(i,kb1) = d\_ea(i,kb1) - h1\_to\_h2
2596         d\_ea(i,kb2) = (d\_ea(i,kb2) - h2\_to\_k1) + h1\_to\_h2
2597         d\_ea(i,k1) = d\_ea(i,k1) + h2\_to\_k1
2598         h2\_to\_k1\_rem = max(h2\_to\_k1\_rem - h2\_to\_k1, 0.0)
2599 
2600         \textcolor{comment}{!   The lower buffer layer has become lighter - it may be necessary to}
2601         \textcolor{comment}{! adjust k1 lighter.}
2602         \textcolor{keywordflow}{if} ((k1>kb2+1) .and. (rcvtgt(k1-1) >= rcv(i,kb2))) \textcolor{keywordflow}{then}
2603           \textcolor{keywordflow}{do} k1=k1,kb2+1,-1 ; \textcolor{keywordflow}{if} (rcvtgt(k1-1) < rcv(i,kb2)) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}
2604 \textcolor{keywordflow}{        endif}
2605 \textcolor{keywordflow}{      endif}
2606 
2607       k0 = k1-1
2608       dr1 = rcvtgt(k0)-rcv(i,kb1) ; dr2 = rcv(i,kb2)-rcvtgt(k0)
2609 
2610       \textcolor{keywordflow}{if} ((k0>kb2) .and. (dr1 > 0.0) .and. (h1 > h\_min\_bl) .and. &
2611           (h2*dr2 < h1*dr1) .and. (r0(i,kb2) > r0(i,kb1))) \textcolor{keywordflow}{then}
2612         \textcolor{comment}{! An interior isopycnal layer (k0) is intermediate in density between}
2613         \textcolor{comment}{! the two buffer layers, and there can be detrainment. The entire}
2614         \textcolor{comment}{! lower buffer layer is combined with a portion of the upper buffer}
2615         \textcolor{comment}{! layer to match the target density of layer k0.}
2616         stays\_merge = 2.0*(h1+h2)*(h1*dr1 - h2*dr2) / &
2617                      ((dr1+dr2)*h1 + dr1*(h1+h2) + &
2618                       sqrt((dr2*h1-dr1*h2)**2 + 4*(h1+h2)*h2*(dr1+dr2)*dr2))
2619 
2620         stays\_min\_merge = max(h\_min\_bl, 2.0*h\_min\_bl - h\_to\_bl, &
2621                   h1 - (h1+h2)*(r0(i,kb1) - r0\_det) / (r0(i,kb2) - r0(i,kb1)))
2622         \textcolor{keywordflow}{if} ((stays\_merge > stays\_min\_merge) .and. &
2623             (stays\_merge + h2\_to\_k1\_rem >= h1 + h2)) \textcolor{keywordflow}{then}
2624           mergeable\_bl = .true.
2625           dpe\_merge = g\_2*(r0(i,kb2)-r0(i,kb1))*(h1-stays\_merge)*(h2-stays\_merge)
2626 \textcolor{keywordflow}{        endif}
2627 \textcolor{keywordflow}{      endif}
2628 
2629       \textcolor{keywordflow}{if} ((k1<=nz).and.(.not.mergeable\_bl)) \textcolor{keywordflow}{then}
2630         \textcolor{comment}{! Check whether linear extrapolation of density (i.e. 2nd order upwind}
2631         \textcolor{comment}{! advection) will allow some of the lower buffer layer to detrain into}
2632         \textcolor{comment}{! the next denser interior layer (k1).}
2633         dr2b = rcvtgt(k1)-rcv(i,kb2) ; dr21 = rcv(i,kb2) - rcv(i,kb1)
2634         \textcolor{keywordflow}{if} (dr2b*(h1+h2) < h2*dr21) \textcolor{keywordflow}{then}
2635           \textcolor{comment}{! Some of layer kb2 is denser than k1.}
2636           h2\_to\_k1 = min(h2 - (h1+h2) * dr2b / dr21, h2\_to\_k1\_rem)
2637 
2638           \textcolor{keywordflow}{if} (h2 > h2\_to\_k1) \textcolor{keywordflow}{then}
2639             drcv = (rcvtgt(k1) - rcv(i,kb2))
2640 
2641             \textcolor{comment}{! Use 2nd order upwind advection of spiciness, limited by the values}
2642             \textcolor{comment}{! in deeper thick layers to determine the detrained temperature and}
2643             \textcolor{comment}{! salinity.}
2644             dspice\_det = (ds\_dt\_gauge*drcv\_ds(i)*(t(i,kb2)-t(i,kb1)) - &
2645                           dt\_ds\_gauge*drcv\_dt(i)*(s(i,kb2)-s(i,kb1))) * &
2646                           (h2 - h2\_to\_k1) / (h1 + h2)
2647             dspice\_lim = 0.0
2648             \textcolor{keywordflow}{if} (h(i,k1) > 10.0*angstrom) \textcolor{keywordflow}{then}
2649               dspice\_lim = ds\_dt\_gauge*drcv\_ds(i)*(t(i,k1)-t(i,kb2)) - &
2650                            dt\_ds\_gauge*drcv\_dt(i)*(s(i,k1)-s(i,kb2))
2651               \textcolor{keywordflow}{if} (dspice\_det*dspice\_lim <= 0.0) dspice\_lim = 0.0
2652 \textcolor{keywordflow}{            endif}
2653             \textcolor{keywordflow}{if} (k1<nz) then; \textcolor{keywordflow}{if} (h(i,k1+1) > 10.0*angstrom) \textcolor{keywordflow}{then}
2654               dspice\_lim2 = ds\_dt\_gauge*drcv\_ds(i)*(t(i,k1+1)-t(i,kb2)) - &
2655                             dt\_ds\_gauge*drcv\_dt(i)*(s(i,k1+1)-s(i,kb2))
2656               \textcolor{keywordflow}{if} ((dspice\_det*dspice\_lim2 > 0.0) .and. &
2657                   (abs(dspice\_lim2) > abs(dspice\_lim))) dspice\_lim = dspice\_lim2
2658 \textcolor{keyword}{            end}if; endif
2659             \textcolor{keywordflow}{if} (abs(dspice\_det) > abs(dspice\_lim)) dspice\_det = dspice\_lim
2660 
2661             i\_denom = 1.0 / (drcv\_ds(i)**2 + (dt\_ds\_gauge*drcv\_dt(i))**2)
2662             t\_det = t(i,kb2) + dt\_ds\_gauge * i\_denom * &
2663                 (dt\_ds\_gauge * drcv\_dt(i) * drcv + drcv\_ds(i) * dspice\_det)
2664             s\_det = s(i,kb2) + i\_denom * &
2665                 (drcv\_ds(i) * drcv - dt\_ds\_gauge * drcv\_dt(i) * dspice\_det)
2666             \textcolor{comment}{! The detrained values of R0 are based on changes in T and S.}
2667             r0\_det = r0(i,kb2) + (t\_det-t(i,kb2)) * dr0\_dt(i) + &
2668                                  (s\_det-s(i,kb2)) * dr0\_ds(i)
2669 
2670             \textcolor{comment}{! Now that the properties of the detrained water are known,}
2671             \textcolor{comment}{! potentially limit the amount of water that is detrained to}
2672             \textcolor{comment}{! avoid creating unphysical properties in the remaining water.}
2673             ih2f = 1.0 / (h2 - h2\_to\_k1)
2674 
2675             t\_min = min(t(i,kb2), t(i,kb1)) - cs%Allowed\_T\_chg
2676             t\_max = max(t(i,kb2), t(i,kb1)) + cs%Allowed\_T\_chg
2677             t\_new = (h2*t(i,kb2) - h2\_to\_k1*t\_det)*ih2f
2678             \textcolor{keywordflow}{if} (t\_new < t\_min) \textcolor{keywordflow}{then}
2679               h2\_to\_k1\_lim = h2 * (t(i,kb2) - t\_min) / (t\_det - t\_min)
2680 \textcolor{comment}{!               write(mesg,'("Low temperature limits det to ", &}
2681 \textcolor{comment}{!                    & 1pe12.5, " from ", 1pe12.5, " at ", 1pg11.4,"E, ",1pg11.4,"N. T=", &}
2682 \textcolor{comment}{!                    & 5(1pe12.5))') &}
2683 \textcolor{comment}{!                    h2\_to\_k1\_lim, h2\_to\_k1, G%geoLonT(i,j), G%geoLatT(i,j), &}
2684 \textcolor{comment}{!                    T\_new, T(i,kb2), T(i,kb1), T\_det, T\_new-T\_min}
2685 \textcolor{comment}{!               call MOM\_error(WARNING, mesg)}
2686               h2\_to\_k1 = h2\_to\_k1\_lim
2687               ih2f = 1.0 / (h2 - h2\_to\_k1)
2688             \textcolor{keywordflow}{elseif} (t\_new > t\_max) \textcolor{keywordflow}{then}
2689               h2\_to\_k1\_lim = h2 * (t(i,kb2) - t\_max) / (t\_det - t\_max)
2690 \textcolor{comment}{!               write(mesg,'("High temperature limits det to ", &}
2691 \textcolor{comment}{!                    & 1pe12.5, " from ", 1pe12.5, " at ", 1pg11.4,"E, ",1pg11.4,"N. T=", &}
2692 \textcolor{comment}{!                    & 5(1pe12.5))') &}
2693 \textcolor{comment}{!                    h2\_to\_k1\_lim, h2\_to\_k1, G%geoLonT(i,j), G%geoLatT(i,j), &}
2694 \textcolor{comment}{!                    T\_new, T(i,kb2), T(i,kb1), T\_det, T\_new-T\_max}
2695 \textcolor{comment}{!               call MOM\_error(WARNING, mesg)}
2696               h2\_to\_k1 = h2\_to\_k1\_lim
2697               ih2f = 1.0 / (h2 - h2\_to\_k1)
2698 \textcolor{keywordflow}{            endif}
2699             s\_min = max(min(s(i,kb2), s(i,kb1)) - cs%Allowed\_S\_chg, 0.0)
2700             s\_max = max(s(i,kb2), s(i,kb1)) + cs%Allowed\_S\_chg
2701             s\_new = (h2*s(i,kb2) - h2\_to\_k1*s\_det)*ih2f
2702             \textcolor{keywordflow}{if} (s\_new < s\_min) \textcolor{keywordflow}{then}
2703               h2\_to\_k1\_lim = h2 * (s(i,kb2) - s\_min) / (s\_det - s\_min)
2704 \textcolor{comment}{!               write(mesg,'("Low salinity limits det to ", &}
2705 \textcolor{comment}{!                    & 1pe12.5, " from ", 1pe12.5, " at ", 1pg11.4,"E, ",1pg11.4,"N. S=", &}
2706 \textcolor{comment}{!                    & 5(1pe12.5))') &}
2707 \textcolor{comment}{!                    h2\_to\_k1\_lim, h2\_to\_k1, G%geoLonT(i,j), G%geoLatT(i,j), &}
2708 \textcolor{comment}{!                    S\_new, S(i,kb2), S(i,kb1), S\_det, S\_new-S\_min}
2709 \textcolor{comment}{!               call MOM\_error(WARNING, mesg)}
2710               h2\_to\_k1 = h2\_to\_k1\_lim
2711               ih2f = 1.0 / (h2 - h2\_to\_k1)
2712             \textcolor{keywordflow}{elseif} (s\_new > s\_max) \textcolor{keywordflow}{then}
2713               h2\_to\_k1\_lim = h2 * (s(i,kb2) - s\_max) / (s\_det - s\_max)
2714 \textcolor{comment}{!               write(mesg,'("High salinity limits det to ", &}
2715 \textcolor{comment}{!                    & 1pe12.5, " from ", 1pe12.5, " at ", 1pg11.4,"E, ",1pg11.4,"N. S=", &}
2716 \textcolor{comment}{!                    & 5(1pe12.5))') &}
2717 \textcolor{comment}{!                    h2\_to\_k1\_lim, h2\_to\_k1, G%geoLonT(i,j), G%geoLatT(i,j), &}
2718 \textcolor{comment}{!                    S\_new, S(i,kb2), S(i,kb1), S\_det, S\_new-S\_max}
2719 \textcolor{comment}{!               call MOM\_error(WARNING, mesg)}
2720               h2\_to\_k1 = h2\_to\_k1\_lim
2721               ih2f = 1.0 / (h2 - h2\_to\_k1)
2722 \textcolor{keywordflow}{            endif}
2723 
2724             ihk1 = 1.0 / (h(i,k1) + h\_neglect + h2\_to\_k1)
2725             rcv(i,k1) = ((h(i,k1)+h\_neglect)*rcv(i,k1) + h2\_to\_k1*rcvtgt(k1)) * ihk1
2726             rcv(i,kb2) = rcv(i,kb2) - h2\_to\_k1*drcv*ih2f
2727 
2728             t(i,kb2) = (h2*t(i,kb2) - h2\_to\_k1*t\_det)*ih2f
2729             t(i,k1) = ((h(i,k1)+h\_neglect)*t(i,k1) + h2\_to\_k1*t\_det) * ihk1
2730 
2731             s(i,kb2) = (h2*s(i,kb2) - h2\_to\_k1*s\_det) * ih2f
2732             s(i,k1) = ((h(i,k1)+h\_neglect)*s(i,k1) + h2\_to\_k1*s\_det) * ihk1
2733 
2734             \textcolor{comment}{! Changes in R0 are based on changes in T and S.}
2735             r0(i,kb2) = (h2*r0(i,kb2) - h2\_to\_k1*r0\_det) * ih2f
2736             r0(i,k1) = ((h(i,k1)+h\_neglect)*r0(i,k1) + h2\_to\_k1*r0\_det) * ihk1
2737           \textcolor{keywordflow}{else}
2738             \textcolor{comment}{! h2==h2\_to\_k1 can happen if dR2b = 0 exactly, but this is very}
2739             \textcolor{comment}{! unlikely.  In this case the entirety of layer kb2 is detrained.}
2740             h2\_to\_k1 = h2  \textcolor{comment}{! These 2 lines are probably unnecessary.}
2741             ihk1 = 1.0 / (h(i,k1) + h2)
2742 
2743             rcv(i,k1) = (h(i,k1)*rcv(i,k1) + h2*rcv(i,kb2)) * ihk1
2744             t(i,k1) = (h(i,k1)*t(i,k1) + h2*t(i,kb2)) * ihk1
2745             s(i,k1) = (h(i,k1)*s(i,k1) + h2*s(i,kb2)) * ihk1
2746             r0(i,k1) = (h(i,k1)*r0(i,k1) + h2*r0(i,kb2)) * ihk1
2747 \textcolor{keywordflow}{          endif}
2748 
2749           h(i,k1) = h(i,k1) + h2\_to\_k1
2750           h(i,kb2) = h(i,kb2) - h2\_to\_k1 ; h2 = h(i,kb2)
2751           \textcolor{comment}{! dPE\_extrap should be positive here.}
2752           dpe\_extrap = i2rho0*(r0\_det-r0(i,kb2))*h2\_to\_k1*h2
2753 
2754           d\_ea(i,kb2) = d\_ea(i,kb2) - h2\_to\_k1
2755           d\_ea(i,k1) = d\_ea(i,k1) + h2\_to\_k1
2756           h2\_to\_k1\_rem = max(h2\_to\_k1\_rem - h2\_to\_k1, 0.0)
2757 \textcolor{keywordflow}{        endif}
2758 \textcolor{keywordflow}{      endif} \textcolor{comment}{! Detrainment by extrapolation.}
2759 
2760 \textcolor{keywordflow}{    endif} \textcolor{comment}{! Detrainment to the interior at all.}
2761 
2762     \textcolor{comment}{! Does some of the detrained water go into the lower buffer layer?}
2763     h\_det\_h2 = max(h\_min\_bl-(h1+h2), 0.0)
2764     \textcolor{keywordflow}{if} (h\_det\_h2 > 0.0) \textcolor{keywordflow}{then}
2765       \textcolor{comment}{! Detrained water will go into both upper and lower buffer layers.}
2766       \textcolor{comment}{! h(kb2) will be h\_min\_bl, but h(kb1) may be larger if there was already}
2767       \textcolor{comment}{! ample detrainment; all water in layer kb1 moves into layer kb2.}
2768 
2769       \textcolor{comment}{! Determine the fluxes between the various layers.}
2770       h\_det\_to\_h2 = min(h\_to\_bl, h\_det\_h2)
2771       h\_ml\_to\_h2 = h\_det\_h2 - h\_det\_to\_h2
2772       h\_det\_to\_h1 = h\_to\_bl - h\_det\_to\_h2
2773       h\_ml\_to\_h1 = max(h\_min\_bl-h\_det\_to\_h1,0.0)
2774 
2775       ih = 1.0/h\_min\_bl
2776       ihdet = 0.0 ; \textcolor{keywordflow}{if} (h\_to\_bl > 0.0) ihdet = 1.0 / h\_to\_bl
2777       ih1f = 1.0 / (h\_det\_to\_h1 + h\_ml\_to\_h1)
2778 
2779       r0(i,kb2) = ((h2*r0(i,kb2) + h1*r0(i,kb1)) + &
2780                    (h\_det\_to\_h2*r0\_to\_bl*ihdet + h\_ml\_to\_h2*r0(i,0))) * ih
2781       r0(i,kb1) = (h\_det\_to\_h1*r0\_to\_bl*ihdet + h\_ml\_to\_h1*r0(i,0)) * ih1f
2782 
2783       rcv(i,kb2) = ((h2*rcv(i,kb2) + h1*rcv(i,kb1)) + &
2784                     (h\_det\_to\_h2*rcv\_to\_bl*ihdet + h\_ml\_to\_h2*rcv(i,0))) * ih
2785       rcv(i,kb1) = (h\_det\_to\_h1*rcv\_to\_bl*ihdet + h\_ml\_to\_h1*rcv(i,0)) * ih1f
2786 
2787       t(i,kb2) = ((h2*t(i,kb2) + h1*t(i,kb1)) + &
2788                   (h\_det\_to\_h2*t\_to\_bl*ihdet + h\_ml\_to\_h2*t(i,0))) * ih
2789       t(i,kb1) = (h\_det\_to\_h1*t\_to\_bl*ihdet + h\_ml\_to\_h1*t(i,0)) * ih1f
2790 
2791       s(i,kb2) = ((h2*s(i,kb2) + h1*s(i,kb1)) + &
2792                   (h\_det\_to\_h2*s\_to\_bl*ihdet + h\_ml\_to\_h2*s(i,0))) * ih
2793       s(i,kb1) = (h\_det\_to\_h1*s\_to\_bl*ihdet + h\_ml\_to\_h1*s(i,0)) * ih1f
2794 
2795       \textcolor{comment}{! Recall that h1 = h(i,kb1) & h2 = h(i,kb2).}
2796       d\_ea(i,1) = d\_ea(i,1) - (h\_ml\_to\_h1 + h\_ml\_to\_h2)
2797       d\_ea(i,kb1) = d\_ea(i,kb1) + ((h\_det\_to\_h1 + h\_ml\_to\_h1) - h1)
2798       d\_ea(i,kb2) = d\_ea(i,kb2) + (h\_min\_bl - h2)
2799 
2800       h(i,kb1) = h\_det\_to\_h1 + h\_ml\_to\_h1 ; h(i,kb2) = h\_min\_bl
2801       h(i,0) = h(i,0) - (h\_ml\_to\_h1 + h\_ml\_to\_h2)
2802 
2803 
2804       \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain) .or. \textcolor{keyword}{allocated}(cs%diag\_PE\_detrain2)) \textcolor{keywordflow}{then}
2805         r0\_det = r0\_to\_bl*ihdet
2806         s1en = g\_2 * idt\_h2 * ( ((r0(i,kb2)-r0(i,kb1))*h1*h2 + &
2807             h\_det\_to\_h2*( (r0(i,kb1)-r0\_det)*h1 + (r0(i,kb2)-r0\_det)*h2 ) + &
2808             h\_ml\_to\_h2*( (r0(i,kb2)-r0(i,0))*h2 + (r0(i,kb1)-r0(i,0))*h1 + &
2809                          (r0\_det-r0(i,0))*h\_det\_to\_h2 ) + &
2810             h\_det\_to\_h1*h\_ml\_to\_h1*(r0\_det-r0(i,0))) - 2.0*gv%Rho0*dpe\_extrap )
2811 
2812         \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain)) &
2813           cs%diag\_PE\_detrain(i,j) = cs%diag\_PE\_detrain(i,j) + s1en
2814 
2815         \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain2)) cs%diag\_PE\_detrain2(i,j) = &
2816             cs%diag\_PE\_detrain2(i,j) + s1en + idt\_h2*rho0xg*dpe\_extrap
2817 \textcolor{keywordflow}{      endif}
2818 
2819     \textcolor{keywordflow}{elseif} ((h\_to\_bl > 0.0) .or. (h1 < h\_min\_bl) .or. (h2 < h\_min\_bl)) \textcolor{keywordflow}{then}
2820     \textcolor{comment}{! Determine how much of the upper buffer layer will be moved into}
2821     \textcolor{comment}{! the lower buffer layer and the properties with which it is moving.}
2822     \textcolor{comment}{! This implementation assumes a 2nd-order upwind advection of density}
2823     \textcolor{comment}{! from the uppermost buffer layer into the next one down.}
2824       h\_from\_ml = h\_min\_bl + max(h\_min\_bl-h2,0.0) - h1 - h\_to\_bl
2825       \textcolor{keywordflow}{if} (h\_from\_ml > 0.0) \textcolor{keywordflow}{then}
2826         \textcolor{comment}{! Some water needs to be moved from the mixed layer so that the upper}
2827         \textcolor{comment}{! (and perhaps lower) buffer layers exceed their minimum thicknesses.}
2828         dpe\_extrap = dpe\_extrap - i2rho0*h\_from\_ml*(r0\_to\_bl - r0(i,0)*h\_to\_bl)
2829         r0\_to\_bl = r0\_to\_bl + h\_from\_ml*r0(i,0)
2830         rcv\_to\_bl = rcv\_to\_bl + h\_from\_ml*rcv(i,0)
2831         t\_to\_bl = t\_to\_bl + h\_from\_ml*t(i,0)
2832         s\_to\_bl = s\_to\_bl + h\_from\_ml*s(i,0)
2833 
2834         h\_to\_bl = h\_to\_bl + h\_from\_ml
2835         h(i,0) = h(i,0) - h\_from\_ml
2836         d\_ea(i,1) = d\_ea(i,1) - h\_from\_ml
2837 \textcolor{keywordflow}{      endif}
2838 
2839       \textcolor{comment}{! The absolute value should be unnecessary and 1e9 is just a large number.}
2840       b1 = 1.0e9
2841       \textcolor{keywordflow}{if} (r0(i,kb2) - r0(i,kb1) > 1.0e-9*abs(r0(i,kb1) - r0\_det)) &
2842         b1 = abs(r0(i,kb1) - r0\_det) / (r0(i,kb2) - r0(i,kb1))
2843       stays\_min = max((1.0-b1)*h1 - b1*h2, 0.0, h\_min\_bl - h\_to\_bl)
2844       stays\_max = h1 - max(h\_min\_bl-h2,0.0)
2845 
2846       scale\_slope = 1.0
2847       \textcolor{keywordflow}{if} (stays\_max <= stays\_min) \textcolor{keywordflow}{then}
2848         stays = stays\_max
2849         mergeable\_bl = .false.
2850         \textcolor{keywordflow}{if} (stays\_max < h1) scale\_slope = (h1 - stays\_min) / (h1 - stays\_max)
2851       \textcolor{keywordflow}{else}
2852         \textcolor{comment}{! There are numerous temporary variables used here that should not be}
2853         \textcolor{comment}{! used outside of this "else" branch: s1, s2, s3sq, I\_ya, bh0}
2854         bh0 = b1*h\_to\_bl
2855         i\_ya =  (h1 + h2) / ((h1 + h2) + h\_to\_bl)
2856         \textcolor{comment}{! s1 is the amount staying that minimizes the PE increase.}
2857         s1 = 0.5*(h1 + (h2 - bh0) * i\_ya) ; s2 = h1 - s1
2858 
2859         \textcolor{keywordflow}{if} (s2 < 0.0) \textcolor{keywordflow}{then}
2860           \textcolor{comment}{! The energy released by detrainment from the lower buffer layer can be}
2861           \textcolor{comment}{! used to mix water from the upper buffer layer into the lower one.}
2862           s3sq = i\_ya*max(bh0*h1-dpe\_extrap, 0.0)
2863         \textcolor{keywordflow}{else}
2864           s3sq = i\_ya*(bh0*h1-min(dpe\_extrap,0.0))
2865 \textcolor{keywordflow}{        endif}
2866 
2867         \textcolor{keywordflow}{if} (s3sq == 0.0) \textcolor{keywordflow}{then}
2868           \textcolor{comment}{! There is a simple, exact solution to the quadratic equation, namely:}
2869           stays = h1 \textcolor{comment}{! This will revert to stays\_max later.}
2870         \textcolor{keywordflow}{elseif} (s2*s2 <= s3sq) \textcolor{keywordflow}{then}
2871           \textcolor{comment}{! There is no solution with 0 PE change - use the minimum energy input.}
2872           stays = s1
2873         \textcolor{keywordflow}{else}
2874           \textcolor{comment}{! The following choose the solutions that are continuous with all water}
2875           \textcolor{comment}{! staying in the upper buffer layer when there is no detrainment,}
2876           \textcolor{comment}{! namely the + root when s2>0 and the - root otherwise. They also}
2877           \textcolor{comment}{! carefully avoid differencing large numbers, using s2 = (h1-s).}
2878           \textcolor{keywordflow}{if} (bh0 <= 0.0) \textcolor{keywordflow}{then} ; stays = h1
2879           \textcolor{keywordflow}{elseif} (s2 > 0.0) \textcolor{keywordflow}{then}
2880   \textcolor{comment}{!         stays = s + sqrt(s2*s2 - s3sq) ! Note that s2 = h1-s}
2881             \textcolor{keywordflow}{if} (s1 >= stays\_max) \textcolor{keywordflow}{then} ; stays = stays\_max
2882             \textcolor{keywordflow}{elseif} (s1 >= 0.0) \textcolor{keywordflow}{then} ; stays = s1 + sqrt(s2*s2 - s3sq)
2883             \textcolor{keywordflow}{else} ; stays = (h1*(s2-s1) - s3sq) / (-s1 + sqrt(s2*s2 - s3sq))
2884 \textcolor{keywordflow}{            endif}
2885           \textcolor{keywordflow}{else}
2886   \textcolor{comment}{!         stays = s - sqrt(s2*s2 - s3sq) ! Note that s2 = h1-s & stays\_min >= 0}
2887             \textcolor{keywordflow}{if} (s1 <= stays\_min) \textcolor{keywordflow}{then} ; stays = stays\_min
2888             \textcolor{keywordflow}{else} ; stays = (h1*(s1-s2) + s3sq) / (s1 + sqrt(s2*s2 - s3sq))
2889 \textcolor{keywordflow}{            endif}
2890 \textcolor{keywordflow}{          endif}
2891 \textcolor{keywordflow}{        endif}
2892 
2893         \textcolor{comment}{! Limit the amount that stays so that the motion of water is from the}
2894         \textcolor{comment}{! upper buffer layer into the lower, but no more than is in the upper}
2895         \textcolor{comment}{! layer, and the water left in the upper layer is no lighter than the}
2896         \textcolor{comment}{! detrained water.}
2897         \textcolor{keywordflow}{if} (stays >= stays\_max) \textcolor{keywordflow}{then} ; stays = stays\_max
2898         \textcolor{keywordflow}{elseif} (stays < stays\_min) \textcolor{keywordflow}{then} ; stays = stays\_min
2899 \textcolor{keywordflow}{        endif}
2900 \textcolor{keywordflow}{      endif}
2901 
2902       dpe\_det = g\_2*((r0(i,kb1)*h\_to\_bl - r0\_to\_bl)*stays + &
2903                      (r0(i,kb2)-r0(i,kb1)) * (h1-stays) * &
2904                      (h2 - scale\_slope*stays*((h1+h2)+h\_to\_bl)/(h1+h2)) ) - &
2905                 rho0xg*dpe\_extrap
2906 
2907       \textcolor{keywordflow}{if} (dpe\_time\_ratio*h\_to\_bl > h\_to\_bl+h(i,0)) \textcolor{keywordflow}{then}
2908         dpe\_ratio = (h\_to\_bl+h(i,0)) / h\_to\_bl
2909       \textcolor{keywordflow}{else}
2910         dpe\_ratio = dpe\_time\_ratio
2911 \textcolor{keywordflow}{      endif}
2912 
2913       \textcolor{keywordflow}{if} ((mergeable\_bl) .and. (num\_events*dpe\_ratio*dpe\_det > dpe\_merge)) \textcolor{keywordflow}{then}
2914         \textcolor{comment}{! It is energetically preferable to merge the two buffer layers, detrain}
2915         \textcolor{comment}{! them into interior layer (k0), move the remaining upper buffer layer}
2916         \textcolor{comment}{! water into the lower buffer layer, and detrain undiluted into the}
2917         \textcolor{comment}{! upper buffer layer.}
2918         h1\_to\_k0 = (h1-stays\_merge)
2919         stays = max(h\_min\_bl-h\_to\_bl,0.0)
2920         h1\_to\_h2 = stays\_merge - stays
2921 
2922         ihk0 = 1.0 / ((h1\_to\_k0 + h2) + h(i,k0))
2923         ih1f = 1.0 / (h\_to\_bl + stays); ih2f = 1.0 / h1\_to\_h2
2924         ih12 = 1.0 / (h1 + h2)
2925 
2926         drcv\_2dz = (rcv(i,kb1) - rcv(i,kb2)) * ih12
2927         drcv\_stays = drcv\_2dz*(h1\_to\_k0 + h1\_to\_h2)
2928         drcv\_det = - drcv\_2dz*(stays + h1\_to\_h2)
2929         rcv(i,k0) = ((h1\_to\_k0*(rcv(i,kb1) + drcv\_det) + &
2930                       h2*rcv(i,kb2)) + h(i,k0)*rcv(i,k0)) * ihk0
2931         rcv(i,kb2) = rcv(i,kb1) + drcv\_2dz*(h1\_to\_k0-stays)
2932         rcv(i,kb1) = (rcv\_to\_bl + stays*(rcv(i,kb1) + drcv\_stays)) * ih1f
2933 
2934         \textcolor{comment}{! Use 2nd order upwind advection of spiciness, limited by the value in}
2935         \textcolor{comment}{! the water from the mixed layer to determine the temperature and}
2936         \textcolor{comment}{! salinity of the water that stays in the buffer layers.}
2937         i\_denom = 1.0 / (drcv\_ds(i)**2 + (dt\_ds\_gauge*drcv\_dt(i))**2)
2938         dspice\_2dz = (ds\_dt\_gauge*drcv\_ds(i)*(t(i,kb1)-t(i,kb2)) - &
2939                       dt\_ds\_gauge*drcv\_dt(i)*(s(i,kb1)-s(i,kb2))) * ih12
2940         dspice\_lim = (ds\_dt\_gauge*dr0\_ds(i)*(t\_to\_bl-t(i,kb1)*h\_to\_bl) - &
2941                       dt\_ds\_gauge*dr0\_dt(i)*(s\_to\_bl-s(i,kb1)*h\_to\_bl)) / h\_to\_bl
2942         \textcolor{keywordflow}{if} (dspice\_lim * dspice\_2dz <= 0.0) dspice\_2dz = 0.0
2943 
2944         \textcolor{keywordflow}{if} (stays > 0.0) \textcolor{keywordflow}{then}
2945         \textcolor{comment}{! Limit the spiciness of the water that stays in the upper buffer layer.}
2946           \textcolor{keywordflow}{if} (abs(dspice\_lim) < abs(dspice\_2dz*(h1\_to\_k0 + h1\_to\_h2))) &
2947             dspice\_2dz = dspice\_lim/(h1\_to\_k0 + h1\_to\_h2)
2948 
2949           dspice\_stays = dspice\_2dz*(h1\_to\_k0 + h1\_to\_h2)
2950           t\_stays = t(i,kb1) + dt\_ds\_gauge * i\_denom * &
2951               (dt\_ds\_gauge * drcv\_dt(i) * drcv\_stays + drcv\_ds(i) * dspice\_stays)
2952           s\_stays = s(i,kb1) + i\_denom * &
2953               (drcv\_ds(i) * drcv\_stays - dt\_ds\_gauge * drcv\_dt(i) * dspice\_stays)
2954           \textcolor{comment}{! The values of R0 are based on changes in T and S.}
2955           r0\_stays = r0(i,kb1) + (t\_stays-t(i,kb1)) * dr0\_dt(i) + &
2956                                  (s\_stays-s(i,kb1)) * dr0\_ds(i)
2957         \textcolor{keywordflow}{else}
2958           \textcolor{comment}{! Limit the spiciness of the water that moves into the lower buffer layer.}
2959           \textcolor{keywordflow}{if} (abs(dspice\_lim) < abs(dspice\_2dz*h1\_to\_k0)) &
2960             dspice\_2dz = dspice\_lim/h1\_to\_k0
2961           \textcolor{comment}{! These will be multiplied by 0 later.}
2962           t\_stays = 0.0 ; s\_stays = 0.0 ; r0\_stays = 0.0
2963 \textcolor{keywordflow}{        endif}
2964 
2965         dspice\_det = - dspice\_2dz*(stays + h1\_to\_h2)
2966         t\_det = t(i,kb1) + dt\_ds\_gauge * i\_denom * &
2967             (dt\_ds\_gauge * drcv\_dt(i) * drcv\_det + drcv\_ds(i) * dspice\_det)
2968         s\_det = s(i,kb1) + i\_denom * &
2969             (drcv\_ds(i) * drcv\_det - dt\_ds\_gauge * drcv\_dt(i) * dspice\_det)
2970         \textcolor{comment}{! The values of R0 are based on changes in T and S.}
2971         r0\_det = r0(i,kb1) + (t\_det-t(i,kb1)) * dr0\_dt(i) + &
2972                              (s\_det-s(i,kb1)) * dr0\_ds(i)
2973 
2974         t(i,k0) = ((h1\_to\_k0*t\_det + h2*t(i,kb2)) + h(i,k0)*t(i,k0)) * ihk0
2975         t(i,kb2) = (h1*t(i,kb1) - stays*t\_stays - h1\_to\_k0*t\_det) * ih2f
2976         t(i,kb1) = (t\_to\_bl + stays*t\_stays) * ih1f
2977 
2978         s(i,k0) = ((h1\_to\_k0*s\_det + h2*s(i,kb2)) + h(i,k0)*s(i,k0)) * ihk0
2979         s(i,kb2) = (h1*s(i,kb1) - stays*s\_stays - h1\_to\_k0*s\_det) * ih2f
2980         s(i,kb1) = (s\_to\_bl + stays*s\_stays) * ih1f
2981 
2982         r0(i,k0) = ((h1\_to\_k0*r0\_det + h2*r0(i,kb2)) + h(i,k0)*r0(i,k0)) * ihk0
2983         r0(i,kb2) = (h1*r0(i,kb1) - stays*r0\_stays - h1\_to\_k0*r0\_det) * ih2f
2984         r0(i,kb1) = (r0\_to\_bl + stays*r0\_stays) * ih1f
2985 
2986 \textcolor{comment}{!        ! The following is 2nd-order upwind advection without limiters.}
2987 \textcolor{comment}{!        dT\_2dz = (T(i,kb1) - T(i,kb2)) * Ih12}
2988 \textcolor{comment}{!        T(i,k0) = (h1\_to\_k0*(T(i,kb1) - dT\_2dz*(stays+h1\_to\_h2)) + &}
2989 \textcolor{comment}{!                     h2*T(i,kb2) + h(i,k0)*T(i,k0)) * Ihk0}
2990 \textcolor{comment}{!        T(i,kb2) = T(i,kb1) + dT\_2dz*(h1\_to\_k0-stays)}
2991 \textcolor{comment}{!        T(i,kb1) = (T\_to\_bl + stays*(T(i,kb1) + &}
2992 \textcolor{comment}{!                      dT\_2dz*(h1\_to\_k0 + h1\_to\_h2))) * Ih1f}
2993 \textcolor{comment}{!        dS\_2dz = (S(i,kb1) - S(i,kb2)) * Ih12}
2994 \textcolor{comment}{!        S(i,k0) = (h1\_to\_k0*(S(i,kb1) - dS\_2dz*(stays+h1\_to\_h2)) + &}
2995 \textcolor{comment}{!                     h2*S(i,kb2) + h(i,k0)*S(i,k0)) * Ihk0}
2996 \textcolor{comment}{!        S(i,kb2) = S(i,kb1) + dS\_2dz*(h1\_to\_k0-stays)}
2997 \textcolor{comment}{!        S(i,kb1) = (S\_to\_bl + stays*(S(i,kb1) + &}
2998 \textcolor{comment}{!                      dS\_2dz*(h1\_to\_k0 + h1\_to\_h2))) * Ih1f}
2999 \textcolor{comment}{!        dR0\_2dz = (R0(i,kb1) - R0(i,kb2)) * Ih12}
3000 \textcolor{comment}{!        R0(i,k0) = (h1\_to\_k0*(R0(i,kb1) - dR0\_2dz*(stays+h1\_to\_h2)) + &}
3001 \textcolor{comment}{!                    h2*R0(i,kb2) + h(i,k0)*R0(i,k0)) * Ihk0}
3002 \textcolor{comment}{!        R0(i,kb2) = R0(i,kb1) + dR0\_2dz*(h1\_to\_k0-stays)}
3003 \textcolor{comment}{!        R0(i,kb1) = (R0\_to\_bl + stays*(R0(i,kb1) + &}
3004 \textcolor{comment}{!                     dR0\_2dz*(h1\_to\_k0 + h1\_to\_h2))) * Ih1f}
3005 
3006         d\_ea(i,kb1) = (d\_ea(i,kb1) + h\_to\_bl) + (stays - h1)
3007         d\_ea(i,kb2) = d\_ea(i,kb2) + (h1\_to\_h2 - h2)
3008         d\_ea(i,k0) = d\_ea(i,k0) + (h1\_to\_k0 + h2)
3009 
3010         h(i,kb1) = stays + h\_to\_bl
3011         h(i,kb2) = h1\_to\_h2
3012         h(i,k0) = h(i,k0) + (h1\_to\_k0 + h2)
3013         \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain)) &
3014           cs%diag\_PE\_detrain(i,j) = cs%diag\_PE\_detrain(i,j) + idt\_h2*dpe\_merge
3015         \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain2)) cs%diag\_PE\_detrain2(i,j) = &
3016              cs%diag\_PE\_detrain2(i,j) + idt\_h2*(dpe\_det + rho0xg*dpe\_extrap)
3017       \textcolor{keywordflow}{else} \textcolor{comment}{! Not mergeable\_bl.}
3018         \textcolor{comment}{! There is no further detrainment from the buffer layers, and the}
3019         \textcolor{comment}{! upper buffer layer water is distributed optimally between the}
3020         \textcolor{comment}{! upper and lower buffer layer.}
3021         h1\_to\_h2 = h1 - stays
3022         ih1f = 1.0 / (h\_to\_bl + stays) ; ih2f = 1.0 / (h2 + h1\_to\_h2)
3023         ih = 1.0 / (h1 + h2)
3024         dr0\_2dz = (r0(i,kb1) - r0(i,kb2)) * ih
3025         r0(i,kb2) = (h2*r0(i,kb2) + h1\_to\_h2*(r0(i,kb1) - &
3026                      scale\_slope*dr0\_2dz*stays)) * ih2f
3027         r0(i,kb1) = (r0\_to\_bl + stays*(r0(i,kb1) + &
3028                         scale\_slope*dr0\_2dz*h1\_to\_h2)) * ih1f
3029 
3030         \textcolor{comment}{! Use 2nd order upwind advection of spiciness, limited by the value}
3031         \textcolor{comment}{! in the detrained water to determine the detrained temperature and}
3032         \textcolor{comment}{! salinity.}
3033         dr0 = scale\_slope*dr0\_2dz*h1\_to\_h2
3034         dspice\_stays = (ds\_dt\_gauge*dr0\_ds(i)*(t(i,kb1)-t(i,kb2)) - &
3035                         dt\_ds\_gauge*dr0\_dt(i)*(s(i,kb1)-s(i,kb2))) * &
3036                         scale\_slope*h1\_to\_h2 * ih
3037         \textcolor{keywordflow}{if} (h\_to\_bl > 0.0) \textcolor{keywordflow}{then}
3038           dspice\_lim = (ds\_dt\_gauge*dr0\_ds(i)*(t\_to\_bl-t(i,kb1)*h\_to\_bl) - &
3039                         dt\_ds\_gauge*dr0\_dt(i)*(s\_to\_bl-s(i,kb1)*h\_to\_bl)) /&
3040                         h\_to\_bl
3041         \textcolor{keywordflow}{else}
3042           dspice\_lim = ds\_dt\_gauge*dr0\_ds(i)*(t(i,0)-t(i,kb1)) - &
3043                        dt\_ds\_gauge*dr0\_dt(i)*(s(i,0)-s(i,kb1))
3044 \textcolor{keywordflow}{        endif}
3045         \textcolor{keywordflow}{if} (dspice\_stays*dspice\_lim <= 0.0) \textcolor{keywordflow}{then}
3046           dspice\_stays = 0.0
3047         \textcolor{keywordflow}{elseif} (abs(dspice\_stays) > abs(dspice\_lim)) \textcolor{keywordflow}{then}
3048           dspice\_stays = dspice\_lim
3049 \textcolor{keywordflow}{        endif}
3050         i\_denom = 1.0 / (dr0\_ds(i)**2 + (dt\_ds\_gauge*dr0\_dt(i))**2)
3051         t\_stays = t(i,kb1) + dt\_ds\_gauge * i\_denom * &
3052             (dt\_ds\_gauge * dr0\_dt(i) * dr0 + dr0\_ds(i) * dspice\_stays)
3053         s\_stays = s(i,kb1) + i\_denom * &
3054             (dr0\_ds(i) * dr0 - dt\_ds\_gauge * dr0\_dt(i) * dspice\_stays)
3055         \textcolor{comment}{! The detrained values of Rcv are based on changes in T and S.}
3056         rcv\_stays = rcv(i,kb1) + (t\_stays-t(i,kb1)) * drcv\_dt(i) + &
3057                                  (s\_stays-s(i,kb1)) * drcv\_ds(i)
3058 
3059         t(i,kb2) = (h2*t(i,kb2) + h1*t(i,kb1) - t\_stays*stays) * ih2f
3060         t(i,kb1) = (t\_to\_bl + stays*t\_stays) * ih1f
3061         s(i,kb2) = (h2*s(i,kb2) + h1*s(i,kb1) - s\_stays*stays) * ih2f
3062         s(i,kb1) = (s\_to\_bl + stays*s\_stays) * ih1f
3063         rcv(i,kb2) = (h2*rcv(i,kb2) + h1*rcv(i,kb1) - rcv\_stays*stays) * ih2f
3064         rcv(i,kb1) = (rcv\_to\_bl + stays*rcv\_stays) * ih1f
3065 
3066 \textcolor{comment}{!        ! The following is 2nd-order upwind advection without limiters.}
3067 \textcolor{comment}{!        dRcv\_2dz = (Rcv(i,kb1) - Rcv(i,kb2)) * Ih}
3068 \textcolor{comment}{!        dRcv = scale\_slope*dRcv\_2dz*h1\_to\_h2}
3069 \textcolor{comment}{!        Rcv(i,kb2) = (h2*Rcv(i,kb2) + h1\_to\_h2*(Rcv(i,kb1) - &}
3070 \textcolor{comment}{!                      scale\_slope*dRcv\_2dz*stays)) * Ih2f}
3071 \textcolor{comment}{!        Rcv(i,kb1) = (Rcv\_to\_bl + stays*(Rcv(i,kb1) + dRcv)) * Ih1f}
3072 \textcolor{comment}{!        dT\_2dz = (T(i,kb1) - T(i,kb2)) * Ih}
3073 \textcolor{comment}{!        T(i,kb2) = (h2*T(i,kb2) + h1\_to\_h2*(T(i,kb1) - &}
3074 \textcolor{comment}{!                      scale\_slope*dT\_2dz*stays)) * Ih2f}
3075 \textcolor{comment}{!        T(i,kb1) = (T\_to\_bl + stays*(T(i,kb1) + &}
3076 \textcolor{comment}{!                      scale\_slope*dT\_2dz*h1\_to\_h2)) * Ih1f}
3077 \textcolor{comment}{!        dS\_2dz = (S(i,kb1) - S(i,kb2)) * Ih}
3078 \textcolor{comment}{!        S(i,kb2) = (h2*S(i,kb2) + h1\_to\_h2*(S(i,kb1) - &}
3079 \textcolor{comment}{!                      scale\_slope*dS\_2dz*stays)) * Ih2f}
3080 \textcolor{comment}{!        S(i,kb1) = (S\_to\_bl + stays*(S(i,kb1) + &}
3081 \textcolor{comment}{!                      scale\_slope*dS\_2dz*h1\_to\_h2)) * Ih1f}
3082 
3083         d\_ea(i,kb1) = d\_ea(i,kb1) + ((stays - h1) + h\_to\_bl)
3084         d\_ea(i,kb2) = d\_ea(i,kb2) + h1\_to\_h2
3085 
3086         h(i,kb1) = stays + h\_to\_bl
3087         h(i,kb2) = h(i,kb2) + h1\_to\_h2
3088 
3089         \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain)) &
3090           cs%diag\_PE\_detrain(i,j) = cs%diag\_PE\_detrain(i,j) + idt\_h2*dpe\_det
3091         \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(cs%diag\_PE\_detrain2)) cs%diag\_PE\_detrain2(i,j) = &
3092           cs%diag\_PE\_detrain2(i,j) + idt\_h2*(dpe\_det + rho0xg*dpe\_extrap)
3093 \textcolor{keywordflow}{      endif}
3094 \textcolor{keywordflow}{    endif} \textcolor{comment}{! End of detrainment...}
3095 
3096 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! i loop}
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}}
\subsubsection{\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(\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{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in,out}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. Layer 0 is the new mixed layer.\\
\hline
\mbox{\tt in,out}  & {\em t} & Layer temperatures \mbox{[}degC\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em s} & Layer salinities \mbox{[}ppt\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em r0} & Potential density referenced to surface pressure \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em rcv} & The coordinate defining potential density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em rcvtgt} & The target value of Rcv for each layer \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt in,out}  & {\em eps} & The (small) thickness that must remain in each layer \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt 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{\tt 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{\tt in}  & {\em ksort} & The density-\/sorted k-\/indicies.\\
\hline
 & {\em cs} & The control structure for this module.\\
\hline
\mbox{\tt 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{\tt 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{\tt 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{\tt 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}
1893   \textcolor{keywordtype}{type}(ocean\_grid\_type),                \textcolor{keywordtype}{intent(in)}    :: g\textcolor{comment}{       !< The ocean's grid structure.}
1894   \textcolor{keywordtype}{type}(verticalgrid\_type),              \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{      !< The ocean's vertical grid}
1895 \textcolor{comment}{                                                                 !! structure.}
1896   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))},   \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{       !< Layer thickness [H ~> m or kg m-2].}
1897 \textcolor{comment}{                                                                 !! Layer 0 is the new mixed layer.}
1898   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))},   \textcolor{keywordtype}{intent(inout)} :: t\textcolor{comment}{       !< Layer temperatures [degC].}
1899   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))},   \textcolor{keywordtype}{intent(inout)} :: s\textcolor{comment}{       !< Layer salinities [ppt].}
1900   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))},   \textcolor{keywordtype}{intent(inout)} :: r0\textcolor{comment}{      !< Potential density referenced to}
1901 \textcolor{comment}{                                                                 !! surface pressure [R ~> kg m-3].}
1902   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK0\_(GV))},   \textcolor{keywordtype}{intent(inout)} :: rcv\textcolor{comment}{     !< The coordinate defining}
1903 \textcolor{comment}{                                                                 !! potential density [R ~> kg m-3].}
1904   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZK\_(GV))},            \textcolor{keywordtype}{intent(in)}    :: rcvtgt\textcolor{comment}{  !< The target value of Rcv for each}
1905 \textcolor{comment}{                                                                 !! layer [R ~> kg m-3].}
1906   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},    \textcolor{keywordtype}{intent(inout)} :: eps\textcolor{comment}{     !< The (small) thickness that must}
1907 \textcolor{comment}{                                                                 !! remain in each layer [H ~> m or kg
       m-2].}
1908   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},    \textcolor{keywordtype}{intent(inout)} :: d\_ea\textcolor{comment}{    !< The upward increase across a}
1909 \textcolor{comment}{                                                                 !! layer in the entrainment from}
1910 \textcolor{comment}{                                                                 !! above [H ~> m or kg m-2].}
1911 \textcolor{comment}{                                                                 !!  Positive d\_ea goes with layer}
1912 \textcolor{comment}{                                                                 !! thickness increases.}
1913   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},    \textcolor{keywordtype}{intent(inout)} :: d\_eb\textcolor{comment}{    !< The downward increase across a}
1914 \textcolor{comment}{                                                                 !! layer in the entrainment from}
1915 \textcolor{comment}{                                                                 !! below [H ~> m or kg m-2]. Positive
       values go}
1916 \textcolor{comment}{                                                                 !! with mass gain by a layer.}
1917   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))}, \textcolor{keywordtype}{intent(in)}    :: ksort\textcolor{comment}{   !< The density-sorted k-indicies.}
1918   \textcolor{keywordtype}{type}(bulkmixedlayer\_cs),              \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{      !< The control structure for this}
1919 \textcolor{comment}{                                                                 !! module.}
1920   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},             \textcolor{keywordtype}{intent(in)}    :: dr0\_dt\textcolor{comment}{  !< The partial derivative of}
1921 \textcolor{comment}{                                                                 !! potential density referenced}
1922 \textcolor{comment}{                                                                 !! to the surface with potential}
1923 \textcolor{comment}{                                                                 !! temperature [R degC-1 ~> kg m-3
       degC-1].}
1924   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},             \textcolor{keywordtype}{intent(in)}    :: dr0\_ds\textcolor{comment}{  !< The partial derivative of}
1925 \textcolor{comment}{                                                                 !! cpotential density referenced}
1926 \textcolor{comment}{                                                                 !! to the surface with salinity,}
1927 \textcolor{comment}{                                                                 !! [R ppt-1 ~> kg m-3 ppt-1].}
1928   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},             \textcolor{keywordtype}{intent(in)}    :: drcv\_dt\textcolor{comment}{ !< The partial derivative of}
1929 \textcolor{comment}{                                                                 !! coordinate defining potential}
1930 \textcolor{comment}{                                                                 !! density with potential}
1931 \textcolor{comment}{                                                                 !! temperature [R degC-1 ~> kg m-3
       degC-1].}
1932   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))},             \textcolor{keywordtype}{intent(in)}    :: drcv\_ds\textcolor{comment}{ !< The partial derivative of}
1933 \textcolor{comment}{                                                                 !! coordinate defining potential}
1934 \textcolor{comment}{                                                                 !! density with salinity,}
1935 \textcolor{comment}{                                                                 !! [R ppt-1 ~> kg m-3 ppt-1].}
1936 
1937 \textcolor{comment}{!   If there are no massive light layers above the deepest of the mixed- and}
1938 \textcolor{comment}{! buffer layers, do nothing (except perhaps to reshuffle these layers).}
1939 \textcolor{comment}{!   If there are nkbl or fewer layers above the deepest mixed- or buffer-}
1940 \textcolor{comment}{! layers, move them (in sorted order) into the buffer layers, even if they}
1941 \textcolor{comment}{! were previously interior layers.}
1942 \textcolor{comment}{!   If there are interior layers that are intermediate in density (both in-situ}
1943 \textcolor{comment}{! and the coordinate density (sigma-2)) between the newly forming mixed layer}
1944 \textcolor{comment}{! and a residual buffer- or mixed layer, and the number of massive layers above}
1945 \textcolor{comment}{! the deepest massive buffer or mixed layer is greater than nkbl, then split}
1946 \textcolor{comment}{! those buffer layers into peices that match the target density of the two}
1947 \textcolor{comment}{! nearest interior layers.}
1948 \textcolor{comment}{!   Otherwise, if there are more than nkbl+1 remaining massive layers}
1949 
1950   \textcolor{comment}{! Local variables}
1951   \textcolor{keywordtype}{real}    :: h\_move, h\_tgt\_old, i\_hnew
1952   \textcolor{keywordtype}{real}    :: dt\_ds\_wt2, dt\_dr, ds\_dr, i\_denom
1953   \textcolor{keywordtype}{real}    :: rcv\_int
1954   \textcolor{keywordtype}{real}    :: t\_up, s\_up, r0\_up, i\_hup, h\_to\_up
1955   \textcolor{keywordtype}{real}    :: t\_dn, s\_dn, r0\_dn, i\_hdn, h\_to\_dn
1956   \textcolor{keywordtype}{real}    :: wt\_dn
1957   \textcolor{keywordtype}{real}    :: dr1, dr2
1958   \textcolor{keywordtype}{real}    :: dpe, hmin, min\_dpe, min\_hmin
1959   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZK\_(GV))} :: &
1960     h\_tmp, r0\_tmp, t\_tmp, s\_tmp, rcv\_tmp
1961   \textcolor{keywordtype}{integer} :: ks\_min
1962   \textcolor{keywordtype}{logical} :: sorted, leave\_in\_layer
1963   \textcolor{keywordtype}{integer} :: ks\_deep(szi\_(g)), k\_count(szi\_(g)), ks2\_reverse(szi\_(g), szk\_(gv))
1964   \textcolor{keywordtype}{integer} :: ks2(szk\_(gv))
1965   \textcolor{keywordtype}{integer} :: i, k, ks, is, ie, nz, k1, k2, k\_tgt, k\_src, k\_int\_top
1966   \textcolor{keywordtype}{integer} :: nks, nkmb, num\_interior, top\_interior\_ks
1967 
1968   is = g%isc ; ie = g%iec ; nz = gv%ke
1969   nkmb = cs%nkml+cs%nkbl
1970 
1971   dt\_ds\_wt2 = cs%dT\_dS\_wt**2
1972 
1973 \textcolor{comment}{! Find out how many massive layers are above the deepest buffer or mixed layer.}
1974   \textcolor{keywordflow}{do} i=is,ie ; ks\_deep(i) = -1 ; k\_count(i) = 0 ;\textcolor{keywordflow}{ enddo}
1975   \textcolor{keywordflow}{do} ks=nz,1,-1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (ksort(i,ks) > 0) \textcolor{keywordflow}{then}
1976     k = ksort(i,ks)
1977 
1978     \textcolor{keywordflow}{if} (h(i,k) > eps(i,k)) \textcolor{keywordflow}{then}
1979       \textcolor{keywordflow}{if} (ks\_deep(i) == -1) \textcolor{keywordflow}{then}
1980         \textcolor{keywordflow}{if} (k <= nkmb) \textcolor{keywordflow}{then}
1981           ks\_deep(i) = ks ; k\_count(i) = k\_count(i) + 1
1982           ks2\_reverse(i,k\_count(i)) = k
1983 \textcolor{keywordflow}{        endif}
1984       \textcolor{keywordflow}{else}
1985         k\_count(i) = k\_count(i) + 1
1986         ks2\_reverse(i,k\_count(i)) = k
1987 \textcolor{keywordflow}{      endif}
1988 \textcolor{keywordflow}{    endif}
1989 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
1990 
1991   \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (k\_count(i) > 1) \textcolor{keywordflow}{then}
1992     \textcolor{comment}{! This column might need to be reshuffled.}
1993     nks = k\_count(i)
1994 
1995     \textcolor{comment}{! Put ks2 in the right order and check whether reshuffling is needed.}
1996     sorted = .true.
1997     ks2(nks) = ks2\_reverse(i,1)
1998     \textcolor{keywordflow}{do} ks=nks-1,1,-1
1999       ks2(ks) = ks2\_reverse(i,1+nks-ks)
2000       \textcolor{keywordflow}{if} (ks2(ks) > ks2(ks+1)) sorted = .false.
2001 \textcolor{keywordflow}{    enddo}
2002 
2003     \textcolor{comment}{! Go to the next column of no reshuffling is needed.}
2004     \textcolor{keywordflow}{if} (sorted) cycle
2005 
2006     \textcolor{comment}{! Find out how many interior layers are being reshuffled.  If none,}
2007     \textcolor{comment}{! then this is a simple swapping procedure.}
2008     num\_interior = 0 ; top\_interior\_ks = 0
2009     \textcolor{keywordflow}{do} ks=nks,1,-1 ; \textcolor{keywordflow}{if} (ks2(ks) > nkmb) \textcolor{keywordflow}{then}
2010       num\_interior = num\_interior+1 ; top\_interior\_ks = ks
2011 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}
2012 
2013     \textcolor{keywordflow}{if} (num\_interior >= 1) \textcolor{keywordflow}{then}
2014       \textcolor{comment}{! Find the lightest interior layer with a target coordinate density}
2015       \textcolor{comment}{! greater than the newly forming mixed layer.}
2016       \textcolor{keywordflow}{do} k=nkmb+1,nz ; \textcolor{keywordflow}{if} (rcv(i,0) < rcvtgt(k)) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}
2017       k\_int\_top = k ; rcv\_int = rcvtgt(k)
2018 
2019       i\_denom = 1.0 / (drcv\_ds(i)**2 + dt\_ds\_wt2*drcv\_dt(i)**2)
2020       dt\_dr = dt\_ds\_wt2*drcv\_dt(i) * i\_denom
2021       ds\_dr = drcv\_ds(i) * i\_denom
2022 
2023 
2024       \textcolor{comment}{! Examine whether layers can be split out of existence.  Stop when there}
2025       \textcolor{comment}{! is a layer that cannot be handled this way, or when the topmost formerly}
2026       \textcolor{comment}{! interior layer has been dealt with.}
2027       \textcolor{keywordflow}{do} ks = nks,top\_interior\_ks,-1
2028         k = ks2(ks)
2029         leave\_in\_layer = .false.
2030         \textcolor{keywordflow}{if} ((k > nkmb) .and. (rcv(i,k) <= rcvtgt(k))) \textcolor{keywordflow}{then}
2031           \textcolor{keywordflow}{if} (rcvtgt(k)-rcv(i,k) < cs%BL\_split\_rho\_tol*(rcvtgt(k) - rcvtgt(k-1))) &
2032             leave\_in\_layer = .true.
2033         \textcolor{keywordflow}{elseif} (k > nkmb) \textcolor{keywordflow}{then}
2034           \textcolor{keywordflow}{if} (rcv(i,k)-rcvtgt(k) < cs%BL\_split\_rho\_tol*(rcvtgt(k+1) - rcvtgt(k))) &
2035             leave\_in\_layer = .true.
2036 \textcolor{keywordflow}{        endif}
2037 
2038         \textcolor{keywordflow}{if} (leave\_in\_layer) \textcolor{keywordflow}{then}
2039           \textcolor{comment}{! Just drop this layer from the sorted list.}
2040           nks = nks-1
2041         \textcolor{keywordflow}{elseif} (rcv(i,k) < rcv\_int) \textcolor{keywordflow}{then}
2042           \textcolor{comment}{! There is no interior layer with a target density that is intermediate}
2043           \textcolor{comment}{! between this layer and the mixed layer.}
2044           \textcolor{keywordflow}{exit}
2045         \textcolor{keywordflow}{else}
2046           \textcolor{comment}{! Try splitting the layer between two interior isopycnal layers.}
2047           \textcolor{comment}{! Find the target densities that bracket this layer.}
2048           \textcolor{keywordflow}{do} k2=k\_int\_top+1,nz ; \textcolor{keywordflow}{if} (rcv(i,k) < rcvtgt(k2)) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}
2049           \textcolor{keywordflow}{if} (k2>nz) \textcolor{keywordflow}{exit}
2050 
2051           \textcolor{comment}{! This layer is bracketed in density between layers k2-1 and k2.}
2052 
2053           dr1 = (rcvtgt(k2-1) - rcv(i,k)) ; dr2 = (rcvtgt(k2) - rcv(i,k))
2054           t\_up = t(i,k) + dt\_dr * dr1
2055           s\_up = s(i,k) + ds\_dr * dr1
2056           t\_dn = t(i,k) + dt\_dr * dr2
2057           s\_dn = s(i,k) + ds\_dr * dr2
2058 
2059           r0\_up = r0(i,k) + (dt\_dr*dr0\_dt(i) + ds\_dr*dr0\_ds(i)) * dr1
2060           r0\_dn = r0(i,k) + (dt\_dr*dr0\_dt(i) + ds\_dr*dr0\_ds(i)) * dr2
2061 
2062           \textcolor{comment}{! Make sure the new properties are acceptable.}
2063           \textcolor{keywordflow}{if} ((r0\_up > r0(i,0)) .or. (r0\_dn > r0(i,0))) &
2064             \textcolor{comment}{! Avoid creating obviously unstable profiles.}
2065             \textcolor{keywordflow}{exit}
2066 
2067           wt\_dn = (rcv(i,k) - rcvtgt(k2-1)) / (rcvtgt(k2) - rcvtgt(k2-1))
2068           h\_to\_up = (h(i,k)-eps(i,k)) * (1.0 - wt\_dn)
2069           h\_to\_dn = (h(i,k)-eps(i,k)) * wt\_dn
2070 
2071           i\_hup = 1.0 / (h(i,k2-1) + h\_to\_up)
2072           i\_hdn = 1.0 / (h(i,k2) + h\_to\_dn)
2073           r0(i,k2-1) = (r0(i,k2)*h(i,k2-1) + r0\_up*h\_to\_up) * i\_hup
2074           r0(i,k2) = (r0(i,k2)*h(i,k2) + r0\_dn*h\_to\_dn) * i\_hdn
2075 
2076           t(i,k2-1) = (t(i,k2)*h(i,k2-1) + t\_up*h\_to\_up) * i\_hup
2077           t(i,k2) = (t(i,k2)*h(i,k2) + t\_dn*h\_to\_dn) * i\_hdn
2078           s(i,k2-1) = (s(i,k2)*h(i,k2-1) + s\_up*h\_to\_up) * i\_hup
2079           s(i,k2) = (s(i,k2)*h(i,k2) + s\_dn*h\_to\_dn) * i\_hdn
2080           rcv(i,k2-1) = (rcv(i,k2)*h(i,k2-1) + rcvtgt(k2-1)*h\_to\_up) * i\_hup
2081           rcv(i,k2) = (rcv(i,k2)*h(i,k2) + rcvtgt(k2)*h\_to\_dn) * i\_hdn
2082 
2083           h(i,k) = eps(i,k)
2084           h(i,k2) = h(i,k2) + h\_to\_dn
2085           h(i,k2-1) = h(i,k2-1) + h\_to\_up
2086 
2087           \textcolor{keywordflow}{if} (k > k2-1) \textcolor{keywordflow}{then}
2088             d\_eb(i,k) = d\_eb(i,k) - h\_to\_up
2089             d\_eb(i,k2-1) = d\_eb(i,k2-1) + h\_to\_up
2090           \textcolor{keywordflow}{elseif} (k < k2-1) \textcolor{keywordflow}{then}
2091             d\_ea(i,k) = d\_ea(i,k) - h\_to\_up
2092             d\_ea(i,k2-1) = d\_ea(i,k2-1) + h\_to\_up
2093 \textcolor{keywordflow}{          endif}
2094           \textcolor{keywordflow}{if} (k > k2) \textcolor{keywordflow}{then}
2095             d\_eb(i,k) = d\_eb(i,k) - h\_to\_dn
2096             d\_eb(i,k2) = d\_eb(i,k2) + h\_to\_dn
2097           \textcolor{keywordflow}{elseif} (k < k2) \textcolor{keywordflow}{then}
2098             d\_ea(i,k) = d\_ea(i,k) - h\_to\_dn
2099             d\_ea(i,k2) = d\_ea(i,k2) + h\_to\_dn
2100 \textcolor{keywordflow}{          endif}
2101           nks = nks-1
2102 \textcolor{keywordflow}{        endif}
2103 \textcolor{keywordflow}{      enddo}
2104 \textcolor{keywordflow}{    endif}
2105 
2106     \textcolor{keywordflow}{do} \textcolor{keywordflow}{while} (nks > nkmb)
2107       \textcolor{comment}{! Having already tried to move surface layers into the interior, there}
2108       \textcolor{comment}{! are still too many layers, and layers must be merged until nks=nkmb.}
2109       \textcolor{comment}{! Examine every merger of a layer with its neighbors, and merge the ones}
2110       \textcolor{comment}{! that increase the potential energy the least.  If there are layers}
2111       \textcolor{comment}{! with (apparently?) negative potential energy change, choose the one}
2112       \textcolor{comment}{! with the smallest total thickness.  Repeat until nkmb layers remain.}
2113       \textcolor{comment}{! Choose the smaller value for the remaining index for convenience.}
2114 
2115       ks\_min = -1 ; min\_dpe = 1.0 ; min\_hmin = 0.0
2116       \textcolor{keywordflow}{do} ks=1,nks-1
2117         k1 = ks2(ks) ; k2 = ks2(ks+1)
2118         dpe = max(0.0, (r0(i,k2)-r0(i,k1)) * h(i,k1) * h(i,k2))
2119         hmin = min(h(i,k1)-eps(i,k1), h(i,k2)-eps(i,k2))
2120         \textcolor{keywordflow}{if} ((ks\_min < 0) .or. (dpe < min\_dpe) .or. &
2121             ((dpe <= 0.0) .and. (hmin < min\_hmin))) \textcolor{keywordflow}{then}
2122            ks\_min = ks ; min\_dpe = dpe ; min\_hmin = hmin
2123 \textcolor{keywordflow}{        endif}
2124 \textcolor{keywordflow}{      enddo}
2125 
2126       \textcolor{comment}{! Now merge the two layers that do the least damage.}
2127       k1 = ks2(ks\_min) ; k2 = ks2(ks\_min+1)
2128       \textcolor{keywordflow}{if} (k1 < k2) \textcolor{keywordflow}{then} ; k\_tgt = k1 ; k\_src = k2
2129       \textcolor{keywordflow}{else} ; k\_tgt = k2 ; k\_src = k1 ; ks2(ks\_min) = k2 ;\textcolor{keywordflow}{ endif}
2130 
2131       h\_tgt\_old = h(i,k\_tgt)
2132       h\_move = h(i,k\_src)-eps(i,k\_src)
2133       h(i,k\_src) = eps(i,k\_src)
2134       h(i,k\_tgt) = h(i,k\_tgt) + h\_move
2135       i\_hnew = 1.0 / (h(i,k\_tgt))
2136       r0(i,k\_tgt) = (r0(i,k\_tgt)*h\_tgt\_old + r0(i,k\_src)*h\_move) * i\_hnew
2137 
2138       t(i,k\_tgt) = (t(i,k\_tgt)*h\_tgt\_old + t(i,k\_src)*h\_move) * i\_hnew
2139       s(i,k\_tgt) = (s(i,k\_tgt)*h\_tgt\_old + s(i,k\_src)*h\_move) * i\_hnew
2140       rcv(i,k\_tgt) = (rcv(i,k\_tgt)*h\_tgt\_old + rcv(i,k\_src)*h\_move) * i\_hnew
2141 
2142       d\_eb(i,k\_src) = d\_eb(i,k\_src) - h\_move
2143       d\_eb(i,k\_tgt) = d\_eb(i,k\_tgt) + h\_move
2144 
2145       \textcolor{comment}{! Remove the newly missing layer from the sorted list.}
2146       \textcolor{keywordflow}{do} ks=ks\_min+1,nks ; ks2(ks) = ks2(ks+1) ;\textcolor{keywordflow}{ enddo}
2147       nks = nks-1
2148 \textcolor{keywordflow}{    enddo}
2149 
2150     \textcolor{comment}{!   Check again whether the layers are sorted, and go on to the next column}
2151     \textcolor{comment}{! if they are.}
2152     sorted = .true.
2153     \textcolor{keywordflow}{do} ks=1,nks-1 ; \textcolor{keywordflow}{if} (ks2(ks) > ks2(ks+1)) sorted = .false. ;\textcolor{keywordflow}{ enddo}
2154     \textcolor{keywordflow}{if} (sorted) cycle
2155 
2156     \textcolor{keywordflow}{if} (nks > 1) \textcolor{keywordflow}{then}
2157       \textcolor{comment}{! At this point, actually swap the properties of the layers, and place}
2158       \textcolor{comment}{! the remaining layers in order starting with nkmb.}
2159 
2160       \textcolor{comment}{! Save all the properties of the nkmb layers that might be replaced.}
2161       \textcolor{keywordflow}{do} k=1,nkmb
2162         h\_tmp(k) = h(i,k) ; r0\_tmp(k) = r0(i,k)
2163         t\_tmp(k) = t(i,k) ; s\_tmp(k) = s(i,k) ; rcv\_tmp(k) = rcv(i,k)
2164 
2165         h(i,k) = 0.0
2166 \textcolor{keywordflow}{      enddo}
2167 
2168       \textcolor{keywordflow}{do} ks=nks,1,-1
2169         k\_tgt = nkmb - nks + ks ; k\_src = ks2(ks)
2170         \textcolor{keywordflow}{if} (k\_tgt == k\_src) \textcolor{keywordflow}{then}
2171           h(i,k\_tgt) = h\_tmp(k\_tgt)  \textcolor{comment}{! This layer doesn't move, so put the water back.}
2172           cycle
2173 \textcolor{keywordflow}{        endif}
2174 
2175         \textcolor{comment}{! Note below that eps=0 for k<=nkmb.}
2176         \textcolor{keywordflow}{if} (k\_src > nkmb) \textcolor{keywordflow}{then}
2177           h\_move = h(i,k\_src)-eps(i,k\_src)
2178           h(i,k\_src) = eps(i,k\_src)
2179           h(i,k\_tgt) = h\_move
2180           r0(i,k\_tgt) = r0(i,k\_src)
2181 
2182           t(i,k\_tgt) = t(i,k\_src) ; s(i,k\_tgt) = s(i,k\_src)
2183           rcv(i,k\_tgt) = rcv(i,k\_src)
2184 
2185           d\_eb(i,k\_src) = d\_eb(i,k\_src) - h\_move
2186           d\_eb(i,k\_tgt) = d\_eb(i,k\_tgt) + h\_move
2187         \textcolor{keywordflow}{else}
2188           h(i,k\_tgt) = h\_tmp(k\_src)
2189           r0(i,k\_tgt) = r0\_tmp(k\_src)
2190 
2191           t(i,k\_tgt) = t\_tmp(k\_src) ; s(i,k\_tgt) = s\_tmp(k\_src)
2192           rcv(i,k\_tgt) = rcv\_tmp(k\_src)
2193 
2194           \textcolor{keywordflow}{if} (k\_src > k\_tgt) \textcolor{keywordflow}{then}
2195             d\_eb(i,k\_src) = d\_eb(i,k\_src) - h\_tmp(k\_src)
2196             d\_eb(i,k\_tgt) = d\_eb(i,k\_tgt) + h\_tmp(k\_src)
2197           \textcolor{keywordflow}{else}
2198             d\_ea(i,k\_src) = d\_ea(i,k\_src) - h\_tmp(k\_src)
2199             d\_ea(i,k\_tgt) = d\_ea(i,k\_tgt) + h\_tmp(k\_src)
2200 \textcolor{keywordflow}{          endif}
2201 \textcolor{keywordflow}{        endif}
2202 \textcolor{keywordflow}{      enddo}
2203 \textcolor{keywordflow}{    endif}
2204 
2205 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}
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}}
\subsubsection{\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(\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{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em r0} & The potential density used to sort the layers \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}.\\
\hline
\mbox{\tt 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{\tt 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}
1841   \textcolor{keywordtype}{type}(ocean\_grid\_type),                \textcolor{keywordtype}{intent(in)}  :: g\textcolor{comment}{     !< The ocean's grid structure.}
1842   \textcolor{keywordtype}{type}(verticalgrid\_type),              \textcolor{keywordtype}{intent(in)}  :: gv\textcolor{comment}{    !< The ocean's vertical grid structure.}
1843   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},    \textcolor{keywordtype}{intent(in)}  :: h\textcolor{comment}{     !< Layer thickness [H ~> m or kg m-2].}
1844   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},    \textcolor{keywordtype}{intent(in)}  :: r0\textcolor{comment}{    !< The potential density used to sort}
1845 \textcolor{comment}{                                                             !! the layers [R ~> kg m-3].}
1846   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))},    \textcolor{keywordtype}{intent(in)}  :: eps\textcolor{comment}{   !< The (small) thickness that must}
1847 \textcolor{comment}{                                                             !! remain in each layer [H ~> m or kg m-2].}
1848   \textcolor{keywordtype}{type}(bulkmixedlayer\_cs),              \textcolor{keywordtype}{pointer}     :: cs\textcolor{comment}{    !< The control structure returned by a}
1849 \textcolor{comment}{                                                             !! previous call to mixedlayer\_init.}
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.}
1851 
1852   \textcolor{comment}{! Local variables}
1853   \textcolor{keywordtype}{real} :: r0sort(szi\_(g),szk\_(gv))
1854   \textcolor{keywordtype}{integer} :: nsort(szi\_(g))
1855   \textcolor{keywordtype}{logical} :: done\_sorting(szi\_(g))
1856   \textcolor{keywordtype}{integer} :: i, k, ks, is, ie, nz, nkmb
1857 
1858   is = g%isc ; ie = g%iec ; nz = gv%ke
1859   nkmb = cs%nkml+cs%nkbl
1860 
1861   \textcolor{comment}{!   Come up with a sorted index list of layers with increasing R0.}
1862   \textcolor{comment}{! Assume that the layers below nkmb are already stably stratified.}
1863   \textcolor{comment}{! Only layers that are thicker than eps are in the list.  Extra elements}
1864   \textcolor{comment}{! have an index of -1.}
1865 
1866   \textcolor{comment}{!   This is coded using straight insertion, on the assumption that the}
1867   \textcolor{comment}{! layers are usually in the right order (or massless) anyway.}
1868 
1869   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; ksort(i,k) = -1 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
1870 
1871   \textcolor{keywordflow}{do} i=is,ie ; nsort(i) = 0 ; done\_sorting(i) = .false. ;\textcolor{keywordflow}{ enddo}
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}
1873     \textcolor{keywordflow}{if} (done\_sorting(i)) \textcolor{keywordflow}{then} ; ks = nsort(i) ; \textcolor{keywordflow}{else}
1874       \textcolor{keywordflow}{do} ks=nsort(i),1,-1
1875         \textcolor{keywordflow}{if} (r0(i,k) >= r0sort(i,ks)) \textcolor{keywordflow}{exit}
1876         r0sort(i,ks+1) = r0sort(i,ks) ; ksort(i,ks+1) = ksort(i,ks)
1877 \textcolor{keywordflow}{      enddo}
1878       \textcolor{keywordflow}{if} ((k > nkmb) .and. (ks == nsort(i))) done\_sorting(i) = .true.
1879 \textcolor{keywordflow}{    endif}
1880 
1881     ksort(i,ks+1) = k
1882     r0sort(i,ks+1) = r0(i,k)
1883     nsort(i) = nsort(i) + 1
1884 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
1885 
\end{DoxyCode}
