\hypertarget{namespacemom__entrain__diffusive}{}\doxysection{mom\+\_\+entrain\+\_\+diffusive Module Reference}
\label{namespacemom__entrain__diffusive}\index{mom\_entrain\_diffusive@{mom\_entrain\_diffusive}}


\doxysubsection{Detailed Description}
Diapycnal mixing and advection in isopycnal mode. 

By Robert Hallberg, September 1997 -\/ July 2000.

This file contains the subroutines that implement diapycnal mixing and advection in isopycnal layers. The main subroutine, calculate\+\_\+entrainment, returns the entrainment by each layer across the interfaces above and below it. These are calculated subject to the constraints that no layers can be driven to neg-\/ ative thickness and that the each layer maintains its target density, using the scheme described in Hallberg (M\+WR 2000). There may or may not be a bulk mixed layer above the isopycnal layers. The solution is iterated until the change in the entrainment between successive iterations is less than some small tolerance.

The dual-\/stream entrainment scheme of Mac\+Dougall and Dewar (J\+PO 1997) is used for combined diapycnal advection and diffusion, modified as described in Hallberg (M\+WR 2000) to be solved implicitly in time. Any profile of diffusivities may be used. Diapycnal advection is fundamentally the residual of diapycnal diffusion, so the fully implicit upwind differencing scheme that is used is entirely appropriate. The downward buoyancy flux in each layer is determined from an implicit calculation based on the previously calculated flux of the layer above and an estim-\/ ated flux in the layer below. This flux is subject to the foll-\/ owing conditions\+: (1) the flux in the top and bottom layers are set by the boundary conditions, and (2) no layer may be driven below an Angstrom thickness. If there is a bulk mixed layer, the mixed and buffer layers are treated as Eulerian layers, whose thicknesses only change due to entrainment by the interior layers. \doxysubsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__entrain__diffusive_1_1entrain__diffusive__cs}{entrain\+\_\+diffusive\+\_\+cs}}
\begin{DoxyCompactList}\small\item\em The control structure holding parametes for the M\+O\+M\+\_\+entrain\+\_\+diffusive module. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \mbox{\hyperlink{namespacemom__entrain__diffusive_ae68ab2fa707778de2f92ce179729f2ff}{entrainment\+\_\+diffusive}} (h, tv, fluxes, dt, G, GV, US, CS, ea, eb, kb\+\_\+out, Kd\+\_\+\+Lay, Kd\+\_\+int)
\begin{DoxyCompactList}\small\item\em This subroutine calculates ea and eb, the rates at which a layer entrains from the layers above and below. The entrainment rates are proportional to the buoyancy flux in a layer and inversely proportional to the density differences between layers. The scheme that is used here is described in detail in Hallberg, Mon. Wea. Rev. 2000. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__entrain__diffusive_a13bbe9f35e3198470100375d9d016b7a}{f\+\_\+to\+\_\+ent}} (F, h, kb, kmb, j, G, GV, CS, dsp1\+\_\+ds, eakb, Ent\+\_\+bl, ea, eb, do\+\_\+i\+\_\+in)
\begin{DoxyCompactList}\small\item\em This subroutine calculates the actual entrainments (ea and eb) and the amount of surface forcing that is applied to each layer if there is no bulk mixed layer. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__entrain__diffusive_a363a25e7823043bb028e920e359733b0}{set\+\_\+ent\+\_\+bl}} (h, dt\+Kd\+\_\+int, tv, kb, kmb, do\+\_\+i, G, GV, US, CS, j, Ent\+\_\+bl, Sref, h\+\_\+bl)
\begin{DoxyCompactList}\small\item\em This subroutine sets the average entrainment across each of the interfaces between buffer layers within a timestep. It also causes thin and relatively light interior layers to be entrained by the deepest buffer layer. Also find the initial coordinate potential densities (Sref) of each layer. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__entrain__diffusive_aa2d2f462912ba1e201b1b73e62a905f3}{determine\+\_\+dskb}} (h\+\_\+bl, Sref, Ent\+\_\+bl, E\+\_\+kb, is, ie, kmb, G, GV, limit, d\+Skb, dd\+Skb\+\_\+dE, d\+Slay, dd\+Slay\+\_\+dE, d\+S\+\_\+anom\+\_\+lim, do\+\_\+i\+\_\+in)
\begin{DoxyCompactList}\small\item\em This subroutine determines the reference density difference between the bottommost buffer layer and the first interior after the mixing between mixed and buffer layers and mixing with the layer below. Within the mixed and buffer layers, entrainment from the layer above is increased when it is necessary to keep the layers from developing a negative thickness; otherwise it equals Ent\+\_\+bl. At each interface, the upward and downward fluxes average out to Ent\+\_\+bl, unless entrainment by the layer below is larger than twice Ent\+\_\+bl. The density difference across the first interior layer may also be returned. It could also be limited to avoid negative values or values that greatly exceed the density differences across an interface. Additionally, the partial derivatives of d\+Skb and d\+Slay with E\+\_\+kb could also be returned. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__entrain__diffusive_adce1b1ba57f5245f1eda57e7a254d4df}{f\+\_\+kb\+\_\+to\+\_\+ea\+\_\+kb}} (h\+\_\+bl, Sref, Ent\+\_\+bl, I\+\_\+d\+Skbp1, F\+\_\+kb, kmb, i, G, GV, CS, ea\+\_\+kb, tol\+\_\+in)
\begin{DoxyCompactList}\small\item\em Given an entrainment from below for layer kb, determine a consistent entrainment from above, such that d\+Skb $\ast$ ea\+\_\+kb = d\+Skbp1 $\ast$ F\+\_\+kb. The input value of ea\+\_\+kb is both the maximum value that can be obtained and the first guess of the iterations. Ideally ea\+\_\+kb should be an under-\/estimate. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__entrain__diffusive_a541b3aadc418110f7359b0ae401e4e78}{determine\+\_\+ea\+\_\+kb}} (h\+\_\+bl, dt\+Kd\+\_\+kb, Sref, I\+\_\+d\+Skbp1, Ent\+\_\+bl, ea\+\_\+kbp1, min\+\_\+eakb, max\+\_\+eakb, kmb, is, ie, do\+\_\+i, G, GV, CS, Ent, error, err\+\_\+min\+\_\+eakb0, err\+\_\+max\+\_\+eakb0, F\+\_\+kb, d\+Fdfm\+\_\+kb)
\begin{DoxyCompactList}\small\item\em This subroutine determines the entrainment from above by the top interior layer (labeled kb elsewhere) given an entrainment by the layer below it, constrained to be within the provided bounds. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__entrain__diffusive_ae45dbf976314c3a9e278ebcebedab109}{find\+\_\+maxf\+\_\+kb}} (h\+\_\+bl, Sref, Ent\+\_\+bl, I\+\_\+d\+Skbp1, min\+\_\+ent\+\_\+in, max\+\_\+ent\+\_\+in, kmb, is, ie, G, GV, CS, maxF, ent\+\_\+maxF, do\+\_\+i\+\_\+in, F\+\_\+lim\+\_\+maxent, F\+\_\+thresh)
\begin{DoxyCompactList}\small\item\em Maximize F = ent$\ast$ds\+\_\+kb$\ast$\+I\+\_\+d\+Skbp1 in the range min\+\_\+ent $<$ ent $<$ max\+\_\+ent. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__entrain__diffusive_a08d3203366fac7ec13472c79681dea0e}{entrain\+\_\+diffusive\+\_\+init}} (Time, G, GV, US, param\+\_\+file, diag, CS, just\+\_\+read\+\_\+params)
\begin{DoxyCompactList}\small\item\em This subroutine initializes the parameters and memory associated with the entrain\+\_\+diffusive module. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__entrain__diffusive_a0e71b47abe1e1889f4b687139615ca14}{entrain\+\_\+diffusive\+\_\+end}} (CS)
\begin{DoxyCompactList}\small\item\em This subroutine cleans up and deallocates any memory associated with the entrain\+\_\+diffusive module. \end{DoxyCompactList}\end{DoxyCompactItemize}


\doxysubsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__entrain__diffusive_aa2d2f462912ba1e201b1b73e62a905f3}\label{namespacemom__entrain__diffusive_aa2d2f462912ba1e201b1b73e62a905f3}} 
\index{mom\_entrain\_diffusive@{mom\_entrain\_diffusive}!determine\_dskb@{determine\_dskb}}
\index{determine\_dskb@{determine\_dskb}!mom\_entrain\_diffusive@{mom\_entrain\_diffusive}}
\doxysubsubsection{\texorpdfstring{determine\_dskb()}{determine\_dskb()}}
{\footnotesize\ttfamily subroutine mom\+\_\+entrain\+\_\+diffusive\+::determine\+\_\+dskb (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h\+\_\+bl,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{Sref,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{Ent\+\_\+bl,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{E\+\_\+kb,  }\item[{integer, intent(in)}]{is,  }\item[{integer, intent(in)}]{ie,  }\item[{integer, intent(in)}]{kmb,  }\item[{type(\mbox{\hyperlink{structmom__grid_1_1ocean__grid__type}{ocean\+\_\+grid\+\_\+type}}), intent(in)}]{G,  }\item[{type(\mbox{\hyperlink{structmom__verticalgrid_1_1verticalgrid__type}{verticalgrid\+\_\+type}}), intent(in)}]{GV,  }\item[{logical, intent(in)}]{limit,  }\item[{real, dimension(szi\+\_\+(g)), intent(inout)}]{d\+Skb,  }\item[{real, dimension(szi\+\_\+(g)), intent(inout), optional}]{dd\+Skb\+\_\+dE,  }\item[{real, dimension(szi\+\_\+(g)), intent(inout), optional}]{d\+Slay,  }\item[{real, dimension(szi\+\_\+(g)), intent(inout), optional}]{dd\+Slay\+\_\+dE,  }\item[{real, dimension(szi\+\_\+(g)), intent(inout), optional}]{d\+S\+\_\+anom\+\_\+lim,  }\item[{logical, dimension(szi\+\_\+(g)), intent(in), optional}]{do\+\_\+i\+\_\+in }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine determines the reference density difference between the bottommost buffer layer and the first interior after the mixing between mixed and buffer layers and mixing with the layer below. Within the mixed and buffer layers, entrainment from the layer above is increased when it is necessary to keep the layers from developing a negative thickness; otherwise it equals Ent\+\_\+bl. At each interface, the upward and downward fluxes average out to Ent\+\_\+bl, unless entrainment by the layer below is larger than twice Ent\+\_\+bl. The density difference across the first interior layer may also be returned. It could also be limited to avoid negative values or values that greatly exceed the density differences across an interface. Additionally, the partial derivatives of d\+Skb and d\+Slay with E\+\_\+kb could also be returned. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em h\+\_\+bl} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em sref} & Reference potential density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em ent\+\_\+bl} & The average entrainment upward and downward across each interface around the buffer layers \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em e\+\_\+kb} & The entrainment by the top interior layer \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em is} & The start of the i-\/index range to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em ie} & The end of the i-\/index range to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em kmb} & The number of mixed and buffer layers. \\
\hline
\mbox{\texttt{ in}}  & {\em limit} & If true, limit d\+Skb and d\+Slay to avoid negative values. \\
\hline
\mbox{\texttt{ in,out}}  & {\em dskb} & The limited potential density difference across the interface between the bottommost buffer layer and the topmost interior layer. \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]} d\+Skb $>$ 0. \\
\hline
\mbox{\texttt{ in,out}}  & {\em ddskb\+\_\+de} & The partial derivative of d\+Skb with E \mbox{[}R H-\/1 $\sim$$>$ kg m-\/4 or m-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em dslay} & The limited potential density difference across the topmost interior layer. 0 $<$ d\+Skb \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em ddslay\+\_\+de} & The partial derivative of d\+Slay with E \mbox{[}R H-\/1 $\sim$$>$ kg m-\/4 or m-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em ds\+\_\+anom\+\_\+lim} & A limiting value to use for the density anomalies below the buffer layer \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em do\+\_\+i\+\_\+in} & If present, determines which columns are worked on. \\
\hline
\end{DoxyParams}


Definition at line 1199 of file M\+O\+M\+\_\+entrain\+\_\+diffusive.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1201   \textcolor{keywordtype}{type}(ocean\_grid\_type),              \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{      !< The ocean's grid structure.}}
\DoxyCodeLine{1202   \textcolor{keywordtype}{type}(verticalGrid\_type),            \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{     !< The ocean's vertical grid}}
\DoxyCodeLine{1203 \textcolor{comment}{                                                              !! structure.}}
\DoxyCodeLine{1204 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))},   \textcolor{keywordtype}{intent(in)}    :: h\_bl\textcolor{comment}{   !< Layer thickness [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{1205 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))},   \textcolor{keywordtype}{intent(in)}    :: Sref\textcolor{comment}{   !< Reference potential density [R \string~> kg m-\/3]}}
\DoxyCodeLine{1206 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))},   \textcolor{keywordtype}{intent(in)}    :: Ent\_bl\textcolor{comment}{ !< The average entrainment upward and}}
\DoxyCodeLine{1207 \textcolor{comment}{                                                              !! downward across each interface}}
\DoxyCodeLine{1208 \textcolor{comment}{                                                              !! around the buffer layers [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1209 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(in)}    :: E\_kb\textcolor{comment}{   !< The entrainment by the top interior}}
\DoxyCodeLine{1210 \textcolor{comment}{                                                              !! layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1211   \textcolor{keywordtype}{integer},                            \textcolor{keywordtype}{intent(in)}    :: is\textcolor{comment}{     !< The start of the i-\/index range to work on.}}
\DoxyCodeLine{1212   \textcolor{keywordtype}{integer},                            \textcolor{keywordtype}{intent(in)}    :: ie\textcolor{comment}{     !< The end of the i-\/index range to work on.}}
\DoxyCodeLine{1213   \textcolor{keywordtype}{integer},                            \textcolor{keywordtype}{intent(in)}    :: kmb\textcolor{comment}{    !< The number of mixed and buffer layers.}}
\DoxyCodeLine{1214   \textcolor{keywordtype}{logical},                            \textcolor{keywordtype}{intent(in)}    :: limit\textcolor{comment}{  !< If true, limit dSkb and dSlay to}}
\DoxyCodeLine{1215 \textcolor{comment}{                                                              !! avoid negative values.}}
\DoxyCodeLine{1216 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},           \textcolor{keywordtype}{intent(inout)} :: dSkb\textcolor{comment}{   !< The limited potential density}}
\DoxyCodeLine{1217 \textcolor{comment}{                                                              !! difference across the interface}}
\DoxyCodeLine{1218 \textcolor{comment}{                                                              !! between the bottommost buffer layer}}
\DoxyCodeLine{1219 \textcolor{comment}{                                                              !! and the topmost interior layer. [R \string~> kg m-\/3]}}
\DoxyCodeLine{1220 \textcolor{comment}{                                                              !! dSkb > 0.}}
\DoxyCodeLine{1221 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: ddSkb\_dE\textcolor{comment}{ !< The partial derivative of dSkb}}
\DoxyCodeLine{1222 \textcolor{comment}{                                                              !! with E [R H-\/1 \string~> kg m-\/4 or m-\/1].}}
\DoxyCodeLine{1223 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: dSlay\textcolor{comment}{  !< The limited potential density}}
\DoxyCodeLine{1224 \textcolor{comment}{                                                              !! difference across the topmost}}
\DoxyCodeLine{1225 \textcolor{comment}{                                                              !! interior layer. 0 < dSkb [R \string~> kg m-\/3]}}
\DoxyCodeLine{1226 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: ddSlay\_dE\textcolor{comment}{ !< The partial derivative of dSlay}}
\DoxyCodeLine{1227 \textcolor{comment}{                                                              !! with E [R H-\/1 \string~> kg m-\/4 or m-\/1].}}
\DoxyCodeLine{1228 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: dS\_anom\_lim\textcolor{comment}{ !< A limiting value to use for}}
\DoxyCodeLine{1229 \textcolor{comment}{                                                              !! the density anomalies below the}}
\DoxyCodeLine{1230 \textcolor{comment}{                                                              !! buffer layer [R \string~> kg m-\/3].}}
\DoxyCodeLine{1231   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)} :: do\_i\_in\textcolor{comment}{ !< If present, determines which}}
\DoxyCodeLine{1232 \textcolor{comment}{                                                              !! columns are worked on.}}
\DoxyCodeLine{1233 }
\DoxyCodeLine{1234 \textcolor{comment}{! Note that dSkb, ddSkb\_dE, dSlay, ddSlay\_dE, and dS\_anom\_lim are declared}}
\DoxyCodeLine{1235 \textcolor{comment}{! intent inout  because they should not change where do\_i\_in is false.}}
\DoxyCodeLine{1236 }
\DoxyCodeLine{1237 \textcolor{comment}{!   This subroutine determines the reference density difference between the}}
\DoxyCodeLine{1238 \textcolor{comment}{! bottommost buffer layer and the first interior after the mixing between mixed}}
\DoxyCodeLine{1239 \textcolor{comment}{! and buffer layers and mixing with the layer below. Within the mixed and buffer}}
\DoxyCodeLine{1240 \textcolor{comment}{! layers, entrainment from the layer above is increased when it is necessary to}}
\DoxyCodeLine{1241 \textcolor{comment}{! keep the layers from developing a negative thickness; otherwise it equals}}
\DoxyCodeLine{1242 \textcolor{comment}{! Ent\_bl.  At each interface, the upward and downward fluxes average out to}}
\DoxyCodeLine{1243 \textcolor{comment}{! Ent\_bl, unless entrainment by the layer below is larger than twice Ent\_bl.}}
\DoxyCodeLine{1244 \textcolor{comment}{!   The density difference across the first interior layer may also be returned.}}
\DoxyCodeLine{1245 \textcolor{comment}{! It could also be limited to avoid negative values or values that greatly}}
\DoxyCodeLine{1246 \textcolor{comment}{! exceed the density differences across an interface.}}
\DoxyCodeLine{1247 \textcolor{comment}{!   Additionally, the partial derivatives of dSkb and dSlay with E\_kb could}}
\DoxyCodeLine{1248 \textcolor{comment}{! also be returned.}}
\DoxyCodeLine{1249 }
\DoxyCodeLine{1250   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1251 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))} :: \&}
\DoxyCodeLine{1252     b1, c1, \&       \textcolor{comment}{! b1 and c1 are variables used by the tridiagonal solver.}}
\DoxyCodeLine{1253     S, dS\_dE, \&     \textcolor{comment}{! The coordinate density [R \string~> kg m-\/3] and its derivative with E.}}
\DoxyCodeLine{1254     ea, dea\_dE, \&   \textcolor{comment}{! The entrainment from above and its derivative with E.}}
\DoxyCodeLine{1255     eb, deb\_dE      \textcolor{comment}{! The entrainment from below and its derivative with E.}}
\DoxyCodeLine{1256 \textcolor{keywordtype}{  real} :: deriv\_dSkb(SZI\_(G))}
\DoxyCodeLine{1257 \textcolor{keywordtype}{  real} :: d1(SZI\_(G))  \textcolor{comment}{! d1 = 1.0-\/c1 is also used by the tridiagonal solver.}}
\DoxyCodeLine{1258 \textcolor{keywordtype}{  real} :: src       \textcolor{comment}{! A source term for dS\_dR.}}
\DoxyCodeLine{1259 \textcolor{keywordtype}{  real} :: h1        \textcolor{comment}{! The thickness in excess of the minimum that will remain}}
\DoxyCodeLine{1260                     \textcolor{comment}{! after exchange with the layer below [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1261   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: do\_i}
\DoxyCodeLine{1262 \textcolor{keywordtype}{  real} :: h\_neglect \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{1263                     \textcolor{comment}{! in roundoff and can be neglected [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1264 \textcolor{keywordtype}{  real} :: h\_tr      \textcolor{comment}{! h\_tr is h at tracer points with a tiny thickness}}
\DoxyCodeLine{1265                     \textcolor{comment}{! added to ensure positive definiteness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1266 \textcolor{keywordtype}{  real} :: b\_denom\_1 \textcolor{comment}{! The first term in the denominator of b1 [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1267 \textcolor{keywordtype}{  real} :: rat}
\DoxyCodeLine{1268 \textcolor{keywordtype}{  real} :: dS\_kbp1, IdS\_kbp1}
\DoxyCodeLine{1269 \textcolor{keywordtype}{  real} :: deriv\_dSLay}
\DoxyCodeLine{1270 \textcolor{keywordtype}{  real} :: Inv\_term     \textcolor{comment}{! [nondim]}}
\DoxyCodeLine{1271 \textcolor{keywordtype}{  real} :: f1, df1\_drat \textcolor{comment}{! Temporary variables [nondim].}}
\DoxyCodeLine{1272 \textcolor{keywordtype}{  real} :: z, dz\_drat, f2, df2\_dz, expz \textcolor{comment}{! Temporary variables [nondim].}}
\DoxyCodeLine{1273 \textcolor{keywordtype}{  real} :: eps\_dSLay, eps\_dSkb \textcolor{comment}{! Small nondimensional constants.}}
\DoxyCodeLine{1274   \textcolor{keywordtype}{integer} :: i, k}
\DoxyCodeLine{1275 }
\DoxyCodeLine{1276   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ddslay\_de) .and. .not.\textcolor{keyword}{present}(dslay)) \textcolor{keyword}{call }mom\_error(fatal, \&}
\DoxyCodeLine{1277       \textcolor{stringliteral}{"{}In deterimine\_dSkb, ddSLay\_dE may only be present if dSlay is."{}})}
\DoxyCodeLine{1278 }
\DoxyCodeLine{1279   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{1280 }
\DoxyCodeLine{1281   \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1282     ea(i,kmb+1) = e\_kb(i) ; dea\_de(i,kmb+1) = 1.0}
\DoxyCodeLine{1283     s(i,kmb+1) = sref(i,kmb+1) ; ds\_de(i,kmb+1) = 0.0}
\DoxyCodeLine{1284     b1(i,kmb+1) = 0.0}
\DoxyCodeLine{1285     d1(i) = 1.0}
\DoxyCodeLine{1286     do\_i(i) = .true.}
\DoxyCodeLine{1287 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1288   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(do\_i\_in)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1289     \textcolor{keywordflow}{do} i=is,ie ; do\_i(i) = do\_i\_in(i) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1290 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1291   \textcolor{keywordflow}{do} k=kmb,1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1292     \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1293       \textcolor{comment}{! The do\_i test here is only for efficiency.}}
\DoxyCodeLine{1294     \textcolor{comment}{! Determine the entrainment from below for each buffer layer.}}
\DoxyCodeLine{1295       \textcolor{keywordflow}{if} (2.0*ent\_bl(i,k+1) > ea(i,k+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1296         eb(i,k) = 2.0*ent\_bl(i,k+1) -\/ ea(i,k+1) ; deb\_de(i,k) = -\/dea\_de(i,k+1)}
\DoxyCodeLine{1297       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1298         eb(i,k) = 0.0 ; deb\_de(i,k) = 0.0}
\DoxyCodeLine{1299 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1300 }
\DoxyCodeLine{1301       \textcolor{comment}{! Determine the entrainment from above for each buffer layer.}}
\DoxyCodeLine{1302       h1 = (h\_bl(i,k) -\/ gv\%Angstrom\_H) + (eb(i,k) -\/ ea(i,k+1))}
\DoxyCodeLine{1303       \textcolor{keywordflow}{if} (h1 >= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1304         ea(i,k) = ent\_bl(i,k) ; dea\_de(i,k) = 0.0}
\DoxyCodeLine{1305       \textcolor{keywordflow}{elseif} (ent\_bl(i,k) + 0.5*h1 >= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1306         ea(i,k) = ent\_bl(i,k) -\/ 0.5*h1}
\DoxyCodeLine{1307         dea\_de(i,k) = 0.5*(dea\_de(i,k+1) -\/ deb\_de(i,k))}
\DoxyCodeLine{1308       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1309         ea(i,k) = -\/h1}
\DoxyCodeLine{1310         dea\_de(i,k) = dea\_de(i,k+1) -\/ deb\_de(i,k)}
\DoxyCodeLine{1311 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1312     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1313       ea(i,k) = 0.0 ; dea\_de(i,k) = 0.0 ; eb(i,k) = 0.0 ; deb\_de(i,k) = 0.0}
\DoxyCodeLine{1314 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1315 }
\DoxyCodeLine{1316     \textcolor{comment}{! This is the first-\/pass of a tridiagonal solver for S.}}
\DoxyCodeLine{1317     h\_tr = h\_bl(i,k) + h\_neglect}
\DoxyCodeLine{1318     c1(i,k) = ea(i,k+1) * b1(i,k+1)}
\DoxyCodeLine{1319     b\_denom\_1 = (h\_tr + d1(i)*eb(i,k))}
\DoxyCodeLine{1320     b1(i,k) = 1.0 / (b\_denom\_1 + ea(i,k))}
\DoxyCodeLine{1321     d1(i) = b\_denom\_1 * b1(i,k)}
\DoxyCodeLine{1322 }
\DoxyCodeLine{1323     s(i,k) = (h\_tr*sref(i,k) + eb(i,k)*s(i,k+1)) * b1(i,k)}
\DoxyCodeLine{1324 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1325   \textcolor{keywordflow}{do} k=2,kmb ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1326     s(i,k) = s(i,k) + c1(i,k-\/1)*s(i,k-\/1)}
\DoxyCodeLine{1327 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1328 }
\DoxyCodeLine{1329   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ddskb\_de) .or. \textcolor{keyword}{present}(ddslay\_de)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1330     \textcolor{comment}{! These two tridiagonal solvers cannot be combined because the solutions for}}
\DoxyCodeLine{1331     \textcolor{comment}{! S are required as a source for dS\_dE.}}
\DoxyCodeLine{1332     \textcolor{keywordflow}{do} k=kmb,2,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1333       \textcolor{keywordflow}{if} (do\_i(i) .and. (dea\_de(i,k) -\/ deb\_de(i,k) > 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1334         src = (((s(i,k+1) -\/ sref(i,k)) * (h\_bl(i,k) + h\_neglect) + \&}
\DoxyCodeLine{1335                 (s(i,k+1) -\/ s(i,k-\/1)) * ea(i,k)) * deb\_de(i,k) -\/ \&}
\DoxyCodeLine{1336                ((sref(i,k) -\/ s(i,k-\/1)) * h\_bl(i,k) + \&}
\DoxyCodeLine{1337                 (s(i,k+1) -\/ s(i,k-\/1)) * eb(i,k)) * dea\_de(i,k)) / \&}
\DoxyCodeLine{1338               ((h\_bl(i,k) + h\_neglect + ea(i,k)) + eb(i,k))}
\DoxyCodeLine{1339       \textcolor{keywordflow}{else} ; src = 0.0 ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1340       ds\_de(i,k) = (src + eb(i,k)*ds\_de(i,k+1)) * b1(i,k)}
\DoxyCodeLine{1341 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1342     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1343       \textcolor{keywordflow}{if} (do\_i(i) .and. (deb\_de(i,1) < 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1344         src = (((s(i,2) -\/ sref(i,1)) * (h\_bl(i,1) + h\_neglect)) * deb\_de(i,1)) / \&}
\DoxyCodeLine{1345               (h\_bl(i,1) + h\_neglect + eb(i,1))}
\DoxyCodeLine{1346       \textcolor{keywordflow}{else} ; src = 0.0 ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1347       ds\_de(i,1) = (src + eb(i,1)*ds\_de(i,2)) * b1(i,1)}
\DoxyCodeLine{1348 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1349     \textcolor{keywordflow}{do} k=2,kmb ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1350       ds\_de(i,k) = ds\_de(i,k) + c1(i,k-\/1)*ds\_de(i,k-\/1)}
\DoxyCodeLine{1351 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1352 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1353 }
\DoxyCodeLine{1354   \textcolor{comment}{! Now, apply any limiting and return the requested variables.}}
\DoxyCodeLine{1355 }
\DoxyCodeLine{1356   eps\_dskb = 1.0e-\/6   \textcolor{comment}{! Should be a small, nondimensional, positive number.}}
\DoxyCodeLine{1357   \textcolor{keywordflow}{if} (.not.limit) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1358     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1359       dskb(i) = sref(i,kmb+1) -\/ s(i,kmb)}
\DoxyCodeLine{1360 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1361     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ddskb\_de)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1362       ddskb\_de(i) = -\/1.0*ds\_de(i,kmb)}
\DoxyCodeLine{1363 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1364 }
\DoxyCodeLine{1365     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dslay)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1366       dslay(i) = 0.5 * (sref(i,kmb+2) -\/ s(i,kmb))}
\DoxyCodeLine{1367 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1368     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ddslay\_de)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1369       ddslay\_de(i) = -\/0.5*ds\_de(i,kmb)}
\DoxyCodeLine{1370 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1371   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1372     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1373       \textcolor{comment}{! Need to ensure that 0 < dSkb <= S\_kb -\/ Sbl}}
\DoxyCodeLine{1374       \textcolor{keywordflow}{if} (sref(i,kmb+1) -\/ s(i,kmb) < eps\_dskb*(sref(i,kmb+2) -\/ sref(i,kmb+1))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1375         dskb(i) = eps\_dskb * (sref(i,kmb+2) -\/ sref(i,kmb+1)) ; deriv\_dskb(i) = 0.0}
\DoxyCodeLine{1376       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1377         dskb(i) = sref(i,kmb+1) -\/ s(i,kmb) ; deriv\_dskb(i) = -\/1.0}
\DoxyCodeLine{1378 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1379       \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ddskb\_de)) ddskb\_de(i) = deriv\_dskb(i)*ds\_de(i,kmb)}
\DoxyCodeLine{1380 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1381 }
\DoxyCodeLine{1382     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dslay)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1383       dz\_drat = 1000.0    \textcolor{comment}{! The limit of large dz\_drat the same as choosing a}}
\DoxyCodeLine{1384                           \textcolor{comment}{! Heaviside function.}}
\DoxyCodeLine{1385       eps\_dslay = 1.0e-\/10 \textcolor{comment}{! Should be \string~= GV\%Angstrom\_H / sqrt(Kd*dt)}}
\DoxyCodeLine{1386       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1387         ds\_kbp1 = sref(i,kmb+2) -\/ sref(i,kmb+1)}
\DoxyCodeLine{1388         ids\_kbp1 = 1.0 / (sref(i,kmb+2) -\/ sref(i,kmb+1))}
\DoxyCodeLine{1389         rat = (sref(i,kmb+1) -\/ s(i,kmb)) * ids\_kbp1}
\DoxyCodeLine{1390         \textcolor{comment}{! Need to ensure that 0 < dSLay <= 2*dSkb}}
\DoxyCodeLine{1391         \textcolor{keywordflow}{if} (rat < 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1392           \textcolor{comment}{! The coefficients here are chosen so that at rat = 0.5, the value (1.5)}}
\DoxyCodeLine{1393           \textcolor{comment}{! and first derivative (-\/0.5) match with the "{}typical"{} case (next).}}
\DoxyCodeLine{1394           \textcolor{comment}{! The functional form here is arbitrary.}}
\DoxyCodeLine{1395           \textcolor{comment}{!   f1 provides a reasonable profile that matches the value and derivative}}
\DoxyCodeLine{1396           \textcolor{comment}{! of the "{}typical"{} case at rat = 0.5, and has a maximum of less than 2.}}
\DoxyCodeLine{1397           inv\_term = 1.0 / (1.0-\/rat)}
\DoxyCodeLine{1398           f1 = 2.0 -\/ 0.125*(inv\_term**2)}
\DoxyCodeLine{1399           df1\_drat = -\/ 0.25*(inv\_term**3)}
\DoxyCodeLine{1400 }
\DoxyCodeLine{1401           \textcolor{comment}{!   f2 ensures that dSLay goes to 0 rapidly if rat is significantly}}
\DoxyCodeLine{1402           \textcolor{comment}{! negative.}}
\DoxyCodeLine{1403           z = dz\_drat * rat + 4.0 \textcolor{comment}{! The 4 here gives f2(0) = 0.982.}}
\DoxyCodeLine{1404           \textcolor{keywordflow}{if} (z >= 18.0) \textcolor{keywordflow}{then} ; f2 = 1.0 ; df2\_dz = 0.0}
\DoxyCodeLine{1405           \textcolor{keywordflow}{elseif} (z <= -\/58.0) \textcolor{keywordflow}{then} ; f2 = eps\_dslay ; df2\_dz = 0.0}
\DoxyCodeLine{1406           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1407             expz = exp(z) ; inv\_term = 1.0 / (1.0 + expz)}
\DoxyCodeLine{1408             f2 = (eps\_dslay + expz) * inv\_term}
\DoxyCodeLine{1409             df2\_dz = (1.0 -\/ eps\_dslay) * expz * inv\_term**2}
\DoxyCodeLine{1410 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1411 }
\DoxyCodeLine{1412           dslay(i) = dskb(i) * f1 * f2}
\DoxyCodeLine{1413           deriv\_dslay = deriv\_dskb(i) * (f1 * f2) -\/ (dskb(i)*ids\_kbp1) * \&}
\DoxyCodeLine{1414                             (df1\_drat*f2 + f1 * dz\_drat * df2\_dz)}
\DoxyCodeLine{1415         \textcolor{keywordflow}{elseif} (dskb(i) <= 3.0*ds\_kbp1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1416           \textcolor{comment}{! This is the "{}typical"{} case.}}
\DoxyCodeLine{1417           dslay(i) = 0.5 * (dskb(i) + ds\_kbp1)}
\DoxyCodeLine{1418           deriv\_dslay = 0.5 * deriv\_dskb(i) \textcolor{comment}{! = -\/0.5}}
\DoxyCodeLine{1419         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1420           dslay(i) = 2.0*ds\_kbp1}
\DoxyCodeLine{1421           deriv\_dslay = 0.0}
\DoxyCodeLine{1422 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1423         \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ddslay\_de)) ddslay\_de(i) = deriv\_dslay*ds\_de(i,kmb)}
\DoxyCodeLine{1424 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1425 \textcolor{keywordflow}{    endif} \textcolor{comment}{! present(dSlay)}}
\DoxyCodeLine{1426 \textcolor{keywordflow}{  endif} \textcolor{comment}{! Not limited.}}
\DoxyCodeLine{1427 }
\DoxyCodeLine{1428   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ds\_anom\_lim)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1429     ds\_anom\_lim(i) = max(0.0, eps\_dskb * (sref(i,kmb+2) -\/ sref(i,kmb+1)) -\/ \&}
\DoxyCodeLine{1430                               (sref(i,kmb+1) -\/ s(i,kmb)) )}
\DoxyCodeLine{1431 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1432 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__entrain__diffusive_a541b3aadc418110f7359b0ae401e4e78}\label{namespacemom__entrain__diffusive_a541b3aadc418110f7359b0ae401e4e78}} 
\index{mom\_entrain\_diffusive@{mom\_entrain\_diffusive}!determine\_ea\_kb@{determine\_ea\_kb}}
\index{determine\_ea\_kb@{determine\_ea\_kb}!mom\_entrain\_diffusive@{mom\_entrain\_diffusive}}
\doxysubsubsection{\texorpdfstring{determine\_ea\_kb()}{determine\_ea\_kb()}}
{\footnotesize\ttfamily subroutine mom\+\_\+entrain\+\_\+diffusive\+::determine\+\_\+ea\+\_\+kb (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h\+\_\+bl,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{dt\+Kd\+\_\+kb,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{Sref,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{I\+\_\+d\+Skbp1,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{Ent\+\_\+bl,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{ea\+\_\+kbp1,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{min\+\_\+eakb,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{max\+\_\+eakb,  }\item[{integer, intent(in)}]{kmb,  }\item[{integer, intent(in)}]{is,  }\item[{integer, intent(in)}]{ie,  }\item[{logical, dimension(szi\+\_\+(g)), intent(in)}]{do\+\_\+i,  }\item[{type(\mbox{\hyperlink{structmom__grid_1_1ocean__grid__type}{ocean\+\_\+grid\+\_\+type}}), intent(in)}]{G,  }\item[{type(\mbox{\hyperlink{structmom__verticalgrid_1_1verticalgrid__type}{verticalgrid\+\_\+type}}), intent(in)}]{GV,  }\item[{type(\mbox{\hyperlink{structmom__entrain__diffusive_1_1entrain__diffusive__cs}{entrain\+\_\+diffusive\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(inout)}]{Ent,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out), optional}]{error,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in), optional}]{err\+\_\+min\+\_\+eakb0,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in), optional}]{err\+\_\+max\+\_\+eakb0,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out), optional}]{F\+\_\+kb,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out), optional}]{d\+Fdfm\+\_\+kb }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine determines the entrainment from above by the top interior layer (labeled kb elsewhere) given an entrainment by the layer below it, constrained to be within the provided bounds. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em h\+\_\+bl} & Layer thickness, with the top interior layer at k-\/index kmb+1 \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em sref} & The coordinate reference potential density, with the value of the topmost interior layer at layer kmb+1 \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ent\+\_\+bl} & The average entrainment upward and downward across each interface around the buffer layers \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em i\+\_\+dskbp1} & The inverse of the difference in reference potential density across the base of the uppermost interior layer \mbox{[}R-\/1 $\sim$$>$ m3 kg-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dtkd\+\_\+kb} & The diapycnal diffusivity in the top interior layer times the time step \mbox{[}H2 $\sim$$>$ m2 or kg2 m-\/4\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ea\+\_\+kbp1} & The entrainment from above by layer kb+1 \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em min\+\_\+eakb} & The minimum permissible rate of entrainment \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em max\+\_\+eakb} & The maximum permissible rate of entrainment \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em kmb} & The number of mixed and buffer layers. \\
\hline
\mbox{\texttt{ in}}  & {\em is} & The start of the i-\/index range to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em ie} & The end of the i-\/index range to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em do\+\_\+i} & A logical variable indicating which i-\/points to work on. \\
\hline
 & {\em cs} & This module\textquotesingle{}s control structure. \\
