\hypertarget{namespacemom__regularize__layers}{}\doxysection{mom\+\_\+regularize\+\_\+layers Module Reference}
\label{namespacemom__regularize__layers}\index{mom\_regularize\_layers@{mom\_regularize\_layers}}


\doxysubsection{Detailed Description}
Provides regularization of layers in isopycnal mode. \doxysubsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__regularize__layers_1_1regularize__layers__cs}{regularize\+\_\+layers\+\_\+cs}}
\begin{DoxyCompactList}\small\item\em This control structure holds parameters used by the M\+O\+M\+\_\+regularize\+\_\+layers module. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \mbox{\hyperlink{namespacemom__regularize__layers_a6cd6e85c8425099cc30c8d67de17c1ed}{regularize\+\_\+layers}} (h, tv, dt, ea, eb, G, GV, US, CS)
\begin{DoxyCompactList}\small\item\em This subroutine partially steps the bulk mixed layer model. The following processes are executed, in the order listed. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__regularize__layers_a220f46f9b601c8d813a3a94fba61ca0e}{regularize\+\_\+surface}} (h, tv, dt, ea, eb, G, GV, US, CS)
\begin{DoxyCompactList}\small\item\em This subroutine ensures that there is a degree of horizontal smoothness in the depths of the near-\/surface interfaces. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__regularize__layers_a4ba20ced31449e07aa640de2b71c0be8}{find\+\_\+deficit\+\_\+ratios}} (e, def\+\_\+rat\+\_\+u, def\+\_\+rat\+\_\+v, G, GV, CS, def\+\_\+rat\+\_\+u\+\_\+2lay, def\+\_\+rat\+\_\+v\+\_\+2lay, halo, h)
\begin{DoxyCompactList}\small\item\em This subroutine determines the amount by which the harmonic mean thickness at velocity points differ from the arithmetic means, relative to the the arithmetic means, after eliminating thickness variations that are solely due to topography and aggregating all interior layers into one. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__regularize__layers_a7f5b60398ab6cb0dd38cdc3b3053091e}{regularize\+\_\+layers\+\_\+init}} (Time, G, GV, param\+\_\+file, diag, CS)
\begin{DoxyCompactList}\small\item\em Initializes the regularize\+\_\+layers control structure. \end{DoxyCompactList}\end{DoxyCompactItemize}


\doxysubsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__regularize__layers_a4ba20ced31449e07aa640de2b71c0be8}\label{namespacemom__regularize__layers_a4ba20ced31449e07aa640de2b71c0be8}} 
\index{mom\_regularize\_layers@{mom\_regularize\_layers}!find\_deficit\_ratios@{find\_deficit\_ratios}}
\index{find\_deficit\_ratios@{find\_deficit\_ratios}!mom\_regularize\_layers@{mom\_regularize\_layers}}
\doxysubsubsection{\texorpdfstring{find\_deficit\_ratios()}{find\_deficit\_ratios()}}
{\footnotesize\ttfamily subroutine mom\+\_\+regularize\+\_\+layers\+::find\+\_\+deficit\+\_\+ratios (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)+1), intent(in)}]{e,  }\item[{real, dimension(szib\+\_\+(g),szj\+\_\+(g)), intent(out)}]{def\+\_\+rat\+\_\+u,  }\item[{real, dimension(szi\+\_\+(g),szjb\+\_\+(g)), intent(out)}]{def\+\_\+rat\+\_\+v,  }\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__regularize__layers_1_1regularize__layers__cs}{regularize\+\_\+layers\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension(szib\+\_\+(g),szj\+\_\+(g)), intent(out), optional}]{def\+\_\+rat\+\_\+u\+\_\+2lay,  }\item[{real, dimension(szi\+\_\+(g),szjb\+\_\+(g)), intent(out), optional}]{def\+\_\+rat\+\_\+v\+\_\+2lay,  }\item[{integer, intent(in), optional}]{halo,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in), optional}]{h }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine determines the amount by which the harmonic mean thickness at velocity points differ from the arithmetic means, relative to the the arithmetic means, after eliminating thickness variations that are solely due to topography and aggregating all interior layers into one. 


\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 e} & Interface depths \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ out}}  & {\em def\+\_\+rat\+\_\+u} & The thickness deficit ratio at u points, \\
\hline
\mbox{\texttt{ out}}  & {\em def\+\_\+rat\+\_\+v} & The thickness deficit ratio at v points, \\
\hline
 & {\em cs} & The control structure returned by a previous call to regularize\+\_\+layers\+\_\+init. \\
