\hypertarget{namespacemom__regularize__layers}{}\section{mom\+\_\+regularize\+\_\+layers Module Reference}
\label{namespacemom__regularize__layers}\index{mom\+\_\+regularize\+\_\+layers@{mom\+\_\+regularize\+\_\+layers}}


\subsection{Detailed Description}
Provides regularization of layers in isopycnal mode. \subsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \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}
\subsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \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 \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 \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 \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}
\subsection*{Variables}
\textbf{ }\par
\begin{DoxyCompactItemize}
\item 
integer \hyperlink{namespacemom__regularize__layers_ab3955b6fce42b5a4b03457a57f46743b}{id\+\_\+clock\+\_\+pass}
\begin{DoxyCompactList}\small\item\em Clock I\+Ds. \end{DoxyCompactList}\item 
integer \hyperlink{namespacemom__regularize__layers_a6de9cd8fa696db5f598a330aaddd772b}{id\+\_\+clock\+\_\+eos}
\begin{DoxyCompactList}\small\item\em Clock I\+Ds. \end{DoxyCompactList}\end{DoxyCompactItemize}



\subsection{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}}
\subsubsection{\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(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(\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{\tt in}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em e} & Interface depths \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt out}  & {\em def\+\_\+rat\+\_\+u} & The thickness deficit ratio at u points,\\
\hline
\mbox{\tt 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{\tt out}  & {\em def\+\_\+rat\+\_\+u\+\_\+2lay} & The thickness deficit ratio at u\\
\hline
\mbox{\tt out}  & {\em def\+\_\+rat\+\_\+v\+\_\+2lay} & The thickness deficit ratio at v\\
\hline
\mbox{\tt in}  & {\em halo} & An extra-\/wide halo size, 0 by default.\\
\hline
\mbox{\tt in}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\end{DoxyParams}


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


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


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


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


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


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


\subsection{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}}
\subsubsection{\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[\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}}
\subsubsection{\texorpdfstring{id\+\_\+clock\+\_\+pass}{id\_clock\_pass}}
{\footnotesize\ttfamily integer mom\+\_\+regularize\+\_\+layers\+::id\+\_\+clock\+\_\+pass}



Clock I\+Ds. 

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


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


\begin{DoxyCode}
64 \textcolor{keywordtype}{integer} :: id\_clock\_pass, id\_clock\_eos
\end{DoxyCode}