\hline
\mbox{\texttt{ in,out}}  & {\em ent} & The entrainment rate of the uppermost interior layer \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. The input value is the first guess. \\
\hline
\mbox{\texttt{ out}}  & {\em error} & The error (locally defined in this routine) associated with the returned solution. \\
\hline
\mbox{\texttt{ in}}  & {\em err\+\_\+min\+\_\+eakb0} & The errors (locally defined) associated with min\+\_\+eakb when ea\+\_\+kbp1 = 0, returned from a previous call to this fn. \\
\hline
\mbox{\texttt{ in}}  & {\em err\+\_\+max\+\_\+eakb0} & The errors (locally defined) associated with min\+\_\+eakb when ea\+\_\+kbp1 = 0, returned from a previous call to this fn. \\
\hline
\mbox{\texttt{ out}}  & {\em f\+\_\+kb} & The entrainment from below by the uppermost interior layer corresponding to the returned value of Ent \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em dfdfm\+\_\+kb} & The partial derivative of F\+\_\+kb with ea\+\_\+kbp1 \mbox{[}nondim\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 1571 of file M\+O\+M\+\_\+entrain\+\_\+diffusive.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1574   \textcolor{keywordtype}{type}(ocean\_grid\_type),            \textcolor{keywordtype}{intent(in)}  :: G\textcolor{comment}{        !< The ocean's grid structure.}}
\DoxyCodeLine{1575   \textcolor{keywordtype}{type}(verticalGrid\_type),          \textcolor{keywordtype}{intent(in)}  :: GV\textcolor{comment}{       !< The ocean's vertical grid structure.}}
\DoxyCodeLine{1576 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}  :: h\_bl\textcolor{comment}{     !< Layer thickness, with the top interior}}
\DoxyCodeLine{1577 \textcolor{comment}{                                                            !! layer at k-\/index kmb+1 [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1578 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}  :: Sref\textcolor{comment}{     !< The coordinate reference potential}}
\DoxyCodeLine{1579 \textcolor{comment}{                                                            !! density, with the value of the}}
\DoxyCodeLine{1580 \textcolor{comment}{                                                            !! topmost interior layer at layer}}
\DoxyCodeLine{1581 \textcolor{comment}{                                                            !! kmb+1 [R \string~> kg m-\/3].}}
\DoxyCodeLine{1582 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}  :: Ent\_bl\textcolor{comment}{   !< The average entrainment upward and}}
\DoxyCodeLine{1583 \textcolor{comment}{                                                            !! downward across each interface around}}
\DoxyCodeLine{1584 \textcolor{comment}{                                                            !! the buffer layers [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1585 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},         \textcolor{keywordtype}{intent(in)}  :: I\_dSkbp1\textcolor{comment}{ !< The inverse of the difference in}}
\DoxyCodeLine{1586 \textcolor{comment}{                                                            !! reference potential density across}}
\DoxyCodeLine{1587 \textcolor{comment}{                                                            !! the base of the uppermost interior}}
\DoxyCodeLine{1588 \textcolor{comment}{                                                            !! layer [R-\/1 \string~> m3 kg-\/1].}}
\DoxyCodeLine{1589 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},         \textcolor{keywordtype}{intent(in)}  :: dtKd\_kb\textcolor{comment}{  !< The diapycnal diffusivity in the top}}
\DoxyCodeLine{1590 \textcolor{comment}{                                                            !! interior layer times the time step}}
\DoxyCodeLine{1591 \textcolor{comment}{                                                            !! [H2 \string~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{1592 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},         \textcolor{keywordtype}{intent(in)}  :: ea\_kbp1\textcolor{comment}{  !< The entrainment from above by layer}}
\DoxyCodeLine{1593 \textcolor{comment}{                                                            !! kb+1 [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1594 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},         \textcolor{keywordtype}{intent(in)}  :: min\_eakb\textcolor{comment}{ !< The minimum permissible rate of}}
\DoxyCodeLine{1595 \textcolor{comment}{                                                            !! entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1596 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},         \textcolor{keywordtype}{intent(in)}  :: max\_eakb\textcolor{comment}{ !< The maximum permissible rate of}}
\DoxyCodeLine{1597 \textcolor{comment}{                                                            !! entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1598   \textcolor{keywordtype}{integer},                          \textcolor{keywordtype}{intent(in)}  :: kmb\textcolor{comment}{      !< The number of mixed and buffer layers.}}
\DoxyCodeLine{1599   \textcolor{keywordtype}{integer},                          \textcolor{keywordtype}{intent(in)}  :: is\textcolor{comment}{       !< The start of the i-\/index range to work on.}}
\DoxyCodeLine{1600   \textcolor{keywordtype}{integer},                          \textcolor{keywordtype}{intent(in)}  :: ie\textcolor{comment}{       !< The end of the i-\/index range to work on.}}
\DoxyCodeLine{1601   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(SZI\_(G))},      \textcolor{keywordtype}{intent(in)}  :: do\_i\textcolor{comment}{     !< A logical variable indicating which}}
\DoxyCodeLine{1602 \textcolor{comment}{                                                            !! i-\/points to work on.}}
\DoxyCodeLine{1603   \textcolor{keywordtype}{type}(entrain\_diffusive\_CS),       \textcolor{keywordtype}{pointer}     :: CS\textcolor{comment}{       !< This module's control structure.}}
\DoxyCodeLine{1604 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},         \textcolor{keywordtype}{intent(inout)} :: Ent\textcolor{comment}{    !< The entrainment rate of the uppermost}}
\DoxyCodeLine{1605 \textcolor{comment}{                                                            !! interior layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1606 \textcolor{comment}{                                                            !! The input value is the first guess.}}
\DoxyCodeLine{1607 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: error\textcolor{comment}{  !< The error (locally defined in this}}
\DoxyCodeLine{1608 \textcolor{comment}{                                                            !! routine) associated with the returned}}
\DoxyCodeLine{1609 \textcolor{comment}{                                                            !! solution.}}
\DoxyCodeLine{1610 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: err\_min\_eakb0\textcolor{comment}{ !< The errors (locally defined)}}
\DoxyCodeLine{1611 \textcolor{comment}{                                                            !! associated with min\_eakb when ea\_kbp1 = 0,}}
\DoxyCodeLine{1612 \textcolor{comment}{                                                            !! returned from a previous call to this fn.}}
\DoxyCodeLine{1613 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: err\_max\_eakb0\textcolor{comment}{ !< The errors (locally defined)}}
\DoxyCodeLine{1614 \textcolor{comment}{                                                            !! associated with min\_eakb when ea\_kbp1 = 0,}}
\DoxyCodeLine{1615 \textcolor{comment}{                                                            !! returned from a previous call to this fn.}}
\DoxyCodeLine{1616 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: F\_kb\textcolor{comment}{   !< The entrainment from below by the}}
\DoxyCodeLine{1617 \textcolor{comment}{                                                            !! uppermost interior layer}}
\DoxyCodeLine{1618 \textcolor{comment}{                                                            !! corresponding to the returned}}
\DoxyCodeLine{1619 \textcolor{comment}{                                                            !! value of Ent [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1620 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: dFdfm\_kb\textcolor{comment}{ !< The partial derivative of F\_kb with}}
\DoxyCodeLine{1621 \textcolor{comment}{                                                            !! ea\_kbp1 [nondim].}}
\DoxyCodeLine{1622 }
\DoxyCodeLine{1623 \textcolor{comment}{!  This subroutine determines the entrainment from above by the top interior}}
\DoxyCodeLine{1624 \textcolor{comment}{! layer (labeled kb elsewhere) given an entrainment by the layer below it,}}
\DoxyCodeLine{1625 \textcolor{comment}{! constrained to be within the provided bounds.}}
\DoxyCodeLine{1626 }
\DoxyCodeLine{1627   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1628 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: \&}
\DoxyCodeLine{1629     dS\_kb, \&                \textcolor{comment}{!   The coordinate-\/density difference between the}}
\DoxyCodeLine{1630                             \textcolor{comment}{! layer kb and deepest buffer layer, limited to}}
\DoxyCodeLine{1631                             \textcolor{comment}{! ensure that it is positive [R \string~> kg m-\/3].}}
\DoxyCodeLine{1632     ds\_lay, \&               \textcolor{comment}{!   The coordinate-\/density difference across layer}}
\DoxyCodeLine{1633                             \textcolor{comment}{! kb, limited to ensure that it is positive and not}}
\DoxyCodeLine{1634                             \textcolor{comment}{! too much bigger than dS\_kb or dS\_kbp1 [R \string~> kg m-\/3].}}
\DoxyCodeLine{1635     ddskb\_de, ddslay\_de, \&  \textcolor{comment}{! The derivatives of dS\_kb and dS\_Lay with E}}
\DoxyCodeLine{1636                             \textcolor{comment}{! [R H-\/1 \string~> kg m-\/4 or m-\/1].}}
\DoxyCodeLine{1637     derror\_de, \&            \textcolor{comment}{! The derivative of err with E [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1638     err, \&                  \textcolor{comment}{! The "{}error"{} whose zero is being sought [H2 \string~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{1639     e\_min, e\_max, \&         \textcolor{comment}{! The minimum and maximum values of E [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1640     error\_mine, error\_maxe  \textcolor{comment}{! err when E = E\_min or E = E\_max [H2 \string~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{1641 \textcolor{keywordtype}{  real} :: err\_est           \textcolor{comment}{! An estimate of what err will be [H2 \string~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{1642 \textcolor{keywordtype}{  real} :: eL                \textcolor{comment}{! 1 or 0, depending on whether increases in E lead}}
\DoxyCodeLine{1643                             \textcolor{comment}{! to decreases in the entrainment from below by the}}
\DoxyCodeLine{1644                             \textcolor{comment}{! deepest buffer layer [nondim].}}
\DoxyCodeLine{1645 \textcolor{keywordtype}{  real} :: fa                \textcolor{comment}{! Temporary variable used to calculate err [nondim].}}
\DoxyCodeLine{1646 \textcolor{keywordtype}{  real} :: fk                \textcolor{comment}{! Temporary variable used to calculate err [H2 \string~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{1647 \textcolor{keywordtype}{  real} :: fm, fr            \textcolor{comment}{! Temporary variables used to calculate err [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1648 \textcolor{keywordtype}{  real} :: tolerance         \textcolor{comment}{! The tolerance within which E must be converged [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1649 \textcolor{keywordtype}{  real} :: E\_prev            \textcolor{comment}{! The previous value of E [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1650   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: false\_position \textcolor{comment}{! If true, the false position}}
\DoxyCodeLine{1651                             \textcolor{comment}{! method might be used for the next iteration.}}
\DoxyCodeLine{1652   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: redo\_i \textcolor{comment}{! If true, more work is needed on this column.}}
\DoxyCodeLine{1653   \textcolor{keywordtype}{logical} :: do\_any}
\DoxyCodeLine{1654 \textcolor{keywordtype}{  real} :: large\_err         \textcolor{comment}{! A large error measure [H2 \string~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{1655   \textcolor{keywordtype}{integer} :: i, it}
\DoxyCodeLine{1656   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{parameter} :: MAXIT = 30}
\DoxyCodeLine{1657 }
\DoxyCodeLine{1658   \textcolor{keywordflow}{if} (.not.cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1659     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"{}determine\_Ea\_kb should not be called "{}}//\&}
\DoxyCodeLine{1660                            \textcolor{stringliteral}{"{}unless BULKMIXEDLAYER is defined."{}})}
\DoxyCodeLine{1661 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1662   tolerance = cs\%Tolerance\_Ent}
\DoxyCodeLine{1663   large\_err = gv\%m\_to\_H**2 * 1.0e30}
\DoxyCodeLine{1664 }
\DoxyCodeLine{1665   \textcolor{keywordflow}{do} i=is,ie ; redo\_i(i) = do\_i(i) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1666 }
\DoxyCodeLine{1667   \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1668     \textcolor{comment}{! The first guess of Ent was the value from the previous iteration.}}
\DoxyCodeLine{1669 }
\DoxyCodeLine{1670     \textcolor{comment}{!   These were previously calculated and provide good limits and estimates}}
\DoxyCodeLine{1671     \textcolor{comment}{! of the errors there. By construction the errors increase with R*ea\_kbp1.}}
\DoxyCodeLine{1672     e\_min(i) = min\_eakb(i) ; e\_max(i) = max\_eakb(i)}
\DoxyCodeLine{1673     error\_mine(i) = -\/large\_err ; error\_maxe(i) = large\_err}
\DoxyCodeLine{1674     false\_position(i) = .true. \textcolor{comment}{! Used to alternate between false\_position and}}
\DoxyCodeLine{1675                                \textcolor{comment}{! bisection when Newton's method isn't working.}}
\DoxyCodeLine{1676     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(err\_min\_eakb0)) error\_mine(i) = err\_min\_eakb0(i) -\/ e\_min(i) * ea\_kbp1(i)}
\DoxyCodeLine{1677     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(err\_max\_eakb0)) error\_maxe(i) = err\_max\_eakb0(i) -\/ e\_max(i) * ea\_kbp1(i)}
\DoxyCodeLine{1678 }
\DoxyCodeLine{1679     \textcolor{keywordflow}{if} ((error\_maxe(i) <= 0.0) .or. (error\_mine(i) >= 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1680       \textcolor{comment}{! The root is not bracketed and one of the limiting values should be used.}}
\DoxyCodeLine{1681       \textcolor{keywordflow}{if} (error\_maxe(i) <= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1682         \textcolor{comment}{! The errors decrease with E*ea\_kbp1, so E\_max is the best solution.}}
\DoxyCodeLine{1683         ent(i) = e\_max(i) ; err(i) = error\_maxe(i)}
\DoxyCodeLine{1684       \textcolor{keywordflow}{else}  \textcolor{comment}{! error\_minE >= 0 is equivalent to ea\_kbp1 = 0.0.}}
\DoxyCodeLine{1685         ent(i) = e\_min(i) ; err(i) = error\_mine(i)}
\DoxyCodeLine{1686 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1687       derror\_de(i) = 0.0}
\DoxyCodeLine{1688       redo\_i(i) = .false.}
\DoxyCodeLine{1689 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1690 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}   \textcolor{comment}{! End of i-\/loop}}
\DoxyCodeLine{1691 }
\DoxyCodeLine{1692   \textcolor{keywordflow}{do} it = 1,maxit}
\DoxyCodeLine{1693     do\_any = .false. ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (redo\_i(i)) do\_any = .true. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1694     \textcolor{keywordflow}{if} (.not.do\_any) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1695     \textcolor{keyword}{call }determine\_dskb(h\_bl, sref, ent\_bl, ent, is, ie, kmb, g, gv, .true., ds\_kb, \&}
\DoxyCodeLine{1696                         ddskb\_de, ds\_lay, ddslay\_de, do\_i\_in = redo\_i)}
\DoxyCodeLine{1697     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (redo\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1698       \textcolor{comment}{!  The correct root is bracketed between E\_min and E\_max.}}
\DoxyCodeLine{1699       \textcolor{comment}{! Note the following limits:  Ent >= 0 ; fa > 1 ; fk > 0}}
\DoxyCodeLine{1700       el = 0.0 ; \textcolor{keywordflow}{if} (2.0*ent\_bl(i,kmb+1) >= ent(i)) el = 1.0}
\DoxyCodeLine{1701       fa = (1.0 + el) + ds\_kb(i)*i\_dskbp1(i)}
\DoxyCodeLine{1702       fk = dtkd\_kb(i) * (ds\_lay(i)/ds\_kb(i))}
\DoxyCodeLine{1703       fm = (ea\_kbp1(i) -\/ h\_bl(i,kmb+1)) + el*2.0*ent\_bl(i,kmb+1)}
\DoxyCodeLine{1704       \textcolor{keywordflow}{if} (fm > -\/gv\%Angstrom\_H) fm = fm + gv\%Angstrom\_H  \textcolor{comment}{! This could be smooth if need be.}}
\DoxyCodeLine{1705       err(i) = (fa * ent(i)**2 -\/ fm * ent(i)) -\/ fk}
\DoxyCodeLine{1706       derror\_de(i) = ((2.0*fa + (ddskb\_de(i)*i\_dskbp1(i))*ent(i))*ent(i) -\/ fm) -\/ \&}
\DoxyCodeLine{1707           dtkd\_kb(i) * (ddslay\_de(i)*ds\_kb(i) -\/ ddskb\_de(i)*ds\_lay(i))/(ds\_kb(i)**2)}
\DoxyCodeLine{1708 }
\DoxyCodeLine{1709       \textcolor{keywordflow}{if} (err(i) == 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1710         redo\_i(i) = .false. ; cycle}
\DoxyCodeLine{1711       \textcolor{keywordflow}{elseif} (err(i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1712         e\_max(i) = ent(i) ; error\_maxe(i) = err(i)}
\DoxyCodeLine{1713       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1714         e\_min(i) = ent(i) ; error\_mine(i) = err(i)}
\DoxyCodeLine{1715 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1716 }
\DoxyCodeLine{1717       e\_prev = ent(i)}
\DoxyCodeLine{1718       \textcolor{keywordflow}{if} ((it == 1) .or. (derror\_de(i) <= 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1719         \textcolor{comment}{!   Assuming that the coefficients of the quadratic equation are correct}}
\DoxyCodeLine{1720         \textcolor{comment}{! will usually give a very good first guess.  Also, if derror\_dE < 0.0,}}
\DoxyCodeLine{1721         \textcolor{comment}{! R is on the wrong side of the approximate parabola.  In either case,}}
\DoxyCodeLine{1722         \textcolor{comment}{! try assuming that the error is approximately a parabola and solve.}}
\DoxyCodeLine{1723         fr = sqrt(fm**2 + 4.0*fa*fk)}
\DoxyCodeLine{1724         \textcolor{keywordflow}{if} (fm >= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1725           ent(i) = (fm + fr) / (2.0 * fa)}
\DoxyCodeLine{1726         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1727           ent(i) = (2.0 * fk) / (fr -\/ fm)}
\DoxyCodeLine{1728 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1729         \textcolor{comment}{! But make sure that the root stays bracketed, bisecting if needed.}}
\DoxyCodeLine{1730         \textcolor{keywordflow}{if} ((ent(i) > e\_max(i)) .or. (ent(i) < e\_min(i))) \&}
\DoxyCodeLine{1731           ent(i) = 0.5*(e\_max(i) + e\_min(i))}
\DoxyCodeLine{1732       \textcolor{keywordflow}{elseif} (((e\_max(i)-\/ent(i))*derror\_de(i) > -\/err(i)) .and. \&}
\DoxyCodeLine{1733               ((ent(i)-\/e\_min(i))*derror\_de(i) > err(i)) ) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1734         \textcolor{comment}{! Use Newton's method for the next estimate, provided it will}}
\DoxyCodeLine{1735         \textcolor{comment}{! remain bracketed between Rmin and Rmax.}}
\DoxyCodeLine{1736         ent(i) = ent(i) -\/ err(i) / derror\_de(i)}
\DoxyCodeLine{1737       \textcolor{keywordflow}{elseif} (false\_position(i) .and. \&}
\DoxyCodeLine{1738               (error\_maxe(i) -\/ error\_mine(i) < 0.9*large\_err)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1739         \textcolor{comment}{! Use the false postion method if there are decent error estimates.}}
\DoxyCodeLine{1740         ent(i) = e\_min(i) + (e\_max(i)-\/e\_min(i)) * \&}
\DoxyCodeLine{1741                 (-\/error\_mine(i)/(error\_maxe(i) -\/ error\_mine(i)))}
\DoxyCodeLine{1742         false\_position(i) = .false.}
\DoxyCodeLine{1743       \textcolor{keywordflow}{else} \textcolor{comment}{! Bisect as a last resort or if the false position method was used last.}}
\DoxyCodeLine{1744         ent(i) = 0.5*(e\_max(i) + e\_min(i))}
\DoxyCodeLine{1745         false\_position(i) = .true.}
\DoxyCodeLine{1746 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1747 }
\DoxyCodeLine{1748       \textcolor{keywordflow}{if} (abs(e\_prev -\/ ent(i)) < tolerance) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1749         err\_est = err(i) + (ent(i) -\/ e\_prev) * derror\_de(i)}
\DoxyCodeLine{1750         \textcolor{keywordflow}{if} ((it > 1) .or. (err\_est*err(i) <= 0.0) .or. \&}
\DoxyCodeLine{1751             (abs(err\_est) < abs(tolerance*derror\_de(i)))) redo\_i(i) = .false.}
\DoxyCodeLine{1752 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1753 }
\DoxyCodeLine{1754 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}   \textcolor{comment}{! End of i-\/loop}}
\DoxyCodeLine{1755 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! End of iterations to determine Ent(i).}}
\DoxyCodeLine{1756 }
\DoxyCodeLine{1757   \textcolor{comment}{! Update the value of dS\_kb for consistency with Ent.}}
\DoxyCodeLine{1758   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(f\_kb) .or. \textcolor{keyword}{present}(dfdfm\_kb)) \&}
\DoxyCodeLine{1759     \textcolor{keyword}{call }determine\_dskb(h\_bl, sref, ent\_bl, ent, is, ie, kmb, g, gv, .true., \&}
\DoxyCodeLine{1760                         ds\_kb, do\_i\_in = do\_i)}
\DoxyCodeLine{1761 }
\DoxyCodeLine{1762   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(f\_kb)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1763     f\_kb(i) = ent(i) * (ds\_kb(i) * i\_dskbp1(i))}
\DoxyCodeLine{1764 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1765   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(error)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1766     error(i) = err(i)}
\DoxyCodeLine{1767 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1768   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dfdfm\_kb)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1769     \textcolor{comment}{!   derror\_dE and ddSkb\_dE are \_not\_ recalculated here, since dFdfm\_kb is}}
\DoxyCodeLine{1770     \textcolor{comment}{! only used in Newton's method, and slightly increasing the accuracy of the}}
\DoxyCodeLine{1771     \textcolor{comment}{! estimate is unlikely to speed convergence.}}
\DoxyCodeLine{1772     \textcolor{keywordflow}{if} (derror\_de(i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1773       dfdfm\_kb(i) = ((ds\_kb(i) + ent(i) * ddskb\_de(i)) * i\_dskbp1(i)) * \&}
\DoxyCodeLine{1774                     (ent(i) / derror\_de(i))}
\DoxyCodeLine{1775     \textcolor{keywordflow}{else} \textcolor{comment}{! Use Adcroft's division by 0 convention.}}
\DoxyCodeLine{1776       dfdfm\_kb(i) = 0.0}
\DoxyCodeLine{1777 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1778 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1779 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__entrain__diffusive_a0e71b47abe1e1889f4b687139615ca14}\label{namespacemom__entrain__diffusive_a0e71b47abe1e1889f4b687139615ca14}} 
\index{mom\_entrain\_diffusive@{mom\_entrain\_diffusive}!entrain\_diffusive\_end@{entrain\_diffusive\_end}}
\index{entrain\_diffusive\_end@{entrain\_diffusive\_end}!mom\_entrain\_diffusive@{mom\_entrain\_diffusive}}
\doxysubsubsection{\texorpdfstring{entrain\_diffusive\_end()}{entrain\_diffusive\_end()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+entrain\+\_\+diffusive\+::entrain\+\_\+diffusive\+\_\+end (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__entrain__diffusive_1_1entrain__diffusive__cs}{entrain\+\_\+diffusive\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



This subroutine cleans up and deallocates any memory associated with the entrain\+\_\+diffusive module. 


\begin{DoxyParams}{Parameters}
{\em cs} & A pointer to the control structure for this module that will be deallocated. \\
\hline
\end{DoxyParams}


Definition at line 2147 of file M\+O\+M\+\_\+entrain\+\_\+diffusive.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{2148   \textcolor{keywordtype}{type}(entrain\_diffusive\_CS), \textcolor{keywordtype}{pointer} :: CS\textcolor{comment}{ !< A pointer to the control structure for this}}
\DoxyCodeLine{2149 \textcolor{comment}{                                            !! module that will be deallocated.}}
\DoxyCodeLine{2150   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{deallocate}(cs)}
\DoxyCodeLine{2151 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__entrain__diffusive_a08d3203366fac7ec13472c79681dea0e}\label{namespacemom__entrain__diffusive_a08d3203366fac7ec13472c79681dea0e}} 
\index{mom\_entrain\_diffusive@{mom\_entrain\_diffusive}!entrain\_diffusive\_init@{entrain\_diffusive\_init}}
\index{entrain\_diffusive\_init@{entrain\_diffusive\_init}!mom\_entrain\_diffusive@{mom\_entrain\_diffusive}}
\doxysubsubsection{\texorpdfstring{entrain\_diffusive\_init()}{entrain\_diffusive\_init()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+entrain\+\_\+diffusive\+::entrain\+\_\+diffusive\+\_\+init (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), intent(in)}]{Time,  }\item[{type(\mbox{\hyperlink{structmom__grid_1_1ocean__grid__type}{ocean\+\_\+grid\+\_\+type}}), intent(in)}]{G,  }\item[{type(\mbox{\hyperlink{structmom__verticalgrid_1_1verticalgrid__type}{verticalgrid\+\_\+type}}), intent(in)}]{GV,  }\item[{type(\mbox{\hyperlink{structmom__unit__scaling_1_1unit__scale__type}{unit\+\_\+scale\+\_\+type}}), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__file__parser_1_1param__file__type}{param\+\_\+file\+\_\+type}}), intent(in)}]{param\+\_\+file,  }\item[{type(\mbox{\hyperlink{structmom__diag__mediator_1_1diag__ctrl}{diag\+\_\+ctrl}}), intent(inout), target}]{diag,  }\item[{type(\mbox{\hyperlink{structmom__entrain__diffusive_1_1entrain__diffusive__cs}{entrain\+\_\+diffusive\+\_\+cs}}), pointer}]{CS,  }\item[{logical, intent(in), optional}]{just\+\_\+read\+\_\+params }\end{DoxyParamCaption})}



This subroutine initializes the parameters and memory associated with the entrain\+\_\+diffusive module. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em time} & The current model time. \\
\hline
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em param\+\_\+file} & A structure to parse for run-\/time parameters. \\
\hline
\mbox{\texttt{ in,out}}  & {\em diag} & A structure that is used to regulate diagnostic output. \\
\hline
 & {\em cs} & A pointer that is set to point to the control structure. \\
\hline
\mbox{\texttt{ in}}  & {\em just\+\_\+read\+\_\+params} & If present and true, this call will only read parameters logging them or registering any diagnostics \\
\hline
\end{DoxyParams}


Definition at line 2080 of file M\+O\+M\+\_\+entrain\+\_\+diffusive.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{2081   \textcolor{keywordtype}{type}(time\_type),         \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{ !< The current model time.}}
\DoxyCodeLine{2082   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{2083   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{2084   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}}
\DoxyCodeLine{2085   \textcolor{keywordtype}{type}(param\_file\_type),   \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< A structure to parse for run-\/time}}
\DoxyCodeLine{2086 \textcolor{comment}{                                                 !! parameters.}}
\DoxyCodeLine{2087   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{ !< A structure that is used to regulate diagnostic}}
\DoxyCodeLine{2088 \textcolor{comment}{                                                 !! output.}}
\DoxyCodeLine{2089   \textcolor{keywordtype}{type}(entrain\_diffusive\_CS), \textcolor{keywordtype}{pointer}    :: CS\textcolor{comment}{   !< A pointer that is set to point to the control}}
\DoxyCodeLine{2090 \textcolor{comment}{                                                 !! structure.}}
\DoxyCodeLine{2091   \textcolor{keywordtype}{logical},       \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: just\_read\_params\textcolor{comment}{ !< If present and true, this call will}}
\DoxyCodeLine{2092 \textcolor{comment}{                                                 !! only read parameters logging them or registering}}
\DoxyCodeLine{2093 \textcolor{comment}{                                                 !! any diagnostics}}
\DoxyCodeLine{2094 }
\DoxyCodeLine{2095   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{2096 \textcolor{keywordtype}{  real} :: dt  \textcolor{comment}{! The dynamics timestep, used here in the default for TOLERANCE\_ENT, in MKS units [s]}}
\DoxyCodeLine{2097 \textcolor{keywordtype}{  real} :: Kd  \textcolor{comment}{! A diffusivity used in the default for TOLERANCE\_ENT, in MKS units [m2 s-\/1]}}
\DoxyCodeLine{2098   \textcolor{keywordtype}{logical} :: just\_read    \textcolor{comment}{! If true, just read parameters but do nothing else.}}
\DoxyCodeLine{2099   \textcolor{comment}{! This include declares and sets the variable "{}version"{}.}}
\DoxyCodeLine{2100 \textcolor{preprocessor}{\# include "{}version\_variable.h"{}}}
\DoxyCodeLine{2101 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"{}MOM\_entrain\_diffusive"{}} \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{2102 }
\DoxyCodeLine{2103   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2104     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"{}entrain\_diffusive\_init called with an associated "{}}// \&}
\DoxyCodeLine{2105                             \textcolor{stringliteral}{"{}control structure."{}})}
\DoxyCodeLine{2106     \textcolor{keywordflow}{return}}
\DoxyCodeLine{2107 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2108   \textcolor{keyword}{allocate}(cs)}
\DoxyCodeLine{2109 }
\DoxyCodeLine{2110   just\_read = .false. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(just\_read\_params)) just\_read = just\_read\_params}
\DoxyCodeLine{2111 }
\DoxyCodeLine{2112   cs\%diag => diag}
\DoxyCodeLine{2113 }
\DoxyCodeLine{2114   cs\%bulkmixedlayer = (gv\%nkml > 0)}
\DoxyCodeLine{2115 }
\DoxyCodeLine{2116 \textcolor{comment}{! Set default, read and log parameters}}
\DoxyCodeLine{2117   \textcolor{keywordflow}{if} (.not.just\_read) \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \textcolor{stringliteral}{"{}"{}})}
\DoxyCodeLine{2118   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}MAX\_ENT\_IT"{}}, cs\%max\_ent\_it, \&}
\DoxyCodeLine{2119                  \textcolor{stringliteral}{"{}The maximum number of iterations that may be used to "{}}//\&}
\DoxyCodeLine{2120                  \textcolor{stringliteral}{"{}calculate the interior diapycnal entrainment."{}}, default=5, do\_not\_log=just\_read)}
\DoxyCodeLine{2121   \textcolor{comment}{! In this module, KD is only used to set the default for TOLERANCE\_ENT. [m2 s-\/1]}}
\DoxyCodeLine{2122   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}KD"{}}, kd, default=0.0)}
\DoxyCodeLine{2123   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}DT"{}}, dt, \&}
\DoxyCodeLine{2124                  \textcolor{stringliteral}{"{}The (baroclinic) dynamics time step."{}}, units = \textcolor{stringliteral}{"{}s"{}}, \&}
\DoxyCodeLine{2125                  fail\_if\_missing=.true., do\_not\_log=just\_read)}
\DoxyCodeLine{2126   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}TOLERANCE\_ENT"{}}, cs\%Tolerance\_Ent, \&}
\DoxyCodeLine{2127                  \textcolor{stringliteral}{"{}The tolerance with which to solve for entrainment values."{}}, \&}
\DoxyCodeLine{2128                  units=\textcolor{stringliteral}{"{}m"{}}, default=max(100.0*gv\%Angstrom\_m,1.0e-\/4*sqrt(dt*kd)), scale=gv\%m\_to\_H, \&}
\DoxyCodeLine{2129                  do\_not\_log=just\_read)}
\DoxyCodeLine{2130 }
\DoxyCodeLine{2131   cs\%Rho\_sig\_off = 1000.0*us\%kg\_m3\_to\_R}
\DoxyCodeLine{2132 }
\DoxyCodeLine{2133   \textcolor{keywordflow}{if} (.not.just\_read) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2134     cs\%id\_Kd = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kd\_effective'}, diag\%axesTL, time, \&}
\DoxyCodeLine{2135         \textcolor{stringliteral}{'Diapycnal diffusivity as applied'}, \textcolor{stringliteral}{'m2 s-\/1'}, conversion=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{2136     cs\%id\_diff\_work = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'diff\_work'}, diag\%axesTi, time, \&}
\DoxyCodeLine{2137         \textcolor{stringliteral}{'Work actually done by diapycnal diffusion across each interface'}, \&}
\DoxyCodeLine{2138         \textcolor{stringliteral}{'W m-\/2'}, conversion=us\%RZ3\_T3\_to\_W\_m2)}
\DoxyCodeLine{2139 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2140 }
\DoxyCodeLine{2141   \textcolor{keywordflow}{if} (just\_read) \textcolor{keyword}{deallocate}(cs)}
\DoxyCodeLine{2142 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__entrain__diffusive_ae68ab2fa707778de2f92ce179729f2ff}\label{namespacemom__entrain__diffusive_ae68ab2fa707778de2f92ce179729f2ff}} 
\index{mom\_entrain\_diffusive@{mom\_entrain\_diffusive}!entrainment\_diffusive@{entrainment\_diffusive}}
\index{entrainment\_diffusive@{entrainment\_diffusive}!mom\_entrain\_diffusive@{mom\_entrain\_diffusive}}
\doxysubsubsection{\texorpdfstring{entrainment\_diffusive()}{entrainment\_diffusive()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+entrain\+\_\+diffusive\+::entrainment\+\_\+diffusive (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{type(\mbox{\hyperlink{structmom__variables_1_1thermo__var__ptrs}{thermo\+\_\+var\+\_\+ptrs}}), intent(in)}]{tv,  }\item[{type(\mbox{\hyperlink{structmom__forcing__type_1_1forcing}{forcing}}), intent(in)}]{fluxes,  }\item[{real, intent(in)}]{dt,  }\item[{type(\mbox{\hyperlink{structmom__grid_1_1ocean__grid__type}{ocean\+\_\+grid\+\_\+type}}), intent(in)}]{G,  }\item[{type(\mbox{\hyperlink{structmom__verticalgrid_1_1verticalgrid__type}{verticalgrid\+\_\+type}}), intent(in)}]{GV,  }\item[{type(\mbox{\hyperlink{structmom__unit__scaling_1_1unit__scale__type}{unit\+\_\+scale\+\_\+type}}), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__entrain__diffusive_1_1entrain__diffusive__cs}{entrain\+\_\+diffusive\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(out)}]{ea,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(out)}]{eb,  }\item[{integer, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed), intent(inout), optional}]{kb\+\_\+out,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in), optional}]{Kd\+\_\+\+Lay,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke+1), intent(in), optional}]{Kd\+\_\+int }\end{DoxyParamCaption})}



This subroutine calculates ea and eb, the rates at which a layer entrains from the layers above and below. The entrainment rates are proportional to the buoyancy flux in a layer and inversely proportional to the density differences between layers. The scheme that is used here is described in detail in Hallberg, Mon. Wea. Rev. 2000. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs. \\
\hline
\mbox{\texttt{ in}}  & {\em fluxes} & A structure of surface fluxes that may be used. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & The time increment \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
 & {\em cs} & The control structure returned by a previous call to entrain\+\_\+diffusive\+\_\+init. \\
\hline
\mbox{\texttt{ out}}  & {\em ea} & The amount of fluid entrained from the layer \\
\hline
\mbox{\texttt{ out}}  & {\em eb} & The amount of fluid entrained from the layer \\
\hline
\mbox{\texttt{ in,out}}  & {\em kb\+\_\+out} & The index of the lightest layer denser than \\
\hline
\mbox{\texttt{ in}}  & {\em kd\+\_\+lay} & The diapycnal diffusivity of layers \\
\hline
\mbox{\texttt{ in}}  & {\em kd\+\_\+int} & The diapycnal diffusivity of interfaces \\
\hline
\end{DoxyParams}


Definition at line 50 of file M\+O\+M\+\_\+entrain\+\_\+diffusive.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{52   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(in)}  :: G\textcolor{comment}{  !< The ocean's grid structure.}}
\DoxyCodeLine{53   \textcolor{keywordtype}{type}(verticalGrid\_type),    \textcolor{keywordtype}{intent(in)}  :: GV\textcolor{comment}{ !< The ocean's vertical grid structure.}}
\DoxyCodeLine{54   \textcolor{keywordtype}{type}(unit\_scale\_type),      \textcolor{keywordtype}{intent(in)}  :: US\textcolor{comment}{ !< A dimensional unit scaling type}}
\DoxyCodeLine{55 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},   \&}
\DoxyCodeLine{56                               \textcolor{keywordtype}{intent(in)}  :: h\textcolor{comment}{  !< Layer thicknesses [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{57   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),      \textcolor{keywordtype}{intent(in)}  :: tv\textcolor{comment}{ !< A structure containing pointers to any available}}
\DoxyCodeLine{58 \textcolor{comment}{                                                !! thermodynamic fields. Absent fields have NULL}}
\DoxyCodeLine{59 \textcolor{comment}{                                                !! ptrs.}}
\DoxyCodeLine{60   \textcolor{keywordtype}{type}(forcing),              \textcolor{keywordtype}{intent(in)}  :: fluxes\textcolor{comment}{ !< A structure of surface fluxes that may}}
\DoxyCodeLine{61 \textcolor{comment}{                                                !! be used.}}
\DoxyCodeLine{62 \textcolor{keywordtype}{  real},                       \textcolor{keywordtype}{intent(in)}  :: dt\textcolor{comment}{ !< The time increment [T \string~> s].}}
\DoxyCodeLine{63   \textcolor{keywordtype}{type}(entrain\_diffusive\_CS), \textcolor{keywordtype}{pointer}     :: CS\textcolor{comment}{ !< The control structure returned by a previous}}
\DoxyCodeLine{64 \textcolor{comment}{                                                !! call to entrain\_diffusive\_init.}}
\DoxyCodeLine{65 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},   \&}
\DoxyCodeLine{66                               \textcolor{keywordtype}{intent(out)} :: ea\textcolor{comment}{ !< The amount of fluid entrained from the layer}}
\DoxyCodeLine{67 \textcolor{comment}{                                                !! above within this time step [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{68 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},   \&}
\DoxyCodeLine{69                               \textcolor{keywordtype}{intent(out)} :: eb\textcolor{comment}{ !< The amount of fluid entrained from the layer}}
\DoxyCodeLine{70 \textcolor{comment}{                                                !! below within this time step [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{71   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))},        \&}
\DoxyCodeLine{72                   \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(inout)} :: kb\_out\textcolor{comment}{ !< The index of the lightest layer denser than}}
\DoxyCodeLine{73 \textcolor{comment}{                                                !! the buffer layer.}}
\DoxyCodeLine{74   \textcolor{comment}{! At least one of the two following arguments must be present.}}
\DoxyCodeLine{75 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},   \&}
\DoxyCodeLine{76                   \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: Kd\_Lay\textcolor{comment}{ !< The diapycnal diffusivity of layers}}
\DoxyCodeLine{77 \textcolor{comment}{                                                !! [Z2 T-\/1 \string~> m2 s-\/1].}}
\DoxyCodeLine{78 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G)+1)}, \&}
\DoxyCodeLine{79                   \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: Kd\_int\textcolor{comment}{ !< The diapycnal diffusivity of interfaces}}
\DoxyCodeLine{80 \textcolor{comment}{                                                !! [Z2 T-\/1 \string~> m2 s-\/1].}}
\DoxyCodeLine{81 }
\DoxyCodeLine{82 \textcolor{comment}{!   This subroutine calculates ea and eb, the rates at which a layer entrains}}
\DoxyCodeLine{83 \textcolor{comment}{! from the layers above and below.  The entrainment rates are proportional to}}
\DoxyCodeLine{84 \textcolor{comment}{! the buoyancy flux in a layer and inversely proportional to the density}}
\DoxyCodeLine{85 \textcolor{comment}{! differences between layers.  The scheme that is used here is described in}}
\DoxyCodeLine{86 \textcolor{comment}{! detail in Hallberg, Mon. Wea. Rev. 2000.}}
\DoxyCodeLine{87 }
\DoxyCodeLine{88 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))} :: \&}
\DoxyCodeLine{89     dtKd    \textcolor{comment}{! The layer diapycnal diffusivity times the time step [H2 \string~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{90 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)} :: \&}
\DoxyCodeLine{91     dtKd\_int \textcolor{comment}{! The diapycnal diffusivity at the interfaces times the time step [H2 \string~> m2 or kg2 m-\/4]}}
\DoxyCodeLine{92 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))} :: \&}
\DoxyCodeLine{93     F, \&    \textcolor{comment}{! The density flux through a layer within a time step divided by the}}
\DoxyCodeLine{94             \textcolor{comment}{! density difference across the interface below the layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{95     maxf, \& \textcolor{comment}{! maxF is the maximum value of F that will not deplete all of the}}
\DoxyCodeLine{96             \textcolor{comment}{! layers above or below a layer within a timestep [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{97     minf, \& \textcolor{comment}{! minF is the minimum flux that should be expected in the absence of}}
\DoxyCodeLine{98             \textcolor{comment}{! interactions between layers [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{99     fprev, \&\textcolor{comment}{! The previous estimate of F [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{100     dfdfm, \&\textcolor{comment}{! The partial derivative of F with respect to changes in F of the}}
\DoxyCodeLine{101             \textcolor{comment}{! neighboring layers.  [nondim]}}
\DoxyCodeLine{102     h\_guess \textcolor{comment}{! An estimate of the layer thicknesses after entrainment, but}}
\DoxyCodeLine{103             \textcolor{comment}{! before the entrainments are adjusted to drive the layer}}
\DoxyCodeLine{104             \textcolor{comment}{! densities toward their target values [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{105 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)} :: \&}
\DoxyCodeLine{106     Ent\_bl  \textcolor{comment}{! The average entrainment upward and downward across}}
\DoxyCodeLine{107             \textcolor{comment}{! each interface around the buffer layers [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{108 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{allocatable}, \textcolor{keywordtype}{dimension(:,:,:)} :: \&}
\DoxyCodeLine{109     Kd\_eff, \&     \textcolor{comment}{! The effective diffusivity that actually applies to each}}
\DoxyCodeLine{110                   \textcolor{comment}{! layer after the effects of boundary conditions are}}
\DoxyCodeLine{111                   \textcolor{comment}{! considered [Z2 T-\/1 \string~> m2 s-\/1].}}
\DoxyCodeLine{112     diff\_work     \textcolor{comment}{! The work actually done by diffusion across each}}
\DoxyCodeLine{113                   \textcolor{comment}{! interface [R Z3 T-\/3 \string~> W m-\/2].  Sum vertically for the total work.}}
\DoxyCodeLine{114 }
\DoxyCodeLine{115 \textcolor{keywordtype}{  real} :: hm, fm, fr, fk  \textcolor{comment}{! Work variables with units of H, H, H, and H2.}}
\DoxyCodeLine{116 }
\DoxyCodeLine{117 \textcolor{keywordtype}{  real} :: b1(SZI\_(G))         \textcolor{comment}{! b1 and c1 are variables used by the}}
\DoxyCodeLine{118 \textcolor{keywordtype}{  real} :: c1(SZI\_(G),SZK\_(G)) \textcolor{comment}{! tridiagonal solver.}}
\DoxyCodeLine{119 }
\DoxyCodeLine{120 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: \&}
\DoxyCodeLine{121     htot, \&       \textcolor{comment}{! The total thickness above or below a layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{122     Rcv, \&        \textcolor{comment}{! Value of the coordinate variable (potential density)}}
\DoxyCodeLine{123                   \textcolor{comment}{! based on the simulated T and S and P\_Ref [R \string~> kg m-\/3].}}
\DoxyCodeLine{124     pres, \&       \textcolor{comment}{! Reference pressure (P\_Ref) [R L2 T-\/2 \string~> Pa].}}
\DoxyCodeLine{125     eakb, \&       \textcolor{comment}{! The entrainment from above by the layer below the buffer}}
\DoxyCodeLine{126                   \textcolor{comment}{! layer (i.e. layer kb) [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{127     ea\_kbp1, \&    \textcolor{comment}{! The entrainment from above by layer kb+1 [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{128     eb\_kmb, \&     \textcolor{comment}{! The entrainment from below by the deepest buffer layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{129     ds\_kb, \&      \textcolor{comment}{! The reference potential density difference across the}}
\DoxyCodeLine{130                   \textcolor{comment}{! interface between the buffer layers and layer kb [R \string~> kg m-\/3].}}
\DoxyCodeLine{131     ds\_anom\_lim, \&\textcolor{comment}{! The amount by which dS\_kb is reduced when limits are}}
\DoxyCodeLine{132                   \textcolor{comment}{! applied [R \string~> kg m-\/3].}}
\DoxyCodeLine{133     i\_dskbp1, \&   \textcolor{comment}{! The inverse of the potential density difference across the}}
\DoxyCodeLine{134                   \textcolor{comment}{! interface below layer kb [R-\/1 \string~> m3 kg-\/1].}}
\DoxyCodeLine{135     dtkd\_kb, \&    \textcolor{comment}{! The diapycnal diffusivity in layer kb times the time step}}
\DoxyCodeLine{136                   \textcolor{comment}{! [H2 \string~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{137     maxf\_correct, \& \textcolor{comment}{! An amount by which to correct maxF due to excessive}}
\DoxyCodeLine{138                   \textcolor{comment}{! surface heat loss [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{139     zeros, \&      \textcolor{comment}{! An array of all zeros. (Usually used with [H \string~> m or kg m-\/2].)}}
\DoxyCodeLine{140     max\_eakb, \&   \textcolor{comment}{! The maximum value of eakb that might be realized [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{141     min\_eakb, \&   \textcolor{comment}{! The minimum value of eakb that might be realized [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{142     err\_max\_eakb0, \& \textcolor{comment}{! The value of error returned by determine\_Ea\_kb}}
\DoxyCodeLine{143     err\_min\_eakb0, \& \textcolor{comment}{! when eakb = min\_eakb and max\_eakb and ea\_kbp1 = 0.}}
\DoxyCodeLine{144     err\_eakb0, \&  \textcolor{comment}{! A value of error returned by determine\_Ea\_kb.}}
\DoxyCodeLine{145     f\_kb, \&       \textcolor{comment}{! The value of F in layer kb, or equivalently the entrainment}}
\DoxyCodeLine{146                   \textcolor{comment}{! from below by layer kb [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{147     dfdfm\_kb, \&   \textcolor{comment}{! The partial derivative of F with fm [nondim]. See dFdfm.}}
\DoxyCodeLine{148     maxf\_kb, \&    \textcolor{comment}{! The maximum value of F\_kb that might be realized [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{149     eakb\_maxf, \&  \textcolor{comment}{! The value of eakb that gives F\_kb=maxF\_kb [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{150     f\_kb\_maxent   \textcolor{comment}{! The value of F\_kb when eakb = max\_eakb [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{151 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))} :: \&}
\DoxyCodeLine{152     Sref, \&  \textcolor{comment}{! The reference potential density of the mixed and buffer layers,}}
\DoxyCodeLine{153              \textcolor{comment}{! and of the two lightest interior layers (kb and kb+1) copied}}
\DoxyCodeLine{154              \textcolor{comment}{! into layers kmb+1 and kmb+2 [R \string~> kg m-\/3].}}
\DoxyCodeLine{155     h\_bl     \textcolor{comment}{! The thicknesses of the mixed and buffer layers, and of the two}}
\DoxyCodeLine{156              \textcolor{comment}{! lightest interior layers (kb and kb+1) copied into layers kmb+1}}
\DoxyCodeLine{157              \textcolor{comment}{! and kmb+2 [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{158 }
\DoxyCodeLine{159 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))} :: \&}
\DoxyCodeLine{160     ds\_dsp1, \&      \textcolor{comment}{! The coordinate variable (sigma-\/2) difference across an}}
\DoxyCodeLine{161                     \textcolor{comment}{! interface divided by the difference across the interface}}
\DoxyCodeLine{162                     \textcolor{comment}{! below it. [nondim]}}
\DoxyCodeLine{163     dsp1\_ds, \&      \textcolor{comment}{! The inverse coordinate variable (sigma-\/2) difference}}
\DoxyCodeLine{164                     \textcolor{comment}{! across an interface times the difference across the}}
\DoxyCodeLine{165                     \textcolor{comment}{! interface above it. [nondim]}}
\DoxyCodeLine{166     i2p2dsp1\_ds, \&  \textcolor{comment}{! 1 / (2 + 2 * ds\_k+1 / ds\_k). [nondim]}}
\DoxyCodeLine{167     grats           \textcolor{comment}{! 2*(2 + ds\_k+1 / ds\_k + ds\_k / ds\_k+1) =}}
\DoxyCodeLine{168                     \textcolor{comment}{!       4*ds\_Lay*(1/ds\_k + 1/ds\_k+1). [nondim]}}
\DoxyCodeLine{169 }
\DoxyCodeLine{170 \textcolor{keywordtype}{  real} :: dRHo      \textcolor{comment}{! The change in locally referenced potential density between}}
\DoxyCodeLine{171                     \textcolor{comment}{! the layers above and below an interface [R \string~> kg m-\/3].}}
\DoxyCodeLine{172 \textcolor{keywordtype}{  real} :: g\_2dt     \textcolor{comment}{! 0.5 * G\_Earth / dt, times unit conversion factors}}
\DoxyCodeLine{173                     \textcolor{comment}{! [m3 H-\/2 s-\/2 T-\/1 \string~> m s-\/3 or m7 kg-\/2 s-\/3].}}
\DoxyCodeLine{174 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: \&}
\DoxyCodeLine{175     pressure, \&      \textcolor{comment}{! The pressure at an interface [R L2 T-\/2 \string~> Pa].}}
\DoxyCodeLine{176     T\_eos, S\_eos, \&  \textcolor{comment}{! The potential temperature and salinity at which to}}
\DoxyCodeLine{177                      \textcolor{comment}{! evaluate dRho\_dT and dRho\_dS [degC] and [ppt].}}
\DoxyCodeLine{178     drho\_dt, drho\_ds \textcolor{comment}{! The partial derivatives of potential density with temperature and}}
\DoxyCodeLine{179                      \textcolor{comment}{! salinity, [R degC-\/1 \string~> kg m-\/3 degC-\/1] and [R ppt-\/1 \string~> kg m-\/3 ppt-\/1].}}
\DoxyCodeLine{180 }
\DoxyCodeLine{181 \textcolor{keywordtype}{  real} :: tolerance  \textcolor{comment}{! The tolerance within which E must be converged [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{182 \textcolor{keywordtype}{  real} :: Angstrom   \textcolor{comment}{! The minimum layer thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{183 \textcolor{keywordtype}{  real} :: h\_neglect  \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{184                      \textcolor{comment}{! in roundoff and can be neglected [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{185 \textcolor{keywordtype}{  real} :: F\_cor      \textcolor{comment}{! A correction to the amount of F that is used to}}
\DoxyCodeLine{186                      \textcolor{comment}{! entrain from the layer above [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{187 \textcolor{keywordtype}{  real} :: Kd\_here    \textcolor{comment}{! The effective diapycnal diffusivity [H2 s-\/1 \string~> m2 s-\/1 or kg2 m-\/4 s-\/1].}}
\DoxyCodeLine{188 \textcolor{keywordtype}{  real} :: h\_avail    \textcolor{comment}{! The thickness that is available for entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{189 \textcolor{keywordtype}{  real} :: dS\_kb\_eff  \textcolor{comment}{! The value of dS\_kb after limiting is taken into account.}}
\DoxyCodeLine{190 \textcolor{keywordtype}{  real} :: Rho\_cor    \textcolor{comment}{! The depth-\/integrated potential density anomaly that}}
\DoxyCodeLine{191                      \textcolor{comment}{! needs to be corrected for [H R \string~> kg m-\/2 or kg2 m-\/5].}}
\DoxyCodeLine{192 \textcolor{keywordtype}{  real} :: ea\_cor     \textcolor{comment}{! The corrective adjustment to eakb [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{193 \textcolor{keywordtype}{  real} :: h1         \textcolor{comment}{! The layer thickness after entrainment through the}}
\DoxyCodeLine{194                      \textcolor{comment}{! interface below is taken into account [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{195 \textcolor{keywordtype}{  real} :: Idt        \textcolor{comment}{! The inverse of the time step [T-\/1 \string~> s-\/1].}}
\DoxyCodeLine{196 }
\DoxyCodeLine{197   \textcolor{keywordtype}{logical} :: do\_any}
\DoxyCodeLine{198   \textcolor{keywordtype}{logical} :: do\_entrain\_eakb    \textcolor{comment}{! True if buffer layer is entrained}}
\DoxyCodeLine{199   \textcolor{keywordtype}{logical} :: do\_i(SZI\_(G)), did\_i(SZI\_(G)), reiterate}
\DoxyCodeLine{200   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: EOSdom \textcolor{comment}{! The i-\/computational domain for the equation of state}}
\DoxyCodeLine{201   \textcolor{keywordtype}{integer} :: it, i, j, k, is, ie, js, je, nz, K2, kmb}
\DoxyCodeLine{202   \textcolor{keywordtype}{integer} :: kb(SZI\_(G))  \textcolor{comment}{! The value of kb in row j.}}
\DoxyCodeLine{203   \textcolor{keywordtype}{integer} :: kb\_min       \textcolor{comment}{! The minimum value of kb in the current j-\/row.}}
\DoxyCodeLine{204   \textcolor{keywordtype}{integer} :: kb\_min\_act   \textcolor{comment}{! The minimum active value of kb in the current j-\/row.}}
\DoxyCodeLine{205   \textcolor{keywordtype}{integer} :: is1, ie1     \textcolor{comment}{! The minimum and maximum active values of i in the current j-\/row.}}
\DoxyCodeLine{206   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = g\%ke}
\DoxyCodeLine{207   angstrom = gv\%Angstrom\_H}
\DoxyCodeLine{208   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{209 }
\DoxyCodeLine{210   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, \&}
\DoxyCodeLine{211          \textcolor{stringliteral}{"{}MOM\_entrain\_diffusive: Module must be initialized before it is used."{}})}
\DoxyCodeLine{212 }
\DoxyCodeLine{213   \textcolor{keywordflow}{if} (.not.(\textcolor{keyword}{present}(kd\_lay) .or. \textcolor{keyword}{present}(kd\_int))) \textcolor{keyword}{call }mom\_error(fatal, \&}
\DoxyCodeLine{214       \textcolor{stringliteral}{"{}MOM\_entrain\_diffusive: Either Kd\_Lay or Kd\_int must be present in call."{}})}
\DoxyCodeLine{215 }
\DoxyCodeLine{216   \textcolor{keywordflow}{if} ((.not.cs\%bulkmixedlayer .and. .not.\textcolor{keyword}{associated}(fluxes\%buoy)) .and. \&}
\DoxyCodeLine{217       (\textcolor{keyword}{associated}(fluxes\%lprec) .or. \textcolor{keyword}{associated}(fluxes\%evap) .or. \&}
\DoxyCodeLine{218        \textcolor{keyword}{associated}(fluxes\%sens) .or. \textcolor{keyword}{associated}(fluxes\%sw))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{219     \textcolor{keywordflow}{if} (is\_root\_pe()) \textcolor{keyword}{call }mom\_error(note, \textcolor{stringliteral}{"{}Calculate\_Entrainment: \&}}
\DoxyCodeLine{220 \textcolor{stringliteral}{}\textcolor{stringliteral}{          \&The code to handle evaporation and precipitation without \&}}
\DoxyCodeLine{221 \textcolor{stringliteral}{}\textcolor{stringliteral}{          \&a bulk mixed layer has not been implemented."{}})}
\DoxyCodeLine{222     \textcolor{keywordflow}{if} (is\_root\_pe()) \textcolor{keyword}{call }mom\_error(fatal, \&}
\DoxyCodeLine{223          \textcolor{stringliteral}{"{}Either define BULKMIXEDLAYER in MOM\_input or use fluxes\%buoy \&}}
\DoxyCodeLine{224 \textcolor{stringliteral}{}\textcolor{stringliteral}{         \&and a linear equation of state to drive the model."{}})}
\DoxyCodeLine{225 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{226 }
\DoxyCodeLine{227   tolerance = cs\%Tolerance\_Ent}
\DoxyCodeLine{228   kmb = gv\%nk\_rho\_varies}
\DoxyCodeLine{229   k2 = max(kmb+1,2) ; kb\_min = k2}
\DoxyCodeLine{230   \textcolor{keywordflow}{if} (.not. cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{231     kb(:) = 1}
\DoxyCodeLine{232     \textcolor{comment}{! These lines fill in values that are arbitrary, but needed because}}
\DoxyCodeLine{233     \textcolor{comment}{! they are used to normalize the buoyancy flux in layer nz.}}
\DoxyCodeLine{234     \textcolor{keywordflow}{do} i=is,ie ; ds\_dsp1(i,nz) = 2.0 ; dsp1\_ds(i,nz) = 0.5 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{235   \textcolor{keywordflow}{else}}
\DoxyCodeLine{236     kb(:) = 0}
\DoxyCodeLine{237     \textcolor{keywordflow}{do} i=is,ie ; ds\_dsp1(i,nz) = 0.0 ; dsp1\_ds(i,nz) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{238 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{239 }
\DoxyCodeLine{240   \textcolor{keywordflow}{if} (cs\%id\_diff\_work > 0) \textcolor{keyword}{allocate}(diff\_work(g\%isd:g\%ied,g\%jsd:g\%jed,nz+1))}
\DoxyCodeLine{241   \textcolor{keywordflow}{if} (cs\%id\_Kd > 0)        \textcolor{keyword}{allocate}(kd\_eff(g\%isd:g\%ied,g\%jsd:g\%jed,nz))}
\DoxyCodeLine{242 }
\DoxyCodeLine{243   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%eqn\_of\_state)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{244     pres(:) = tv\%P\_Ref}
\DoxyCodeLine{245   \textcolor{keywordflow}{else}}
\DoxyCodeLine{246     pres(:) = 0.0}
\DoxyCodeLine{247 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{248   eosdom(:) = eos\_domain(g\%HI)}
\DoxyCodeLine{249 }
\DoxyCodeLine{250   \textcolor{comment}{!\$OMP parallel do default(none) shared(is,ie,js,je,nz,Kd\_Lay,G,GV,US,dt,CS,h,tv,   \&}}
\DoxyCodeLine{251   \textcolor{comment}{!\$OMP                                  kmb,Angstrom,fluxes,K2,h\_neglect,tolerance, \&}}
\DoxyCodeLine{252   \textcolor{comment}{!\$OMP                                  ea,eb,Kd\_int,Kd\_eff,EOSdom,diff\_work,g\_2dt, kb\_out) \&}}
\DoxyCodeLine{253   \textcolor{comment}{!\$OMP                     firstprivate(kb,ds\_dsp1,dsp1\_ds,pres,kb\_min)             \&}}
\DoxyCodeLine{254   \textcolor{comment}{!\$OMP                          private(dtKd,dtKd\_int,do\_i,Ent\_bl,dtKd\_kb,h\_bl,     \&}}
\DoxyCodeLine{255   \textcolor{comment}{!\$OMP                                  I2p2dsp1\_ds,grats,htot,max\_eakb,I\_dSkbp1,   \&}}
\DoxyCodeLine{256   \textcolor{comment}{!\$OMP                                  zeros,maxF\_kb,maxF,ea\_kbp1,eakb,Sref,       \&}}
\DoxyCodeLine{257   \textcolor{comment}{!\$OMP                                  maxF\_correct,do\_any,do\_entrain\_eakb,        \&}}
\DoxyCodeLine{258   \textcolor{comment}{!\$OMP                                  err\_min\_eakb0,err\_max\_eakb0,eakb\_maxF,      \&}}
\DoxyCodeLine{259   \textcolor{comment}{!\$OMP                                  min\_eakb,err\_eakb0,F,minF,hm,fk,F\_kb\_maxent,\&}}
\DoxyCodeLine{260   \textcolor{comment}{!\$OMP                                  F\_kb,is1,ie1,kb\_min\_act,dFdfm\_kb,b1,dFdfm,  \&}}
\DoxyCodeLine{261   \textcolor{comment}{!\$OMP                                  Fprev,fm,fr,c1,reiterate,eb\_kmb,did\_i,      \&}}
\DoxyCodeLine{262   \textcolor{comment}{!\$OMP                                  h\_avail,h\_guess,dS\_kb,Rcv,F\_cor,dS\_kb\_eff,  \&}}
\DoxyCodeLine{263   \textcolor{comment}{!\$OMP                                  Rho\_cor,ea\_cor,h1,Idt,Kd\_here,pressure,     \&}}
\DoxyCodeLine{264   \textcolor{comment}{!\$OMP                                  T\_eos,S\_eos,dRho\_dT,dRho\_dS,dRho,dS\_anom\_lim)}}
\DoxyCodeLine{265   \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{266     \textcolor{keywordflow}{do} i=is,ie ; kb(i) = 1 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{267 }
\DoxyCodeLine{268     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_lay)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{269       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{270         dtkd(i,k) = gv\%Z\_to\_H**2 * (dt * kd\_lay(i,j,k))}
\DoxyCodeLine{271 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{272       \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kd\_int)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{273         \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{274           dtkd\_int(i,k) = gv\%Z\_to\_H**2 * (dt * kd\_int(i,j,k))}
\DoxyCodeLine{275 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{276       \textcolor{keywordflow}{else}}
\DoxyCodeLine{277         \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{278           dtkd\_int(i,k) = gv\%Z\_to\_H**2 * (0.5 * dt * (kd\_lay(i,j,k-\/1) + kd\_lay(i,j,k)))}
\DoxyCodeLine{279 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{280 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{281     \textcolor{keywordflow}{else} \textcolor{comment}{! Kd\_int must be present, or there already would have been an error.}}
\DoxyCodeLine{282       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{283         dtkd(i,k) = gv\%Z\_to\_H**2 * (0.5 * dt * (kd\_int(i,j,k)+kd\_int(i,j,k+1)))}
\DoxyCodeLine{284 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{285       \textcolor{keywordflow}{dO} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{286         dtkd\_int(i,k) = gv\%Z\_to\_H**2 * (dt * kd\_int(i,j,k))}
\DoxyCodeLine{287 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{288 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{289 }
\DoxyCodeLine{290     \textcolor{keywordflow}{do} i=is,ie ; do\_i(i) = (g\%mask2dT(i,j) > 0.5) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{291     \textcolor{keywordflow}{do} i=is,ie ; ds\_dsp1(i,nz) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{292     \textcolor{keywordflow}{do} i=is,ie ; dsp1\_ds(i,nz) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{293 }
\DoxyCodeLine{294     \textcolor{keywordflow}{do} k=2,nz-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{295       ds\_dsp1(i,k) = gv\%g\_prime(k) / gv\%g\_prime(k+1)}
\DoxyCodeLine{296 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{297 }
\DoxyCodeLine{298     \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{299       \textcolor{comment}{!   This subroutine determines the averaged entrainment across each}}
\DoxyCodeLine{300       \textcolor{comment}{! interface and causes thin and relatively light interior layers to be}}
\DoxyCodeLine{301       \textcolor{comment}{! entrained by the deepest buffer layer.  This also determines kb.}}
\DoxyCodeLine{302       \textcolor{keyword}{call }set\_ent\_bl(h, dtkd\_int, tv, kb, kmb, do\_i, g, gv, us, cs, j, ent\_bl, sref, h\_bl)}
\DoxyCodeLine{303 }
\DoxyCodeLine{304       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{305         dtkd\_kb(i) = 0.0 ; \textcolor{keywordflow}{if} (kb(i) < nz) dtkd\_kb(i) = dtkd(i,kb(i))}
\DoxyCodeLine{306 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{307     \textcolor{keywordflow}{else}}
\DoxyCodeLine{308       \textcolor{keywordflow}{do} i=is,ie ; ent\_bl(i,kmb+1) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{309 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{310 }
\DoxyCodeLine{311     \textcolor{keywordflow}{do} k=2,nz-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{312       dsp1\_ds(i,k) = 1.0 / ds\_dsp1(i,k)}
\DoxyCodeLine{313       i2p2dsp1\_ds(i,k) = 0.5/(1.0+dsp1\_ds(i,k))}
\DoxyCodeLine{314       grats(i,k) = 2.0*(2.0+(dsp1\_ds(i,k)+ds\_dsp1(i,k)))}
\DoxyCodeLine{315 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{316 }
\DoxyCodeLine{317 \textcolor{comment}{!   Determine the maximum flux, maxF, for each of the isopycnal layers.}}
\DoxyCodeLine{318 \textcolor{comment}{!   Also determine when the fluxes start entraining}}
\DoxyCodeLine{319 \textcolor{comment}{! from various buffer or mixed layers, where appropriate.}}
\DoxyCodeLine{320     \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{321       kb\_min = nz}
\DoxyCodeLine{322       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{323         htot(i) = h(i,j,1) -\/ angstrom}
\DoxyCodeLine{324 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{325       \textcolor{keywordflow}{do} k=2,kmb ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{326         htot(i) = htot(i) + (h(i,j,k) -\/ angstrom)}
\DoxyCodeLine{327 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{328       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{329         max\_eakb(i) = max(ent\_bl(i,kmb+1) + 0.5*htot(i), htot(i))}
\DoxyCodeLine{330         i\_dskbp1(i) = 1.0 / (sref(i,kmb+2) -\/ sref(i,kmb+1))}
\DoxyCodeLine{331         zeros(i) = 0.0}
\DoxyCodeLine{332 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{333 }
\DoxyCodeLine{334       \textcolor{comment}{!   Find the maximum amount of entrainment from below that the top}}
\DoxyCodeLine{335       \textcolor{comment}{! interior layer could exhibit (maxF(i,kb)), approximating that}}
\DoxyCodeLine{336       \textcolor{comment}{! entrainment by (eakb*max(dS\_kb/dSkbp1,0)).  eakb is in the range}}
\DoxyCodeLine{337       \textcolor{comment}{! from 0 to max\_eakb.}}
\DoxyCodeLine{338       \textcolor{keyword}{call }find\_maxf\_kb(h\_bl, sref, ent\_bl, i\_dskbp1, zeros, max\_eakb, kmb, \&}
\DoxyCodeLine{339                         is, ie, g, gv, cs, maxf\_kb, eakb\_maxf, do\_i, f\_kb\_maxent)}
\DoxyCodeLine{340       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (kb(i) <= nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{341         maxf(i,kb(i)) = max(0.0, maxf\_kb(i), f\_kb\_maxent(i))}
\DoxyCodeLine{342         \textcolor{keywordflow}{if} ((maxf\_kb(i) > f\_kb\_maxent(i)) .and. (eakb\_maxf(i) >= htot(i))) \&}
\DoxyCodeLine{343           max\_eakb(i) = eakb\_maxf(i)}
\DoxyCodeLine{344 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{345 }
\DoxyCodeLine{346       \textcolor{keywordflow}{do} i=is,ie ; ea\_kbp1(i) = 0.0 ; eakb(i) = max\_eakb(i) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{347       \textcolor{keyword}{call }determine\_ea\_kb(h\_bl, dtkd\_kb, sref, i\_dskbp1, ent\_bl, ea\_kbp1, \&}
\DoxyCodeLine{348                            max\_eakb, max\_eakb, kmb, is, ie, do\_i, g, gv, cs, eakb, \&}
\DoxyCodeLine{349                            error=err\_max\_eakb0, f\_kb=f\_kb)}
\DoxyCodeLine{350 }
\DoxyCodeLine{351       \textcolor{comment}{!   The maximum value of F(kb) between htot and max\_eakb determines}}
\DoxyCodeLine{352       \textcolor{comment}{! what maxF(kb+1) should be.}}
\DoxyCodeLine{353       \textcolor{keywordflow}{do} i=is,ie ; min\_eakb(i) = min(htot(i), max\_eakb(i)) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{354       \textcolor{keyword}{call }find\_maxf\_kb(h\_bl, sref, ent\_bl, i\_dskbp1, min\_eakb, max\_eakb, \&}
\DoxyCodeLine{355                         kmb, is, ie, g, gv, cs, f\_kb\_maxent, do\_i\_in = do\_i)}
\DoxyCodeLine{356 }
\DoxyCodeLine{357       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{358         do\_entrain\_eakb = .false.}
\DoxyCodeLine{359         \textcolor{comment}{! If error\_max\_eakb0 < 0, then buffer layers are always all entrained}}
\DoxyCodeLine{360         \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (err\_max\_eakb0(i) < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{361           do\_entrain\_eakb = .true.}
\DoxyCodeLine{362 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{363 }
\DoxyCodeLine{364         \textcolor{keywordflow}{if} (do\_entrain\_eakb) \textcolor{keywordflow}{then}}
\DoxyCodeLine{365           eakb(i) = max\_eakb(i) ; min\_eakb(i) = max\_eakb(i)}
\DoxyCodeLine{366         \textcolor{keywordflow}{else}}
\DoxyCodeLine{367           eakb(i) = 0.0 ; min\_eakb(i) = 0.0}
\DoxyCodeLine{368 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{369 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{370 }
\DoxyCodeLine{371       \textcolor{comment}{!   Find the amount of entrainment of the buffer layers that would occur}}
\DoxyCodeLine{372       \textcolor{comment}{! if there were no entrainment by the deeper interior layers.  Also find}}
\DoxyCodeLine{373       \textcolor{comment}{! how much entrainment of the deeper layers would occur.}}
\DoxyCodeLine{374       \textcolor{keyword}{call }determine\_ea\_kb(h\_bl, dtkd\_kb, sref, i\_dskbp1, ent\_bl, ea\_kbp1, \&}
\DoxyCodeLine{375                            zeros, max\_eakb, kmb, is, ie, do\_i, g, gv, cs, min\_eakb, \&}
\DoxyCodeLine{376                            error=err\_min\_eakb0, f\_kb=f\_kb, err\_max\_eakb0=err\_max\_eakb0)}
\DoxyCodeLine{377       \textcolor{comment}{! Error\_min\_eakb0 should be \string~0 unless error\_max\_eakb0 < 0.}}
\DoxyCodeLine{378       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} ((kb(i)<nz) .and. (kb\_min>kb(i))) kb\_min = kb(i) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{379     \textcolor{keywordflow}{else}}
\DoxyCodeLine{380       \textcolor{comment}{! Without a bulk mixed layer, surface fluxes are applied in this}}
\DoxyCodeLine{381       \textcolor{comment}{! subroutine.  (Otherwise, they are handled in mixedlayer.)}}
\DoxyCodeLine{382       \textcolor{comment}{!   Initially the maximum flux in layer zero is given by the surface}}
\DoxyCodeLine{383       \textcolor{comment}{! buoyancy flux.  It will later be limited if the surface flux is}}
\DoxyCodeLine{384       \textcolor{comment}{! too large.  Here buoy is the surface buoyancy flux.}}
\DoxyCodeLine{385       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{386         maxf(i,1) = 0.0}
\DoxyCodeLine{387         htot(i) = h(i,j,1) -\/ angstrom}
\DoxyCodeLine{388 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{389       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes\%buoy)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{390         maxf(i,1) = gv\%Z\_to\_H * (dt*fluxes\%buoy(i,j)) / gv\%g\_prime(2)}
\DoxyCodeLine{391 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{392 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{393 }
\DoxyCodeLine{394 \textcolor{comment}{! The following code calculates the maximum flux, maxF, for the interior}}
\DoxyCodeLine{395 \textcolor{comment}{! layers.}}
\DoxyCodeLine{396     \textcolor{keywordflow}{do} k=kb\_min,nz-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{397       \textcolor{keywordflow}{if} ((k == kb(i)+1) .and. cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{398         maxf(i,k) = ds\_dsp1(i,k)*(f\_kb\_maxent(i) + htot(i))}
\DoxyCodeLine{399         htot(i) = htot(i) + (h(i,j,k) -\/ angstrom)}
\DoxyCodeLine{400       \textcolor{keywordflow}{elseif} (k > kb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{401         maxf(i,k) = ds\_dsp1(i,k)*(maxf(i,k-\/1) + htot(i))}
\DoxyCodeLine{402         htot(i) = htot(i) + (h(i,j,k) -\/ angstrom)}
\DoxyCodeLine{403 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{404 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{405     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{406       maxf(i,nz) = 0.0}
\DoxyCodeLine{407       \textcolor{keywordflow}{if} (.not.cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{408         maxf\_correct(i) = max(0.0, -\/(maxf(i,nz-\/1) + htot(i)))}
\DoxyCodeLine{409 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{410       htot(i) = h(i,j,nz) -\/ angstrom}
\DoxyCodeLine{411 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{412     \textcolor{keywordflow}{if} (.not.cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{413       do\_any = .false. ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (maxf\_correct(i) > 0.0) do\_any = .true. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{414       \textcolor{keywordflow}{if} (do\_any) \textcolor{keywordflow}{then}}
\DoxyCodeLine{415         \textcolor{keywordflow}{do} k=nz-\/1,1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{416           maxf(i,k) = maxf(i,k) + maxf\_correct(i)}
\DoxyCodeLine{417           maxf\_correct(i) = maxf\_correct(i) * dsp1\_ds(i,k)}
\DoxyCodeLine{418 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{419 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{420 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{421     \textcolor{keywordflow}{do} k=nz-\/1,kb\_min,-\/1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{422       \textcolor{keywordflow}{if} (k >= kb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{423         maxf(i,k) = min(maxf(i,k),dsp1\_ds(i,k+1)*maxf(i,k+1) + htot(i))}
\DoxyCodeLine{424         htot(i) = htot(i) + (h(i,j,k) -\/ angstrom)}
\DoxyCodeLine{425 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{426       \textcolor{keywordflow}{if} (k == kb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{427         \textcolor{keywordflow}{if} ((maxf(i,k) < f\_kb(i)) .or. (maxf(i,k) < maxf\_kb(i)) \&}
\DoxyCodeLine{428             .and. (eakb\_maxf(i) <= max\_eakb(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{429           \textcolor{comment}{!   In this case, too much was being entrained by the topmost interior}}
\DoxyCodeLine{430           \textcolor{comment}{! layer, even with the minimum initial estimate.  The buffer layer}}
\DoxyCodeLine{431           \textcolor{comment}{! will always entrain the maximum amount.}}
\DoxyCodeLine{432           f\_kb(i) = maxf(i,k)}
\DoxyCodeLine{433           \textcolor{keywordflow}{if} ((f\_kb(i) <= maxf\_kb(i)) .and. (eakb\_maxf(i) <= max\_eakb(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{434             eakb(i) = eakb\_maxf(i)}
\DoxyCodeLine{435           \textcolor{keywordflow}{else}}
\DoxyCodeLine{436             eakb(i) = max\_eakb(i)}
\DoxyCodeLine{437 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{438           \textcolor{keyword}{call }f\_kb\_to\_ea\_kb(h\_bl, sref, ent\_bl, i\_dskbp1, f\_kb, kmb, i, \&}
\DoxyCodeLine{439                              g, gv, cs, eakb, angstrom)}
\DoxyCodeLine{440           \textcolor{keywordflow}{if} ((eakb(i) < max\_eakb(i)) .or. (eakb(i) < min\_eakb(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{441             \textcolor{keyword}{call }determine\_ea\_kb(h\_bl, dtkd\_kb, sref, i\_dskbp1, ent\_bl, zeros, \&}
\DoxyCodeLine{442                                  eakb, eakb, kmb, i, i, do\_i, g, gv, cs, eakb, \&}
\DoxyCodeLine{443                                  error=err\_eakb0)}
\DoxyCodeLine{444             \textcolor{keywordflow}{if} (eakb(i) < max\_eakb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{445               max\_eakb(i) = eakb(i) ; err\_max\_eakb0(i) = err\_eakb0(i)}
\DoxyCodeLine{446 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{447             \textcolor{keywordflow}{if} (eakb(i) < min\_eakb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{448               min\_eakb(i) = eakb(i) ; err\_min\_eakb0(i) = err\_eakb0(i)}
\DoxyCodeLine{449 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{450 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{451 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{452 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{453 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{454     \textcolor{keywordflow}{if} (.not.cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{455       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{456         maxf(i,1) = min(maxf(i,1),dsp1\_ds(i,2)*maxf(i,2) + htot(i))}
\DoxyCodeLine{457 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{458 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{459 }
\DoxyCodeLine{460 \textcolor{comment}{!   The following code provides an initial estimate of the flux in}}
\DoxyCodeLine{461 \textcolor{comment}{! each layer, F.  The initial guess for the layer diffusive flux is}}
\DoxyCodeLine{462 \textcolor{comment}{! the smaller of a forward discretization or the maximum diffusive}}
\DoxyCodeLine{463 \textcolor{comment}{! flux starting from zero thickness in one time step without}}
\DoxyCodeLine{464 \textcolor{comment}{! considering adjacent layers.}}
\DoxyCodeLine{465     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{466       f(i,1) = maxf(i,1)}
\DoxyCodeLine{467       f(i,nz) = maxf(i,nz) ; minf(i,nz) = 0.0}
\DoxyCodeLine{468 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{469     \textcolor{keywordflow}{do} k=nz-\/1,k2,-\/1}
\DoxyCodeLine{470       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{471         \textcolor{keywordflow}{if} ((k==kb(i)) .and. (do\_i(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{472           eakb(i) = min\_eakb(i)}
\DoxyCodeLine{473           minf(i,k) = 0.0}
\DoxyCodeLine{474         \textcolor{keywordflow}{elseif} ((k>kb(i)) .and. (do\_i(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{475 \textcolor{comment}{!   Here the layer flux is estimated, assuming no entrainment from}}
\DoxyCodeLine{476 \textcolor{comment}{! the surrounding layers.  The estimate is a forward (steady) flux,}}
\DoxyCodeLine{477 \textcolor{comment}{! limited by the maximum flux for a layer starting with zero}}
\DoxyCodeLine{478 \textcolor{comment}{! thickness.  This is often a good guess and leads to few iterations.}}
\DoxyCodeLine{479           hm = h(i,j,k) + h\_neglect}
\DoxyCodeLine{480   \textcolor{comment}{!   Note: Tried sqrt((0.5*ds\_dsp1(i,k))*dtKd(i,k)) for the second limit,}}
\DoxyCodeLine{481   \textcolor{comment}{! but it usually doesn't work as well.}}
\DoxyCodeLine{482           f(i,k) = min(maxf(i,k), sqrt(ds\_dsp1(i,k)*dtkd(i,k)), \&}
\DoxyCodeLine{483                        0.5*(ds\_dsp1(i,k)+1.0) * (dtkd(i,k) / hm))}
\DoxyCodeLine{484 }
\DoxyCodeLine{485 \textcolor{comment}{!   Calculate the minimum flux that can be expected if there is no entrainment}}
\DoxyCodeLine{486 \textcolor{comment}{! from the neighboring layers.  The 0.9 is used to give used to give a 10\%}}
\DoxyCodeLine{487 \textcolor{comment}{! known error tolerance.}}
\DoxyCodeLine{488           fk = dtkd(i,k) * grats(i,k)}
\DoxyCodeLine{489           minf(i,k) = min(maxf(i,k), \&}
\DoxyCodeLine{490                           0.9*(i2p2dsp1\_ds(i,k) * fk / (hm + sqrt(hm*hm + fk))))}
\DoxyCodeLine{491           \textcolor{keywordflow}{if} (k==kb(i)) minf(i,k) = 0.0 \textcolor{comment}{! BACKWARD COMPATIBILITY -\/ DELETE LATER?}}
\DoxyCodeLine{492         \textcolor{keywordflow}{else}}
\DoxyCodeLine{493           f(i,k) = 0.0}
\DoxyCodeLine{494           minf(i,k) = 0.0}
\DoxyCodeLine{495 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{496 \textcolor{keywordflow}{      enddo} \textcolor{comment}{! end of i loop}}
\DoxyCodeLine{497 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! end of k loop}}
\DoxyCodeLine{498 }
\DoxyCodeLine{499     \textcolor{comment}{! This is where the fluxes are actually calculated.}}
\DoxyCodeLine{500 }
\DoxyCodeLine{501     is1 = ie+1 ; ie1 = is-\/1}
\DoxyCodeLine{502     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then} ; is1 = i ; \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{503     \textcolor{keywordflow}{do} i=ie,is,-\/1 ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then} ; ie1 = i ; \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{504 }
\DoxyCodeLine{505     \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{506       kb\_min\_act = nz}
\DoxyCodeLine{507       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{508         \textcolor{keywordflow}{if} (do\_i(i) .and. (kb(i) < kb\_min\_act)) kb\_min\_act = kb(i)}
\DoxyCodeLine{509 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{510       \textcolor{comment}{!   Solve for the entrainment rate from above in the topmost interior}}
\DoxyCodeLine{511       \textcolor{comment}{! layer, eakb, such that}}
\DoxyCodeLine{512       \textcolor{comment}{!   eakb*dS\_implicit = dt*Kd*dS\_layer\_implicit / h\_implicit.}}
\DoxyCodeLine{513       \textcolor{keywordflow}{do} i=is1,ie1}
\DoxyCodeLine{514         ea\_kbp1(i) = 0.0}
\DoxyCodeLine{515         \textcolor{keywordflow}{if} (do\_i(i) .and. (kb(i) < nz)) \&}
\DoxyCodeLine{516           ea\_kbp1(i) = dsp1\_ds(i,kb(i)+1)*f(i,kb(i)+1)}
\DoxyCodeLine{517 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{518       \textcolor{keyword}{call }determine\_ea\_kb(h\_bl, dtkd\_kb, sref, i\_dskbp1, ent\_bl, ea\_kbp1, min\_eakb, \&}
\DoxyCodeLine{519                            max\_eakb, kmb, is1, ie1, do\_i, g, gv, cs, eakb, f\_kb=f\_kb, \&}
\DoxyCodeLine{520                            err\_max\_eakb0=err\_max\_eakb0, err\_min\_eakb0=err\_min\_eakb0, \&}
\DoxyCodeLine{521                            dfdfm\_kb=dfdfm\_kb)}
\DoxyCodeLine{522     \textcolor{keywordflow}{else}}
\DoxyCodeLine{523       kb\_min\_act = kb\_min}
\DoxyCodeLine{524 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{525 }
\DoxyCodeLine{526     \textcolor{keywordflow}{do} it=0,cs\%max\_ent\_it-\/1}
\DoxyCodeLine{527       \textcolor{keywordflow}{do} i=is1,ie1 ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{528         \textcolor{keywordflow}{if} (.not.cs\%bulkmixedlayer) f(i,1) = min(f(i,1),maxf(i,1))}
\DoxyCodeLine{529         b1(i) = 1.0}
\DoxyCodeLine{530 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! end of i loop}}
\DoxyCodeLine{531 }
\DoxyCodeLine{532      \textcolor{comment}{! F\_kb has already been found for this iteration, either above or at}}
\DoxyCodeLine{533      \textcolor{comment}{! the end of the code for the previous iteration.}}
\DoxyCodeLine{534       \textcolor{keywordflow}{do} k=kb\_min\_act,nz-\/1 ; \textcolor{keywordflow}{do} i=is1,ie1 ; \textcolor{keywordflow}{if} (do\_i(i) .and. (k>=kb(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{535         \textcolor{comment}{! Calculate the flux in layer k.}}
\DoxyCodeLine{536         \textcolor{keywordflow}{if} (cs\%bulkmixedlayer .and. (k==kb(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{537           f(i,k) = f\_kb(i)}
\DoxyCodeLine{538           dfdfm(i,k) = dfdfm\_kb(i)}
\DoxyCodeLine{539         \textcolor{keywordflow}{else} \textcolor{comment}{! k > kb(i)}}
\DoxyCodeLine{540           fprev(i,k) = f(i,k)}
\DoxyCodeLine{541           fm = (f(i,k-\/1) -\/ h(i,j,k)) + dsp1\_ds(i,k+1)*f(i,k+1)}
\DoxyCodeLine{542           fk = grats(i,k)*dtkd(i,k)}
\DoxyCodeLine{543           fr = sqrt(fm*fm + fk)}
\DoxyCodeLine{544 }
\DoxyCodeLine{545           \textcolor{keywordflow}{if} (fm>=0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{546             f(i,k) = min(maxf(i,k), i2p2dsp1\_ds(i,k) * (fm+fr))}
\DoxyCodeLine{547           \textcolor{keywordflow}{else}}
\DoxyCodeLine{548             f(i,k) = min(maxf(i,k), i2p2dsp1\_ds(i,k) * (fk / (-\/1.0*fm+fr)))}
\DoxyCodeLine{549 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{550 }
\DoxyCodeLine{551           \textcolor{keywordflow}{if} ((f(i,k) >= maxf(i,k)) .or. (fr == 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{552             dfdfm(i,k) = 0.0}
\DoxyCodeLine{553           \textcolor{keywordflow}{else}}
\DoxyCodeLine{554             dfdfm(i,k) = i2p2dsp1\_ds(i,k) * ((fr + fm) / fr)}
\DoxyCodeLine{555 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{556 }
\DoxyCodeLine{557           \textcolor{keywordflow}{if} (k > k2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{558             \textcolor{comment}{! This is part of a tridiagonal solver for the actual flux.}}
\DoxyCodeLine{559             c1(i,k) = dfdfm(i,k-\/1)*(dsp1\_ds(i,k)*b1(i))}
\DoxyCodeLine{560             b1(i) = 1.0 / (1.0 -\/ c1(i,k)*dfdfm(i,k))}
\DoxyCodeLine{561             f(i,k) = min(b1(i)*(f(i,k)-\/fprev(i,k)) + fprev(i,k), maxf(i,k))}
\DoxyCodeLine{562             \textcolor{keywordflow}{if} (f(i,k) >= maxf(i,k)) dfdfm(i,k) = 0.0}
\DoxyCodeLine{563 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{564 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{565 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{566 }
\DoxyCodeLine{567       \textcolor{keywordflow}{do} k=nz-\/2,kb\_min\_act,-\/1 ; \textcolor{keywordflow}{do} i=is1,ie1}
\DoxyCodeLine{568         \textcolor{keywordflow}{if} (do\_i(i) .and. (k > kb(i))) \&}
\DoxyCodeLine{569           f(i,k) = min((f(i,k)+c1(i,k+1)*(f(i,k+1)-\/fprev(i,k+1))),maxf(i,k))}
\DoxyCodeLine{570 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{571 }
\DoxyCodeLine{572       \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{573         \textcolor{keywordflow}{do} i=is1,ie1}
\DoxyCodeLine{574           \textcolor{keywordflow}{if} (do\_i(i) .and. (kb(i) < nz)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{575             \textcolor{comment}{! F will be increased to minF later.}}
\DoxyCodeLine{576             ea\_kbp1(i) = dsp1\_ds(i,kb(i)+1)*max(f(i,kb(i)+1), minf(i,kb(i)+1))}
\DoxyCodeLine{577           \textcolor{keywordflow}{else}}
\DoxyCodeLine{578             ea\_kbp1(i) = 0.0}
\DoxyCodeLine{579 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{580 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{581         \textcolor{keyword}{call }determine\_ea\_kb(h\_bl, dtkd\_kb, sref, i\_dskbp1, ent\_bl, ea\_kbp1, min\_eakb, \&}
\DoxyCodeLine{582                              max\_eakb, kmb, is1, ie1, do\_i, g, gv, cs, eakb, f\_kb=f\_kb, \&}
\DoxyCodeLine{583                              err\_max\_eakb0=err\_max\_eakb0, err\_min\_eakb0=err\_min\_eakb0, \&}
\DoxyCodeLine{584                              dfdfm\_kb=dfdfm\_kb)}
\DoxyCodeLine{585         \textcolor{keywordflow}{do} i=is1,ie1}
\DoxyCodeLine{586           \textcolor{keywordflow}{if} (do\_i(i) .and. (kb(i) < nz)) f(i,kb(i)) = f\_kb(i)}
\DoxyCodeLine{587 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{588 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{589 }
\DoxyCodeLine{590 \textcolor{comment}{! Determine whether to do another iteration.}}
\DoxyCodeLine{591       \textcolor{keywordflow}{if} (it < cs\%max\_ent\_it-\/1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{592 }
\DoxyCodeLine{593         reiterate = .false.}
\DoxyCodeLine{594         \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is1,ie1 ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{595           eb\_kmb(i) = max(2.0*ent\_bl(i,kmb+1) -\/ eakb(i), 0.0)}
\DoxyCodeLine{596 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{597         \textcolor{keywordflow}{do} i=is1,ie1}
\DoxyCodeLine{598           did\_i(i) = do\_i(i) ; do\_i(i) = .false.}
\DoxyCodeLine{599 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{600         \textcolor{keywordflow}{do} k=kb\_min\_act,nz-\/1 ; \textcolor{keywordflow}{do} i=is1,ie1}
\DoxyCodeLine{601           \textcolor{keywordflow}{if} (did\_i(i) .and. (k >= kb(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{602             \textcolor{keywordflow}{if} (f(i,k) < minf(i,k)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{603               f(i,k) = minf(i,k)}
\DoxyCodeLine{604               do\_i(i) = .true. ; reiterate = .true.}
\DoxyCodeLine{605             \textcolor{keywordflow}{elseif} (k > kb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{606               \textcolor{keywordflow}{if} ((abs(f(i,k) -\/ fprev(i,k)) > tolerance) .or. \&}
\DoxyCodeLine{607                   ((h(i,j,k) + ((1.0+dsp1\_ds(i,k))*f(i,k) -\/ \&}
\DoxyCodeLine{608                    (f(i,k-\/1) + dsp1\_ds(i,k+1)*f(i,k+1)))) < 0.9*angstrom)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{609                 do\_i(i) = .true. ; reiterate = .true.}
\DoxyCodeLine{610 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{611             \textcolor{keywordflow}{else} \textcolor{comment}{! (k == kb(i))}}
\DoxyCodeLine{612 \textcolor{comment}{! A more complicated test is required for the layer beneath the buffer layer,}}
\DoxyCodeLine{613 \textcolor{comment}{! since its flux may be partially used to entrain directly from the mixed layer.}}
\DoxyCodeLine{614 \textcolor{comment}{! Negative fluxes should not occur with the bulk mixed layer.}}
\DoxyCodeLine{615               \textcolor{keywordflow}{if} (h(i,j,k) + ((f(i,k) + eakb(i)) -\/ \&}
\DoxyCodeLine{616                   (eb\_kmb(i) + dsp1\_ds(i,k+1)*f(i,k+1))) < 0.9*angstrom) \textcolor{keywordflow}{then}}
\DoxyCodeLine{617                 do\_i(i) = .true. ; reiterate = .true.}
\DoxyCodeLine{618 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{619 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{620 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{621 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{622         \textcolor{keywordflow}{if} (.not.reiterate) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{623 \textcolor{keywordflow}{      endif} \textcolor{comment}{! end of if (it < CS\%max\_ent\_it-\/1)}}
\DoxyCodeLine{624 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! end of it loop}}
\DoxyCodeLine{625 \textcolor{comment}{! This is the end of the section that might be iterated.}}
\DoxyCodeLine{626 }
\DoxyCodeLine{627 }
\DoxyCodeLine{628     \textcolor{keywordflow}{if} (it == (cs\%max\_ent\_it)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{629       \textcolor{comment}{!   Limit the flux so that the layer below is not depleted.}}
\DoxyCodeLine{630       \textcolor{comment}{! This should only be applied to the last iteration.}}
\DoxyCodeLine{631       \textcolor{keywordflow}{do} i=is1,ie1 ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{632         f(i,nz-\/1) = max(f(i,nz-\/1), min(minf(i,nz-\/1), 0.0))}
\DoxyCodeLine{633         \textcolor{keywordflow}{if} (kb(i) >= nz-\/1) \textcolor{keywordflow}{then} ; ea\_kbp1(i) = 0.0 ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{634 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{635       \textcolor{keywordflow}{do} k=nz-\/2,kb\_min\_act,-\/1 ; \textcolor{keywordflow}{do} i=is1,ie1 ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{636         \textcolor{keywordflow}{if} (k>kb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{637           f(i,k) = min(max(minf(i,k),f(i,k)), (dsp1\_ds(i,k+1)*f(i,k+1) + \&}
\DoxyCodeLine{638                max(((f(i,k+1)-\/dsp1\_ds(i,k+2)*f(i,k+2)) + \&}
\DoxyCodeLine{639                     (h(i,j,k+1) -\/ angstrom)), 0.5*(h(i,j,k+1)-\/angstrom))))}
\DoxyCodeLine{640         \textcolor{keywordflow}{elseif} (k==kb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{641           ea\_kbp1(i) = dsp1\_ds(i,k+1)*f(i,k+1)}
\DoxyCodeLine{642           h\_avail = dsp1\_ds(i,k+1)*f(i,k+1) + max(0.5*(h(i,j,k+1)-\/angstrom), \&}
\DoxyCodeLine{643                ((f(i,k+1)-\/dsp1\_ds(i,k+2)*f(i,k+2)) + (h(i,j,k+1) -\/ angstrom)))}
\DoxyCodeLine{644           \textcolor{keywordflow}{if} ((f(i,k) > 0.0) .and. (f(i,k) > h\_avail)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{645             f\_kb(i) = max(0.0, h\_avail)}
\DoxyCodeLine{646             f(i,k) = f\_kb(i)}
\DoxyCodeLine{647             \textcolor{keywordflow}{if} ((f\_kb(i) < maxf\_kb(i)) .and. (eakb\_maxf(i) <= eakb(i))) \&}
\DoxyCodeLine{648               eakb(i) = eakb\_maxf(i)}
\DoxyCodeLine{649             \textcolor{keyword}{call }f\_kb\_to\_ea\_kb(h\_bl, sref, ent\_bl, i\_dskbp1, f\_kb, kmb, i, \&}
\DoxyCodeLine{650                                g, gv, cs, eakb)}
\DoxyCodeLine{651 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{652 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{653 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{654 }
\DoxyCodeLine{655 }
\DoxyCodeLine{656       \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is1,ie1}
\DoxyCodeLine{657         \textcolor{keywordflow}{if} (do\_i(i) .and. (kb(i) < nz)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{658           h\_avail = eakb(i) + max(0.5*(h\_bl(i,kmb+1)-\/angstrom), \&}
\DoxyCodeLine{659                 (f\_kb(i)-\/ea\_kbp1(i)) + (h\_bl(i,kmb+1)-\/angstrom))}
\DoxyCodeLine{660           \textcolor{comment}{! Ensure that 0 < eb\_kmb < h\_avail.}}
\DoxyCodeLine{661           ent\_bl(i,kmb+1) = min(ent\_bl(i,kmb+1),0.5*(eakb(i) + h\_avail))}
\DoxyCodeLine{662 }
\DoxyCodeLine{663           eb\_kmb(i) = max(2.0*ent\_bl(i,kmb+1) -\/ eakb(i), 0.0)}
\DoxyCodeLine{664 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{665 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{666 }
\DoxyCodeLine{667      \textcolor{comment}{!  Limit the flux so that the layer above is not depleted.}}
\DoxyCodeLine{668       \textcolor{keywordflow}{do} k=kb\_min\_act+1,nz-\/1 ; \textcolor{keywordflow}{do} i=is1,ie1 ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{669         \textcolor{keywordflow}{if} ((.not.cs\%bulkmixedlayer) .or. (k > kb(i)+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{670           f(i,k) = min(f(i,k), ds\_dsp1(i,k)*( ((f(i,k-\/1) + \&}
\DoxyCodeLine{671               dsp1\_ds(i,k-\/1)*f(i,k-\/1)) -\/ f(i,k-\/2)) + (h(i,j,k-\/1) -\/ angstrom)))}
\DoxyCodeLine{672           f(i,k) = max(f(i,k),min(minf(i,k),0.0))}
\DoxyCodeLine{673         \textcolor{keywordflow}{elseif} (k == kb(i)+1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{674           f(i,k) = min(f(i,k), ds\_dsp1(i,k)*( ((f(i,k-\/1) + eakb(i)) -\/ \&}
\DoxyCodeLine{675               eb\_kmb(i)) + (h(i,j,k-\/1) -\/ angstrom)))}
\DoxyCodeLine{676           f(i,k) = max(f(i,k),min(minf(i,k),0.0))}
\DoxyCodeLine{677 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{678 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{679 \textcolor{keywordflow}{    endif} \textcolor{comment}{! (it == (CS\%max\_ent\_it))}}
\DoxyCodeLine{680 }
\DoxyCodeLine{681     \textcolor{keyword}{call }f\_to\_ent(f, h, kb, kmb, j, g, gv, cs, dsp1\_ds, eakb, ent\_bl, ea, eb)}
\DoxyCodeLine{682 }
\DoxyCodeLine{683     \textcolor{comment}{!   Calculate the layer thicknesses after the entrainment to constrain the}}
\DoxyCodeLine{684     \textcolor{comment}{! corrective fluxes.}}
\DoxyCodeLine{685     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%eqn\_of\_state)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{686       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{687         h\_guess(i,1) = (h(i,j,1) -\/ angstrom) + (eb(i,j,1) -\/ ea(i,j,2))}
\DoxyCodeLine{688         h\_guess(i,nz) = (h(i,j,nz) -\/ angstrom) + (ea(i,j,nz) -\/ eb(i,j,nz-\/1))}
\DoxyCodeLine{689         \textcolor{keywordflow}{if} (h\_guess(i,1) < 0.0) h\_guess(i,1) = 0.0}
\DoxyCodeLine{690         \textcolor{keywordflow}{if} (h\_guess(i,nz) < 0.0) h\_guess(i,nz) = 0.0}
\DoxyCodeLine{691 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{692       \textcolor{keywordflow}{do} k=2,nz-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{693         h\_guess(i,k) = (h(i,j,k) -\/ angstrom) + ((ea(i,j,k) -\/ eb(i,j,k-\/1)) + \&}
\DoxyCodeLine{694                    (eb(i,j,k) -\/ ea(i,j,k+1)))}
\DoxyCodeLine{695         \textcolor{keywordflow}{if} (h\_guess(i,k) < 0.0) h\_guess(i,k) = 0.0}
\DoxyCodeLine{696 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{697       \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{698         \textcolor{keyword}{call }determine\_dskb(h\_bl, sref, ent\_bl, eakb, is, ie, kmb, g, gv, \&}
\DoxyCodeLine{699                             .true., ds\_kb, ds\_anom\_lim=ds\_anom\_lim)}
\DoxyCodeLine{700         \textcolor{keywordflow}{do} k=nz-\/1,kb\_min,-\/1}
\DoxyCodeLine{701           \textcolor{keyword}{call }calculate\_density(tv\%T(:,j,k), tv\%S(:,j,k), pres, rcv, tv\%eqn\_of\_state, eosdom)}
\DoxyCodeLine{702           \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{703             \textcolor{keywordflow}{if} ((k>kb(i)) .and. (f(i,k) > 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{704               \textcolor{comment}{! Within a time step, a layer may entrain no more than its}}
\DoxyCodeLine{705               \textcolor{comment}{! thickness for correction.  This limitation should apply}}
\DoxyCodeLine{706               \textcolor{comment}{! extremely rarely, but precludes undesirable behavior.}}
\DoxyCodeLine{707               \textcolor{comment}{!  Note: Corrected a sign/logic error \& factor of 2 error, and}}
\DoxyCodeLine{708               \textcolor{comment}{!    the layers tracked the target density better, mostly due to}}
\DoxyCodeLine{709               \textcolor{comment}{!    the factor of 2 error.}}
\DoxyCodeLine{710               f\_cor = h(i,j,k) * min(1.0 , max(-\/ds\_dsp1(i,k), \&}
\DoxyCodeLine{711                           (gv\%Rlay(k) -\/ rcv(i)) / (gv\%Rlay(k+1)-\/gv\%Rlay(k))) )}
\DoxyCodeLine{712 }
\DoxyCodeLine{713               \textcolor{comment}{! Ensure that (1) Entrainments are positive, (2) Corrections in}}
\DoxyCodeLine{714               \textcolor{comment}{! a layer cannot deplete the layer itself (very generously), and}}
\DoxyCodeLine{715               \textcolor{comment}{! (3) a layer can take no more than a quarter the mass of its}}
\DoxyCodeLine{716               \textcolor{comment}{! neighbor.}}
\DoxyCodeLine{717               \textcolor{keywordflow}{if} (f\_cor > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{718                 f\_cor = min(f\_cor, 0.9*f(i,k), ds\_dsp1(i,k)*0.5*h\_guess(i,k), \&}
\DoxyCodeLine{719                             0.25*h\_guess(i,k+1))}
\DoxyCodeLine{720               \textcolor{keywordflow}{else}}
\DoxyCodeLine{721                 f\_cor = -\/min(-\/f\_cor, 0.9*f(i,k), 0.5*h\_guess(i,k), \&}
\DoxyCodeLine{722                              0.25*ds\_dsp1(i,k)*h\_guess(i,k-\/1) )}
\DoxyCodeLine{723 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{724 }
\DoxyCodeLine{725               ea(i,j,k) = ea(i,j,k) -\/ dsp1\_ds(i,k)*f\_cor}
\DoxyCodeLine{726               eb(i,j,k) = eb(i,j,k) + f\_cor}
\DoxyCodeLine{727             \textcolor{keywordflow}{elseif} ((k==kb(i)) .and. (f(i,k) > 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{728               \textcolor{comment}{!   Rho\_cor is the density anomaly that needs to be corrected,}}
\DoxyCodeLine{729               \textcolor{comment}{! taking into account that the true potential density of the}}
\DoxyCodeLine{730               \textcolor{comment}{! deepest buffer layer is not exactly what is returned as dS\_kb.}}
\DoxyCodeLine{731               ds\_kb\_eff = 2.0*ds\_kb(i) -\/ ds\_anom\_lim(i) \textcolor{comment}{! Could be negative!!!}}
\DoxyCodeLine{732               rho\_cor = h(i,j,k) * (gv\%Rlay(k)-\/rcv(i)) + eakb(i)*ds\_anom\_lim(i)}
\DoxyCodeLine{733 }
\DoxyCodeLine{734               \textcolor{comment}{! Ensure that  -\/.9*eakb < ea\_cor < .9*eakb}}
\DoxyCodeLine{735               \textcolor{keywordflow}{if} (abs(rho\_cor) < abs(0.9*eakb(i)*ds\_kb\_eff)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{736                 ea\_cor = -\/rho\_cor / ds\_kb\_eff}
\DoxyCodeLine{737               \textcolor{keywordflow}{else}}
\DoxyCodeLine{738                 ea\_cor = sign(0.9*eakb(i),-\/rho\_cor*ds\_kb\_eff)}
\DoxyCodeLine{739 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{740 }
\DoxyCodeLine{741               \textcolor{keywordflow}{if} (ea\_cor > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{742                 \textcolor{comment}{! Ensure that -\/F\_cor < 0.5*h\_guess}}
\DoxyCodeLine{743                 ea\_cor = min(ea\_cor, 0.5*(max\_eakb(i) -\/ eakb(i)), \&}
\DoxyCodeLine{744                              0.5*h\_guess(i,k) / (ds\_kb(i) * i\_dskbp1(i)))}
\DoxyCodeLine{745               \textcolor{keywordflow}{else}}
\DoxyCodeLine{746                 \textcolor{comment}{! Ensure that -\/ea\_cor < 0.5*h\_guess \& F\_cor < 0.25*h\_guess(k+1)}}
\DoxyCodeLine{747                 ea\_cor = -\/min(-\/ea\_cor, 0.5*h\_guess(i,k), \&}
\DoxyCodeLine{748                               0.25*h\_guess(i,k+1) / (ds\_kb(i) * i\_dskbp1(i)))}
\DoxyCodeLine{749 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{750 }
\DoxyCodeLine{751               ea(i,j,k) = ea(i,j,k) + ea\_cor}
\DoxyCodeLine{752               eb(i,j,k) = eb(i,j,k) -\/ (ds\_kb(i) * i\_dskbp1(i)) * ea\_cor}
\DoxyCodeLine{753             \textcolor{keywordflow}{elseif} (k < kb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{754               \textcolor{comment}{! Repetative, unless ea(kb) has been corrected.}}
\DoxyCodeLine{755               ea(i,j,k) = ea(i,j,k+1)}
\DoxyCodeLine{756 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{757 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{758 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{759         \textcolor{keywordflow}{do} k=kb\_min-\/1,k2,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{760           ea(i,j,k) = ea(i,j,k+1)}
\DoxyCodeLine{761 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{762 }
\DoxyCodeLine{763         \textcolor{comment}{! Repetative, unless ea(kb) has been corrected.}}
\DoxyCodeLine{764         k=kmb}
\DoxyCodeLine{765         \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{766           \textcolor{comment}{! Do not adjust eb through the base of the buffer layers, but it}}
\DoxyCodeLine{767           \textcolor{comment}{! may be necessary to change entrainment from above.}}
\DoxyCodeLine{768           h1 = (h(i,j,k) -\/ angstrom) + (eb(i,j,k) -\/ ea(i,j,k+1))}
\DoxyCodeLine{769           ea(i,j,k) = max(ent\_bl(i,k), ent\_bl(i,k)-\/0.5*h1, -\/h1)}
\DoxyCodeLine{770 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{771         \textcolor{keywordflow}{do} k=kmb-\/1,2,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{772           \textcolor{comment}{! Determine the entrainment from below for each buffer layer.}}
\DoxyCodeLine{773           eb(i,j,k) = max(2.0*ent\_bl(i,k+1) -\/ ea(i,j,k+1), 0.0)}
\DoxyCodeLine{774 }
\DoxyCodeLine{775           \textcolor{comment}{! Determine the entrainment from above for each buffer layer.}}
\DoxyCodeLine{776           h1 = (h(i,j,k) -\/ angstrom) + (eb(i,j,k) -\/ ea(i,j,k+1))}
\DoxyCodeLine{777           ea(i,j,k) = max(ent\_bl(i,k), ent\_bl(i,k)-\/0.5*h1, -\/h1)}
\DoxyCodeLine{778 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{779         \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{780           eb(i,j,1) = max(2.0*ent\_bl(i,2) -\/ ea(i,j,2), 0.0)}
\DoxyCodeLine{781 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{782 }
\DoxyCodeLine{783       \textcolor{keywordflow}{else} \textcolor{comment}{! not bulkmixedlayer}}
\DoxyCodeLine{784         \textcolor{keywordflow}{do} k=k2,nz-\/1;}
\DoxyCodeLine{785           \textcolor{keyword}{call }calculate\_density(tv\%T(:,j,k), tv\%S(:,j,k), pres, rcv, tv\%eqn\_of\_state, eosdom)}
\DoxyCodeLine{786           \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (f(i,k) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{787             \textcolor{comment}{! Within a time step, a layer may entrain no more than}}
\DoxyCodeLine{788             \textcolor{comment}{! its thickness for correction.  This limitation should}}
\DoxyCodeLine{789             \textcolor{comment}{! apply extremely rarely, but precludes undesirable}}
\DoxyCodeLine{790             \textcolor{comment}{! behavior.}}
\DoxyCodeLine{791             f\_cor = h(i,j,k) * min(dsp1\_ds(i,k) , max(-\/1.0, \&}
\DoxyCodeLine{792                        (gv\%Rlay(k) -\/ rcv(i)) / (gv\%Rlay(k+1)-\/gv\%Rlay(k))) )}
\DoxyCodeLine{793 }
\DoxyCodeLine{794             \textcolor{comment}{! Ensure that (1) Entrainments are positive, (2) Corrections in}}
\DoxyCodeLine{795             \textcolor{comment}{! a layer cannot deplete the layer itself (very generously), and}}
\DoxyCodeLine{796             \textcolor{comment}{! (3) a layer can take no more than a quarter the mass of its}}
\DoxyCodeLine{797             \textcolor{comment}{! neighbor.}}
\DoxyCodeLine{798             \textcolor{keywordflow}{if} (f\_cor >= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{799               f\_cor = min(f\_cor, 0.9*f(i,k), 0.5*dsp1\_ds(i,k)*h\_guess(i,k), \&}
\DoxyCodeLine{800                           0.25*h\_guess(i,k+1))}
\DoxyCodeLine{801             \textcolor{keywordflow}{else}}
\DoxyCodeLine{802               f\_cor = -\/min(-\/f\_cor, 0.9*f(i,k), 0.5*h\_guess(i,k), \&}
\DoxyCodeLine{803                            0.25*ds\_dsp1(i,k)*h\_guess(i,k-\/1) )}
\DoxyCodeLine{804 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{805 }
\DoxyCodeLine{806             ea(i,j,k) = ea(i,j,k) -\/ dsp1\_ds(i,k)*f\_cor}
\DoxyCodeLine{807             eb(i,j,k) = eb(i,j,k) + f\_cor}
\DoxyCodeLine{808 \textcolor{keywordflow}{          endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{809 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{810 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{811 }
\DoxyCodeLine{812 \textcolor{keywordflow}{    endif}   \textcolor{comment}{!  associated(tv\%eqn\_of\_state))}}
\DoxyCodeLine{813 }
\DoxyCodeLine{814     \textcolor{keywordflow}{if} (cs\%id\_Kd > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{815       idt = gv\%H\_to\_Z**2 / dt}
\DoxyCodeLine{816       \textcolor{keywordflow}{do} k=2,nz-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{817         \textcolor{keywordflow}{if} (k<kb(i)) \textcolor{keywordflow}{then} ; kd\_here = 0.0 ; \textcolor{keywordflow}{else}}
\DoxyCodeLine{818           kd\_here = f(i,k) * ( h(i,j,k) + ((ea(i,j,k) -\/ eb(i,j,k-\/1)) + \&}
\DoxyCodeLine{819               (eb(i,j,k) -\/ ea(i,j,k+1))) ) / (i2p2dsp1\_ds(i,k) * grats(i,k))}
\DoxyCodeLine{820 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{821 }
\DoxyCodeLine{822         kd\_eff(i,j,k) = max(dtkd(i,k), kd\_here)*idt}
\DoxyCodeLine{823 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{824       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{825         kd\_eff(i,j,1) = dtkd(i,1)*idt}
\DoxyCodeLine{826         kd\_eff(i,j,nz) = dtkd(i,nz)*idt}
\DoxyCodeLine{827 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{828 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{829 }
\DoxyCodeLine{830     \textcolor{keywordflow}{if} (cs\%id\_diff\_work > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{831       g\_2dt = 0.5 * gv\%H\_to\_Z**2*us\%L\_to\_Z**2 * (gv\%g\_Earth / dt)}
\DoxyCodeLine{832       \textcolor{keywordflow}{do} i=is,ie ; diff\_work(i,j,1) = 0.0 ; diff\_work(i,j,nz+1) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{833       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%eqn\_of\_state)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{834         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes\%p\_surf)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{835           \textcolor{keywordflow}{do} i=is,ie ; pressure(i) = fluxes\%p\_surf(i,j) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{836         \textcolor{keywordflow}{else}}
\DoxyCodeLine{837           \textcolor{keywordflow}{do} i=is,ie ; pressure(i) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{838 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{839         \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{840           \textcolor{keywordflow}{do} i=is,ie ; pressure(i) = pressure(i) + (gv\%g\_Earth*gv\%H\_to\_RZ)*h(i,j,k-\/1) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{841           \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{842             \textcolor{keywordflow}{if} (k==kb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{843               t\_eos(i) = 0.5*(tv\%T(i,j,kmb) + tv\%T(i,j,k))}
\DoxyCodeLine{844               s\_eos(i) = 0.5*(tv\%S(i,j,kmb) + tv\%S(i,j,k))}
\DoxyCodeLine{845             \textcolor{keywordflow}{else}}
\DoxyCodeLine{846               t\_eos(i) = 0.5*(tv\%T(i,j,k-\/1) + tv\%T(i,j,k))}
\DoxyCodeLine{847               s\_eos(i) = 0.5*(tv\%S(i,j,k-\/1) + tv\%S(i,j,k))}
\DoxyCodeLine{848 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{849 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{850           \textcolor{keyword}{call }calculate\_density\_derivs(t\_eos, s\_eos, pressure, drho\_dt, drho\_ds, \&}
\DoxyCodeLine{851                                         tv\%eqn\_of\_state, eosdom)}
\DoxyCodeLine{852           \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{853             \textcolor{keywordflow}{if} ((k>kmb) .and. (k<kb(i))) \textcolor{keywordflow}{then} ; diff\_work(i,j,k) = 0.0}
\DoxyCodeLine{854             \textcolor{keywordflow}{else}}
\DoxyCodeLine{855               \textcolor{keywordflow}{if} (k==kb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{856                 drho = drho\_dt(i) * (tv\%T(i,j,k)-\/tv\%T(i,j,kmb)) + \&}
\DoxyCodeLine{857                        drho\_ds(i) * (tv\%S(i,j,k)-\/tv\%S(i,j,kmb))}
\DoxyCodeLine{858               \textcolor{keywordflow}{else}}
\DoxyCodeLine{859                 drho = drho\_dt(i) * (tv\%T(i,j,k)-\/tv\%T(i,j,k-\/1)) + \&}
\DoxyCodeLine{860                        drho\_ds(i) * (tv\%S(i,j,k)-\/tv\%S(i,j,k-\/1))}
\DoxyCodeLine{861 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{862               diff\_work(i,j,k) = g\_2dt * drho * \&}
\DoxyCodeLine{863                    (ea(i,j,k) * (h(i,j,k) + ea(i,j,k)) + \&}
\DoxyCodeLine{864                     eb(i,j,k-\/1)*(h(i,j,k-\/1) + eb(i,j,k-\/1)))}
\DoxyCodeLine{865 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{866 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{867 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{868       \textcolor{keywordflow}{else}}
\DoxyCodeLine{869         \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{870           diff\_work(i,j,k) = g\_2dt * (gv\%Rlay(k)-\/gv\%Rlay(k-\/1)) * \&}
\DoxyCodeLine{871                (ea(i,j,k) * (h(i,j,k) + ea(i,j,k)) + \&}
\DoxyCodeLine{872                 eb(i,j,k-\/1)*(h(i,j,k-\/1) + eb(i,j,k-\/1)))}
\DoxyCodeLine{873 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{874 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{875 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{876 }
\DoxyCodeLine{877     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kb\_out)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{878       \textcolor{keywordflow}{do} i=is,ie ; kb\_out(i,j) = kb(i) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{879 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{880 }
\DoxyCodeLine{881 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! end of j loop}}
\DoxyCodeLine{882 }
\DoxyCodeLine{883 \textcolor{comment}{! Offer diagnostic fields for averaging.}}
\DoxyCodeLine{884   \textcolor{keywordflow}{if} (cs\%id\_Kd > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd, kd\_eff, cs\%diag)}
\DoxyCodeLine{885   \textcolor{keywordflow}{if} (cs\%id\_Kd > 0) \textcolor{keyword}{deallocate}(kd\_eff)}
\DoxyCodeLine{886   \textcolor{keywordflow}{if} (cs\%id\_diff\_work > 0) \textcolor{keyword}{call }post\_data(cs\%id\_diff\_work, diff\_work, cs\%diag)}
\DoxyCodeLine{887   \textcolor{keywordflow}{if} (cs\%id\_diff\_work > 0) \textcolor{keyword}{deallocate}(diff\_work)}
\DoxyCodeLine{888 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__entrain__diffusive_adce1b1ba57f5245f1eda57e7a254d4df}\label{namespacemom__entrain__diffusive_adce1b1ba57f5245f1eda57e7a254d4df}} 
\index{mom\_entrain\_diffusive@{mom\_entrain\_diffusive}!f\_kb\_to\_ea\_kb@{f\_kb\_to\_ea\_kb}}
\index{f\_kb\_to\_ea\_kb@{f\_kb\_to\_ea\_kb}!mom\_entrain\_diffusive@{mom\_entrain\_diffusive}}
\doxysubsubsection{\texorpdfstring{f\_kb\_to\_ea\_kb()}{f\_kb\_to\_ea\_kb()}}
{\footnotesize\ttfamily subroutine mom\+\_\+entrain\+\_\+diffusive\+::f\+\_\+kb\+\_\+to\+\_\+ea\+\_\+kb (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h\+\_\+bl,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{Sref,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{Ent\+\_\+bl,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{I\+\_\+d\+Skbp1,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{F\+\_\+kb,  }\item[{integer, intent(in)}]{kmb,  }\item[{integer, intent(in)}]{i,  }\item[{type(\mbox{\hyperlink{structmom__grid_1_1ocean__grid__type}{ocean\+\_\+grid\+\_\+type}}), intent(in)}]{G,  }\item[{type(\mbox{\hyperlink{structmom__verticalgrid_1_1verticalgrid__type}{verticalgrid\+\_\+type}}), intent(in)}]{GV,  }\item[{type(\mbox{\hyperlink{structmom__entrain__diffusive_1_1entrain__diffusive__cs}{entrain\+\_\+diffusive\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(inout)}]{ea\+\_\+kb,  }\item[{real, intent(in), optional}]{tol\+\_\+in }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Given an entrainment from below for layer kb, determine a consistent entrainment from above, such that d\+Skb $\ast$ ea\+\_\+kb = d\+Skbp1 $\ast$ F\+\_\+kb. The input value of ea\+\_\+kb is both the maximum value that can be obtained and the first guess of the iterations. Ideally ea\+\_\+kb should be an under-\/estimate. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em h\+\_\+bl} & Layer thickness, with the top interior \\
\hline
\mbox{\texttt{ in}}  & {\em sref} & The coordinate reference potential density, \\
\hline
\mbox{\texttt{ in}}  & {\em ent\+\_\+bl} & The average entrainment upward and downward \\
\hline
\mbox{\texttt{ in}}  & {\em i\+\_\+dskbp1} & The inverse of the difference in reference potential density across the base of the uppermost interior layer \mbox{[}R-\/1 $\sim$$>$ m3 kg-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em f\+\_\+kb} & The entrainment from below by the uppermost interior layer \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em kmb} & The number of mixed and buffer layers. \\
\hline
\mbox{\texttt{ in}}  & {\em i} & The i-\/index to work on \\
\hline
 & {\em cs} & This module\textquotesingle{}s control structure. \\
\hline
\mbox{\texttt{ in,out}}  & {\em ea\+\_\+kb} & The entrainment from above by the layer below the buffer layer (i.\+e. layer kb) \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em tol\+\_\+in} & A tolerance for the iterative determination of the entrainment \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 1439 of file M\+O\+M\+\_\+entrain\+\_\+diffusive.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1441   \textcolor{keywordtype}{type}(ocean\_grid\_type),    \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure}}
\DoxyCodeLine{1442   \textcolor{keywordtype}{type}(verticalGrid\_type),  \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure}}
\DoxyCodeLine{1443 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1444                             \textcolor{keywordtype}{intent(in)}    :: h\_bl\textcolor{comment}{ !< Layer thickness, with the top interior}}
\DoxyCodeLine{1445 \textcolor{comment}{                                                  !! layer at k-\/index kmb+1 [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1446 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1447                             \textcolor{keywordtype}{intent(in)}    :: Sref\textcolor{comment}{ !< The coordinate reference potential density,}}
\DoxyCodeLine{1448 \textcolor{comment}{                                                  !! with the value of the topmost interior layer}}
\DoxyCodeLine{1449 \textcolor{comment}{                                                  !! at index kmb+1 [R \string~> kg m-\/3].}}
\DoxyCodeLine{1450 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1451                             \textcolor{keywordtype}{intent(in)}    :: Ent\_bl\textcolor{comment}{ !< The average entrainment upward and downward}}
\DoxyCodeLine{1452 \textcolor{comment}{                                                  !! across each interface around the buffer layers,}}
\DoxyCodeLine{1453 \textcolor{comment}{                                                  !! [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1454 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: I\_dSkbp1\textcolor{comment}{ !< The inverse of the difference in reference}}
\DoxyCodeLine{1455 \textcolor{comment}{                                                  !! potential density across the base of the}}
\DoxyCodeLine{1456 \textcolor{comment}{                                                  !! uppermost interior layer [R-\/1 \string~> m3 kg-\/1].}}
\DoxyCodeLine{1457 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(in)}    :: F\_kb\textcolor{comment}{ !< The entrainment from below by the}}
\DoxyCodeLine{1458 \textcolor{comment}{                                                  !! uppermost interior layer [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{1459   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}    :: kmb\textcolor{comment}{  !< The number of mixed and buffer layers.}}
\DoxyCodeLine{1460   \textcolor{keywordtype}{integer},                  \textcolor{keywordtype}{intent(in)}    :: i\textcolor{comment}{    !< The i-\/index to work on}}
\DoxyCodeLine{1461   \textcolor{keywordtype}{type}(entrain\_diffusive\_CS), \textcolor{keywordtype}{pointer}     :: CS\textcolor{comment}{   !< This module's control structure.}}
\DoxyCodeLine{1462 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \textcolor{keywordtype}{intent(inout)} :: ea\_kb\textcolor{comment}{ !< The entrainment from above by the layer below}}
\DoxyCodeLine{1463 \textcolor{comment}{                                                  !! the buffer layer (i.e. layer kb) [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1464 \textcolor{keywordtype}{  real},           \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: tol\_in\textcolor{comment}{ !< A tolerance for the iterative determination}}
\DoxyCodeLine{1465 \textcolor{comment}{                                                  !! of the entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1466 }
\DoxyCodeLine{1467 \textcolor{keywordtype}{  real} :: max\_ea, min\_ea}
\DoxyCodeLine{1468 \textcolor{keywordtype}{  real} :: err, err\_min, err\_max}
\DoxyCodeLine{1469 \textcolor{keywordtype}{  real} :: derr\_dea}
\DoxyCodeLine{1470 \textcolor{keywordtype}{  real} :: val, tolerance, tol1}
\DoxyCodeLine{1471 \textcolor{keywordtype}{  real} :: ea\_prev}
\DoxyCodeLine{1472 \textcolor{keywordtype}{  real} :: dS\_kbp1}
\DoxyCodeLine{1473   \textcolor{keywordtype}{logical} :: bisect\_next, Newton}
\DoxyCodeLine{1474 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: dS\_kb}
\DoxyCodeLine{1475 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: maxF, ent\_maxF, zeros}
\DoxyCodeLine{1476 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: ddSkb\_dE}
\DoxyCodeLine{1477   \textcolor{keywordtype}{integer} :: it}
\DoxyCodeLine{1478   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{parameter} :: MAXIT = 30}
\DoxyCodeLine{1479 }
\DoxyCodeLine{1480   ds\_kbp1 = sref(i,kmb+2) -\/ sref(i,kmb+1)}
\DoxyCodeLine{1481   max\_ea = ea\_kb(i) ; min\_ea = 0.0}
\DoxyCodeLine{1482   val = ds\_kbp1 * f\_kb(i)}
\DoxyCodeLine{1483   err\_min = -\/val}
\DoxyCodeLine{1484 }
\DoxyCodeLine{1485   tolerance = cs\%Tolerance\_Ent}
\DoxyCodeLine{1486   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(tol\_in)) tolerance = tol\_in}
\DoxyCodeLine{1487   bisect\_next = .true.}
\DoxyCodeLine{1488 }
\DoxyCodeLine{1489   \textcolor{keyword}{call }determine\_dskb(h\_bl, sref, ent\_bl, ea\_kb, i, i, kmb, g, gv, .true., \&}
\DoxyCodeLine{1490                       ds\_kb, ddskb\_de)}
\DoxyCodeLine{1491 }
\DoxyCodeLine{1492   err = ds\_kb(i) * ea\_kb(i) -\/ val}
\DoxyCodeLine{1493   derr\_dea = ds\_kb(i) + ddskb\_de(i) * ea\_kb(i)}
\DoxyCodeLine{1494   \textcolor{comment}{! Return if Newton's method on the first guess would give a tolerably small}}
\DoxyCodeLine{1495   \textcolor{comment}{! change in the value of ea\_kb.}}
\DoxyCodeLine{1496   \textcolor{keywordflow}{if} ((err <= 0.0) .and. (abs(err) <= tolerance*abs(derr\_dea))) \textcolor{keywordflow}{return}}
\DoxyCodeLine{1497 }
\DoxyCodeLine{1498   \textcolor{keywordflow}{if} (err == 0.0) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{return} \textcolor{comment}{! The exact solution on the first guess...}}
\DoxyCodeLine{1499   \textcolor{keywordflow}{elseif} (err > 0.0) \textcolor{keywordflow}{then} \textcolor{comment}{! The root is properly bracketed.}}
\DoxyCodeLine{1500     max\_ea = ea\_kb(i) ; err\_max = err}
\DoxyCodeLine{1501     \textcolor{comment}{!   Use Newton's method (if it stays bounded) or the false position method}}
\DoxyCodeLine{1502     \textcolor{comment}{! to find the next value.}}
\DoxyCodeLine{1503     \textcolor{keywordflow}{if} ((derr\_dea > 0.0) .and. (derr\_dea*(ea\_kb(i) -\/ min\_ea) > err) .and. \&}
\DoxyCodeLine{1504         (derr\_dea*(max\_ea -\/ ea\_kb(i)) > -\/1.0*err)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1505       ea\_kb(i) = ea\_kb(i) -\/ err / derr\_dea}
\DoxyCodeLine{1506     \textcolor{keywordflow}{else} \textcolor{comment}{! Use the bisection for the next guess.}}
\DoxyCodeLine{1507       ea\_kb(i) = 0.5*(max\_ea+min\_ea)}
\DoxyCodeLine{1508 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1509   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1510     \textcolor{comment}{!   Try to bracket the root first.  If unable to bracket the root, return}}
\DoxyCodeLine{1511     \textcolor{comment}{! the maximum.}}
\DoxyCodeLine{1512     zeros(i) = 0.0}
\DoxyCodeLine{1513     \textcolor{keyword}{call }find\_maxf\_kb(h\_bl, sref, ent\_bl, i\_dskbp1, zeros, ea\_kb, \&}
\DoxyCodeLine{1514                       kmb, i, i, g, gv, cs, maxf, ent\_maxf, f\_thresh = f\_kb)}
\DoxyCodeLine{1515     err\_max = ds\_kbp1 * maxf(i) -\/ val}
\DoxyCodeLine{1516     \textcolor{comment}{! If err\_max is negative, there is no good solution, so use the maximum}}
\DoxyCodeLine{1517     \textcolor{comment}{! value of F in the valid range.}}
\DoxyCodeLine{1518     \textcolor{keywordflow}{if} (err\_max <= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1519       ea\_kb(i) = ent\_maxf(i) ; \textcolor{keywordflow}{return}}
\DoxyCodeLine{1520     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1521       max\_ea = ent\_maxf(i)}
\DoxyCodeLine{1522       ea\_kb(i) = 0.5*(max\_ea+min\_ea) \textcolor{comment}{! Use bisection for the next guess.}}
\DoxyCodeLine{1523 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1524 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1525 }
\DoxyCodeLine{1526   \textcolor{comment}{! Exit if the range between max\_ea and min\_ea already acceptable.}}
\DoxyCodeLine{1527   \textcolor{comment}{! if (abs(max\_ea -\/ min\_ea) < 0.1*tolerance) return}}
\DoxyCodeLine{1528 }
\DoxyCodeLine{1529   \textcolor{keywordflow}{do} it = 1, maxit}
\DoxyCodeLine{1530     \textcolor{keyword}{call }determine\_dskb(h\_bl, sref, ent\_bl, ea\_kb, i, i, kmb, g, gv, .true., \&}
\DoxyCodeLine{1531                         ds\_kb, ddskb\_de)}
\DoxyCodeLine{1532 }
\DoxyCodeLine{1533     err = ds\_kb(i) * ea\_kb(i) -\/ val}
\DoxyCodeLine{1534     derr\_dea = ds\_kb(i) + ddskb\_de(i) * ea\_kb(i)}
\DoxyCodeLine{1535 }
\DoxyCodeLine{1536     ea\_prev = ea\_kb(i)}
\DoxyCodeLine{1537     \textcolor{comment}{! Use Newton's method or the false position method to find the next value.}}
\DoxyCodeLine{1538     newton = .false.}
\DoxyCodeLine{1539     \textcolor{keywordflow}{if} (err > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1540       max\_ea = ea\_kb(i) ; err\_max = err}
\DoxyCodeLine{1541       \textcolor{keywordflow}{if} ((derr\_dea > 0.0) .and. (derr\_dea*(ea\_kb(i)-\/min\_ea) > err)) newton = .true.}
\DoxyCodeLine{1542     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1543       min\_ea = ea\_kb(i) ; err\_min = err}
\DoxyCodeLine{1544       \textcolor{keywordflow}{if} ((derr\_dea > 0.0) .and. (derr\_dea*(ea\_kb(i)-\/max\_ea) < err)) newton = .true.}
\DoxyCodeLine{1545 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1546 }
\DoxyCodeLine{1547     \textcolor{keywordflow}{if} (newton) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1548       ea\_kb(i) = ea\_kb(i) -\/ err / derr\_dea}
\DoxyCodeLine{1549     \textcolor{keywordflow}{elseif} (bisect\_next) \textcolor{keywordflow}{then} \textcolor{comment}{! Use bisection to reduce the range.}}
\DoxyCodeLine{1550       ea\_kb(i) = 0.5*(max\_ea+min\_ea)}
\DoxyCodeLine{1551       bisect\_next = .false.}
\DoxyCodeLine{1552     \textcolor{keywordflow}{else}  \textcolor{comment}{! Use the false-\/position method for the next guess.}}
\DoxyCodeLine{1553       ea\_kb(i) = min\_ea + (max\_ea-\/min\_ea) * (err\_min/(err\_min -\/ err\_max))}
\DoxyCodeLine{1554       bisect\_next = .true.}
\DoxyCodeLine{1555 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1556 }
\DoxyCodeLine{1557     tol1 = tolerance ; \textcolor{keywordflow}{if} (err > 0.0) tol1 = 0.099*tolerance}
\DoxyCodeLine{1558     \textcolor{keywordflow}{if} (ds\_kb(i) <= ds\_kbp1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1559       \textcolor{keywordflow}{if} (abs(ea\_kb(i) -\/ ea\_prev) <= tol1) \textcolor{keywordflow}{return}}
\DoxyCodeLine{1560     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1561       \textcolor{keywordflow}{if} (ds\_kbp1*abs(ea\_kb(i) -\/ ea\_prev) <= ds\_kb(i)*tol1) \textcolor{keywordflow}{return}}
\DoxyCodeLine{1562 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1563 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1564 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__entrain__diffusive_a13bbe9f35e3198470100375d9d016b7a}\label{namespacemom__entrain__diffusive_a13bbe9f35e3198470100375d9d016b7a}} 
\index{mom\_entrain\_diffusive@{mom\_entrain\_diffusive}!f\_to\_ent@{f\_to\_ent}}
\index{f\_to\_ent@{f\_to\_ent}!mom\_entrain\_diffusive@{mom\_entrain\_diffusive}}
\doxysubsubsection{\texorpdfstring{f\_to\_ent()}{f\_to\_ent()}}
{\footnotesize\ttfamily subroutine mom\+\_\+entrain\+\_\+diffusive\+::f\+\_\+to\+\_\+ent (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{F,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{integer, dimension(szi\+\_\+(g)), intent(in)}]{kb,  }\item[{integer, intent(in)}]{kmb,  }\item[{integer, intent(in)}]{j,  }\item[{type(\mbox{\hyperlink{structmom__grid_1_1ocean__grid__type}{ocean\+\_\+grid\+\_\+type}}), intent(in)}]{G,  }\item[{type(\mbox{\hyperlink{structmom__verticalgrid_1_1verticalgrid__type}{verticalgrid\+\_\+type}}), intent(in)}]{GV,  }\item[{type(\mbox{\hyperlink{structmom__entrain__diffusive_1_1entrain__diffusive__cs}{entrain\+\_\+diffusive\+\_\+cs}}), intent(in)}]{CS,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke), intent(in)}]{dsp1\+\_\+ds,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in)}]{eakb,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke), intent(in)}]{Ent\+\_\+bl,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(inout)}]{ea,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(inout)}]{eb,  }\item[{logical, dimension( g \%isd\+: g \%ied), intent(in), optional}]{do\+\_\+i\+\_\+in }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine calculates the actual entrainments (ea and eb) and the amount of surface forcing that is applied to each layer if there is no bulk mixed layer. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em f} & The density flux through a layer within a time step divided by the density difference across the interface below the layer \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em kb} & The index of the lightest layer denser than the deepest buffer layer. \\
\hline
\mbox{\texttt{ in}}  & {\em kmb} & The number of mixed and buffer layers. \\
\hline
\mbox{\texttt{ in}}  & {\em j} & The meridional index upon which to work. \\
\hline
\mbox{\texttt{ in}}  & {\em cs} & This module\textquotesingle{}s control structure. \\
\hline
\mbox{\texttt{ in}}  & {\em dsp1\+\_\+ds} & The ratio of coordinate variable differences across the interfaces below a layer over the difference across the interface above the layer. \\
\hline
\mbox{\texttt{ in}}  & {\em eakb} & The entrainment from above by the layer below the buffer layer \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ent\+\_\+bl} & The average entrainment upward and downward across each interface around the buffer layers \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em ea} & The amount of fluid entrained from the layer \\
\hline
\mbox{\texttt{ in,out}}  & {\em eb} & The amount of fluid entrained from the layer \\
\hline
\mbox{\texttt{ in}}  & {\em do\+\_\+i\+\_\+in} & Indicates which i-\/points to work on. \\
\hline
\end{DoxyParams}


Definition at line 894 of file M\+O\+M\+\_\+entrain\+\_\+diffusive.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{895   \textcolor{keywordtype}{type}(ocean\_grid\_type),            \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure}}
\DoxyCodeLine{896   \textcolor{keywordtype}{type}(verticalGrid\_type),          \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure}}
\DoxyCodeLine{897 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}    :: F\textcolor{comment}{    !< The density flux through a layer within}}
\DoxyCodeLine{898 \textcolor{comment}{                                                          !! a time step divided by the density}}
\DoxyCodeLine{899 \textcolor{comment}{                                                          !! difference across the interface below}}
\DoxyCodeLine{900 \textcolor{comment}{                                                          !! the layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{901 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{902                                     \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{    !< Layer thicknesses [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{903   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G))},      \textcolor{keywordtype}{intent(in)}    :: kb\textcolor{comment}{   !< The index of the lightest layer denser than}}
\DoxyCodeLine{904 \textcolor{comment}{                                                          !! the deepest buffer layer.}}
\DoxyCodeLine{905   \textcolor{keywordtype}{integer},                          \textcolor{keywordtype}{intent(in)}    :: kmb\textcolor{comment}{  !< The number of mixed and buffer layers.}}
\DoxyCodeLine{906   \textcolor{keywordtype}{integer},                          \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{    !< The meridional index upon which to work.}}
\DoxyCodeLine{907   \textcolor{keywordtype}{type}(entrain\_diffusive\_CS),       \textcolor{keywordtype}{intent(in)}    :: CS\textcolor{comment}{   !< This module's control structure.}}
\DoxyCodeLine{908 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}    :: dsp1\_ds\textcolor{comment}{ !< The ratio of coordinate variable}}
\DoxyCodeLine{909 \textcolor{comment}{                                                          !! differences across the interfaces below}}
\DoxyCodeLine{910 \textcolor{comment}{                                                          !! a layer over the difference across the}}
\DoxyCodeLine{911 \textcolor{comment}{                                                          !! interface above the layer.}}
\DoxyCodeLine{912 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},         \textcolor{keywordtype}{intent(in)}    :: eakb\textcolor{comment}{ !< The entrainment from above by the layer}}
\DoxyCodeLine{913 \textcolor{comment}{                                                          !! below the buffer layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{914 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}    :: Ent\_bl\textcolor{comment}{ !< The average entrainment upward and}}
\DoxyCodeLine{915 \textcolor{comment}{                                                          !! downward across each interface around}}
\DoxyCodeLine{916 \textcolor{comment}{                                                          !! the buffer layers [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{917 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{918                                     \textcolor{keywordtype}{intent(inout)} :: ea\textcolor{comment}{   !< The amount of fluid entrained from the layer}}
\DoxyCodeLine{919 \textcolor{comment}{                                                          !! above within this time step [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{920 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{921                                     \textcolor{keywordtype}{intent(inout)} :: eb\textcolor{comment}{   !< The amount of fluid entrained from the layer}}
\DoxyCodeLine{922 \textcolor{comment}{                                                          !! below within this time step [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{923   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \&}
\DoxyCodeLine{924                           \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: do\_i\_in\textcolor{comment}{ !< Indicates which i-\/points to work on.}}
\DoxyCodeLine{925 \textcolor{comment}{!   This subroutine calculates the actual entrainments (ea and eb) and the}}
\DoxyCodeLine{926 \textcolor{comment}{! amount of surface forcing that is applied to each layer if there is no bulk}}
\DoxyCodeLine{927 \textcolor{comment}{! mixed layer.}}
\DoxyCodeLine{928 }
\DoxyCodeLine{929 \textcolor{keywordtype}{  real} :: h1        \textcolor{comment}{! The thickness in excess of the minimum that will remain}}
\DoxyCodeLine{930                     \textcolor{comment}{! after exchange with the layer below [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{931   \textcolor{keywordtype}{logical} :: do\_i(SZI\_(G))}
\DoxyCodeLine{932   \textcolor{keywordtype}{integer} :: i, k, is, ie, nz}
\DoxyCodeLine{933 }
\DoxyCodeLine{934   is = g\%isc ; ie = g\%iec ; nz = g\%ke}
\DoxyCodeLine{935 }
\DoxyCodeLine{936   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(do\_i\_in)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{937     \textcolor{keywordflow}{do} i=is,ie ; do\_i(i) = do\_i\_in(i) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{938     \textcolor{keywordflow}{do} i=g\%isc,g\%iec ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{939       is = i ; \textcolor{keywordflow}{exit}}
\DoxyCodeLine{940 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{941     \textcolor{keywordflow}{do} i=g\%iec,g\%isc,-\/1 ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{942       ie = i ; \textcolor{keywordflow}{exit}}
\DoxyCodeLine{943 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{944   \textcolor{keywordflow}{else}}
\DoxyCodeLine{945     \textcolor{keywordflow}{do} i=is,ie ; do\_i(i) = .true. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{946 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{947 }
\DoxyCodeLine{948   \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{949     ea(i,j,nz) = 0.0 ; eb(i,j,nz) = 0.0}
\DoxyCodeLine{950 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{951   \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{952     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{953       eb(i,j,kmb) = max(2.0*ent\_bl(i,kmb+1) -\/ eakb(i), 0.0)}
\DoxyCodeLine{954 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{955     \textcolor{keywordflow}{do} k=nz-\/1,kmb+1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{956       \textcolor{keywordflow}{if} (k > kb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{957         \textcolor{comment}{! With a bulk mixed layer, surface buoyancy fluxes are applied}}
\DoxyCodeLine{958         \textcolor{comment}{! elsewhere, so F should always be nonnegative.}}
\DoxyCodeLine{959         ea(i,j,k) = dsp1\_ds(i,k)*f(i,k)}
\DoxyCodeLine{960         eb(i,j,k) = f(i,k)}
\DoxyCodeLine{961       \textcolor{keywordflow}{elseif} (k == kb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{962         ea(i,j,k) = eakb(i)}
\DoxyCodeLine{963         eb(i,j,k) = f(i,k)}
\DoxyCodeLine{964       \textcolor{keywordflow}{elseif} (k == kb(i)-\/1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{965         ea(i,j,k) = ea(i,j,k+1)}
\DoxyCodeLine{966         eb(i,j,k) = eb(i,j,kmb)}
\DoxyCodeLine{967       \textcolor{keywordflow}{else}}
\DoxyCodeLine{968         ea(i,j,k) = ea(i,j,k+1)}
\DoxyCodeLine{969         \textcolor{comment}{!   Add the entrainment of the thin interior layers to eb going}}
\DoxyCodeLine{970         \textcolor{comment}{! up into the buffer layer.}}
\DoxyCodeLine{971         eb(i,j,k) = eb(i,j,k+1) + max(0.0, h(i,j,k+1) -\/ gv\%Angstrom\_H)}
\DoxyCodeLine{972 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{973 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{974     k = kmb}
\DoxyCodeLine{975     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{976       \textcolor{comment}{! Adjust the previously calculated entrainment from below by the deepest}}
\DoxyCodeLine{977       \textcolor{comment}{! buffer layer to account for entrainment of thin interior layers .}}
\DoxyCodeLine{978       \textcolor{keywordflow}{if} (kb(i) > kmb+1) \&}
\DoxyCodeLine{979         eb(i,j,k) = eb(i,j,k+1) + max(0.0, h(i,j,k+1) -\/ gv\%Angstrom\_H)}
\DoxyCodeLine{980 }
\DoxyCodeLine{981       \textcolor{comment}{! Determine the entrainment from above for each buffer layer.}}
\DoxyCodeLine{982       h1 = (h(i,j,k) -\/ gv\%Angstrom\_H) + (eb(i,j,k) -\/ ea(i,j,k+1))}
\DoxyCodeLine{983       ea(i,j,k) = max(ent\_bl(i,k), ent\_bl(i,k)-\/0.5*h1, -\/h1)}
\DoxyCodeLine{984 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{985     \textcolor{keywordflow}{do} k=kmb-\/1,2,-\/1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{986       \textcolor{comment}{! Determine the entrainment from below for each buffer layer.}}
\DoxyCodeLine{987       eb(i,j,k) = max(2.0*ent\_bl(i,k+1) -\/ ea(i,j,k+1), 0.0)}
\DoxyCodeLine{988 }
\DoxyCodeLine{989       \textcolor{comment}{! Determine the entrainment from above for each buffer layer.}}
\DoxyCodeLine{990       h1 = (h(i,j,k) -\/ gv\%Angstrom\_H) + (eb(i,j,k) -\/ ea(i,j,k+1))}
\DoxyCodeLine{991       ea(i,j,k) = max(ent\_bl(i,k), ent\_bl(i,k)-\/0.5*h1, -\/h1)}
\DoxyCodeLine{992 \textcolor{comment}{!       if (h1 >= 0.0) then ;                     ea(i,j,k) = Ent\_bl(i,K)}}
\DoxyCodeLine{993 \textcolor{comment}{!       elseif (Ent\_bl(i,K)+0.5*h1 >= 0.0) then ; ea(i,j,k) = Ent\_bl(i,K)-\/0.5*h1}}
\DoxyCodeLine{994 \textcolor{comment}{!       else ;                                    ea(i,j,k) = -\/h1 ; endif}}
\DoxyCodeLine{995 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{996     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{997       eb(i,j,1) = max(2.0*ent\_bl(i,2) -\/ ea(i,j,2), 0.0)}
\DoxyCodeLine{998       ea(i,j,1) = 0.0}
\DoxyCodeLine{999 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1000   \textcolor{keywordflow}{else}                                          \textcolor{comment}{! not BULKMIXEDLAYER}}
\DoxyCodeLine{1001     \textcolor{comment}{! Calculate the entrainment by each layer from above and below.}}
\DoxyCodeLine{1002     \textcolor{comment}{! Entrainment is always positive, but F may be negative due to}}
\DoxyCodeLine{1003     \textcolor{comment}{! surface buoyancy fluxes.}}
\DoxyCodeLine{1004     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1005       ea(i,j,1) = 0.0 ; eb(i,j,1) = max(f(i,1),0.0)}
\DoxyCodeLine{1006       ea(i,j,2) = dsp1\_ds(i,2)*f(i,2) -\/ min(f(i,1),0.0)}
\DoxyCodeLine{1007 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1008 }
\DoxyCodeLine{1009     \textcolor{keywordflow}{do} k=2,nz-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1010       eb(i,j,k) = max(f(i,k),0.0)}
\DoxyCodeLine{1011       ea(i,j,k+1) = dsp1\_ds(i,k+1)*f(i,k+1) -\/ (f(i,k)-\/eb(i,j,k))}
\DoxyCodeLine{1012       \textcolor{keywordflow}{if} (ea(i,j,k+1) < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1013         eb(i,j,k) = eb(i,j,k) -\/ ea(i,j,k+1)}
\DoxyCodeLine{1014         ea(i,j,k+1) = 0.0}
\DoxyCodeLine{1015 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1016 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1017 \textcolor{keywordflow}{  endif}                                         \textcolor{comment}{! end BULKMIXEDLAYER}}

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__entrain__diffusive_ae45dbf976314c3a9e278ebcebedab109}\label{namespacemom__entrain__diffusive_ae45dbf976314c3a9e278ebcebedab109}} 
\index{mom\_entrain\_diffusive@{mom\_entrain\_diffusive}!find\_maxf\_kb@{find\_maxf\_kb}}
\index{find\_maxf\_kb@{find\_maxf\_kb}!mom\_entrain\_diffusive@{mom\_entrain\_diffusive}}
\doxysubsubsection{\texorpdfstring{find\_maxf\_kb()}{find\_maxf\_kb()}}
{\footnotesize\ttfamily subroutine mom\+\_\+entrain\+\_\+diffusive\+::find\+\_\+maxf\+\_\+kb (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h\+\_\+bl,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{Sref,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(in)}]{Ent\+\_\+bl,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{I\+\_\+d\+Skbp1,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{min\+\_\+ent\+\_\+in,  }\item[{real, dimension(szi\+\_\+(g)), intent(in)}]{max\+\_\+ent\+\_\+in,  }\item[{integer, intent(in)}]{kmb,  }\item[{integer, intent(in)}]{is,  }\item[{integer, intent(in)}]{ie,  }\item[{type(\mbox{\hyperlink{structmom__grid_1_1ocean__grid__type}{ocean\+\_\+grid\+\_\+type}}), intent(in)}]{G,  }\item[{type(\mbox{\hyperlink{structmom__verticalgrid_1_1verticalgrid__type}{verticalgrid\+\_\+type}}), intent(in)}]{GV,  }\item[{type(\mbox{\hyperlink{structmom__entrain__diffusive_1_1entrain__diffusive__cs}{entrain\+\_\+diffusive\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out)}]{maxF,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out), optional}]{ent\+\_\+maxF,  }\item[{logical, dimension( g \%isd\+: g \%ied), intent(in), optional}]{do\+\_\+i\+\_\+in,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(out), optional}]{F\+\_\+lim\+\_\+maxent,  }\item[{real, dimension( g \%isd\+: g \%ied), intent(in), optional}]{F\+\_\+thresh }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Maximize F = ent$\ast$ds\+\_\+kb$\ast$\+I\+\_\+d\+Skbp1 in the range min\+\_\+ent $<$ ent $<$ max\+\_\+ent. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em h\+\_\+bl} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em sref} & Reference potential density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ent\+\_\+bl} & The average entrainment upward and \\
\hline
\mbox{\texttt{ in}}  & {\em i\+\_\+dskbp1} & The inverse of the difference in reference potential density across the base of the uppermost interior layer \mbox{[}R-\/1 $\sim$$>$ m3 kg-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em min\+\_\+ent\+\_\+in} & The minimum value of ent to search, \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em max\+\_\+ent\+\_\+in} & The maximum value of ent to search, \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em kmb} & The number of mixed and buffer layers. \\
\hline
\mbox{\texttt{ in}}  & {\em is} & The start of the i-\/index range to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em ie} & The end of the i-\/index range to work on. \\
\hline
 & {\em cs} & This module\textquotesingle{}s control structure. \\
\hline
\mbox{\texttt{ out}}  & {\em maxf} & The maximum value of F = ent$\ast$ds\+\_\+kb$\ast$\+I\+\_\+d\+Skbp1 found in the range min\+\_\+ent $<$ ent $<$ max\+\_\+ent \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em ent\+\_\+maxf} & The value of ent at that maximum \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em do\+\_\+i\+\_\+in} & A logical array indicating which columns \\
\hline
\mbox{\texttt{ out}}  & {\em f\+\_\+lim\+\_\+maxent} & If present, do not apply the limit in \\
\hline
\mbox{\texttt{ in}}  & {\em f\+\_\+thresh} & If F\+\_\+thresh is present, return the first \\
\hline
\end{DoxyParams}


Definition at line 1783 of file M\+O\+M\+\_\+entrain\+\_\+diffusive.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1786   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(in)}  :: G\textcolor{comment}{        !< The ocean's grid structure.}}
\DoxyCodeLine{1787   \textcolor{keywordtype}{type}(verticalGrid\_type),    \textcolor{keywordtype}{intent(in)}  :: GV\textcolor{comment}{       !< The ocean's vertical grid structure.}}
\DoxyCodeLine{1788 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1789                               \textcolor{keywordtype}{intent(in)}  :: h\_bl\textcolor{comment}{     !< Layer thickness [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{1790 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1791                               \textcolor{keywordtype}{intent(in)}  :: Sref\textcolor{comment}{     !< Reference potential density [R \string~> kg m-\/3].}}
\DoxyCodeLine{1792 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1793                               \textcolor{keywordtype}{intent(in)}  :: Ent\_bl\textcolor{comment}{   !< The average entrainment upward and}}
\DoxyCodeLine{1794 \textcolor{comment}{                                                      !! downward across each interface around}}
\DoxyCodeLine{1795 \textcolor{comment}{                                                      !! the buffer layers [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1796 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(in)}  :: I\_dSkbp1\textcolor{comment}{ !< The inverse of the difference in}}
\DoxyCodeLine{1797 \textcolor{comment}{                                                      !! reference potential density across the}}
\DoxyCodeLine{1798 \textcolor{comment}{                                                      !! base of the uppermost interior layer}}
\DoxyCodeLine{1799 \textcolor{comment}{                                                      !! [R-\/1 \string~> m3 kg-\/1].}}
\DoxyCodeLine{1800 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(in)}  :: min\_ent\_in\textcolor{comment}{ !< The minimum value of ent to search,}}
\DoxyCodeLine{1801 \textcolor{comment}{                                                      !! [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1802 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(in)}  :: max\_ent\_in\textcolor{comment}{ !< The maximum value of ent to search,}}
\DoxyCodeLine{1803 \textcolor{comment}{                                                      !! [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1804   \textcolor{keywordtype}{integer},                    \textcolor{keywordtype}{intent(in)}  :: kmb\textcolor{comment}{      !< The number of mixed and buffer layers.}}
\DoxyCodeLine{1805   \textcolor{keywordtype}{integer},                    \textcolor{keywordtype}{intent(in)}  :: is\textcolor{comment}{       !< The start of the i-\/index range to work on.}}
\DoxyCodeLine{1806   \textcolor{keywordtype}{integer},                    \textcolor{keywordtype}{intent(in)}  :: ie\textcolor{comment}{       !< The end of the i-\/index range to work on.}}
\DoxyCodeLine{1807   \textcolor{keywordtype}{type}(entrain\_diffusive\_CS), \textcolor{keywordtype}{pointer}     :: CS\textcolor{comment}{       !< This module's control structure.}}
\DoxyCodeLine{1808 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))},   \textcolor{keywordtype}{intent(out)} :: maxF\textcolor{comment}{     !< The maximum value of F}}
\DoxyCodeLine{1809 \textcolor{comment}{                                                      !! = ent*ds\_kb*I\_dSkbp1 found in the range}}
\DoxyCodeLine{1810 \textcolor{comment}{                                                      !! min\_ent < ent < max\_ent [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1811 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \&}
\DoxyCodeLine{1812                     \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: ent\_maxF\textcolor{comment}{ !< The value of ent at that maximum [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1813   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \&}
\DoxyCodeLine{1814                     \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: do\_i\_in\textcolor{comment}{  !< A logical array indicating which columns}}
\DoxyCodeLine{1815 \textcolor{comment}{                                                      !! to work on.}}
\DoxyCodeLine{1816 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \&}
\DoxyCodeLine{1817                     \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: F\_lim\_maxent\textcolor{comment}{ !< If present, do not apply the limit in}}
\DoxyCodeLine{1818 \textcolor{comment}{                                                      !! finding the maximum value, but return the}}
\DoxyCodeLine{1819 \textcolor{comment}{                                                      !! limited value at ent=max\_ent\_in in this}}
\DoxyCodeLine{1820 \textcolor{comment}{                                                      !! array [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1821 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))}, \&}
\DoxyCodeLine{1822                     \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: F\_thresh\textcolor{comment}{ !< If F\_thresh is present, return the first}}
\DoxyCodeLine{1823 \textcolor{comment}{                                                      !! value found that has F > F\_thresh, or}}
\DoxyCodeLine{1824 \textcolor{comment}{                                                      !! the maximum.}}
\DoxyCodeLine{1825 }
\DoxyCodeLine{1826 \textcolor{comment}{! Maximize F = ent*ds\_kb*I\_dSkbp1 in the range min\_ent < ent < max\_ent.}}
\DoxyCodeLine{1827 \textcolor{comment}{! ds\_kb may itself be limited to positive values in determine\_dSkb, which gives}}
\DoxyCodeLine{1828 \textcolor{comment}{! the prospect of two local maxima in the range -\/ one at max\_ent\_in with that}}
\DoxyCodeLine{1829 \textcolor{comment}{! minimum value of ds\_kb, and the other due to the unlimited (potentially}}
\DoxyCodeLine{1830 \textcolor{comment}{! negative) value.  It is faster to find the true maximum by first finding the}}
\DoxyCodeLine{1831 \textcolor{comment}{! unlimited maximum and comparing it to the limited value at max\_ent\_in.}}
\DoxyCodeLine{1832 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: \&}
\DoxyCodeLine{1833     ent, \&}
\DoxyCodeLine{1834     minent, maxent, ent\_best, \&}
\DoxyCodeLine{1835     F\_max\_ent\_in, \&}
\DoxyCodeLine{1836     F\_maxent, F\_minent, F, F\_best, \&}
\DoxyCodeLine{1837     dF\_dent, dF\_dE\_max, dF\_dE\_min, dF\_dE\_best, \&}
\DoxyCodeLine{1838     dS\_kb, dS\_kb\_lim, ddSkb\_dE, dS\_anom\_lim, \&}
\DoxyCodeLine{1839     chg\_prev, chg\_pre\_prev}
\DoxyCodeLine{1840 \textcolor{keywordtype}{  real} :: dF\_dE\_mean, maxslope, minslope}
\DoxyCodeLine{1841 \textcolor{keywordtype}{  real} :: tolerance}
\DoxyCodeLine{1842 \textcolor{keywordtype}{  real} :: ratio\_select\_end}
\DoxyCodeLine{1843 \textcolor{keywordtype}{  real} :: rat, max\_chg, min\_chg, chg1, chg2, chg}
\DoxyCodeLine{1844   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: do\_i, last\_it, need\_bracket, may\_use\_best}
\DoxyCodeLine{1845   \textcolor{keywordtype}{logical} :: doany, OK1, OK2, bisect, new\_min\_bound}
\DoxyCodeLine{1846   \textcolor{keywordtype}{integer} :: i, it, is1, ie1}
\DoxyCodeLine{1847   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{parameter} :: MAXIT = 20}
\DoxyCodeLine{1848 }
\DoxyCodeLine{1849   tolerance = cs\%Tolerance\_Ent}
\DoxyCodeLine{1850 }
\DoxyCodeLine{1851   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(do\_i\_in)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1852     \textcolor{keywordflow}{do} i=is,ie ; do\_i(i) = do\_i\_in(i) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1853   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1854     \textcolor{keywordflow}{do} i=is,ie ; do\_i(i) = .true. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1855 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1856 }
\DoxyCodeLine{1857   \textcolor{comment}{! The most likely value is at max\_ent.}}
\DoxyCodeLine{1858   \textcolor{keyword}{call }determine\_dskb(h\_bl, sref, ent\_bl, max\_ent\_in, is, ie, kmb, g, gv, .false., \&}
\DoxyCodeLine{1859                       ds\_kb, ddskb\_de, ds\_anom\_lim=ds\_anom\_lim)}
\DoxyCodeLine{1860   ie1 = is-\/1 ; doany = .false.}
\DoxyCodeLine{1861   \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1862     ds\_kb\_lim(i) = ds\_kb(i) + ds\_anom\_lim(i)}
\DoxyCodeLine{1863     f\_max\_ent\_in(i) = max\_ent\_in(i)*ds\_kb\_lim(i)*i\_dskbp1(i)}
\DoxyCodeLine{1864     maxent(i) = max\_ent\_in(i) ; minent(i) = min\_ent\_in(i)}
\DoxyCodeLine{1865     \textcolor{keywordflow}{if} ((abs(maxent(i) -\/ minent(i)) < tolerance) .or. (.not.do\_i(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1866       f\_best(i) = max\_ent\_in(i)*ds\_kb(i)*i\_dskbp1(i)}
\DoxyCodeLine{1867       ent\_best(i) = max\_ent\_in(i) ; ent(i) = max\_ent\_in(i)}
\DoxyCodeLine{1868       do\_i(i) = .false.}
\DoxyCodeLine{1869     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1870       f\_maxent(i) = maxent(i) * ds\_kb(i) * i\_dskbp1(i)}
\DoxyCodeLine{1871       df\_de\_max(i) = (ds\_kb(i) + maxent(i)*ddskb\_de(i)) * i\_dskbp1(i)}
\DoxyCodeLine{1872       doany = .true. ; last\_it(i) = .false. ; need\_bracket(i) = .true.}
\DoxyCodeLine{1873 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1874 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1875 }
\DoxyCodeLine{1876   \textcolor{keywordflow}{if} (doany) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1877     ie1 = is-\/1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) ie1 = i ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1878     \textcolor{keywordflow}{do} i=ie1,is,-\/1 ; \textcolor{keywordflow}{if} (do\_i(i)) is1 = i ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1879     \textcolor{comment}{! Find the value of F and its derivative at min\_ent.}}
\DoxyCodeLine{1880     \textcolor{keyword}{call }determine\_dskb(h\_bl, sref, ent\_bl, minent, is1, ie1, kmb, g, gv, .false., \&}
\DoxyCodeLine{1881                         ds\_kb, ddskb\_de, do\_i\_in = do\_i)}
\DoxyCodeLine{1882     \textcolor{keywordflow}{do} i=is1,ie1 ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1883       f\_minent(i) = minent(i) * ds\_kb(i) * i\_dskbp1(i)}
\DoxyCodeLine{1884       df\_de\_min(i) = (ds\_kb(i) + minent(i)*ddskb\_de(i)) * i\_dskbp1(i)}
\DoxyCodeLine{1885 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1886 }
\DoxyCodeLine{1887     ratio\_select\_end = 0.9}
\DoxyCodeLine{1888     \textcolor{keywordflow}{do} it=1,maxit}
\DoxyCodeLine{1889       ratio\_select\_end = 0.5*ratio\_select\_end}
\DoxyCodeLine{1890       \textcolor{keywordflow}{do} i=is1,ie1 ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1891         \textcolor{keywordflow}{if} (need\_bracket(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1892           df\_de\_mean = (f\_maxent(i) -\/ f\_minent(i)) / (maxent(i) -\/ minent(i))}
\DoxyCodeLine{1893           maxslope = max(df\_de\_mean, df\_de\_min(i), df\_de\_max(i))}
\DoxyCodeLine{1894           minslope = min(df\_de\_mean, df\_de\_min(i), df\_de\_max(i))}
\DoxyCodeLine{1895           \textcolor{keywordflow}{if} (f\_minent(i) >= f\_maxent(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1896             \textcolor{keywordflow}{if} (df\_de\_min(i) > 0.0) \textcolor{keywordflow}{then} ; rat = 0.02 \textcolor{comment}{! A small step should bracket the soln.}}
\DoxyCodeLine{1897             \textcolor{keywordflow}{elseif} (maxslope < ratio\_select\_end*minslope) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1898               \textcolor{comment}{! The maximum of F is at minent.}}
\DoxyCodeLine{1899               f\_best(i) = f\_minent(i) ; ent\_best(i) = minent(i) ; rat = 0.0}
\DoxyCodeLine{1900               do\_i(i) = .false.}
\DoxyCodeLine{1901             \textcolor{keywordflow}{else} ; rat = 0.382 ;\textcolor{keywordflow}{ endif} \textcolor{comment}{! Use the golden ratio}}
\DoxyCodeLine{1902           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1903             \textcolor{keywordflow}{if} (df\_de\_max(i) < 0.0) \textcolor{keywordflow}{then} ; rat = 0.98 \textcolor{comment}{! A small step should bracket the soln.}}
\DoxyCodeLine{1904             \textcolor{keywordflow}{elseif} (minslope > ratio\_select\_end*maxslope) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1905               \textcolor{comment}{! The maximum of F is at maxent.}}
\DoxyCodeLine{1906               f\_best(i) = f\_maxent(i) ; ent\_best(i) = maxent(i) ; rat = 1.0}
\DoxyCodeLine{1907               do\_i(i) = .false.}
\DoxyCodeLine{1908             \textcolor{keywordflow}{else} ; rat = 0.618 ;\textcolor{keywordflow}{ endif} \textcolor{comment}{! Use the golden ratio}}
\DoxyCodeLine{1909 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1910 }
\DoxyCodeLine{1911           \textcolor{keywordflow}{if} (rat >= 0.0) ent(i) = rat*maxent(i) + (1.0-\/rat)*minent(i)}
\DoxyCodeLine{1912           \textcolor{keywordflow}{if} (((maxent(i) -\/ minent(i)) < tolerance) .or. (it==maxit)) \&}
\DoxyCodeLine{1913             last\_it(i) = .true.}
\DoxyCodeLine{1914         \textcolor{keywordflow}{else} \textcolor{comment}{! The maximum is bracketed by minent, ent\_best, and maxent.}}
\DoxyCodeLine{1915           chg1 = 2.0*(maxent(i) -\/ minent(i)) ; chg2 = chg1}
\DoxyCodeLine{1916           \textcolor{keywordflow}{if} (df\_de\_best(i) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1917             max\_chg = maxent(i) -\/ ent\_best(i) ; min\_chg = 0.0}
\DoxyCodeLine{1918           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1919             max\_chg = 0.0 ; min\_chg = minent(i) -\/ ent\_best(i) \textcolor{comment}{! < 0}}
\DoxyCodeLine{1920 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1921           \textcolor{keywordflow}{if} (max\_chg -\/ min\_chg < 2.0*tolerance) last\_it(i) = .true.}
\DoxyCodeLine{1922           \textcolor{keywordflow}{if} (df\_de\_max(i) /= df\_de\_best(i)) \&}
\DoxyCodeLine{1923             chg1 = (maxent(i) -\/ ent\_best(i))*df\_de\_best(i) / \&}
\DoxyCodeLine{1924                    (df\_de\_best(i) -\/ df\_de\_max(i))}
\DoxyCodeLine{1925           \textcolor{keywordflow}{if} (df\_de\_min(i) /= df\_de\_best(i)) \&}
\DoxyCodeLine{1926             chg2 = (minent(i) -\/ ent\_best(i))*df\_de\_best(i) / \&}
\DoxyCodeLine{1927                    (df\_de\_best(i) -\/ df\_de\_min(i))}
\DoxyCodeLine{1928           ok1 = ((chg1 < max\_chg) .and. (chg1 > min\_chg))}
\DoxyCodeLine{1929           ok2 = ((chg2 < max\_chg) .and. (chg2 > min\_chg))}
\DoxyCodeLine{1930           \textcolor{keywordflow}{if} (.not.(ok1 .or. ok2)) \textcolor{keywordflow}{then} ; bisect = .true. ; \textcolor{keywordflow}{else}}
\DoxyCodeLine{1931             \textcolor{keywordflow}{if} (ok1 .and. ok2) \textcolor{keywordflow}{then} \textcolor{comment}{! Take the acceptable smaller change.}}
\DoxyCodeLine{1932               chg = chg1 ; \textcolor{keywordflow}{if} (abs(chg2) < abs(chg1)) chg = chg2}
\DoxyCodeLine{1933             \textcolor{keywordflow}{elseif} (ok1) \textcolor{keywordflow}{then} ; chg = chg1}
\DoxyCodeLine{1934             \textcolor{keywordflow}{else} ; chg = chg2 ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1935             \textcolor{keywordflow}{if} (abs(chg) > 0.5*abs(chg\_pre\_prev(i))) \textcolor{keywordflow}{then} ; bisect = .true.}
\DoxyCodeLine{1936             \textcolor{keywordflow}{else} ; bisect = .false. ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1937 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1938           chg\_pre\_prev(i) = chg\_prev(i)}
\DoxyCodeLine{1939           \textcolor{keywordflow}{if} (bisect) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1940             \textcolor{keywordflow}{if} (df\_de\_best(i) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1941               ent(i) = 0.5*(maxent(i) + ent\_best(i))}
\DoxyCodeLine{1942               chg\_prev(i) = 0.5*(maxent(i) -\/ ent\_best(i))}
\DoxyCodeLine{1943             \textcolor{keywordflow}{else}}
\DoxyCodeLine{1944               ent(i) = 0.5*(minent(i) + ent\_best(i))}
\DoxyCodeLine{1945               chg\_prev(i) = 0.5*(minent(i) -\/ ent\_best(i))}
\DoxyCodeLine{1946 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1947           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1948             \textcolor{keywordflow}{if} (abs(chg) < tolerance) chg = sign(tolerance,chg)}
\DoxyCodeLine{1949             ent(i) = ent\_best(i) + chg}
\DoxyCodeLine{1950             chg\_prev(i) = chg}
\DoxyCodeLine{1951 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1952 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1953 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1954 }
\DoxyCodeLine{1955       \textcolor{keywordflow}{if} (mod(it,3) == 0) \textcolor{keywordflow}{then}  \textcolor{comment}{! Re-\/determine the loop bounds.}}
\DoxyCodeLine{1956         ie1 = is-\/1 ; \textcolor{keywordflow}{do} i=is1,ie ; \textcolor{keywordflow}{if} (do\_i(i)) ie1 = i ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1957         \textcolor{keywordflow}{do} i=ie1,is,-\/1 ; \textcolor{keywordflow}{if} (do\_i(i)) is1 = i ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1958 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1959 }
\DoxyCodeLine{1960       \textcolor{keyword}{call }determine\_dskb(h\_bl, sref, ent\_bl, ent, is1, ie1, kmb, g, gv, .false., \&}
\DoxyCodeLine{1961                           ds\_kb, ddskb\_de, do\_i\_in = do\_i)}
\DoxyCodeLine{1962       \textcolor{keywordflow}{do} i=is1,ie1 ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1963         f(i) = ent(i)*ds\_kb(i)*i\_dskbp1(i)}
\DoxyCodeLine{1964         df\_dent(i) = (ds\_kb(i) + ent(i)*ddskb\_de(i)) * i\_dskbp1(i)}
\DoxyCodeLine{1965 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1966 }
\DoxyCodeLine{1967       \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(f\_thresh)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is1,ie1 ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1968         \textcolor{keywordflow}{if} (f(i) >= f\_thresh(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1969           f\_best(i) = f(i) ; ent\_best(i) = ent(i) ; do\_i(i) = .false.}
\DoxyCodeLine{1970 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1971 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1972 }
\DoxyCodeLine{1973       doany = .false.}
\DoxyCodeLine{1974       \textcolor{keywordflow}{do} i=is1,ie1 ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1975         \textcolor{keywordflow}{if} (.not.last\_it(i)) doany = .true.}
\DoxyCodeLine{1976         \textcolor{keywordflow}{if} (last\_it(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1977           \textcolor{keywordflow}{if} (need\_bracket(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1978             \textcolor{keywordflow}{if} ((f(i) > f\_maxent(i)) .and. (f(i) > f\_minent(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1979               f\_best(i) = f(i) ; ent\_best(i) = ent(i)}
\DoxyCodeLine{1980             \textcolor{keywordflow}{elseif} (f\_maxent(i) > f\_minent(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1981               f\_best(i) = f\_maxent(i) ; ent\_best(i) = maxent(i)}
\DoxyCodeLine{1982             \textcolor{keywordflow}{else}}
\DoxyCodeLine{1983               f\_best(i) = f\_minent(i) ; ent\_best(i) = minent(i)}
\DoxyCodeLine{1984 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1985           \textcolor{keywordflow}{elseif} (f(i) > f\_best(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1986             f\_best(i) = f(i) ; ent\_best(i) = ent(i)}
\DoxyCodeLine{1987 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1988           do\_i(i) = .false.}
\DoxyCodeLine{1989         \textcolor{keywordflow}{elseif} (need\_bracket(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1990           \textcolor{keywordflow}{if} ((f(i) > f\_maxent(i)) .and. (f(i) > f\_minent(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1991             need\_bracket(i) = .false. \textcolor{comment}{! The maximum is now bracketed.}}
\DoxyCodeLine{1992             chg\_prev(i) = (maxent(i) -\/ minent(i))}
\DoxyCodeLine{1993             chg\_pre\_prev(i) = 2.0*chg\_prev(i)}
\DoxyCodeLine{1994             ent\_best(i) = ent(i) ; f\_best(i) = f(i) ; df\_de\_best(i) = df\_dent(i)}
\DoxyCodeLine{1995           \textcolor{keywordflow}{elseif} ((f(i) <= f\_maxent(i)) .and. (f(i) > f\_minent(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1996             new\_min\_bound = .true.  \textcolor{comment}{! We have a new minimum bound.}}
\DoxyCodeLine{1997           \textcolor{keywordflow}{elseif} ((f(i) <= f\_maxent(i)) .and. (f(i) > f\_minent(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1998             new\_min\_bound = .false. \textcolor{comment}{! We have a new maximum bound.}}
\DoxyCodeLine{1999           \textcolor{keywordflow}{else} \textcolor{comment}{! This case would bracket a minimum.  Wierd.}}
\DoxyCodeLine{2000              \textcolor{comment}{! Unless the derivative indicates that there is a maximum near the}}
\DoxyCodeLine{2001              \textcolor{comment}{! lower bound, try keeping the end with the larger value of F}}
\DoxyCodeLine{2002              \textcolor{comment}{! in a tie keep the minimum as the answer here will be compared}}
\DoxyCodeLine{2003              \textcolor{comment}{! with the maximum input value later.}}
\DoxyCodeLine{2004              new\_min\_bound = .true.}
\DoxyCodeLine{2005              \textcolor{keywordflow}{if} (df\_de\_min(i) > 0.0 .or. (f\_minent(i) >= f\_maxent(i))) \&}
\DoxyCodeLine{2006                new\_min\_bound = .false.}
\DoxyCodeLine{2007 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{2008           \textcolor{keywordflow}{if} (need\_bracket(i)) \textcolor{keywordflow}{then} \textcolor{comment}{! Still not bracketed.}}
\DoxyCodeLine{2009             \textcolor{keywordflow}{if} (new\_min\_bound) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2010               minent(i) = ent(i) ; f\_minent(i) = f(i) ; df\_de\_min(i) = df\_dent(i)}
\DoxyCodeLine{2011             \textcolor{keywordflow}{else}}
\DoxyCodeLine{2012               maxent(i) = ent(i) ; f\_maxent(i) = f(i) ; df\_de\_max(i) = df\_dent(i)}
\DoxyCodeLine{2013 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{2014 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{2015         \textcolor{keywordflow}{else}  \textcolor{comment}{! The root was previously bracketed.}}
\DoxyCodeLine{2016           \textcolor{keywordflow}{if} (f(i) >= f\_best(i)) \textcolor{keywordflow}{then} \textcolor{comment}{! There is a new maximum.}}
\DoxyCodeLine{2017             \textcolor{keywordflow}{if} (ent(i) > ent\_best(i)) \textcolor{keywordflow}{then} \textcolor{comment}{! Replace minent with ent\_prev.}}
\DoxyCodeLine{2018               minent(i) = ent\_best(i) ; f\_minent(i) = f\_best(i) ; df\_de\_min(i) = df\_de\_best(i)}
\DoxyCodeLine{2019             \textcolor{keywordflow}{else} \textcolor{comment}{! Replace maxent with ent\_best.}}
\DoxyCodeLine{2020               maxent(i) = ent\_best(i) ; f\_maxent(i) = f\_best(i) ; df\_de\_max(i) = df\_de\_best(i)}
\DoxyCodeLine{2021 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{2022             ent\_best(i) = ent(i) ; f\_best(i) = f(i) ; df\_de\_best(i) = df\_dent(i)}
\DoxyCodeLine{2023           \textcolor{keywordflow}{else}}
\DoxyCodeLine{2024             \textcolor{keywordflow}{if} (ent(i) < ent\_best(i)) \textcolor{keywordflow}{then} \textcolor{comment}{! Replace the minent with ent.}}
\DoxyCodeLine{2025               minent(i) = ent(i) ; f\_minent(i) = f(i) ; df\_de\_min(i) = df\_dent(i)}
\DoxyCodeLine{2026             \textcolor{keywordflow}{else} \textcolor{comment}{! Replace maxent with ent\_prev.}}
\DoxyCodeLine{2027               maxent(i) = ent(i) ; f\_maxent(i) = f(i) ; df\_de\_max(i) = df\_dent(i)}
\DoxyCodeLine{2028 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{2029 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{2030           \textcolor{keywordflow}{if} ((maxent(i) -\/ minent(i)) <= tolerance) do\_i(i) = .false. \textcolor{comment}{! Done.}}
\DoxyCodeLine{2031 \textcolor{keywordflow}{        endif} \textcolor{comment}{! need\_bracket.}}
\DoxyCodeLine{2032 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2033       \textcolor{keywordflow}{if} (.not.doany) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{2034 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2035 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2036 }
\DoxyCodeLine{2037   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(f\_lim\_maxent)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2038     \textcolor{comment}{! Return the unlimited maximum in maxF, and the limited value of F at maxent.}}
\DoxyCodeLine{2039     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2040       maxf(i) = f\_best(i)}
\DoxyCodeLine{2041       f\_lim\_maxent(i) = f\_max\_ent\_in(i)}
\DoxyCodeLine{2042       \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ent\_maxf)) ent\_maxf(i) = ent\_best(i)}
\DoxyCodeLine{2043 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2044   \textcolor{keywordflow}{else}}
\DoxyCodeLine{2045     \textcolor{comment}{! Now compare the two? potential maxima using the limited value of dF\_kb.}}
\DoxyCodeLine{2046     doany = .false.}
\DoxyCodeLine{2047     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2048       may\_use\_best(i) = (ent\_best(i) /= max\_ent\_in(i))}
\DoxyCodeLine{2049       \textcolor{keywordflow}{if} (may\_use\_best(i)) doany = .true.}
\DoxyCodeLine{2050 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2051     \textcolor{keywordflow}{if} (doany) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2052       \textcolor{comment}{! For efficiency, could save previous value of dS\_anom\_lim\_best?}}
\DoxyCodeLine{2053       \textcolor{keyword}{call }determine\_dskb(h\_bl, sref, ent\_bl, ent\_best, is, ie, kmb, g, gv, .true., \&}
\DoxyCodeLine{2054                           ds\_kb\_lim)}
\DoxyCodeLine{2055       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2056         f\_best(i) = ent\_best(i)*ds\_kb\_lim(i)*i\_dskbp1(i)}
\DoxyCodeLine{2057         \textcolor{comment}{! The second test seems necessary because of roundoff differences that}}
\DoxyCodeLine{2058         \textcolor{comment}{! can arise during compilation.}}
\DoxyCodeLine{2059         \textcolor{keywordflow}{if} ((f\_best(i) > f\_max\_ent\_in(i)) .and. (may\_use\_best(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2060           maxf(i) = f\_best(i)}
\DoxyCodeLine{2061           \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ent\_maxf)) ent\_maxf(i) = ent\_best(i)}
\DoxyCodeLine{2062         \textcolor{keywordflow}{else}}
\DoxyCodeLine{2063           maxf(i) = f\_max\_ent\_in(i)}
\DoxyCodeLine{2064           \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ent\_maxf)) ent\_maxf(i) = max\_ent\_in(i)}
\DoxyCodeLine{2065 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2066 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{2067     \textcolor{keywordflow}{else}}
\DoxyCodeLine{2068       \textcolor{comment}{! All of the maxima are at the maximum entrainment.}}
\DoxyCodeLine{2069       \textcolor{keywordflow}{do} i=is,ie ; maxf(i) = f\_max\_ent\_in(i) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2070       \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ent\_maxf)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2071         \textcolor{keywordflow}{do} i=is,ie ; ent\_maxf(i) = max\_ent\_in(i) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2072 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2073 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2074 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2075 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__entrain__diffusive_a363a25e7823043bb028e920e359733b0}\label{namespacemom__entrain__diffusive_a363a25e7823043bb028e920e359733b0}} 
\index{mom\_entrain\_diffusive@{mom\_entrain\_diffusive}!set\_ent\_bl@{set\_ent\_bl}}
\index{set\_ent\_bl@{set\_ent\_bl}!mom\_entrain\_diffusive@{mom\_entrain\_diffusive}}
\doxysubsubsection{\texorpdfstring{set\_ent\_bl()}{set\_ent\_bl()}}
{\footnotesize\ttfamily subroutine mom\+\_\+entrain\+\_\+diffusive\+::set\+\_\+ent\+\_\+bl (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%ke+1), intent(in)}]{dt\+Kd\+\_\+int,  }\item[{type(\mbox{\hyperlink{structmom__variables_1_1thermo__var__ptrs}{thermo\+\_\+var\+\_\+ptrs}}), intent(in)}]{tv,  }\item[{integer, dimension( g \%isd\+: g \%ied), intent(inout)}]{kb,  }\item[{integer, intent(in)}]{kmb,  }\item[{logical, dimension( g \%isd\+: g \%ied), intent(in)}]{do\+\_\+i,  }\item[{type(\mbox{\hyperlink{structmom__grid_1_1ocean__grid__type}{ocean\+\_\+grid\+\_\+type}}), intent(in)}]{G,  }\item[{type(\mbox{\hyperlink{structmom__verticalgrid_1_1verticalgrid__type}{verticalgrid\+\_\+type}}), intent(in)}]{GV,  }\item[{type(\mbox{\hyperlink{structmom__unit__scaling_1_1unit__scale__type}{unit\+\_\+scale\+\_\+type}}), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__entrain__diffusive_1_1entrain__diffusive__cs}{entrain\+\_\+diffusive\+\_\+cs}}), pointer}]{CS,  }\item[{integer, intent(in)}]{j,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)+1), intent(out)}]{Ent\+\_\+bl,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(out)}]{Sref,  }\item[{real, dimension(szi\+\_\+(g),szk\+\_\+(g)), intent(out)}]{h\+\_\+bl }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine sets the average entrainment across each of the interfaces between buffer layers within a timestep. It also causes thin and relatively light interior layers to be entrained by the deepest buffer layer. Also find the initial coordinate potential densities (Sref) of each layer. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em dtkd\+\_\+int} & The diapycnal diffusivity across \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs. \\
\hline
\mbox{\texttt{ in,out}}  & {\em kb} & The index of the lightest layer denser than the buffer layer or 1 if there is no buffer layer. \\
\hline
\mbox{\texttt{ in}}  & {\em kmb} & The number of mixed and buffer layers. \\
\hline
\mbox{\texttt{ in}}  & {\em do\+\_\+i} & A logical variable indicating which i-\/points to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & This module\textquotesingle{}s control structure. \\
\hline
\mbox{\texttt{ in}}  & {\em j} & The meridional index upon which to work. \\
\hline
\mbox{\texttt{ out}}  & {\em ent\+\_\+bl} & The average entrainment upward and \\
\hline
\mbox{\texttt{ out}}  & {\em sref} & The coordinate potential density minus 1000 for each layer \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em h\+\_\+bl} & The thickness of each layer \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 1024 of file M\+O\+M\+\_\+entrain\+\_\+diffusive.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1025   \textcolor{keywordtype}{type}(ocean\_grid\_type),            \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{1026   \textcolor{keywordtype}{type}(verticalGrid\_type),          \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{1027 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{1028                                     \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{    !< Layer thicknesses [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{1029 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)},       \&}
\DoxyCodeLine{1030                                     \textcolor{keywordtype}{intent(in)}    :: dtKd\_int\textcolor{comment}{ !< The diapycnal diffusivity across}}
\DoxyCodeLine{1031 \textcolor{comment}{                                                          !! each interface times the time step}}
\DoxyCodeLine{1032 \textcolor{comment}{                                                          !! [H2 \string~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{1033   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),            \textcolor{keywordtype}{intent(in)}    :: tv\textcolor{comment}{   !< A structure containing pointers to any}}
\DoxyCodeLine{1034 \textcolor{comment}{                                                          !! available thermodynamic fields. Absent}}
\DoxyCodeLine{1035 \textcolor{comment}{                                                          !! fields have NULL ptrs.}}
\DoxyCodeLine{1036   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G))},      \textcolor{keywordtype}{intent(inout)} :: kb\textcolor{comment}{   !< The index of the lightest layer denser}}
\DoxyCodeLine{1037 \textcolor{comment}{                                                          !! than the buffer layer or 1 if there is}}
\DoxyCodeLine{1038 \textcolor{comment}{                                                          !! no buffer layer.}}
\DoxyCodeLine{1039   \textcolor{keywordtype}{integer},                          \textcolor{keywordtype}{intent(in)}    :: kmb\textcolor{comment}{  !< The number of mixed and buffer layers.}}
\DoxyCodeLine{1040   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(SZI\_(G))},      \textcolor{keywordtype}{intent(in)}    :: do\_i\textcolor{comment}{ !< A logical variable indicating which}}
\DoxyCodeLine{1041 \textcolor{comment}{                                                          !! i-\/points to work on.}}
\DoxyCodeLine{1042   \textcolor{keywordtype}{type}(unit\_scale\_type),            \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}}
\DoxyCodeLine{1043   \textcolor{keywordtype}{type}(entrain\_diffusive\_CS),       \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< This module's control structure.}}
\DoxyCodeLine{1044   \textcolor{keywordtype}{integer},                          \textcolor{keywordtype}{intent(in)}    :: j\textcolor{comment}{    !< The meridional index upon which to work.}}
\DoxyCodeLine{1045 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)},       \&}
\DoxyCodeLine{1046                                     \textcolor{keywordtype}{intent(out)}   :: Ent\_bl\textcolor{comment}{ !< The average entrainment upward and}}
\DoxyCodeLine{1047 \textcolor{comment}{                                                          !! downward across each interface around}}
\DoxyCodeLine{1048 \textcolor{comment}{                                                          !! the buffer layers [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1049 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(out)}   :: Sref\textcolor{comment}{ !< The coordinate potential density minus}}
\DoxyCodeLine{1050 \textcolor{comment}{                                                          !! 1000 for each layer [R \string~> kg m-\/3].}}
\DoxyCodeLine{1051 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(out)}   :: h\_bl\textcolor{comment}{ !< The thickness of each layer [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1052 }
\DoxyCodeLine{1053 \textcolor{comment}{!   This subroutine sets the average entrainment across each of the interfaces}}
\DoxyCodeLine{1054 \textcolor{comment}{! between buffer layers within a timestep. It also causes thin and relatively}}
\DoxyCodeLine{1055 \textcolor{comment}{! light interior layers to be entrained by the deepest buffer layer.}}
\DoxyCodeLine{1056 \textcolor{comment}{!   Also find the initial coordinate potential densities (Sref) of each layer.}}
\DoxyCodeLine{1057 \textcolor{comment}{! Does there need to be limiting when the layers below are all thin?}}
\DoxyCodeLine{1058 }
\DoxyCodeLine{1059   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1060 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: \&}
\DoxyCodeLine{1061     b1, d1, \&   \textcolor{comment}{! Variables used by the tridiagonal solver [H-\/1 \string~> m-\/1 or m2 kg-\/1] and [nondim].}}
\DoxyCodeLine{1062     Rcv, \&      \textcolor{comment}{! Value of the coordinate variable (potential density)}}
\DoxyCodeLine{1063                 \textcolor{comment}{! based on the simulated T and S and P\_Ref [R \string~> kg m-\/3].}}
\DoxyCodeLine{1064     pres, \&     \textcolor{comment}{! Reference pressure (P\_Ref) [R L2 T-\/2 \string~> Pa].}}
\DoxyCodeLine{1065     frac\_rem, \& \textcolor{comment}{! The fraction of the diffusion remaining [nondim].}}
\DoxyCodeLine{1066     h\_interior  \textcolor{comment}{! The interior thickness available for entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1067 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G), SZK\_(G))} :: \&}
\DoxyCodeLine{1068     S\_est \textcolor{comment}{! An estimate of the coordinate potential density -\/ 1000 after}}
\DoxyCodeLine{1069           \textcolor{comment}{! entrainment for each layer [R \string~> kg m-\/3].}}
\DoxyCodeLine{1070 \textcolor{keywordtype}{  real} :: max\_ent  \textcolor{comment}{! The maximum possible entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1071 \textcolor{keywordtype}{  real} :: dh       \textcolor{comment}{! An available thickness [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1072 \textcolor{keywordtype}{  real} :: Kd\_x\_dt  \textcolor{comment}{! The diffusion that remains after thin layers are}}
\DoxyCodeLine{1073                    \textcolor{comment}{! entrained [H2 \string~> m2 or kg2 m-\/4].}}
\DoxyCodeLine{1074 \textcolor{keywordtype}{  real} :: h\_neglect \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{1075                     \textcolor{comment}{! in roundoff and can be neglected [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{1076   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: EOSdom \textcolor{comment}{! The i-\/computational domain for the equation of state}}
\DoxyCodeLine{1077   \textcolor{keywordtype}{integer} :: i, k, is, ie, nz}
\DoxyCodeLine{1078   is = g\%isc ; ie = g\%iec ; nz = g\%ke}
\DoxyCodeLine{1079 }
\DoxyCodeLine{1080 \textcolor{comment}{!  max\_ent = 1.0e14*GV\%Angstrom\_H ! This is set to avoid roundoff problems.}}
\DoxyCodeLine{1081   max\_ent = 1.0e4*gv\%m\_to\_H}
\DoxyCodeLine{1082   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{1083 }
\DoxyCodeLine{1084   \textcolor{keywordflow}{do} i=is,ie ; pres(i) = tv\%P\_Ref ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1085   eosdom(:) = eos\_domain(g\%HI)}
\DoxyCodeLine{1086   \textcolor{keywordflow}{do} k=1,kmb}
\DoxyCodeLine{1087     \textcolor{keyword}{call }calculate\_density(tv\%T(:,j,k), tv\%S(:,j,k), pres, rcv, tv\%eqn\_of\_state, eosdom)}
\DoxyCodeLine{1088     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1089       h\_bl(i,k) = h(i,j,k) + h\_neglect}
\DoxyCodeLine{1090       sref(i,k) = rcv(i) -\/ cs\%Rho\_sig\_off}
\DoxyCodeLine{1091 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1092 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1093 }
\DoxyCodeLine{1094   \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1095     h\_interior(i) = 0.0 ; ent\_bl(i,1) = 0.0}
\DoxyCodeLine{1096 \textcolor{comment}{!     if (kb(i) > nz) Ent\_bl(i,Kmb+1) = 0.0}}
\DoxyCodeLine{1097 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1098 }
\DoxyCodeLine{1099   \textcolor{keywordflow}{do} k=2,kmb ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1100     \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1101       ent\_bl(i,k) = min(2.0 * dtkd\_int(i,k) / (h(i,j,k-\/1) + h(i,j,k) + h\_neglect), \&}
\DoxyCodeLine{1102                         max\_ent)}
\DoxyCodeLine{1103     \textcolor{keywordflow}{else} ; ent\_bl(i,k) = 0.0 ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1104 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1105 }
\DoxyCodeLine{1106   \textcolor{comment}{!   Determine the coordinate density of the bottommost buffer layer if there}}
\DoxyCodeLine{1107   \textcolor{comment}{! is no entrainment from the layers below.  This is a partial solver, based}}
\DoxyCodeLine{1108   \textcolor{comment}{! on the first pass of a tridiagonal solver, as the values in the upper buffer}}
\DoxyCodeLine{1109   \textcolor{comment}{! layers are not needed.}}
\DoxyCodeLine{1110 }
\DoxyCodeLine{1111   \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1112     b1(i) = 1.0 / (h\_bl(i,1) + ent\_bl(i,2))}
\DoxyCodeLine{1113     d1(i) = h\_bl(i,1) * b1(i)  \textcolor{comment}{! = 1.0 -\/ Ent\_bl(i,2)*b1(i)}}
\DoxyCodeLine{1114     s\_est(i,1) = (h\_bl(i,1)*sref(i,1)) * b1(i)}
\DoxyCodeLine{1115 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1116   \textcolor{keywordflow}{do} k=2,kmb-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1117     b1(i) = 1.0 / ((h\_bl(i,k) + ent\_bl(i,k+1)) + d1(i)*ent\_bl(i,k))}
\DoxyCodeLine{1118     d1(i) = (h\_bl(i,k) + d1(i)*ent\_bl(i,k)) * b1(i)  \textcolor{comment}{! = 1.0 -\/ Ent\_bl(i,K+1)*b1(i)}}
\DoxyCodeLine{1119     s\_est(i,k) = (h\_bl(i,k)*sref(i,k) + ent\_bl(i,k)*s\_est(i,k-\/1)) * b1(i)}
\DoxyCodeLine{1120 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1121   \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1122     s\_est(i,kmb) = (h\_bl(i,kmb)*sref(i,kmb) + ent\_bl(i,kmb)*s\_est(i,kmb-\/1)) / \&}
\DoxyCodeLine{1123                    (h\_bl(i,kmb) + d1(i)*ent\_bl(i,kmb))}
\DoxyCodeLine{1124     frac\_rem(i) = 1.0}
\DoxyCodeLine{1125 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1126 }
\DoxyCodeLine{1127   \textcolor{comment}{!   Entrain any thin interior layers that are lighter (in the coordinate}}
\DoxyCodeLine{1128   \textcolor{comment}{! potential density) than the deepest buffer layer will be, and adjust kb.}}
\DoxyCodeLine{1129   \textcolor{keywordflow}{do} i=is,ie ; kb(i) = nz+1 ; \textcolor{keywordflow}{if} (do\_i(i)) kb(i) = kmb+1 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1130 }
\DoxyCodeLine{1131   \textcolor{keywordflow}{do} k=kmb+1,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1132     \textcolor{keywordflow}{if} ((k == kb(i)) .and. (s\_est(i,kmb) > (gv\%Rlay(k) -\/ cs\%Rho\_sig\_off))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1133       \textcolor{keywordflow}{if} (4.0*dtkd\_int(i,kmb+1)*frac\_rem(i) > \&}
\DoxyCodeLine{1134           (h\_bl(i,kmb) + h(i,j,k)) * (h(i,j,k) -\/ gv\%Angstrom\_H)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1135         \textcolor{comment}{! Entrain this layer into the buffer layer and move kb down.}}
\DoxyCodeLine{1136         dh = max((h(i,j,k) -\/ gv\%Angstrom\_H), 0.0)}
\DoxyCodeLine{1137         \textcolor{keywordflow}{if} (dh > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1138           frac\_rem(i) = frac\_rem(i) -\/ ((h\_bl(i,kmb) + h(i,j,k)) * dh) / \&}
\DoxyCodeLine{1139                                        (4.0*dtkd\_int(i,kmb+1))}
\DoxyCodeLine{1140           sref(i,kmb) = (h\_bl(i,kmb)*sref(i,kmb) + dh*(gv\%Rlay(k)-\/cs\%Rho\_sig\_off)) / \&}
\DoxyCodeLine{1141                         (h\_bl(i,kmb) + dh)}
\DoxyCodeLine{1142           h\_bl(i,kmb) = h\_bl(i,kmb) + dh}
\DoxyCodeLine{1143           s\_est(i,kmb) = (h\_bl(i,kmb)*sref(i,kmb) + ent\_bl(i,kmb)*s\_est(i,kmb-\/1)) / \&}
\DoxyCodeLine{1144                          (h\_bl(i,kmb) + d1(i)*ent\_bl(i,kmb))}
\DoxyCodeLine{1145 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1146         kb(i) = kb(i) + 1}
\DoxyCodeLine{1147 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1148 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1149 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1150 }
\DoxyCodeLine{1151  \textcolor{comment}{!    This is where variables are be set up with a different vertical grid}}
\DoxyCodeLine{1152  \textcolor{comment}{!  in which the (newly?) massless layers are taken out.}}
\DoxyCodeLine{1153   \textcolor{keywordflow}{do} k=nz,kmb+1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1154     \textcolor{keywordflow}{if} (k >= kb(i)) h\_interior(i) = h\_interior(i) + (h(i,j,k)-\/gv\%Angstrom\_H)}
\DoxyCodeLine{1155     \textcolor{keywordflow}{if} (k==kb(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1156       h\_bl(i,kmb+1) = h(i,j,k) ; sref(i,kmb+1) = gv\%Rlay(k) -\/ cs\%Rho\_sig\_off}
\DoxyCodeLine{1157     \textcolor{keywordflow}{elseif} (k==kb(i)+1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1158       h\_bl(i,kmb+2) = h(i,j,k) ; sref(i,kmb+2) = gv\%Rlay(k) -\/ cs\%Rho\_sig\_off}
\DoxyCodeLine{1159 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1160 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1161   \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (kb(i) >= nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1162     h\_bl(i,kmb+1) = h(i,j,nz)}
\DoxyCodeLine{1163     sref(i,kmb+1) = gv\%Rlay(nz) -\/ cs\%Rho\_sig\_off}
\DoxyCodeLine{1164     h\_bl(i,kmb+2) = gv\%Angstrom\_H}
\DoxyCodeLine{1165     sref(i,kmb+2) = sref(i,kmb+1) + (gv\%Rlay(nz) -\/ gv\%Rlay(nz-\/1))}
\DoxyCodeLine{1166 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1167 }
\DoxyCodeLine{1168   \textcolor{comment}{!   Perhaps we should revisit the way that the average entrainment between the}}
\DoxyCodeLine{1169   \textcolor{comment}{! buffer layer and the interior is calculated so that it is not unduly}}
\DoxyCodeLine{1170   \textcolor{comment}{! limited when the layers are less than sqrt(Kd * dt) thick?}}
\DoxyCodeLine{1171   \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1172     kd\_x\_dt = frac\_rem(i) * dtkd\_int(i,kmb+1)}
\DoxyCodeLine{1173     \textcolor{keywordflow}{if} ((kd\_x\_dt <= 0.0) .or. (h\_interior(i) <= 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1174       ent\_bl(i,kmb+1) = 0.0}
\DoxyCodeLine{1175     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1176       \textcolor{comment}{!   If the combined layers are exceptionally thin, use sqrt(Kd*dt) as the}}
\DoxyCodeLine{1177       \textcolor{comment}{! estimate of the thickness in the denominator of the thickness diffusion.}}
\DoxyCodeLine{1178       ent\_bl(i,kmb+1) = min(0.5*h\_interior(i), sqrt(kd\_x\_dt), \&}
\DoxyCodeLine{1179                             kd\_x\_dt / (0.5*(h\_bl(i,kmb) + h\_bl(i,kmb+1))))}
\DoxyCodeLine{1180 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1181   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1182     ent\_bl(i,kmb+1) = 0.0}
\DoxyCodeLine{1183 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1184 }

\end{DoxyCode}