\hline
\mbox{\texttt{ out}}  & {\em def\+\_\+rat\+\_\+u\+\_\+2lay} & The thickness deficit ratio at u \\
\hline
\mbox{\texttt{ out}}  & {\em def\+\_\+rat\+\_\+v\+\_\+2lay} & The thickness deficit ratio at v \\
\hline
\mbox{\texttt{ in}}  & {\em halo} & An extra-\/wide halo size, 0 by default. \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 615 of file M\+O\+M\+\_\+regularize\+\_\+layers.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{617   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(in)}  :: G\textcolor{comment}{         !< The ocean's grid structure.}}
\DoxyCodeLine{618   \textcolor{keywordtype}{type}(verticalGrid\_type),    \textcolor{keywordtype}{intent(in)}  :: GV\textcolor{comment}{        !< The ocean's vertical grid structure.}}
\DoxyCodeLine{619 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G)+1)}, \&}
\DoxyCodeLine{620                               \textcolor{keywordtype}{intent(in)}  :: e\textcolor{comment}{         !< Interface depths [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{621 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))},          \&}
\DoxyCodeLine{622                               \textcolor{keywordtype}{intent(out)} :: def\_rat\_u\textcolor{comment}{ !< The thickness deficit ratio at u points,}}
\DoxyCodeLine{623 \textcolor{comment}{                                                       !! [nondim].}}
\DoxyCodeLine{624 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))},          \&}
\DoxyCodeLine{625                               \textcolor{keywordtype}{intent(out)} :: def\_rat\_v\textcolor{comment}{ !< The thickness deficit ratio at v points,}}
\DoxyCodeLine{626 \textcolor{comment}{                                                       !! [nondim].}}
\DoxyCodeLine{627   \textcolor{keywordtype}{type}(regularize\_layers\_CS), \textcolor{keywordtype}{pointer}     :: CS\textcolor{comment}{        !< The control structure returned by a}}
\DoxyCodeLine{628 \textcolor{comment}{                                                       !! previous call to regularize\_layers\_init.}}
\DoxyCodeLine{629 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))},          \&}
\DoxyCodeLine{630                     \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: def\_rat\_u\_2lay\textcolor{comment}{ !< The thickness deficit ratio at u}}
\DoxyCodeLine{631 \textcolor{comment}{                                                       !! points when the mixed and buffer layers}}
\DoxyCodeLine{632 \textcolor{comment}{                                                       !! are aggregated into 1 layer [nondim].}}
\DoxyCodeLine{633 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))},          \&}
\DoxyCodeLine{634                     \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: def\_rat\_v\_2lay\textcolor{comment}{ !< The thickness deficit ratio at v}}
\DoxyCodeLine{635 \textcolor{comment}{                                                       !! pointswhen the mixed and buffer layers}}
\DoxyCodeLine{636 \textcolor{comment}{                                                       !! are aggregated into 1 layer [nondim].}}
\DoxyCodeLine{637   \textcolor{keywordtype}{integer},          \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: halo\textcolor{comment}{      !< An extra-\/wide halo size, 0 by default.}}
\DoxyCodeLine{638 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},   \&}
\DoxyCodeLine{639                     \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: h\textcolor{comment}{         !< Layer thicknesses [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{640 \textcolor{comment}{                                                       !! If h is not present, vertical differences}}
\DoxyCodeLine{641 \textcolor{comment}{                                                       !! in interface heights are used instead.}}
\DoxyCodeLine{642   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{643 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))} :: \&}
\DoxyCodeLine{644     h\_def\_u, \&  \textcolor{comment}{! The vertically summed thickness deficits at u-\/points [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{645     h\_norm\_u, \& \textcolor{comment}{! The vertically summed arithmetic mean thickness by which}}
\DoxyCodeLine{646                 \textcolor{comment}{! h\_def\_u is normalized [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{647     h\_def2\_u}
\DoxyCodeLine{648 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))} :: \&}
\DoxyCodeLine{649     h\_def\_v, \&  \textcolor{comment}{! The vertically summed thickness deficits at v-\/points [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{650     h\_norm\_v, \& \textcolor{comment}{! The vertically summed arithmetic mean thickness by which}}
\DoxyCodeLine{651                 \textcolor{comment}{! h\_def\_v is normalized [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{652     h\_def2\_v}
\DoxyCodeLine{653 \textcolor{keywordtype}{  real} :: h\_neglect \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{654                     \textcolor{comment}{! in roundoff and can be neglected [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{655 \textcolor{keywordtype}{  real} :: Hmix\_min  \textcolor{comment}{! A local copy of CS\%Hmix\_min [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{656 \textcolor{keywordtype}{  real} :: h1, h2  \textcolor{comment}{! Temporary thicknesses [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{657   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz, nkmb}
\DoxyCodeLine{658 }
\DoxyCodeLine{659   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = g\%ke}
\DoxyCodeLine{660   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(halo)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{661     is = g\%isc-\/halo ; ie = g\%iec+halo ; js = g\%jsc-\/halo ; je = g\%jec+halo}
\DoxyCodeLine{662 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{663   nkmb = gv\%nk\_rho\_varies}
\DoxyCodeLine{664   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{665   hmix\_min = cs\%Hmix\_min}
\DoxyCodeLine{666 }
\DoxyCodeLine{667   \textcolor{comment}{! Determine which zonal faces are problematic.}}
\DoxyCodeLine{668   \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{669     \textcolor{comment}{! Aggregate all water below the mixed and buffer layers for the purposes of}}
\DoxyCodeLine{670     \textcolor{comment}{! this diagnostic.}}
\DoxyCodeLine{671     h1 = e(i,j,nkmb+1)-\/e(i,j,nz+1) ; h2 = e(i+1,j,nkmb+1)-\/e(i+1,j,nz+1)}
\DoxyCodeLine{672     \textcolor{keywordflow}{if} (e(i,j,nz+1) < e(i+1,j,nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{673       \textcolor{keywordflow}{if} (h1 > h2) h1 = max(e(i,j,nkmb+1)-\/e(i+1,j,nz+1), h2)}
\DoxyCodeLine{674     \textcolor{keywordflow}{elseif} (e(i+1,j,nz+1) < e(i,j,nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{675       \textcolor{keywordflow}{if} (h2 > h1) h2 = max(e(i+1,j,nkmb+1)-\/e(i,j,nz+1), h1)}
\DoxyCodeLine{676 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{677     h\_def\_u(i,j) = 0.5*(h1-\/h2)**2 / ((h1 + h2) + h\_neglect)}
\DoxyCodeLine{678     h\_norm\_u(i,j) = 0.5*(h1+h2)}
\DoxyCodeLine{679 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{680   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(def\_rat\_u\_2lay)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{681     \textcolor{comment}{! This is a particular metric of the aggregation into two layers.}}
\DoxyCodeLine{682     h1 = e(i,j,1)-\/e(i,j,nkmb+1) ; h2 = e(i+1,j,1)-\/e(i+1,j,nkmb+1)}
\DoxyCodeLine{683     \textcolor{keywordflow}{if} (e(i,j,nkmb+1) < e(i+1,j,nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{684       \textcolor{keywordflow}{if} (h1 > h2) h1 = max(e(i,j,1)-\/e(i+1,j,nz+1), h2)}
\DoxyCodeLine{685     \textcolor{keywordflow}{elseif} (e(i+1,j,nkmb+1) < e(i,j,nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{686       \textcolor{keywordflow}{if} (h2 > h1) h2 = max(e(i+1,j,1)-\/e(i,j,nz+1), h1)}
\DoxyCodeLine{687 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{688     h\_def2\_u(i,j) = h\_def\_u(i,j) + 0.5*(h1-\/h2)**2 / ((h1 + h2) + h\_neglect)}
\DoxyCodeLine{689 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{690   \textcolor{keywordflow}{do} k=1,nkmb ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{691     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(h)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{692       h1 = h(i,j,k) ; h2 = h(i+1,j,k)}
\DoxyCodeLine{693     \textcolor{keywordflow}{else}}
\DoxyCodeLine{694       h1 = e(i,j,k)-\/e(i,j,k+1) ; h2 = e(i+1,j,k)-\/e(i+1,j,k+1)}
\DoxyCodeLine{695 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{696     \textcolor{comment}{! Thickness deficits can not arise simply because a layer's bottom is bounded}}
\DoxyCodeLine{697     \textcolor{comment}{! by the bathymetry.}}
\DoxyCodeLine{698     \textcolor{keywordflow}{if} (e(i,j,k+1) < e(i+1,j,nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{699       \textcolor{keywordflow}{if} (h1 > h2) h1 = max(e(i,j,k)-\/e(i+1,j,nz+1), h2)}
\DoxyCodeLine{700     \textcolor{keywordflow}{elseif} (e(i+1,j,k+1) < e(i,j,nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{701       \textcolor{keywordflow}{if} (h2 > h1) h2 = max(e(i+1,j,k)-\/e(i,j,nz+1), h1)}
\DoxyCodeLine{702 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{703     h\_def\_u(i,j) = h\_def\_u(i,j) + 0.5*(h1-\/h2)**2 / ((h1 + h2) + h\_neglect)}
\DoxyCodeLine{704     h\_norm\_u(i,j) = h\_norm\_u(i,j) + 0.5*(h1+h2)}
\DoxyCodeLine{705 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{706   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(def\_rat\_u\_2lay)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{707     def\_rat\_u(i,j) = g\%mask2dCu(i,j) * h\_def\_u(i,j) / \&}
\DoxyCodeLine{708                      (max(hmix\_min, h\_norm\_u(i,j)) + h\_neglect)}
\DoxyCodeLine{709     def\_rat\_u\_2lay(i,j) = g\%mask2dCu(i,j) * h\_def2\_u(i,j) / \&}
\DoxyCodeLine{710                           (max(hmix\_min, h\_norm\_u(i,j)) + h\_neglect)}
\DoxyCodeLine{711 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ; \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{712     def\_rat\_u(i,j) = g\%mask2dCu(i,j) * h\_def\_u(i,j) / \&}
\DoxyCodeLine{713                      (max(hmix\_min, h\_norm\_u(i,j)) + h\_neglect)}
\DoxyCodeLine{714 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{715 }
\DoxyCodeLine{716   \textcolor{comment}{! Determine which meridional faces are problematic.}}
\DoxyCodeLine{717   \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{718     \textcolor{comment}{! Aggregate all water below the mixed and buffer layers for the purposes of}}
\DoxyCodeLine{719     \textcolor{comment}{! this diagnostic.}}
\DoxyCodeLine{720     h1 = e(i,j,nkmb+1)-\/e(i,j,nz+1) ; h2 = e(i,j+1,nkmb+1)-\/e(i,j+1,nz+1)}
\DoxyCodeLine{721     \textcolor{keywordflow}{if} (e(i,j,nz+1) < e(i,j+1,nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{722       \textcolor{keywordflow}{if} (h1 > h2) h1 = max(e(i,j,nkmb+1)-\/e(i,j+1,nz+1), h2)}
\DoxyCodeLine{723     \textcolor{keywordflow}{elseif} (e(i,j+1,nz+1) < e(i,j,nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{724       \textcolor{keywordflow}{if} (h2 > h1) h2 = max(e(i,j+1,nkmb+1)-\/e(i,j,nz+1), h1)}
\DoxyCodeLine{725 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{726     h\_def\_v(i,j) = 0.5*(h1-\/h2)**2 / ((h1 + h2) + h\_neglect)}
\DoxyCodeLine{727     h\_norm\_v(i,j) = 0.5*(h1+h2)}
\DoxyCodeLine{728 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{729   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(def\_rat\_v\_2lay)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{730     \textcolor{comment}{! This is a particular metric of the aggregation into two layers.}}
\DoxyCodeLine{731     h1 = e(i,j,1)-\/e(i,j,nkmb+1) ; h2 = e(i,j+1,1)-\/e(i,j+1,nkmb+1)}
\DoxyCodeLine{732     \textcolor{keywordflow}{if} (e(i,j,nkmb+1) < e(i,j+1,nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{733       \textcolor{keywordflow}{if} (h1 > h2) h1 = max(e(i,j,1)-\/e(i,j+1,nz+1), h2)}
\DoxyCodeLine{734     \textcolor{keywordflow}{elseif} (e(i,j+1,nkmb+1) < e(i,j,nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{735       \textcolor{keywordflow}{if} (h2 > h1) h2 = max(e(i,j+1,1)-\/e(i,j,nz+1), h1)}
\DoxyCodeLine{736 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{737     h\_def2\_v(i,j) = h\_def\_v(i,j) + 0.5*(h1-\/h2)**2 / ((h1 + h2) + h\_neglect)}
\DoxyCodeLine{738 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{739   \textcolor{keywordflow}{do} k=1,nkmb ; \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{740     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(h)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{741       h1 = h(i,j,k) ; h2 = h(i,j+1,k)}
\DoxyCodeLine{742     \textcolor{keywordflow}{else}}
\DoxyCodeLine{743       h1 = e(i,j,k)-\/e(i,j,k+1) ; h2 = e(i,j+1,k)-\/e(i,j+1,k+1)}
\DoxyCodeLine{744 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{745     \textcolor{comment}{! Thickness deficits can not arise simply because a layer's bottom is bounded}}
\DoxyCodeLine{746     \textcolor{comment}{! by the bathymetry.}}
\DoxyCodeLine{747     \textcolor{keywordflow}{if} (e(i,j,k+1) < e(i,j+1,nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{748       \textcolor{keywordflow}{if} (h1 > h2) h1 = max(e(i,j,k)-\/e(i,j+1,nz+1), h2)}
\DoxyCodeLine{749     \textcolor{keywordflow}{elseif} (e(i,j+1,k+1) < e(i,j,nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{750       \textcolor{keywordflow}{if} (h2 > h1) h2 = max(e(i,j+1,k)-\/e(i,j,nz+1), h1)}
\DoxyCodeLine{751 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{752     h\_def\_v(i,j) = h\_def\_v(i,j) + 0.5*(h1-\/h2)**2 / ((h1 + h2) + h\_neglect)}
\DoxyCodeLine{753     h\_norm\_v(i,j) = h\_norm\_v(i,j) + 0.5*(h1+h2)}
\DoxyCodeLine{754 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{755   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(def\_rat\_v\_2lay)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{756     def\_rat\_v(i,j) = g\%mask2dCv(i,j) * h\_def\_v(i,j) / \&}
\DoxyCodeLine{757                       (max(hmix\_min, h\_norm\_v(i,j)) + h\_neglect)}
\DoxyCodeLine{758     def\_rat\_v\_2lay(i,j) = g\%mask2dCv(i,j) * h\_def2\_v(i,j) / \&}
\DoxyCodeLine{759                       (max(hmix\_min, h\_norm\_v(i,j)) + h\_neglect)}
\DoxyCodeLine{760 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ; \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{761     def\_rat\_v(i,j) = g\%mask2dCv(i,j) * h\_def\_v(i,j) / \&}
\DoxyCodeLine{762                       (max(hmix\_min, h\_norm\_v(i,j)) + h\_neglect)}
\DoxyCodeLine{763 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{764 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__regularize__layers_a6cd6e85c8425099cc30c8d67de17c1ed}\label{namespacemom__regularize__layers_a6cd6e85c8425099cc30c8d67de17c1ed}} 
\index{mom\_regularize\_layers@{mom\_regularize\_layers}!regularize\_layers@{regularize\_layers}}
\index{regularize\_layers@{regularize\_layers}!mom\_regularize\_layers@{mom\_regularize\_layers}}
\doxysubsubsection{\texorpdfstring{regularize\_layers()}{regularize\_layers()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+regularize\+\_\+layers\+::regularize\+\_\+layers (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(inout)}]{h,  }\item[{type(\mbox{\hyperlink{structmom__variables_1_1thermo__var__ptrs}{thermo\+\_\+var\+\_\+ptrs}}), intent(inout)}]{tv,  }\item[{real, intent(in)}]{dt,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(inout)}]{ea,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(inout)}]{eb,  }\item[{type(\mbox{\hyperlink{structmom__grid_1_1ocean__grid__type}{ocean\+\_\+grid\+\_\+type}}), intent(inout)}]{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__regularize__layers_1_1regularize__layers__cs}{regularize\+\_\+layers\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



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


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in,out}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em ea} & The amount of fluid moved downward into a \\
\hline
\mbox{\texttt{ in,out}}  & {\em eb} & The amount of fluid moved upward into a layer \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & The control structure returned by a previous call to regularize\+\_\+layers\+\_\+init. \\
\hline
\end{DoxyParams}


Definition at line 71 of file M\+O\+M\+\_\+regularize\+\_\+layers.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{72   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{  !< The ocean's grid structure.}}
\DoxyCodeLine{73   \textcolor{keywordtype}{type}(verticalGrid\_type),    \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{ !< The ocean's vertical grid structure.}}
\DoxyCodeLine{74 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{75                               \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{  !< Layer thicknesses [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{76   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),      \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{ !< A structure containing pointers to any}}
\DoxyCodeLine{77 \textcolor{comment}{                                                  !! available thermodynamic fields. Absent fields}}
\DoxyCodeLine{78 \textcolor{comment}{                                                  !! have NULL ptrs.}}
\DoxyCodeLine{79 \textcolor{keywordtype}{  real},                       \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{ !< Time increment [T \string~> s].}}
\DoxyCodeLine{80 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{81                               \textcolor{keywordtype}{intent(inout)} :: ea\textcolor{comment}{ !< The amount of fluid moved downward into a}}
\DoxyCodeLine{82 \textcolor{comment}{                                                  !! layer; this should be increased due to mixed}}
\DoxyCodeLine{83 \textcolor{comment}{                                                  !! layer detrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{84 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{85                               \textcolor{keywordtype}{intent(inout)} :: eb\textcolor{comment}{ !< The amount of fluid moved upward into a layer}}
\DoxyCodeLine{86 \textcolor{comment}{                                                  !! this should be increased due to mixed layer}}
\DoxyCodeLine{87 \textcolor{comment}{                                                  !! entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{88   \textcolor{keywordtype}{type}(unit\_scale\_type),      \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{ !< A dimensional unit scaling type}}
\DoxyCodeLine{89   \textcolor{keywordtype}{type}(regularize\_layers\_CS), \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{ !< The control structure returned by a previous}}
\DoxyCodeLine{90 \textcolor{comment}{                                                  !! call to regularize\_layers\_init.}}
\DoxyCodeLine{91   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{92   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz}
\DoxyCodeLine{93 }
\DoxyCodeLine{94   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = g\%ke}
\DoxyCodeLine{95 }
\DoxyCodeLine{96   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"{}MOM\_regularize\_layers: "{}}//\&}
\DoxyCodeLine{97          \textcolor{stringliteral}{"{}Module must be initialized before it is used."{}})}
\DoxyCodeLine{98 }
\DoxyCodeLine{99   \textcolor{keywordflow}{if} (cs\%regularize\_surface\_layers) \textcolor{keywordflow}{then}}
\DoxyCodeLine{100     \textcolor{keyword}{call }pass\_var(h, g\%Domain, clock=id\_clock\_pass)}
\DoxyCodeLine{101     \textcolor{keyword}{call }regularize\_surface(h, tv, dt, ea, eb, g, gv, us, cs)}
\DoxyCodeLine{102 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{103 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__regularize__layers_a7f5b60398ab6cb0dd38cdc3b3053091e}\label{namespacemom__regularize__layers_a7f5b60398ab6cb0dd38cdc3b3053091e}} 
\index{mom\_regularize\_layers@{mom\_regularize\_layers}!regularize\_layers\_init@{regularize\_layers\_init}}
\index{regularize\_layers\_init@{regularize\_layers\_init}!mom\_regularize\_layers@{mom\_regularize\_layers}}
\doxysubsubsection{\texorpdfstring{regularize\_layers\_init()}{regularize\_layers\_init()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+regularize\+\_\+layers\+::regularize\+\_\+layers\+\_\+init (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), intent(in), target}]{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__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__regularize__layers_1_1regularize__layers__cs}{regularize\+\_\+layers\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Initializes the regularize\+\_\+layers control structure. 


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


Definition at line 768 of file M\+O\+M\+\_\+regularize\+\_\+layers.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{769   \textcolor{keywordtype}{type}(time\_type), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{ !< The current model time.}}
\DoxyCodeLine{770   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{771   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{772   \textcolor{keywordtype}{type}(param\_file\_type),   \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< A structure to parse for}}
\DoxyCodeLine{773 \textcolor{comment}{                                                 !! run-\/time parameters.}}
\DoxyCodeLine{774   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{ !< A structure that is used to regulate}}
\DoxyCodeLine{775 \textcolor{comment}{                                                 !! diagnostic output.}}
\DoxyCodeLine{776   \textcolor{keywordtype}{type}(regularize\_layers\_CS), \textcolor{keywordtype}{pointer}    :: CS\textcolor{comment}{   !< A pointer that is set to point to the}}
\DoxyCodeLine{777 \textcolor{comment}{                                                 !! control structure for this module.}}
\DoxyCodeLine{778 \textcolor{preprocessor}{\#include "{}version\_variable.h"{}}}
\DoxyCodeLine{779 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"{}MOM\_regularize\_layers"{}}  \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{780   \textcolor{keywordtype}{logical} :: use\_temperature}
\DoxyCodeLine{781   \textcolor{keywordtype}{logical} :: default\_2018\_answers}
\DoxyCodeLine{782   \textcolor{keywordtype}{logical} :: just\_read}
\DoxyCodeLine{783   \textcolor{keywordtype}{integer} :: isd, ied, jsd, jed}
\DoxyCodeLine{784   isd = g\%isd ; ied = g\%ied ; jsd = g\%jsd ; jed = g\%jed}
\DoxyCodeLine{785 }
\DoxyCodeLine{786   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{787     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"{}regularize\_layers\_init called with an associated"{}}// \&}
\DoxyCodeLine{788                             \textcolor{stringliteral}{"{}associated control structure."{}})}
\DoxyCodeLine{789     \textcolor{keywordflow}{return}}
\DoxyCodeLine{790   \textcolor{keywordflow}{else} ; \textcolor{keyword}{allocate}(cs) ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{791 }
\DoxyCodeLine{792   cs\%diag => diag}
\DoxyCodeLine{793   cs\%Time => time}
\DoxyCodeLine{794 }
\DoxyCodeLine{795 \textcolor{comment}{! Set default, read and log parameters}}
\DoxyCodeLine{796   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}REGULARIZE\_SURFACE\_LAYERS"{}}, cs\%regularize\_surface\_layers, \&}
\DoxyCodeLine{797                  default=.false., do\_not\_log=.true.)}
\DoxyCodeLine{798   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \textcolor{stringliteral}{"{}"{}}, all\_default=.not.cs\%regularize\_surface\_layers)}
\DoxyCodeLine{799   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}REGULARIZE\_SURFACE\_LAYERS"{}}, cs\%regularize\_surface\_layers, \&}
\DoxyCodeLine{800                  \textcolor{stringliteral}{"{}If defined, vertically restructure the near-\/surface "{}}//\&}
\DoxyCodeLine{801                  \textcolor{stringliteral}{"{}layers when they have too much lateral variations to "{}}//\&}
\DoxyCodeLine{802                  \textcolor{stringliteral}{"{}allow for sensible lateral barotropic transports."{}}, \&}
\DoxyCodeLine{803                  default=.false.)}
\DoxyCodeLine{804   just\_read = .not.cs\%regularize\_surface\_layers}
\DoxyCodeLine{805   \textcolor{keywordflow}{if} (cs\%regularize\_surface\_layers) \textcolor{keywordflow}{then}}
\DoxyCodeLine{806     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}REGULARIZE\_SURFACE\_DETRAIN"{}}, cs\%reg\_sfc\_detrain, \&}
\DoxyCodeLine{807                  \textcolor{stringliteral}{"{}If true, allow the buffer layers to detrain into the "{}}//\&}
\DoxyCodeLine{808                  \textcolor{stringliteral}{"{}interior as a part of the restructuring when "{}}//\&}
\DoxyCodeLine{809                  \textcolor{stringliteral}{"{}REGULARIZE\_SURFACE\_LAYERS is true."{}}, default=.true., do\_not\_log=just\_read)}
\DoxyCodeLine{810     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}REG\_SFC\_DENSE\_MATCH\_TOLERANCE"{}}, cs\%density\_match\_tol, \&}
\DoxyCodeLine{811                  \textcolor{stringliteral}{"{}A relative tolerance for how well the densities must match with the target "{}}//\&}
\DoxyCodeLine{812                  \textcolor{stringliteral}{"{}densities during detrainment when regularizing the near-\/surface layers.  The "{}}//\&}
\DoxyCodeLine{813                  \textcolor{stringliteral}{"{}default of 0.6 gives 20\% overlaps in density"{}}, \&}
\DoxyCodeLine{814                  units=\textcolor{stringliteral}{"{}nondim"{}}, default=0.6, do\_not\_log=just\_read)}
\DoxyCodeLine{815     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}DEFAULT\_2018\_ANSWERS"{}}, default\_2018\_answers, \&}
\DoxyCodeLine{816                  \textcolor{stringliteral}{"{}This sets the default value for the various \_2018\_ANSWERS parameters."{}}, \&}
\DoxyCodeLine{817                  default=.false., do\_not\_log=just\_read)}
\DoxyCodeLine{818     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}REGULARIZE\_LAYERS\_2018\_ANSWERS"{}}, cs\%answers\_2018, \&}
\DoxyCodeLine{819                  \textcolor{stringliteral}{"{}If true, use the order of arithmetic and expressions that recover the answers "{}}//\&}
\DoxyCodeLine{820                  \textcolor{stringliteral}{"{}from the end of 2018.  Otherwise, use updated and more robust forms of the "{}}//\&}
\DoxyCodeLine{821                  \textcolor{stringliteral}{"{}same expressions."{}}, default=default\_2018\_answers, do\_not\_log=just\_read)}
\DoxyCodeLine{822 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{823 }
\DoxyCodeLine{824   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}HMIX\_MIN"{}}, cs\%Hmix\_min, \&}
\DoxyCodeLine{825                  \textcolor{stringliteral}{"{}The minimum mixed layer depth if the mixed layer depth is determined "{}}//\&}
\DoxyCodeLine{826                  \textcolor{stringliteral}{"{}dynamically."{}}, units=\textcolor{stringliteral}{"{}m"{}}, default=0.0, scale=gv\%m\_to\_H, do\_not\_log=just\_read)}
\DoxyCodeLine{827   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}REG\_SFC\_DEFICIT\_TOLERANCE"{}}, cs\%h\_def\_tol1, \&}
\DoxyCodeLine{828                  \textcolor{stringliteral}{"{}The value of the relative thickness deficit at which "{}}//\&}
\DoxyCodeLine{829                  \textcolor{stringliteral}{"{}to start modifying the layer structure when "{}}//\&}
\DoxyCodeLine{830                  \textcolor{stringliteral}{"{}REGULARIZE\_SURFACE\_LAYERS is true."{}}, units=\textcolor{stringliteral}{"{}nondim"{}}, \&}
\DoxyCodeLine{831                  default=0.5, do\_not\_log=just\_read)}
\DoxyCodeLine{832   cs\%h\_def\_tol2 = 0.2 + 0.8*cs\%h\_def\_tol1}
\DoxyCodeLine{833   cs\%h\_def\_tol3 = 0.3 + 0.7*cs\%h\_def\_tol1}
\DoxyCodeLine{834   cs\%h\_def\_tol4 = 0.5 + 0.5*cs\%h\_def\_tol1}
\DoxyCodeLine{835 }
\DoxyCodeLine{836   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}DEBUG"{}}, cs\%debug, default=.false.)}
\DoxyCodeLine{837 \textcolor{comment}{!  if (.not. CS\%debug) \&}}
\DoxyCodeLine{838 \textcolor{comment}{!    call get\_param(param\_file, mdl, "{}DEBUG\_CONSERVATION"{}, CS\%debug, \&}}
\DoxyCodeLine{839 \textcolor{comment}{!                 "{}If true, monitor conservation and extrema."{}, default=.false., do\_not\_log=just\_read)}}
\DoxyCodeLine{840 }
\DoxyCodeLine{841   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"{}ALLOW\_CLOCKS\_IN\_OMP\_LOOPS"{}}, cs\%allow\_clocks\_in\_omp\_loops, \&}
\DoxyCodeLine{842                  \textcolor{stringliteral}{"{}If true, clocks can be called from inside loops that can "{}}//\&}
\DoxyCodeLine{843                  \textcolor{stringliteral}{"{}be threaded. To run with multiple threads, set to False."{}}, \&}
\DoxyCodeLine{844                  default=.true., do\_not\_log=just\_read)}
\DoxyCodeLine{845 }
\DoxyCodeLine{846   \textcolor{keywordflow}{if} (.not.cs\%regularize\_surface\_layers) \textcolor{keywordflow}{return}}
\DoxyCodeLine{847 }
\DoxyCodeLine{848   cs\%id\_def\_rat = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'deficit\_ratio'}, diag\%axesT1, \&}
\DoxyCodeLine{849       time, \textcolor{stringliteral}{'Max face thickness deficit ratio'}, \textcolor{stringliteral}{'nondim'})}
\DoxyCodeLine{850 }
\DoxyCodeLine{851   \textcolor{keywordflow}{if} (cs\%allow\_clocks\_in\_omp\_loops) \textcolor{keywordflow}{then}}
\DoxyCodeLine{852     id\_clock\_eos = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean regularize\_layers EOS)'}, grain=clock\_routine)}
\DoxyCodeLine{853 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{854   id\_clock\_pass = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean regularize\_layers halo updates)'}, grain=clock\_routine)}
\DoxyCodeLine{855 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__regularize__layers_a220f46f9b601c8d813a3a94fba61ca0e}\label{namespacemom__regularize__layers_a220f46f9b601c8d813a3a94fba61ca0e}} 
\index{mom\_regularize\_layers@{mom\_regularize\_layers}!regularize\_surface@{regularize\_surface}}
\index{regularize\_surface@{regularize\_surface}!mom\_regularize\_layers@{mom\_regularize\_layers}}
\doxysubsubsection{\texorpdfstring{regularize\_surface()}{regularize\_surface()}}
{\footnotesize\ttfamily subroutine mom\+\_\+regularize\+\_\+layers\+::regularize\+\_\+surface (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(inout)}]{h,  }\item[{type(\mbox{\hyperlink{structmom__variables_1_1thermo__var__ptrs}{thermo\+\_\+var\+\_\+ptrs}}), intent(inout)}]{tv,  }\item[{real, intent(in)}]{dt,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(inout)}]{ea,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(inout)}]{eb,  }\item[{type(\mbox{\hyperlink{structmom__grid_1_1ocean__grid__type}{ocean\+\_\+grid\+\_\+type}}), intent(inout)}]{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__regularize__layers_1_1regularize__layers__cs}{regularize\+\_\+layers\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine ensures that there is a degree of horizontal smoothness in the depths of the near-\/surface interfaces. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in,out}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em ea} & The amount of fluid moved downward into a \\
\hline
\mbox{\texttt{ in,out}}  & {\em eb} & The amount of fluid moved upward into a layer \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & The control structure returned by a previous call to regularize\+\_\+layers\+\_\+init. \\
\hline
\end{DoxyParams}


Definition at line 108 of file M\+O\+M\+\_\+regularize\+\_\+layers.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{109   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{  !< The ocean's grid structure.}}
\DoxyCodeLine{110   \textcolor{keywordtype}{type}(verticalGrid\_type),    \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{ !< The ocean's vertical grid structure.}}
\DoxyCodeLine{111 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{112                               \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{  !< Layer thicknesses [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{113   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),      \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{ !< A structure containing pointers to any}}
\DoxyCodeLine{114 \textcolor{comment}{                                                  !! available thermodynamic fields. Absent fields}}
\DoxyCodeLine{115 \textcolor{comment}{                                                  !! have NULL ptrs.}}
\DoxyCodeLine{116 \textcolor{keywordtype}{  real},                       \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{ !< Time increment [T \string~> s].}}
\DoxyCodeLine{117 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{118                               \textcolor{keywordtype}{intent(inout)} :: ea\textcolor{comment}{ !< The amount of fluid moved downward into a}}
\DoxyCodeLine{119 \textcolor{comment}{                                                  !! layer; this should be increased due to mixed}}
\DoxyCodeLine{120 \textcolor{comment}{                                                  !! layer detrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{121 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{122                               \textcolor{keywordtype}{intent(inout)} :: eb\textcolor{comment}{ !< The amount of fluid moved upward into a layer}}
\DoxyCodeLine{123 \textcolor{comment}{                                                  !! this should be increased due to mixed layer}}
\DoxyCodeLine{124 \textcolor{comment}{                                                  !! entrainment [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{125   \textcolor{keywordtype}{type}(unit\_scale\_type),      \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{ !< A dimensional unit scaling type}}
\DoxyCodeLine{126   \textcolor{keywordtype}{type}(regularize\_layers\_CS), \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{ !< The control structure returned by a previous}}
\DoxyCodeLine{127 \textcolor{comment}{                                                  !! call to regularize\_layers\_init.}}
\DoxyCodeLine{128   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{129 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))} :: \&}
\DoxyCodeLine{130     def\_rat\_u   \textcolor{comment}{! The ratio of the thickness deficit to the minimum depth [nondim].}}
\DoxyCodeLine{131 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))} :: \&}
\DoxyCodeLine{132     def\_rat\_v   \textcolor{comment}{! The ratio of the thickness deficit to the minimum depth [nondim].}}
\DoxyCodeLine{133 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: \&}
\DoxyCodeLine{134     def\_rat\_h   \textcolor{comment}{! The ratio of the thickness deficit to the minimum depth [nondim].}}
\DoxyCodeLine{135 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G)+1)} :: \&}
\DoxyCodeLine{136     e           \textcolor{comment}{! The interface depths [H \string~> m or kg m-\/2], positive upward.}}
\DoxyCodeLine{137 }
\DoxyCodeLine{138 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G)+1)} :: \&}
\DoxyCodeLine{139     e\_filt, e\_2d  \textcolor{comment}{! The interface depths [H \string~> m or kg m-\/2], positive upward.}}
\DoxyCodeLine{140 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(G))} :: \&}
\DoxyCodeLine{141     h\_2d, \&     \textcolor{comment}{!   A 2-\/d version of h [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{142     T\_2d, \&     \textcolor{comment}{!   A 2-\/d version of tv\%T [degC].}}
\DoxyCodeLine{143     S\_2d, \&     \textcolor{comment}{!   A 2-\/d version of tv\%S [ppt].}}
\DoxyCodeLine{144     Rcv, \&      \textcolor{comment}{!   A 2-\/d version of the coordinate density [R \string~> kg m-\/3].}}
\DoxyCodeLine{145     h\_2d\_init, \&  \textcolor{comment}{! The initial value of h\_2d [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{146     T\_2d\_init, \&  \textcolor{comment}{! THe initial value of T\_2d [degC].}}
\DoxyCodeLine{147     S\_2d\_init, \&  \textcolor{comment}{! The initial value of S\_2d [ppt].}}
\DoxyCodeLine{148     d\_eb, \&     \textcolor{comment}{!   The downward increase across a layer in the entrainment from}}
\DoxyCodeLine{149                 \textcolor{comment}{! below [H \string~> m or kg m-\/2].  The sign convention is that positive values of}}
\DoxyCodeLine{150                 \textcolor{comment}{! d\_eb correspond to a gain in mass by a layer by upward motion.}}
\DoxyCodeLine{151     d\_ea        \textcolor{comment}{!   The upward increase across a layer in the entrainment from}}
\DoxyCodeLine{152                 \textcolor{comment}{! above [H \string~> m or kg m-\/2].  The sign convention is that positive values of}}
\DoxyCodeLine{153                 \textcolor{comment}{! d\_ea mean a net gain in mass by a layer from downward motion.}}
\DoxyCodeLine{154 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: \&}
\DoxyCodeLine{155     p\_ref\_cv, \& \textcolor{comment}{!   Reference pressure for the potential density which defines}}
\DoxyCodeLine{156                 \textcolor{comment}{! the coordinate variable, set to P\_Ref [R L2 T-\/2 \string~> Pa].}}
\DoxyCodeLine{157     rcv\_tol, \&  \textcolor{comment}{!   A tolerence, relative to the target density differences}}
\DoxyCodeLine{158                 \textcolor{comment}{! between layers, for detraining into the interior [nondim].}}
\DoxyCodeLine{159     h\_add\_tgt, h\_add\_tot, \&}
\DoxyCodeLine{160     h\_tot1, th\_tot1, sh\_tot1, \&}
\DoxyCodeLine{161     h\_tot3, th\_tot3, sh\_tot3, \&}
\DoxyCodeLine{162     h\_tot2, th\_tot2, sh\_tot2}
\DoxyCodeLine{163 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(G))} :: \&}
\DoxyCodeLine{164     h\_prev\_1d     \textcolor{comment}{! The previous thicknesses [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{165 \textcolor{keywordtype}{  real} :: I\_dtol  \textcolor{comment}{! The inverse of the tolerance changes [nondim].}}
\DoxyCodeLine{166 \textcolor{keywordtype}{  real} :: I\_dtol34 \textcolor{comment}{! The inverse of the tolerance changes [nondim].}}
\DoxyCodeLine{167 \textcolor{keywordtype}{  real} :: h1, h2  \textcolor{comment}{! Temporary thicknesses [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{168 \textcolor{keywordtype}{  real} :: e\_e, e\_w, e\_n, e\_s  \textcolor{comment}{! Temporary interface heights [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{169 \textcolor{keywordtype}{  real} :: wt    \textcolor{comment}{! The weight of the filted interfaces in setting the targets [nondim].}}
\DoxyCodeLine{170 \textcolor{keywordtype}{  real} :: scale \textcolor{comment}{! A scaling factor [nondim].}}
\DoxyCodeLine{171 \textcolor{keywordtype}{  real} :: h\_neglect \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{172                     \textcolor{comment}{! in roundoff and can be neglected [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{173 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(G)+1)} :: \&}
\DoxyCodeLine{174     int\_flux, int\_Tflux, int\_Sflux, int\_Rflux}
\DoxyCodeLine{175 \textcolor{keywordtype}{  real} :: h\_add}
\DoxyCodeLine{176 \textcolor{keywordtype}{  real} :: h\_det\_tot}
\DoxyCodeLine{177 \textcolor{keywordtype}{  real} :: max\_def\_rat}
\DoxyCodeLine{178 \textcolor{keywordtype}{  real} :: Rcv\_min\_det  \textcolor{comment}{! The lightest (min) and densest (max) coordinate density}}
\DoxyCodeLine{179 \textcolor{keywordtype}{  real} :: Rcv\_max\_det  \textcolor{comment}{! that can detrain into a layer [R \string~> kg m-\/3].}}
\DoxyCodeLine{180 }
\DoxyCodeLine{181 \textcolor{keywordtype}{  real} :: int\_top, int\_bot}
\DoxyCodeLine{182 \textcolor{keywordtype}{  real} :: h\_predicted}
\DoxyCodeLine{183 \textcolor{keywordtype}{  real} :: h\_prev}
\DoxyCodeLine{184 \textcolor{keywordtype}{  real} :: h\_deficit}
\DoxyCodeLine{185 }
\DoxyCodeLine{186   \textcolor{keywordtype}{logical} :: cols\_left, ent\_any, more\_ent\_i(SZI\_(G)), ent\_i(SZI\_(G))}
\DoxyCodeLine{187   \textcolor{keywordtype}{logical} :: det\_any, det\_i(SZI\_(G))}
\DoxyCodeLine{188   \textcolor{keywordtype}{logical} :: do\_j(SZJ\_(G)), do\_i(SZI\_(G)), find\_i(SZI\_(G))}
\DoxyCodeLine{189   \textcolor{keywordtype}{logical} :: debug = .false.}
\DoxyCodeLine{190   \textcolor{keywordtype}{logical} :: fatal\_error}
\DoxyCodeLine{191   \textcolor{keywordtype}{character(len=256)} :: mesg    \textcolor{comment}{! Message for error messages.}}
\DoxyCodeLine{192   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: EOSdom \textcolor{comment}{! The i-\/computational domain for the equation of state}}
\DoxyCodeLine{193   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz, nkmb, nkml, k1, k2, k3, ks, nz\_filt, kmax\_d\_ea}
\DoxyCodeLine{194 }
\DoxyCodeLine{195   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = g\%ke}
\DoxyCodeLine{196 }
\DoxyCodeLine{197   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"{}MOM\_regularize\_layers: "{}}//\&}
\DoxyCodeLine{198          \textcolor{stringliteral}{"{}Module must be initialized before it is used."{}})}
\DoxyCodeLine{199 }
\DoxyCodeLine{200   \textcolor{keywordflow}{if} (gv\%nkml<1) \textcolor{keywordflow}{return}}
\DoxyCodeLine{201   nkmb = gv\%nk\_rho\_varies ; nkml = gv\%nkml}
\DoxyCodeLine{202   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(tv\%eqn\_of\_state)) \textcolor{keyword}{call }mom\_error(fatal, \&}
\DoxyCodeLine{203     \textcolor{stringliteral}{"{}MOM\_regularize\_layers: This module now requires the use of temperature and "{}}//\&}
\DoxyCodeLine{204     \textcolor{stringliteral}{"{}an equation of state."{}})}
\DoxyCodeLine{205 }
\DoxyCodeLine{206   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{207   debug = (debug .or. cs\%debug)}
\DoxyCodeLine{208 }
\DoxyCodeLine{209   i\_dtol = 1.0 / max(cs\%h\_def\_tol2 -\/ cs\%h\_def\_tol1, 1e-\/40)}
\DoxyCodeLine{210   i\_dtol34 = 1.0 / max(cs\%h\_def\_tol4 -\/ cs\%h\_def\_tol3, 1e-\/40)}
\DoxyCodeLine{211 }
\DoxyCodeLine{212   p\_ref\_cv(:) = tv\%P\_Ref}
\DoxyCodeLine{213   eosdom(:) = eos\_domain(g\%HI)}
\DoxyCodeLine{214 }
\DoxyCodeLine{215   \textcolor{keywordflow}{do} j=js-\/1,je+1 ; \textcolor{keywordflow}{do} i=is-\/1,ie+1}
\DoxyCodeLine{216     e(i,j,1) = 0.0}
\DoxyCodeLine{217 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{218   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js-\/1,je+1 ; \textcolor{keywordflow}{do} i=is-\/1,ie+1}
\DoxyCodeLine{219     e(i,j,k+1) = e(i,j,k) -\/ h(i,j,k)}
\DoxyCodeLine{220 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{221 }
\DoxyCodeLine{222   \textcolor{keyword}{call }find\_deficit\_ratios(e, def\_rat\_u, def\_rat\_v, g, gv, cs, h=h)}
\DoxyCodeLine{223 }
\DoxyCodeLine{224   \textcolor{comment}{! Determine which columns are problematic}}
\DoxyCodeLine{225   \textcolor{keywordflow}{do} j=js,je ; do\_j(j) = .false. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{226   \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{227     def\_rat\_h(i,j) = max(def\_rat\_u(i-\/1,j), def\_rat\_u(i,j), \&}
\DoxyCodeLine{228                          def\_rat\_v(i,j-\/1), def\_rat\_v(i,j))}
\DoxyCodeLine{229     \textcolor{keywordflow}{if} (def\_rat\_h(i,j) > cs\%h\_def\_tol1) do\_j(j) = .true.}
\DoxyCodeLine{230 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{231 }
\DoxyCodeLine{232   \textcolor{comment}{! Now restructure the layers.}}
\DoxyCodeLine{233   \textcolor{comment}{!\$OMP parallel do default(private) shared(is,ie,js,je,nz,do\_j,def\_rat\_h,CS,nkmb,G,GV,US, \&}}
\DoxyCodeLine{234   \textcolor{comment}{!\$OMP                                     e,I\_dtol,h,tv,debug,h\_neglect,p\_ref\_cv,ea, \&}}
\DoxyCodeLine{235   \textcolor{comment}{!\$OMP                                     eb,id\_clock\_EOS,nkml,EOSdom)}}
\DoxyCodeLine{236   \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{if} (do\_j(j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{237 }
\DoxyCodeLine{238 \textcolor{comment}{!  call cpu\_clock\_begin(id\_clock\_EOS)}}
\DoxyCodeLine{239 \textcolor{comment}{!  call calculate\_density\_derivs(T(:,1), S(:,1), p\_ref\_cv, dRcv\_dT, dRcv\_dS, tv\%eqn\_of\_state, EOSdom)}}
\DoxyCodeLine{240 \textcolor{comment}{!  call cpu\_clock\_end(id\_clock\_EOS)}}
\DoxyCodeLine{241 }
\DoxyCodeLine{242     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; d\_ea(i,k) = 0.0 ; d\_eb(i,k) = 0.0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{243     kmax\_d\_ea = 0}
\DoxyCodeLine{244 }
\DoxyCodeLine{245     max\_def\_rat = 0.0}
\DoxyCodeLine{246     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{247       do\_i(i) = def\_rat\_h(i,j) > cs\%h\_def\_tol1}
\DoxyCodeLine{248       \textcolor{keywordflow}{if} (def\_rat\_h(i,j) > max\_def\_rat) max\_def\_rat = def\_rat\_h(i,j)}
\DoxyCodeLine{249 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{250     nz\_filt = nkmb+1 ; \textcolor{keywordflow}{if} (max\_def\_rat > cs\%h\_def\_tol3) nz\_filt = nz+1}
\DoxyCodeLine{251 }
\DoxyCodeLine{252     \textcolor{comment}{! Find a 2-\/D 1-\/2-\/1 filtered version of e to target.  Area weights are}}
\DoxyCodeLine{253     \textcolor{comment}{! deliberately omitted here.  This is slightly more complicated than a}}
\DoxyCodeLine{254     \textcolor{comment}{! simple filter so that the effects of topography are eliminated.}}
\DoxyCodeLine{255     \textcolor{keywordflow}{do} k=1,nz\_filt ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{256       \textcolor{keywordflow}{if} (g\%mask2dCu(i,j) <= 0.0) \textcolor{keywordflow}{then} ; e\_e = e(i,j,k) ; \textcolor{keywordflow}{else}}
\DoxyCodeLine{257         e\_e = max(e(i+1,j,k) + min(e(i,j,k) -\/ e(i+1,j,nz+1), 0.0), \&}
\DoxyCodeLine{258                   e(i,j,nz+1) + (nz+1-\/k)*gv\%Angstrom\_H)}
\DoxyCodeLine{259 }
\DoxyCodeLine{260 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{261       \textcolor{keywordflow}{if} (g\%mask2dCu(i-\/1,j) <= 0.0) \textcolor{keywordflow}{then} ; e\_w = e(i,j,k) ; \textcolor{keywordflow}{else}}
\DoxyCodeLine{262         e\_w = max(e(i-\/1,j,k) + min(e(i,j,k) -\/ e(i-\/1,j,nz+1), 0.0), \&}
\DoxyCodeLine{263                   e(i,j,nz+1) + (nz+1-\/k)*gv\%Angstrom\_H)}
\DoxyCodeLine{264 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{265       \textcolor{keywordflow}{if} (g\%mask2dCv(i,j) <= 0.0) \textcolor{keywordflow}{then} ; e\_n = e(i,j,k) ; \textcolor{keywordflow}{else}}
\DoxyCodeLine{266         e\_n = max(e(i,j+1,k) + min(e(i,j,k) -\/ e(i,j+1,nz+1), 0.0), \&}
\DoxyCodeLine{267                   e(i,j,nz+1) + (nz+1-\/k)*gv\%Angstrom\_H)}
\DoxyCodeLine{268 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{269       \textcolor{keywordflow}{if} (g\%mask2dCv(i,j-\/1) <= 0.0) \textcolor{keywordflow}{then} ; e\_s = e(i,j,k) ; \textcolor{keywordflow}{else}}
\DoxyCodeLine{270         e\_s = max(e(i,j-\/1,k) + min(e(i,j,k) -\/ e(i,j-\/1,nz+1), 0.0), \&}
\DoxyCodeLine{271                   e(i,j,nz+1) + (nz+1-\/k)*gv\%Angstrom\_H)}
\DoxyCodeLine{272 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{273 }
\DoxyCodeLine{274       wt = max(0.0, min(1.0, i\_dtol*(def\_rat\_h(i,j)-\/cs\%h\_def\_tol1)))}
\DoxyCodeLine{275 }
\DoxyCodeLine{276       e\_filt(i,k) = (1.0 -\/ 0.5*wt) * e(i,j,k) + \&}
\DoxyCodeLine{277                   wt * 0.125 * ((e\_e + e\_w) + (e\_n + e\_s))}
\DoxyCodeLine{278       e\_2d(i,k) = e(i,j,k)}
\DoxyCodeLine{279 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{280     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{281       h\_2d(i,k) = h(i,j,k)}
\DoxyCodeLine{282       t\_2d(i,k) = tv\%T(i,j,k) ; s\_2d(i,k) = tv\%S(i,j,k)}
\DoxyCodeLine{283 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{284 }
\DoxyCodeLine{285     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{286       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{287         h\_2d\_init(i,k) = h(i,j,k)}
\DoxyCodeLine{288         t\_2d\_init(i,k) = tv\%T(i,j,k) ; s\_2d\_init(i,k) = tv\%S(i,j,k)}
\DoxyCodeLine{289 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{290 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{291 }
\DoxyCodeLine{292     \textcolor{comment}{! First, try to entrain from the interior.}}
\DoxyCodeLine{293     ent\_any = .false.}
\DoxyCodeLine{294     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{295       more\_ent\_i(i) = .false. ; ent\_i(i) = .false.}
\DoxyCodeLine{296       h\_add\_tgt(i) = 0.0 ; h\_add\_tot(i) = 0.0}
\DoxyCodeLine{297       \textcolor{keywordflow}{if} (do\_i(i) .and. (e\_2d(i,nkmb+1) > e\_filt(i,nkmb+1))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{298         more\_ent\_i(i) = .true. ; ent\_i(i) = .true. ; ent\_any = .true.}
\DoxyCodeLine{299         h\_add\_tgt(i) = e\_2d(i,nkmb+1) -\/ e\_filt(i,nkmb+1)}
\DoxyCodeLine{300 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{301 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{302 }
\DoxyCodeLine{303     \textcolor{keywordflow}{if} (ent\_any) \textcolor{keywordflow}{then}}
\DoxyCodeLine{304       \textcolor{keywordflow}{do} k=nkmb+1,nz}
\DoxyCodeLine{305         cols\_left = .false.}
\DoxyCodeLine{306         \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (more\_ent\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{307           \textcolor{keywordflow}{if} (h\_2d(i,k) -\/ gv\%Angstrom\_H > h\_neglect) \textcolor{keywordflow}{then}}
\DoxyCodeLine{308             \textcolor{keywordflow}{if} (e\_2d(i,nkmb+1)-\/e\_filt(i,nkmb+1) > h\_2d(i,k) -\/ gv\%Angstrom\_H) \textcolor{keywordflow}{then}}
\DoxyCodeLine{309               h\_add = h\_2d(i,k) -\/ gv\%Angstrom\_H}
\DoxyCodeLine{310               h\_2d(i,k) = gv\%Angstrom\_H}
\DoxyCodeLine{311               e\_2d(i,nkmb+1) = e\_2d(i,nkmb+1) -\/ h\_add}
\DoxyCodeLine{312             \textcolor{keywordflow}{else}}
\DoxyCodeLine{313               h\_add = e\_2d(i,nkmb+1) -\/ e\_filt(i,nkmb+1)}
\DoxyCodeLine{314               h\_2d(i,k) = h\_2d(i,k) -\/ h\_add}
\DoxyCodeLine{315               \textcolor{keywordflow}{if} (cs\%answers\_2018) \textcolor{keywordflow}{then}}
\DoxyCodeLine{316                 e\_2d(i,nkmb+1) = e\_2d(i,nkmb+1) -\/ h\_add}
\DoxyCodeLine{317               \textcolor{keywordflow}{else}}
\DoxyCodeLine{318                 e\_2d(i,nkmb+1) = e\_filt(i,nkmb+1)}
\DoxyCodeLine{319 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{320 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{321             d\_eb(i,k-\/1) = d\_eb(i,k-\/1) + h\_add}
\DoxyCodeLine{322             h\_add\_tot(i) = h\_add\_tot(i) + h\_add}
\DoxyCodeLine{323             h\_prev = h\_2d(i,nkmb)}
\DoxyCodeLine{324             h\_2d(i,nkmb) = h\_2d(i,nkmb) + h\_add}
\DoxyCodeLine{325 }
\DoxyCodeLine{326             t\_2d(i,nkmb) = (h\_prev*t\_2d(i,nkmb) + h\_add*t\_2d(i,k)) / h\_2d(i,nkmb)}
\DoxyCodeLine{327             s\_2d(i,nkmb) = (h\_prev*s\_2d(i,nkmb) + h\_add*s\_2d(i,k)) / h\_2d(i,nkmb)}
\DoxyCodeLine{328 }
\DoxyCodeLine{329             \textcolor{keywordflow}{if} ((e\_2d(i,nkmb+1) <= e\_filt(i,nkmb+1)) .or. \&}
\DoxyCodeLine{330                 (h\_add\_tot(i) > 0.6*h\_add\_tgt(i))) \textcolor{keywordflow}{then}  \textcolor{comment}{!\#\#\# 0.6 is adjustable?.}}
\DoxyCodeLine{331               more\_ent\_i(i) = .false.}
\DoxyCodeLine{332             \textcolor{keywordflow}{else}}
\DoxyCodeLine{333               cols\_left = .true.}
\DoxyCodeLine{334 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{335           \textcolor{keywordflow}{else}}
\DoxyCodeLine{336             cols\_left = .true.}
\DoxyCodeLine{337 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{338 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{339         \textcolor{keywordflow}{if} (.not.cols\_left) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{340 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{341 }
\DoxyCodeLine{342       ks = min(k-\/1,nz-\/1)}
\DoxyCodeLine{343       \textcolor{keywordflow}{do} k=ks,nkmb,-\/1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (ent\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{344         d\_eb(i,k) = d\_eb(i,k) + d\_eb(i,k+1)}
\DoxyCodeLine{345 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{346 \textcolor{keywordflow}{    endif} \textcolor{comment}{! ent\_any}}
\DoxyCodeLine{347 }
\DoxyCodeLine{348     \textcolor{comment}{!   This is where code to detrain to the interior will go.}}
\DoxyCodeLine{349     \textcolor{comment}{! The buffer layers can only detrain water into layers when the buffer}}
\DoxyCodeLine{350     \textcolor{comment}{! layer potential density is between (c*Rlay(k-\/1) + (1-\/c)*Rlay(k)) and}}
\DoxyCodeLine{351     \textcolor{comment}{! (c*Rlay(k+1) + (1-\/c)*Rlay(k)), where 0.5 <= c < 1.0.}}
\DoxyCodeLine{352     \textcolor{comment}{!    Do not detrain if the 2-\/layer deficit ratio is not significant.}}
\DoxyCodeLine{353     \textcolor{comment}{!    Detrainment must be able to come from all mixed and buffer layers.}}
\DoxyCodeLine{354     \textcolor{comment}{!    All water is moved out of the buffer layers below before moving from}}
\DoxyCodeLine{355     \textcolor{comment}{!  a shallower layer (characteristics do not cross).}}
\DoxyCodeLine{356     det\_any = .false.}
\DoxyCodeLine{357     \textcolor{keywordflow}{if} ((max\_def\_rat > cs\%h\_def\_tol3) .and. (cs\%reg\_sfc\_detrain)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{358       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{359         det\_i(i) = .false. ; rcv\_tol(i) = 0.0}
\DoxyCodeLine{360         \textcolor{keywordflow}{if} (do\_i(i) .and. (e\_2d(i,nkmb+1) < e\_filt(i,nkmb+1)) .and. \&}
\DoxyCodeLine{361             (def\_rat\_h(i,j) > cs\%h\_def\_tol3)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{362           det\_i(i) = .true. ; det\_any = .true.}
\DoxyCodeLine{363           \textcolor{comment}{! The CS\%density\_match\_tol default value of 0.6 gives 20\% overlap in acceptable densities.}}
\DoxyCodeLine{364           rcv\_tol(i) = cs\%density\_match\_tol * min((def\_rat\_h(i,j) -\/ cs\%h\_def\_tol3), 1.0)}
\DoxyCodeLine{365 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{366 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{367 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{368     \textcolor{keywordflow}{if} (det\_any) \textcolor{keywordflow}{then}}
\DoxyCodeLine{369       \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_eos)}
\DoxyCodeLine{370       \textcolor{keywordflow}{do} k=1,nkmb}
\DoxyCodeLine{371         \textcolor{keyword}{call }calculate\_density(t\_2d(:,k), s\_2d(:,k), p\_ref\_cv, rcv(:,k), tv\%eqn\_of\_state, eosdom)}
\DoxyCodeLine{372 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{373       \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_eos)}
\DoxyCodeLine{374 }
\DoxyCodeLine{375       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (det\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{376         k1 = nkmb ; k2 = nz}
\DoxyCodeLine{377         h\_det\_tot = 0.0}
\DoxyCodeLine{378         \textcolor{keywordflow}{do} \textcolor{comment}{! This loop is terminated by exits.}}
\DoxyCodeLine{379           \textcolor{keywordflow}{if} (k1 <= 1) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{380           \textcolor{keywordflow}{if} (k2 <= nkmb) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{381           rcv\_min\_det = (gv\%Rlay(k2) + rcv\_tol(i)*(gv\%Rlay(k2-\/1)-\/gv\%Rlay(k2)))}
\DoxyCodeLine{382           \textcolor{keywordflow}{if} (k2 < nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{383             rcv\_max\_det = (gv\%Rlay(k2) + rcv\_tol(i)*(gv\%Rlay(k2+1)-\/gv\%Rlay(k2)))}
\DoxyCodeLine{384           \textcolor{keywordflow}{else}}
\DoxyCodeLine{385             rcv\_max\_det = (gv\%Rlay(nz) + rcv\_tol(i)*(gv\%Rlay(nz)-\/gv\%Rlay(nz-\/1)))}
\DoxyCodeLine{386 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{387           \textcolor{keywordflow}{if} (rcv(i,k1) > rcv\_max\_det) \&}
\DoxyCodeLine{388             \textcolor{keywordflow}{exit} \textcolor{comment}{! All shallower interior layers are too light for detrainment.}}
\DoxyCodeLine{389 }
\DoxyCodeLine{390           h\_deficit = (e\_filt(i,k2)-\/e\_filt(i,k2+1)) -\/ h\_2d(i,k2)}
\DoxyCodeLine{391           \textcolor{keywordflow}{if} ((e\_filt(i,k2) > e\_2d(i,k1+1)) .and. (h\_deficit > 0.0) .and. \&}
\DoxyCodeLine{392               (rcv(i,k1) < rcv\_max\_det) .and. (rcv(i,k1) > rcv\_min\_det)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{393             \textcolor{comment}{! Detrainment will occur.}}
\DoxyCodeLine{394             h\_add = min(e\_filt(i,k2) -\/ e\_2d(i,k2), h\_deficit )}
\DoxyCodeLine{395             \textcolor{keywordflow}{if} (h\_add < h\_2d(i,k1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{396               \textcolor{comment}{! Only part of layer k1 detrains.}}
\DoxyCodeLine{397               \textcolor{keywordflow}{if} (h\_add > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{398                 h\_prev = h\_2d(i,k2)}
\DoxyCodeLine{399                 h\_2d(i,k2) = h\_2d(i,k2) + h\_add}
\DoxyCodeLine{400                 e\_2d(i,k2) = e\_2d(i,k2+1) + h\_2d(i,k2)}
\DoxyCodeLine{401                 d\_ea(i,k2) = d\_ea(i,k2) + h\_add}
\DoxyCodeLine{402                 kmax\_d\_ea = max(kmax\_d\_ea, k2)}
\DoxyCodeLine{403                 \textcolor{comment}{! This is upwind.  It should perhaps be higher order...}}
\DoxyCodeLine{404                 t\_2d(i,k2) = (h\_prev*t\_2d(i,k2) + h\_add*t\_2d(i,k1)) / h\_2d(i,k2)}
\DoxyCodeLine{405                 s\_2d(i,k2) = (h\_prev*s\_2d(i,k2) + h\_add*s\_2d(i,k1)) / h\_2d(i,k2)}
\DoxyCodeLine{406                 h\_det\_tot = h\_det\_tot + h\_add}
\DoxyCodeLine{407 }
\DoxyCodeLine{408                 h\_2d(i,k1) = h\_2d(i,k1) -\/ h\_add}
\DoxyCodeLine{409                 \textcolor{keywordflow}{do} k3=k1,nkmb ; e\_2d(i,k3+1) = e\_2d(i,k3) -\/ h\_2d(i,k3) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{410                 \textcolor{keywordflow}{do} k3=k1+1,nkmb ; d\_ea(i,k3) = d\_ea(i,k3) + h\_add ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{411               \textcolor{keywordflow}{else}}
\DoxyCodeLine{412                 \textcolor{keywordflow}{if} (h\_add < 0.0) \&}
\DoxyCodeLine{413                   \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"{}h\_add is negative.  Some logic is wrong."{}})}
\DoxyCodeLine{414                 h\_add = 0.0 \textcolor{comment}{! This usually should not happen...}}
\DoxyCodeLine{415 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{416 }
\DoxyCodeLine{417               \textcolor{comment}{! Move up to the next target layer.}}
\DoxyCodeLine{418               k2 = k2-\/1}
\DoxyCodeLine{419               \textcolor{keywordflow}{if} (k2>nkmb+1) e\_2d(i,k2) = e\_2d(i,k2) + h\_det\_tot}
\DoxyCodeLine{420             \textcolor{keywordflow}{else}}
\DoxyCodeLine{421               h\_add = h\_2d(i,k1)}
\DoxyCodeLine{422               h\_prev = h\_2d(i,k2)}
\DoxyCodeLine{423               h\_2d(i,k2) = h\_2d(i,k2) + h\_add}
\DoxyCodeLine{424               e\_2d(i,k2) = e\_2d(i,k2+1) + h\_2d(i,k2)}
\DoxyCodeLine{425               d\_ea(i,k2) = d\_ea(i,k2) + h\_add}
\DoxyCodeLine{426               kmax\_d\_ea = max(kmax\_d\_ea, k2)}
\DoxyCodeLine{427               t\_2d(i,k2) = (h\_prev*t\_2d(i,k2) + h\_add*t\_2d(i,k1)) / h\_2d(i,k2)}
\DoxyCodeLine{428               s\_2d(i,k2) = (h\_prev*s\_2d(i,k2) + h\_add*s\_2d(i,k1)) / h\_2d(i,k2)}
\DoxyCodeLine{429               h\_det\_tot = h\_det\_tot + h\_add}
\DoxyCodeLine{430 }
\DoxyCodeLine{431               h\_2d(i,k1) = 0.0}
\DoxyCodeLine{432               \textcolor{keywordflow}{do} k3=k1,nkmb ; e\_2d(i,k3+1) = e\_2d(i,k3) -\/ h\_2d(i,k3) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{433               \textcolor{keywordflow}{do} k3=k1+1,nkmb ; d\_ea(i,k3) = d\_ea(i,k3) + h\_add ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{434 }
\DoxyCodeLine{435               \textcolor{comment}{! Move up to the next source layer.}}
\DoxyCodeLine{436               k1 = k1-\/1}
\DoxyCodeLine{437 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{438 }
\DoxyCodeLine{439           \textcolor{keywordflow}{else}}
\DoxyCodeLine{440             \textcolor{comment}{! Move up to the next target layer.}}
\DoxyCodeLine{441             k2 = k2-\/1}
\DoxyCodeLine{442             \textcolor{keywordflow}{if} (k2>nkmb+1) e\_2d(i,k2) = e\_2d(i,k2) + h\_det\_tot}
\DoxyCodeLine{443 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{444 }
\DoxyCodeLine{445 \textcolor{keywordflow}{        enddo} \textcolor{comment}{! exit terminated loop.}}
\DoxyCodeLine{446 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{447       \textcolor{keywordflow}{do} k=kmax\_d\_ea-\/1,nkmb+1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (det\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{448         d\_ea(i,k) = d\_ea(i,k) + d\_ea(i,k+1)}
\DoxyCodeLine{449 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{450 \textcolor{keywordflow}{    endif}  \textcolor{comment}{! Detrainment to the interior.}}
\DoxyCodeLine{451     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{452       \textcolor{keywordflow}{do} i=is,ie ; h\_tot3(i) = 0.0 ; th\_tot3(i) = 0.0 ; sh\_tot3(i) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{453       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{454         h\_tot3(i) = h\_tot3(i) + h\_2d(i,k)}
\DoxyCodeLine{455         th\_tot3(i) = th\_tot3(i) + h\_2d(i,k) * t\_2d(i,k)}
\DoxyCodeLine{456         sh\_tot3(i) = sh\_tot3(i) + h\_2d(i,k) * s\_2d(i,k)}
\DoxyCodeLine{457 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{458 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{459 }
\DoxyCodeLine{460     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{461       \textcolor{comment}{! Rescale the interface targets so the depth at the bottom of the deepest}}
\DoxyCodeLine{462       \textcolor{comment}{! buffer layer matches.}}
\DoxyCodeLine{463       scale = e\_2d(i,nkmb+1) / e\_filt(i,nkmb+1)}
\DoxyCodeLine{464       \textcolor{keywordflow}{do} k=2,nkmb+1 ; e\_filt(i,k) = e\_filt(i,k) * scale ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{465 }
\DoxyCodeLine{466       \textcolor{comment}{! Ensure that layer 1 only has water from layers 1 to nkml and rescale}}
\DoxyCodeLine{467       \textcolor{comment}{! the remaining layer thicknesses if necessary.}}
\DoxyCodeLine{468       \textcolor{keywordflow}{if} (e\_filt(i,2) < e\_2d(i,nkml)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{469         scale = (e\_2d(i,nkml) -\/ e\_filt(i,nkmb+1)) / \&}
\DoxyCodeLine{470                 ((e\_filt(i,2) -\/ e\_filt(i,nkmb+1)) + h\_neglect)}
\DoxyCodeLine{471         \textcolor{keywordflow}{do} k=3,nkmb}
\DoxyCodeLine{472           e\_filt(i,k) = e\_filt(i,nkmb+1) + scale * (e\_filt(i,k) -\/ e\_filt(i,nkmb+1))}
\DoxyCodeLine{473 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{474         e\_filt(i,2) = e\_2d(i,nkml)}
\DoxyCodeLine{475 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{476 }
\DoxyCodeLine{477       \textcolor{comment}{! Map the water back into the layers.  There are not mixed or buffer layers that are exceedingly}}
\DoxyCodeLine{478       \textcolor{comment}{! small compared to the others, so the code here is less prone to roundoff than elsewhere in MOM6.}}
\DoxyCodeLine{479       k1 = 1 ; k2 = 1}
\DoxyCodeLine{480       int\_top = 0.0}
\DoxyCodeLine{481       \textcolor{keywordflow}{do} k=1,nkmb+1}
\DoxyCodeLine{482         int\_flux(k) = 0.0 ; int\_rflux(k) = 0.0}
\DoxyCodeLine{483         int\_tflux(k) = 0.0 ; int\_sflux(k) = 0.0}
\DoxyCodeLine{484 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{485       \textcolor{keywordflow}{do} k=1,2*nkmb}
\DoxyCodeLine{486         int\_bot = max(e\_2d(i,k1+1),e\_filt(i,k2+1))}
\DoxyCodeLine{487         h\_add = int\_top -\/ int\_bot}
\DoxyCodeLine{488 }
\DoxyCodeLine{489         \textcolor{keywordflow}{if} (k2 > k1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{490           \textcolor{keywordflow}{do} k3=k1+1,k2}
\DoxyCodeLine{491             d\_ea(i,k3) = d\_ea(i,k3) + h\_add}
\DoxyCodeLine{492             int\_flux(k3) = int\_flux(k3) + h\_add}
\DoxyCodeLine{493             int\_tflux(k3) = int\_tflux(k3) + h\_add*t\_2d(i,k1)}
\DoxyCodeLine{494             int\_sflux(k3) = int\_sflux(k3) + h\_add*s\_2d(i,k1)}
\DoxyCodeLine{495 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{496         \textcolor{keywordflow}{elseif} (k1 > k2) \textcolor{keywordflow}{then}}
\DoxyCodeLine{497           \textcolor{keywordflow}{do} k3=k2,k1-\/1}
\DoxyCodeLine{498             d\_eb(i,k3) = d\_eb(i,k3) + h\_add}
\DoxyCodeLine{499             int\_flux(k3+1) = int\_flux(k3+1) -\/ h\_add}
\DoxyCodeLine{500             int\_tflux(k3+1) = int\_tflux(k3+1) -\/ h\_add*t\_2d(i,k1)}
\DoxyCodeLine{501             int\_sflux(k3+1) = int\_sflux(k3+1) -\/ h\_add*s\_2d(i,k1)}
\DoxyCodeLine{502 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{503 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{504 }
\DoxyCodeLine{505         \textcolor{keywordflow}{if} (int\_bot <= e\_filt(i,k2+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{506           \textcolor{comment}{! Increment the target layer.}}
\DoxyCodeLine{507           k2 = k2 + 1}
\DoxyCodeLine{508         \textcolor{keywordflow}{elseif} (int\_bot <= e\_2d(i,k1+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{509           \textcolor{comment}{! Increment the source layer.}}
\DoxyCodeLine{510           k1 = k1 + 1}
\DoxyCodeLine{511         \textcolor{keywordflow}{else}}
\DoxyCodeLine{512           \textcolor{keyword}{call }mom\_error(fatal, \&}
\DoxyCodeLine{513             \textcolor{stringliteral}{"{}Regularize\_surface: Could not increment target or source."{}})}
\DoxyCodeLine{514 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{515         \textcolor{keywordflow}{if} ((k1 > nkmb) .or. (k2 > nkmb)) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{516         int\_top = int\_bot}
\DoxyCodeLine{517 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{518       \textcolor{keywordflow}{if} (k2 < nkmb) \&}
\DoxyCodeLine{519         \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"{}Regularize\_surface: Did not assign fluid to layer nkmb."{}})}
\DoxyCodeLine{520 }
\DoxyCodeLine{521       \textcolor{comment}{! Note that movement of water across the base of the bottommost buffer}}
\DoxyCodeLine{522       \textcolor{comment}{! layer has already been dealt with separately.}}
\DoxyCodeLine{523       \textcolor{keywordflow}{do} k=1,nkmb ; h\_prev\_1d(k) = h\_2d(i,k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{524       h\_2d(i,1) = h\_2d(i,1) -\/ int\_flux(2)}
\DoxyCodeLine{525       \textcolor{keywordflow}{do} k=2,nkmb-\/1}
\DoxyCodeLine{526         h\_2d(i,k) = h\_2d(i,k) + (int\_flux(k) -\/ int\_flux(k+1))}
\DoxyCodeLine{527 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{528       \textcolor{comment}{! Note that movement of water across the base of the bottommost buffer}}
\DoxyCodeLine{529       \textcolor{comment}{! layer has already been dealt with separately.}}
\DoxyCodeLine{530       h\_2d(i,nkmb) = h\_2d(i,nkmb) + int\_flux(nkmb)}
\DoxyCodeLine{531 }
\DoxyCodeLine{532       t\_2d(i,1) = (t\_2d(i,1)*h\_prev\_1d(1) -\/ int\_tflux(2)) / h\_2d(i,1)}
\DoxyCodeLine{533       s\_2d(i,1) = (s\_2d(i,1)*h\_prev\_1d(1) -\/ int\_sflux(2)) / h\_2d(i,1)}
\DoxyCodeLine{534       \textcolor{keywordflow}{do} k=2,nkmb-\/1}
\DoxyCodeLine{535         t\_2d(i,k) = (t\_2d(i,k)*h\_prev\_1d(k) + (int\_tflux(k) -\/ int\_tflux(k+1))) / h\_2d(i,k)}
\DoxyCodeLine{536         s\_2d(i,k) = (s\_2d(i,k)*h\_prev\_1d(k) + (int\_sflux(k) -\/ int\_sflux(k+1))) / h\_2d(i,k)}
\DoxyCodeLine{537 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{538       t\_2d(i,nkmb) = (t\_2d(i,nkmb)*h\_prev\_1d(nkmb) + int\_tflux(nkmb) ) / h\_2d(i,nkmb)}
\DoxyCodeLine{539       s\_2d(i,nkmb) = (s\_2d(i,nkmb)*h\_prev\_1d(nkmb) + int\_sflux(nkmb) ) / h\_2d(i,nkmb)}
\DoxyCodeLine{540 }
\DoxyCodeLine{541 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i-\/loop}}
\DoxyCodeLine{542 }
\DoxyCodeLine{543     \textcolor{comment}{! Copy the interior thicknesses and other fields back to the 3-\/d arrays.}}
\DoxyCodeLine{544     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{545       h(i,j,k) = h\_2d(i,k)}
\DoxyCodeLine{546       tv\%T(i,j,k) = t\_2d(i,k) ; tv\%S(i,j,k) = s\_2d(i,k)}
\DoxyCodeLine{547       ea(i,j,k) = ea(i,j,k) + d\_ea(i,k)}
\DoxyCodeLine{548       eb(i,j,k) = eb(i,j,k) + d\_eb(i,k)}
\DoxyCodeLine{549 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{550 }
\DoxyCodeLine{551     \textcolor{keywordflow}{if} (debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{552       \textcolor{keywordflow}{do} i=is,ie ; h\_tot1(i) = 0.0 ; th\_tot1(i) = 0.0 ; sh\_tot1(i) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{553       \textcolor{keywordflow}{do} i=is,ie ; h\_tot2(i) = 0.0 ; th\_tot2(i) = 0.0 ; sh\_tot2(i) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{554 }
\DoxyCodeLine{555       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{556         h\_tot1(i) = h\_tot1(i) + h\_2d\_init(i,k)}
\DoxyCodeLine{557         h\_tot2(i) = h\_tot2(i) + h(i,j,k)}
\DoxyCodeLine{558 }
\DoxyCodeLine{559         th\_tot1(i) = th\_tot1(i) + h\_2d\_init(i,k) * t\_2d\_init(i,k)}
\DoxyCodeLine{560         th\_tot2(i) = th\_tot2(i) + h(i,j,k) * tv\%T(i,j,k)}
\DoxyCodeLine{561         sh\_tot1(i) = sh\_tot1(i) + h\_2d\_init(i,k) * s\_2d\_init(i,k)}
\DoxyCodeLine{562         sh\_tot2(i) = sh\_tot2(i) + h(i,j,k) * tv\%S(i,j,k)}
\DoxyCodeLine{563         \textcolor{keywordflow}{if} (h(i,j,k) < 0.0) \&}
\DoxyCodeLine{564           \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"{}regularize\_surface: Negative thicknesses."{}})}
\DoxyCodeLine{565         \textcolor{keywordflow}{if} (k==1) \textcolor{keywordflow}{then} ; h\_predicted = h\_2d\_init(i,k) + (d\_eb(i,k) -\/ d\_ea(i,k+1))}
\DoxyCodeLine{566         \textcolor{keywordflow}{elseif} (k==nz) \textcolor{keywordflow}{then} ; h\_predicted = h\_2d\_init(i,k) + (d\_ea(i,k) -\/ d\_eb(i,k-\/1))}
\DoxyCodeLine{567         \textcolor{keywordflow}{else}}
\DoxyCodeLine{568           h\_predicted = h\_2d\_init(i,k) + ((d\_ea(i,k) -\/ d\_eb(i,k-\/1)) + \&}
\DoxyCodeLine{569                                           (d\_eb(i,k) -\/ d\_ea(i,k+1)))}
\DoxyCodeLine{570 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{571         \textcolor{keywordflow}{if} (abs(h(i,j,k) -\/ h\_predicted) > max(1e-\/9*abs(h\_predicted),gv\%Angstrom\_H)) \&}
\DoxyCodeLine{572           \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"{}regularize\_surface: d\_ea mismatch."{}})}
\DoxyCodeLine{573 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{574       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{575         fatal\_error = .false.}
\DoxyCodeLine{576         \textcolor{keywordflow}{if} (abs(h\_tot1(i) -\/ h\_tot2(i)) > 1e-\/12*h\_tot1(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{577           \textcolor{keyword}{write}(mesg,\textcolor{stringliteral}{'(ES11.4,"{} became "{},ES11.4,"{} diff "{},ES11.4)'}) \&}
\DoxyCodeLine{578                 h\_tot1(i), h\_tot2(i), (h\_tot1(i) -\/ h\_tot2(i))}
\DoxyCodeLine{579           \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"{}regularize\_surface: Mass non-\/conservation."{}}//\&}
\DoxyCodeLine{580                           trim(mesg), .true.)}
\DoxyCodeLine{581           fatal\_error = .true.}
\DoxyCodeLine{582 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{583         \textcolor{keywordflow}{if} (abs(th\_tot1(i) -\/ th\_tot2(i)) > 1e-\/12*(th\_tot1(i)+10.0*h\_tot1(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{584           \textcolor{keyword}{write}(mesg,\textcolor{stringliteral}{'(ES11.4,"{} became "{},ES11.4,"{} diff "{},ES11.4,"{} int diff "{},ES11.4)'}) \&}
\DoxyCodeLine{585                 th\_tot1(i), th\_tot2(i), (th\_tot1(i) -\/ th\_tot2(i)), (th\_tot1(i) -\/ th\_tot3(i))}
\DoxyCodeLine{586           \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"{}regularize\_surface: Heat non-\/conservation."{}}//\&}
\DoxyCodeLine{587                           trim(mesg), .true.)}
\DoxyCodeLine{588           fatal\_error = .true.}
\DoxyCodeLine{589 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{590         \textcolor{keywordflow}{if} (abs(sh\_tot1(i) -\/ sh\_tot2(i)) > 1e-\/12*(sh\_tot1(i)+10.0*h\_tot1(i))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{591           \textcolor{keyword}{write}(mesg,\textcolor{stringliteral}{'(ES11.4,"{} became "{},ES11.4,"{} diff "{},ES11.4,"{} int diff "{},ES11.4)'}) \&}
\DoxyCodeLine{592                 sh\_tot1(i), sh\_tot2(i), (sh\_tot1(i) -\/ sh\_tot2(i)), (sh\_tot1(i) -\/ sh\_tot3(i))}
\DoxyCodeLine{593           \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"{}regularize\_surface: Salinity non-\/conservation."{}}//\&}
\DoxyCodeLine{594                           trim(mesg), .true.)}
\DoxyCodeLine{595           fatal\_error = .true.}
\DoxyCodeLine{596 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{597         \textcolor{keywordflow}{if} (fatal\_error) \textcolor{keywordflow}{then}}
\DoxyCodeLine{598           \textcolor{keyword}{write}(mesg,\textcolor{stringliteral}{'("{}Error at lat/lon "{},2(ES11.4))'}) g\%geoLatT(i,j), g\%geoLonT(i,j)}
\DoxyCodeLine{599           \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"{}regularize\_surface: Terminating with fatal error.  "{}}//\&}
\DoxyCodeLine{600                           trim(mesg))}
\DoxyCodeLine{601 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{602 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{603 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{604 }
\DoxyCodeLine{605 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! j-\/loop.}}
\DoxyCodeLine{606 }
\DoxyCodeLine{607   \textcolor{keywordflow}{if} (cs\%id\_def\_rat > 0) \textcolor{keyword}{call }post\_data(cs\%id\_def\_rat, def\_rat\_h, cs\%diag)}
\DoxyCodeLine{608 }

\end{DoxyCode}


\doxysubsection{Variable Documentation}
\mbox{\Hypertarget{namespacemom__regularize__layers_a6de9cd8fa696db5f598a330aaddd772b}\label{namespacemom__regularize__layers_a6de9cd8fa696db5f598a330aaddd772b}} 
\index{mom\_regularize\_layers@{mom\_regularize\_layers}!id\_clock\_eos@{id\_clock\_eos}}
\index{id\_clock\_eos@{id\_clock\_eos}!mom\_regularize\_layers@{mom\_regularize\_layers}}
\doxysubsubsection{\texorpdfstring{id\_clock\_eos}{id\_clock\_eos}}
{\footnotesize\ttfamily integer mom\+\_\+regularize\+\_\+layers\+::id\+\_\+clock\+\_\+eos\hspace{0.3cm}{\ttfamily [private]}}



Clock I\+Ds. 

\begin{DoxyRefDesc}{Todo}
\item[\mbox{\hyperlink{todo__todo000005}{Todo}}]Should these be global? \end{DoxyRefDesc}


Definition at line 64 of file M\+O\+M\+\_\+regularize\+\_\+layers.\+F90.

\mbox{\Hypertarget{namespacemom__regularize__layers_ab3955b6fce42b5a4b03457a57f46743b}\label{namespacemom__regularize__layers_ab3955b6fce42b5a4b03457a57f46743b}} 
\index{mom\_regularize\_layers@{mom\_regularize\_layers}!id\_clock\_pass@{id\_clock\_pass}}
\index{id\_clock\_pass@{id\_clock\_pass}!mom\_regularize\_layers@{mom\_regularize\_layers}}
\doxysubsubsection{\texorpdfstring{id\_clock\_pass}{id\_clock\_pass}}
{\footnotesize\ttfamily integer mom\+\_\+regularize\+\_\+layers\+::id\+\_\+clock\+\_\+pass}



Clock I\+Ds. 

\begin{DoxyRefDesc}{Todo}
\item[\mbox{\hyperlink{todo__todo000005}{Todo}}]Should these be global? \end{DoxyRefDesc}


Definition at line 64 of file M\+O\+M\+\_\+regularize\+\_\+layers.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{64 \textcolor{keywordtype}{integer} :: id\_clock\_pass, id\_clock\_EOS}

\end{DoxyCode}
