\hypertarget{namespacemom__neutral__diffusion}{}\section{mom\+\_\+neutral\+\_\+diffusion Module Reference}
\label{namespacemom__neutral__diffusion}\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}


\subsection{Detailed Description}
A column-\/wise toolbox for implementing neutral diffusion. \subsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__neutral__diffusion_1_1neutral__diffusion__cs}{neutral\+\_\+diffusion\+\_\+cs}}
\begin{DoxyCompactList}\small\item\em The control structure for the M\+O\+M\+\_\+neutral\+\_\+diffusion module. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
logical function, public \mbox{\hyperlink{namespacemom__neutral__diffusion_a39ce023f4b8ebc8d30f25a9b1ca93925}{neutral\+\_\+diffusion\+\_\+init}} (Time, G, US, param\+\_\+file, diag, E\+OS, diabatic\+\_\+\+C\+Sp, CS)
\begin{DoxyCompactList}\small\item\em Read parameters and allocate control structure for neutral\+\_\+diffusion module. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__neutral__diffusion_abc3fd398047ffb61825ceced9ae5ff41}{neutral\+\_\+diffusion\+\_\+calc\+\_\+coeffs}} (G, GV, US, h, T, S, CS, p\+\_\+surf)
\begin{DoxyCompactList}\small\item\em Calculate remapping factors for u/v columns used to map adjoining columns to a shared coordinate space. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__neutral__diffusion_a5469b7be43f8c428f092b58dd5ca41c8}{neutral\+\_\+diffusion}} (G, GV, h, Coef\+\_\+x, Coef\+\_\+y, dt, Reg, US, CS)
\begin{DoxyCompactList}\small\item\em Update tracer concentration due to neutral diffusion; layer thickness unchanged by this update. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__neutral__diffusion_aec9b89b6a5ddc81f102309b964a0b969}{interface\+\_\+scalar}} (nk, h, S, Si, i\+\_\+method, h\+\_\+neglect)
\begin{DoxyCompactList}\small\item\em Returns interface scalar, Si, for a column of layer values, S. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__neutral__diffusion_ae931a15b9ab6a7672b005eb5fe382219}{ppm\+\_\+edge}} (hkm1, hk, hkp1, hkp2, Ak, Akp1, Pk, Pkp1, h\+\_\+neglect)
\begin{DoxyCompactList}\small\item\em Returns the P\+PM quasi-\/fourth order edge value at k+1/2 following equation 1.\+6 in Colella \& Woodward, 1984\+: J\+CP 54, 174-\/201. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__neutral__diffusion_a6931d9a07d09aa5d76d5abd2e47a54df}{ppm\+\_\+ave}} (xL, xR, aL, aR, a\+Mean)
\begin{DoxyCompactList}\small\item\em Returns the average of a P\+PM reconstruction between two fractional positions. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__neutral__diffusion_a118ace85483009d5b7a1841e3c745499}{signum}} (a, x)
\begin{DoxyCompactList}\small\item\em A true signum function that returns either -\/abs(a), when x$<$0; or abs(a) when x$>$0; or 0 when x=0. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__neutral__diffusion_ac211ae488cb6e3c6685df85d24ecd0dd}{plm\+\_\+diff}} (nk, h, S, c\+\_\+method, b\+\_\+method, diff)
\begin{DoxyCompactList}\small\item\em Returns P\+LM slopes for a column where the slopes are the difference in value across each cell. The limiting follows equation 1.\+8 in Colella \& Woodward, 1984\+: J\+CP 54, 174-\/201. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__neutral__diffusion_a1c5ac28d7c54581cdd872fe58ef0b204}{fv\+\_\+diff}} (hkm1, hk, hkp1, Skm1, Sk, Skp1)
\begin{DoxyCompactList}\small\item\em Returns the cell-\/centered second-\/order finite volume (unlimited P\+LM) slope using three consecutive cell widths and average values. Slope is returned as a difference across the central cell (i.\+e. units of scalar S). Discretization follows equation 1.\+7 in Colella \& Woodward, 1984\+: J\+CP 54, 174-\/201. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__neutral__diffusion_a6ed3814088af470ad6fc4a3a80926fd7}{fvlsq\+\_\+slope}} (hkm1, hk, hkp1, Skm1, Sk, Skp1)
\begin{DoxyCompactList}\small\item\em Returns the cell-\/centered second-\/order weighted least squares slope using three consecutive cell widths and average values. Slope is returned as a gradient (i.\+e. units of scalar S over width units). \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__neutral__diffusion_acb07c024cb6aa162a8789a4f50139aa5}{find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+continuous}} (nk, Pl, Tl, Sl, d\+Rd\+Tl, d\+Rd\+Sl, Pr, Tr, Sr, d\+Rd\+Tr, d\+Rd\+Sr, PoL, PoR, KoL, KoR, h\+Eff, bl\+\_\+kl, bl\+\_\+kr, bl\+\_\+zl, bl\+\_\+zr)
\begin{DoxyCompactList}\small\item\em Returns positions within left/right columns of combined interfaces using continuous reconstructions of T/S. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__neutral__diffusion_a666836e70bdae7ef4ec271246cf91993}{interpolate\+\_\+for\+\_\+nondim\+\_\+position}} (d\+Rho\+Neg, Pneg, d\+Rho\+Pos, Ppos)
\begin{DoxyCompactList}\small\item\em Returns the non-\/dimensional position between Pneg and Ppos where the interpolated density difference equals zero. The result is always bounded to be between 0 and 1. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__neutral__diffusion_a72eca09bc6cc25f8a3831ad8e2be91d4}{find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+discontinuous}} (CS, nk, Pres\+\_\+l, hcol\+\_\+l, Tl, Sl, ppoly\+\_\+\+T\+\_\+l, ppoly\+\_\+\+S\+\_\+l, stable\+\_\+l, Pres\+\_\+r, hcol\+\_\+r, Tr, Sr, ppoly\+\_\+\+T\+\_\+r, ppoly\+\_\+\+S\+\_\+r, stable\+\_\+r, PoL, PoR, KoL, KoR, h\+Eff, zeta\+\_\+bot\+\_\+L, zeta\+\_\+bot\+\_\+R, k\+\_\+bot\+\_\+L, k\+\_\+bot\+\_\+R, hard\+\_\+fail\+\_\+heff)
\begin{DoxyCompactList}\small\item\em Higher order version of find\+\_\+neutral\+\_\+surface\+\_\+positions. Returns positions within left/right columns of combined interfaces using intracell reconstructions of T/S. Note that the polynomial reconstrcutions of T and S are optional to aid with unit testing, but will always be passed otherwise. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__neutral__diffusion_a3ef7040590d6448a34013763e17edf47}{mark\+\_\+unstable\+\_\+cells}} (CS, nk, T, S, P, stable\+\_\+cell)
\begin{DoxyCompactList}\small\item\em Sweep down through the column and mark as stable if the bottom interface of a cell is denser than the top. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__neutral__diffusion_a2952cbcce84dbdae01fce09240f819c5}{search\+\_\+other\+\_\+column}} (CS, ksurf, pos\+\_\+last, T\+\_\+from, S\+\_\+from, P\+\_\+from, T\+\_\+top, S\+\_\+top, P\+\_\+top, T\+\_\+bot, S\+\_\+bot, P\+\_\+bot, T\+\_\+poly, S\+\_\+poly)
\begin{DoxyCompactList}\small\item\em Searches the \char`\"{}other\char`\"{} (searched) column for the position of the neutral surface. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__neutral__diffusion_aa8a9c4a6e7e96ae356f913d4e8611f17}{increment\+\_\+interface}} (nk, kl, ki, reached\+\_\+bottom, searching\+\_\+this\+\_\+column, searching\+\_\+other\+\_\+column)
\begin{DoxyCompactList}\small\item\em Increments the interface which was just connected and also set flags if the bottom is reached. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__neutral__diffusion_a464e946951f3bbf7e2df500831c7afd4}{find\+\_\+neutral\+\_\+pos\+\_\+linear}} (CS, z0, T\+\_\+ref, S\+\_\+ref, d\+Rd\+T\+\_\+ref, d\+Rd\+S\+\_\+ref, d\+Rd\+T\+\_\+top, d\+Rd\+S\+\_\+top, d\+Rd\+T\+\_\+bot, d\+Rd\+S\+\_\+bot, ppoly\+\_\+T, ppoly\+\_\+S)
\begin{DoxyCompactList}\small\item\em Search a layer to find where delta\+\_\+rho = 0 based on a linear interpolation of alpha and beta of the top and bottom being searched and polynomial reconstructions of T and S. Compressibility is not needed because either, we are assuming incompressibility in the equation of state for this module or alpha and beta are calculated having been displaced to the average pressures of the two pressures We need Newton\textquotesingle{}s method because the T and S reconstructions make delta\+\_\+rho a polynomial function of z if using P\+PM or higher. If Newton\textquotesingle{}s method would search fall out of the interval \mbox{[}0,1\mbox{]}, a bisection step would be taken instead. Also this linearization of alpha, beta means that second derivatives of the E\+OS are not needed. Note that delta in variable names below refers to horizontal differences and \textquotesingle{}d\textquotesingle{} refers to vertical differences. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__neutral__diffusion_a8bef35a99b64bf7afad086a557ba7c1d}{find\+\_\+neutral\+\_\+pos\+\_\+full}} (CS, z0, T\+\_\+ref, S\+\_\+ref, P\+\_\+ref, P\+\_\+top, P\+\_\+bot, ppoly\+\_\+T, ppoly\+\_\+S)
\begin{DoxyCompactList}\small\item\em Use the full equation of state to calculate the difference in locally referenced potential density. The derivatives in this case are not trivial to calculate, so instead we use a regula falsi method. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__neutral__diffusion_a096623adeec0973cbb99cb9048946ad3}{calc\+\_\+delta\+\_\+rho\+\_\+and\+\_\+derivs}} (CS, T1, S1, p1\+\_\+in, T2, S2, p2\+\_\+in, drho, drdt1\+\_\+out, drds1\+\_\+out, drdt2\+\_\+out, drds2\+\_\+out)
\begin{DoxyCompactList}\small\item\em Calculate the difference in density between two points in a variety of ways. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__neutral__diffusion_ae0420673e01c23b26e87dc68317347a1}{delta\+\_\+rho\+\_\+from\+\_\+derivs}} (T1, S1, P1, d\+Rd\+T1, d\+Rd\+S1, T2, S2, P2, d\+Rd\+T2, d\+Rd\+S2)
\begin{DoxyCompactList}\small\item\em Calculate delta rho from derivatives and gradients of properties $ \Delta \rho = \frac{1}{2}\left[ (\alpha_1 + \alpha_2)*(T_1-T_2) + (\beta_1 + \beta_2)*(S_1-S_2) + (\gamma^{-1}_1 + \gamma^{-1}_2)*(P_1-P_2) \right] $. \end{DoxyCompactList}\item 
real function \mbox{\hyperlink{namespacemom__neutral__diffusion_a16279fc70f169ee1011030e7afecdd4e}{absolute\+\_\+position}} (n, ns, Pint, Karr, N\+Parr, k\+\_\+surface)
\begin{DoxyCompactList}\small\item\em Converts non-\/dimensional position within a layer to absolute position (for debugging) \end{DoxyCompactList}\item 
real function, dimension(ns) \mbox{\hyperlink{namespacemom__neutral__diffusion_a515d1738f61e410218b0c268c37807b3}{absolute\+\_\+positions}} (n, ns, Pint, Karr, N\+Parr)
\begin{DoxyCompactList}\small\item\em Converts non-\/dimensional positions within layers to absolute positions (for debugging) \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__neutral__diffusion_aa394289328ee392cdb17000fb6fa90e8}{neutral\+\_\+surface\+\_\+flux}} (nk, nsurf, deg, hl, hr, Tl, Tr, PiL, PiR, KoL, KoR, h\+Eff, Flx, continuous, h\+\_\+neglect, remap\+\_\+\+CS, h\+\_\+neglect\+\_\+edge)
\begin{DoxyCompactList}\small\item\em Returns a single column of neutral diffusion fluxes of a tracer. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__neutral__diffusion_a005d759280cfcb84cafa23a984ffc818}{neutral\+\_\+surface\+\_\+t\+\_\+eval}} (nk, ns, k\+\_\+sub, Ks, Ps, T\+\_\+mean, T\+\_\+int, deg, i\+Method, T\+\_\+poly, T\+\_\+top, T\+\_\+bot, T\+\_\+sub, T\+\_\+top\+\_\+int, T\+\_\+bot\+\_\+int, T\+\_\+layer)
\begin{DoxyCompactList}\small\item\em Evaluate various parts of the reconstructions to calculate gradient-\/based flux limter. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__neutral__diffusion_a9b380e21dcc9c53efcec4712228b931f}{ppm\+\_\+left\+\_\+right\+\_\+edge\+\_\+values}} (nk, Tl, Ti, aL, aR)
\begin{DoxyCompactList}\small\item\em Discontinuous P\+PM reconstructions of the left/right edge values within a cell. \end{DoxyCompactList}\item 
logical function, public \mbox{\hyperlink{namespacemom__neutral__diffusion_a6f91c93a6363fb39395796420f25fad2}{neutral\+\_\+diffusion\+\_\+unit\+\_\+tests}} (verbose)
\begin{DoxyCompactList}\small\item\em Returns true if unit tests of neutral\+\_\+diffusion functions fail. Otherwise returns false. \end{DoxyCompactList}\item 
logical function \mbox{\hyperlink{namespacemom__neutral__diffusion_a9aaf0988f76c9992d8f1db48ce23bf34}{ndiff\+\_\+unit\+\_\+tests\+\_\+continuous}} (verbose)
\begin{DoxyCompactList}\small\item\em Returns true if unit tests of neutral\+\_\+diffusion functions fail. Otherwise returns false. \end{DoxyCompactList}\item 
logical function \mbox{\hyperlink{namespacemom__neutral__diffusion_aa44f980bb24df5dc71913b32892b2f71}{ndiff\+\_\+unit\+\_\+tests\+\_\+discontinuous}} (verbose)
\item 
logical function \mbox{\hyperlink{namespacemom__neutral__diffusion_ac51219df490c739774d70d9958bae047}{test\+\_\+fv\+\_\+diff}} (verbose, hkm1, hk, hkp1, Skm1, Sk, Skp1, Ptrue, title)
\begin{DoxyCompactList}\small\item\em Returns true if a test of \mbox{\hyperlink{namespacemom__neutral__diffusion_a1c5ac28d7c54581cdd872fe58ef0b204}{fv\+\_\+diff()}} fails, and conditionally writes results to stream. \end{DoxyCompactList}\item 
logical function \mbox{\hyperlink{namespacemom__neutral__diffusion_ad964ab49cd5901b79dc32f2367d24aed}{test\+\_\+fvlsq\+\_\+slope}} (verbose, hkm1, hk, hkp1, Skm1, Sk, Skp1, Ptrue, title)
\begin{DoxyCompactList}\small\item\em Returns true if a test of \mbox{\hyperlink{namespacemom__neutral__diffusion_a6ed3814088af470ad6fc4a3a80926fd7}{fvlsq\+\_\+slope()}} fails, and conditionally writes results to stream. \end{DoxyCompactList}\item 
logical function \mbox{\hyperlink{namespacemom__neutral__diffusion_a1a21234946c77d1e4ed852a4173503f1}{test\+\_\+ifndp}} (verbose, rho\+Neg, Pneg, rho\+Pos, Ppos, Ptrue, title)
\begin{DoxyCompactList}\small\item\em Returns true if a test of \mbox{\hyperlink{namespacemom__neutral__diffusion_a666836e70bdae7ef4ec271246cf91993}{interpolate\+\_\+for\+\_\+nondim\+\_\+position()}} fails, and conditionally writes results to stream. \end{DoxyCompactList}\item 
logical function \mbox{\hyperlink{namespacemom__neutral__diffusion_a2870eae4b580e548577943e34321b8f0}{test\+\_\+data1d}} (verbose, nk, Po, Ptrue, title)
\begin{DoxyCompactList}\small\item\em Returns true if comparison of Po and Ptrue fails, and conditionally writes results to stream. \end{DoxyCompactList}\item 
logical function \mbox{\hyperlink{namespacemom__neutral__diffusion_aaa2d682e80cdba470a09bcff6bda1767}{test\+\_\+data1di}} (verbose, nk, Po, Ptrue, title)
\begin{DoxyCompactList}\small\item\em Returns true if comparison of Po and Ptrue fails, and conditionally writes results to stream. \end{DoxyCompactList}\item 
logical function \mbox{\hyperlink{namespacemom__neutral__diffusion_ab5eaa2002c1f6405f5037645bbca3101}{test\+\_\+nsp}} (verbose, ns, KoL, KoR, pL, pR, h\+Eff, Ko\+L0, Ko\+R0, p\+L0, p\+R0, h\+Eff0, title)
\begin{DoxyCompactList}\small\item\em Returns true if output of find\+\_\+neutral\+\_\+surface\+\_\+positions() does not match correct values, and conditionally writes results to stream. \end{DoxyCompactList}\item 
logical function \mbox{\hyperlink{namespacemom__neutral__diffusion_a5725b17791c79900343532931ac04503}{compare\+\_\+nsp\+\_\+row}} (KoL, KoR, pL, pR, Ko\+L0, Ko\+R0, p\+L0, p\+R0)
\begin{DoxyCompactList}\small\item\em Compares a single row, k, of output from find\+\_\+neutral\+\_\+surface\+\_\+positions() \end{DoxyCompactList}\item 
logical function \mbox{\hyperlink{namespacemom__neutral__diffusion_ab75333897fa8800441d8eddc1720f631}{test\+\_\+rnp}} (expected\+\_\+pos, test\+\_\+pos, title)
\begin{DoxyCompactList}\small\item\em Compares output position from refine\+\_\+nondim\+\_\+position with an expected value. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__neutral__diffusion_ae8a313d25818dfa8c3bc056210fac76b}{neutral\+\_\+diffusion\+\_\+end}} (CS)
\begin{DoxyCompactList}\small\item\em Deallocates neutral\+\_\+diffusion control structure. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Variables}
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{namespacemom__neutral__diffusion_afdf44450544eb51e29afe3454ecbc0cc}\label{namespacemom__neutral__diffusion_afdf44450544eb51e29afe3454ecbc0cc}} 
character(len=40) \mbox{\hyperlink{namespacemom__neutral__diffusion_afdf44450544eb51e29afe3454ecbc0cc}{mdl}} = \char`\"{}M\+O\+M\+\_\+neutral\+\_\+diffusion\char`\"{}
\begin{DoxyCompactList}\small\item\em module name \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a16279fc70f169ee1011030e7afecdd4e}\label{namespacemom__neutral__diffusion_a16279fc70f169ee1011030e7afecdd4e}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!absolute\+\_\+position@{absolute\+\_\+position}}
\index{absolute\+\_\+position@{absolute\+\_\+position}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{absolute\+\_\+position()}{absolute\_position()}}
{\footnotesize\ttfamily real function mom\+\_\+neutral\+\_\+diffusion\+::absolute\+\_\+position (\begin{DoxyParamCaption}\item[{integer, intent(in)}]{n,  }\item[{integer, intent(in)}]{ns,  }\item[{real, dimension(n+1), intent(in)}]{Pint,  }\item[{integer, dimension(ns), intent(in)}]{Karr,  }\item[{real, dimension(ns), intent(in)}]{N\+Parr,  }\item[{integer, intent(in)}]{k\+\_\+surface }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Converts non-\/dimensional position within a layer to absolute position (for debugging) 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em n} & Number of levels\\
\hline
\mbox{\tt in}  & {\em ns} & Number of neutral surfaces\\
\hline
\mbox{\tt in}  & {\em pint} & Position of interfaces \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} or other units\\
\hline
\mbox{\tt in}  & {\em karr} & Index of interface above position\\
\hline
\mbox{\tt in}  & {\em nparr} & Non-\/dimensional position within layer Karr(\+:) \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em k\+\_\+surface} & k-\/interface to query\\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
The absolute position of a location \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} or other units following Pint 
\end{DoxyReturn}


Definition at line 1837 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
1837   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{intent(in)} :: n\textcolor{comment}{            !< Number of levels}
1838   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{intent(in)} :: ns\textcolor{comment}{           !< Number of neutral surfaces}
1839   \textcolor{keywordtype}{real},    \textcolor{keywordtype}{intent(in)} :: Pint(n+1)\textcolor{comment}{    !< Position of interfaces [R L2 T-2 ~> Pa] or other units}
1840   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{intent(in)} :: Karr(ns)\textcolor{comment}{     !< Index of interface above position}
1841   \textcolor{keywordtype}{real},    \textcolor{keywordtype}{intent(in)} :: NParr(ns)\textcolor{comment}{    !< Non-dimensional position within layer Karr(:) [nondim]}
1842   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{intent(in)} :: k\_surface\textcolor{comment}{    !< k-interface to query}
1843   \textcolor{keywordtype}{real}                :: absolute\_position\textcolor{comment}{ !< The absolute position of a location [R L2 T-2 ~> Pa]}
1844 \textcolor{comment}{                                      !! or other units following Pint}
1845   \textcolor{comment}{! Local variables}
1846   \textcolor{keywordtype}{integer} :: k
1847 
1848   k = karr(k\_surface)
1849   \textcolor{keywordflow}{if} (k>n) stop \textcolor{stringliteral}{'absolute\_position: k>nk is out of bounds!'}
1850   absolute\_position = pint(k) + nparr(k\_surface) * ( pint(k+1) - pint(k) )
1851 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a515d1738f61e410218b0c268c37807b3}\label{namespacemom__neutral__diffusion_a515d1738f61e410218b0c268c37807b3}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!absolute\+\_\+positions@{absolute\+\_\+positions}}
\index{absolute\+\_\+positions@{absolute\+\_\+positions}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{absolute\+\_\+positions()}{absolute\_positions()}}
{\footnotesize\ttfamily real function, dimension(ns) mom\+\_\+neutral\+\_\+diffusion\+::absolute\+\_\+positions (\begin{DoxyParamCaption}\item[{integer, intent(in)}]{n,  }\item[{integer, intent(in)}]{ns,  }\item[{real, dimension(n+1), intent(in)}]{Pint,  }\item[{integer, dimension(ns), intent(in)}]{Karr,  }\item[{real, dimension(ns), intent(in)}]{N\+Parr }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Converts non-\/dimensional positions within layers to absolute positions (for debugging) 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em n} & Number of levels\\
\hline
\mbox{\tt in}  & {\em ns} & Number of neutral surfaces\\
\hline
\mbox{\tt in}  & {\em pint} & Position of interface \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} or other units\\
\hline
\mbox{\tt in}  & {\em karr} & Indexes of interfaces about positions\\
\hline
\mbox{\tt in}  & {\em nparr} & Non-\/dimensional positions within layers Karr(\+:)\\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
Absolute positions \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} or other units following Pint 
\end{DoxyReturn}


Definition at line 1856 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
1856   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{intent(in)} :: n\textcolor{comment}{         !< Number of levels}
1857   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{intent(in)} :: ns\textcolor{comment}{        !< Number of neutral surfaces}
1858   \textcolor{keywordtype}{real},    \textcolor{keywordtype}{intent(in)} :: Pint(n+1)\textcolor{comment}{ !< Position of interface [R L2 T-2 ~> Pa] or other units}
1859   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{intent(in)} :: Karr(ns)\textcolor{comment}{  !< Indexes of interfaces about positions}
1860   \textcolor{keywordtype}{real},    \textcolor{keywordtype}{intent(in)} :: NParr(ns)\textcolor{comment}{ !< Non-dimensional positions within layers Karr(:)}
1861 
1862   \textcolor{keywordtype}{real},  \textcolor{keywordtype}{dimension(ns)} :: absolute\_positions\textcolor{comment}{ !< Absolute positions [R L2 T-2 ~> Pa]}
1863 \textcolor{comment}{                                   !! or other units following Pint}
1864 
1865   \textcolor{comment}{! Local variables}
1866   \textcolor{keywordtype}{integer} :: k\_surface, k
1867 
1868   \textcolor{keywordflow}{do} k\_surface = 1, ns
1869     absolute\_positions(k\_surface) = absolute\_position(n,ns,pint,karr,nparr,k\_surface)
1870 \textcolor{keywordflow}{  enddo}
1871 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a096623adeec0973cbb99cb9048946ad3}\label{namespacemom__neutral__diffusion_a096623adeec0973cbb99cb9048946ad3}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!calc\+\_\+delta\+\_\+rho\+\_\+and\+\_\+derivs@{calc\+\_\+delta\+\_\+rho\+\_\+and\+\_\+derivs}}
\index{calc\+\_\+delta\+\_\+rho\+\_\+and\+\_\+derivs@{calc\+\_\+delta\+\_\+rho\+\_\+and\+\_\+derivs}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{calc\+\_\+delta\+\_\+rho\+\_\+and\+\_\+derivs()}{calc\_delta\_rho\_and\_derivs()}}
{\footnotesize\ttfamily subroutine mom\+\_\+neutral\+\_\+diffusion\+::calc\+\_\+delta\+\_\+rho\+\_\+and\+\_\+derivs (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__neutral__diffusion_1_1neutral__diffusion__cs}{neutral\+\_\+diffusion\+\_\+cs}})}]{CS,  }\item[{real, intent(in)}]{T1,  }\item[{real, intent(in)}]{S1,  }\item[{real, intent(in)}]{p1\+\_\+in,  }\item[{real, intent(in)}]{T2,  }\item[{real, intent(in)}]{S2,  }\item[{real, intent(in)}]{p2\+\_\+in,  }\item[{real, intent(out)}]{drho,  }\item[{real, intent(out), optional}]{drdt1\+\_\+out,  }\item[{real, intent(out), optional}]{drds1\+\_\+out,  }\item[{real, intent(out), optional}]{drdt2\+\_\+out,  }\item[{real, intent(out), optional}]{drds2\+\_\+out }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Calculate the difference in density between two points in a variety of ways. 


\begin{DoxyParams}[1]{Parameters}
 & {\em cs} & Neutral diffusion control structure\\
\hline
\mbox{\tt in}  & {\em t1} & Temperature at point 1 \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s1} & Salinity at point 1 \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em p1\+\_\+in} & Pressure at point 1 \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt in}  & {\em t2} & Temperature at point 2 \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s2} & Salinity at point 2 \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em p2\+\_\+in} & Pressure at point 2 \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt out}  & {\em drho} & Difference in density between the two points \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}\\
\hline
\mbox{\tt out}  & {\em drdt1\+\_\+out} & drho\+\_\+dt at point 1 \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}\\
\hline
\mbox{\tt out}  & {\em drds1\+\_\+out} & drho\+\_\+ds at point 1 \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}\\
\hline
\mbox{\tt out}  & {\em drdt2\+\_\+out} & drho\+\_\+dt at point 2 \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}\\
\hline
\mbox{\tt out}  & {\em drds2\+\_\+out} & drho\+\_\+ds at point 2 \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 1756 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
1756   \textcolor{keywordtype}{type}(neutral\_diffusion\_CS)    :: CS\textcolor{comment}{        !< Neutral diffusion control structure}
1757   \textcolor{keywordtype}{real},           \textcolor{keywordtype}{intent(in   )} :: T1\textcolor{comment}{        !< Temperature at point 1 [degC]}
1758   \textcolor{keywordtype}{real},           \textcolor{keywordtype}{intent(in   )} :: S1\textcolor{comment}{        !< Salinity at point 1 [ppt]}
1759   \textcolor{keywordtype}{real},           \textcolor{keywordtype}{intent(in   )} :: p1\_in\textcolor{comment}{     !< Pressure at point 1 [R L2 T-2 ~> Pa]}
1760   \textcolor{keywordtype}{real},           \textcolor{keywordtype}{intent(in   )} :: T2\textcolor{comment}{        !< Temperature at point 2 [degC]}
1761   \textcolor{keywordtype}{real},           \textcolor{keywordtype}{intent(in   )} :: S2\textcolor{comment}{        !< Salinity at point 2 [ppt]}
1762   \textcolor{keywordtype}{real},           \textcolor{keywordtype}{intent(in   )} :: p2\_in\textcolor{comment}{     !< Pressure at point 2 [R L2 T-2 ~> Pa]}
1763   \textcolor{keywordtype}{real},           \textcolor{keywordtype}{intent(  out)} :: drho\textcolor{comment}{      !< Difference in density between the two points [R ~> kg m-3]}
1764   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(  out)} :: dRdT1\_out\textcolor{comment}{ !< drho\_dt at point 1 [R degC-1 ~> kg m-3 degC-1]}
1765   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(  out)} :: dRdS1\_out\textcolor{comment}{ !< drho\_ds at point 1 [R ppt-1 ~> kg m-3 ppt-1]}
1766   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(  out)} :: dRdT2\_out\textcolor{comment}{ !< drho\_dt at point 2 [R degC-1 ~> kg m-3 degC-1]}
1767   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(  out)} :: dRdS2\_out\textcolor{comment}{ !< drho\_ds at point 2 [R ppt-1 ~> kg m-3 ppt-1]}
1768   \textcolor{comment}{! Local variables}
1769   \textcolor{keywordtype}{real} :: rho1, rho2   \textcolor{comment}{! Densities [R ~> kg m-3]}
1770   \textcolor{keywordtype}{real} :: p1, p2, pmid \textcolor{comment}{! Pressures [R L2 T-2 ~> Pa]}
1771   \textcolor{keywordtype}{real} :: drdt1, drdt2 \textcolor{comment}{! Partial derivatives of density with temperature [R degC-1 ~> kg m-3 degC-1]}
1772   \textcolor{keywordtype}{real} :: drds1, drds2 \textcolor{comment}{! Partial derivatives of density with salinity [R ppt-1 ~> kg m-3 ppt-1]}
1773   \textcolor{keywordtype}{real} :: drdp1, drdp2 \textcolor{comment}{! Partial derivatives of density with pressure [T2 L-2 ~> s2 m-2]}
1774 
1775   \textcolor{comment}{! Use the same reference pressure or the in-situ pressure}
1776   \textcolor{keywordflow}{if} (cs%ref\_pres > 0.) \textcolor{keywordflow}{then}
1777     p1 = cs%ref\_pres
1778     p2 = cs%ref\_pres
1779   \textcolor{keywordflow}{else}
1780     p1 = p1\_in
1781     p2 = p2\_in
1782 \textcolor{keywordflow}{  endif}
1783 
1784   \textcolor{comment}{! Use the full linear equation of state to calculate the difference in density (expensive!)}
1785   \textcolor{keywordflow}{if}     (trim(cs%delta\_rho\_form) == \textcolor{stringliteral}{'full'}) \textcolor{keywordflow}{then}
1786     pmid = 0.5 * (p1 + p2)
1787     \textcolor{keyword}{call }calculate\_density( t1, s1, pmid, rho1, cs%EOS)
1788     \textcolor{keyword}{call }calculate\_density( t2, s2, pmid, rho2, cs%EOS)
1789     drho = rho1 - rho2
1790   \textcolor{comment}{! Use the density derivatives at the average of pressures and the differentces int temperature}
1791   \textcolor{keywordflow}{elseif} (trim(cs%delta\_rho\_form) == \textcolor{stringliteral}{'mid\_pressure'}) \textcolor{keywordflow}{then}
1792     pmid = 0.5 * (p1 + p2)
1793     \textcolor{keywordflow}{if} (cs%ref\_pres>=0) pmid = cs%ref\_pres
1794     \textcolor{keyword}{call }calculate\_density\_derivs(t1, s1, pmid, drdt1, drds1, cs%EOS)
1795     \textcolor{keyword}{call }calculate\_density\_derivs(t2, s2, pmid, drdt2, drds2, cs%EOS)
1796     drho = delta\_rho\_from\_derivs( t1, s1, p1, drdt1, drds1, t2, s2, p2, drdt2, drds2)
1797   \textcolor{keywordflow}{elseif} (trim(cs%delta\_rho\_form) == \textcolor{stringliteral}{'local\_pressure'}) \textcolor{keywordflow}{then}
1798     \textcolor{keyword}{call }calculate\_density\_derivs(t1, s1, p1, drdt1, drds1, cs%EOS)
1799     \textcolor{keyword}{call }calculate\_density\_derivs(t2, s2, p2, drdt2, drds2, cs%EOS)
1800     drho = delta\_rho\_from\_derivs( t1, s1, p1, drdt1, drds1, t2, s2, p2, drdt2, drds2)
1801   \textcolor{keywordflow}{else}
1802     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"delta\_rho\_form is not recognized"})
1803 \textcolor{keywordflow}{  endif}
1804 
1805   \textcolor{keywordflow}{if} (\textcolor{keyword}{PRESENT}(drdt1\_out)) drdt1\_out = drdt1
1806   \textcolor{keywordflow}{if} (\textcolor{keyword}{PRESENT}(drds1\_out)) drds1\_out = drds1
1807   \textcolor{keywordflow}{if} (\textcolor{keyword}{PRESENT}(drdt2\_out)) drdt2\_out = drdt2
1808   \textcolor{keywordflow}{if} (\textcolor{keyword}{PRESENT}(drds2\_out)) drds2\_out = drds2
1809 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a5725b17791c79900343532931ac04503}\label{namespacemom__neutral__diffusion_a5725b17791c79900343532931ac04503}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!compare\+\_\+nsp\+\_\+row@{compare\+\_\+nsp\+\_\+row}}
\index{compare\+\_\+nsp\+\_\+row@{compare\+\_\+nsp\+\_\+row}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{compare\+\_\+nsp\+\_\+row()}{compare\_nsp\_row()}}
{\footnotesize\ttfamily logical function mom\+\_\+neutral\+\_\+diffusion\+::compare\+\_\+nsp\+\_\+row (\begin{DoxyParamCaption}\item[{integer, intent(in)}]{KoL,  }\item[{integer, intent(in)}]{KoR,  }\item[{real, intent(in)}]{pL,  }\item[{real, intent(in)}]{pR,  }\item[{integer, intent(in)}]{Ko\+L0,  }\item[{integer, intent(in)}]{Ko\+R0,  }\item[{real, intent(in)}]{p\+L0,  }\item[{real, intent(in)}]{p\+R0 }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Compares a single row, k, of output from find\+\_\+neutral\+\_\+surface\+\_\+positions() 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em kol} & Index of first left interface above neutral surface\\
\hline
\mbox{\tt in}  & {\em kor} & Index of first right interface above neutral surface\\
\hline
\mbox{\tt in}  & {\em pl} & Fractional position of neutral surface within layer KoL of left column\\
\hline
\mbox{\tt in}  & {\em pr} & Fractional position of neutral surface within layer KoR of right column\\
\hline
\mbox{\tt in}  & {\em kol0} & Correct value for KoL\\
\hline
\mbox{\tt in}  & {\em kor0} & Correct value for KoR\\
\hline
\mbox{\tt in}  & {\em pl0} & Correct value for pL\\
\hline
\mbox{\tt in}  & {\em pr0} & Correct value for pR \\
\hline
\end{DoxyParams}


Definition at line 2829 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2829   \textcolor{keywordtype}{integer},  \textcolor{keywordtype}{intent(in)} :: KoL\textcolor{comment}{   !< Index of first left interface above neutral surface}
2830   \textcolor{keywordtype}{integer},  \textcolor{keywordtype}{intent(in)} :: KoR\textcolor{comment}{   !< Index of first right interface above neutral surface}
2831   \textcolor{keywordtype}{real},     \textcolor{keywordtype}{intent(in)} :: pL\textcolor{comment}{    !< Fractional position of neutral surface within layer KoL of left column}
2832   \textcolor{keywordtype}{real},     \textcolor{keywordtype}{intent(in)} :: pR\textcolor{comment}{    !< Fractional position of neutral surface within layer KoR of right column}
2833   \textcolor{keywordtype}{integer},  \textcolor{keywordtype}{intent(in)} :: KoL0\textcolor{comment}{  !< Correct value for KoL}
2834   \textcolor{keywordtype}{integer},  \textcolor{keywordtype}{intent(in)} :: KoR0\textcolor{comment}{  !< Correct value for KoR}
2835   \textcolor{keywordtype}{real},     \textcolor{keywordtype}{intent(in)} :: pL0\textcolor{comment}{   !< Correct value for pL}
2836   \textcolor{keywordtype}{real},     \textcolor{keywordtype}{intent(in)} :: pR0\textcolor{comment}{   !< Correct value for pR}
2837 
2838   compare\_nsp\_row = .false.
2839   \textcolor{keywordflow}{if} (kol /= kol0) compare\_nsp\_row = .true.
2840   \textcolor{keywordflow}{if} (kor /= kor0) compare\_nsp\_row = .true.
2841   \textcolor{keywordflow}{if} (pl /= pl0) compare\_nsp\_row = .true.
2842   \textcolor{keywordflow}{if} (pr /= pr0) compare\_nsp\_row = .true.
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_ae0420673e01c23b26e87dc68317347a1}\label{namespacemom__neutral__diffusion_ae0420673e01c23b26e87dc68317347a1}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!delta\+\_\+rho\+\_\+from\+\_\+derivs@{delta\+\_\+rho\+\_\+from\+\_\+derivs}}
\index{delta\+\_\+rho\+\_\+from\+\_\+derivs@{delta\+\_\+rho\+\_\+from\+\_\+derivs}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{delta\+\_\+rho\+\_\+from\+\_\+derivs()}{delta\_rho\_from\_derivs()}}
{\footnotesize\ttfamily real function mom\+\_\+neutral\+\_\+diffusion\+::delta\+\_\+rho\+\_\+from\+\_\+derivs (\begin{DoxyParamCaption}\item[{real}]{T1,  }\item[{real}]{S1,  }\item[{real}]{P1,  }\item[{real}]{d\+Rd\+T1,  }\item[{real}]{d\+Rd\+S1,  }\item[{real}]{T2,  }\item[{real}]{S2,  }\item[{real}]{P2,  }\item[{real}]{d\+Rd\+T2,  }\item[{real}]{d\+Rd\+S2 }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Calculate delta rho from derivatives and gradients of properties $ \Delta \rho = \frac{1}{2}\left[ (\alpha_1 + \alpha_2)*(T_1-T_2) + (\beta_1 + \beta_2)*(S_1-S_2) + (\gamma^{-1}_1 + \gamma^{-1}_2)*(P_1-P_2) \right] $. 


\begin{DoxyParams}{Parameters}
{\em t1} & Temperature at point 1 \mbox{[}degC\mbox{]}\\
\hline
{\em s1} & Salinity at point 1 \mbox{[}ppt\mbox{]}\\
\hline
{\em p1} & Pressure at point 1 \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
{\em drdt1} & The partial derivative of density with temperature at point 1 \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}\\
\hline
{\em drds1} & The partial derivative of density with salinity at point 1 \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}\\
\hline
{\em t2} & Temperature at point 2 \mbox{[}degC\mbox{]}\\
\hline
{\em s2} & Salinity at point 2 \mbox{[}ppt\mbox{]}\\
\hline
{\em p2} & Pressure at point 2 \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
{\em drdt2} & The partial derivative of density with temperature at point 2 \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}\\
\hline
{\em drds2} & The partial derivative of density with salinity at point 2 \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 1818 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
1818   \textcolor{keywordtype}{real} :: T1\textcolor{comment}{    !< Temperature at point 1 [degC]}
1819   \textcolor{keywordtype}{real} :: S1\textcolor{comment}{    !< Salinity at point 1 [ppt]}
1820   \textcolor{keywordtype}{real} :: P1\textcolor{comment}{    !< Pressure at point 1 [R L2 T-2 ~> Pa]}
1821   \textcolor{keywordtype}{real} :: dRdT1\textcolor{comment}{ !< The partial derivative of density with temperature at point 1 [R degC-1 ~> kg m-3
       degC-1]}
1822   \textcolor{keywordtype}{real} :: dRdS1\textcolor{comment}{ !< The partial derivative of density with salinity at point 1 [R ppt-1 ~> kg m-3 ppt-1]}
1823   \textcolor{keywordtype}{real} :: T2\textcolor{comment}{    !< Temperature at point 2 [degC]}
1824   \textcolor{keywordtype}{real} :: S2\textcolor{comment}{    !< Salinity at point 2 [ppt]}
1825   \textcolor{keywordtype}{real} :: P2\textcolor{comment}{    !< Pressure at point 2 [R L2 T-2 ~> Pa]}
1826   \textcolor{keywordtype}{real} :: dRdT2\textcolor{comment}{ !< The partial derivative of density with temperature at point 2 [R degC-1 ~> kg m-3
       degC-1]}
1827   \textcolor{keywordtype}{real} :: dRdS2\textcolor{comment}{ !< The partial derivative of density with salinity at point 2 [R ppt-1 ~> kg m-3 ppt-1]}
1828   \textcolor{comment}{! Local variables}
1829   \textcolor{keywordtype}{real} :: drho  \textcolor{comment}{! The density difference [R ~> kg m-3]}
1830 
1831   drho = 0.5 * ( (drdt1+drdt2)*(t1-t2) + (drds1+drds2)*(s1-s2))
1832 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a8bef35a99b64bf7afad086a557ba7c1d}\label{namespacemom__neutral__diffusion_a8bef35a99b64bf7afad086a557ba7c1d}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!find\+\_\+neutral\+\_\+pos\+\_\+full@{find\+\_\+neutral\+\_\+pos\+\_\+full}}
\index{find\+\_\+neutral\+\_\+pos\+\_\+full@{find\+\_\+neutral\+\_\+pos\+\_\+full}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{find\+\_\+neutral\+\_\+pos\+\_\+full()}{find\_neutral\_pos\_full()}}
{\footnotesize\ttfamily real function mom\+\_\+neutral\+\_\+diffusion\+::find\+\_\+neutral\+\_\+pos\+\_\+full (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__neutral__diffusion_1_1neutral__diffusion__cs}{neutral\+\_\+diffusion\+\_\+cs}}), intent(in)}]{CS,  }\item[{real, intent(in)}]{z0,  }\item[{real, intent(in)}]{T\+\_\+ref,  }\item[{real, intent(in)}]{S\+\_\+ref,  }\item[{real, intent(in)}]{P\+\_\+ref,  }\item[{real, intent(in)}]{P\+\_\+top,  }\item[{real, intent(in)}]{P\+\_\+bot,  }\item[{real, dimension(\+:), intent(in)}]{ppoly\+\_\+T,  }\item[{real, dimension(\+:), intent(in)}]{ppoly\+\_\+S }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Use the full equation of state to calculate the difference in locally referenced potential density. The derivatives in this case are not trivial to calculate, so instead we use a regula falsi method. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em cs} & Control structure with parameters for this module\\
\hline
\mbox{\tt in}  & {\em z0} & Lower bound of position, also serves as the initial guess \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em t\+\_\+ref} & Temperature at the searched from interface \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s\+\_\+ref} & Salinity at the searched from interface \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em p\+\_\+ref} & Pressure at the searched from interface \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt in}  & {\em p\+\_\+top} & Pressure at top of layer being searched \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt in}  & {\em p\+\_\+bot} & Pressure at bottom of layer being searched \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt in}  & {\em ppoly\+\_\+t} & Coefficients of the polynomial reconstruction of T within the layer to be searched \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em ppoly\+\_\+s} & Coefficients of the polynomial reconstruction of T within the layer to be searched \mbox{[}ppt\mbox{]}\\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
Position where drho = 0 \mbox{[}nondim\mbox{]} 
\end{DoxyReturn}


Definition at line 1662 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
1662   \textcolor{keywordtype}{type}(neutral\_diffusion\_CS),\textcolor{keywordtype}{intent(in)} :: CS\textcolor{comment}{        !< Control structure with parameters for this module}
1663   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: z0\textcolor{comment}{        !< Lower bound of position, also serves as the}
1664 \textcolor{comment}{                                                     !! initial guess [nondim]}
1665   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: T\_ref\textcolor{comment}{     !< Temperature at the searched from interface [degC]}
1666   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: S\_ref\textcolor{comment}{     !< Salinity at the searched from interface [ppt]}
1667   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: P\_ref\textcolor{comment}{     !< Pressure at the searched from interface [R L2 T-2
       ~> Pa]}
1668   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: P\_top\textcolor{comment}{     !< Pressure at top of layer being searched [R L2 T-2
       ~> Pa]}
1669   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: P\_bot\textcolor{comment}{     !< Pressure at bottom of layer being searched [R L2
       T-2 ~> Pa]}
1670   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)},        \textcolor{keywordtype}{intent(in)} :: ppoly\_T\textcolor{comment}{   !< Coefficients of the polynomial reconstruction of T
       within}
1671 \textcolor{comment}{                                                     !! the layer to be searched [degC]}
1672   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)},        \textcolor{keywordtype}{intent(in)} :: ppoly\_S\textcolor{comment}{   !< Coefficients of the polynomial reconstruction of T
       within}
1673 \textcolor{comment}{                                                     !! the layer to be searched [ppt]}
1674   \textcolor{keywordtype}{real}                                  :: z\textcolor{comment}{         !< Position where drho = 0 [nondim]}
1675   \textcolor{comment}{! Local variables}
1676   \textcolor{keywordtype}{integer} :: iter
1677   \textcolor{keywordtype}{integer} :: nterm
1678 
1679   \textcolor{keywordtype}{real} :: drho\_a, drho\_b, drho\_c \textcolor{comment}{! Density differences [R ~> kg m-3]}
1680   \textcolor{keywordtype}{real} :: a, b, c     \textcolor{comment}{! Fractional positions [nondim]}
1681   \textcolor{keywordtype}{real} :: Ta, Tb, Tc  \textcolor{comment}{! Temperatures [degC]}
1682   \textcolor{keywordtype}{real} :: Sa, Sb, Sc  \textcolor{comment}{! Salinities [ppt]}
1683   \textcolor{keywordtype}{real} :: Pa, Pb, Pc  \textcolor{comment}{! Pressures [R L2 T-2 ~> Pa]}
1684   \textcolor{keywordtype}{integer} :: side
1685 
1686   side = 0
1687   \textcolor{comment}{! Set the first two evaluation to the endpoints of the interval}
1688   b = z0 ; c = 1
1689   nterm = \textcolor{keyword}{SIZE}(ppoly\_t)
1690 
1691   \textcolor{comment}{! Calculate drho at the minimum bound}
1692   tb = evaluation\_polynomial( ppoly\_t, nterm, b )
1693   sb = evaluation\_polynomial( ppoly\_s, nterm, b )
1694   pb = p\_top*(1.-b) + p\_bot*b
1695   \textcolor{keyword}{call }calc\_delta\_rho\_and\_derivs(cs, tb, sb, pb, t\_ref, s\_ref, p\_ref, drho\_b)
1696 
1697   \textcolor{comment}{! Calculate drho at the maximum bound}
1698   tc = evaluation\_polynomial( ppoly\_t, nterm, 1. )
1699   sc = evaluation\_polynomial( ppoly\_s, nterm, 1. )
1700   pc = p\_bot
1701   \textcolor{keyword}{call }calc\_delta\_rho\_and\_derivs(cs, tc, sc, pc, t\_ref, s\_ref, p\_ref, drho\_c)
1702 
1703   \textcolor{keywordflow}{if} (drho\_b >= 0.) \textcolor{keywordflow}{then}
1704     z = z0
1705     \textcolor{keywordflow}{return}
1706   \textcolor{keywordflow}{elseif} (drho\_c == 0.) \textcolor{keywordflow}{then}
1707     z = 1.
1708     \textcolor{keywordflow}{return}
1709 \textcolor{keywordflow}{  endif}
1710   \textcolor{keywordflow}{if} ( sign(1.,drho\_b) == sign(1.,drho\_c) ) \textcolor{keywordflow}{then}
1711     z = z0
1712     \textcolor{keywordflow}{return}
1713 \textcolor{keywordflow}{  endif}
1714 
1715   \textcolor{keywordflow}{do} iter = 1, cs%max\_iter
1716     \textcolor{comment}{! Calculate new position and evaluate if we have converged}
1717     a = (drho\_b*c - drho\_c*b)/(drho\_b-drho\_c)
1718     ta = evaluation\_polynomial( ppoly\_t, nterm, a )
1719     sa = evaluation\_polynomial( ppoly\_s, nterm, a )
1720     pa = p\_top*(1.-a) + p\_bot*a
1721     \textcolor{keyword}{call }calc\_delta\_rho\_and\_derivs(cs, ta, sa, pa, t\_ref, s\_ref, p\_ref, drho\_a)
1722     \textcolor{keywordflow}{if} (abs(drho\_a) < cs%drho\_tol) \textcolor{keywordflow}{then}
1723       z = a
1724       \textcolor{keywordflow}{return}
1725 \textcolor{keywordflow}{    endif}
1726 
1727     \textcolor{keywordflow}{if} (drho\_a*drho\_c > 0.) \textcolor{keywordflow}{then}
1728       \textcolor{keywordflow}{if} ( abs(a-c)<cs%x\_tol) \textcolor{keywordflow}{then}
1729         z = a
1730         \textcolor{keywordflow}{return}
1731 \textcolor{keywordflow}{      endif}
1732       c = a ; drho\_c = drho\_a;
1733       \textcolor{keywordflow}{if} (side == -1) drho\_b = 0.5*drho\_b
1734       side = -1
1735     \textcolor{keywordflow}{elseif} ( drho\_b*drho\_a > 0 ) \textcolor{keywordflow}{then}
1736       \textcolor{keywordflow}{if} ( abs(a-b)<cs%x\_tol) \textcolor{keywordflow}{then}
1737         z = a
1738         \textcolor{keywordflow}{return}
1739 \textcolor{keywordflow}{      endif}
1740       b = a ; drho\_b = drho\_a
1741       \textcolor{keywordflow}{if} (side == 1) drho\_c = 0.5*drho\_c
1742       side = 1
1743     \textcolor{keywordflow}{else}
1744       z = a
1745       \textcolor{keywordflow}{return}
1746 \textcolor{keywordflow}{    endif}
1747 \textcolor{keywordflow}{  enddo}
1748 
1749   z = a
1750 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a464e946951f3bbf7e2df500831c7afd4}\label{namespacemom__neutral__diffusion_a464e946951f3bbf7e2df500831c7afd4}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!find\+\_\+neutral\+\_\+pos\+\_\+linear@{find\+\_\+neutral\+\_\+pos\+\_\+linear}}
\index{find\+\_\+neutral\+\_\+pos\+\_\+linear@{find\+\_\+neutral\+\_\+pos\+\_\+linear}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{find\+\_\+neutral\+\_\+pos\+\_\+linear()}{find\_neutral\_pos\_linear()}}
{\footnotesize\ttfamily real function mom\+\_\+neutral\+\_\+diffusion\+::find\+\_\+neutral\+\_\+pos\+\_\+linear (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__neutral__diffusion_1_1neutral__diffusion__cs}{neutral\+\_\+diffusion\+\_\+cs}}), intent(in)}]{CS,  }\item[{real, intent(in)}]{z0,  }\item[{real, intent(in)}]{T\+\_\+ref,  }\item[{real, intent(in)}]{S\+\_\+ref,  }\item[{real, intent(in)}]{d\+Rd\+T\+\_\+ref,  }\item[{real, intent(in)}]{d\+Rd\+S\+\_\+ref,  }\item[{real, intent(in)}]{d\+Rd\+T\+\_\+top,  }\item[{real, intent(in)}]{d\+Rd\+S\+\_\+top,  }\item[{real, intent(in)}]{d\+Rd\+T\+\_\+bot,  }\item[{real, intent(in)}]{d\+Rd\+S\+\_\+bot,  }\item[{real, dimension(\+:), intent(in)}]{ppoly\+\_\+T,  }\item[{real, dimension(\+:), intent(in)}]{ppoly\+\_\+S }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Search a layer to find where delta\+\_\+rho = 0 based on a linear interpolation of alpha and beta of the top and bottom being searched and polynomial reconstructions of T and S. Compressibility is not needed because either, we are assuming incompressibility in the equation of state for this module or alpha and beta are calculated having been displaced to the average pressures of the two pressures We need Newton\textquotesingle{}s method because the T and S reconstructions make delta\+\_\+rho a polynomial function of z if using P\+PM or higher. If Newton\textquotesingle{}s method would search fall out of the interval \mbox{[}0,1\mbox{]}, a bisection step would be taken instead. Also this linearization of alpha, beta means that second derivatives of the E\+OS are not needed. Note that delta in variable names below refers to horizontal differences and \textquotesingle{}d\textquotesingle{} refers to vertical differences. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em cs} & Control structure with parameters for this module\\
\hline
\mbox{\tt in}  & {\em z0} & Lower bound of position, also serves as the initial guess \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em t\+\_\+ref} & Temperature at the searched from interface \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s\+\_\+ref} & Salinity at the searched from interface \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em drdt\+\_\+ref} & d\+Rho/dT at the searched from interface \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em drds\+\_\+ref} & d\+Rho/dS at the searched from interface \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em drdt\+\_\+top} & d\+Rho/dT at top of layer being searched \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em drds\+\_\+top} & d\+Rho/dS at top of layer being searched \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em drdt\+\_\+bot} & d\+Rho/dT at bottom of layer being searched \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em drds\+\_\+bot} & d\+Rho/dS at bottom of layer being searched \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em ppoly\+\_\+t} & Coefficients of the polynomial reconstruction of T within the layer to be searched \mbox{[}degC\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em ppoly\+\_\+s} & Coefficients of the polynomial reconstruction of S within the layer to be searched \mbox{[}ppt\mbox{]}.\\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
Position where drho = 0 \mbox{[}nondim\mbox{]} 
\end{DoxyReturn}


Definition at line 1542 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
1542   \textcolor{keywordtype}{type}(neutral\_diffusion\_CS),\textcolor{keywordtype}{intent(in)} :: CS\textcolor{comment}{        !< Control structure with parameters for this module}
1543   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: z0\textcolor{comment}{        !< Lower bound of position, also serves as the}
1544 \textcolor{comment}{                                                     !! initial guess [nondim]}
1545   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: T\_ref\textcolor{comment}{     !< Temperature at the searched from interface [degC]}
1546   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: S\_ref\textcolor{comment}{     !< Salinity at the searched from interface [ppt]}
1547   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: dRdT\_ref\textcolor{comment}{  !< dRho/dT at the searched from interface}
1548 \textcolor{comment}{                                                     !! [R degC-1 ~> kg m-3 degC-1]}
1549   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: dRdS\_ref\textcolor{comment}{  !< dRho/dS at the searched from interface}
1550 \textcolor{comment}{                                                     !! [R ppt-1 ~> kg m-3 ppt-1]}
1551   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: dRdT\_top\textcolor{comment}{  !< dRho/dT at top of layer being searched}
1552 \textcolor{comment}{                                                     !! [R degC-1 ~> kg m-3 degC-1]}
1553   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: dRdS\_top\textcolor{comment}{  !< dRho/dS at top of layer being searched}
1554 \textcolor{comment}{                                                     !! [R ppt-1 ~> kg m-3 ppt-1]}
1555   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: dRdT\_bot\textcolor{comment}{  !< dRho/dT at bottom of layer being searched}
1556 \textcolor{comment}{                                                     !! [R degC-1 ~> kg m-3 degC-1]}
1557   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(in)} :: dRdS\_bot\textcolor{comment}{  !< dRho/dS at bottom of layer being searched}
1558 \textcolor{comment}{                                                     !! [R ppt-1 ~> kg m-3 ppt-1]}
1559   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)},        \textcolor{keywordtype}{intent(in)} :: ppoly\_T\textcolor{comment}{   !< Coefficients of the polynomial reconstruction of T
       within}
1560 \textcolor{comment}{                                                     !! the layer to be searched [degC].}
1561   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)},        \textcolor{keywordtype}{intent(in)} :: ppoly\_S\textcolor{comment}{   !< Coefficients of the polynomial reconstruction of S
       within}
1562 \textcolor{comment}{                                                     !! the layer to be searched [ppt].}
1563   \textcolor{keywordtype}{real}                                  :: z\textcolor{comment}{         !< Position where drho = 0 [nondim]}
1564   \textcolor{comment}{! Local variables}
1565   \textcolor{keywordtype}{real} :: dRdT\_diff  \textcolor{comment}{! Difference in the partial derivative of density with temperature across the}
1566                      \textcolor{comment}{! layer [R degC-1 ~> kg m-3 degC-1]}
1567   \textcolor{keywordtype}{real} :: dRdS\_diff  \textcolor{comment}{! Difference in the partial derivative of density with salinity across the}
1568                      \textcolor{comment}{! layer [R ppt-1 ~> kg m-3 ppt-1]}
1569   \textcolor{keywordtype}{real} :: drho, drho\_dz \textcolor{comment}{! Density anomaly and its derivative with fracitonal position [R ~> kg m-3]}
1570   \textcolor{keywordtype}{real} :: dRdT\_z     \textcolor{comment}{! Partial derivative of density with temperature at a point [R degC-1 ~> kg m-3
       degC-1]}
1571   \textcolor{keywordtype}{real} :: dRdS\_z     \textcolor{comment}{! Partial derivative of density with salinity at a point [R ppt-1 ~> kg m-3 ppt-1]}
1572   \textcolor{keywordtype}{real} :: T\_z, dT\_dz \textcolor{comment}{! Temperature at a point and its derivative with fractional position [degC]}
1573   \textcolor{keywordtype}{real} :: S\_z, dS\_dz \textcolor{comment}{! Salinity at a point and its derivative with fractional position [ppt]}
1574   \textcolor{keywordtype}{real} :: drho\_min, drho\_max \textcolor{comment}{! Bounds on density differences [R ~> kg m-3]}
1575   \textcolor{keywordtype}{real} :: ztest, zmin, zmax \textcolor{comment}{! Fractional positions in the cell [nondim]}
1576   \textcolor{keywordtype}{real} :: dz         \textcolor{comment}{! Change in position in the cell [nondim]}
1577   \textcolor{keywordtype}{real} :: a1, a2     \textcolor{comment}{! Fractional weights of the top and bottom values [nondim]}
1578   \textcolor{keywordtype}{integer} :: iter
1579   \textcolor{keywordtype}{integer} :: nterm
1580 
1581   nterm = \textcolor{keyword}{SIZE}(ppoly\_t)
1582 
1583   \textcolor{comment}{! Position independent quantities}
1584   drdt\_diff = drdt\_bot - drdt\_top
1585   drds\_diff = drds\_bot - drds\_top
1586   \textcolor{comment}{! Initial starting drho (used for bisection)}
1587   zmin = z0        \textcolor{comment}{! Lower bounding interval}
1588   zmax = 1.        \textcolor{comment}{! Maximum bounding interval (bottom of layer)}
1589   a1 = 1. - zmin
1590   a2 = zmin
1591   t\_z = evaluation\_polynomial( ppoly\_t, nterm, zmin )
1592   s\_z = evaluation\_polynomial( ppoly\_s, nterm, zmin )
1593   drdt\_z = a1*drdt\_top + a2*drdt\_bot
1594   drds\_z = a1*drds\_top + a2*drds\_bot
1595   drho\_min = 0.5*((drdt\_z+drdt\_ref)*(t\_z-t\_ref) + (drds\_z+drds\_ref)*(s\_z-s\_ref))
1596 
1597   t\_z = evaluation\_polynomial( ppoly\_t, nterm, 1. )
1598   s\_z = evaluation\_polynomial( ppoly\_s, nterm, 1. )
1599   drho\_max = 0.5*((drdt\_bot+drdt\_ref)*(t\_z-t\_ref) + (drds\_bot+drds\_ref)*(s\_z-s\_ref))
1600 
1601   \textcolor{keywordflow}{if} (drho\_min >= 0.) \textcolor{keywordflow}{then}
1602     z = z0
1603     \textcolor{keywordflow}{return}
1604   \textcolor{keywordflow}{elseif} (drho\_max == 0.) \textcolor{keywordflow}{then}
1605     z = 1.
1606     \textcolor{keywordflow}{return}
1607 \textcolor{keywordflow}{  endif}
1608   \textcolor{keywordflow}{if} ( sign(1.,drho\_min) == sign(1.,drho\_max) ) \textcolor{keywordflow}{then}
1609     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"drho\_min is the same sign as dhro\_max"})
1610 \textcolor{keywordflow}{  endif}
1611 
1612   z = z0
1613   ztest = z0
1614   \textcolor{keywordflow}{do} iter = 1, cs%max\_iter
1615     \textcolor{comment}{! Calculate quantities at the current nondimensional position}
1616     a1 = 1.-z
1617     a2 = z
1618     drdt\_z    = a1*drdt\_top + a2*drdt\_bot
1619     drds\_z    = a1*drds\_top + a2*drds\_bot
1620     t\_z       = evaluation\_polynomial( ppoly\_t, nterm, z )
1621     s\_z       = evaluation\_polynomial( ppoly\_s, nterm, z )
1622     drho = 0.5*((drdt\_z+drdt\_ref)*(t\_z-t\_ref) + (drds\_z+drds\_ref)*(s\_z-s\_ref))
1623 
1624     \textcolor{comment}{! Check for convergence}
1625     \textcolor{keywordflow}{if} (abs(drho) <= cs%drho\_tol) \textcolor{keywordflow}{exit}
1626     \textcolor{comment}{! Update bisection bracketing intervals}
1627     \textcolor{keywordflow}{if} (drho < 0. .and. drho > drho\_min) \textcolor{keywordflow}{then}
1628       drho\_min = drho
1629       zmin = z
1630     \textcolor{keywordflow}{elseif} (drho > 0. .and. drho < drho\_max) \textcolor{keywordflow}{then}
1631       drho\_max = drho
1632       zmax = z
1633 \textcolor{keywordflow}{    endif}
1634 
1635     \textcolor{comment}{! Calculate a Newton step}
1636     dt\_dz = first\_derivative\_polynomial( ppoly\_t, nterm, z )
1637     ds\_dz = first\_derivative\_polynomial( ppoly\_s, nterm, z )
1638     drho\_dz = 0.5*( (drdt\_diff*(t\_z - t\_ref) + (drdt\_ref+drdt\_z)*dt\_dz) + &
1639                     (drds\_diff*(s\_z - s\_ref) + (drds\_ref+drds\_z)*ds\_dz) )
1640 
1641     ztest = z - drho/drho\_dz
1642     \textcolor{comment}{! Take a bisection if z falls out of [zmin,zmax]}
1643     \textcolor{keywordflow}{if} (ztest < zmin .or. ztest > zmax) \textcolor{keywordflow}{then}
1644       \textcolor{keywordflow}{if} ( drho < 0. ) \textcolor{keywordflow}{then}
1645         ztest = 0.5*(z + zmax)
1646       \textcolor{keywordflow}{else}
1647         ztest = 0.5*(zmin + z)
1648 \textcolor{keywordflow}{      endif}
1649 \textcolor{keywordflow}{    endif}
1650 
1651     \textcolor{comment}{! Test to ensure we haven't stalled out}
1652     \textcolor{keywordflow}{if} ( abs(z-ztest) <= cs%x\_tol ) \textcolor{keywordflow}{exit}
1653     \textcolor{comment}{! Reset for next iteration}
1654     z = ztest
1655 \textcolor{keywordflow}{  enddo}
1656 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_acb07c024cb6aa162a8789a4f50139aa5}\label{namespacemom__neutral__diffusion_acb07c024cb6aa162a8789a4f50139aa5}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+continuous@{find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+continuous}}
\index{find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+continuous@{find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+continuous}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+continuous()}{find\_neutral\_surface\_positions\_continuous()}}
{\footnotesize\ttfamily subroutine mom\+\_\+neutral\+\_\+diffusion\+::find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+continuous (\begin{DoxyParamCaption}\item[{integer, intent(in)}]{nk,  }\item[{real, dimension(nk+1), intent(in)}]{Pl,  }\item[{real, dimension(nk+1), intent(in)}]{Tl,  }\item[{real, dimension(nk+1), intent(in)}]{Sl,  }\item[{real, dimension(nk+1), intent(in)}]{d\+Rd\+Tl,  }\item[{real, dimension(nk+1), intent(in)}]{d\+Rd\+Sl,  }\item[{real, dimension(nk+1), intent(in)}]{Pr,  }\item[{real, dimension(nk+1), intent(in)}]{Tr,  }\item[{real, dimension(nk+1), intent(in)}]{Sr,  }\item[{real, dimension(nk+1), intent(in)}]{d\+Rd\+Tr,  }\item[{real, dimension(nk+1), intent(in)}]{d\+Rd\+Sr,  }\item[{real, dimension(2$\ast$nk+2), intent(inout)}]{PoL,  }\item[{real, dimension(2$\ast$nk+2), intent(inout)}]{PoR,  }\item[{integer, dimension(2$\ast$nk+2), intent(inout)}]{KoL,  }\item[{integer, dimension(2$\ast$nk+2), intent(inout)}]{KoR,  }\item[{real, dimension(2$\ast$nk+1), intent(inout)}]{h\+Eff,  }\item[{integer, intent(in), optional}]{bl\+\_\+kl,  }\item[{integer, intent(in), optional}]{bl\+\_\+kr,  }\item[{real, intent(in), optional}]{bl\+\_\+zl,  }\item[{real, intent(in), optional}]{bl\+\_\+zr }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns positions within left/right columns of combined interfaces using continuous reconstructions of T/S. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em nk} & Number of levels\\
\hline
\mbox{\tt in}  & {\em pl} & Left-\/column interface pressure \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} or other units\\
\hline
\mbox{\tt in}  & {\em tl} & Left-\/column interface potential temperature \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em sl} & Left-\/column interface salinity \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em drdtl} & Left-\/column d\+Rho/dT \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em drdsl} & Left-\/column d\+Rho/dS \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em pr} & Right-\/column interface pressure \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} or other units\\
\hline
\mbox{\tt in}  & {\em tr} & Right-\/column interface potential temperature \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em sr} & Right-\/column interface salinity \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em drdtr} & Left-\/column d\+Rho/dT \mbox{[}R deg\+C-\/1 $\sim$$>$ kg m-\/3 deg\+C-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em drdsr} & Left-\/column d\+Rho/dS \mbox{[}R ppt-\/1 $\sim$$>$ kg m-\/3 ppt-\/1\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em pol} & Fractional position of neutral surface within layer KoL of left column\\
\hline
\mbox{\tt in,out}  & {\em por} & Fractional position of neutral surface within layer KoR of right column\\
\hline
\mbox{\tt in,out}  & {\em kol} & Index of first left interface above neutral surface\\
\hline
\mbox{\tt in,out}  & {\em kor} & Index of first right interface above neutral surface\\
\hline
\mbox{\tt in,out}  & {\em heff} & Effective thickness between two neutral surfaces \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} or other units following Pl and Pr.\\
\hline
\mbox{\tt in}  & {\em bl\+\_\+kl} & Layer index of the boundary layer (left)\\
\hline
\mbox{\tt in}  & {\em bl\+\_\+kr} & Layer index of the boundary layer (right)\\
\hline
\mbox{\tt in}  & {\em bl\+\_\+zl} & Nondimensional position of the boundary layer (left)\\
\hline
\mbox{\tt in}  & {\em bl\+\_\+zr} & Nondimensional position of the boundary layer (right) \\
\hline
\end{DoxyParams}


Definition at line 933 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
933   \textcolor{keywordtype}{integer},                    \textcolor{keywordtype}{intent(in)}    :: nk\textcolor{comment}{    !< Number of levels}
934   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)},      \textcolor{keywordtype}{intent(in)}    :: Pl\textcolor{comment}{    !< Left-column interface pressure [R L2 T-2 ~> Pa] or
       other units}
935   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)},      \textcolor{keywordtype}{intent(in)}    :: Tl\textcolor{comment}{    !< Left-column interface potential temperature [degC]}
936   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)},      \textcolor{keywordtype}{intent(in)}    :: Sl\textcolor{comment}{    !< Left-column interface salinity [ppt]}
937   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)},      \textcolor{keywordtype}{intent(in)}    :: dRdTl\textcolor{comment}{ !< Left-column dRho/dT [R degC-1 ~> kg m-3 degC-1]}
938   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)},      \textcolor{keywordtype}{intent(in)}    :: dRdSl\textcolor{comment}{ !< Left-column dRho/dS [R ppt-1 ~> kg m-3 ppt-1]}
939   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)},      \textcolor{keywordtype}{intent(in)}    :: Pr\textcolor{comment}{    !< Right-column interface pressure [R L2 T-2 ~> Pa] or
       other units}
940   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)},      \textcolor{keywordtype}{intent(in)}    :: Tr\textcolor{comment}{    !< Right-column interface potential temperature [degC]}
941   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)},      \textcolor{keywordtype}{intent(in)}    :: Sr\textcolor{comment}{    !< Right-column interface salinity [ppt]}
942   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)},      \textcolor{keywordtype}{intent(in)}    :: dRdTr\textcolor{comment}{ !< Left-column dRho/dT [R degC-1 ~> kg m-3 degC-1]}
943   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)},      \textcolor{keywordtype}{intent(in)}    :: dRdSr\textcolor{comment}{ !< Left-column dRho/dS [R ppt-1 ~> kg m-3 ppt-1]}
944   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(2*nk+2)},    \textcolor{keywordtype}{intent(inout)} :: PoL\textcolor{comment}{   !< Fractional position of neutral surface within}
945 \textcolor{comment}{                                                     !! layer KoL of left column}
946   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(2*nk+2)},    \textcolor{keywordtype}{intent(inout)} :: PoR\textcolor{comment}{   !< Fractional position of neutral surface within}
947 \textcolor{comment}{                                                     !! layer KoR of right column}
948   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2*nk+2)}, \textcolor{keywordtype}{intent(inout)} :: KoL\textcolor{comment}{   !< Index of first left interface above neutral surface}
949   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2*nk+2)}, \textcolor{keywordtype}{intent(inout)} :: KoR\textcolor{comment}{   !< Index of first right interface above neutral
       surface}
950   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(2*nk+1)},    \textcolor{keywordtype}{intent(inout)} :: hEff\textcolor{comment}{  !< Effective thickness between two neutral surfaces}
951 \textcolor{comment}{                                                     !! [R L2 T-2 ~> Pa] or other units following Pl and
       Pr.}
952   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{optional},          \textcolor{keywordtype}{intent(in)}    :: bl\_kl\textcolor{comment}{ !< Layer index of the boundary layer (left)}
953   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{optional},          \textcolor{keywordtype}{intent(in)}    :: bl\_kr\textcolor{comment}{ !< Layer index of the boundary layer (right)}
954   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional},             \textcolor{keywordtype}{intent(in)}    :: bl\_zl\textcolor{comment}{ !< Nondimensional position of the boundary layer
       (left)}
955   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional},             \textcolor{keywordtype}{intent(in)}    :: bl\_zr\textcolor{comment}{ !< Nondimensional position of the boundary layer
       (right)}
956 
957   \textcolor{comment}{! Local variables}
958   \textcolor{keywordtype}{integer} :: ns                     \textcolor{comment}{! Number of neutral surfaces}
959   \textcolor{keywordtype}{integer} :: k\_surface              \textcolor{comment}{! Index of neutral surface}
960   \textcolor{keywordtype}{integer} :: kl                     \textcolor{comment}{! Index of left interface}
961   \textcolor{keywordtype}{integer} :: kr                     \textcolor{comment}{! Index of right interface}
962   \textcolor{keywordtype}{real}    :: dRdT, dRdS             \textcolor{comment}{! dRho/dT [kg m-3 degC-1] and dRho/dS [kg m-3 ppt-1] for the neutral
       surface}
963   \textcolor{keywordtype}{logical} :: searching\_left\_column  \textcolor{comment}{! True if searching for the position of a right interface in the left
       column}
964   \textcolor{keywordtype}{logical} :: searching\_right\_column \textcolor{comment}{! True if searching for the position of a left interface in the right
       column}
965   \textcolor{keywordtype}{logical} :: reached\_bottom         \textcolor{comment}{! True if one of the bottom-most interfaces has been used as the target}
966   \textcolor{keywordtype}{integer} :: krm1, klm1
967   \textcolor{keywordtype}{real}    :: dRho, dRhoTop, dRhoBot \textcolor{comment}{! Potential density differences at various points [R ~> kg m-3]}
968   \textcolor{keywordtype}{real}    :: hL, hR                 \textcolor{comment}{! Pressure thicknesses [R L2 T-2 ~> Pa]}
969   \textcolor{keywordtype}{integer} :: lastK\_left, lastK\_right \textcolor{comment}{! Layers used during the last iteration}
970   \textcolor{keywordtype}{real}    :: lastP\_left, lastP\_right \textcolor{comment}{! Fractional positions during the last iteration [nondim]}
971   \textcolor{keywordtype}{logical} :: interior\_limit
972 
973   ns = 2*nk+2
974 
975   \textcolor{comment}{! Initialize variables for the search}
976   kr = 1 ;
977   kl = 1 ;
978   lastp\_right = 0.
979   lastp\_left = 0.
980   lastk\_right = 1
981   lastk\_left  = 1
982   reached\_bottom = .false.
983 
984   \textcolor{comment}{! Check to see if we should limit the diffusion to the interior}
985   interior\_limit = \textcolor{keyword}{PRESENT}(bl\_kl) .and. \textcolor{keyword}{PRESENT}(bl\_kr) .and. \textcolor{keyword}{PRESENT}(bl\_zr) .and. \textcolor{keyword}{PRESENT}(bl\_zl)
986 
987   \textcolor{comment}{! Loop over each neutral surface, working from top to bottom}
988   neutral\_surfaces: \textcolor{keywordflow}{do} k\_surface = 1, ns
989     klm1 = max(kl-1, 1)
990     \textcolor{keywordflow}{if} (klm1>nk) stop \textcolor{stringliteral}{'find\_neutral\_surface\_positions(): klm1 went out of bounds!'}
991     krm1 = max(kr-1, 1)
992     \textcolor{keywordflow}{if} (krm1>nk) stop \textcolor{stringliteral}{'find\_neutral\_surface\_positions(): krm1 went out of bounds!'}
993 
994     \textcolor{comment}{! Potential density difference, rho(kr) - rho(kl)}
995     drho = 0.5 * ( ( drdtr(kr) + drdtl(kl) ) * ( tr(kr) - tl(kl) ) &
996                  + ( drdsr(kr) + drdsl(kl) ) * ( sr(kr) - sl(kl) ) )
997     \textcolor{comment}{! Which column has the lighter surface for the current indexes, kr and kl}
998     \textcolor{keywordflow}{if} (.not. reached\_bottom) \textcolor{keywordflow}{then}
999       \textcolor{keywordflow}{if} (drho < 0.) \textcolor{keywordflow}{then}
1000         searching\_left\_column = .true.
1001         searching\_right\_column = .false.
1002       \textcolor{keywordflow}{elseif} (drho > 0.) \textcolor{keywordflow}{then}
1003         searching\_right\_column = .true.
1004         searching\_left\_column = .false.
1005       \textcolor{keywordflow}{else} \textcolor{comment}{! dRho == 0.}
1006         \textcolor{keywordflow}{if} (kl + kr == 2) \textcolor{keywordflow}{then} \textcolor{comment}{! Still at surface}
1007           searching\_left\_column = .true.
1008           searching\_right\_column = .false.
1009         \textcolor{keywordflow}{else} \textcolor{comment}{! Not the surface so we simply change direction}
1010           searching\_left\_column = .not.  searching\_left\_column
1011           searching\_right\_column = .not.  searching\_right\_column
1012 \textcolor{keywordflow}{        endif}
1013 \textcolor{keywordflow}{      endif}
1014 \textcolor{keywordflow}{    endif}
1015 
1016     \textcolor{keywordflow}{if} (searching\_left\_column) \textcolor{keywordflow}{then}
1017       \textcolor{comment}{! Interpolate for the neutral surface position within the left column, layer klm1}
1018       \textcolor{comment}{! Potential density difference, rho(kl-1) - rho(kr) (should be negative)}
1019       drhotop = 0.5 * ( ( drdtl(klm1) + drdtr(kr) ) * ( tl(klm1) - tr(kr) ) &
1020                      + ( drdsl(klm1) + drdsr(kr) ) * ( sl(klm1) - sr(kr) ) )
1021       \textcolor{comment}{! Potential density difference, rho(kl) - rho(kr) (will be positive)}
1022       drhobot = 0.5 * ( ( drdtl(klm1+1) + drdtr(kr) ) * ( tl(klm1+1) - tr(kr) ) &
1023                       + ( drdsl(klm1+1) + drdsr(kr) ) * ( sl(klm1+1) - sr(kr) ) )
1024 
1025       \textcolor{comment}{! Because we are looking left, the right surface, kr, is lighter than klm1+1 and should be denser
       than klm1}
1026       \textcolor{comment}{! unless we are still at the top of the left column (kl=1)}
1027       \textcolor{keywordflow}{if} (drhotop > 0. .or. kr+kl==2) \textcolor{keywordflow}{then}
1028         pol(k\_surface) = 0. \textcolor{comment}{! The right surface is lighter than anything in layer klm1}
1029       \textcolor{keywordflow}{elseif} (drhotop >= drhobot) \textcolor{keywordflow}{then} \textcolor{comment}{! Left layer is unstratified}
1030         pol(k\_surface) = 1.
1031       \textcolor{keywordflow}{else}
1032         \textcolor{comment}{! Linearly interpolate for the position between Pl(kl-1) and Pl(kl) where the density difference}
1033         \textcolor{comment}{! between right and left is zero. The Pl here are only used to handle massless layers.}
1034         pol(k\_surface) = interpolate\_for\_nondim\_position( drhotop, pl(klm1), drhobot, pl(klm1+1) )
1035 \textcolor{keywordflow}{      endif}
1036       \textcolor{keywordflow}{if} (pol(k\_surface)>=1. .and. klm1<nk) \textcolor{keywordflow}{then} \textcolor{comment}{! >= is really ==, when PoL==1 we point to the bottom of
       the cell}
1037         klm1 = klm1 + 1
1038         pol(k\_surface) = pol(k\_surface) - 1.
1039 \textcolor{keywordflow}{      endif}
1040       \textcolor{keywordflow}{if} (\textcolor{keywordtype}{real}(klm1-lastK\_left)+(PoL(k\_surface)-lastP\_left)<0.) then
1041         pol(k\_surface) = lastp\_left
1042         klm1 = lastk\_left
1043 \textcolor{keywordflow}{      endif}
1044       kol(k\_surface) = klm1
1045       \textcolor{keywordflow}{if} (kr <= nk) \textcolor{keywordflow}{then}
1046         por(k\_surface) = 0.
1047         kor(k\_surface) = kr
1048       \textcolor{keywordflow}{else}
1049         por(k\_surface) = 1.
1050         kor(k\_surface) = nk
1051 \textcolor{keywordflow}{      endif}
1052       \textcolor{keywordflow}{if} (kr <= nk) \textcolor{keywordflow}{then}
1053         kr = kr + 1
1054       \textcolor{keywordflow}{else}
1055         reached\_bottom = .true.
1056         searching\_right\_column = .true.
1057         searching\_left\_column = .false.
1058 \textcolor{keywordflow}{      endif}
1059     \textcolor{keywordflow}{elseif} (searching\_right\_column) \textcolor{keywordflow}{then}
1060       \textcolor{comment}{! Interpolate for the neutral surface position within the right column, layer krm1}
1061       \textcolor{comment}{! Potential density difference, rho(kr-1) - rho(kl) (should be negative)}
1062       drhotop = 0.5 * ( ( drdtr(krm1) + drdtl(kl) ) * ( tr(krm1) - tl(kl) ) + &
1063                         ( drdsr(krm1) + drdsl(kl) ) * ( sr(krm1) - sl(kl) ) )
1064       \textcolor{comment}{! Potential density difference, rho(kr) - rho(kl) (will be positive)}
1065       drhobot = 0.5 * ( ( drdtr(krm1+1) + drdtl(kl) ) * ( tr(krm1+1) - tl(kl) ) + &
1066                         ( drdsr(krm1+1) + drdsl(kl) ) * ( sr(krm1+1) - sl(kl) ) )
1067 
1068       \textcolor{comment}{! Because we are looking right, the left surface, kl, is lighter than krm1+1 and should be denser
       than krm1}
1069       \textcolor{comment}{! unless we are still at the top of the right column (kr=1)}
1070       \textcolor{keywordflow}{if} (drhotop >= 0. .or. kr+kl==2) \textcolor{keywordflow}{then}
1071         por(k\_surface) = 0. \textcolor{comment}{! The left surface is lighter than anything in layer krm1}
1072       \textcolor{keywordflow}{elseif} (drhotop >= drhobot) \textcolor{keywordflow}{then} \textcolor{comment}{! Right layer is unstratified}
1073         por(k\_surface) = 1.
1074       \textcolor{keywordflow}{else}
1075         \textcolor{comment}{! Linearly interpolate for the position between Pr(kr-1) and Pr(kr) where the density difference}
1076         \textcolor{comment}{! between right and left is zero. The Pr here are only used to handle massless layers.}
1077         por(k\_surface) = interpolate\_for\_nondim\_position( drhotop, pr(krm1), drhobot, pr(krm1+1) )
1078 \textcolor{keywordflow}{      endif}
1079       \textcolor{keywordflow}{if} (por(k\_surface)>=1. .and. krm1<nk) \textcolor{keywordflow}{then} \textcolor{comment}{! >= is really ==, when PoR==1 we point to the bottom of
       the cell}
1080         krm1 = krm1 + 1
1081         por(k\_surface) = por(k\_surface) - 1.
1082 \textcolor{keywordflow}{      endif}
1083       \textcolor{keywordflow}{if} (\textcolor{keywordtype}{real}(krm1-lastK\_right)+(PoR(k\_surface)-lastP\_right)<0.) then
1084         por(k\_surface) = lastp\_right
1085         krm1 = lastk\_right
1086 \textcolor{keywordflow}{      endif}
1087       kor(k\_surface) = krm1
1088       \textcolor{keywordflow}{if} (kl <= nk) \textcolor{keywordflow}{then}
1089         pol(k\_surface) = 0.
1090         kol(k\_surface) = kl
1091       \textcolor{keywordflow}{else}
1092         pol(k\_surface) = 1.
1093         kol(k\_surface) = nk
1094 \textcolor{keywordflow}{      endif}
1095       \textcolor{keywordflow}{if} (kl <= nk) \textcolor{keywordflow}{then}
1096         kl = kl + 1
1097       \textcolor{keywordflow}{else}
1098         reached\_bottom = .true.
1099         searching\_right\_column = .false.
1100         searching\_left\_column = .true.
1101 \textcolor{keywordflow}{      endif}
1102     \textcolor{keywordflow}{else}
1103       stop \textcolor{stringliteral}{'Else what?'}
1104 \textcolor{keywordflow}{    endif}
1105     \textcolor{keywordflow}{if} (interior\_limit) \textcolor{keywordflow}{then}
1106       \textcolor{keywordflow}{if} (kol(k\_surface)<=bl\_kl) \textcolor{keywordflow}{then}
1107         kol(k\_surface) = bl\_kl
1108         \textcolor{keywordflow}{if} (pol(k\_surface)<bl\_zl) \textcolor{keywordflow}{then}
1109           pol(k\_surface) = bl\_zl
1110 \textcolor{keywordflow}{        endif}
1111 \textcolor{keywordflow}{      endif}
1112       \textcolor{keywordflow}{if} (kor(k\_surface)<=bl\_kr) \textcolor{keywordflow}{then}
1113         kor(k\_surface) = bl\_kr
1114         \textcolor{keywordflow}{if} (por(k\_surface)<bl\_zr) \textcolor{keywordflow}{then}
1115           por(k\_surface) = bl\_zr
1116 \textcolor{keywordflow}{        endif}
1117 \textcolor{keywordflow}{      endif}
1118 \textcolor{keywordflow}{    endif}
1119 
1120     lastk\_left = kol(k\_surface) ; lastp\_left = pol(k\_surface)
1121     lastk\_right = kor(k\_surface) ; lastp\_right = por(k\_surface)
1122     \textcolor{comment}{! Effective thickness}
1123     \textcolor{comment}{! NOTE: This would be better expressed in terms of the layers thicknesses rather}
1124     \textcolor{comment}{! than as differences of position - AJA}
1125     \textcolor{keywordflow}{if} (k\_surface>1) \textcolor{keywordflow}{then}
1126       hl = absolute\_position(nk,ns,pl,kol,pol,k\_surface) - absolute\_position(nk,ns,pl,kol,pol,k\_surface-1)
1127       hr = absolute\_position(nk,ns,pr,kor,por,k\_surface) - absolute\_position(nk,ns,pr,kor,por,k\_surface-1)
1128       \textcolor{keywordflow}{if} ( hl + hr > 0.) \textcolor{keywordflow}{then}
1129         heff(k\_surface-1) = 2. * hl * hr / ( hl + hr ) \textcolor{comment}{! Harmonic mean of layer thicknesses}
1130       \textcolor{keywordflow}{else}
1131         heff(k\_surface-1) = 0.
1132 \textcolor{keywordflow}{      endif}
1133 \textcolor{keywordflow}{    endif}
1134 
1135 \textcolor{keywordflow}{  enddo} neutral\_surfaces
1136 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a72eca09bc6cc25f8a3831ad8e2be91d4}\label{namespacemom__neutral__diffusion_a72eca09bc6cc25f8a3831ad8e2be91d4}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+discontinuous@{find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+discontinuous}}
\index{find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+discontinuous@{find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+discontinuous}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+discontinuous()}{find\_neutral\_surface\_positions\_discontinuous()}}
{\footnotesize\ttfamily subroutine mom\+\_\+neutral\+\_\+diffusion\+::find\+\_\+neutral\+\_\+surface\+\_\+positions\+\_\+discontinuous (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__neutral__diffusion_1_1neutral__diffusion__cs}{neutral\+\_\+diffusion\+\_\+cs}}), intent(inout)}]{CS,  }\item[{integer, intent(in)}]{nk,  }\item[{real, dimension(nk,2), intent(in)}]{Pres\+\_\+l,  }\item[{real, dimension(nk), intent(in)}]{hcol\+\_\+l,  }\item[{real, dimension(nk,2), intent(in)}]{Tl,  }\item[{real, dimension(nk,2), intent(in)}]{Sl,  }\item[{real, dimension(\+:,\+:), intent(in)}]{ppoly\+\_\+\+T\+\_\+l,  }\item[{real, dimension(\+:,\+:), intent(in)}]{ppoly\+\_\+\+S\+\_\+l,  }\item[{logical, dimension(nk), intent(in)}]{stable\+\_\+l,  }\item[{real, dimension(nk,2), intent(in)}]{Pres\+\_\+r,  }\item[{real, dimension(nk), intent(in)}]{hcol\+\_\+r,  }\item[{real, dimension(nk,2), intent(in)}]{Tr,  }\item[{real, dimension(nk,2), intent(in)}]{Sr,  }\item[{real, dimension(\+:,\+:), intent(in)}]{ppoly\+\_\+\+T\+\_\+r,  }\item[{real, dimension(\+:,\+:), intent(in)}]{ppoly\+\_\+\+S\+\_\+r,  }\item[{logical, dimension(nk), intent(in)}]{stable\+\_\+r,  }\item[{real, dimension(4$\ast$nk), intent(inout)}]{PoL,  }\item[{real, dimension(4$\ast$nk), intent(inout)}]{PoR,  }\item[{integer, dimension(4$\ast$nk), intent(inout)}]{KoL,  }\item[{integer, dimension(4$\ast$nk), intent(inout)}]{KoR,  }\item[{real, dimension(4$\ast$nk-\/1), intent(inout)}]{h\+Eff,  }\item[{real, intent(in), optional}]{zeta\+\_\+bot\+\_\+L,  }\item[{real, intent(in), optional}]{zeta\+\_\+bot\+\_\+R,  }\item[{integer, intent(in), optional}]{k\+\_\+bot\+\_\+L,  }\item[{integer, intent(in), optional}]{k\+\_\+bot\+\_\+R,  }\item[{logical, intent(in), optional}]{hard\+\_\+fail\+\_\+heff }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Higher order version of find\+\_\+neutral\+\_\+surface\+\_\+positions. Returns positions within left/right columns of combined interfaces using intracell reconstructions of T/S. Note that the polynomial reconstrcutions of T and S are optional to aid with unit testing, but will always be passed otherwise. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in,out}  & {\em cs} & Neutral diffusion control structure\\
\hline
\mbox{\tt in}  & {\em nk} & Number of levels\\
\hline
\mbox{\tt in}  & {\em pres\+\_\+l} & Left-\/column interface pressure \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt in}  & {\em hcol\+\_\+l} & Left-\/column layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} or other units\\
\hline
\mbox{\tt in}  & {\em tl} & Left-\/column top interface potential temperature \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em sl} & Left-\/column top interface salinity \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em ppoly\+\_\+t\+\_\+l} & Left-\/column coefficients of T reconstruction \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em ppoly\+\_\+s\+\_\+l} & Left-\/column coefficients of S reconstruction \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em stable\+\_\+l} & True where the left-\/column is stable\\
\hline
\mbox{\tt in}  & {\em pres\+\_\+r} & Right-\/column interface pressure \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt in}  & {\em hcol\+\_\+r} & Left-\/column layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} or other units\\
\hline
\mbox{\tt in}  & {\em tr} & Right-\/column top interface potential temperature \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em sr} & Right-\/column top interface salinity \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em ppoly\+\_\+t\+\_\+r} & Right-\/column coefficients of T reconstruction \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em ppoly\+\_\+s\+\_\+r} & Right-\/column coefficients of S reconstruction \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em stable\+\_\+r} & True where the right-\/column is stable\\
\hline
\mbox{\tt in,out}  & {\em pol} & Fractional position of neutral surface within layer KoL of left column \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em por} & Fractional position of neutral surface within layer KoR of right column \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em kol} & Index of first left interface above neutral surface\\
\hline
\mbox{\tt in,out}  & {\em kor} & Index of first right interface above neutral surface\\
\hline
\mbox{\tt in,out}  & {\em heff} & Effective thickness between two neutral surfaces \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} or other units taken from hcol\+\_\+l\\
\hline
\mbox{\tt in}  & {\em zeta\+\_\+bot\+\_\+l} & Non-\/dimensional distance to where the boundary layer intersetcs the cell (left) \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em zeta\+\_\+bot\+\_\+r} & Non-\/dimensional distance to where the boundary layer intersetcs the cell (right) \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em k\+\_\+bot\+\_\+l} & k-\/index for the boundary layer (left) \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em k\+\_\+bot\+\_\+r} & k-\/index for the boundary layer (right) \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em hard\+\_\+fail\+\_\+heff} & If true (default) bring down the model if the neutral surfaces ever cross \mbox{[}logical\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 1187 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
1187 
1188   \textcolor{keywordtype}{type}(neutral\_diffusion\_CS),     \textcolor{keywordtype}{intent(inout)} :: CS\textcolor{comment}{        !< Neutral diffusion control structure}
1189   \textcolor{keywordtype}{integer},                        \textcolor{keywordtype}{intent(in)}    :: nk\textcolor{comment}{        !< Number of levels}
1190   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)},          \textcolor{keywordtype}{intent(in)}    :: Pres\_l\textcolor{comment}{    !< Left-column interface pressure [R L2 T-2 ~>
       Pa]}
1191   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},            \textcolor{keywordtype}{intent(in)}    :: hcol\_l\textcolor{comment}{    !< Left-column layer thicknesses [H ~> m or kg
       m-2]}
1192 \textcolor{comment}{                                                             !! or other units}
1193   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)},          \textcolor{keywordtype}{intent(in)}    :: Tl\textcolor{comment}{        !< Left-column top interface potential
       temperature [degC]}
1194   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)},          \textcolor{keywordtype}{intent(in)}    :: Sl\textcolor{comment}{        !< Left-column top interface salinity [ppt]}
1195   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:,:)},           \textcolor{keywordtype}{intent(in)}    :: ppoly\_T\_l\textcolor{comment}{ !< Left-column coefficients of T
       reconstruction [degC]}
1196   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:,:)},           \textcolor{keywordtype}{intent(in)}    :: ppoly\_S\_l\textcolor{comment}{ !< Left-column coefficients of S
       reconstruction [ppt]}
1197   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(nk)},         \textcolor{keywordtype}{intent(in)}    :: stable\_l\textcolor{comment}{  !< True where the left-column is stable}
1198   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)},          \textcolor{keywordtype}{intent(in)}    :: Pres\_r\textcolor{comment}{    !< Right-column interface pressure [R L2 T-2
       ~> Pa]}
1199   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},            \textcolor{keywordtype}{intent(in)}    :: hcol\_r\textcolor{comment}{    !< Left-column layer thicknesses [H ~> m or kg
       m-2]}
1200 \textcolor{comment}{                                                             !! or other units}
1201   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)},          \textcolor{keywordtype}{intent(in)}    :: Tr\textcolor{comment}{        !< Right-column top interface potential
       temperature [degC]}
1202   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)},          \textcolor{keywordtype}{intent(in)}    :: Sr\textcolor{comment}{        !< Right-column top interface salinity [ppt]}
1203   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:,:)},           \textcolor{keywordtype}{intent(in)}    :: ppoly\_T\_r\textcolor{comment}{ !< Right-column coefficients of T
       reconstruction [degC]}
1204   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:,:)},           \textcolor{keywordtype}{intent(in)}    :: ppoly\_S\_r\textcolor{comment}{ !< Right-column coefficients of S
       reconstruction [ppt]}
1205   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(nk)},         \textcolor{keywordtype}{intent(in)}    :: stable\_r\textcolor{comment}{  !< True where the right-column is stable}
1206   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(4*nk)},          \textcolor{keywordtype}{intent(inout)} :: PoL\textcolor{comment}{       !< Fractional position of neutral surface
       within}
1207 \textcolor{comment}{                                                             !! layer KoL of left column [nondim]}
1208   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(4*nk)},          \textcolor{keywordtype}{intent(inout)} :: PoR\textcolor{comment}{       !< Fractional position of neutral surface
       within}
1209 \textcolor{comment}{                                                             !! layer KoR of right column [nondim]}
1210   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(4*nk)},       \textcolor{keywordtype}{intent(inout)} :: KoL\textcolor{comment}{       !< Index of first left interface above neutral
       surface}
1211   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(4*nk)},       \textcolor{keywordtype}{intent(inout)} :: KoR\textcolor{comment}{       !< Index of first right interface above
       neutral surface}
1212   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(4*nk-1)},        \textcolor{keywordtype}{intent(inout)} :: hEff\textcolor{comment}{      !< Effective thickness between two neutral
       surfaces}
1213 \textcolor{comment}{                                                             !! [H ~> m or kg m-2] or other units taken
       from hcol\_l}
1214   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional},                 \textcolor{keywordtype}{intent(in)}    :: zeta\_bot\_L\textcolor{comment}{!< Non-dimensional distance to where the
       boundary layer}
1215 \textcolor{comment}{                                                             !! intersetcs the cell (left) [nondim]}
1216   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{optional},                 \textcolor{keywordtype}{intent(in)}    :: zeta\_bot\_R\textcolor{comment}{!< Non-dimensional distance to where the
       boundary layer}
1217 \textcolor{comment}{                                                             !! intersetcs the cell (right) [nondim]}
1218 
1219   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{optional},              \textcolor{keywordtype}{intent(in)}    :: k\_bot\_L\textcolor{comment}{   !< k-index for the boundary layer (left)
       [nondim]}
1220   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{optional},              \textcolor{keywordtype}{intent(in)}    :: k\_bot\_R\textcolor{comment}{   !< k-index for the boundary layer (right)
       [nondim]}
1221   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{optional},              \textcolor{keywordtype}{intent(in)}    :: hard\_fail\_heff\textcolor{comment}{ !< If true (default) bring down the model
       if the}
1222 \textcolor{comment}{                                                             !! neutral surfaces ever cross [logical]}
1223   \textcolor{comment}{! Local variables}
1224   \textcolor{keywordtype}{integer} :: ns                     \textcolor{comment}{! Number of neutral surfaces}
1225   \textcolor{keywordtype}{integer} :: k\_surface              \textcolor{comment}{! Index of neutral surface}
1226   \textcolor{keywordtype}{integer} :: kl\_left, kl\_right      \textcolor{comment}{! Index of layers on the left/right}
1227   \textcolor{keywordtype}{integer} :: ki\_left, ki\_right      \textcolor{comment}{! Index of interfaces on the left/right}
1228   \textcolor{keywordtype}{logical} :: searching\_left\_column  \textcolor{comment}{! True if searching for the position of a right interface in the left
       column}
1229   \textcolor{keywordtype}{logical} :: searching\_right\_column \textcolor{comment}{! True if searching for the position of a left interface in the right
       column}
1230   \textcolor{keywordtype}{logical} :: reached\_bottom         \textcolor{comment}{! True if one of the bottom-most interfaces has been used as the target}
1231   \textcolor{keywordtype}{logical} :: search\_layer
1232   \textcolor{keywordtype}{logical} :: fail\_heff              \textcolor{comment}{! Fail if negative thickness are encountered.  By default this}
1233                                     \textcolor{comment}{! is true, but it can take its value from hard\_fail\_heff.}
1234   \textcolor{keywordtype}{real}    :: dRho                   \textcolor{comment}{! A density difference between columns [R ~> kg m-3]}
1235   \textcolor{keywordtype}{real}    :: hL, hR                 \textcolor{comment}{! Left and right layer thicknesses [H ~> m or kg m-2] or units from
       hcol\_l}
1236   \textcolor{keywordtype}{real}    :: lastP\_left, lastP\_right \textcolor{comment}{! Previous positions for left and right [nondim]}
1237   \textcolor{keywordtype}{integer} :: k\_init\_L, k\_init\_R      \textcolor{comment}{! Starting indices layers for left and right}
1238   \textcolor{keywordtype}{real}    :: p\_init\_L, p\_init\_R      \textcolor{comment}{! Starting positions for left and right [nondim]}
1239   \textcolor{comment}{! Initialize variables for the search}
1240   ns = 4*nk
1241   ki\_right = 1
1242   ki\_left = 1
1243   kl\_left = 1
1244   kl\_right = 1
1245   lastp\_left = 0.
1246   lastp\_right = 0.
1247   reached\_bottom = .false.
1248   searching\_left\_column = .false.
1249   searching\_right\_column = .false.
1250 
1251   fail\_heff = .true.
1252   \textcolor{keywordflow}{if} (\textcolor{keyword}{PRESENT}(hard\_fail\_heff)) fail\_heff = hard\_fail\_heff
1253 
1254   \textcolor{keywordflow}{if} (\textcolor{keyword}{PRESENT}(k\_bot\_l) .and. \textcolor{keyword}{PRESENT}(k\_bot\_r) .and. \textcolor{keyword}{PRESENT}(zeta\_bot\_l) .and. \textcolor{keyword}{PRESENT}(zeta\_bot\_r)) \textcolor{keywordflow}{then}
1255     k\_init\_l = k\_bot\_l; k\_init\_r = k\_bot\_r
1256     p\_init\_l = zeta\_bot\_l; p\_init\_r = zeta\_bot\_r
1257     lastp\_left = zeta\_bot\_l; lastp\_right = zeta\_bot\_r
1258     kl\_left = k\_bot\_l; kl\_right = k\_bot\_r
1259   \textcolor{keywordflow}{else}
1260     k\_init\_l = 1  ; k\_init\_r = 1
1261     p\_init\_l = 0. ; p\_init\_r = 0.
1262 \textcolor{keywordflow}{  endif}
1263   \textcolor{comment}{! Loop over each neutral surface, working from top to bottom}
1264   neutral\_surfaces: \textcolor{keywordflow}{do} k\_surface = 1, ns
1265 
1266     \textcolor{keywordflow}{if} (k\_surface == ns) \textcolor{keywordflow}{then}
1267         pol(k\_surface) = 1.
1268         por(k\_surface) = 1.
1269         kol(k\_surface) = nk
1270         kor(k\_surface) = nk
1271     \textcolor{comment}{! If the layers are unstable, then simply point the surface to the previous location}
1272     \textcolor{keywordflow}{elseif} (.not. stable\_l(kl\_left)) \textcolor{keywordflow}{then}
1273       \textcolor{keywordflow}{if} (k\_surface > 1) \textcolor{keywordflow}{then}
1274         pol(k\_surface) = ki\_left - 1 \textcolor{comment}{! Top interface is at position = 0., Bottom is at position = 1}
1275         kol(k\_surface) = kl\_left
1276         por(k\_surface) = por(k\_surface-1)
1277         kor(k\_surface) = kor(k\_surface-1)
1278       \textcolor{keywordflow}{else}
1279         por(k\_surface) = p\_init\_r
1280         kor(k\_surface) = k\_init\_r
1281         pol(k\_surface) = p\_init\_l
1282         kol(k\_surface) = k\_init\_l
1283 \textcolor{keywordflow}{      endif}
1284       \textcolor{keyword}{call }increment\_interface(nk, kl\_left, ki\_left, reached\_bottom, searching\_left\_column, 
      searching\_right\_column)
1285       searching\_left\_column = .true.
1286       searching\_right\_column = .false.
1287     \textcolor{keywordflow}{elseif} (.not. stable\_r(kl\_right)) \textcolor{keywordflow}{then} \textcolor{comment}{! Check the right layer for stability}
1288       \textcolor{keywordflow}{if} (k\_surface > 1) \textcolor{keywordflow}{then}
1289         por(k\_surface) = ki\_right - 1 \textcolor{comment}{! Top interface is at position = 0., Bottom is at position = 1}
1290         kor(k\_surface) = kl\_right
1291         pol(k\_surface) = pol(k\_surface-1)
1292         kol(k\_surface) = kol(k\_surface-1)
1293       \textcolor{keywordflow}{else}
1294         por(k\_surface) = 0.
1295         kor(k\_surface) = 1
1296         pol(k\_surface) = 0.
1297         kol(k\_surface) = 1
1298 \textcolor{keywordflow}{      endif}
1299       \textcolor{keyword}{call }increment\_interface(nk, kl\_right, ki\_right, reached\_bottom, searching\_right\_column, 
      searching\_left\_column)
1300       searching\_left\_column = .false.
1301       searching\_right\_column = .true.
1302     \textcolor{keywordflow}{else} \textcolor{comment}{! Layers are stable so need to figure out whether we need to search right or left}
1303       \textcolor{comment}{! For convenience, the left column uses the searched "from" interface variables, and the right column}
1304       \textcolor{comment}{! uses the searched 'to'. These will get reset in subsequent calc\_delta\_rho calls}
1305 
1306       \textcolor{keyword}{call }calc\_delta\_rho\_and\_derivs(cs,                                                                   
           &
1307                                      tr(kl\_right, ki\_right), sr(kl\_right, ki\_right), pres\_r(kl\_right,
      ki\_right), &
1308                                      tl(kl\_left, ki\_left),   sl(kl\_left, ki\_left)  , pres\_l(kl\_left,ki\_left
      ),   &
1309                                      drho)
1310       \textcolor{keywordflow}{if} (cs%debug) \textcolor{keyword}{write}(stdout,\textcolor{stringliteral}{'(A,I2,A,E12.4,A,I2,A,I2,A,I2,A,I2)'}) &
1311           \textcolor{stringliteral}{"k\_surface="},k\_surface, \textcolor{stringliteral}{"  dRho="},cs%R\_to\_kg\_m3*drho, &
1312           \textcolor{stringliteral}{"kl\_left="},kl\_left, \textcolor{stringliteral}{"  ki\_left="},ki\_left, \textcolor{stringliteral}{"  kl\_right="},kl\_right, \textcolor{stringliteral}{"  ki\_right="},ki\_right
1313       \textcolor{comment}{! Which column has the lighter surface for the current indexes, kr and kl}
1314       \textcolor{keywordflow}{if} (.not. reached\_bottom) \textcolor{keywordflow}{then}
1315         \textcolor{keywordflow}{if} (drho < 0.) \textcolor{keywordflow}{then}
1316           searching\_left\_column  = .true.
1317           searching\_right\_column = .false.
1318         \textcolor{keywordflow}{elseif} (drho > 0.) \textcolor{keywordflow}{then}
1319           searching\_left\_column  = .false.
1320           searching\_right\_column = .true.
1321         \textcolor{keywordflow}{else} \textcolor{comment}{! dRho == 0.}
1322           \textcolor{keywordflow}{if} (  ( kl\_left + kl\_right == 2 ) .and. (ki\_left + ki\_right == 2) ) \textcolor{keywordflow}{then} \textcolor{comment}{! Still at surface}
1323             searching\_left\_column  = .true.
1324             searching\_right\_column = .false.
1325           \textcolor{keywordflow}{else} \textcolor{comment}{! Not the surface so we simply change direction}
1326             searching\_left\_column = .not. searching\_left\_column
1327             searching\_right\_column = .not. searching\_right\_column
1328 \textcolor{keywordflow}{          endif}
1329 \textcolor{keywordflow}{        endif}
1330 \textcolor{keywordflow}{      endif}
1331       \textcolor{keywordflow}{if} (searching\_left\_column) \textcolor{keywordflow}{then}
1332         \textcolor{comment}{! Position of the right interface is known and all quantities are fixed}
1333         por(k\_surface) = ki\_right - 1.
1334         kor(k\_surface) = kl\_right
1335         pol(k\_surface) = search\_other\_column(cs, k\_surface, lastp\_left,                                &
1336                            tr(kl\_right, ki\_right), sr(kl\_right, ki\_right), pres\_r(kl\_right, ki\_right), &
1337                            tl(kl\_left,1),          sl(kl\_left,1),          pres\_l(kl\_left,1),          &
1338                            tl(kl\_left,2),          sl(kl\_left,2),          pres\_l(kl\_left,2),          &
1339                            ppoly\_t\_l(kl\_left,:), ppoly\_s\_l(kl\_left,:))
1340         kol(k\_surface) = kl\_left
1341 
1342         \textcolor{keywordflow}{if} (cs%debug) \textcolor{keywordflow}{then}
1343           \textcolor{keyword}{write}(stdout,\textcolor{stringliteral}{'(A,I2)'}) \textcolor{stringliteral}{"Searching left layer "}, kl\_left
1344           \textcolor{keyword}{write}(stdout,\textcolor{stringliteral}{'(A,I2,X,I2)'}) \textcolor{stringliteral}{"Searching from right: "}, kl\_right, ki\_right
1345           \textcolor{keyword}{write}(stdout,*) \textcolor{stringliteral}{"Temp/Salt Reference: "}, tr(kl\_right,ki\_right), sr(kl\_right,ki\_right)
1346           \textcolor{keyword}{write}(stdout,*) \textcolor{stringliteral}{"Temp/Salt Top L: "}, tl(kl\_left,1), sl(kl\_left,1)
1347           \textcolor{keyword}{write}(stdout,*) \textcolor{stringliteral}{"Temp/Salt Bot L: "}, tl(kl\_left,2), sl(kl\_left,2)
1348 \textcolor{keywordflow}{        endif}
1349         \textcolor{keyword}{call }increment\_interface(nk, kl\_right, ki\_right, reached\_bottom, searching\_right\_column, 
      searching\_left\_column)
1350         lastp\_left = pol(k\_surface)
1351         \textcolor{comment}{! If the right layer increments, then we need to reset the last position on the right}
1352         \textcolor{keywordflow}{if} ( kl\_right == (kor(k\_surface) + 1) ) lastp\_right = 0.
1353 
1354       \textcolor{keywordflow}{elseif} (searching\_right\_column) \textcolor{keywordflow}{then}
1355         \textcolor{comment}{! Position of the right interface is known and all quantities are fixed}
1356         pol(k\_surface) = ki\_left - 1.
1357         kol(k\_surface) = kl\_left
1358         por(k\_surface) = search\_other\_column(cs, k\_surface, lastp\_right,                         &
1359                            tl(kl\_left, ki\_left), sl(kl\_left, ki\_left), pres\_l(kl\_left, ki\_left), &
1360                            tr(kl\_right,1),       sr(kl\_right,1),       pres\_r(kl\_right,1),       &
1361                            tr(kl\_right,2),       sr(kl\_right,2),       pres\_r(kl\_right,2),       &
1362                            ppoly\_t\_r(kl\_right,:), ppoly\_s\_r(kl\_right,:))
1363         kor(k\_surface) = kl\_right
1364 
1365         \textcolor{keywordflow}{if} (cs%debug) \textcolor{keywordflow}{then}
1366           \textcolor{keyword}{write}(stdout,\textcolor{stringliteral}{'(A,I2)'}) \textcolor{stringliteral}{"Searching right layer "}, kl\_right
1367           \textcolor{keyword}{write}(stdout,\textcolor{stringliteral}{'(A,I2,X,I2)'}) \textcolor{stringliteral}{"Searching from left: "}, kl\_left, ki\_left
1368           \textcolor{keyword}{write}(stdout,*) \textcolor{stringliteral}{"Temp/Salt Reference: "}, tl(kl\_left,ki\_left), sl(kl\_left,ki\_left)
1369           \textcolor{keyword}{write}(stdout,*) \textcolor{stringliteral}{"Temp/Salt Top L: "}, tr(kl\_right,1), sr(kl\_right,1)
1370           \textcolor{keyword}{write}(stdout,*) \textcolor{stringliteral}{"Temp/Salt Bot L: "}, tr(kl\_right,2), sr(kl\_right,2)
1371 \textcolor{keywordflow}{        endif}
1372         \textcolor{keyword}{call }increment\_interface(nk, kl\_left, ki\_left, reached\_bottom, searching\_left\_column, 
      searching\_right\_column)
1373         lastp\_right = por(k\_surface)
1374         \textcolor{comment}{! If the right layer increments, then we need to reset the last position on the right}
1375         \textcolor{keywordflow}{if} ( kl\_left == (kol(k\_surface) + 1) ) lastp\_left = 0.
1376       \textcolor{keywordflow}{else}
1377         stop \textcolor{stringliteral}{'Else what?'}
1378 \textcolor{keywordflow}{      endif}
1379       \textcolor{keywordflow}{if} (cs%debug)  \textcolor{keyword}{write}(stdout,\textcolor{stringliteral}{'(A,I3,A,ES16.6,A,I2,A,ES16.6)'}) \textcolor{stringliteral}{"KoL:"}, kol(k\_surface), \textcolor{stringliteral}{" PoL:"}, pol(
      k\_surface), &
1380                      \textcolor{stringliteral}{"     KoR:"}, kor(k\_surface), \textcolor{stringliteral}{" PoR:"}, por(k\_surface)
1381 \textcolor{keywordflow}{    endif}
1382     \textcolor{comment}{! Effective thickness}
1383     \textcolor{keywordflow}{if} (k\_surface>1) \textcolor{keywordflow}{then}
1384       \textcolor{keywordflow}{if} ( kol(k\_surface) == kol(k\_surface-1) .and. kor(k\_surface) == kor(k\_surface-1) ) \textcolor{keywordflow}{then}
1385         hl = (pol(k\_surface) - pol(k\_surface-1))*hcol\_l(kol(k\_surface))
1386         hr = (por(k\_surface) - por(k\_surface-1))*hcol\_r(kor(k\_surface))
1387         \textcolor{keywordflow}{if} (hl < 0. .or. hr < 0.) \textcolor{keywordflow}{then}
1388           \textcolor{keywordflow}{if} (fail\_heff) \textcolor{keywordflow}{then}
1389             \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"Negative thicknesses in neutral diffusion"})
1390           \textcolor{keywordflow}{else}
1391             \textcolor{keywordflow}{if} (searching\_left\_column) \textcolor{keywordflow}{then}
1392               pol(k\_surface) = pol(k\_surface-1)
1393               kol(k\_surface) = kol(k\_surface-1)
1394             \textcolor{keywordflow}{elseif} (searching\_right\_column) \textcolor{keywordflow}{then}
1395               por(k\_surface) = por(k\_surface-1)
1396               kor(k\_surface) = kor(k\_surface-1)
1397 \textcolor{keywordflow}{            endif}
1398 \textcolor{keywordflow}{          endif}
1399         \textcolor{keywordflow}{elseif} ( hl + hr == 0. ) \textcolor{keywordflow}{then}
1400           heff(k\_surface-1) = 0.
1401         \textcolor{keywordflow}{else}
1402           heff(k\_surface-1) = 2. * ( (hl * hr) / ( hl + hr ) )\textcolor{comment}{! Harmonic mean}
1403           \textcolor{keywordflow}{if} ( kol(k\_surface) /= kol(k\_surface-1) ) \textcolor{keywordflow}{then}
1404             \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"Neutral sublayer spans multiple layers"})
1405 \textcolor{keywordflow}{          endif}
1406           \textcolor{keywordflow}{if} ( kor(k\_surface) /= kor(k\_surface-1) ) \textcolor{keywordflow}{then}
1407             \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"Neutral sublayer spans multiple layers"})
1408 \textcolor{keywordflow}{          endif}
1409 \textcolor{keywordflow}{        endif}
1410       \textcolor{keywordflow}{else}
1411         heff(k\_surface-1) = 0.
1412 \textcolor{keywordflow}{      endif}
1413 \textcolor{keywordflow}{    endif}
1414 \textcolor{keywordflow}{  enddo} neutral\_surfaces
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a1c5ac28d7c54581cdd872fe58ef0b204}\label{namespacemom__neutral__diffusion_a1c5ac28d7c54581cdd872fe58ef0b204}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!fv\+\_\+diff@{fv\+\_\+diff}}
\index{fv\+\_\+diff@{fv\+\_\+diff}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{fv\+\_\+diff()}{fv\_diff()}}
{\footnotesize\ttfamily real function mom\+\_\+neutral\+\_\+diffusion\+::fv\+\_\+diff (\begin{DoxyParamCaption}\item[{real, intent(in)}]{hkm1,  }\item[{real, intent(in)}]{hk,  }\item[{real, intent(in)}]{hkp1,  }\item[{real, intent(in)}]{Skm1,  }\item[{real, intent(in)}]{Sk,  }\item[{real, intent(in)}]{Skp1 }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns the cell-\/centered second-\/order finite volume (unlimited P\+LM) slope using three consecutive cell widths and average values. Slope is returned as a difference across the central cell (i.\+e. units of scalar S). Discretization follows equation 1.\+7 in Colella \& Woodward, 1984\+: J\+CP 54, 174-\/201. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em hkm1} & Left cell width\\
\hline
\mbox{\tt in}  & {\em hk} & Center cell width\\
\hline
\mbox{\tt in}  & {\em hkp1} & Right cell width\\
\hline
\mbox{\tt in}  & {\em skm1} & Left cell average value\\
\hline
\mbox{\tt in}  & {\em sk} & Center cell average value\\
\hline
\mbox{\tt in}  & {\em skp1} & Right cell average value \\
\hline
\end{DoxyParams}


Definition at line 876 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
876   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: hkm1\textcolor{comment}{ !< Left cell width}
877   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: hk\textcolor{comment}{   !< Center cell width}
878   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: hkp1\textcolor{comment}{ !< Right cell width}
879   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: Skm1\textcolor{comment}{ !< Left cell average value}
880   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: Sk\textcolor{comment}{   !< Center cell average value}
881   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: Skp1\textcolor{comment}{ !< Right cell average value}
882 
883   \textcolor{comment}{! Local variables}
884   \textcolor{keywordtype}{real} :: h\_sum, hp, hm
885 
886   h\_sum = ( hkm1 + hkp1 ) + hk
887   \textcolor{keywordflow}{if} (h\_sum /= 0.) h\_sum = 1./ h\_sum
888   hm =  hkm1 + hk
889   \textcolor{keywordflow}{if} (hm /= 0.) hm = 1./ hm
890   hp =  hkp1 + hk
891   \textcolor{keywordflow}{if} (hp /= 0.) hp = 1./ hp
892   fv\_diff = ( hk * h\_sum ) * &
893             (   ( 2. * hkm1 + hk ) * hp * ( skp1 - sk ) &
894               + ( 2. * hkp1 + hk ) * hm * ( sk - skm1 ) )
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a6ed3814088af470ad6fc4a3a80926fd7}\label{namespacemom__neutral__diffusion_a6ed3814088af470ad6fc4a3a80926fd7}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!fvlsq\+\_\+slope@{fvlsq\+\_\+slope}}
\index{fvlsq\+\_\+slope@{fvlsq\+\_\+slope}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{fvlsq\+\_\+slope()}{fvlsq\_slope()}}
{\footnotesize\ttfamily real function mom\+\_\+neutral\+\_\+diffusion\+::fvlsq\+\_\+slope (\begin{DoxyParamCaption}\item[{real, intent(in)}]{hkm1,  }\item[{real, intent(in)}]{hk,  }\item[{real, intent(in)}]{hkp1,  }\item[{real, intent(in)}]{Skm1,  }\item[{real, intent(in)}]{Sk,  }\item[{real, intent(in)}]{Skp1 }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns the cell-\/centered second-\/order weighted least squares slope using three consecutive cell widths and average values. Slope is returned as a gradient (i.\+e. units of scalar S over width units). 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em hkm1} & Left cell width\\
\hline
\mbox{\tt in}  & {\em hk} & Center cell width\\
\hline
\mbox{\tt in}  & {\em hkp1} & Right cell width\\
\hline
\mbox{\tt in}  & {\em skm1} & Left cell average value\\
\hline
\mbox{\tt in}  & {\em sk} & Center cell average value\\
\hline
\mbox{\tt in}  & {\em skp1} & Right cell average value \\
\hline
\end{DoxyParams}


Definition at line 902 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
902   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: hkm1\textcolor{comment}{ !< Left cell width}
903   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: hk\textcolor{comment}{   !< Center cell width}
904   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: hkp1\textcolor{comment}{ !< Right cell width}
905   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: Skm1\textcolor{comment}{ !< Left cell average value}
906   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: Sk\textcolor{comment}{   !< Center cell average value}
907   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: Skp1\textcolor{comment}{ !< Right cell average value}
908 
909   \textcolor{comment}{! Local variables}
910   \textcolor{keywordtype}{real} :: xkm1, xkp1
911   \textcolor{keywordtype}{real} :: h\_sum, hx\_sum, hxsq\_sum, hxy\_sum, hy\_sum, det
912 
913   xkm1 = -0.5 * ( hk + hkm1 )
914   xkp1 = 0.5 * ( hk + hkp1 )
915   h\_sum = ( hkm1 + hkp1 ) + hk
916   hx\_sum = hkm1*xkm1 + hkp1*xkp1
917   hxsq\_sum = hkm1*(xkm1**2) + hkp1*(xkp1**2)
918   hxy\_sum = hkm1*xkm1*skm1 + hkp1*xkp1*skp1
919   hy\_sum = ( hkm1*skm1 + hkp1*skp1 ) + hk*sk
920   det = h\_sum * hxsq\_sum - hx\_sum**2
921   \textcolor{keywordflow}{if} (det /= 0.) \textcolor{keywordflow}{then}
922     \textcolor{comment}{!a = ( hxsq\_sum * hy\_sum - hx\_sum*hxy\_sum ) / det ! a would be mean of straight line fit}
923     fvlsq\_slope = ( h\_sum * hxy\_sum - hx\_sum*hy\_sum ) / det \textcolor{comment}{! Gradient of straight line fit}
924   \textcolor{keywordflow}{else}
925     fvlsq\_slope = 0. \textcolor{comment}{! Adcroft's reciprocal rule}
926 \textcolor{keywordflow}{  endif}
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_aa8a9c4a6e7e96ae356f913d4e8611f17}\label{namespacemom__neutral__diffusion_aa8a9c4a6e7e96ae356f913d4e8611f17}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!increment\+\_\+interface@{increment\+\_\+interface}}
\index{increment\+\_\+interface@{increment\+\_\+interface}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{increment\+\_\+interface()}{increment\_interface()}}
{\footnotesize\ttfamily subroutine mom\+\_\+neutral\+\_\+diffusion\+::increment\+\_\+interface (\begin{DoxyParamCaption}\item[{integer, intent(in)}]{nk,  }\item[{integer, intent(inout)}]{kl,  }\item[{integer, intent(inout)}]{ki,  }\item[{logical, intent(inout)}]{reached\+\_\+bottom,  }\item[{logical, intent(inout)}]{searching\+\_\+this\+\_\+column,  }\item[{logical, intent(inout)}]{searching\+\_\+other\+\_\+column }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Increments the interface which was just connected and also set flags if the bottom is reached. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em nk} & Number of vertical levels\\
\hline
\mbox{\tt in,out}  & {\em kl} & Current layer (potentially updated)\\
\hline
\mbox{\tt in,out}  & {\em ki} & Current interface\\
\hline
\mbox{\tt in,out}  & {\em reached\+\_\+bottom} & Updated when kl == nk and ki == 2\\
\hline
\mbox{\tt in,out}  & {\em searching\+\_\+this\+\_\+column} & Updated when kl == nk and ki == 2\\
\hline
\mbox{\tt in,out}  & {\em searching\+\_\+other\+\_\+column} & Updated when kl == nk and ki == 2 \\
\hline
\end{DoxyParams}


Definition at line 1507 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
1507   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{intent(in   )}                :: nk\textcolor{comment}{                     !< Number of vertical levels}
1508   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{intent(inout)}                :: kl\textcolor{comment}{                     !< Current layer (potentially updated)}
1509   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{intent(inout)}                :: ki\textcolor{comment}{                     !< Current interface}
1510   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{intent(inout)}                :: reached\_bottom\textcolor{comment}{         !< Updated when kl == nk and ki == 2}
1511   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{intent(inout)}                :: searching\_this\_column\textcolor{comment}{  !< Updated when kl == nk and ki == 2}
1512   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{intent(inout)}                :: searching\_other\_column\textcolor{comment}{ !< Updated when kl == nk and ki == 2}
1513   \textcolor{keywordtype}{integer} :: k
1514 
1515   reached\_bottom = .false.
1516   \textcolor{keywordflow}{if} (ki == 2) \textcolor{keywordflow}{then} \textcolor{comment}{! At the bottom interface}
1517     \textcolor{keywordflow}{if} ((ki == 2) .and. (kl < nk) ) \textcolor{keywordflow}{then} \textcolor{comment}{! Not at the bottom so just go to the next layer}
1518       kl = kl+1
1519       ki = 1
1520     \textcolor{keywordflow}{elseif} ((kl == nk) .and. (ki==2)) \textcolor{keywordflow}{then}
1521       reached\_bottom = .true.
1522       searching\_this\_column = .false.
1523       searching\_other\_column = .true.
1524 \textcolor{keywordflow}{    endif}
1525   \textcolor{keywordflow}{elseif} (ki==1) \textcolor{keywordflow}{then} \textcolor{comment}{! At the top interface}
1526     ki = 2 \textcolor{comment}{! Next interface is same layer, but bottom interface}
1527   \textcolor{keywordflow}{else}
1528     \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"Unanticipated eventuality in increment\_interface"})
1529 \textcolor{keywordflow}{  endif}
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_aec9b89b6a5ddc81f102309b964a0b969}\label{namespacemom__neutral__diffusion_aec9b89b6a5ddc81f102309b964a0b969}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!interface\+\_\+scalar@{interface\+\_\+scalar}}
\index{interface\+\_\+scalar@{interface\+\_\+scalar}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{interface\+\_\+scalar()}{interface\_scalar()}}
{\footnotesize\ttfamily subroutine mom\+\_\+neutral\+\_\+diffusion\+::interface\+\_\+scalar (\begin{DoxyParamCaption}\item[{integer, intent(in)}]{nk,  }\item[{real, dimension(nk), intent(in)}]{h,  }\item[{real, dimension(nk), intent(in)}]{S,  }\item[{real, dimension(nk+1), intent(inout)}]{Si,  }\item[{integer, intent(in)}]{i\+\_\+method,  }\item[{real, intent(in)}]{h\+\_\+neglect }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns interface scalar, Si, for a column of layer values, S. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em nk} & Number of levels\\
\hline
\mbox{\tt in}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s} & Layer scalar (conc, e.\+g. ppt)\\
\hline
\mbox{\tt in,out}  & {\em si} & Interface scalar (conc, e.\+g. ppt)\\
\hline
\mbox{\tt in}  & {\em i\+\_\+method} & =1 use average of P\+LM edges =2 use continuous P\+PM edge interpolation\\
\hline
\mbox{\tt in}  & {\em h\+\_\+neglect} & A negligibly small thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 693 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
693   \textcolor{keywordtype}{integer},               \textcolor{keywordtype}{intent(in)}    :: nk\textcolor{comment}{       !< Number of levels}
694   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},   \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{        !< Layer thickness [H ~> m or kg m-2]}
695   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},   \textcolor{keywordtype}{intent(in)}    :: S\textcolor{comment}{        !< Layer scalar (conc, e.g. ppt)}
696   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)}, \textcolor{keywordtype}{intent(inout)} :: Si\textcolor{comment}{       !< Interface scalar (conc, e.g. ppt)}
697   \textcolor{keywordtype}{integer},               \textcolor{keywordtype}{intent(in)}    :: i\_method\textcolor{comment}{ !< =1 use average of PLM edges}
698 \textcolor{comment}{                                                   !! =2 use continuous PPM edge interpolation}
699   \textcolor{keywordtype}{real},                  \textcolor{keywordtype}{intent(in)}    :: h\_neglect\textcolor{comment}{ !< A negligibly small thickness [H ~> m or kg m-2]}
700   \textcolor{comment}{! Local variables}
701   \textcolor{keywordtype}{integer} :: k, km2, kp1
702   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)} :: diff
703   \textcolor{keywordtype}{real} :: Sb, Sa
704 
705   \textcolor{keyword}{call }plm\_diff(nk, h, s, 2, 1, diff)
706   si(1) = s(1) - 0.5 * diff(1)
707   \textcolor{keywordflow}{if} (i\_method==1) \textcolor{keywordflow}{then}
708     \textcolor{keywordflow}{do} k = 2, nk
709       \textcolor{comment}{! Average of the two edge values (will be bounded and,}
710       \textcolor{comment}{! when slopes are unlimited, notionally second-order accurate)}
711       sa = s(k-1) + 0.5 * diff(k-1) \textcolor{comment}{! Lower edge value of a PLM reconstruction for layer above}
712       sb = s(k) - 0.5 * diff(k) \textcolor{comment}{! Upper edge value of a PLM reconstruction for layer below}
713       si(k) = 0.5 * ( sa + sb )
714 \textcolor{keywordflow}{    enddo}
715   \textcolor{keywordflow}{elseif} (i\_method==2) \textcolor{keywordflow}{then}
716     \textcolor{keywordflow}{do} k = 2, nk
717       \textcolor{comment}{! PPM quasi-fourth order interpolation for edge values following}
718       \textcolor{comment}{! equation 1.6 in Colella & Woodward, 1984: JCP 54, 174-201.}
719       km2 = max(1, k-2)
720       kp1 = min(nk, k+1)
721       si(k) = ppm\_edge(h(km2), h(k-1), h(k), h(kp1),  s(k-1), s(k), diff(k-1), diff(k), h\_neglect)
722 \textcolor{keywordflow}{    enddo}
723 \textcolor{keywordflow}{  endif}
724   si(nk+1) = s(nk) + 0.5 * diff(nk)
725 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a666836e70bdae7ef4ec271246cf91993}\label{namespacemom__neutral__diffusion_a666836e70bdae7ef4ec271246cf91993}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!interpolate\+\_\+for\+\_\+nondim\+\_\+position@{interpolate\+\_\+for\+\_\+nondim\+\_\+position}}
\index{interpolate\+\_\+for\+\_\+nondim\+\_\+position@{interpolate\+\_\+for\+\_\+nondim\+\_\+position}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{interpolate\+\_\+for\+\_\+nondim\+\_\+position()}{interpolate\_for\_nondim\_position()}}
{\footnotesize\ttfamily real function mom\+\_\+neutral\+\_\+diffusion\+::interpolate\+\_\+for\+\_\+nondim\+\_\+position (\begin{DoxyParamCaption}\item[{real, intent(in)}]{d\+Rho\+Neg,  }\item[{real, intent(in)}]{Pneg,  }\item[{real, intent(in)}]{d\+Rho\+Pos,  }\item[{real, intent(in)}]{Ppos }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns the non-\/dimensional position between Pneg and Ppos where the interpolated density difference equals zero. The result is always bounded to be between 0 and 1. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em drhoneg} & Negative density difference \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}\\
\hline
\mbox{\tt in}  & {\em pneg} & Position of negative density difference \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} or \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em drhopos} & Positive density difference \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}\\
\hline
\mbox{\tt in}  & {\em ppos} & Position of positive density difference \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} or \mbox{[}nondim\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 1143 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
1143   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: dRhoNeg\textcolor{comment}{ !< Negative density difference [R ~> kg m-3]}
1144   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: Pneg\textcolor{comment}{    !< Position of negative density difference [R L2 T-2 ~> Pa] or [nondim]}
1145   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: dRhoPos\textcolor{comment}{ !< Positive density difference [R ~> kg m-3]}
1146   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: Ppos\textcolor{comment}{    !< Position of positive density difference [R L2 T-2 ~> Pa] or [nondim]}
1147 
1148   \textcolor{keywordtype}{character(len=120)} :: mesg
1149 
1150   \textcolor{keywordflow}{if} (ppos < pneg) \textcolor{keywordflow}{then}
1151     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{'interpolate\_for\_nondim\_position: Houston, we have a problem! Ppos<Pneg'})
1152   \textcolor{keywordflow}{elseif} (drhoneg>drhopos) \textcolor{keywordflow}{then}
1153     \textcolor{keyword}{write}(stderr,*) \textcolor{stringliteral}{'dRhoNeg, Pneg, dRhoPos, Ppos='},drhoneg, pneg, drhopos, ppos
1154     \textcolor{keyword}{write}(mesg,*) \textcolor{stringliteral}{'dRhoNeg, Pneg, dRhoPos, Ppos='}, drhoneg, pneg, drhopos, ppos
1155     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{'interpolate\_for\_nondim\_position: '}//trim(mesg))
1156   \textcolor{keywordflow}{elseif} (drhoneg>drhopos) \textcolor{keywordflow}{then} \textcolor{comment}{!### Does this duplicated test belong here?}
1157     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{'interpolate\_for\_nondim\_position: Houston, we have a problem! dRhoNeg>dRhoPos'})
1158 \textcolor{keywordflow}{  endif}
1159   \textcolor{keywordflow}{if} (ppos<=pneg) \textcolor{keywordflow}{then} \textcolor{comment}{! Handle vanished or inverted layers}
1160     interpolate\_for\_nondim\_position = 0.5
1161   \textcolor{keywordflow}{elseif} ( drhopos - drhoneg > 0. ) \textcolor{keywordflow}{then}
1162     interpolate\_for\_nondim\_position = min( 1., max( 0., -drhoneg / ( drhopos - drhoneg ) ) )
1163   \textcolor{keywordflow}{elseif} ( drhopos - drhoneg == 0) \textcolor{keywordflow}{then}
1164     \textcolor{keywordflow}{if} (drhoneg>0.) \textcolor{keywordflow}{then}
1165       interpolate\_for\_nondim\_position = 0.
1166     \textcolor{keywordflow}{elseif} (drhoneg<0.) \textcolor{keywordflow}{then}
1167       interpolate\_for\_nondim\_position = 1.
1168     \textcolor{keywordflow}{else} \textcolor{comment}{! dRhoPos = dRhoNeg = 0}
1169       interpolate\_for\_nondim\_position = 0.5
1170 \textcolor{keywordflow}{    endif}
1171   \textcolor{keywordflow}{else} \textcolor{comment}{! dRhoPos - dRhoNeg < 0}
1172     interpolate\_for\_nondim\_position = 0.5
1173 \textcolor{keywordflow}{  endif}
1174   \textcolor{keywordflow}{if} ( interpolate\_for\_nondim\_position < 0. ) &
1175     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{'interpolate\_for\_nondim\_position: Houston, we have a problem! Pint < Pneg'})
1176   \textcolor{keywordflow}{if} ( interpolate\_for\_nondim\_position > 1. ) &
1177     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{'interpolate\_for\_nondim\_position: Houston, we have a problem! Pint > Ppos'})
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a3ef7040590d6448a34013763e17edf47}\label{namespacemom__neutral__diffusion_a3ef7040590d6448a34013763e17edf47}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!mark\+\_\+unstable\+\_\+cells@{mark\+\_\+unstable\+\_\+cells}}
\index{mark\+\_\+unstable\+\_\+cells@{mark\+\_\+unstable\+\_\+cells}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{mark\+\_\+unstable\+\_\+cells()}{mark\_unstable\_cells()}}
{\footnotesize\ttfamily subroutine mom\+\_\+neutral\+\_\+diffusion\+::mark\+\_\+unstable\+\_\+cells (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__neutral__diffusion_1_1neutral__diffusion__cs}{neutral\+\_\+diffusion\+\_\+cs}}), intent(inout)}]{CS,  }\item[{integer, intent(in)}]{nk,  }\item[{real, dimension(nk,2), intent(in)}]{T,  }\item[{real, dimension(nk,2), intent(in)}]{S,  }\item[{real, dimension(nk,2), intent(in)}]{P,  }\item[{logical, dimension(nk), intent(out)}]{stable\+\_\+cell }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Sweep down through the column and mark as stable if the bottom interface of a cell is denser than the top. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in,out}  & {\em cs} & Neutral diffusion control structure\\
\hline
\mbox{\tt in}  & {\em nk} & Number of levels in a column\\
\hline
\mbox{\tt in}  & {\em t} & Temperature at interfaces \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s} & Salinity at interfaces \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em p} & Pressure at interfaces \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt out}  & {\em stable\+\_\+cell} & True if this cell is unstably stratified \\
\hline
\end{DoxyParams}


Definition at line 1419 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
1419   \textcolor{keywordtype}{type}(neutral\_diffusion\_CS), \textcolor{keywordtype}{intent(inout)} :: CS\textcolor{comment}{      !< Neutral diffusion control structure}
1420   \textcolor{keywordtype}{integer},                \textcolor{keywordtype}{intent(in)}    :: nk\textcolor{comment}{          !< Number of levels in a column}
1421   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)},  \textcolor{keywordtype}{intent(in)}    :: T\textcolor{comment}{           !< Temperature at interfaces [degC]}
1422   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)},  \textcolor{keywordtype}{intent(in)}    :: S\textcolor{comment}{           !< Salinity at interfaces [ppt]}
1423   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)},  \textcolor{keywordtype}{intent(in)}    :: P\textcolor{comment}{           !< Pressure at interfaces [R L2 T-2 ~> Pa]}
1424   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(nk)}, \textcolor{keywordtype}{intent(  out)} :: stable\_cell\textcolor{comment}{ !< True if this cell is unstably stratified}
1425 
1426   \textcolor{keywordtype}{integer} :: k, first\_stable, prev\_stable
1427   \textcolor{keywordtype}{real} :: delta\_rho \textcolor{comment}{! A density difference [R ~> kg m-3]}
1428 
1429   \textcolor{keywordflow}{do} k = 1,nk
1430     \textcolor{keyword}{call }calc\_delta\_rho\_and\_derivs( cs, t(k,2), s(k,2), max(p(k,2), cs%ref\_pres), &
1431                                         t(k,1), s(k,1), max(p(k,1), cs%ref\_pres), delta\_rho )
1432     stable\_cell(k) = (delta\_rho > 0.)
1433 \textcolor{keywordflow}{  enddo}
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a9aaf0988f76c9992d8f1db48ce23bf34}\label{namespacemom__neutral__diffusion_a9aaf0988f76c9992d8f1db48ce23bf34}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!ndiff\+\_\+unit\+\_\+tests\+\_\+continuous@{ndiff\+\_\+unit\+\_\+tests\+\_\+continuous}}
\index{ndiff\+\_\+unit\+\_\+tests\+\_\+continuous@{ndiff\+\_\+unit\+\_\+tests\+\_\+continuous}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{ndiff\+\_\+unit\+\_\+tests\+\_\+continuous()}{ndiff\_unit\_tests\_continuous()}}
{\footnotesize\ttfamily logical function mom\+\_\+neutral\+\_\+diffusion\+::ndiff\+\_\+unit\+\_\+tests\+\_\+continuous (\begin{DoxyParamCaption}\item[{logical, intent(in)}]{verbose }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns true if unit tests of neutral\+\_\+diffusion functions fail. Otherwise returns false. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em verbose} & If true, write results to stdout \\
\hline
\end{DoxyParams}


Definition at line 2089 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2089   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{intent(in)} :: verbose\textcolor{comment}{ !< If true, write results to stdout}
2090   \textcolor{comment}{! Local variables}
2091   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{parameter}         :: nk = 4
2092   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)}      :: TiL, TiR1, TiR2, TiR4, Tio \textcolor{comment}{! Test interface temperatures}
2093   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)}        :: TL                         \textcolor{comment}{! Test layer temperatures}
2094   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)}      :: SiL                        \textcolor{comment}{! Test interface salinities}
2095   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)}      :: PiL, PiR4                  \textcolor{comment}{! Test interface positions}
2096   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(2*nk+2)}    :: PiLRo, PiRLo               \textcolor{comment}{! Test positions}
2097   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2*nk+2)} :: KoL, KoR                   \textcolor{comment}{! Test indexes}
2098   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(2*nk+1)}    :: hEff                       \textcolor{comment}{! Test positions}
2099   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(2*nk+1)}    :: Flx                        \textcolor{comment}{! Test flux}
2100   \textcolor{keywordtype}{integer} :: k
2101   \textcolor{keywordtype}{logical} :: v
2102   \textcolor{keywordtype}{real} :: h\_neglect
2103 
2104   h\_neglect = 1.0e-30
2105 
2106   v = verbose
2107 
2108   ndiff\_unit\_tests\_continuous = .false. \textcolor{comment}{! Normally return false}
2109   \textcolor{keyword}{write}(stdout,*) \textcolor{stringliteral}{'==== MOM\_neutral\_diffusion: ndiff\_unit\_tests\_continuous ='}
2110 
2111   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2112     test\_fv\_diff(v,1.,1.,1., 0.,1.,2., 1., \textcolor{stringliteral}{'FV: Straight line on uniform grid'})
2113   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2114     test\_fv\_diff(v,1.,1.,0., 0.,4.,8., 7., \textcolor{stringliteral}{'FV: Vanished right cell'})
2115   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2116     test\_fv\_diff(v,0.,1.,1., 0.,4.,8., 7., \textcolor{stringliteral}{'FV: Vanished left cell'})
2117   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2118     test\_fv\_diff(v,1.,2.,4., 0.,3.,9., 4., \textcolor{stringliteral}{'FV: Stretched grid'})
2119   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2120     test\_fv\_diff(v,2.,0.,2., 0.,1.,2., 0., \textcolor{stringliteral}{'FV: Vanished middle cell'})
2121   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2122     test\_fv\_diff(v,0.,1.,0., 0.,1.,2., 2., \textcolor{stringliteral}{'FV: Vanished on both sides'})
2123   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2124     test\_fv\_diff(v,1.,0.,0., 0.,1.,2., 0., \textcolor{stringliteral}{'FV: Two vanished cell sides'})
2125   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2126     test\_fv\_diff(v,0.,0.,0., 0.,1.,2., 0., \textcolor{stringliteral}{'FV: All vanished cells'})
2127 
2128   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2129     test\_fvlsq\_slope(v,1.,1.,1., 0.,1.,2., 1., \textcolor{stringliteral}{'LSQ: Straight line on uniform grid'})
2130   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2131     test\_fvlsq\_slope(v,1.,1.,0., 0.,1.,2., 1., \textcolor{stringliteral}{'LSQ: Vanished right cell'})
2132   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2133     test\_fvlsq\_slope(v,0.,1.,1., 0.,1.,2., 1., \textcolor{stringliteral}{'LSQ: Vanished left cell'})
2134   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2135     test\_fvlsq\_slope(v,1.,2.,4., 0.,3.,9., 2., \textcolor{stringliteral}{'LSQ: Stretched grid'})
2136   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2137     test\_fvlsq\_slope(v,1.,0.,1., 0.,1.,2., 2., \textcolor{stringliteral}{'LSQ: Vanished middle cell'})
2138   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2139     test\_fvlsq\_slope(v,0.,1.,0., 0.,1.,2., 0., \textcolor{stringliteral}{'LSQ: Vanished on both sides'})
2140   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2141     test\_fvlsq\_slope(v,1.,0.,0., 0.,1.,2., 0., \textcolor{stringliteral}{'LSQ: Two vanished cell sides'})
2142   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2143     test\_fvlsq\_slope(v,0.,0.,0., 0.,1.,2., 0., \textcolor{stringliteral}{'LSQ: All vanished cells'})
2144 
2145   \textcolor{keyword}{call }interface\_scalar(4, (/10.,10.,10.,10./), (/24.,18.,12.,6./), tio, 1, h\_neglect)
2146   \textcolor{comment}{!ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &}
2147   \textcolor{comment}{!  test\_data1d(5, Tio, (/27.,21.,15.,9.,3./), 'Linear profile, interface temperatures')}
2148   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2149     test\_data1d(v,5, tio, (/24.,22.5,15.,7.5,6./), \textcolor{stringliteral}{'Linear profile, linear interface temperatures'})
2150   \textcolor{keyword}{call }interface\_scalar(4, (/10.,10.,10.,10./), (/24.,18.,12.,6./), tio, 2, h\_neglect)
2151   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2152     test\_data1d(v,5, tio, (/24.,22.,15.,8.,6./), \textcolor{stringliteral}{'Linear profile, PPM interface temperatures'})
2153 
2154   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2155     test\_ifndp(v,-1.0, 0.,  1.0, 1.0, 0.5, \textcolor{stringliteral}{'Check mid-point'})
2156   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2157     test\_ifndp(v, 0.0, 0.,  1.0, 1.0, 0.0, \textcolor{stringliteral}{'Check bottom'})
2158   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2159     test\_ifndp(v, 0.1, 0.,  1.1, 1.0, 0.0, \textcolor{stringliteral}{'Check below'})
2160   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2161     test\_ifndp(v,-1.0, 0.,  0.0, 1.0, 1.0, \textcolor{stringliteral}{'Check top'})
2162   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2163     test\_ifndp(v,-1.0, 0., -0.1, 1.0, 1.0, \textcolor{stringliteral}{'Check above'})
2164   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2165     test\_ifndp(v,-1.0, 0.,  3.0, 1.0, 0.25, \textcolor{stringliteral}{'Check 1/4'})
2166   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2167     test\_ifndp(v,-3.0, 0.,  1.0, 1.0, 0.75, \textcolor{stringliteral}{'Check 3/4'})
2168   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2169     test\_ifndp(v, 1.0, 0.,  1.0, 1.0, 0.0, \textcolor{stringliteral}{'Check dRho=0 below'})
2170   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2171     test\_ifndp(v,-1.0, 0., -1.0, 1.0, 1.0, \textcolor{stringliteral}{'Check dRho=0 above'})
2172   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2173     test\_ifndp(v, 0.0, 0.,  0.0, 1.0, 0.5, \textcolor{stringliteral}{'Check dRho=0 mid'})
2174   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. &
2175     test\_ifndp(v,-2.0, .5,  5.0, 0.5, 0.5, \textcolor{stringliteral}{'Check dP=0'})
2176 
2177   \textcolor{comment}{! Identical columns}
2178   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_continuous(3, &
2179              (/0.,10.,20.,30./), (/22.,18.,14.,10./), (/0.,0.,0.,0./), & \textcolor{comment}{! Left positions, T and S}
2180              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Left dRdT and dRdS}
2181              (/0.,10.,20.,30./), (/22.,18.,14.,10./), (/0.,0.,0.,0./), & \textcolor{comment}{! Right positions, T and S}
2182              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Right dRdT and dRdS}
2183              pilro, pirlo, kol, kor, heff)
2184   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or.  test\_nsp(v, 8, kol, kor, pilro, pirlo, 
      heff, &
2185                                    (/1,1,2,2,3,3,3,3/), & \textcolor{comment}{! KoL}
2186                                    (/1,1,2,2,3,3,3,3/), & \textcolor{comment}{! KoR}
2187                                    (/0.,0.,0.,0.,0.,0.,1.,1./), & \textcolor{comment}{! pL}
2188                                    (/0.,0.,0.,0.,0.,0.,1.,1./), & \textcolor{comment}{! pR}
2189                                    (/0.,10.,0.,10.,0.,10.,0./), & \textcolor{comment}{! hEff}
2190                                    \textcolor{stringliteral}{'Identical columns'})
2191   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. test\_data1d(v, 8, &
2192                                    absolute\_positions(3, 8, (/0.,10.,20.,30./), kol, pilro), &
2193                                    (/0.,0.,10.,10.,20.,20.,30.,30./), \textcolor{stringliteral}{'... left positions'})
2194   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. test\_data1d(v, 8, &
2195                                    absolute\_positions(3, 8, (/0.,10.,20.,30./), kor, pirlo), &
2196                                    (/0.,0.,10.,10.,20.,20.,30.,30./), \textcolor{stringliteral}{'... right positions'})
2197   \textcolor{keyword}{call }neutral\_surface\_flux(3, 2*3+2, 2, (/10.,10.,10./), (/10.,10.,10./), & \textcolor{comment}{! nk, hL, hR}
2198                                (/20.,16.,12./), (/20.,16.,12./), & \textcolor{comment}{! Tl, Tr}
2199                                pilro, pirlo, kol, kor, heff, flx, .true., h\_neglect)
2200   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. test\_data1d(v, 7, flx, &
2201               (/0.,0.,0.,0.,0.,0.,0./), \textcolor{stringliteral}{'Identical columns, rho flux (=0)'})
2202   \textcolor{keyword}{call }neutral\_surface\_flux(3, 2*3+2, 2, (/10.,10.,10./), (/10.,10.,10./), & \textcolor{comment}{! nk, hL, hR}
2203                                (/-1.,-1.,-1./), (/1.,1.,1./), & \textcolor{comment}{! Sl, Sr}
2204                                pilro, pirlo, kol, kor, heff, flx, .true., h\_neglect)
2205   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. test\_data1d(v, 7, flx, &
2206               (/0.,20.,0.,20.,0.,20.,0./), \textcolor{stringliteral}{'Identical columns, S flux'})
2207 
2208   \textcolor{comment}{! Right column slightly cooler than left}
2209   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_continuous(3, &
2210              (/0.,10.,20.,30./), (/22.,18.,14.,10./), (/0.,0.,0.,0./), & \textcolor{comment}{! Left positions, T and S}
2211              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Left dRdT and dRdS}
2212              (/0.,10.,20.,30./), (/20.,16.,12.,8./), (/0.,0.,0.,0./), & \textcolor{comment}{! Right positions, T and S}
2213              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Right dRdT and dRdS}
2214              pilro, pirlo, kol, kor, heff)
2215   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or.  test\_nsp(v, 8, kol, kor, pilro, pirlo, 
      heff, &
2216                                    (/1,1,2,2,3,3,3,3/), & \textcolor{comment}{! kL}
2217                                    (/1,1,1,2,2,3,3,3/), & \textcolor{comment}{! kR}
2218                                    (/0.,0.5,0.,0.5,0.,0.5,1.,1./), & \textcolor{comment}{! pL}
2219                                    (/0.,0.,0.5,0.,0.5,0.,0.5,1./), & \textcolor{comment}{! pR}
2220                                    (/0.,5.,5.,5.,5.,5.,0./), & \textcolor{comment}{! hEff}
2221                                    \textcolor{stringliteral}{'Right column slightly cooler'})
2222   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. test\_data1d(v, 8, &
2223                                    absolute\_positions(3, 8, (/0.,10.,20.,30./), kol, pilro), &
2224                                    (/0.,5.,10.,15.,20.,25.,30.,30./), \textcolor{stringliteral}{'... left positions'})
2225   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or. test\_data1d(v, 8, &
2226                                    absolute\_positions(3, 8, (/0.,10.,20.,30./), kor, pirlo), &
2227                                    (/0.,0.,5.,10.,15.,20.,25.,30./), \textcolor{stringliteral}{'... right positions'})
2228 
2229   \textcolor{comment}{! Right column slightly warmer than left}
2230   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_continuous(3, &
2231              (/0.,10.,20.,30./), (/22.,18.,14.,10./), (/0.,0.,0.,0./), & \textcolor{comment}{! Left positions, T and S}
2232              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Left dRdT and dRdS}
2233              (/0.,10.,20.,30./), (/24.,20.,16.,12./), (/0.,0.,0.,0./), & \textcolor{comment}{! Right positions, T and S}
2234              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Right dRdT and dRdS}
2235              pilro, pirlo, kol, kor, heff)
2236   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or.  test\_nsp(v, 8, kol, kor, pilro, pirlo, 
      heff, &
2237                                    (/1,1,1,2,2,3,3,3/), & \textcolor{comment}{! kL}
2238                                    (/1,1,2,2,3,3,3,3/), & \textcolor{comment}{! kR}
2239                                    (/0.,0.,0.5,0.,0.5,0.,0.5,1./), & \textcolor{comment}{! pL}
2240                                    (/0.,0.5,0.,0.5,0.,0.5,1.,1./), & \textcolor{comment}{! pR}
2241                                    (/0.,5.,5.,5.,5.,5.,0./), & \textcolor{comment}{! hEff}
2242                                    \textcolor{stringliteral}{'Right column slightly warmer'})
2243 
2244   \textcolor{comment}{! Right column somewhat cooler than left}
2245   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_continuous(3, &
2246              (/0.,10.,20.,30./), (/22.,18.,14.,10./), (/0.,0.,0.,0./), & \textcolor{comment}{! Left positions, T and S}
2247              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Left dRdT and dRdS}
2248              (/0.,10.,20.,30./), (/16.,12.,8.,4./), (/0.,0.,0.,0./), & \textcolor{comment}{! Right positions, T and S}
2249              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Right dRdT and dRdS}
2250              pilro, pirlo, kol, kor, heff)
2251   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or.  test\_nsp(v, 8, kol, kor, pilro, pirlo, 
      heff, &
2252                                    (/1,2,2,3,3,3,3,3/), & \textcolor{comment}{! kL}
2253                                    (/1,1,1,1,2,2,3,3/), & \textcolor{comment}{! kR}
2254                                    (/0.,0.,0.5,0.,0.5,1.,1.,1./), & \textcolor{comment}{! pL}
2255                                    (/0.,0.,0.,0.5,0.,0.5,0.,1./), & \textcolor{comment}{! pR}
2256                                    (/0.,0.,5.,5.,5.,0.,0./), & \textcolor{comment}{! hEff}
2257                                    \textcolor{stringliteral}{'Right column somewhat cooler'})
2258 
2259   \textcolor{comment}{! Right column much colder than left with no overlap}
2260   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_continuous(3, &
2261              (/0.,10.,20.,30./), (/22.,18.,14.,10./), (/0.,0.,0.,0./), & \textcolor{comment}{! Left positions, T and S}
2262              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Left dRdT and dRdS}
2263              (/0.,10.,20.,30./), (/9.,7.,5.,3./), (/0.,0.,0.,0./), & \textcolor{comment}{! Right positions, T and S}
2264              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Right dRdT and dRdS}
2265              pilro, pirlo, kol, kor, heff)
2266   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or.  test\_nsp(v, 8, kol, kor, pilro, pirlo, 
      heff, &
2267                                    (/1,2,3,3,3,3,3,3/), & \textcolor{comment}{! kL}
2268                                    (/1,1,1,1,1,2,3,3/), & \textcolor{comment}{! kR}
2269                                    (/0.,0.,0.,1.,1.,1.,1.,1./), & \textcolor{comment}{! pL}
2270                                    (/0.,0.,0.,0.,0.,0.,0.,1./), & \textcolor{comment}{! pR}
2271                                    (/0.,0.,0.,0.,0.,0.,0./), & \textcolor{comment}{! hEff}
2272                                    \textcolor{stringliteral}{'Right column much cooler'})
2273 
2274   \textcolor{comment}{! Right column with mixed layer}
2275   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_continuous(3, &
2276              (/0.,10.,20.,30./), (/22.,18.,14.,10./), (/0.,0.,0.,0./), & \textcolor{comment}{! Left positions, T and S}
2277              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Left dRdT and dRdS}
2278              (/0.,10.,20.,30./), (/14.,14.,10.,2./), (/0.,0.,0.,0./), & \textcolor{comment}{! Right positions, T and S}
2279              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Right dRdT and dRdS}
2280              pilro, pirlo, kol, kor, heff)
2281   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or.  test\_nsp(v, 8, kol, kor, pilro, pirlo, 
      heff, &
2282                                    (/1,2,3,3,3,3,3,3/), & \textcolor{comment}{! kL}
2283                                    (/1,1,1,1,2,3,3,3/), & \textcolor{comment}{! kR}
2284                                    (/0.,0.,0.,0.,0.,1.,1.,1./), & \textcolor{comment}{! pL}
2285                                    (/0.,0.,0.,0.,0.,0.,0.,1./), & \textcolor{comment}{! pR}
2286                                    (/0.,0.,0.,0.,10.,0.,0./), & \textcolor{comment}{! hEff}
2287                                    \textcolor{stringliteral}{'Right column with mixed layer'})
2288 
2289   \textcolor{comment}{! Identical columns with mixed layer}
2290   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_continuous(3, &
2291              (/0.,10.,20.,30./), (/14.,14.,10.,2./), (/0.,0.,0.,0./), & \textcolor{comment}{! Left positions, T and S}
2292              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Left dRdT and dRdS}
2293              (/0.,10.,20.,30./), (/14.,14.,10.,2./), (/0.,0.,0.,0./), & \textcolor{comment}{! Right positions, T and S}
2294              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Right dRdT and dRdS}
2295              pilro, pirlo, kol, kor, heff)
2296   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or.  test\_nsp(v, 8, kol, kor, pilro, pirlo, 
      heff, &
2297                                    (/1,1,2,2,3,3,3,3/), & \textcolor{comment}{! kL}
2298                                    (/1,1,2,2,3,3,3,3/), & \textcolor{comment}{! kR}
2299                                    (/0.,0.,0.,0.,0.,0.,1.,1./), & \textcolor{comment}{! pL}
2300                                    (/0.,0.,0.,0.,0.,0.,1.,1./), & \textcolor{comment}{! pR}
2301                                    (/0.,10.,0.,10.,0.,10.,0./), & \textcolor{comment}{! hEff}
2302                                    \textcolor{stringliteral}{'Identical columns with mixed layer'})
2303 
2304   \textcolor{comment}{! Right column with unstable mixed layer}
2305   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_continuous(3, &
2306              (/0.,10.,20.,30./), (/14.,14.,10.,2./), (/0.,0.,0.,0./), & \textcolor{comment}{! Left positions, T and S}
2307              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Left dRdT and dRdS}
2308              (/0.,10.,20.,30./), (/10.,14.,12.,4./), (/0.,0.,0.,0./), & \textcolor{comment}{! Right positions, T and S}
2309              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Right dRdT and dRdS}
2310              pilro, pirlo, kol, kor, heff)
2311   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or.  test\_nsp(v, 8, kol, kor, pilro, pirlo, 
      heff, &
2312                                    (/1,2,3,3,3,3,3,3/), & \textcolor{comment}{! kL}
2313                                    (/1,1,1,2,3,3,3,3/), & \textcolor{comment}{! kR}
2314                                    (/0.,0.,0.,0.,0.,0.,.75,1./), & \textcolor{comment}{! pL}
2315                                    (/0.,0.,0.,0.,0.,0.25,1.,1./), & \textcolor{comment}{! pR}
2316                                    (/0.,0.,0.,0.,0.,7.5,0./), & \textcolor{comment}{! hEff}
2317                                    \textcolor{stringliteral}{'Right column with unstable mixed layer'})
2318 
2319   \textcolor{comment}{! Left column with unstable mixed layer}
2320   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_continuous(3, &
2321              (/0.,10.,20.,30./), (/10.,14.,12.,4./), (/0.,0.,0.,0./), & \textcolor{comment}{! Left positions, T and S}
2322              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Left dRdT and dRdS}
2323              (/0.,10.,20.,30./), (/14.,14.,10.,2./), (/0.,0.,0.,0./), & \textcolor{comment}{! Right positions, T and S}
2324              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Right dRdT and dRdS}
2325              pilro, pirlo, kol, kor, heff)
2326   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or.  test\_nsp(v, 8, kol, kor, pilro, pirlo, 
      heff, &
2327                                    (/1,1,1,2,3,3,3,3/), & \textcolor{comment}{! kL}
2328                                    (/1,2,3,3,3,3,3,3/), & \textcolor{comment}{! kR}
2329                                    (/0.,0.,0.,0.,0.,0.25,1.,1./), & \textcolor{comment}{! pL}
2330                                    (/0.,0.,0.,0.,0.,0.,.75,1./), & \textcolor{comment}{! pR}
2331                                    (/0.,0.,0.,0.,0.,7.5,0./), & \textcolor{comment}{! hEff}
2332                                    \textcolor{stringliteral}{'Left column with unstable mixed layer'})
2333 
2334   \textcolor{comment}{! Two unstable mixed layers}
2335   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_continuous(3, &
2336              (/0.,10.,20.,30./), (/8.,12.,10.,2./), (/0.,0.,0.,0./), & \textcolor{comment}{! Left positions, T and S}
2337              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Left dRdT and dRdS}
2338              (/0.,10.,20.,30./), (/10.,14.,12.,4./), (/0.,0.,0.,0./), & \textcolor{comment}{! Right positions, T and S}
2339              (/-1.,-1.,-1.,-1./), (/1.,1.,1.,1./), &\textcolor{comment}{! Right dRdT and dRdS}
2340              pilro, pirlo, kol, kor, heff)
2341   ndiff\_unit\_tests\_continuous = ndiff\_unit\_tests\_continuous .or.  test\_nsp(v, 8, kol, kor, pilro, pirlo, 
      heff, &
2342                                    (/1,1,1,1,2,3,3,3/), & \textcolor{comment}{! kL}
2343                                    (/1,2,3,3,3,3,3,3/), & \textcolor{comment}{! kR}
2344                                    (/0.,0.,0.,0.,0.,0.,0.75,1./), & \textcolor{comment}{! pL}
2345                                    (/0.,0.,0.,0.5,0.5,0.5,1.,1./), & \textcolor{comment}{! pR}
2346                                    (/0.,0.,0.,0.,0.,6.,0./), & \textcolor{comment}{! hEff}
2347                                    \textcolor{stringliteral}{'Two unstable mixed layers'})
2348 
2349   \textcolor{keywordflow}{if} (.not. ndiff\_unit\_tests\_continuous) \textcolor{keyword}{write}(stdout,*) \textcolor{stringliteral}{'Pass'}
2350 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_aa44f980bb24df5dc71913b32892b2f71}\label{namespacemom__neutral__diffusion_aa44f980bb24df5dc71913b32892b2f71}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!ndiff\+\_\+unit\+\_\+tests\+\_\+discontinuous@{ndiff\+\_\+unit\+\_\+tests\+\_\+discontinuous}}
\index{ndiff\+\_\+unit\+\_\+tests\+\_\+discontinuous@{ndiff\+\_\+unit\+\_\+tests\+\_\+discontinuous}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{ndiff\+\_\+unit\+\_\+tests\+\_\+discontinuous()}{ndiff\_unit\_tests\_discontinuous()}}
{\footnotesize\ttfamily logical function mom\+\_\+neutral\+\_\+diffusion\+::ndiff\+\_\+unit\+\_\+tests\+\_\+discontinuous (\begin{DoxyParamCaption}\item[{logical, intent(in)}]{verbose }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em verbose} & It true, write results to stdout \\
\hline
\end{DoxyParams}


Definition at line 2354 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2354   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{intent(in)} :: verbose\textcolor{comment}{ !< It true, write results to stdout}
2355   \textcolor{comment}{! Local variables}
2356   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{parameter}          :: nk = 3
2357   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{parameter}          :: ns = nk*4
2358   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)}         :: Sl, Sr, Tl, Tr \textcolor{comment}{! Salinities [ppt] and temperatures [degC]}
2359   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)}         :: hl, hr    \textcolor{comment}{! Thicknesses in pressure units [R L2 T-2 ~> Pa]}
2360   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)}       :: TiL, SiL, TiR, SiR \textcolor{comment}{! Cell edge salinities [ppt] and temperatures [degC]}
2361   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)}       :: Pres\_l, Pres\_r \textcolor{comment}{! Interface pressures [R L2 T-2 ~> Pa]}
2362   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(ns)}      :: KoL, KoR
2363   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(ns)}         :: PoL, PoR
2364   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(ns-1)}       :: hEff, Flx
2365   \textcolor{keywordtype}{type}(neutral\_diffusion\_CS)  :: CS\textcolor{comment}{        !< Neutral diffusion control structure}
2366   \textcolor{keywordtype}{type}(EOS\_type),     \textcolor{keywordtype}{pointer} :: EOS\textcolor{comment}{       !< Structure for linear equation of state}
2367   \textcolor{keywordtype}{type}(remapping\_CS), \textcolor{keywordtype}{pointer} :: remap\_CS\textcolor{comment}{  !< Remapping control structure (PLM)}
2368   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)}       :: ppoly\_T\_l, ppoly\_T\_r \textcolor{comment}{! Linear reconstruction for T}
2369   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)}       :: ppoly\_S\_l, ppoly\_S\_r \textcolor{comment}{! Linear reconstruction for S}
2370   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)}       :: dRdT\textcolor{comment}{      !< Partial derivative of density with temperature at}
2371 \textcolor{comment}{                                           !! cell edges [R degC-1 ~> kg m-3 degC-1]}
2372   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)}       :: dRdS\textcolor{comment}{      !< Partial derivative of density with salinity at}
2373 \textcolor{comment}{                                           !! cell edges [R ppt-1 ~> kg m-3 ppt-1]}
2374   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(nk)}      :: stable\_l, stable\_r
2375   \textcolor{keywordtype}{integer}                     :: iMethod
2376   \textcolor{keywordtype}{integer}                     :: ns\_l, ns\_r
2377   \textcolor{keywordtype}{integer} :: k
2378   \textcolor{keywordtype}{logical} :: v
2379 
2380   v = verbose
2381   ndiff\_unit\_tests\_discontinuous = .false. \textcolor{comment}{! Normally return false}
2382   \textcolor{keyword}{write}(stdout,*) \textcolor{stringliteral}{'==== MOM\_neutral\_diffusion: ndiff\_unit\_tests\_discontinuous ='}
2383 
2384   \textcolor{comment}{! Unit tests for find\_neutral\_surface\_positions\_discontinuous}
2385   \textcolor{comment}{! Salinity is 0 for all these tests}
2386   \textcolor{keyword}{allocate}(cs%EOS)
2387   \textcolor{keyword}{call }eos\_manual\_init(cs%EOS, form\_of\_eos=eos\_linear, drho\_dt=-1., drho\_ds=0.)
2388   sl(:) = 0. ; sr(:) = 0. ; ; sil(:,:) = 0. ; sir(:,:) = 0.
2389   ppoly\_t\_l(:,:) = 0.; ppoly\_t\_r(:,:) = 0.
2390   ppoly\_s\_l(:,:) = 0.; ppoly\_s\_r(:,:) = 0.
2391   \textcolor{comment}{! Intialize any control structures needed for unit tests}
2392   cs%ref\_pres = -1.
2393 
2394   hl = (/10.,10.,10./) ; hr = (/10.,10.,10./)
2395   pres\_l(1,1) = 0. ; pres\_l(1,2) = hl(1) ; pres\_r(1,1) = 0. ; pres\_r(1,2) = hr(1)
2396   \textcolor{keywordflow}{do} k = 2,nk
2397     pres\_l(k,1) = pres\_l(k-1,2)
2398     pres\_l(k,2) = pres\_l(k,1) + hl(k)
2399     pres\_r(k,1) = pres\_r(k-1,2)
2400     pres\_r(k,2) = pres\_r(k,1) + hr(k)
2401 \textcolor{keywordflow}{  enddo}
2402   cs%delta\_rho\_form = \textcolor{stringliteral}{'mid\_pressure'}
2403   cs%neutral\_pos\_method = 1
2404 
2405   til(1,:) = (/ 22.00, 18.00 /); til(2,:) = (/ 18.00, 14.00 /); til(3,:) = (/ 14.00, 10.00 /);
2406   tir(1,:) = (/ 22.00, 18.00 /); tir(2,:) = (/ 18.00, 14.00 /); tir(3,:) = (/ 14.00, 10.00 /);
2407   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, til, sil, pres\_l, stable\_l )
2408   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, tir, sir, pres\_r, stable\_r )
2409   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, nk, pres\_l, hl, til, sil, ppoly\_t\_l, ppoly\_s\_l, 
      stable\_l, &
2410            pres\_r, hr, tir, sir, ppoly\_t\_r, ppoly\_s\_r, stable\_r, pol, por, kol, kor, heff)
2411   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or.  test\_nsp(v, 12, kol, kor, pol, por,
       heff, &
2412     (/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoL}
2413     (/ 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoR}
2414     (/ 0.00, 0.00, 1.00, 1.00, 0.00, 0.00, 1.00, 1.00, 0.00, 0.00, 1.00, 1.00 /),  & \textcolor{comment}{! PoL}
2415     (/ 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 1.00, 1.00 /),  & \textcolor{comment}{! PoR}
2416     (/ 0.00, 10.00, 0.00, 0.00, 0.00, 10.00, 0.00, 0.00, 0.00, 10.00, 0.00 /),  & \textcolor{comment}{! hEff}
2417     \textcolor{stringliteral}{'Identical Columns'})
2418 
2419   til(1,:) = (/ 22.00, 18.00 /); til(2,:) = (/ 18.00, 14.00 /); til(3,:) = (/ 14.00, 10.00 /);
2420   tir(1,:) = (/ 20.00, 16.00 /); tir(2,:) = (/ 16.00, 12.00 /); tir(3,:) = (/ 12.00, 8.00 /);
2421   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, til, sil, pres\_l, stable\_l )
2422   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, tir, sir, pres\_r, stable\_r )
2423   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, nk, pres\_l, hl, til, sil, ppoly\_t\_l, ppoly\_s\_l, 
      stable\_l, &
2424            pres\_r, hr, tir, sir, ppoly\_t\_r, ppoly\_s\_r, stable\_r, pol, por, kol, kor, heff)
2425   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or.  test\_nsp(v, 12, kol, kor, pol, por,
       heff, &
2426     (/ 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoL}
2427     (/ 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3 /),  & \textcolor{comment}{! KoR}
2428     (/ 0.00, 0.50, 1.00, 0.00, 0.50, 0.50, 1.00, 0.00, 0.50, 0.50, 1.00, 1.00 /),  & \textcolor{comment}{! PoL}
2429     (/ 0.00, 0.00, 0.50, 0.50, 1.00, 0.00, 0.50, 0.50, 1.00, 0.00, 0.50, 1.00 /),  & \textcolor{comment}{! PoR}
2430     (/ 0.00, 5.00, 0.00, 5.00, 0.00, 5.00, 0.00, 5.00, 0.00, 5.00, 0.00 /),  & \textcolor{comment}{! hEff}
2431     \textcolor{stringliteral}{'Right slightly cooler'})
2432 
2433   til(1,:) = (/ 20.00, 16.00 /); til(2,:) = (/ 16.00, 12.00 /); til(3,:) = (/ 12.00, 8.00 /);
2434   tir(1,:) = (/ 22.00, 18.00 /); tir(2,:) = (/ 18.00, 14.00 /); tir(3,:) = (/ 14.00, 10.00 /);
2435   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, til, sil, pres\_l, stable\_l )
2436   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, tir, sir, pres\_r, stable\_r )
2437   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, nk, pres\_l, hl, til, sil, ppoly\_t\_l, ppoly\_s\_l, 
      stable\_l, &
2438            pres\_r, hr, tir, sir, ppoly\_t\_r, ppoly\_s\_r, stable\_r, pol, por, kol, kor, heff)
2439   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or.  test\_nsp(v, 12, kol, kor, pol, por,
       heff, &
2440     (/ 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3 /),  & \textcolor{comment}{! KoL}
2441     (/ 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoR}
2442     (/ 0.00, 0.00, 0.50, 0.50, 1.00, 0.00, 0.50, 0.50, 1.00, 0.00, 0.50, 1.00 /),  & \textcolor{comment}{! PoL}
2443     (/ 0.00, 0.50, 1.00, 0.00, 0.50, 0.50, 1.00, 0.00, 0.50, 0.50, 1.00, 1.00 /),  & \textcolor{comment}{! PoR}
2444     (/ 0.00, 5.00, 0.00, 5.00, 0.00, 5.00, 0.00, 5.00, 0.00, 5.00, 0.00 /),  & \textcolor{comment}{! hEff}
2445     \textcolor{stringliteral}{'Left slightly cooler'})
2446 
2447   til(1,:) = (/ 22.00, 20.00 /); til(2,:) = (/ 18.00, 16.00 /); til(3,:) = (/ 14.00, 12.00 /);
2448   tir(1,:) = (/ 32.00, 24.00 /); tir(2,:) = (/ 22.00, 14.00 /); tir(3,:) = (/ 12.00, 4.00 /);
2449   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, til, sil, pres\_l, stable\_l )
2450   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, tir, sir, pres\_r, stable\_r )
2451   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, nk, pres\_l, hl, til, sil, ppoly\_t\_l, ppoly\_s\_l, 
      stable\_l, &
2452            pres\_r, hr, tir, sir, ppoly\_t\_r, ppoly\_s\_r, stable\_r, pol, por, kol, kor, heff)
2453   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or.  test\_nsp(v, 12, kol, kor, pol, por,
       heff, &
2454     (/ 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoL}
2455     (/ 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoR}
2456     (/ 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 1.00, 0.00, 0.00, 1.00, 1.00, 1.00 /),  & \textcolor{comment}{! PoL}
2457     (/ 0.00, 1.00, 0.00, 0.00, 0.25, 0.50, 0.75, 1.00, 0.00, 0.00, 0.00, 1.00 /),  & \textcolor{comment}{! PoR}
2458     (/ 0.00, 0.00, 0.00, 4.00, 0.00, 4.00, 0.00, 0.00, 0.00, 0.00, 0.00 /),  & \textcolor{comment}{! hEff}
2459     \textcolor{stringliteral}{'Right more strongly stratified'})
2460 
2461   til(1,:) = (/ 22.00, 18.00 /); til(2,:) = (/ 18.00, 14.00 /); til(3,:) = (/ 14.00, 10.00 /);
2462   tir(1,:) = (/ 14.00, 14.00 /); tir(2,:) = (/ 14.00, 14.00 /); tir(3,:) = (/ 12.00, 8.00 /);
2463   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, til, sil, pres\_l, stable\_l )
2464   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, tir, sir, pres\_r, stable\_r )
2465   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, nk, pres\_l, hl, til, sil, ppoly\_t\_l, ppoly\_s\_l, 
      stable\_l, &
2466            pres\_r, hr, tir, sir, ppoly\_t\_r, ppoly\_s\_r, stable\_r, pol, por, kol, kor, heff)
2467   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or.  test\_nsp(v, 12, kol, kor, pol, por,
       heff, &
2468     (/ 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoL}
2469     (/ 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoR}
2470     (/ 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 1.00, 0.00, 0.50, 1.00, 1.00 /),  & \textcolor{comment}{! PoL}
2471     (/ 0.00, 1.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.50, 1.00 /),  & \textcolor{comment}{! PoR}
2472     (/ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 5.00, 0.00 /),  & \textcolor{comment}{! hEff}
2473     \textcolor{stringliteral}{'Deep Mixed layer on the right'})
2474 
2475   til(1,:) = (/ 14.00, 14.00 /); til(2,:) = (/ 14.00, 12.00 /); til(3,:) = (/ 10.00, 8.00 /);
2476   tir(1,:) = (/ 14.00, 14.00 /); tir(2,:) = (/ 14.00, 14.00 /); tir(3,:) = (/ 14.00, 14.00 /);
2477   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, til, sil, pres\_l, stable\_l )
2478   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, tir, sir, pres\_r, stable\_r )
2479   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, nk, pres\_l, hl, til, sil, ppoly\_t\_l, ppoly\_s\_l, 
      stable\_l, &
2480            pres\_r, hr, tir, sir, ppoly\_t\_r, ppoly\_s\_r, stable\_r, pol, por, kol, kor, heff)
2481   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or.  test\_nsp(v, 12, kol, kor, pol, por,
       heff, &
2482     (/ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3 /),  & \textcolor{comment}{! KoL}
2483     (/ 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoR}
2484     (/ 0.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00 /),  & \textcolor{comment}{! PoL}
2485     (/ 0.00, 0.00, 0.00, 1.00, 0.00, 1.00, 0.00, 1.00, 1.00, 1.00, 1.00, 1.00 /),  & \textcolor{comment}{! PoR}
2486     (/ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 /),  & \textcolor{comment}{! hEff}
2487     \textcolor{stringliteral}{'Right unstratified column'})
2488 
2489   til(1,:) = (/ 14.00, 14.00 /); til(2,:) = (/ 14.00, 12.00 /); til(3,:) = (/ 10.00, 8.00 /);
2490   tir(1,:) = (/ 14.00, 14.00 /); tir(2,:) = (/ 14.00, 14.00 /); tir(3,:) = (/ 12.00, 4.00 /);
2491   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, til, sil, pres\_l, stable\_l )
2492   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, tir, sir, pres\_r, stable\_r )
2493   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, nk, pres\_l, hl, til, sil, ppoly\_t\_l, ppoly\_s\_l, 
      stable\_l, &
2494            pres\_r, hr, tir, sir, ppoly\_t\_r, ppoly\_s\_r, stable\_r, pol, por, kol, kor, heff)
2495   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or.  test\_nsp(v, 12, kol, kor, pol, por,
       heff, &
2496     (/ 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3 /),  & \textcolor{comment}{! KoL}
2497     (/ 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoR}
2498     (/ 0.00, 1.00, 1.00, 1.00, 1.00, 1.00, 0.00, 1.00, 1.00, 0.00, 1.00, 1.00 /),  & \textcolor{comment}{! PoL}
2499     (/ 0.00, 0.00, 0.00, 1.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.25, 0.50, 1.00 /),  & \textcolor{comment}{! PoR}
2500     (/ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 4.00, 0.00 /),  & \textcolor{comment}{! hEff}
2501     \textcolor{stringliteral}{'Right unstratified column'})
2502 
2503   til(1,:) = (/ 14.00, 14.00 /); til(2,:) = (/ 14.00, 10.00 /); til(3,:) = (/ 10.00, 2.00 /);
2504   tir(1,:) = (/ 14.00, 14.00 /); tir(2,:) = (/ 14.00, 10.00 /); tir(3,:) = (/ 10.00, 2.00 /);
2505   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, til, sil, pres\_l, stable\_l )
2506   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, tir, sir, pres\_r, stable\_r )
2507   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, nk, pres\_l, hl, til, sil, ppoly\_t\_l, ppoly\_s\_l, 
      stable\_l, &
2508            pres\_r, hr, tir, sir, ppoly\_t\_r, ppoly\_s\_r, stable\_r, pol, por, kol, kor, heff)
2509   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or.  test\_nsp(v, 12, kol, kor, pol, por,
       heff, &
2510     (/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoL}
2511     (/ 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoR}
2512     (/ 0.00, 1.00, 1.00, 1.00, 0.00, 0.00, 1.00, 1.00, 0.00, 0.00, 1.00, 1.00 /),  & \textcolor{comment}{! PoL}
2513     (/ 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 1.00, 1.00 /),  & \textcolor{comment}{! PoR}
2514     (/ 0.00, 0.00, 0.00, 0.00, 0.00, 10.00, 0.00, 0.00, 0.00, 10.00, 0.00 /),  & \textcolor{comment}{! hEff}
2515     \textcolor{stringliteral}{'Identical columns with mixed layer'})
2516 
2517   til(1,:) = (/ 14.00, 12.00 /); til(2,:) = (/ 10.00, 10.00 /); til(3,:) = (/ 8.00, 2.00 /);
2518   tir(1,:) = (/ 14.00, 12.00 /); tir(2,:) = (/ 12.00, 8.00 /); tir(3,:) = (/ 8.00, 2.00 /);
2519   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, til, sil, pres\_l, stable\_l )
2520   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, tir, sir, pres\_r, stable\_r )
2521   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, nk, pres\_l, hl, til, sil, ppoly\_t\_l, ppoly\_s\_l, 
      stable\_l, &
2522            pres\_r, hr, tir, sir, ppoly\_t\_r, ppoly\_s\_r, stable\_r, pol, por, kol, kor, heff)
2523   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or.  test\_nsp(v, 12, kol, kor, pol, por,
       heff, &
2524     (/ 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoL}
2525     (/ 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3 /),  & \textcolor{comment}{! KoR}
2526     (/ 0.00, 0.00, 1.00, 1.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 1.00, 1.00 /),  & \textcolor{comment}{! PoL}
2527     (/ 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 1.00, 1.00, 0.00, 1.00, 1.00 /),  & \textcolor{comment}{! PoR}
2528     (/ 0.00, 10.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 10.00, 0.00 /),  & \textcolor{comment}{! hEff}
2529     \textcolor{stringliteral}{'Left interior unstratified'})
2530 
2531   til(1,:) = (/ 12.00, 12.00 /); til(2,:) = (/ 12.00, 10.00 /); til(3,:) = (/ 10.00, 6.00 /);
2532   tir(1,:) = (/ 12.00, 10.00 /); tir(2,:) = (/ 10.00, 12.00 /); tir(3,:) = (/ 8.00, 4.00 /);
2533   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, til, sil, pres\_l, stable\_l )
2534   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, tir, sir, pres\_r, stable\_r )
2535   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, nk, pres\_l, hl, til, sil, ppoly\_t\_l, ppoly\_s\_l, 
      stable\_l, &
2536            pres\_r, hr, tir, sir, ppoly\_t\_r, ppoly\_s\_r, stable\_r, pol, por, kol, kor, heff)
2537   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or.  test\_nsp(v, 12, kol, kor, pol, por,
       heff, &
2538     (/ 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoL}
2539     (/ 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoR}
2540     (/ 0.00, 1.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.50, 1.00, 1.00 /),  & \textcolor{comment}{! PoL}
2541     (/ 0.00, 0.00, 0.00, 0.00, 1.00, 1.00, 0.00, 1.00, 0.00, 0.00, 0.50, 1.00 /),  & \textcolor{comment}{! PoR}
2542     (/ 0.00, 0.00, 0.00, 10.00, 0.00, 0.00, 0.00, 0.00, 0.00, 5.00, 0.00 /),  & \textcolor{comment}{! hEff}
2543     \textcolor{stringliteral}{'Left mixed layer, Right unstable interior'})
2544 
2545   til(1,:) = (/ 14.00, 14.00 /); til(2,:) = (/ 10.00, 10.00 /); til(3,:) = (/ 8.00, 6.00 /);
2546   tir(1,:) = (/ 10.00, 14.00 /); tir(2,:) = (/ 16.00, 16.00 /); tir(3,:) = (/ 12.00, 4.00 /);
2547   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, til, sil, pres\_l, stable\_l )
2548   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, tir, sir, pres\_r, stable\_r )
2549   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, nk, pres\_l, hl, til, sil, ppoly\_t\_l, ppoly\_s\_l, 
      stable\_l, &
2550            pres\_r, hr, tir, sir, ppoly\_t\_r, ppoly\_s\_r, stable\_r, pol, por, kol, kor, heff)
2551   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or.  test\_nsp(v, 12, kol, kor, pol, por,
       heff, &
2552     (/ 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoL}
2553     (/ 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoR}
2554     (/ 0.00, 1.00, 0.00, 1.00, 1.00, 1.00, 1.00, 1.00, 0.00, 0.00, 1.00, 1.00 /),  & \textcolor{comment}{! PoL}
2555     (/ 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 1.00, 0.00, 0.50, 0.75, 1.00 /),  & \textcolor{comment}{! PoR}
2556     (/ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 4.00, 0.00 /),  & \textcolor{comment}{! hEff}
2557     \textcolor{stringliteral}{'Left thick mixed layer, Right unstable mixed'})
2558 
2559   til(1,:) = (/ 8.00, 12.00 /); til(2,:) = (/ 12.00, 10.00 /); til(3,:) = (/ 8.00, 4.00 /);
2560   tir(1,:) = (/ 10.00, 14.00 /); tir(2,:) = (/ 14.00, 12.00 /); tir(3,:) = (/ 10.00, 6.00 /);
2561   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, til, sil, pres\_l, stable\_l )
2562   \textcolor{keyword}{call }mark\_unstable\_cells( cs, nk, tir, sir, pres\_r, stable\_r )
2563   \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, nk, pres\_l, hl, til, sil, ppoly\_t\_l, ppoly\_s\_l, 
      stable\_l, &
2564            pres\_r, hr, tir, sir, ppoly\_t\_r, ppoly\_s\_r, stable\_r, pol, por, kol, kor, heff)
2565   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or.  test\_nsp(v, 12, kol, kor, pol, por,
       heff, &
2566     (/ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoL}
2567     (/ 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3 /),  & \textcolor{comment}{! KoR}
2568     (/ 0.00, 1.00, 1.00, 1.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.50, 1.00 /),  & \textcolor{comment}{! PoL}
2569     (/ 0.00, 0.00, 0.00, 1.00, 0.00, 1.00, 1.00, 0.00, 0.00, 0.50, 1.00, 1.00 /),  & \textcolor{comment}{! PoR}
2570     (/ 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 5.00, 0.00 /),  & \textcolor{comment}{! hEff}
2571     \textcolor{stringliteral}{'Unstable mixed layers, left cooler'})
2572 
2573   \textcolor{keyword}{call }eos\_manual\_init(cs%EOS, form\_of\_eos = eos\_linear, drho\_dt = -1., drho\_ds = 2.)
2574   \textcolor{comment}{! Tests for linearized version of searching the layer for neutral surface position}
2575   \textcolor{comment}{! EOS linear in T, uniform alpha}
2576   cs%max\_iter = 10
2577   \textcolor{comment}{! Unit tests require explicit initialization of tolerance}
2578   cs%Drho\_tol = 0.
2579   cs%x\_tol = 0.
2580   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or. (test\_rnp(0.5, &
2581              find\_neutral\_pos\_linear(cs, 0., 10., 35., -0.2, 0., &
2582                                      -0.2, 0., -0.2, 0.,                     &
2583                                      (/12.,-4./), (/34.,0./)), \textcolor{stringliteral}{"Temp Uniform Linearized Alpha/Beta"}))
2584   \textcolor{comment}{! EOS linear in S, uniform beta}
2585   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or. (test\_rnp(0.5, &
2586              find\_neutral\_pos\_linear(cs, 0., 10., 35., 0., 0.8, &
2587                                      0., 0.8, 0., 0.8,                &
2588                                     (/12.,0./), (/34.,2./)), \textcolor{stringliteral}{"Salt Uniform Linearized Alpha/Beta"}))
2589   \textcolor{comment}{! EOS linear in T/S, uniform alpha/beta}
2590   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or. (test\_rnp(0.5,   &
2591              find\_neutral\_pos\_linear(cs, 0., 10., 35., -0.5, 0.5,                &
2592                                      -0.5, 0.5, -0.5, 0.5,  &
2593                                      (/12.,-4./), (/34.,2./)), \textcolor{stringliteral}{"Temp/salt Uniform Linearized Alpha/Beta"}))
2594   \textcolor{comment}{! EOS linear in T, insensitive to So}
2595   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or. (test\_rnp(0.5, &
2596              find\_neutral\_pos\_linear(cs, 0., 10., 35., -0.2, 0., &
2597                                      -0.4, 0., -0.6, 0.,  &
2598                                      (/12.,-4./), (/34.,0./)), \textcolor{stringliteral}{"Temp stratified Linearized Alpha/Beta"}))
2599   \textcolor{comment}{! EOS linear in S, insensitive to T}
2600   ndiff\_unit\_tests\_discontinuous = ndiff\_unit\_tests\_discontinuous .or. (test\_rnp(0.5, &
2601              find\_neutral\_pos\_linear(cs, 0., 10., 35., 0., 0.8,  &
2602                                       0., 1.0,  0., 0.5,  &
2603                                      (/12.,0./), (/34.,2./)), \textcolor{stringliteral}{"Salt stratified Linearized Alpha/Beta"}))
2604   \textcolor{keywordflow}{if} (.not. ndiff\_unit\_tests\_discontinuous) \textcolor{keyword}{write}(stdout,*) \textcolor{stringliteral}{'Pass'}
2605 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a5469b7be43f8c428f092b58dd5ca41c8}\label{namespacemom__neutral__diffusion_a5469b7be43f8c428f092b58dd5ca41c8}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!neutral\+\_\+diffusion@{neutral\+\_\+diffusion}}
\index{neutral\+\_\+diffusion@{neutral\+\_\+diffusion}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{neutral\+\_\+diffusion()}{neutral\_diffusion()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+neutral\+\_\+diffusion\+::neutral\+\_\+diffusion (\begin{DoxyParamCaption}\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed), intent(in)}]{Coef\+\_\+x,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb), intent(in)}]{Coef\+\_\+y,  }\item[{real, intent(in)}]{dt,  }\item[{type(tracer\+\_\+registry\+\_\+type), pointer}]{Reg,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__neutral__diffusion_1_1neutral__diffusion__cs}{neutral\+\_\+diffusion\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Update tracer concentration due to neutral diffusion; layer thickness unchanged by this update. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & Ocean grid structure\\
\hline
\mbox{\tt in}  & {\em gv} & ocean vertical grid structure\\
\hline
\mbox{\tt in}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em coef\+\_\+x} & dt $\ast$ Kh $\ast$ dy / dx at u-\/points \mbox{[}L2 $\sim$$>$ m2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em coef\+\_\+y} & dt $\ast$ Kh $\ast$ dx / dy at v-\/points \mbox{[}L2 $\sim$$>$ m2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em dt} & Tracer time step $\ast$ I\+\_\+numitts \mbox{[}T $\sim$$>$ s\mbox{]} (I\+\_\+numitts in tracer\+\_\+hordiff)\\
\hline
 & {\em reg} & Tracer registry\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em cs} & Neutral diffusion control structure \\
\hline
\end{DoxyParams}


Definition at line 530 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
530   \textcolor{keywordtype}{type}(ocean\_grid\_type),                     \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{      !< Ocean grid structure}
531   \textcolor{keywordtype}{type}(verticalGrid\_type),                   \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{     !< ocean vertical grid structure}
532   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{      !< Layer thickness [H ~> m or kg m-2]}
533   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))},         \textcolor{keywordtype}{intent(in)}    :: Coef\_x\textcolor{comment}{ !< dt * Kh * dy / dx at u-points [L2
       ~> m2]}
534   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))},         \textcolor{keywordtype}{intent(in)}    :: Coef\_y\textcolor{comment}{ !< dt * Kh * dx / dy at v-points [L2
       ~> m2]}
535   \textcolor{keywordtype}{real},                                      \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{     !< Tracer time step * I\_numitts [T ~>
       s]}
536 \textcolor{comment}{                                                                     !! (I\_numitts in tracer\_hordiff)}
537   \textcolor{keywordtype}{type}(tracer\_registry\_type),                \textcolor{keywordtype}{pointer}       :: Reg\textcolor{comment}{    !< Tracer registry}
538   \textcolor{keywordtype}{type}(unit\_scale\_type),                     \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{     !< A dimensional unit scaling type}
539   \textcolor{keywordtype}{type}(neutral\_diffusion\_CS),                \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{     !< Neutral diffusion control structure}
540 
541   \textcolor{comment}{! Local variables}
542   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),CS%nsurf-1)} :: uFlx        \textcolor{comment}{! Zonal flux of tracer [H conc ~> m conc or
       conc kg m-2]}
543   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),CS%nsurf-1)} :: vFlx        \textcolor{comment}{! Meridional flux of tracer}
544                                                               \textcolor{comment}{! [H conc ~> m conc or conc kg m-2]}
545   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),G%ke)}        :: tendency    \textcolor{comment}{! tendency array for diagn}
546   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))}             :: tendency\_2d \textcolor{comment}{! depth integrated content tendency for diagn}
547   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))}            :: trans\_x\_2d  \textcolor{comment}{! depth integrated diffusive tracer
       x-transport diagn}
548   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))}            :: trans\_y\_2d  \textcolor{comment}{! depth integrated diffusive tracer
       y-transport diagn}
549   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(G%ke)}                        :: dTracer     \textcolor{comment}{! change in tracer concentration due to
       ndiffusion}
550 
551   \textcolor{keywordtype}{type}(tracer\_type), \textcolor{keywordtype}{pointer}                   :: Tracer => null() \textcolor{comment}{! Pointer to the current tracer}
552 
553   \textcolor{keywordtype}{integer} :: i, j, k, m, ks, nk
554   \textcolor{keywordtype}{real} :: Idt  \textcolor{comment}{! The inverse of the time step [T-1 ~> s-1]}
555   \textcolor{keywordtype}{real} :: h\_neglect, h\_neglect\_edge
556 
557   \textcolor{keywordflow}{if} (.not.cs%remap\_answers\_2018) \textcolor{keywordflow}{then}
558     h\_neglect = gv%H\_subroundoff ; h\_neglect\_edge = gv%H\_subroundoff
559   \textcolor{keywordflow}{else}
560     h\_neglect = gv%m\_to\_H*1.0e-30 ; h\_neglect\_edge = gv%m\_to\_H*1.0e-10
561 \textcolor{keywordflow}{  endif}
562 
563   nk = gv%ke
564 
565   \textcolor{keywordflow}{do} m = 1,reg%ntr \textcolor{comment}{! Loop over tracer registry}
566 
567     tracer => reg%Tr(m)
568 
569     \textcolor{comment}{! for diagnostics}
570     \textcolor{keywordflow}{if} (tracer%id\_dfxy\_conc > 0 .or. tracer%id\_dfxy\_cont > 0 .or. tracer%id\_dfxy\_cont\_2d > 0 .or. &
571         tracer%id\_dfx\_2d > 0 .or. tracer%id\_dfy\_2d > 0) \textcolor{keywordflow}{then}
572       idt = 1.0 / dt
573       tendency(:,:,:)  = 0.0
574 \textcolor{keywordflow}{    endif}
575 
576     uflx(:,:,:) = 0.
577     vflx(:,:,:) = 0.
578 
579     \textcolor{comment}{! x-flux}
580     \textcolor{keywordflow}{do} j = g%jsc,g%jec ; \textcolor{keywordflow}{do} i = g%isc-1,g%iec
581       \textcolor{keywordflow}{if} (g%mask2dCu(i,j)>0.) \textcolor{keywordflow}{then}
582         \textcolor{keyword}{call }neutral\_surface\_flux(nk, cs%nsurf, cs%deg, h(i,j,:), h(i+1,j,:),       &
583                                   tracer%t(i,j,:), tracer%t(i+1,j,:), &
584                                   cs%uPoL(i,j,:), cs%uPoR(i,j,:), &
585                                   cs%uKoL(i,j,:), cs%uKoR(i,j,:), &
586                                   cs%uhEff(i,j,:), uflx(i,j,:), &
587                                   cs%continuous\_reconstruction, h\_neglect, cs%remap\_CS, h\_neglect\_edge)
588 \textcolor{keywordflow}{      endif}
589 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
590 
591     \textcolor{comment}{! y-flux}
592     \textcolor{keywordflow}{do} j = g%jsc-1,g%jec ; \textcolor{keywordflow}{do} i = g%isc,g%iec
593       \textcolor{keywordflow}{if} (g%mask2dCv(i,j)>0.) \textcolor{keywordflow}{then}
594         \textcolor{keyword}{call }neutral\_surface\_flux(nk, cs%nsurf, cs%deg, h(i,j,:), h(i,j+1,:),       &
595                                   tracer%t(i,j,:), tracer%t(i,j+1,:), &
596                                   cs%vPoL(i,j,:), cs%vPoR(i,j,:), &
597                                   cs%vKoL(i,j,:), cs%vKoR(i,j,:), &
598                                   cs%vhEff(i,j,:), vflx(i,j,:),   &
599                                   cs%continuous\_reconstruction, h\_neglect, cs%remap\_CS, h\_neglect\_edge)
600 \textcolor{keywordflow}{      endif}
601 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
602 
603     \textcolor{comment}{! Update the tracer concentration from divergence of neutral diffusive flux components}
604     \textcolor{keywordflow}{do} j = g%jsc,g%jec ; \textcolor{keywordflow}{do} i = g%isc,g%iec
605       \textcolor{keywordflow}{if} (g%mask2dT(i,j)>0.) \textcolor{keywordflow}{then}
606 
607         dtracer(:) = 0.
608         \textcolor{keywordflow}{do} ks = 1,cs%nsurf-1
609           k = cs%uKoL(i,j,ks)
610           dtracer(k) = dtracer(k) + coef\_x(i,j)   * uflx(i,j,ks)
611           k = cs%uKoR(i-1,j,ks)
612           dtracer(k) = dtracer(k) - coef\_x(i-1,j) * uflx(i-1,j,ks)
613           k = cs%vKoL(i,j,ks)
614           dtracer(k) = dtracer(k) + coef\_y(i,j)   * vflx(i,j,ks)
615           k = cs%vKoR(i,j-1,ks)
616           dtracer(k) = dtracer(k) - coef\_y(i,j-1) * vflx(i,j-1,ks)
617 \textcolor{keywordflow}{        enddo}
618         \textcolor{keywordflow}{do} k = 1, gv%ke
619           tracer%t(i,j,k) = tracer%t(i,j,k) + dtracer(k) * &
620                           ( g%IareaT(i,j) / ( h(i,j,k) + gv%H\_subroundoff ) )
621 \textcolor{keywordflow}{        enddo}
622 
623         \textcolor{keywordflow}{if} (tracer%id\_dfxy\_conc > 0  .or. tracer%id\_dfxy\_cont > 0 .or. tracer%id\_dfxy\_cont\_2d > 0 ) \textcolor{keywordflow}{then}
624           \textcolor{keywordflow}{do} k = 1, gv%ke
625             tendency(i,j,k) = dtracer(k) * g%IareaT(i,j) * idt
626 \textcolor{keywordflow}{          enddo}
627 \textcolor{keywordflow}{        endif}
628 
629 \textcolor{keywordflow}{      endif}
630 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
631 
632     \textcolor{comment}{! Diagnose vertically summed zonal flux, giving zonal tracer transport from ndiff.}
633     \textcolor{comment}{! Note sign corresponds to downgradient flux convention.}
634     \textcolor{keywordflow}{if} (tracer%id\_dfx\_2d > 0) \textcolor{keywordflow}{then}
635       \textcolor{keywordflow}{do} j = g%jsc,g%jec ; \textcolor{keywordflow}{do} i = g%isc-1,g%iec
636         trans\_x\_2d(i,j) = 0.
637         \textcolor{keywordflow}{if} (g%mask2dCu(i,j)>0.) \textcolor{keywordflow}{then}
638           \textcolor{keywordflow}{do} ks = 1,cs%nsurf-1
639             trans\_x\_2d(i,j) = trans\_x\_2d(i,j) - coef\_x(i,j) * uflx(i,j,ks)
640 \textcolor{keywordflow}{          enddo}
641           trans\_x\_2d(i,j) = trans\_x\_2d(i,j) * idt
642 \textcolor{keywordflow}{        endif}
643 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
644       \textcolor{keyword}{call }post\_data(tracer%id\_dfx\_2d, trans\_x\_2d(:,:), cs%diag)
645 \textcolor{keywordflow}{    endif}
646 
647     \textcolor{comment}{! Diagnose vertically summed merid flux, giving meridional tracer transport from ndiff.}
648     \textcolor{comment}{! Note sign corresponds to downgradient flux convention.}
649     \textcolor{keywordflow}{if} (tracer%id\_dfy\_2d > 0) \textcolor{keywordflow}{then}
650       \textcolor{keywordflow}{do} j = g%jsc-1,g%jec ; \textcolor{keywordflow}{do} i = g%isc,g%iec
651         trans\_y\_2d(i,j) = 0.
652         \textcolor{keywordflow}{if} (g%mask2dCv(i,j)>0.) \textcolor{keywordflow}{then}
653           \textcolor{keywordflow}{do} ks = 1,cs%nsurf-1
654             trans\_y\_2d(i,j) = trans\_y\_2d(i,j) - coef\_y(i,j) * vflx(i,j,ks)
655 \textcolor{keywordflow}{          enddo}
656           trans\_y\_2d(i,j) = trans\_y\_2d(i,j) * idt
657 \textcolor{keywordflow}{        endif}
658 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
659       \textcolor{keyword}{call }post\_data(tracer%id\_dfy\_2d, trans\_y\_2d(:,:), cs%diag)
660 \textcolor{keywordflow}{    endif}
661 
662     \textcolor{comment}{! post tendency of tracer content}
663     \textcolor{keywordflow}{if} (tracer%id\_dfxy\_cont > 0) \textcolor{keywordflow}{then}
664       \textcolor{keyword}{call }post\_data(tracer%id\_dfxy\_cont, tendency(:,:,:), cs%diag)
665 \textcolor{keywordflow}{    endif}
666 
667     \textcolor{comment}{! post depth summed tendency for tracer content}
668     \textcolor{keywordflow}{if} (tracer%id\_dfxy\_cont\_2d > 0) \textcolor{keywordflow}{then}
669       tendency\_2d(:,:) = 0.
670       \textcolor{keywordflow}{do} j = g%jsc,g%jec ; \textcolor{keywordflow}{do} i = g%isc,g%iec
671         \textcolor{keywordflow}{do} k = 1, gv%ke
672           tendency\_2d(i,j) = tendency\_2d(i,j) + tendency(i,j,k)
673 \textcolor{keywordflow}{        enddo}
674 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
675       \textcolor{keyword}{call }post\_data(tracer%id\_dfxy\_cont\_2d, tendency\_2d(:,:), cs%diag)
676 \textcolor{keywordflow}{    endif}
677 
678     \textcolor{comment}{! post tendency of tracer concentration; this step must be}
679     \textcolor{comment}{! done after posting tracer content tendency, since we alter}
680     \textcolor{comment}{! the tendency array.}
681     \textcolor{keywordflow}{if} (tracer%id\_dfxy\_conc > 0) \textcolor{keywordflow}{then}
682       \textcolor{keywordflow}{do} k = 1, gv%ke ; \textcolor{keywordflow}{do} j = g%jsc,g%jec ; \textcolor{keywordflow}{do} i = g%isc,g%iec
683         tendency(i,j,k) =  tendency(i,j,k) / ( h(i,j,k) + gv%H\_subroundoff )
684 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
685       \textcolor{keyword}{call }post\_data(tracer%id\_dfxy\_conc, tendency, cs%diag)
686 \textcolor{keywordflow}{    endif}
687 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! Loop over tracer registry}
688 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_abc3fd398047ffb61825ceced9ae5ff41}\label{namespacemom__neutral__diffusion_abc3fd398047ffb61825ceced9ae5ff41}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!neutral\+\_\+diffusion\+\_\+calc\+\_\+coeffs@{neutral\+\_\+diffusion\+\_\+calc\+\_\+coeffs}}
\index{neutral\+\_\+diffusion\+\_\+calc\+\_\+coeffs@{neutral\+\_\+diffusion\+\_\+calc\+\_\+coeffs}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{neutral\+\_\+diffusion\+\_\+calc\+\_\+coeffs()}{neutral\_diffusion\_calc\_coeffs()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+neutral\+\_\+diffusion\+::neutral\+\_\+diffusion\+\_\+calc\+\_\+coeffs (\begin{DoxyParamCaption}\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{T,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{S,  }\item[{type(\mbox{\hyperlink{structmom__neutral__diffusion_1_1neutral__diffusion__cs}{neutral\+\_\+diffusion\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed), intent(in), optional}]{p\+\_\+surf }\end{DoxyParamCaption})}



Calculate remapping factors for u/v columns used to map adjoining columns to a shared coordinate space. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em g} & Ocean grid structure\\
\hline
\mbox{\tt in}  & {\em gv} & ocean vertical grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em t} & Potential temperature \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s} & Salinity \mbox{[}ppt\mbox{]}\\
\hline
 & {\em cs} & Neutral diffusion control structure\\
\hline
\mbox{\tt in}  & {\em p\+\_\+surf} & Surface pressure to include in pressures used for equation of state calculations \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 284 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
284   \textcolor{keywordtype}{type}(ocean\_grid\_type),                    \textcolor{keywordtype}{intent(in)} :: G\textcolor{comment}{   !< Ocean grid structure}
285   \textcolor{keywordtype}{type}(verticalGrid\_type),                  \textcolor{keywordtype}{intent(in)} :: GV\textcolor{comment}{  !< ocean vertical grid structure}
286   \textcolor{keywordtype}{type}(unit\_scale\_type),                    \textcolor{keywordtype}{intent(in)} :: US\textcolor{comment}{  !< A dimensional unit scaling type}
287   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)} :: h\textcolor{comment}{   !< Layer thickness [H ~> m or kg m-2]}
288   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)} :: T\textcolor{comment}{   !< Potential temperature [degC]}
289   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)} :: S\textcolor{comment}{   !< Salinity [ppt]}
290   \textcolor{keywordtype}{type}(neutral\_diffusion\_CS),               \textcolor{keywordtype}{pointer}    :: CS\textcolor{comment}{  !< Neutral diffusion control structure}
291   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))}, \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)} :: p\_surf\textcolor{comment}{ !< Surface pressure to include in
       pressures used}
292 \textcolor{comment}{                                                              !! for equation of state calculations [R L2
       T-2 ~> Pa]}
293 
294   \textcolor{comment}{! Local variables}
295   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: EOSdom \textcolor{comment}{! The i-computational domain for the equation of state}
296   \textcolor{keywordtype}{integer} :: i, j, k
297   \textcolor{comment}{! Variables used for reconstructions}
298   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZK\_(G),2)} :: ppoly\_r\_S       \textcolor{comment}{! Reconstruction slopes}
299   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G), SZJ\_(G))} :: hEff\_sum \textcolor{comment}{! Summed effective face thicknesses [H ~> m or kg m-2]}
300   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))}  :: hbl      \textcolor{comment}{! Boundary layer depth [H ~> m or kg m-2]}
301   \textcolor{keywordtype}{integer} :: iMethod
302   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: ref\_pres \textcolor{comment}{! Reference pressure used to calculate alpha/beta [R L2 T-2 ~> Pa]}
303   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G))} :: rho\_tmp  \textcolor{comment}{! Routine to calculate drho\_dp, returns density which is not used}
304   \textcolor{keywordtype}{real} :: h\_neglect, h\_neglect\_edge    \textcolor{comment}{! Negligible thicknesses [H ~> m or kg m-2]}
305   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G), SZJ\_(G))} :: k\_top  \textcolor{comment}{! Index of the first layer within the boundary}
306   \textcolor{keywordtype}{real},    \textcolor{keywordtype}{dimension(SZI\_(G), SZJ\_(G))} :: zeta\_top \textcolor{comment}{! Distance from the top of a layer to the intersection
       of the}
307                                                    \textcolor{comment}{! top extent of the boundary layer (0 at top, 1 at
       bottom) [nondim]}
308   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G), SZJ\_(G))} :: k\_bot    \textcolor{comment}{! Index of the last layer within the boundary}
309   \textcolor{keywordtype}{real},    \textcolor{keywordtype}{dimension(SZI\_(G), SZJ\_(G))} :: zeta\_bot \textcolor{comment}{! Distance of the lower layer to the boundary layer
       depth}
310   \textcolor{keywordtype}{real} :: pa\_to\_H                      \textcolor{comment}{! A conversion factor from pressure to H units [H T2 R-1 Z-2 ~> m
       Pa-1 or s2 m-2]}
311 
312   pa\_to\_h = 1. / (gv%H\_to\_RZ * gv%g\_Earth)
313 
314   k\_top(:,:) = 1     ; k\_bot(:,:) = 1
315   zeta\_top(:,:) = 0. ; zeta\_bot(:,:) = 0.
316 
317   \textcolor{comment}{! Check if hbl needs to be extracted}
318   \textcolor{keywordflow}{if} (cs%interior\_only) \textcolor{keywordflow}{then}
319     hbl(:,:) = 0.
320     \textcolor{keywordflow}{if} (\textcolor{keyword}{ASSOCIATED}(cs%KPP\_CSp)) \textcolor{keyword}{call }kpp\_get\_bld(cs%KPP\_CSp, hbl, g, us, m\_to\_bld\_units=gv%m\_to\_H)
321     \textcolor{keywordflow}{if} (\textcolor{keyword}{ASSOCIATED}(cs%energetic\_PBL\_CSp)) &
322       \textcolor{keyword}{call }energetic\_pbl\_get\_mld(cs%energetic\_PBL\_CSp, hbl, g, us, m\_to\_mld\_units=gv%m\_to\_H)
323     \textcolor{keyword}{call }pass\_var(hbl, g%Domain)
324     \textcolor{comment}{! get k-indices and zeta}
325     \textcolor{keywordflow}{do} j=g%jsc-1, g%jec+1 ; \textcolor{keywordflow}{do} i=g%isc-1,g%iec+1
326       \textcolor{keyword}{call }boundary\_k\_range(surface, g%ke, h(i,j,:), hbl(i,j), k\_top(i,j), zeta\_top(i,j), k\_bot(i,j), 
      zeta\_bot(i,j))
327 \textcolor{keyword}{    end}do; enddo
328     \textcolor{comment}{! TODO: add similar code for BOTTOM boundary layer}
329 \textcolor{keywordflow}{  endif}
330 
331   \textcolor{keywordflow}{if} (.not.cs%remap\_answers\_2018) \textcolor{keywordflow}{then}
332     h\_neglect = gv%H\_subroundoff ; h\_neglect\_edge = gv%H\_subroundoff
333   \textcolor{keywordflow}{elseif} (gv%Boussinesq) \textcolor{keywordflow}{then}
334     h\_neglect = gv%m\_to\_H*1.0e-30 ; h\_neglect\_edge = gv%m\_to\_H*1.0e-10
335   \textcolor{keywordflow}{else}
336     h\_neglect = gv%kg\_m2\_to\_H*1.0e-30 ; h\_neglect\_edge = gv%kg\_m2\_to\_H*1.0e-10
337 \textcolor{keywordflow}{  endif}
338 
339   \textcolor{comment}{! If doing along isopycnal diffusion (as opposed to neutral diffusion, set the reference pressure)}
340   \textcolor{keywordflow}{if} (cs%ref\_pres>=0.) \textcolor{keywordflow}{then}
341     ref\_pres(:) = cs%ref\_pres
342 \textcolor{keywordflow}{  endif}
343 
344   \textcolor{keywordflow}{if} (cs%continuous\_reconstruction) \textcolor{keywordflow}{then}
345     cs%dRdT(:,:,:) = 0.
346     cs%dRdS(:,:,:) = 0.
347   \textcolor{keywordflow}{else}
348     cs%T\_i(:,:,:,:) = 0.
349     cs%S\_i(:,:,:,:) = 0.
350     cs%dRdT\_i(:,:,:,:) = 0.
351     cs%dRdS\_i(:,:,:,:) = 0.
352     cs%ns(:,:) = 0.
353     cs%stable\_cell(:,:,:) = .true.
354 \textcolor{keywordflow}{  endif}
355 
356   \textcolor{comment}{! Calculate pressure at interfaces and layer averaged alpha/beta}
357   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(p\_surf)) \textcolor{keywordflow}{then}
358      \textcolor{keywordflow}{do} j=g%jsc-1,g%jec+1 ; \textcolor{keywordflow}{do} i=g%isc-1,g%iec+1
359        cs%Pint(i,j,1) = p\_surf(i,j)
360 \textcolor{keywordflow}{     enddo} ;\textcolor{keywordflow}{ enddo}
361   \textcolor{keywordflow}{else}
362     cs%Pint(:,:,1) = 0.
363 \textcolor{keywordflow}{  endif}
364   \textcolor{keywordflow}{do} k=1,g%ke ; \textcolor{keywordflow}{do} j=g%jsc-1,g%jec+1 ; \textcolor{keywordflow}{do} i=g%isc-1,g%iec+1
365     cs%Pint(i,j,k+1) = cs%Pint(i,j,k) + h(i,j,k)*(gv%g\_Earth*gv%H\_to\_RZ)
366 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
367 
368   \textcolor{comment}{! Pressures at the interfaces, this is redundant as P\_i(k,1) = P\_i(k-1,2) however retain this}
369   \textcolor{comment}{! for now to ensure consitency of indexing for diiscontinuous reconstructions}
370   \textcolor{keywordflow}{if} (.not. cs%continuous\_reconstruction) \textcolor{keywordflow}{then}
371     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(p\_surf)) \textcolor{keywordflow}{then}
372       \textcolor{keywordflow}{do} j=g%jsc-1,g%jec+1 ; \textcolor{keywordflow}{do} i=g%isc-1,g%iec+1
373         cs%P\_i(i,j,1,1) = p\_surf(i,j)
374         cs%P\_i(i,j,1,2) = p\_surf(i,j) + h(i,j,1)*(gv%H\_to\_RZ*gv%g\_Earth)
375 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
376     \textcolor{keywordflow}{else}
377       \textcolor{keywordflow}{do} j=g%jsc-1,g%jec+1 ; \textcolor{keywordflow}{do} i=g%isc-1,g%iec+1
378         cs%P\_i(i,j,1,1) = 0.
379         cs%P\_i(i,j,1,2) = h(i,j,1)*(gv%H\_to\_RZ*gv%g\_Earth)
380 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
381 \textcolor{keywordflow}{    endif}
382     \textcolor{keywordflow}{do} k=2,g%ke ; \textcolor{keywordflow}{do} j=g%jsc-1,g%jec+1 ; \textcolor{keywordflow}{do} i=g%isc-1,g%iec+1
383       cs%P\_i(i,j,k,1) = cs%P\_i(i,j,k-1,2)
384       cs%P\_i(i,j,k,2) = cs%P\_i(i,j,k-1,2) + h(i,j,k)*(gv%H\_to\_RZ*gv%g\_Earth)
385 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
386 \textcolor{keywordflow}{  endif}
387 
388   eosdom(:) = eos\_domain(g%HI, halo=1)
389   \textcolor{keywordflow}{do} j = g%jsc-1, g%jec+1
390     \textcolor{comment}{! Interpolate state to interface}
391     \textcolor{keywordflow}{do} i = g%isc-1, g%iec+1
392       \textcolor{keywordflow}{if} (cs%continuous\_reconstruction) \textcolor{keywordflow}{then}
393         \textcolor{keyword}{call }interface\_scalar(g%ke, h(i,j,:), t(i,j,:), cs%Tint(i,j,:), 2, h\_neglect)
394         \textcolor{keyword}{call }interface\_scalar(g%ke, h(i,j,:), s(i,j,:), cs%Sint(i,j,:), 2, h\_neglect)
395       \textcolor{keywordflow}{else}
396         \textcolor{keyword}{call }build\_reconstructions\_1d( cs%remap\_CS, g%ke, h(i,j,:), t(i,j,:), cs%ppoly\_coeffs\_T(i,j,:,:), &
397                                        cs%T\_i(i,j,:,:), ppoly\_r\_s, imethod, h\_neglect, h\_neglect\_edge )
398         \textcolor{keyword}{call }build\_reconstructions\_1d( cs%remap\_CS, g%ke, h(i,j,:), s(i,j,:), cs%ppoly\_coeffs\_S(i,j,:,:), &
399                                        cs%S\_i(i,j,:,:), ppoly\_r\_s, imethod, h\_neglect, h\_neglect\_edge )
400         \textcolor{comment}{! In the current ALE formulation, interface values are not exactly at the 0. or 1. of the}
401         \textcolor{comment}{! polynomial reconstructions}
402         \textcolor{keywordflow}{do} k=1,g%ke
403            cs%T\_i(i,j,k,1) = evaluation\_polynomial( cs%ppoly\_coeffs\_T(i,j,k,:), cs%deg+1, 0. )
404            cs%T\_i(i,j,k,2) = evaluation\_polynomial( cs%ppoly\_coeffs\_T(i,j,k,:), cs%deg+1, 1. )
405            cs%S\_i(i,j,k,1) = evaluation\_polynomial( cs%ppoly\_coeffs\_S(i,j,k,:), cs%deg+1, 0. )
406            cs%S\_i(i,j,k,2) = evaluation\_polynomial( cs%ppoly\_coeffs\_S(i,j,k,:), cs%deg+1, 1. )
407 \textcolor{keywordflow}{        enddo}
408 \textcolor{keywordflow}{      endif}
409 \textcolor{keywordflow}{    enddo}
410 
411     \textcolor{comment}{! Continuous reconstruction}
412     \textcolor{keywordflow}{if} (cs%continuous\_reconstruction) \textcolor{keywordflow}{then}
413       \textcolor{keywordflow}{do} k = 1, g%ke+1
414         \textcolor{keywordflow}{if} (cs%ref\_pres<0) ref\_pres(:) = cs%Pint(:,j,k)
415         \textcolor{keyword}{call }calculate\_density\_derivs(cs%Tint(:,j,k), cs%Sint(:,j,k), ref\_pres, cs%dRdT(:,j,k), &
416                                       cs%dRdS(:,j,k), cs%EOS, eosdom)
417 \textcolor{keywordflow}{      enddo}
418     \textcolor{keywordflow}{else} \textcolor{comment}{! Discontinuous reconstruction}
419       \textcolor{keywordflow}{do} k = 1, g%ke
420         \textcolor{keywordflow}{if} (cs%ref\_pres<0) ref\_pres(:) = cs%Pint(:,j,k)
421         \textcolor{comment}{! Calculate derivatives for the top interface}
422         \textcolor{keyword}{call }calculate\_density\_derivs(cs%T\_i(:,j,k,1), cs%S\_i(:,j,k,1), ref\_pres, cs%dRdT\_i(:,j,k,1), &
423                                       cs%dRdS\_i(:,j,k,1), cs%EOS, eosdom)
424         \textcolor{keywordflow}{if} (cs%ref\_pres<0) ref\_pres(:) = cs%Pint(:,j,k+1)
425         \textcolor{comment}{! Calculate derivatives at the bottom interface}
426         \textcolor{keyword}{call }calculate\_density\_derivs(cs%T\_i(:,j,k,2), cs%S\_i(:,j,k,2), ref\_pres, cs%dRdT\_i(:,j,k,2), &
427                                       cs%dRdS\_i(:,j,k,2), cs%EOS, eosdom)
428 \textcolor{keywordflow}{      enddo}
429 \textcolor{keywordflow}{    endif}
430 \textcolor{keywordflow}{  enddo}
431 
432   \textcolor{keywordflow}{if} (.not. cs%continuous\_reconstruction) \textcolor{keywordflow}{then}
433     \textcolor{keywordflow}{do} j = g%jsc-1, g%jec+1 ; \textcolor{keywordflow}{do} i = g%isc-1, g%iec+1
434       \textcolor{keyword}{call }mark\_unstable\_cells( cs, g%ke, cs%T\_i(i,j,:,:), cs%S\_i(i,j,:,:), cs%P\_i(i,j,:,:), cs%stable\_cell
      (i,j,:) )
435       \textcolor{keywordflow}{if} (cs%interior\_only) \textcolor{keywordflow}{then}
436         \textcolor{keywordflow}{if} (.not. cs%stable\_cell(i,j,k\_bot(i,j))) zeta\_bot(i,j) = -1.
437         \textcolor{comment}{! set values in the surface and bottom boundary layer to false.}
438         \textcolor{keywordflow}{do} k = 1, k\_bot(i,j)-1
439           cs%stable\_cell(i,j,k) = .false.
440 \textcolor{keywordflow}{        enddo}
441 \textcolor{keywordflow}{      endif}
442 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
443 \textcolor{keywordflow}{  endif}
444 
445   cs%uhEff(:,:,:) = 0.
446   cs%vhEff(:,:,:) = 0.
447   cs%uPoL(:,:,:) = 0.
448   cs%vPoL(:,:,:) = 0.
449   cs%uPoR(:,:,:) = 0.
450   cs%vPoR(:,:,:) = 0.
451   cs%uKoL(:,:,:) = 1
452   cs%vKoL(:,:,:) = 1
453   cs%uKoR(:,:,:) = 1
454   cs%vKoR(:,:,:) = 1
455 
456   \textcolor{comment}{! Neutral surface factors at U points}
457   \textcolor{keywordflow}{do} j = g%jsc, g%jec ; \textcolor{keywordflow}{do} i = g%isc-1, g%iec
458     \textcolor{keywordflow}{if} (g%mask2dCu(i,j) > 0.) \textcolor{keywordflow}{then}
459       \textcolor{keywordflow}{if} (cs%continuous\_reconstruction) \textcolor{keywordflow}{then}
460         \textcolor{keyword}{call }find\_neutral\_surface\_positions\_continuous(g%ke,                                    &
461                 cs%Pint(i,j,:), cs%Tint(i,j,:), cs%Sint(i,j,:), cs%dRdT(i,j,:), cs%dRdS(i,j,:),            
      &
462                 cs%Pint(i+1,j,:), cs%Tint(i+1,j,:), cs%Sint(i+1,j,:), cs%dRdT(i+1,j,:), cs%dRdS(i+1,j,:),  
      &
463                 cs%uPoL(i,j,:), cs%uPoR(i,j,:), cs%uKoL(i,j,:), cs%uKoR(i,j,:), cs%uhEff(i,j,:),           
      &
464                 k\_bot(i,j), k\_bot(i+1,j), zeta\_bot(i,j), zeta\_bot(i+1,j))
465       \textcolor{keywordflow}{else}
466         \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, g%ke, &
467             cs%P\_i(i,j,:,:), h(i,j,:), cs%T\_i(i,j,:,:), cs%S\_i(i,j,:,:), cs%ppoly\_coeffs\_T(i,j,:,:),       
          &
468             cs%ppoly\_coeffs\_S(i,j,:,:),cs%stable\_cell(i,j,:),                                              
          &
469             cs%P\_i(i+1,j,:,:), h(i+1,j,:), cs%T\_i(i+1,j,:,:), cs%S\_i(i+1,j,:,:), cs%ppoly\_coeffs\_T(i+1,j,:,
      :), &
470             cs%ppoly\_coeffs\_S(i+1,j,:,:), cs%stable\_cell(i+1,j,:),                                         
          &
471             cs%uPoL(i,j,:), cs%uPoR(i,j,:), cs%uKoL(i,j,:), cs%uKoR(i,j,:), cs%uhEff(i,j,:),               
          &
472             hard\_fail\_heff = cs%hard\_fail\_heff)
473 \textcolor{keywordflow}{      endif}
474 \textcolor{keywordflow}{    endif}
475 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
476 
477   \textcolor{comment}{! Neutral surface factors at V points}
478   \textcolor{keywordflow}{do} j = g%jsc-1, g%jec ; \textcolor{keywordflow}{do} i = g%isc, g%iec
479     \textcolor{keywordflow}{if} (g%mask2dCv(i,j) > 0.) \textcolor{keywordflow}{then}
480       \textcolor{keywordflow}{if} (cs%continuous\_reconstruction) \textcolor{keywordflow}{then}
481         \textcolor{keyword}{call }find\_neutral\_surface\_positions\_continuous(g%ke,                                              &
482                 cs%Pint(i,j,:), cs%Tint(i,j,:), cs%Sint(i,j,:), cs%dRdT(i,j,:), cs%dRdS(i,j,:),           &
483                 cs%Pint(i,j+1,:), cs%Tint(i,j+1,:), cs%Sint(i,j+1,:), cs%dRdT(i,j+1,:), cs%dRdS(i,j+1,:), &
484                 cs%vPoL(i,j,:), cs%vPoR(i,j,:), cs%vKoL(i,j,:), cs%vKoR(i,j,:), cs%vhEff(i,j,:), &
485                 k\_bot(i,j), k\_bot(i,j+1), zeta\_bot(i,j), zeta\_bot(i,j+1))
486       \textcolor{keywordflow}{else}
487         \textcolor{keyword}{call }find\_neutral\_surface\_positions\_discontinuous(cs, g%ke, &
488             cs%P\_i(i,j,:,:), h(i,j,:), cs%T\_i(i,j,:,:), cs%S\_i(i,j,:,:), cs%ppoly\_coeffs\_T(i,j,:,:),       
          &
489             cs%ppoly\_coeffs\_S(i,j,:,:),cs%stable\_cell(i,j,:),                                              
          &
490             cs%P\_i(i,j+1,:,:), h(i,j+1,:), cs%T\_i(i,j+1,:,:), cs%S\_i(i,j+1,:,:), cs%ppoly\_coeffs\_T(i,j+1,:,
      :), &
491             cs%ppoly\_coeffs\_S(i,j+1,:,:), cs%stable\_cell(i,j+1,:),                                         
          &
492             cs%vPoL(i,j,:), cs%vPoR(i,j,:), cs%vKoL(i,j,:), cs%vKoR(i,j,:), cs%vhEff(i,j,:),               
          &
493             hard\_fail\_heff = cs%hard\_fail\_heff)
494 \textcolor{keywordflow}{      endif}
495 \textcolor{keywordflow}{    endif}
496 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
497 
498   \textcolor{comment}{! Continuous reconstructions calculate hEff as the difference between the pressures of the}
499   \textcolor{comment}{! neutral surfaces which need to be reconverted to thickness units. The discontinuous version}
500   \textcolor{comment}{! calculates hEff from the nondimensional fraction of the layer spanned by adjacent neutral}
501   \textcolor{comment}{! surfaces, so hEff is already in thickness units.}
502   \textcolor{keywordflow}{if} (cs%continuous\_reconstruction) \textcolor{keywordflow}{then}
503     \textcolor{keywordflow}{do} k = 1, cs%nsurf-1 ; \textcolor{keywordflow}{do} j = g%jsc, g%jec ; \textcolor{keywordflow}{do} i = g%isc-1, g%iec
504       \textcolor{keywordflow}{if} (g%mask2dCu(i,j) > 0.) cs%uhEff(i,j,k) = cs%uhEff(i,j,k) * pa\_to\_h
505 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
506     \textcolor{keywordflow}{do} k = 1, cs%nsurf-1 ; \textcolor{keywordflow}{do} j = g%jsc-1, g%jec ; \textcolor{keywordflow}{do} i = g%isc, g%iec
507       \textcolor{keywordflow}{if} (g%mask2dCv(i,j) > 0.) cs%vhEff(i,j,k) = cs%vhEff(i,j,k) * pa\_to\_h
508 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
509 \textcolor{keywordflow}{  endif}
510 
511   \textcolor{keywordflow}{if} (cs%id\_uhEff\_2d>0) \textcolor{keywordflow}{then}
512     heff\_sum(:,:) = 0.
513     \textcolor{keywordflow}{do} k = 1,cs%nsurf-1 ; \textcolor{keywordflow}{do} j=g%jsc,g%jec ; \textcolor{keywordflow}{do} i=g%isc-1,g%iec
514       heff\_sum(i,j) = heff\_sum(i,j) + cs%uhEff(i,j,k)
515 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
516     \textcolor{keyword}{call }post\_data(cs%id\_uhEff\_2d, heff\_sum, cs%diag)
517 \textcolor{keywordflow}{  endif}
518   \textcolor{keywordflow}{if} (cs%id\_vhEff\_2d>0) \textcolor{keywordflow}{then}
519     heff\_sum(:,:) = 0.
520     \textcolor{keywordflow}{do} k = 1,cs%nsurf-1 ; \textcolor{keywordflow}{do} j=g%jsc-1,g%jec ; \textcolor{keywordflow}{do} i=g%isc,g%iec
521       heff\_sum(i,j) = heff\_sum(i,j) + cs%vhEff(i,j,k)
522 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
523     \textcolor{keyword}{call }post\_data(cs%id\_vhEff\_2d, heff\_sum, cs%diag)
524 \textcolor{keywordflow}{  endif}
525 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_ae8a313d25818dfa8c3bc056210fac76b}\label{namespacemom__neutral__diffusion_ae8a313d25818dfa8c3bc056210fac76b}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!neutral\+\_\+diffusion\+\_\+end@{neutral\+\_\+diffusion\+\_\+end}}
\index{neutral\+\_\+diffusion\+\_\+end@{neutral\+\_\+diffusion\+\_\+end}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{neutral\+\_\+diffusion\+\_\+end()}{neutral\_diffusion\_end()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+neutral\+\_\+diffusion\+::neutral\+\_\+diffusion\+\_\+end (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__neutral__diffusion_1_1neutral__diffusion__cs}{neutral\+\_\+diffusion\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Deallocates neutral\+\_\+diffusion control structure. 


\begin{DoxyParams}{Parameters}
{\em cs} & Neutral diffusion control structure \\
\hline
\end{DoxyParams}


Definition at line 2863 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2863   \textcolor{keywordtype}{type}(neutral\_diffusion\_CS), \textcolor{keywordtype}{pointer} :: CS\textcolor{comment}{  !< Neutral diffusion control structure}
2864 
2865   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{deallocate}(cs)
2866 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a39ce023f4b8ebc8d30f25a9b1ca93925}\label{namespacemom__neutral__diffusion_a39ce023f4b8ebc8d30f25a9b1ca93925}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!neutral\+\_\+diffusion\+\_\+init@{neutral\+\_\+diffusion\+\_\+init}}
\index{neutral\+\_\+diffusion\+\_\+init@{neutral\+\_\+diffusion\+\_\+init}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{neutral\+\_\+diffusion\+\_\+init()}{neutral\_diffusion\_init()}}
{\footnotesize\ttfamily logical function, public mom\+\_\+neutral\+\_\+diffusion\+::neutral\+\_\+diffusion\+\_\+init (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), intent(in), target}]{Time,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file,  }\item[{type(diag\+\_\+ctrl), intent(inout), target}]{diag,  }\item[{type(eos\+\_\+type), intent(in), target}]{E\+OS,  }\item[{type(diabatic\+\_\+cs), pointer}]{diabatic\+\_\+\+C\+Sp,  }\item[{type(\mbox{\hyperlink{structmom__neutral__diffusion_1_1neutral__diffusion__cs}{neutral\+\_\+diffusion\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Read parameters and allocate control structure for neutral\+\_\+diffusion module. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em time} & Time structure\\
\hline
\mbox{\tt in}  & {\em g} & Grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in,out}  & {\em diag} & Diagnostics control structure\\
\hline
\mbox{\tt in}  & {\em param\+\_\+file} & Parameter file structure\\
\hline
\mbox{\tt in}  & {\em eos} & Equation of state\\
\hline
 & {\em diabatic\+\_\+csp} & K\+PP control structure needed to get B\+LD\\
\hline
 & {\em cs} & Neutral diffusion control structure \\
\hline
\end{DoxyParams}


Definition at line 117 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
117   \textcolor{keywordtype}{type}(time\_type), \textcolor{keywordtype}{target},    \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{       !< Time structure}
118   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{          !< Grid structure}
119   \textcolor{keywordtype}{type}(unit\_scale\_type),      \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{         !< A dimensional unit scaling type}
120   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target},    \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{       !< Diagnostics control structure}
121   \textcolor{keywordtype}{type}(param\_file\_type),      \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< Parameter file structure}
122   \textcolor{keywordtype}{type}(EOS\_type),  \textcolor{keywordtype}{target},    \textcolor{keywordtype}{intent(in)}    :: EOS\textcolor{comment}{        !< Equation of state}
123   \textcolor{keywordtype}{type}(diabatic\_CS),          \textcolor{keywordtype}{pointer}       :: diabatic\_CSp\textcolor{comment}{!< KPP control structure needed to get BLD}
124   \textcolor{keywordtype}{type}(neutral\_diffusion\_CS), \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{         !< Neutral diffusion control structure}
125 
126   \textcolor{comment}{! Local variables}
127   \textcolor{keywordtype}{character(len=256)} :: mesg    \textcolor{comment}{! Message for error messages.}
128   \textcolor{keywordtype}{character(len=80)}  :: string  \textcolor{comment}{! Temporary strings}
129   \textcolor{keywordtype}{logical} :: default\_2018\_answers
130   \textcolor{keywordtype}{logical} :: boundary\_extrap
131 
132   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}
133     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"neutral\_diffusion\_init called with associated control structure."})
134     \textcolor{keywordflow}{return}
135 \textcolor{keywordflow}{  endif}
136 
137   \textcolor{comment}{! Log this module and master switch for turning it on/off}
138   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_NEUTRAL\_DIFFUSION"}, neutral\_diffusion\_init, &
139                  default=.false., do\_not\_log=.true.)
140   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, &
141            \textcolor{stringliteral}{"This module implements neutral diffusion of tracers"}, &
142            all\_default=.not.neutral\_diffusion\_init)
143   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_NEUTRAL\_DIFFUSION"}, neutral\_diffusion\_init, &
144                  \textcolor{stringliteral}{"If true, enables the neutral diffusion module."}, &
145                  default=.false.)
146 
147   \textcolor{keywordflow}{if} (.not.neutral\_diffusion\_init) \textcolor{keywordflow}{return}
148 
149   \textcolor{keyword}{allocate}(cs)
150   cs%diag => diag
151   cs%EOS => eos
152  \textcolor{comment}{! call openParameterBlock(param\_file,'NEUTRAL\_DIFF')}
153 
154   \textcolor{comment}{! Read all relevant parameters and write them to the model log.}
155   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NDIFF\_CONTINUOUS"}, cs%continuous\_reconstruction, &
156                  \textcolor{stringliteral}{"If true, uses a continuous reconstruction of T and S when "}//&
157                  \textcolor{stringliteral}{"finding neutral surfaces along which diffusion will happen. "}//&
158                  \textcolor{stringliteral}{"If false, a PPM discontinuous reconstruction of T and S "}//&
159                  \textcolor{stringliteral}{"is done which results in a higher order routine but exacts "}//&
160                  \textcolor{stringliteral}{"a higher computational cost."}, default=.true.)
161   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NDIFF\_REF\_PRES"}, cs%ref\_pres,                    &
162                  \textcolor{stringliteral}{"The reference pressure (Pa) used for the derivatives of "}//&
163                  \textcolor{stringliteral}{"the equation of state. If negative (default), local pressure is used."}, &
164                  units=\textcolor{stringliteral}{"Pa"}, default = -1., scale=us%kg\_m3\_to\_R*us%m\_s\_to\_L\_T**2)
165   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NDIFF\_INTERIOR\_ONLY"}, cs%interior\_only, &
166                  \textcolor{stringliteral}{"If true, only applies neutral diffusion in the ocean interior."}//&
167                  \textcolor{stringliteral}{"That is, the algorithm will exclude the surface and bottom"}//&
168                  \textcolor{stringliteral}{"boundary layers."}, default = .false.)
169 
170   \textcolor{comment}{! Initialize and configure remapping}
171   \textcolor{keywordflow}{if} ( .not.cs%continuous\_reconstruction ) \textcolor{keywordflow}{then}
172     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NDIFF\_BOUNDARY\_EXTRAP"}, boundary\_extrap, &
173                    \textcolor{stringliteral}{"Extrapolate at the top and bottommost cells, otherwise   \(\backslash\)n"}//  &
174                    \textcolor{stringliteral}{"assume boundaries are piecewise constant"},                      &
175                    default=.false.)
176     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NDIFF\_REMAPPING\_SCHEME"}, string, &
177                    \textcolor{stringliteral}{"This sets the reconstruction scheme used "}//&
178                    \textcolor{stringliteral}{"for vertical remapping for all variables. "}//&
179                    \textcolor{stringliteral}{"It can be one of the following schemes: "}//&
180                    trim(remappingschemesdoc), default=remappingdefaultscheme)
181     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEFAULT\_2018\_ANSWERS"}, default\_2018\_answers, &
182                  \textcolor{stringliteral}{"This sets the default value for the various \_2018\_ANSWERS parameters."}, &
183                  default=.false.)
184     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"REMAPPING\_2018\_ANSWERS"}, cs%remap\_answers\_2018, &
185                  \textcolor{stringliteral}{"If true, use the order of arithmetic and expressions that recover the "}//&
186                  \textcolor{stringliteral}{"answers from the end of 2018.  Otherwise, use updated and more robust "}//&
187                  \textcolor{stringliteral}{"forms of the same expressions."}, default=default\_2018\_answers)
188     \textcolor{keyword}{call }initialize\_remapping( cs%remap\_CS, string, boundary\_extrapolation=boundary\_extrap, &
189                                answers\_2018=cs%remap\_answers\_2018 )
190     \textcolor{keyword}{call }extract\_member\_remapping\_cs(cs%remap\_CS, degree=cs%deg)
191     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NEUTRAL\_POS\_METHOD"}, cs%neutral\_pos\_method,   &
192                    \textcolor{stringliteral}{"Method used to find the neutral position                 \(\backslash\)n"}// &
193                    \textcolor{stringliteral}{"1. Delta\_rho varies linearly, find 0 crossing            \(\backslash\)n"}// &
194                    \textcolor{stringliteral}{"2. Alpha and beta vary linearly from top to bottom,      \(\backslash\)n"}// &
195                    \textcolor{stringliteral}{"   Newton's method for neutral position                  \(\backslash\)n"}// &
196                    \textcolor{stringliteral}{"3. Full nonlinear equation of state, use regula falsi    \(\backslash\)n"}// &
197                    \textcolor{stringliteral}{"   for neutral position"}, default=3)
198     \textcolor{keywordflow}{if} (cs%neutral\_pos\_method > 4 .or. cs%neutral\_pos\_method < 0) \textcolor{keywordflow}{then}
199       \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"Invalid option for NEUTRAL\_POS\_METHOD"})
200 \textcolor{keywordflow}{    endif}
201 
202     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DELTA\_RHO\_FORM"}, cs%delta\_rho\_form,           &
203                    \textcolor{stringliteral}{"Determine how the difference in density is calculated    \(\backslash\)n"}// &
204                    \textcolor{stringliteral}{"  full       : Difference of in-situ densities           \(\backslash\)n"}// &
205                    \textcolor{stringliteral}{"  no\_pressure: Calculated from dRdT, dRdS, but no        \(\backslash\)n"}// &
206                    \textcolor{stringliteral}{"               pressure dependence"},                           &
207                    default=\textcolor{stringliteral}{"mid\_pressure"})
208     \textcolor{keywordflow}{if} (cs%neutral\_pos\_method > 1) \textcolor{keywordflow}{then}
209       \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NDIFF\_DRHO\_TOL"}, cs%drho\_tol,            &
210                      \textcolor{stringliteral}{"Sets the convergence criterion for finding the neutral\(\backslash\)n"}// &
211                      \textcolor{stringliteral}{"position within a layer in kg m-3."},                        &
212                      default=1.e-10, scale=us%kg\_m3\_to\_R)
213       \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NDIFF\_X\_TOL"}, cs%x\_tol,            &
214                      \textcolor{stringliteral}{"Sets the convergence criterion for a change in nondim\(\backslash\)n"}// &
215                      \textcolor{stringliteral}{"position within a layer."},                        &
216                      default=0.)
217       \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NDIFF\_MAX\_ITER"}, cs%max\_iter,              &
218                     \textcolor{stringliteral}{"The maximum number of iterations to be done before \(\backslash\)n"}//     &
219                      \textcolor{stringliteral}{"exiting the iterative loop to find the neutral surface"},    &
220                      default=10)
221 \textcolor{keywordflow}{    endif}
222     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NDIFF\_DEBUG"}, cs%debug,             &
223                    \textcolor{stringliteral}{"Turns on verbose output for discontinuous neutral "}//&
224                    \textcolor{stringliteral}{"diffusion routines."}, &
225                    default = .false.)
226     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HARD\_FAIL\_HEFF"}, cs%hard\_fail\_heff, &
227                   \textcolor{stringliteral}{"Bring down the model if a problem with heff is detected"},&
228                    default = .true.)
229 \textcolor{keywordflow}{  endif}
230 
231   \textcolor{comment}{! Store a rescaling factor for use in diagnostic messages.}
232   cs%R\_to\_kg\_m3 = us%R\_to\_kg\_m3
233 
234   \textcolor{keywordflow}{if} (cs%interior\_only) \textcolor{keywordflow}{then}
235     \textcolor{keyword}{call }extract\_diabatic\_member(diabatic\_csp, kpp\_csp=cs%KPP\_CSp)
236     \textcolor{keyword}{call }extract\_diabatic\_member(diabatic\_csp, energetic\_pbl\_csp=cs%energetic\_PBL\_CSp)
237     \textcolor{keywordflow}{if} ( .not. \textcolor{keyword}{ASSOCIATED}(cs%energetic\_PBL\_CSp) .and. .not. \textcolor{keyword}{ASSOCIATED}(cs%KPP\_CSp) ) \textcolor{keywordflow}{then}
238       \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"NDIFF\_INTERIOR\_ONLY is true, but no valid boundary layer scheme was found"})
239 \textcolor{keywordflow}{    endif}
240 \textcolor{keywordflow}{  endif}
241 
242 \textcolor{comment}{! call get\_param(param\_file, mdl, "KHTR", CS%KhTr, &}
243 \textcolor{comment}{!                "The background along-isopycnal tracer diffusivity.", &}
244 \textcolor{comment}{!                units="m2 s-1", default=0.0)}
245 \textcolor{comment}{!  call closeParameterBlock(param\_file)}
246   \textcolor{keywordflow}{if} (cs%continuous\_reconstruction) \textcolor{keywordflow}{then}
247     cs%nsurf = 2*g%ke+2 \textcolor{comment}{! Continuous reconstruction means that every interface has two connections}
248     \textcolor{keyword}{allocate}(cs%dRdT(szi\_(g),szj\_(g),szk\_(g)+1)) ; cs%dRdT(:,:,:) = 0.
249     \textcolor{keyword}{allocate}(cs%dRdS(szi\_(g),szj\_(g),szk\_(g)+1)) ; cs%dRdS(:,:,:) = 0.
250   \textcolor{keywordflow}{else}
251     cs%nsurf = 4*g%ke   \textcolor{comment}{! Discontinuous means that every interface has four connections}
252     \textcolor{keyword}{allocate}(cs%T\_i(szi\_(g),szj\_(g),szk\_(g),2))    ; cs%T\_i(:,:,:,:) = 0.
253     \textcolor{keyword}{allocate}(cs%S\_i(szi\_(g),szj\_(g),szk\_(g),2))    ; cs%S\_i(:,:,:,:) = 0.
254     \textcolor{keyword}{allocate}(cs%P\_i(szi\_(g),szj\_(g),szk\_(g),2))    ; cs%P\_i(:,:,:,:) = 0.
255     \textcolor{keyword}{allocate}(cs%dRdT\_i(szi\_(g),szj\_(g),szk\_(g),2)) ; cs%dRdT\_i(:,:,:,:) = 0.
256     \textcolor{keyword}{allocate}(cs%dRdS\_i(szi\_(g),szj\_(g),szk\_(g),2)) ; cs%dRdS\_i(:,:,:,:) = 0.
257     \textcolor{keyword}{allocate}(cs%ppoly\_coeffs\_T(szi\_(g),szj\_(g),szk\_(g),cs%deg+1)) ; cs%ppoly\_coeffs\_T(:,:,:,:) = 0.
258     \textcolor{keyword}{allocate}(cs%ppoly\_coeffs\_S(szi\_(g),szj\_(g),szk\_(g),cs%deg+1)) ; cs%ppoly\_coeffs\_S(:,:,:,:) = 0.
259     \textcolor{keyword}{allocate}(cs%ns(szi\_(g),szj\_(g)))    ; cs%ns(:,:) = 0.
260 \textcolor{keywordflow}{  endif}
261   \textcolor{comment}{! T-points}
262   \textcolor{keyword}{allocate}(cs%Tint(szi\_(g),szj\_(g),szk\_(g)+1)) ; cs%Tint(:,:,:) = 0.
263   \textcolor{keyword}{allocate}(cs%Sint(szi\_(g),szj\_(g),szk\_(g)+1)) ; cs%Sint(:,:,:) = 0.
264   \textcolor{keyword}{allocate}(cs%Pint(szi\_(g),szj\_(g),szk\_(g)+1)) ; cs%Pint(:,:,:) = 0.
265   \textcolor{keyword}{allocate}(cs%stable\_cell(szi\_(g),szj\_(g),szk\_(g))) ; cs%stable\_cell(:,:,:) = .true.
266   \textcolor{comment}{! U-points}
267   \textcolor{keyword}{allocate}(cs%uPoL(g%isd:g%ied,g%jsd:g%jed, cs%nsurf)); cs%uPoL(g%isc-1:g%iec,g%jsc:g%jec,:)   = 0.
268   \textcolor{keyword}{allocate}(cs%uPoR(g%isd:g%ied,g%jsd:g%jed, cs%nsurf)); cs%uPoR(g%isc-1:g%iec,g%jsc:g%jec,:)   = 0.
269   \textcolor{keyword}{allocate}(cs%uKoL(g%isd:g%ied,g%jsd:g%jed, cs%nsurf)); cs%uKoL(g%isc-1:g%iec,g%jsc:g%jec,:)   = 0
270   \textcolor{keyword}{allocate}(cs%uKoR(g%isd:g%ied,g%jsd:g%jed, cs%nsurf)); cs%uKoR(g%isc-1:g%iec,g%jsc:g%jec,:)   = 0
271   \textcolor{keyword}{allocate}(cs%uHeff(g%isd:g%ied,g%jsd:g%jed,cs%nsurf-1)); cs%uHeff(g%isc-1:g%iec,g%jsc:g%jec,:) = 0
272   \textcolor{comment}{! V-points}
273   \textcolor{keyword}{allocate}(cs%vPoL(g%isd:g%ied,g%jsd:g%jed, cs%nsurf)); cs%vPoL(g%isc:g%iec,g%jsc-1:g%jec,:)   = 0.
274   \textcolor{keyword}{allocate}(cs%vPoR(g%isd:g%ied,g%jsd:g%jed, cs%nsurf)); cs%vPoR(g%isc:g%iec,g%jsc-1:g%jec,:)   = 0.
275   \textcolor{keyword}{allocate}(cs%vKoL(g%isd:g%ied,g%jsd:g%jed, cs%nsurf)); cs%vKoL(g%isc:g%iec,g%jsc-1:g%jec,:)   = 0
276   \textcolor{keyword}{allocate}(cs%vKoR(g%isd:g%ied,g%jsd:g%jed, cs%nsurf)); cs%vKoR(g%isc:g%iec,g%jsc-1:g%jec,:)   = 0
277   \textcolor{keyword}{allocate}(cs%vHeff(g%isd:g%ied,g%jsd:g%jed,cs%nsurf-1)); cs%vHeff(g%isc:g%iec,g%jsc-1:g%jec,:) = 0
278 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a6f91c93a6363fb39395796420f25fad2}\label{namespacemom__neutral__diffusion_a6f91c93a6363fb39395796420f25fad2}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!neutral\+\_\+diffusion\+\_\+unit\+\_\+tests@{neutral\+\_\+diffusion\+\_\+unit\+\_\+tests}}
\index{neutral\+\_\+diffusion\+\_\+unit\+\_\+tests@{neutral\+\_\+diffusion\+\_\+unit\+\_\+tests}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{neutral\+\_\+diffusion\+\_\+unit\+\_\+tests()}{neutral\_diffusion\_unit\_tests()}}
{\footnotesize\ttfamily logical function, public mom\+\_\+neutral\+\_\+diffusion\+::neutral\+\_\+diffusion\+\_\+unit\+\_\+tests (\begin{DoxyParamCaption}\item[{logical, intent(in)}]{verbose }\end{DoxyParamCaption})}



Returns true if unit tests of neutral\+\_\+diffusion functions fail. Otherwise returns false. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em verbose} & If true, write results to stdout \\
\hline
\end{DoxyParams}


Definition at line 2080 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2080   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{intent(in)} :: verbose\textcolor{comment}{ !< If true, write results to stdout}
2081 
2082   neutral\_diffusion\_unit\_tests = .false. .or. &
2083     ndiff\_unit\_tests\_continuous(verbose) .or. ndiff\_unit\_tests\_discontinuous(verbose)
2084 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_aa394289328ee392cdb17000fb6fa90e8}\label{namespacemom__neutral__diffusion_aa394289328ee392cdb17000fb6fa90e8}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!neutral\+\_\+surface\+\_\+flux@{neutral\+\_\+surface\+\_\+flux}}
\index{neutral\+\_\+surface\+\_\+flux@{neutral\+\_\+surface\+\_\+flux}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{neutral\+\_\+surface\+\_\+flux()}{neutral\_surface\_flux()}}
{\footnotesize\ttfamily subroutine mom\+\_\+neutral\+\_\+diffusion\+::neutral\+\_\+surface\+\_\+flux (\begin{DoxyParamCaption}\item[{integer, intent(in)}]{nk,  }\item[{integer, intent(in)}]{nsurf,  }\item[{integer, intent(in)}]{deg,  }\item[{real, dimension(nk), intent(in)}]{hl,  }\item[{real, dimension(nk), intent(in)}]{hr,  }\item[{real, dimension(nk), intent(in)}]{Tl,  }\item[{real, dimension(nk), intent(in)}]{Tr,  }\item[{real, dimension(nsurf), intent(in)}]{PiL,  }\item[{real, dimension(nsurf), intent(in)}]{PiR,  }\item[{integer, dimension(nsurf), intent(in)}]{KoL,  }\item[{integer, dimension(nsurf), intent(in)}]{KoR,  }\item[{real, dimension(nsurf-\/1), intent(in)}]{h\+Eff,  }\item[{real, dimension(nsurf-\/1), intent(inout)}]{Flx,  }\item[{logical, intent(in)}]{continuous,  }\item[{real, intent(in)}]{h\+\_\+neglect,  }\item[{type(remapping\+\_\+cs), intent(in), optional}]{remap\+\_\+\+CS,  }\item[{real, intent(in), optional}]{h\+\_\+neglect\+\_\+edge }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns a single column of neutral diffusion fluxes of a tracer. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em nk} & Number of levels\\
\hline
\mbox{\tt in}  & {\em nsurf} & Number of neutral surfaces\\
\hline
\mbox{\tt in}  & {\em deg} & Degree of polynomial reconstructions\\
\hline
\mbox{\tt in}  & {\em hl} & Left-\/column layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em hr} & Right-\/column layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em tl} & Left-\/column layer tracer (conc, e.\+g. degC)\\
\hline
\mbox{\tt in}  & {\em tr} & Right-\/column layer tracer (conc, e.\+g. degC)\\
\hline
\mbox{\tt in}  & {\em pil} & Fractional position of neutral surface within layer KoL of left column\\
\hline
\mbox{\tt in}  & {\em pir} & Fractional position of neutral surface within layer KoR of right column\\
\hline
\mbox{\tt in}  & {\em kol} & Index of first left interface above neutral surface\\
\hline
\mbox{\tt in}  & {\em kor} & Index of first right interface above neutral surface\\
\hline
\mbox{\tt in}  & {\em heff} & Effective thickness between two neutral surfaces \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em flx} & Flux of tracer between pairs of neutral layers (conc H)\\
\hline
\mbox{\tt in}  & {\em continuous} & True if using continuous reconstruction\\
\hline
\mbox{\tt in}  & {\em h\+\_\+neglect} & A negligibly small width for the purpose of cell reconstructions \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em remap\+\_\+cs} & Remapping control structure used to create sublayers\\
\hline
\mbox{\tt in}  & {\em h\+\_\+neglect\+\_\+edge} & A negligibly small width used for edge value calculations if continuous is false \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 1877 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
1877   \textcolor{keywordtype}{integer},                      \textcolor{keywordtype}{intent(in)}    :: nk\textcolor{comment}{    !< Number of levels}
1878   \textcolor{keywordtype}{integer},                      \textcolor{keywordtype}{intent(in)}    :: nsurf\textcolor{comment}{ !< Number of neutral surfaces}
1879   \textcolor{keywordtype}{integer},                      \textcolor{keywordtype}{intent(in)}    :: deg\textcolor{comment}{   !< Degree of polynomial reconstructions}
1880   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},          \textcolor{keywordtype}{intent(in)}    :: hl\textcolor{comment}{    !< Left-column layer thickness [H ~> m or kg m-2]}
1881   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},          \textcolor{keywordtype}{intent(in)}    :: hr\textcolor{comment}{    !< Right-column layer thickness [H ~> m or kg m-2]}
1882   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},          \textcolor{keywordtype}{intent(in)}    :: Tl\textcolor{comment}{    !< Left-column layer tracer (conc, e.g. degC)}
1883   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},          \textcolor{keywordtype}{intent(in)}    :: Tr\textcolor{comment}{    !< Right-column layer tracer (conc, e.g. degC)}
1884   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nsurf)},       \textcolor{keywordtype}{intent(in)}    :: PiL\textcolor{comment}{   !< Fractional position of neutral surface}
1885 \textcolor{comment}{                                                       !! within layer KoL of left column}
1886   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nsurf)},       \textcolor{keywordtype}{intent(in)}    :: PiR\textcolor{comment}{   !< Fractional position of neutral surface}
1887 \textcolor{comment}{                                                       !! within layer KoR of right column}
1888   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(nsurf)},    \textcolor{keywordtype}{intent(in)}    :: KoL\textcolor{comment}{   !< Index of first left interface above neutral
       surface}
1889   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(nsurf)},    \textcolor{keywordtype}{intent(in)}    :: KoR\textcolor{comment}{   !< Index of first right interface above neutral
       surface}
1890   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nsurf-1)},     \textcolor{keywordtype}{intent(in)}    :: hEff\textcolor{comment}{  !< Effective thickness between two neutral}
1891 \textcolor{comment}{                                                       !! surfaces [H ~> m or kg m-2]}
1892   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nsurf-1)},     \textcolor{keywordtype}{intent(inout)} :: Flx\textcolor{comment}{   !< Flux of tracer between pairs of neutral layers
       (conc H)}
1893   \textcolor{keywordtype}{logical},                      \textcolor{keywordtype}{intent(in)}    :: continuous\textcolor{comment}{ !< True if using continuous reconstruction}
1894   \textcolor{keywordtype}{real},                         \textcolor{keywordtype}{intent(in)}    :: h\_neglect\textcolor{comment}{ !< A negligibly small width for the}
1895 \textcolor{comment}{                                             !! purpose of cell reconstructions [H ~> m or kg m-2]}
1896   \textcolor{keywordtype}{type}(remapping\_CS), \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: remap\_CS\textcolor{comment}{ !< Remapping control structure used}
1897 \textcolor{comment}{                                             !! to create sublayers}
1898   \textcolor{keywordtype}{real},               \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: h\_neglect\_edge\textcolor{comment}{ !< A negligibly small width used for}
1899 \textcolor{comment}{                                             !! edge value calculations if continuous is false [H ~> m or
       kg m-2]}
1900   \textcolor{comment}{! Local variables}
1901   \textcolor{keywordtype}{integer} :: k\_sublayer, klb, klt, krb, krt, k
1902   \textcolor{keywordtype}{real} :: T\_right\_top, T\_right\_bottom, T\_right\_layer, T\_right\_sub, T\_right\_top\_int, T\_right\_bot\_int
1903   \textcolor{keywordtype}{real} :: T\_left\_top, T\_left\_bottom, T\_left\_layer, T\_left\_sub, T\_left\_top\_int, T\_left\_bot\_int
1904   \textcolor{keywordtype}{real} :: dT\_top, dT\_bottom, dT\_layer, dT\_ave, dT\_sublayer, dT\_top\_int, dT\_bot\_int
1905   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)} :: Til\textcolor{comment}{ !< Left-column interface tracer (conc, e.g. degC)}
1906   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)} :: Tir\textcolor{comment}{ !< Right-column interface tracer (conc, e.g. degC)}
1907   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)} :: aL\_l\textcolor{comment}{ !< Left-column left edge value of tracer (conc, e.g. degC)}
1908   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)} :: aR\_l\textcolor{comment}{ !< Left-column right edge value of tracer (conc, e.g. degC)}
1909   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)} :: aL\_r\textcolor{comment}{ !< Right-column left edge value of tracer (conc, e.g. degC)}
1910   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)} :: aR\_r\textcolor{comment}{ !< Right-column right edge value of tracer (conc, e.g. degC)}
1911   \textcolor{comment}{! Discontinuous reconstruction}
1912   \textcolor{keywordtype}{integer}               :: iMethod
1913   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)} :: Tid\_l\textcolor{comment}{ !< Left-column interface tracer (conc, e.g. degC)}
1914   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)} :: Tid\_r\textcolor{comment}{ !< Right-column interface tracer (conc, e.g. degC)}
1915   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,deg+1)} :: ppoly\_r\_coeffs\_l
1916   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,deg+1)} :: ppoly\_r\_coeffs\_r
1917   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,deg+1)} :: ppoly\_r\_S\_l
1918   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,deg+1)} :: ppoly\_r\_S\_r
1919   \textcolor{keywordtype}{logical} :: down\_flux
1920   \textcolor{comment}{! Setup reconstruction edge values}
1921   \textcolor{keywordflow}{if} (continuous) \textcolor{keywordflow}{then}
1922     \textcolor{keyword}{call }interface\_scalar(nk, hl, tl, til, 2, h\_neglect)
1923     \textcolor{keyword}{call }interface\_scalar(nk, hr, tr, tir, 2, h\_neglect)
1924     \textcolor{keyword}{call }ppm\_left\_right\_edge\_values(nk, tl, til, al\_l, ar\_l)
1925     \textcolor{keyword}{call }ppm\_left\_right\_edge\_values(nk, tr, tir, al\_r, ar\_r)
1926   \textcolor{keywordflow}{else}
1927     ppoly\_r\_coeffs\_l(:,:) = 0.
1928     ppoly\_r\_coeffs\_r(:,:) = 0.
1929     tid\_l(:,:) = 0.
1930     tid\_r(:,:) = 0.
1931 
1932     \textcolor{keyword}{call }build\_reconstructions\_1d( remap\_cs, nk, hl, tl, ppoly\_r\_coeffs\_l, tid\_l, &
1933                                    ppoly\_r\_s\_l, imethod, h\_neglect, h\_neglect\_edge )
1934     \textcolor{keyword}{call }build\_reconstructions\_1d( remap\_cs, nk, hr, tr, ppoly\_r\_coeffs\_r, tid\_r, &
1935                                    ppoly\_r\_s\_r, imethod, h\_neglect, h\_neglect\_edge )
1936 \textcolor{keywordflow}{  endif}
1937 
1938   \textcolor{keywordflow}{do} k\_sublayer = 1, nsurf-1
1939     \textcolor{keywordflow}{if} (heff(k\_sublayer) == 0.) \textcolor{keywordflow}{then}
1940       flx(k\_sublayer) = 0.
1941     \textcolor{keywordflow}{else}
1942       \textcolor{keywordflow}{if} (continuous) \textcolor{keywordflow}{then}
1943         klb = kol(k\_sublayer+1)
1944         t\_left\_bottom = ( 1. - pil(k\_sublayer+1) ) * til(klb) + pil(k\_sublayer+1) * til(klb+1)
1945         klt = kol(k\_sublayer)
1946         t\_left\_top = ( 1. - pil(k\_sublayer) ) * til(klt) + pil(k\_sublayer) * til(klt+1)
1947         t\_left\_layer = ppm\_ave(pil(k\_sublayer), pil(k\_sublayer+1) + \textcolor{keywordtype}{real(klb-klt)}, &
1948                                aL\_l(klt), aR\_l(klt), Tl(klt))
1949 
1950         krb = kor(k\_sublayer+1)
1951         t\_right\_bottom = ( 1. - pir(k\_sublayer+1) ) * tir(krb) + pir(k\_sublayer+1) * tir(krb+1)
1952         krt = kor(k\_sublayer)
1953         t\_right\_top = ( 1. - pir(k\_sublayer) ) * tir(krt) + pir(k\_sublayer) * tir(krt+1)
1954         t\_right\_layer = ppm\_ave(pir(k\_sublayer), pir(k\_sublayer+1) + \textcolor{keywordtype}{real(krb-krt)}, &
1955                                 aL\_r(krt), aR\_r(krt), Tr(krt))
1956         dt\_top = t\_right\_top - t\_left\_top
1957         dt\_bottom = t\_right\_bottom - t\_left\_bottom
1958         dt\_ave = 0.5 * ( dt\_top + dt\_bottom )
1959         dt\_layer = t\_right\_layer - t\_left\_layer
1960         \textcolor{keywordflow}{if} (signum(1.,dt\_top) * signum(1.,dt\_bottom) <= 0. .or. signum(1.,dt\_ave) * signum(1.,dt\_layer) <= 
      0.) \textcolor{keywordflow}{then}
1961           dt\_ave = 0.
1962         \textcolor{keywordflow}{else}
1963           dt\_ave = dt\_layer
1964 \textcolor{keywordflow}{        endif}
1965         flx(k\_sublayer) = dt\_ave * heff(k\_sublayer)
1966       \textcolor{keywordflow}{else} \textcolor{comment}{! Discontinuous reconstruction}
1967         \textcolor{comment}{! Calculate tracer values on left and right side of the neutral surface}
1968         \textcolor{keyword}{call }neutral\_surface\_t\_eval(nk, nsurf, k\_sublayer, kol, pil, tl, tid\_l, deg, imethod, &
1969                                     ppoly\_r\_coeffs\_l, t\_left\_top, t\_left\_bottom, t\_left\_sub, &
1970                                     t\_left\_top\_int, t\_left\_bot\_int, t\_left\_layer)
1971         \textcolor{keyword}{call }neutral\_surface\_t\_eval(nk, nsurf, k\_sublayer, kor, pir, tr, tid\_r, deg, imethod, &
1972                                     ppoly\_r\_coeffs\_r, t\_right\_top, t\_right\_bottom, t\_right\_sub, &
1973                                     t\_right\_top\_int, t\_right\_bot\_int, t\_right\_layer)
1974 
1975         dt\_top      = t\_right\_top     - t\_left\_top
1976         dt\_bottom   = t\_right\_bottom  - t\_left\_bottom
1977         dt\_sublayer = t\_right\_sub     - t\_left\_sub
1978         dt\_top\_int  = t\_right\_top\_int - t\_left\_top\_int
1979         dt\_bot\_int  = t\_right\_bot\_int - t\_left\_bot\_int
1980         \textcolor{comment}{! Enforcing the below criterion incorrectly zero out fluxes}
1981         \textcolor{comment}{!dT\_layer = T\_right\_layer - T\_left\_layer}
1982 
1983         down\_flux = dt\_top <= 0. .and. dt\_bottom <= 0. .and.       &
1984                     dt\_sublayer <= 0. .and. dt\_top\_int <= 0. .and. &
1985                     dt\_bot\_int <= 0.
1986         down\_flux = down\_flux .or.                                 &
1987                     (dt\_top >= 0. .and. dt\_bottom >= 0. .and.      &
1988                     dt\_sublayer >= 0. .and. dt\_top\_int >= 0. .and. &
1989                     dt\_bot\_int >= 0.)
1990         \textcolor{keywordflow}{if} (down\_flux) \textcolor{keywordflow}{then}
1991           flx(k\_sublayer) = dt\_sublayer * heff(k\_sublayer)
1992         \textcolor{keywordflow}{else}
1993           flx(k\_sublayer) = 0.
1994 \textcolor{keywordflow}{        endif}
1995 \textcolor{keywordflow}{      endif}
1996 \textcolor{keywordflow}{    endif}
1997 \textcolor{keywordflow}{  enddo}
1998 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a005d759280cfcb84cafa23a984ffc818}\label{namespacemom__neutral__diffusion_a005d759280cfcb84cafa23a984ffc818}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!neutral\+\_\+surface\+\_\+t\+\_\+eval@{neutral\+\_\+surface\+\_\+t\+\_\+eval}}
\index{neutral\+\_\+surface\+\_\+t\+\_\+eval@{neutral\+\_\+surface\+\_\+t\+\_\+eval}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{neutral\+\_\+surface\+\_\+t\+\_\+eval()}{neutral\_surface\_t\_eval()}}
{\footnotesize\ttfamily subroutine mom\+\_\+neutral\+\_\+diffusion\+::neutral\+\_\+surface\+\_\+t\+\_\+eval (\begin{DoxyParamCaption}\item[{integer, intent(in)}]{nk,  }\item[{integer, intent(in)}]{ns,  }\item[{integer, intent(in)}]{k\+\_\+sub,  }\item[{integer, dimension(ns), intent(in)}]{Ks,  }\item[{real, dimension(ns), intent(in)}]{Ps,  }\item[{real, dimension(nk), intent(in)}]{T\+\_\+mean,  }\item[{real, dimension(nk,2), intent(in)}]{T\+\_\+int,  }\item[{integer, intent(in)}]{deg,  }\item[{integer, intent(in)}]{i\+Method,  }\item[{real, dimension(nk,deg+1), intent(in)}]{T\+\_\+poly,  }\item[{real, intent(out)}]{T\+\_\+top,  }\item[{real, intent(out)}]{T\+\_\+bot,  }\item[{real, intent(out)}]{T\+\_\+sub,  }\item[{real, intent(out)}]{T\+\_\+top\+\_\+int,  }\item[{real, intent(out)}]{T\+\_\+bot\+\_\+int,  }\item[{real, intent(out)}]{T\+\_\+layer }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Evaluate various parts of the reconstructions to calculate gradient-\/based flux limter. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em nk} & Number of cell everages\\
\hline
\mbox{\tt in}  & {\em ns} & Number of neutral surfaces\\
\hline
\mbox{\tt in}  & {\em k\+\_\+sub} & Index of current neutral layer\\
\hline
\mbox{\tt in}  & {\em ks} & List of the layers associated with each neutral surface\\
\hline
\mbox{\tt in}  & {\em ps} & List of the positions within a layer of each surface\\
\hline
\mbox{\tt in}  & {\em t\+\_\+mean} & Cell average of tracer\\
\hline
\mbox{\tt in}  & {\em t\+\_\+int} & Cell interface values of tracer from reconstruction\\
\hline
\mbox{\tt in}  & {\em deg} & Degree of reconstruction polynomial (e.\+g. 1 is linear)\\
\hline
\mbox{\tt in}  & {\em imethod} & Method of integration to use\\
\hline
\mbox{\tt in}  & {\em t\+\_\+poly} & Coefficients of polynomial reconstructions\\
\hline
\mbox{\tt out}  & {\em t\+\_\+top} & Tracer value at top (across discontinuity if necessary)\\
\hline
\mbox{\tt out}  & {\em t\+\_\+bot} & Tracer value at bottom (across discontinuity if necessary)\\
\hline
\mbox{\tt out}  & {\em t\+\_\+sub} & Average of the tracer value over the sublayer\\
\hline
\mbox{\tt out}  & {\em t\+\_\+top\+\_\+int} & Tracer value at top interface of neutral layer\\
\hline
\mbox{\tt out}  & {\em t\+\_\+bot\+\_\+int} & Tracer value at bottom interface of neutral layer\\
\hline
\mbox{\tt out}  & {\em t\+\_\+layer} & Cell-\/average that the the reconstruction belongs to \\
\hline
\end{DoxyParams}


Definition at line 2004 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2004   \textcolor{keywordtype}{integer},                   \textcolor{keywordtype}{intent(in   )} :: nk\textcolor{comment}{        !< Number of cell everages}
2005   \textcolor{keywordtype}{integer},                   \textcolor{keywordtype}{intent(in   )} :: ns\textcolor{comment}{        !< Number of neutral surfaces}
2006   \textcolor{keywordtype}{integer},                   \textcolor{keywordtype}{intent(in   )} :: k\_sub\textcolor{comment}{     !< Index of current neutral layer}
2007   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(ns)},    \textcolor{keywordtype}{intent(in   )} :: Ks\textcolor{comment}{        !< List of the layers associated with each neutral
       surface}
2008   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(ns)},       \textcolor{keywordtype}{intent(in   )} :: Ps\textcolor{comment}{        !< List of the positions within a layer of each
       surface}
2009   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},       \textcolor{keywordtype}{intent(in   )} :: T\_mean\textcolor{comment}{    !< Cell average of tracer}
2010   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,2)},     \textcolor{keywordtype}{intent(in   )} :: T\_int\textcolor{comment}{     !< Cell interface values of tracer from
       reconstruction}
2011   \textcolor{keywordtype}{integer},                   \textcolor{keywordtype}{intent(in   )} :: deg\textcolor{comment}{       !< Degree of reconstruction polynomial (e.g. 1 is
       linear)}
2012   \textcolor{keywordtype}{integer},                   \textcolor{keywordtype}{intent(in   )} :: iMethod\textcolor{comment}{   !< Method of integration to use}
2013   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk,deg+1)}, \textcolor{keywordtype}{intent(in   )} :: T\_poly\textcolor{comment}{    !< Coefficients of polynomial reconstructions}
2014   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(  out)} :: T\_top\textcolor{comment}{     !< Tracer value at top (across discontinuity if
       necessary)}
2015   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(  out)} :: T\_bot\textcolor{comment}{     !< Tracer value at bottom (across discontinuity if
       necessary)}
2016   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(  out)} :: T\_sub\textcolor{comment}{     !< Average of the tracer value over the sublayer}
2017   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(  out)} :: T\_top\_int\textcolor{comment}{ !< Tracer value at top interface of neutral layer}
2018   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(  out)} :: T\_bot\_int\textcolor{comment}{ !< Tracer value at bottom interface of neutral
       layer}
2019   \textcolor{keywordtype}{real},                      \textcolor{keywordtype}{intent(  out)} :: T\_layer\textcolor{comment}{   !< Cell-average that the the reconstruction belongs
       to}
2020 
2021   \textcolor{keywordtype}{integer} :: kl, ks\_top, ks\_bot
2022 
2023   ks\_top = k\_sub
2024   ks\_bot = k\_sub + 1
2025   \textcolor{keywordflow}{if} ( ks(ks\_top) /= ks(ks\_bot) ) \textcolor{keywordflow}{then}
2026     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Neutral surfaces span more than one layer"})
2027 \textcolor{keywordflow}{  endif}
2028   kl = ks(k\_sub)
2029   \textcolor{comment}{! First if the neutral surfaces spans the entirety of a cell, then do not search across the discontinuity}
2030   \textcolor{keywordflow}{if} ( (ps(ks\_top) == 0.) .and. (ps(ks\_bot) == 1.)) \textcolor{keywordflow}{then}
2031     t\_top = t\_int(kl,1)
2032     t\_bot = t\_int(kl,2)
2033   \textcolor{keywordflow}{else}
2034     \textcolor{comment}{! Search across potential discontinuity at top}
2035     \textcolor{keywordflow}{if} ( (kl > 1) .and. (ps(ks\_top) == 0.)  ) \textcolor{keywordflow}{then}
2036       t\_top = t\_int(kl-1,2)
2037     \textcolor{keywordflow}{else}
2038       t\_top = evaluation\_polynomial( t\_poly(kl,:), deg+1, ps(ks\_top) )
2039 \textcolor{keywordflow}{    endif}
2040     \textcolor{comment}{! Search across potential discontinuity at bottom}
2041     \textcolor{keywordflow}{if} ( (kl < nk) .and. (ps(ks\_bot) == 1.) ) \textcolor{keywordflow}{then}
2042       t\_bot = t\_int(kl+1,1)
2043     \textcolor{keywordflow}{else}
2044       t\_bot = evaluation\_polynomial( t\_poly(kl,:), deg+1, ps(ks\_bot) )
2045 \textcolor{keywordflow}{    endif}
2046 \textcolor{keywordflow}{  endif}
2047   t\_sub = average\_value\_ppoly(nk, t\_mean, t\_int, t\_poly, imethod, kl, ps(ks\_top), ps(ks\_bot))
2048   t\_top\_int = evaluation\_polynomial( t\_poly(kl,:), deg+1, ps(ks\_top))
2049   t\_bot\_int = evaluation\_polynomial( t\_poly(kl,:), deg+1, ps(ks\_bot))
2050   t\_layer = t\_mean(kl)
2051 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_ac211ae488cb6e3c6685df85d24ecd0dd}\label{namespacemom__neutral__diffusion_ac211ae488cb6e3c6685df85d24ecd0dd}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!plm\+\_\+diff@{plm\+\_\+diff}}
\index{plm\+\_\+diff@{plm\+\_\+diff}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{plm\+\_\+diff()}{plm\_diff()}}
{\footnotesize\ttfamily subroutine mom\+\_\+neutral\+\_\+diffusion\+::plm\+\_\+diff (\begin{DoxyParamCaption}\item[{integer, intent(in)}]{nk,  }\item[{real, dimension(nk), intent(in)}]{h,  }\item[{real, dimension(nk), intent(in)}]{S,  }\item[{integer, intent(in)}]{c\+\_\+method,  }\item[{integer, intent(in)}]{b\+\_\+method,  }\item[{real, dimension(nk), intent(inout)}]{diff }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns P\+LM slopes for a column where the slopes are the difference in value across each cell. The limiting follows equation 1.\+8 in Colella \& Woodward, 1984\+: J\+CP 54, 174-\/201. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em nk} & Number of levels\\
\hline
\mbox{\tt in}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s} & Layer salinity (conc, e.\+g. ppt)\\
\hline
\mbox{\tt in}  & {\em c\+\_\+method} & Method to use for the centered difference\\
\hline
\mbox{\tt in}  & {\em b\+\_\+method} & =1, use P\+CM in first/last cell, =2 uses linear extrapolation\\
\hline
\mbox{\tt in,out}  & {\em diff} & Scalar difference across layer (conc, e.\+g. ppt) determined by the following values for c\+\_\+method\+:
\begin{DoxyEnumerate}
\item Second order finite difference (not recommended)
\item Second order finite volume (used in original P\+PM)
\item Finite-\/volume weighted least squares linear fit 
\end{DoxyEnumerate}\\
\hline
\end{DoxyParams}
\begin{DoxyRefDesc}{Todo}
\item[\mbox{\hyperlink{todo__todo000008}{Todo}}]The use of c\+\_\+method to choose a scheme is inefficient and should eventually be moved up the call tree. \end{DoxyRefDesc}


Definition at line 809 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
809   \textcolor{keywordtype}{integer},             \textcolor{keywordtype}{intent(in)}    :: nk\textcolor{comment}{       !< Number of levels}
810   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)}, \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{        !< Layer thickness [H ~> m or kg m-2]}
811   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)}, \textcolor{keywordtype}{intent(in)}    :: S\textcolor{comment}{        !< Layer salinity (conc, e.g. ppt)}
812   \textcolor{keywordtype}{integer},             \textcolor{keywordtype}{intent(in)}    :: c\_method\textcolor{comment}{ !< Method to use for the centered difference}
813   \textcolor{keywordtype}{integer},             \textcolor{keywordtype}{intent(in)}    :: b\_method\textcolor{comment}{ !< =1, use PCM in first/last cell, =2 uses linear
       extrapolation}
814   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)}, \textcolor{keywordtype}{intent(inout)} :: diff\textcolor{comment}{     !< Scalar difference across layer (conc, e.g. ppt)}
815 \textcolor{comment}{                                                 !! determined by the following values for c\_method:}
816 \textcolor{comment}{                                                 !!   1. Second order finite difference (not recommended)}
817 \textcolor{comment}{                                                 !!   2. Second order finite volume (used in original PPM)}
818 \textcolor{comment}{                                                 !!   3. Finite-volume weighted least squares linear fit}
819 \textcolor{comment}{                                                 !! \(\backslash\)todo  The use of c\_method to choose a scheme is
       inefficient}
820 \textcolor{comment}{                                                 !! and should eventually be moved up the call tree.}
821 
822   \textcolor{comment}{! Local variables}
823   \textcolor{keywordtype}{integer} :: k
824   \textcolor{keywordtype}{real} :: hkm1, hk, hkp1, Skm1, Sk, Skp1, diff\_l, diff\_r, diff\_c
825 
826   \textcolor{keywordflow}{do} k = 2, nk-1
827     hkm1 = h(k-1)
828     hk = h(k)
829     hkp1 = h(k+1)
830 
831     \textcolor{keywordflow}{if} ( ( hkp1 + hk ) * ( hkm1 + hk ) > 0.) \textcolor{keywordflow}{then}
832       skm1 = s(k-1)
833       sk = s(k)
834       skp1 = s(k+1)
835       \textcolor{keywordflow}{if} (c\_method==1) \textcolor{keywordflow}{then}
836         \textcolor{comment}{! Simple centered diff (from White)}
837         \textcolor{keywordflow}{if} ( hk + 0.5 * (hkm1 + hkp1) /= 0. ) \textcolor{keywordflow}{then}
838           diff\_c = ( skp1 - skm1 ) * ( hk / ( hk + 0.5 * (hkm1 + hkp1) ) )
839         \textcolor{keywordflow}{else}
840           diff\_c = 0.
841 \textcolor{keywordflow}{        endif}
842       \textcolor{keywordflow}{elseif} (c\_method==2) \textcolor{keywordflow}{then}
843         \textcolor{comment}{! Second order accurate centered FV slope (from Colella and Woodward, JCP 1984)}
844         diff\_c = fv\_diff(hkm1, hk, hkp1, skm1, sk, skp1)
845       \textcolor{keywordflow}{elseif} (c\_method==3) \textcolor{keywordflow}{then}
846         \textcolor{comment}{! Second order accurate finite-volume least squares slope}
847         diff\_c = hk * fvlsq\_slope(hkm1, hk, hkp1, skm1, sk, skp1)
848 \textcolor{keywordflow}{      endif}
849       \textcolor{comment}{! Limit centered slope by twice the side differenced slopes}
850       diff\_l = 2. * ( sk - skm1 )
851       diff\_r = 2. * ( skp1 - sk )
852       \textcolor{keywordflow}{if} ( signum(1., diff\_l) * signum(1., diff\_r) <= 0. ) \textcolor{keywordflow}{then}
853         diff(k) = 0. \textcolor{comment}{! PCM for local extrema}
854       \textcolor{keywordflow}{else}
855         diff(k) = sign( min( abs(diff\_l), abs(diff\_c), abs(diff\_r) ), diff\_c )
856 \textcolor{keywordflow}{      endif}
857     \textcolor{keywordflow}{else}
858       diff(k) = 0. \textcolor{comment}{! PCM next to vanished layers}
859 \textcolor{keywordflow}{    endif}
860 \textcolor{keywordflow}{  enddo}
861   \textcolor{keywordflow}{if} (b\_method==1) \textcolor{keywordflow}{then} \textcolor{comment}{! PCM for top and bottom layer}
862     diff(1) = 0.
863     diff(nk) = 0.
864   \textcolor{keywordflow}{elseif} (b\_method==2) \textcolor{keywordflow}{then} \textcolor{comment}{! Linear extrapolation for top and bottom interfaces}
865     diff(1) = ( s(2) - s(1) ) * 2. * ( h(1) / ( h(1) + h(2) ) )
866     diff(nk) = s(nk) - s(nk-1) * 2. * ( h(nk) / ( h(nk-1) + h(nk) ) )
867 \textcolor{keywordflow}{  endif}
868 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a6931d9a07d09aa5d76d5abd2e47a54df}\label{namespacemom__neutral__diffusion_a6931d9a07d09aa5d76d5abd2e47a54df}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!ppm\+\_\+ave@{ppm\+\_\+ave}}
\index{ppm\+\_\+ave@{ppm\+\_\+ave}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{ppm\+\_\+ave()}{ppm\_ave()}}
{\footnotesize\ttfamily real function mom\+\_\+neutral\+\_\+diffusion\+::ppm\+\_\+ave (\begin{DoxyParamCaption}\item[{real, intent(in)}]{xL,  }\item[{real, intent(in)}]{xR,  }\item[{real, intent(in)}]{aL,  }\item[{real, intent(in)}]{aR,  }\item[{real, intent(in)}]{a\+Mean }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns the average of a P\+PM reconstruction between two fractional positions. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em xl} & Fraction position of left bound (0,1)\\
\hline
\mbox{\tt in}  & {\em xr} & Fraction position of right bound (0,1)\\
\hline
\mbox{\tt in}  & {\em al} & Left edge scalar value, at x=0\\
\hline
\mbox{\tt in}  & {\em ar} & Right edge scalar value, at x=1\\
\hline
\mbox{\tt in}  & {\em amean} & Average scalar value of cell \\
\hline
\end{DoxyParams}


Definition at line 771 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
771   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: xL\textcolor{comment}{    !< Fraction position of left bound (0,1)}
772   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: xR\textcolor{comment}{    !< Fraction position of right bound (0,1)}
773   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: aL\textcolor{comment}{    !< Left edge scalar value, at x=0}
774   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: aR\textcolor{comment}{    !< Right edge scalar value, at x=1}
775   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: aMean\textcolor{comment}{ !< Average scalar value of cell}
776 
777   \textcolor{comment}{! Local variables}
778   \textcolor{keywordtype}{real} :: dx, xave, a6, a6o3
779 
780   dx = xr - xl
781   xave = 0.5 * ( xr + xl )
782   a6o3 = 2. * amean - ( al + ar ) \textcolor{comment}{! a6 / 3.}
783   a6 = 3. * a6o3
784 
785   \textcolor{keywordflow}{if} (dx<0.) \textcolor{keywordflow}{then}
786     stop \textcolor{stringliteral}{'ppm\_ave: dx<0 should not happend!'}
787   \textcolor{keywordflow}{elseif} (dx>1.) \textcolor{keywordflow}{then}
788     stop \textcolor{stringliteral}{'ppm\_ave: dx>1 should not happend!'}
789   \textcolor{keywordflow}{elseif} (dx==0.) \textcolor{keywordflow}{then}
790     ppm\_ave = al + ( ar - al ) * xr + a6 * xr * ( 1. - xr )
791   \textcolor{keywordflow}{else}
792     ppm\_ave = ( al + xave * ( ( ar - al ) + a6 ) )  - a6o3 * ( xr**2 + xr * xl + xl**2 )
793 \textcolor{keywordflow}{  endif}
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_ae931a15b9ab6a7672b005eb5fe382219}\label{namespacemom__neutral__diffusion_ae931a15b9ab6a7672b005eb5fe382219}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!ppm\+\_\+edge@{ppm\+\_\+edge}}
\index{ppm\+\_\+edge@{ppm\+\_\+edge}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{ppm\+\_\+edge()}{ppm\_edge()}}
{\footnotesize\ttfamily real function mom\+\_\+neutral\+\_\+diffusion\+::ppm\+\_\+edge (\begin{DoxyParamCaption}\item[{real, intent(in)}]{hkm1,  }\item[{real, intent(in)}]{hk,  }\item[{real, intent(in)}]{hkp1,  }\item[{real, intent(in)}]{hkp2,  }\item[{real, intent(in)}]{Ak,  }\item[{real, intent(in)}]{Akp1,  }\item[{real, intent(in)}]{Pk,  }\item[{real, intent(in)}]{Pkp1,  }\item[{real, intent(in)}]{h\+\_\+neglect }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns the P\+PM quasi-\/fourth order edge value at k+1/2 following equation 1.\+6 in Colella \& Woodward, 1984\+: J\+CP 54, 174-\/201. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em hkm1} & Width of cell k-\/1\\
\hline
\mbox{\tt in}  & {\em hk} & Width of cell k\\
\hline
\mbox{\tt in}  & {\em hkp1} & Width of cell k+1\\
\hline
\mbox{\tt in}  & {\em hkp2} & Width of cell k+2\\
\hline
\mbox{\tt in}  & {\em ak} & Average scalar value of cell k\\
\hline
\mbox{\tt in}  & {\em akp1} & Average scalar value of cell k+1\\
\hline
\mbox{\tt in}  & {\em pk} & P\+LM slope for cell k\\
\hline
\mbox{\tt in}  & {\em pkp1} & P\+LM slope for cell k+1\\
\hline
\mbox{\tt in}  & {\em h\+\_\+neglect} & A negligibly small thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 731 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
731   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: hkm1\textcolor{comment}{ !< Width of cell k-1}
732   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: hk\textcolor{comment}{   !< Width of cell k}
733   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: hkp1\textcolor{comment}{ !< Width of cell k+1}
734   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: hkp2\textcolor{comment}{ !< Width of cell k+2}
735   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: Ak\textcolor{comment}{   !< Average scalar value of cell k}
736   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: Akp1\textcolor{comment}{ !< Average scalar value of cell k+1}
737   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: Pk\textcolor{comment}{   !< PLM slope for cell k}
738   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: Pkp1\textcolor{comment}{ !< PLM slope for cell k+1}
739   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: h\_neglect\textcolor{comment}{ !< A negligibly small thickness [H ~> m or kg m-2]}
740 
741   \textcolor{comment}{! Local variables}
742   \textcolor{keywordtype}{real} :: R\_hk\_hkp1, R\_2hk\_hkp1, R\_hk\_2hkp1, f1, f2, f3, f4
743 
744   r\_hk\_hkp1 = hk + hkp1
745   \textcolor{keywordflow}{if} (r\_hk\_hkp1 <= 0.) \textcolor{keywordflow}{then}
746     ppm\_edge = 0.5 * ( ak + akp1 )
747     \textcolor{keywordflow}{return}
748 \textcolor{keywordflow}{  endif}
749   r\_hk\_hkp1 = 1. / r\_hk\_hkp1
750   \textcolor{keywordflow}{if} (hk<hkp1) \textcolor{keywordflow}{then}
751     ppm\_edge = ak + ( hk * r\_hk\_hkp1 ) * ( akp1 - ak )
752   \textcolor{keywordflow}{else}
753     ppm\_edge = akp1 + ( hkp1 * r\_hk\_hkp1 ) * ( ak - akp1 )
754 \textcolor{keywordflow}{  endif}
755 
756   r\_2hk\_hkp1 = 1. / ( ( 2. * hk + hkp1 ) + h\_neglect )
757   r\_hk\_2hkp1 = 1. / ( ( hk + 2. * hkp1 ) + h\_neglect )
758   f1 = 1./ ( ( hk + hkp1) + ( hkm1 + hkp2 ) )
759   f2 = 2. * ( hkp1 * hk ) * r\_hk\_hkp1 * &
760             ( ( hkm1 + hk ) * r\_2hk\_hkp1  - ( hkp2 + hkp1 ) * r\_hk\_2hkp1 )
761   f3 = hk * ( hkm1 + hk ) * r\_2hk\_hkp1
762   f4 = hkp1 * ( hkp1 + hkp2 ) * r\_hk\_2hkp1
763 
764   ppm\_edge = ppm\_edge + f1 * ( f2 * ( akp1 - ak ) - ( f3 * pkp1 - f4 * pk ) )
765 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a9b380e21dcc9c53efcec4712228b931f}\label{namespacemom__neutral__diffusion_a9b380e21dcc9c53efcec4712228b931f}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!ppm\+\_\+left\+\_\+right\+\_\+edge\+\_\+values@{ppm\+\_\+left\+\_\+right\+\_\+edge\+\_\+values}}
\index{ppm\+\_\+left\+\_\+right\+\_\+edge\+\_\+values@{ppm\+\_\+left\+\_\+right\+\_\+edge\+\_\+values}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{ppm\+\_\+left\+\_\+right\+\_\+edge\+\_\+values()}{ppm\_left\_right\_edge\_values()}}
{\footnotesize\ttfamily subroutine mom\+\_\+neutral\+\_\+diffusion\+::ppm\+\_\+left\+\_\+right\+\_\+edge\+\_\+values (\begin{DoxyParamCaption}\item[{integer, intent(in)}]{nk,  }\item[{real, dimension(nk), intent(in)}]{Tl,  }\item[{real, dimension(nk+1), intent(in)}]{Ti,  }\item[{real, dimension(nk), intent(inout)}]{aL,  }\item[{real, dimension(nk), intent(inout)}]{aR }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Discontinuous P\+PM reconstructions of the left/right edge values within a cell. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em nk} & Number of levels\\
\hline
\mbox{\tt in}  & {\em tl} & Layer tracer (conc, e.\+g. degC)\\
\hline
\mbox{\tt in}  & {\em ti} & Interface tracer (conc, e.\+g. degC)\\
\hline
\mbox{\tt in,out}  & {\em al} & Left edge value of tracer (conc, e.\+g. degC)\\
\hline
\mbox{\tt in,out}  & {\em ar} & Right edge value of tracer (conc, e.\+g. degC) \\
\hline
\end{DoxyParams}


Definition at line 2056 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2056   \textcolor{keywordtype}{integer},                    \textcolor{keywordtype}{intent(in)}    :: nk\textcolor{comment}{ !< Number of levels}
2057   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},        \textcolor{keywordtype}{intent(in)}    :: Tl\textcolor{comment}{ !< Layer tracer (conc, e.g. degC)}
2058   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk+1)},      \textcolor{keywordtype}{intent(in)}    :: Ti\textcolor{comment}{ !< Interface tracer (conc, e.g. degC)}
2059   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},        \textcolor{keywordtype}{intent(inout)} :: aL\textcolor{comment}{ !< Left edge value of tracer (conc, e.g. degC)}
2060   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)},        \textcolor{keywordtype}{intent(inout)} :: aR\textcolor{comment}{ !< Right edge value of tracer (conc, e.g. degC)}
2061 
2062   \textcolor{keywordtype}{integer} :: k
2063   \textcolor{comment}{! Setup reconstruction edge values}
2064   \textcolor{keywordflow}{do} k = 1, nk
2065     al(k) = ti(k)
2066     ar(k) = ti(k+1)
2067     \textcolor{keywordflow}{if} ( signum(1., ar(k) - tl(k))*signum(1., tl(k) - al(k)) <= 0.0 ) \textcolor{keywordflow}{then}
2068       al(k) = tl(k)
2069       ar(k) = tl(k)
2070     \textcolor{keywordflow}{elseif} ( sign(3., ar(k) - al(k)) * ( (tl(k) - al(k)) + (tl(k) - ar(k))) > abs(ar(k) - al(k)) ) \textcolor{keywordflow}{then}
2071       al(k) = tl(k) + 2.0 * ( tl(k) - ar(k) )
2072     \textcolor{keywordflow}{elseif} ( sign(3., ar(k) - al(k)) * ( (tl(k) - al(k)) + (tl(k) - ar(k))) < -abs(ar(k) - al(k)) ) \textcolor{keywordflow}{then}
2073       ar(k) = tl(k) + 2.0 * ( tl(k) - al(k) )
2074 \textcolor{keywordflow}{    endif}
2075 \textcolor{keywordflow}{  enddo}
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a2952cbcce84dbdae01fce09240f819c5}\label{namespacemom__neutral__diffusion_a2952cbcce84dbdae01fce09240f819c5}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!search\+\_\+other\+\_\+column@{search\+\_\+other\+\_\+column}}
\index{search\+\_\+other\+\_\+column@{search\+\_\+other\+\_\+column}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{search\+\_\+other\+\_\+column()}{search\_other\_column()}}
{\footnotesize\ttfamily real function mom\+\_\+neutral\+\_\+diffusion\+::search\+\_\+other\+\_\+column (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__neutral__diffusion_1_1neutral__diffusion__cs}{neutral\+\_\+diffusion\+\_\+cs}}), intent(in)}]{CS,  }\item[{integer, intent(in)}]{ksurf,  }\item[{real, intent(in)}]{pos\+\_\+last,  }\item[{real, intent(in)}]{T\+\_\+from,  }\item[{real, intent(in)}]{S\+\_\+from,  }\item[{real, intent(in)}]{P\+\_\+from,  }\item[{real, intent(in)}]{T\+\_\+top,  }\item[{real, intent(in)}]{S\+\_\+top,  }\item[{real, intent(in)}]{P\+\_\+top,  }\item[{real, intent(in)}]{T\+\_\+bot,  }\item[{real, intent(in)}]{S\+\_\+bot,  }\item[{real, intent(in)}]{P\+\_\+bot,  }\item[{real, dimension(\+:), intent(in)}]{T\+\_\+poly,  }\item[{real, dimension(\+:), intent(in)}]{S\+\_\+poly }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Searches the \char`\"{}other\char`\"{} (searched) column for the position of the neutral surface. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em cs} & Neutral diffusion control structure\\
\hline
\mbox{\tt in}  & {\em ksurf} & Current index of neutral surface\\
\hline
\mbox{\tt in}  & {\em pos\+\_\+last} & Last position within the current layer, used as the lower bound in the root finding algorithm \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em t\+\_\+from} & Temperature at the searched from interface \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s\+\_\+from} & Salinity at the searched from interface \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em p\+\_\+from} & Pressure at the searched from interface \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt in}  & {\em t\+\_\+top} & Temperature at the searched to top interface \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s\+\_\+top} & Salinity at the searched to top interface \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em p\+\_\+top} & Pressure at the searched to top interface \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} interface \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt in}  & {\em t\+\_\+bot} & Temperature at the searched to bottom interface \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s\+\_\+bot} & Salinity at the searched to bottom interface \mbox{[}ppt\mbox{]}\\
\hline
\mbox{\tt in}  & {\em p\+\_\+bot} & Pressure at the searched to bottom interface \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt in}  & {\em t\+\_\+poly} & Temperature polynomial reconstruction coefficients \mbox{[}degC\mbox{]}\\
\hline
\mbox{\tt in}  & {\em s\+\_\+poly} & Salinity polynomial reconstruction coefficients \mbox{[}ppt\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 1439 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
1439   \textcolor{keywordtype}{type}(neutral\_diffusion\_CS), \textcolor{keywordtype}{intent(in   )} :: CS\textcolor{comment}{       !< Neutral diffusion control structure}
1440   \textcolor{keywordtype}{integer},                    \textcolor{keywordtype}{intent(in   )} :: ksurf\textcolor{comment}{    !< Current index of neutral surface}
1441   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in   )} :: pos\_last\textcolor{comment}{ !< Last position within the current layer, used as
       the lower}
1442 \textcolor{comment}{                                                        !! bound in the root finding algorithm [nondim]}
1443   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in   )} :: T\_from\textcolor{comment}{   !< Temperature at the searched from interface
       [degC]}
1444   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in   )} :: S\_from\textcolor{comment}{   !< Salinity    at the searched from interface [ppt]}
1445   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in   )} :: P\_from\textcolor{comment}{   !< Pressure at the searched from interface [R L2
       T-2 ~> Pa]}
1446   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in   )} :: T\_top\textcolor{comment}{    !< Temperature at the searched to top interface
       [degC]}
1447   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in   )} :: S\_top\textcolor{comment}{    !< Salinity    at the searched to top interface
       [ppt]}
1448   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in   )} :: P\_top\textcolor{comment}{    !< Pressure at the searched to top interface [R L2
       T-2 ~> Pa]}
1449 \textcolor{comment}{                                                        !! interface [R L2 T-2 ~> Pa]}
1450   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in   )} :: T\_bot\textcolor{comment}{    !< Temperature at the searched to bottom interface
       [degC]}
1451   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in   )} :: S\_bot\textcolor{comment}{    !< Salinity    at the searched to bottom interface
       [ppt]}
1452   \textcolor{keywordtype}{real},                       \textcolor{keywordtype}{intent(in   )} :: P\_bot\textcolor{comment}{    !< Pressure at the searched to bottom}
1453 \textcolor{comment}{                                                        !! interface [R L2 T-2 ~> Pa]}
1454   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)},         \textcolor{keywordtype}{intent(in   )} :: T\_poly\textcolor{comment}{   !< Temperature polynomial reconstruction
       coefficients [degC]}
1455   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(:)},         \textcolor{keywordtype}{intent(in   )} :: S\_poly\textcolor{comment}{   !< Salinity    polynomial reconstruction
       coefficients [ppt]}
1456   \textcolor{comment}{! Local variables}
1457   \textcolor{keywordtype}{real} :: dRhotop, dRhobot \textcolor{comment}{! Density differences [R ~> kg m-3]}
1458   \textcolor{keywordtype}{real} :: dRdT\_top, dRdT\_bot, dRdT\_from \textcolor{comment}{! Partial derivatives of density with temperature [R degC-1 ~> kg
       m-3 degC-1]}
1459   \textcolor{keywordtype}{real} :: dRdS\_top, dRdS\_bot, dRdS\_from \textcolor{comment}{! Partial derivatives of density with salinity [R ppt-1 ~> kg m-3
       ppt-1]}
1460 
1461   \textcolor{comment}{! Calculate the differencei in density at the tops or the bottom}
1462   \textcolor{keywordflow}{if} (cs%neutral\_pos\_method == 1 .or. cs%neutral\_pos\_method == 3) \textcolor{keywordflow}{then}
1463     \textcolor{keyword}{call }calc\_delta\_rho\_and\_derivs(cs, t\_top, s\_top, p\_top, t\_from, s\_from, p\_from, drhotop)
1464     \textcolor{keyword}{call }calc\_delta\_rho\_and\_derivs(cs, t\_bot, s\_bot, p\_bot, t\_from, s\_from, p\_from, drhobot)
1465   \textcolor{keywordflow}{elseif} (cs%neutral\_pos\_method == 2) \textcolor{keywordflow}{then}
1466     \textcolor{keyword}{call }calc\_delta\_rho\_and\_derivs(cs, t\_top, s\_top, p\_top, t\_from, s\_from, p\_from, drhotop, &
1467                                    drdt\_top, drds\_top, drdt\_from, drds\_from)
1468     \textcolor{keyword}{call }calc\_delta\_rho\_and\_derivs(cs, t\_bot, s\_bot, p\_bot, t\_from, s\_from, p\_from, drhobot, &
1469                                    drdt\_bot, drds\_bot, drdt\_from, drds\_from)
1470 \textcolor{keywordflow}{  endif}
1471 
1472   \textcolor{comment}{! Handle all the special cases EXCEPT if it connects within the layer}
1473   \textcolor{keywordflow}{if} ( (drhotop > 0.) .or. (ksurf == 1) ) \textcolor{keywordflow}{then}      \textcolor{comment}{! First interface or lighter than anything in layer}
1474     pos = pos\_last
1475   \textcolor{keywordflow}{elseif} ( drhotop > drhobot ) \textcolor{keywordflow}{then}                 \textcolor{comment}{! Unstably stratified}
1476     pos = 1.
1477   \textcolor{keywordflow}{elseif} ( drhotop < 0. .and. drhobot < 0.) \textcolor{keywordflow}{then}    \textcolor{comment}{! Denser than anything in layer}
1478     pos = 1.
1479   \textcolor{keywordflow}{elseif} ( drhotop == 0. .and. drhobot == 0. ) \textcolor{keywordflow}{then} \textcolor{comment}{! Perfectly unstratified}
1480     pos = 1.
1481   \textcolor{keywordflow}{elseif} ( drhobot == 0. ) \textcolor{keywordflow}{then}                     \textcolor{comment}{! Matches perfectly at the Top}
1482     pos = 1.
1483   \textcolor{keywordflow}{elseif} ( drhotop == 0. ) \textcolor{keywordflow}{then}                     \textcolor{comment}{! Matches perfectly at the Bottom}
1484     pos = pos\_last
1485   \textcolor{keywordflow}{else}                                              \textcolor{comment}{! Neutral surface within layer}
1486     pos = -1
1487 \textcolor{keywordflow}{  endif}
1488 
1489   \textcolor{comment}{! Can safely return if position is >= 0 otherwise will need to find the position within the layer}
1490   \textcolor{keywordflow}{if} (pos>=0) \textcolor{keywordflow}{return}
1491 
1492   \textcolor{keywordflow}{if} (cs%neutral\_pos\_method==1) \textcolor{keywordflow}{then}
1493     pos = interpolate\_for\_nondim\_position( drhotop, p\_top, drhobot, p\_bot )
1494   \textcolor{comment}{! For the 'Linear' case of finding the neutral position, the fromerence pressure to use is the average}
1495   \textcolor{comment}{! of the midpoint of the layer being searched and the interface being searched from}
1496   \textcolor{keywordflow}{elseif} (cs%neutral\_pos\_method == 2) \textcolor{keywordflow}{then}
1497     pos = find\_neutral\_pos\_linear( cs, pos\_last, t\_from, s\_from, drdt\_from, drds\_from, &
1498                                    drdt\_top, drds\_top, drdt\_bot, drds\_bot, t\_poly, s\_poly )
1499   \textcolor{keywordflow}{elseif} (cs%neutral\_pos\_method == 3) \textcolor{keywordflow}{then}
1500     pos = find\_neutral\_pos\_full( cs, pos\_last, t\_from, s\_from, p\_from, p\_top, p\_bot, t\_poly, s\_poly)
1501 \textcolor{keywordflow}{  endif}
1502 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a118ace85483009d5b7a1841e3c745499}\label{namespacemom__neutral__diffusion_a118ace85483009d5b7a1841e3c745499}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!signum@{signum}}
\index{signum@{signum}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{signum()}{signum()}}
{\footnotesize\ttfamily real function mom\+\_\+neutral\+\_\+diffusion\+::signum (\begin{DoxyParamCaption}\item[{real, intent(in)}]{a,  }\item[{real, intent(in)}]{x }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



A true signum function that returns either -\/abs(a), when x$<$0; or abs(a) when x$>$0; or 0 when x=0. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em a} & The magnitude argument\\
\hline
\mbox{\tt in}  & {\em x} & The sign (or zero) argument \\
\hline
\end{DoxyParams}


Definition at line 798 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
798   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: a\textcolor{comment}{ !< The magnitude argument}
799   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{intent(in)} :: x\textcolor{comment}{ !< The sign (or zero) argument}
800 
801   signum = sign(a,x)
802   \textcolor{keywordflow}{if} (x==0.) signum = 0.
803 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a2870eae4b580e548577943e34321b8f0}\label{namespacemom__neutral__diffusion_a2870eae4b580e548577943e34321b8f0}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!test\+\_\+data1d@{test\+\_\+data1d}}
\index{test\+\_\+data1d@{test\+\_\+data1d}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{test\+\_\+data1d()}{test\_data1d()}}
{\footnotesize\ttfamily logical function mom\+\_\+neutral\+\_\+diffusion\+::test\+\_\+data1d (\begin{DoxyParamCaption}\item[{logical, intent(in)}]{verbose,  }\item[{integer, intent(in)}]{nk,  }\item[{real, dimension(nk), intent(in)}]{Po,  }\item[{real, dimension(nk), intent(in)}]{Ptrue,  }\item[{character(len=$\ast$), intent(in)}]{title }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns true if comparison of Po and Ptrue fails, and conditionally writes results to stream. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em verbose} & If true, write results to stdout\\
\hline
\mbox{\tt in}  & {\em nk} & Number of layers\\
\hline
\mbox{\tt in}  & {\em po} & Calculated answer\\
\hline
\mbox{\tt in}  & {\em ptrue} & True answer\\
\hline
\mbox{\tt in}  & {\em title} & Title for messages \\
\hline
\end{DoxyParams}


Definition at line 2706 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2706   \textcolor{keywordtype}{logical},             \textcolor{keywordtype}{intent(in)} :: verbose\textcolor{comment}{ !< If true, write results to stdout}
2707   \textcolor{keywordtype}{integer},             \textcolor{keywordtype}{intent(in)} :: nk\textcolor{comment}{    !< Number of layers}
2708   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)}, \textcolor{keywordtype}{intent(in)} :: Po\textcolor{comment}{    !< Calculated answer}
2709   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(nk)}, \textcolor{keywordtype}{intent(in)} :: Ptrue\textcolor{comment}{ !< True answer}
2710   \textcolor{keywordtype}{character(len=*)},    \textcolor{keywordtype}{intent(in)} :: title\textcolor{comment}{ !< Title for messages}
2711 
2712   \textcolor{comment}{! Local variables}
2713   \textcolor{keywordtype}{integer} :: k, stdunit
2714 
2715   test\_data1d = .false.
2716   \textcolor{keywordflow}{do} k = 1,nk
2717     \textcolor{keywordflow}{if} (po(k) /= ptrue(k)) test\_data1d = .true.
2718 \textcolor{keywordflow}{  enddo}
2719 
2720   \textcolor{keywordflow}{if} (test\_data1d .or. verbose) \textcolor{keywordflow}{then}
2721     stdunit = stdout
2722     \textcolor{keywordflow}{if} (test\_data1d) stdunit = stderr \textcolor{comment}{! In case of wrong results, write to error stream}
2723     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(a)'}) title
2724     \textcolor{keywordflow}{do} k = 1,nk
2725       \textcolor{keywordflow}{if} (po(k) /= ptrue(k)) \textcolor{keywordflow}{then}
2726         test\_data1d = .true.
2727         \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(a,i2,2(x,a,f20.16),x,a,1pe22.15,x,a)'}) &
2728               \textcolor{stringliteral}{'k='},k,\textcolor{stringliteral}{'Po='},po(k),\textcolor{stringliteral}{'Ptrue='},ptrue(k),\textcolor{stringliteral}{'err='},po(k)-ptrue(k),\textcolor{stringliteral}{'WRONG!'}
2729       \textcolor{keywordflow}{else}
2730         \textcolor{keywordflow}{if} (verbose) &
2731           \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(a,i2,2(x,a,f20.16),x,a,1pe22.15)'}) &
2732                 \textcolor{stringliteral}{'k='},k,\textcolor{stringliteral}{'Po='},po(k),\textcolor{stringliteral}{'Ptrue='},ptrue(k),\textcolor{stringliteral}{'err='},po(k)-ptrue(k)
2733 \textcolor{keywordflow}{      endif}
2734 \textcolor{keywordflow}{    enddo}
2735 \textcolor{keywordflow}{  endif}
2736 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_aaa2d682e80cdba470a09bcff6bda1767}\label{namespacemom__neutral__diffusion_aaa2d682e80cdba470a09bcff6bda1767}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!test\+\_\+data1di@{test\+\_\+data1di}}
\index{test\+\_\+data1di@{test\+\_\+data1di}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{test\+\_\+data1di()}{test\_data1di()}}
{\footnotesize\ttfamily logical function mom\+\_\+neutral\+\_\+diffusion\+::test\+\_\+data1di (\begin{DoxyParamCaption}\item[{logical, intent(in)}]{verbose,  }\item[{integer, intent(in)}]{nk,  }\item[{integer, dimension(nk), intent(in)}]{Po,  }\item[{integer, dimension(nk), intent(in)}]{Ptrue,  }\item[{character(len=$\ast$), intent(in)}]{title }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns true if comparison of Po and Ptrue fails, and conditionally writes results to stream. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em verbose} & If true, write results to stdout\\
\hline
\mbox{\tt in}  & {\em nk} & Number of layers\\
\hline
\mbox{\tt in}  & {\em po} & Calculated answer\\
\hline
\mbox{\tt in}  & {\em ptrue} & True answer\\
\hline
\mbox{\tt in}  & {\em title} & Title for messages \\
\hline
\end{DoxyParams}


Definition at line 2741 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2741   \textcolor{keywordtype}{logical},                \textcolor{keywordtype}{intent(in)} :: verbose\textcolor{comment}{ !< If true, write results to stdout}
2742   \textcolor{keywordtype}{integer},                \textcolor{keywordtype}{intent(in)} :: nk\textcolor{comment}{    !< Number of layers}
2743   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(nk)}, \textcolor{keywordtype}{intent(in)} :: Po\textcolor{comment}{    !< Calculated answer}
2744   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(nk)}, \textcolor{keywordtype}{intent(in)} :: Ptrue\textcolor{comment}{ !< True answer}
2745   \textcolor{keywordtype}{character(len=*)},       \textcolor{keywordtype}{intent(in)} :: title\textcolor{comment}{ !< Title for messages}
2746 
2747   \textcolor{comment}{! Local variables}
2748   \textcolor{keywordtype}{integer} :: k, stdunit
2749 
2750   test\_data1di = .false.
2751   \textcolor{keywordflow}{do} k = 1,nk
2752     \textcolor{keywordflow}{if} (po(k) /= ptrue(k)) test\_data1di = .true.
2753 \textcolor{keywordflow}{  enddo}
2754 
2755   \textcolor{keywordflow}{if} (test\_data1di .or. verbose) \textcolor{keywordflow}{then}
2756     stdunit = stdout
2757     \textcolor{keywordflow}{if} (test\_data1di) stdunit = stderr \textcolor{comment}{! In case of wrong results, write to error stream}
2758     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(a)'}) title
2759     \textcolor{keywordflow}{do} k = 1,nk
2760       \textcolor{keywordflow}{if} (po(k) /= ptrue(k)) \textcolor{keywordflow}{then}
2761         test\_data1di = .true.
2762         \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(a,i2,2(x,a,i5),x,a)'}) \textcolor{stringliteral}{'k='},k,\textcolor{stringliteral}{'Io='},po(k),\textcolor{stringliteral}{'Itrue='},ptrue(k),\textcolor{stringliteral}{'WRONG!'}
2763       \textcolor{keywordflow}{else}
2764         \textcolor{keywordflow}{if} (verbose) &
2765           \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(a,i2,2(x,a,i5))'}) \textcolor{stringliteral}{'k='},k,\textcolor{stringliteral}{'Io='},po(k),\textcolor{stringliteral}{'Itrue='},ptrue(k)
2766 \textcolor{keywordflow}{      endif}
2767 \textcolor{keywordflow}{    enddo}
2768 \textcolor{keywordflow}{  endif}
2769 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_ac51219df490c739774d70d9958bae047}\label{namespacemom__neutral__diffusion_ac51219df490c739774d70d9958bae047}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!test\+\_\+fv\+\_\+diff@{test\+\_\+fv\+\_\+diff}}
\index{test\+\_\+fv\+\_\+diff@{test\+\_\+fv\+\_\+diff}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{test\+\_\+fv\+\_\+diff()}{test\_fv\_diff()}}
{\footnotesize\ttfamily logical function mom\+\_\+neutral\+\_\+diffusion\+::test\+\_\+fv\+\_\+diff (\begin{DoxyParamCaption}\item[{logical, intent(in)}]{verbose,  }\item[{real, intent(in)}]{hkm1,  }\item[{real, intent(in)}]{hk,  }\item[{real, intent(in)}]{hkp1,  }\item[{real, intent(in)}]{Skm1,  }\item[{real, intent(in)}]{Sk,  }\item[{real, intent(in)}]{Skp1,  }\item[{real, intent(in)}]{Ptrue,  }\item[{character(len=$\ast$), intent(in)}]{title }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns true if a test of \mbox{\hyperlink{namespacemom__neutral__diffusion_a1c5ac28d7c54581cdd872fe58ef0b204}{fv\+\_\+diff()}} fails, and conditionally writes results to stream. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em verbose} & If true, write results to stdout\\
\hline
\mbox{\tt in}  & {\em hkm1} & Left cell width \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em hk} & Center cell width \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em hkp1} & Right cell width \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em skm1} & Left cell average value\\
\hline
\mbox{\tt in}  & {\em sk} & Center cell average value\\
\hline
\mbox{\tt in}  & {\em skp1} & Right cell average value\\
\hline
\mbox{\tt in}  & {\em ptrue} & True answer \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em title} & Title for messages \\
\hline
\end{DoxyParams}


Definition at line 2610 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2610   \textcolor{keywordtype}{logical},          \textcolor{keywordtype}{intent(in)} :: verbose\textcolor{comment}{ !< If true, write results to stdout}
2611   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: hkm1\textcolor{comment}{  !< Left cell width [nondim]}
2612   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: hk\textcolor{comment}{    !< Center cell width [nondim]}
2613   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: hkp1\textcolor{comment}{  !< Right cell width [nondim]}
2614   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: Skm1\textcolor{comment}{  !< Left cell average value}
2615   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: Sk\textcolor{comment}{    !< Center cell average value}
2616   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: Skp1\textcolor{comment}{  !< Right cell average value}
2617   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: Ptrue\textcolor{comment}{ !< True answer [nondim]}
2618   \textcolor{keywordtype}{character(len=*)}, \textcolor{keywordtype}{intent(in)} :: title\textcolor{comment}{ !< Title for messages}
2619 
2620   \textcolor{comment}{! Local variables}
2621   \textcolor{keywordtype}{integer} :: stdunit
2622   \textcolor{keywordtype}{real} :: Pret
2623 
2624   pret = fv\_diff(hkm1, hk, hkp1, skm1, sk, skp1)
2625   test\_fv\_diff = (pret /= ptrue)
2626 
2627   \textcolor{keywordflow}{if} (test\_fv\_diff .or. verbose) \textcolor{keywordflow}{then}
2628     stdunit = stdout
2629     \textcolor{keywordflow}{if} (test\_fv\_diff) stdunit = stderr \textcolor{comment}{! In case of wrong results, write to error stream}
2630     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(a)'}) title
2631     \textcolor{keywordflow}{if} (test\_fv\_diff) \textcolor{keywordflow}{then}
2632       \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(2(x,a,f20.16),x,a)'}) \textcolor{stringliteral}{'pRet='},pret,\textcolor{stringliteral}{'pTrue='},ptrue,\textcolor{stringliteral}{'WRONG!'}
2633     \textcolor{keywordflow}{else}
2634       \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(2(x,a,f20.16))'}) \textcolor{stringliteral}{'pRet='},pret,\textcolor{stringliteral}{'pTrue='},ptrue
2635 \textcolor{keywordflow}{    endif}
2636 \textcolor{keywordflow}{  endif}
2637 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_ad964ab49cd5901b79dc32f2367d24aed}\label{namespacemom__neutral__diffusion_ad964ab49cd5901b79dc32f2367d24aed}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!test\+\_\+fvlsq\+\_\+slope@{test\+\_\+fvlsq\+\_\+slope}}
\index{test\+\_\+fvlsq\+\_\+slope@{test\+\_\+fvlsq\+\_\+slope}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{test\+\_\+fvlsq\+\_\+slope()}{test\_fvlsq\_slope()}}
{\footnotesize\ttfamily logical function mom\+\_\+neutral\+\_\+diffusion\+::test\+\_\+fvlsq\+\_\+slope (\begin{DoxyParamCaption}\item[{logical, intent(in)}]{verbose,  }\item[{real, intent(in)}]{hkm1,  }\item[{real, intent(in)}]{hk,  }\item[{real, intent(in)}]{hkp1,  }\item[{real, intent(in)}]{Skm1,  }\item[{real, intent(in)}]{Sk,  }\item[{real, intent(in)}]{Skp1,  }\item[{real, intent(in)}]{Ptrue,  }\item[{character(len=$\ast$), intent(in)}]{title }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns true if a test of \mbox{\hyperlink{namespacemom__neutral__diffusion_a6ed3814088af470ad6fc4a3a80926fd7}{fvlsq\+\_\+slope()}} fails, and conditionally writes results to stream. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em verbose} & If true, write results to stdout\\
\hline
\mbox{\tt in}  & {\em hkm1} & Left cell width\\
\hline
\mbox{\tt in}  & {\em hk} & Center cell width\\
\hline
\mbox{\tt in}  & {\em hkp1} & Right cell width\\
\hline
\mbox{\tt in}  & {\em skm1} & Left cell average value\\
\hline
\mbox{\tt in}  & {\em sk} & Center cell average value\\
\hline
\mbox{\tt in}  & {\em skp1} & Right cell average value\\
\hline
\mbox{\tt in}  & {\em ptrue} & True answer\\
\hline
\mbox{\tt in}  & {\em title} & Title for messages \\
\hline
\end{DoxyParams}


Definition at line 2642 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2642   \textcolor{keywordtype}{logical},          \textcolor{keywordtype}{intent(in)} :: verbose\textcolor{comment}{ !< If true, write results to stdout}
2643   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: hkm1\textcolor{comment}{  !< Left cell width}
2644   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: hk\textcolor{comment}{    !< Center cell width}
2645   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: hkp1\textcolor{comment}{  !< Right cell width}
2646   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: Skm1\textcolor{comment}{  !< Left cell average value}
2647   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: Sk\textcolor{comment}{    !< Center cell average value}
2648   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: Skp1\textcolor{comment}{  !< Right cell average value}
2649   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: Ptrue\textcolor{comment}{ !< True answer}
2650   \textcolor{keywordtype}{character(len=*)}, \textcolor{keywordtype}{intent(in)} :: title\textcolor{comment}{ !< Title for messages}
2651 
2652   \textcolor{comment}{! Local variables}
2653   \textcolor{keywordtype}{integer} :: stdunit
2654   \textcolor{keywordtype}{real} :: Pret
2655 
2656   pret = fvlsq\_slope(hkm1, hk, hkp1, skm1, sk, skp1)
2657   test\_fvlsq\_slope = (pret /= ptrue)
2658 
2659   \textcolor{keywordflow}{if} (test\_fvlsq\_slope .or. verbose) \textcolor{keywordflow}{then}
2660     stdunit = stdout
2661     \textcolor{keywordflow}{if} (test\_fvlsq\_slope) stdunit = stderr \textcolor{comment}{! In case of wrong results, write to error stream}
2662     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(a)'}) title
2663     \textcolor{keywordflow}{if} (test\_fvlsq\_slope) \textcolor{keywordflow}{then}
2664       \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(2(x,a,f20.16),x,a)'}) \textcolor{stringliteral}{'pRet='},pret,\textcolor{stringliteral}{'pTrue='},ptrue,\textcolor{stringliteral}{'WRONG!'}
2665     \textcolor{keywordflow}{else}
2666       \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(2(x,a,f20.16))'}) \textcolor{stringliteral}{'pRet='},pret,\textcolor{stringliteral}{'pTrue='},ptrue
2667 \textcolor{keywordflow}{    endif}
2668 \textcolor{keywordflow}{  endif}
2669 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_a1a21234946c77d1e4ed852a4173503f1}\label{namespacemom__neutral__diffusion_a1a21234946c77d1e4ed852a4173503f1}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!test\+\_\+ifndp@{test\+\_\+ifndp}}
\index{test\+\_\+ifndp@{test\+\_\+ifndp}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{test\+\_\+ifndp()}{test\_ifndp()}}
{\footnotesize\ttfamily logical function mom\+\_\+neutral\+\_\+diffusion\+::test\+\_\+ifndp (\begin{DoxyParamCaption}\item[{logical, intent(in)}]{verbose,  }\item[{real, intent(in)}]{rho\+Neg,  }\item[{real, intent(in)}]{Pneg,  }\item[{real, intent(in)}]{rho\+Pos,  }\item[{real, intent(in)}]{Ppos,  }\item[{real, intent(in)}]{Ptrue,  }\item[{character(len=$\ast$), intent(in)}]{title }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns true if a test of \mbox{\hyperlink{namespacemom__neutral__diffusion_a666836e70bdae7ef4ec271246cf91993}{interpolate\+\_\+for\+\_\+nondim\+\_\+position()}} fails, and conditionally writes results to stream. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em verbose} & If true, write results to stdout\\
\hline
\mbox{\tt in}  & {\em rhoneg} & Lighter density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}\\
\hline
\mbox{\tt in}  & {\em pneg} & Interface position of lighter density \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em rhopos} & Heavier density \mbox{[}R $\sim$$>$ kg m-\/3\mbox{]}\\
\hline
\mbox{\tt in}  & {\em ppos} & Interface position of heavier density \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em ptrue} & True answer \mbox{[}nondim\mbox{]}\\
\hline
\mbox{\tt in}  & {\em title} & Title for messages \\
\hline
\end{DoxyParams}


Definition at line 2674 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2674   \textcolor{keywordtype}{logical},          \textcolor{keywordtype}{intent(in)} :: verbose\textcolor{comment}{ !< If true, write results to stdout}
2675   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: rhoNeg\textcolor{comment}{ !< Lighter density [R ~> kg m-3]}
2676   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: Pneg\textcolor{comment}{   !< Interface position of lighter density [nondim]}
2677   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: rhoPos\textcolor{comment}{ !< Heavier density [R ~> kg m-3]}
2678   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: Ppos\textcolor{comment}{   !< Interface position of heavier density [nondim]}
2679   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: Ptrue\textcolor{comment}{  !< True answer [nondim]}
2680   \textcolor{keywordtype}{character(len=*)}, \textcolor{keywordtype}{intent(in)} :: title\textcolor{comment}{  !< Title for messages}
2681 
2682   \textcolor{comment}{! Local variables}
2683   \textcolor{keywordtype}{integer} :: stdunit
2684   \textcolor{keywordtype}{real} :: Pret
2685 
2686   pret = interpolate\_for\_nondim\_position(rhoneg, pneg, rhopos, ppos)
2687   test\_ifndp = (pret /= ptrue)
2688 
2689   \textcolor{keywordflow}{if} (test\_ifndp .or. verbose) \textcolor{keywordflow}{then}
2690     stdunit = stdout
2691     \textcolor{keywordflow}{if} (test\_ifndp) stdunit = stderr \textcolor{comment}{! In case of wrong results, write to error stream}
2692     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(a)'}) title
2693     \textcolor{keywordflow}{if} (test\_ifndp) \textcolor{keywordflow}{then}
2694       \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(4(x,a,f20.16),2(x,a,1pe22.15),x,a)'}) &
2695             \textcolor{stringliteral}{'r1='},rhoneg,\textcolor{stringliteral}{'p1='},pneg,\textcolor{stringliteral}{'r2='},rhopos,\textcolor{stringliteral}{'p2='},ppos,\textcolor{stringliteral}{'pRet='},pret,\textcolor{stringliteral}{'pTrue='},ptrue,\textcolor{stringliteral}{'WRONG!'}
2696     \textcolor{keywordflow}{else}
2697       \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(4(x,a,f20.16),2(x,a,1pe22.15))'}) &
2698             \textcolor{stringliteral}{'r1='},rhoneg,\textcolor{stringliteral}{'p1='},pneg,\textcolor{stringliteral}{'r2='},rhopos,\textcolor{stringliteral}{'p2='},ppos,\textcolor{stringliteral}{'pRet='},pret,\textcolor{stringliteral}{'pTrue='},ptrue
2699 \textcolor{keywordflow}{    endif}
2700 \textcolor{keywordflow}{  endif}
2701 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_ab5eaa2002c1f6405f5037645bbca3101}\label{namespacemom__neutral__diffusion_ab5eaa2002c1f6405f5037645bbca3101}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!test\+\_\+nsp@{test\+\_\+nsp}}
\index{test\+\_\+nsp@{test\+\_\+nsp}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{test\+\_\+nsp()}{test\_nsp()}}
{\footnotesize\ttfamily logical function mom\+\_\+neutral\+\_\+diffusion\+::test\+\_\+nsp (\begin{DoxyParamCaption}\item[{logical, intent(in)}]{verbose,  }\item[{integer, intent(in)}]{ns,  }\item[{integer, dimension(ns), intent(in)}]{KoL,  }\item[{integer, dimension(ns), intent(in)}]{KoR,  }\item[{real, dimension(ns), intent(in)}]{pL,  }\item[{real, dimension(ns), intent(in)}]{pR,  }\item[{real, dimension(ns-\/1), intent(in)}]{h\+Eff,  }\item[{integer, dimension(ns), intent(in)}]{Ko\+L0,  }\item[{integer, dimension(ns), intent(in)}]{Ko\+R0,  }\item[{real, dimension(ns), intent(in)}]{p\+L0,  }\item[{real, dimension(ns), intent(in)}]{p\+R0,  }\item[{real, dimension(ns-\/1), intent(in)}]{h\+Eff0,  }\item[{character(len=$\ast$), intent(in)}]{title }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Returns true if output of find\+\_\+neutral\+\_\+surface\+\_\+positions() does not match correct values, and conditionally writes results to stream. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em verbose} & If true, write results to stdout\\
\hline
\mbox{\tt in}  & {\em ns} & Number of surfaces\\
\hline
\mbox{\tt in}  & {\em kol} & Index of first left interface above neutral surface\\
\hline
\mbox{\tt in}  & {\em kor} & Index of first right interface above neutral surface\\
\hline
\mbox{\tt in}  & {\em pl} & Fractional position of neutral surface within layer KoL of left column\\
\hline
\mbox{\tt in}  & {\em pr} & Fractional position of neutral surface within layer KoR of right column\\
\hline
\mbox{\tt in}  & {\em heff} & Effective thickness between two neutral surfaces \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}\\
\hline
\mbox{\tt in}  & {\em kol0} & Correct value for KoL\\
\hline
\mbox{\tt in}  & {\em kor0} & Correct value for KoR\\
\hline
\mbox{\tt in}  & {\em pl0} & Correct value for pL\\
\hline
\mbox{\tt in}  & {\em pr0} & Correct value for pR\\
\hline
\mbox{\tt in}  & {\em heff0} & Correct value for h\+Eff\\
\hline
\mbox{\tt in}  & {\em title} & Title for messages \\
\hline
\end{DoxyParams}


Definition at line 2775 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2775   \textcolor{keywordtype}{logical},                \textcolor{keywordtype}{intent(in)} :: verbose\textcolor{comment}{ !< If true, write results to stdout}
2776   \textcolor{keywordtype}{integer},                \textcolor{keywordtype}{intent(in)} :: ns\textcolor{comment}{    !< Number of surfaces}
2777   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(ns)}, \textcolor{keywordtype}{intent(in)} :: KoL\textcolor{comment}{   !< Index of first left interface above neutral surface}
2778   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(ns)}, \textcolor{keywordtype}{intent(in)} :: KoR\textcolor{comment}{   !< Index of first right interface above neutral surface}
2779   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(ns)},    \textcolor{keywordtype}{intent(in)} :: pL\textcolor{comment}{    !< Fractional position of neutral surface within layer KoL of
       left column}
2780   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(ns)},    \textcolor{keywordtype}{intent(in)} :: pR\textcolor{comment}{    !< Fractional position of neutral surface within layer KoR of
       right column}
2781   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(ns-1)},  \textcolor{keywordtype}{intent(in)} :: hEff\textcolor{comment}{  !< Effective thickness between two neutral surfaces [R L2 T-2
       ~> Pa]}
2782   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(ns)}, \textcolor{keywordtype}{intent(in)} :: KoL0\textcolor{comment}{  !< Correct value for KoL}
2783   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(ns)}, \textcolor{keywordtype}{intent(in)} :: KoR0\textcolor{comment}{  !< Correct value for KoR}
2784   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(ns)},    \textcolor{keywordtype}{intent(in)} :: pL0\textcolor{comment}{   !< Correct value for pL}
2785   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(ns)},    \textcolor{keywordtype}{intent(in)} :: pR0\textcolor{comment}{   !< Correct value for pR}
2786   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(ns-1)},  \textcolor{keywordtype}{intent(in)} :: hEff0\textcolor{comment}{ !< Correct value for hEff}
2787   \textcolor{keywordtype}{character(len=*)},       \textcolor{keywordtype}{intent(in)} :: title\textcolor{comment}{ !< Title for messages}
2788 
2789   \textcolor{comment}{! Local variables}
2790   \textcolor{keywordtype}{integer} :: k, stdunit
2791   \textcolor{keywordtype}{logical} :: this\_row\_failed
2792 
2793   test\_nsp = .false.
2794   \textcolor{keywordflow}{do} k = 1,ns
2795     test\_nsp = test\_nsp .or. compare\_nsp\_row(kol(k), kor(k), pl(k), pr(k), kol0(k), kor0(k), pl0(k), pr0(k)
      )
2796     \textcolor{keywordflow}{if} (k < ns) \textcolor{keywordflow}{then}
2797       \textcolor{keywordflow}{if} (heff(k) /= heff0(k)) test\_nsp = .true.
2798 \textcolor{keywordflow}{    endif}
2799 \textcolor{keywordflow}{  enddo}
2800 
2801   \textcolor{keywordflow}{if} (test\_nsp .or. verbose) \textcolor{keywordflow}{then}
2802     stdunit = stdout
2803     \textcolor{keywordflow}{if} (test\_nsp) stdunit = stderr \textcolor{comment}{! In case of wrong results, write to error stream}
2804     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(a)'}) title
2805     \textcolor{keywordflow}{do} k = 1,ns
2806       this\_row\_failed = compare\_nsp\_row(kol(k), kor(k), pl(k), pr(k), kol0(k), kor0(k), pl0(k), pr0(k))
2807       \textcolor{keywordflow}{if} (this\_row\_failed) \textcolor{keywordflow}{then}
2808         \textcolor{keyword}{write}(stdunit,10) k,kol(k),pl(k),kor(k),pr(k),\textcolor{stringliteral}{' <-- WRONG!'}
2809         \textcolor{keyword}{write}(stdunit,10) k,kol0(k),pl0(k),kor0(k),pr0(k),\textcolor{stringliteral}{' <-- should be this'}
2810       \textcolor{keywordflow}{else}
2811         \textcolor{keyword}{write}(stdunit,10) k,kol(k),pl(k),kor(k),pr(k)
2812 \textcolor{keywordflow}{      endif}
2813       \textcolor{keywordflow}{if} (k < ns) \textcolor{keywordflow}{then}
2814         \textcolor{keywordflow}{if} (heff(k) /= heff0(k)) \textcolor{keywordflow}{then}
2815           \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(i3,8x,"layer hEff =",2(f20.16,a))'}) k,heff(k),\textcolor{stringliteral}{" .neq. "},heff0(k),\textcolor{stringliteral}{' <-- WRONG!'}
2816         \textcolor{keywordflow}{else}
2817           \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(i3,8x,"layer hEff =",f20.16)'}) k,heff(k)
2818 \textcolor{keywordflow}{        endif}
2819 \textcolor{keywordflow}{      endif}
2820 \textcolor{keywordflow}{    enddo}
2821 \textcolor{keywordflow}{  endif}
2822   \textcolor{keywordflow}{if} (test\_nsp) \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"test\_nsp failed"})
2823 
2824 10 \textcolor{keyword}{format}(\textcolor{stringliteral}{"ks="},i3,\textcolor{stringliteral}{" kL="},i3,\textcolor{stringliteral}{" pL="},f20.16,\textcolor{stringliteral}{" kR="},i3,\textcolor{stringliteral}{" pR="},f20.16,a)
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__neutral__diffusion_ab75333897fa8800441d8eddc1720f631}\label{namespacemom__neutral__diffusion_ab75333897fa8800441d8eddc1720f631}} 
\index{mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}!test\+\_\+rnp@{test\+\_\+rnp}}
\index{test\+\_\+rnp@{test\+\_\+rnp}!mom\+\_\+neutral\+\_\+diffusion@{mom\+\_\+neutral\+\_\+diffusion}}
\subsubsection{\texorpdfstring{test\+\_\+rnp()}{test\_rnp()}}
{\footnotesize\ttfamily logical function mom\+\_\+neutral\+\_\+diffusion\+::test\+\_\+rnp (\begin{DoxyParamCaption}\item[{real, intent(in)}]{expected\+\_\+pos,  }\item[{real, intent(in)}]{test\+\_\+pos,  }\item[{character(len=$\ast$), intent(in)}]{title }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Compares output position from refine\+\_\+nondim\+\_\+position with an expected value. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em expected\+\_\+pos} & The expected position\\
\hline
\mbox{\tt in}  & {\em test\+\_\+pos} & The position returned by the code\\
\hline
\mbox{\tt in}  & {\em title} & A label for this test \\
\hline
\end{DoxyParams}


Definition at line 2847 of file M\+O\+M\+\_\+neutral\+\_\+diffusion.\+F90.


\begin{DoxyCode}
2847   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: expected\_pos\textcolor{comment}{ !< The expected position}
2848   \textcolor{keywordtype}{real},             \textcolor{keywordtype}{intent(in)} :: test\_pos\textcolor{comment}{ !< The position returned by the code}
2849   \textcolor{keywordtype}{character(len=*)}, \textcolor{keywordtype}{intent(in)} :: title\textcolor{comment}{    !< A label for this test}
2850   \textcolor{comment}{! Local variables}
2851   \textcolor{keywordtype}{integer} :: stdunit
2852 
2853   stdunit = stdout \textcolor{comment}{! Output to standard error}
2854   test\_rnp = abs(expected\_pos - test\_pos) > 2*epsilon(test\_pos)
2855   \textcolor{keywordflow}{if} (test\_rnp) \textcolor{keywordflow}{then}
2856     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(A, f20.16, " .neq. ", f20.16, " <-- WRONG")'}) title, expected\_pos, test\_pos
2857   \textcolor{keywordflow}{else}
2858     \textcolor{keyword}{write}(stdunit,\textcolor{stringliteral}{'(A, f20.16, " ==  ", f20.16)'}) title, expected\_pos, test\_pos
2859 \textcolor{keywordflow}{  endif}
\end{DoxyCode}
