\hypertarget{namespacemom__tracer__hor__diff}{}\doxysection{mom\+\_\+tracer\+\_\+hor\+\_\+diff Module Reference}
\label{namespacemom__tracer__hor__diff}\index{mom\_tracer\_hor\_diff@{mom\_tracer\_hor\_diff}}


\doxysubsection{Detailed Description}
Main routine for lateral (along surface or neutral) diffusion of tracers. 

\hypertarget{namespacemom__tracer__hor__diff_section_intro}{}\doxysubsection{Introduction to the module}\label{namespacemom__tracer__hor__diff_section_intro}
This module contains subroutines that handle horizontal diffusion (i.\+e., isoneutral or along layer) of tracers.

Each of the tracers are subject to Fickian along-\/coordinate diffusion if Khtr is defined and positive. The tracer diffusion can use a suitable number of iterations to guarantee stability with an arbitrarily large time step. \doxysubsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__tracer__hor__diff_1_1p2d}{p2d}}
\begin{DoxyCompactList}\small\item\em A type that can be used to create arrays of pointers to 2D arrays. \end{DoxyCompactList}\item 
type \mbox{\hyperlink{structmom__tracer__hor__diff_1_1p2di}{p2di}}
\begin{DoxyCompactList}\small\item\em A type that can be used to create arrays of pointers to 2D integer arrays. \end{DoxyCompactList}\item 
type \mbox{\hyperlink{structmom__tracer__hor__diff_1_1tracer__hor__diff__cs}{tracer\+\_\+hor\+\_\+diff\+\_\+cs}}
\begin{DoxyCompactList}\small\item\em The control structure for along-\/layer and epineutral tracer diffusion. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \mbox{\hyperlink{namespacemom__tracer__hor__diff_a098229e37012e7bd93d13036bfc864ac}{tracer\+\_\+hordiff}} (h, dt, M\+E\+KE, Var\+Mix, G, GV, US, CS, Reg, tv, do\+\_\+online\+\_\+flag, read\+\_\+khdt\+\_\+x, read\+\_\+khdt\+\_\+y)
\begin{DoxyCompactList}\small\item\em Compute along-\/coordinate diffusion of all tracers using the diffusivity in CSKh\+Tr, or using space-\/dependent diffusivity. Multiple iterations are used (if necessary) so that there is no limit on the acceptable time increment. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__tracer__hor__diff_af6d8a8262d4c1030fc02aae4cd062821}{tracer\+\_\+epipycnal\+\_\+ml\+\_\+diff}} (h, dt, Tr, ntr, khdt\+\_\+epi\+\_\+x, khdt\+\_\+epi\+\_\+y, G, GV, US, CS, tv, num\+\_\+itts)
\begin{DoxyCompactList}\small\item\em This subroutine does epipycnal diffusion of all tracers between the mixed and buffer layers and the interior, using the diffusivity in CSKh\+Tr. Multiple iterations are used (if necessary) so that there is no limit on the acceptable time increment. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__tracer__hor__diff_a87eed0408da0c4732372732b34451b73}{tracer\+\_\+hor\+\_\+diff\+\_\+init}} (Time, G, US, param\+\_\+file, diag, E\+OS, diabatic\+\_\+\+C\+Sp, CS)
\begin{DoxyCompactList}\small\item\em Initialize lateral tracer diffusion module. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__tracer__hor__diff_a715439f7286842d78d2ce52b7e5371a4}{tracer\+\_\+hor\+\_\+diff\+\_\+end}} (CS)
\end{DoxyCompactItemize}


\doxysubsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__tracer__hor__diff_af6d8a8262d4c1030fc02aae4cd062821}\label{namespacemom__tracer__hor__diff_af6d8a8262d4c1030fc02aae4cd062821}} 
\index{mom\_tracer\_hor\_diff@{mom\_tracer\_hor\_diff}!tracer\_epipycnal\_ml\_diff@{tracer\_epipycnal\_ml\_diff}}
\index{tracer\_epipycnal\_ml\_diff@{tracer\_epipycnal\_ml\_diff}!mom\_tracer\_hor\_diff@{mom\_tracer\_hor\_diff}}
\doxysubsubsection{\texorpdfstring{tracer\_epipycnal\_ml\_diff()}{tracer\_epipycnal\_ml\_diff()}}
{\footnotesize\ttfamily subroutine mom\+\_\+tracer\+\_\+hor\+\_\+diff\+::tracer\+\_\+epipycnal\+\_\+ml\+\_\+diff (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{real, intent(in)}]{dt,  }\item[{type(tracer\+\_\+type), dimension(\+:), intent(inout)}]{Tr,  }\item[{integer, intent(in)}]{ntr,  }\item[{real, dimension(szib\+\_\+(g),szj\+\_\+(g)), intent(in)}]{khdt\+\_\+epi\+\_\+x,  }\item[{real, dimension(szi\+\_\+(g),szjb\+\_\+(g)), intent(in)}]{khdt\+\_\+epi\+\_\+y,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(inout)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__tracer__hor__diff_1_1tracer__hor__diff__cs}{tracer\+\_\+hor\+\_\+diff\+\_\+cs}}), intent(inout)}]{CS,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{integer, intent(in)}]{num\+\_\+itts }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine does epipycnal diffusion of all tracers between the mixed and buffer layers and the interior, using the diffusivity in CSKh\+Tr. Multiple iterations are used (if necessary) so that there is no limit on the acceptable time increment. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em g} & ocean grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & ocean vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em h} & layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & time step \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em tr} & tracer array \\
\hline
\mbox{\texttt{ in}}  & {\em ntr} & number of tracers \\
\hline
\mbox{\texttt{ in}}  & {\em khdt\+\_\+epi\+\_\+x} & Zonal epipycnal diffusivity times a time step and the ratio of the open face width over the distance between adjacent tracer points \mbox{[}L2 $\sim$$>$ m2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em khdt\+\_\+epi\+\_\+y} & Meridional epipycnal diffusivity times a time step and the ratio of the open face width over the distance between adjacent tracer points \mbox{[}L2 $\sim$$>$ m2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in,out}}  & {\em cs} & module control structure \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & thermodynamic structure \\
\hline
\mbox{\texttt{ in}}  & {\em num\+\_\+itts} & number of iterations (usually=1) \\
\hline
\end{DoxyParams}


Definition at line 587 of file M\+O\+M\+\_\+tracer\+\_\+hor\+\_\+diff.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{589   \textcolor{keywordtype}{type}(ocean\_grid\_type),                    \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{          !< ocean grid structure}}
\DoxyCodeLine{590   \textcolor{keywordtype}{type}(verticalGrid\_type),                  \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{         !< ocean vertical grid structure}}
\DoxyCodeLine{591 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{          !< layer thickness [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{592 \textcolor{keywordtype}{  real},                                     \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{         !< time step [T \string~> s]}}
\DoxyCodeLine{593   \textcolor{keywordtype}{type}(tracer\_type),                        \textcolor{keywordtype}{intent(inout)} :: Tr(:)\textcolor{comment}{      !< tracer array}}
\DoxyCodeLine{594   \textcolor{keywordtype}{integer},                                  \textcolor{keywordtype}{intent(in)}    :: ntr\textcolor{comment}{        !< number of tracers}}
\DoxyCodeLine{595 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))},        \textcolor{keywordtype}{intent(in)}    :: khdt\_epi\_x\textcolor{comment}{ !< Zonal epipycnal diffusivity times}}
\DoxyCodeLine{596 \textcolor{comment}{                                                           !! a time step and the ratio of the open face width over}}
\DoxyCodeLine{597 \textcolor{comment}{                                                           !! the distance between adjacent tracer points [L2 \string~> m2]}}
\DoxyCodeLine{598 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))},        \textcolor{keywordtype}{intent(in)}    :: khdt\_epi\_y\textcolor{comment}{ !< Meridional epipycnal diffusivity times}}
\DoxyCodeLine{599 \textcolor{comment}{                                                           !! a time step and the ratio of the open face width over}}
\DoxyCodeLine{600 \textcolor{comment}{                                                           !! the distance between adjacent tracer points [L2 \string~> m2]}}
\DoxyCodeLine{601   \textcolor{keywordtype}{type}(unit\_scale\_type),                    \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{ !< A dimensional unit scaling type}}
\DoxyCodeLine{602   \textcolor{keywordtype}{type}(tracer\_hor\_diff\_CS),                 \textcolor{keywordtype}{intent(inout)} :: CS\textcolor{comment}{         !< module control structure}}
\DoxyCodeLine{603   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),                    \textcolor{keywordtype}{intent(in)}    :: tv\textcolor{comment}{         !< thermodynamic structure}}
\DoxyCodeLine{604   \textcolor{keywordtype}{integer},                                  \textcolor{keywordtype}{intent(in)}    :: num\_itts\textcolor{comment}{   !< number of iterations (usually=1)}}
\DoxyCodeLine{605 }
\DoxyCodeLine{606 }
\DoxyCodeLine{607 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G), SZJ\_(G))} :: \&}
\DoxyCodeLine{608     Rml\_max  \textcolor{comment}{! The maximum coordinate density within the mixed layer [R \string~> kg m-\/3].}}
\DoxyCodeLine{609 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G), SZJ\_(G), max(1,GV\%nk\_rho\_varies))} :: \&}
\DoxyCodeLine{610     rho\_coord \textcolor{comment}{! The coordinate density that is used to mix along [R \string~> kg m-\/3].}}
\DoxyCodeLine{611 }
\DoxyCodeLine{612   \textcolor{comment}{! The naming mnemonic is a=above,b=below,L=Left,R=Right,u=u-\/point,v=v-\/point.}}
\DoxyCodeLine{613   \textcolor{comment}{! These are 1-\/D arrays of pointers to 2-\/d arrays to minimize memory usage.}}
\DoxyCodeLine{614   \textcolor{keywordtype}{type}(p2d), \textcolor{keywordtype}{dimension(SZJ\_(G))} :: \&}
\DoxyCodeLine{615     deep\_wt\_Lu, deep\_wt\_Ru, \&  \textcolor{comment}{! The relative weighting of the deeper of a pair [nondim].}}
\DoxyCodeLine{616     hP\_Lu, hP\_Ru       \textcolor{comment}{! The total thickness on each side for each pair [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{617 }
\DoxyCodeLine{618   \textcolor{keywordtype}{type}(p2d), \textcolor{keywordtype}{dimension(SZJB\_(G))} :: \&}
\DoxyCodeLine{619     deep\_wt\_Lv, deep\_wt\_Rv, \& \textcolor{comment}{! The relative weighting of the deeper of a pair [nondim].}}
\DoxyCodeLine{620     hP\_Lv, hP\_Rv       \textcolor{comment}{! The total thickness on each side for each pair [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{621 }
\DoxyCodeLine{622   \textcolor{keywordtype}{type}(p2di), \textcolor{keywordtype}{dimension(SZJ\_(G))} :: \&}
\DoxyCodeLine{623     k0b\_Lu, k0a\_Lu, \&  \textcolor{comment}{! The original k-\/indices of the layers that participate}}
\DoxyCodeLine{624     k0b\_Ru, k0a\_Ru     \textcolor{comment}{! in each pair of mixing at u-\/faces.}}
\DoxyCodeLine{625   \textcolor{keywordtype}{type}(p2di), \textcolor{keywordtype}{dimension(SZJB\_(G))} :: \&}
\DoxyCodeLine{626     k0b\_Lv, k0a\_Lv, \&  \textcolor{comment}{! The original k-\/indices of the layers that participate}}
\DoxyCodeLine{627     k0b\_Rv, k0a\_Rv     \textcolor{comment}{! in each pair of mixing at v-\/faces.}}
\DoxyCodeLine{628 }
\DoxyCodeLine{629 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G), SZJ\_(G), SZK\_(G))} :: \&}
\DoxyCodeLine{630     tr\_flux\_conv  \textcolor{comment}{! The flux convergence of tracers [conc H L2 \string~> conc m3 or conc kg]}}
\DoxyCodeLine{631 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G), SZJ\_(G), SZK\_(G))} :: Tr\_flux\_3d, Tr\_adj\_vert\_L, Tr\_adj\_vert\_R}
\DoxyCodeLine{632 }
\DoxyCodeLine{633 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G), SZK\_(G), SZJ\_(G))} :: \&}
\DoxyCodeLine{634     rho\_srt, \& \textcolor{comment}{! The density of each layer of the sorted columns [R \string~> kg m-\/3].}}
\DoxyCodeLine{635     h\_srt      \textcolor{comment}{! The thickness of each layer of the sorted columns [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{636   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G), SZK\_(G), SZJ\_(G))} :: \&}
\DoxyCodeLine{637     k0\_srt     \textcolor{comment}{! The original k-\/index that each layer of the sorted column}}
\DoxyCodeLine{638                \textcolor{comment}{! corresponds to.}}
\DoxyCodeLine{639 }
\DoxyCodeLine{640 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(G))} :: \&}
\DoxyCodeLine{641     h\_demand\_L, \& \textcolor{comment}{! The thickness in the left (\_L) or right (\_R) column that}}
\DoxyCodeLine{642     h\_demand\_R, \& \textcolor{comment}{! is demanded to match the thickness in the counterpart [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{643     h\_used\_L, \&   \textcolor{comment}{! The summed thickness from the left or right columns that}}
\DoxyCodeLine{644     h\_used\_R, \&   \textcolor{comment}{! have actually been used [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{645     h\_supply\_frac\_L, \&  \textcolor{comment}{! The fraction of the demanded thickness that can}}
\DoxyCodeLine{646     h\_supply\_frac\_R     \textcolor{comment}{! actually be supplied from a layer.}}
\DoxyCodeLine{647   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G), SZJ\_(G))}  :: \&}
\DoxyCodeLine{648     num\_srt, \&   \textcolor{comment}{! The number of layers that are sorted in each column.}}
\DoxyCodeLine{649     k\_end\_srt, \& \textcolor{comment}{! The maximum index in each column that might need to be}}
\DoxyCodeLine{650                  \textcolor{comment}{! sorted, based on neighboring values of max\_kRho}}
\DoxyCodeLine{651     max\_krho     \textcolor{comment}{! The index of the layer whose target density is just denser}}
\DoxyCodeLine{652                  \textcolor{comment}{! than the densest part of the mixed layer.}}
\DoxyCodeLine{653   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZJ\_(G))}           :: \&}
\DoxyCodeLine{654     max\_srt      \textcolor{comment}{! The maximum value of num\_srt in a k-\/row.}}
\DoxyCodeLine{655   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZIB\_(G), SZJ\_(G))} :: \&}
\DoxyCodeLine{656     nPu          \textcolor{comment}{! The number of epipycnal pairings at each u-\/point.}}
\DoxyCodeLine{657   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZI\_(G), SZJB\_(G))} :: \&}
\DoxyCodeLine{658     nPv          \textcolor{comment}{! The number of epipycnal pairings at each v-\/point.}}
\DoxyCodeLine{659 \textcolor{keywordtype}{  real} :: h\_exclude    \textcolor{comment}{! A thickness that layers must attain to be considered}}
\DoxyCodeLine{660                        \textcolor{comment}{! for inclusion in mixing [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{661 \textcolor{keywordtype}{  real} :: Idt        \textcolor{comment}{! The inverse of the time step [T-\/1 \string~> s-\/1].}}
\DoxyCodeLine{662 \textcolor{keywordtype}{  real} :: I\_maxitt   \textcolor{comment}{! The inverse of the maximum number of iterations.}}
\DoxyCodeLine{663 \textcolor{keywordtype}{  real} :: rho\_pair, rho\_a, rho\_b  \textcolor{comment}{! Temporary densities [R \string~> kg m-\/3].}}
\DoxyCodeLine{664 \textcolor{keywordtype}{  real} :: Tr\_min\_face  \textcolor{comment}{! The minimum and maximum tracer concentrations}}
\DoxyCodeLine{665 \textcolor{keywordtype}{  real} :: Tr\_max\_face  \textcolor{comment}{! associated with a pairing [Conc]}}
\DoxyCodeLine{666 \textcolor{keywordtype}{  real} :: Tr\_La, Tr\_Lb \textcolor{comment}{! The 4 tracer concentrations that might be}}
\DoxyCodeLine{667 \textcolor{keywordtype}{  real} :: Tr\_Ra, Tr\_Rb \textcolor{comment}{! associated with a pairing [Conc]}}
\DoxyCodeLine{668 \textcolor{keywordtype}{  real} :: Tr\_av\_L    \textcolor{comment}{! The average tracer concentrations on the left and right}}
\DoxyCodeLine{669 \textcolor{keywordtype}{  real} :: Tr\_av\_R    \textcolor{comment}{! sides of a pairing [Conc].}}
\DoxyCodeLine{670 \textcolor{keywordtype}{  real} :: Tr\_flux    \textcolor{comment}{! The tracer flux from left to right in a pair [conc H L2 \string~> conc m3 or conc kg].}}
\DoxyCodeLine{671 \textcolor{keywordtype}{  real} :: Tr\_adj\_vert  \textcolor{comment}{! A downward vertical adjustment to Tr\_flux between the}}
\DoxyCodeLine{672                      \textcolor{comment}{! two cells that make up one side of the pairing [conc H L2 \string~> conc m3 or conc kg].}}
\DoxyCodeLine{673 \textcolor{keywordtype}{  real} :: h\_L, h\_R   \textcolor{comment}{! Thicknesses to the left and right [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{674 \textcolor{keywordtype}{  real} :: wt\_a, wt\_b \textcolor{comment}{! Fractional weights of layers above and below [nondim].}}
\DoxyCodeLine{675 \textcolor{keywordtype}{  real} :: vol        \textcolor{comment}{! A cell volume or mass [H L2 \string~> m3 or kg].}}
\DoxyCodeLine{676 }
\DoxyCodeLine{677   \textcolor{comment}{! The total number of pairings is usually much less than twice the number of layers, but}}
\DoxyCodeLine{678   \textcolor{comment}{! the memory in these 1-\/d columns of pairings can be allocated generously for safety.}}
\DoxyCodeLine{679   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZK\_(G)*2)} :: \&}
\DoxyCodeLine{680     kbs\_Lp, \&   \textcolor{comment}{! The sorted indices of the Left and Right columns for}}
\DoxyCodeLine{681     kbs\_Rp      \textcolor{comment}{! each pairing.}}
\DoxyCodeLine{682   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(SZK\_(G)*2)} :: \&}
\DoxyCodeLine{683     left\_set, \&  \textcolor{comment}{! If true, the left or right point determines the density of}}
\DoxyCodeLine{684     right\_set    \textcolor{comment}{! of the trio.  If densities are exactly equal, both are true.}}
\DoxyCodeLine{685 }
\DoxyCodeLine{686 \textcolor{keywordtype}{  real} :: tmp}
\DoxyCodeLine{687 \textcolor{keywordtype}{  real} :: p\_ref\_cv(SZI\_(G)) \textcolor{comment}{! The reference pressure for the coordinate density [R L2 T-\/2 \string~> Pa]}}
\DoxyCodeLine{688 }
\DoxyCodeLine{689   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: EOSdom \textcolor{comment}{! The i-\/computational domain for the equation of state}}
\DoxyCodeLine{690   \textcolor{keywordtype}{integer} :: k\_max, k\_min, k\_test, itmp}
\DoxyCodeLine{691   \textcolor{keywordtype}{integer} :: i, j, k, k2, m, is, ie, js, je, nz, nkmb}
\DoxyCodeLine{692   \textcolor{keywordtype}{integer} :: isd, ied, jsd, jed, IsdB, IedB, k\_size}
\DoxyCodeLine{693   \textcolor{keywordtype}{integer} :: kL, kR, kLa, kLb, kRa, kRb, nP, itt, ns, max\_itt}
\DoxyCodeLine{694   \textcolor{keywordtype}{integer} :: PEmax\_kRho}
\DoxyCodeLine{695   \textcolor{keywordtype}{integer} :: isv, iev, jsv, jev \textcolor{comment}{! The valid range of the indices.}}
\DoxyCodeLine{696 }
\DoxyCodeLine{697   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = gv\%ke}
\DoxyCodeLine{698   isd = g\%isd ; ied = g\%ied ; jsd = g\%jsd ; jed = g\%jed}
\DoxyCodeLine{699   isdb = g\%IsdB ; iedb = g\%IedB}
\DoxyCodeLine{700   idt = 1.0 / dt}
\DoxyCodeLine{701   nkmb = gv\%nk\_rho\_varies}
\DoxyCodeLine{702 }
\DoxyCodeLine{703   \textcolor{keywordflow}{if} (num\_itts <= 1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{704     max\_itt = 1 ; i\_maxitt = 1.0}
\DoxyCodeLine{705   \textcolor{keywordflow}{else}}
\DoxyCodeLine{706     max\_itt = num\_itts ; i\_maxitt = 1.0 / (real(max\_itt))}
\DoxyCodeLine{707 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{708 }
\DoxyCodeLine{709   \textcolor{keywordflow}{do} i=is-\/2,ie+2 ; p\_ref\_cv(i) = tv\%P\_Ref ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{710   eosdom(:) = eos\_domain(g\%HI,halo=2)}
\DoxyCodeLine{711 }
\DoxyCodeLine{712   \textcolor{keyword}{call }do\_group\_pass(cs\%pass\_t, g\%Domain, clock=id\_clock\_pass)}
\DoxyCodeLine{713   \textcolor{comment}{! Determine which layers the mixed-\/ and buffer-\/layers map into...}}
\DoxyCodeLine{714   \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{715   \textcolor{keywordflow}{do} k=1,nkmb ; \textcolor{keywordflow}{do} j=js-\/2,je+2}
\DoxyCodeLine{716     \textcolor{keyword}{call }calculate\_density(tv\%T(:,j,k),tv\%S(:,j,k), p\_ref\_cv, rho\_coord(:,j,k), \&}
\DoxyCodeLine{717                            tv\%eqn\_of\_state, eosdom)}
\DoxyCodeLine{718 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{719 }
\DoxyCodeLine{720   \textcolor{keywordflow}{do} j=js-\/2,je+2 ; \textcolor{keywordflow}{do} i=is-\/2,ie+2}
\DoxyCodeLine{721     rml\_max(i,j) = rho\_coord(i,j,1)}
\DoxyCodeLine{722     num\_srt(i,j) = 0 ; max\_krho(i,j) = 0}
\DoxyCodeLine{723 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{724   \textcolor{keywordflow}{do} k=2,nkmb ; \textcolor{keywordflow}{do} j=js-\/2,je+2 ; \textcolor{keywordflow}{do} i=is-\/2,ie+2}
\DoxyCodeLine{725     \textcolor{keywordflow}{if} (rml\_max(i,j) < rho\_coord(i,j,k)) rml\_max(i,j) = rho\_coord(i,j,k)}
\DoxyCodeLine{726 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{727   \textcolor{comment}{!   Use bracketing and bisection to find the k-\/level that the densest of the}}
\DoxyCodeLine{728   \textcolor{comment}{! mixed and buffer layer corresponds to, such that:}}
\DoxyCodeLine{729   \textcolor{comment}{!     GV\%Rlay(max\_kRho-\/1) < Rml\_max <= GV\%Rlay(max\_kRho)}}
\DoxyCodeLine{730   \textcolor{comment}{!\$OMP parallel do default(shared) private(k\_min,k\_max,k\_test)}}
\DoxyCodeLine{731   \textcolor{keywordflow}{do} j=js-\/2,je+2 ; \textcolor{keywordflow}{do} i=is-\/2,ie+2 ; \textcolor{keywordflow}{if} (g\%mask2dT(i,j) > 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{732     \textcolor{keywordflow}{if} ((rml\_max(i,j) > gv\%Rlay(nz)) .or. (nkmb+1 > nz)) \textcolor{keywordflow}{then} ; max\_krho(i,j) = nz+1}
\DoxyCodeLine{733     \textcolor{keywordflow}{elseif} ((rml\_max(i,j) <= gv\%Rlay(nkmb+1)) .or. (nkmb+2 > nz)) \textcolor{keywordflow}{then} ; max\_krho(i,j) = nkmb+1}
\DoxyCodeLine{734     \textcolor{keywordflow}{else}}
\DoxyCodeLine{735       k\_min = nkmb+2 ; k\_max = nz}
\DoxyCodeLine{736       \textcolor{keywordflow}{do}}
\DoxyCodeLine{737         k\_test = (k\_min + k\_max) / 2}
\DoxyCodeLine{738         \textcolor{keywordflow}{if} (rml\_max(i,j) <= gv\%Rlay(k\_test-\/1)) \textcolor{keywordflow}{then} ; k\_max = k\_test-\/1}
\DoxyCodeLine{739         \textcolor{keywordflow}{elseif} (gv\%Rlay(k\_test) < rml\_max(i,j)) \textcolor{keywordflow}{then} ; k\_min = k\_test+1}
\DoxyCodeLine{740         \textcolor{keywordflow}{else} ; max\_krho(i,j) = k\_test ; \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{741 }
\DoxyCodeLine{742         \textcolor{keywordflow}{if} (k\_min == k\_max) \textcolor{keywordflow}{then} ; max\_krho(i,j) = k\_max ; \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{743 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{744 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{745 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{746 }
\DoxyCodeLine{747   pemax\_krho = 0}
\DoxyCodeLine{748   \textcolor{keywordflow}{do} j=js-\/1,je+1 ; \textcolor{keywordflow}{do} i=is-\/1,ie+1}
\DoxyCodeLine{749     k\_end\_srt(i,j) = max(max\_krho(i,j), max\_krho(i-\/1,j), max\_krho(i+1,j), \&}
\DoxyCodeLine{750                          max\_krho(i,j-\/1), max\_krho(i,j+1))}
\DoxyCodeLine{751     \textcolor{keywordflow}{if} (pemax\_krho < k\_end\_srt(i,j)) pemax\_krho = k\_end\_srt(i,j)}
\DoxyCodeLine{752 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{753   \textcolor{keywordflow}{if} (pemax\_krho > nz) pemax\_krho = nz \textcolor{comment}{! PEmax\_kRho could have been nz+1.}}
\DoxyCodeLine{754 }
\DoxyCodeLine{755   h\_exclude = 10.0*(gv\%Angstrom\_H + gv\%H\_subroundoff)}
\DoxyCodeLine{756   \textcolor{comment}{!\$OMP parallel default(shared) private(ns,tmp,itmp)}}
\DoxyCodeLine{757   \textcolor{comment}{!\$OMP do}}
\DoxyCodeLine{758   \textcolor{keywordflow}{do} j=js-\/1,je+1}
\DoxyCodeLine{759     \textcolor{keywordflow}{do} k=1,nkmb ; \textcolor{keywordflow}{do} i=is-\/1,ie+1 ; \textcolor{keywordflow}{if} (g\%mask2dT(i,j) > 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{760       \textcolor{keywordflow}{if} (h(i,j,k) > h\_exclude) \textcolor{keywordflow}{then}}
\DoxyCodeLine{761         num\_srt(i,j) = num\_srt(i,j) + 1 ; ns = num\_srt(i,j)}
\DoxyCodeLine{762         k0\_srt(i,ns,j) = k}
\DoxyCodeLine{763         rho\_srt(i,ns,j) = rho\_coord(i,j,k)}
\DoxyCodeLine{764         h\_srt(i,ns,j) = h(i,j,k)}
\DoxyCodeLine{765 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{766 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{767     \textcolor{keywordflow}{do} k=nkmb+1,pemax\_krho ; \textcolor{keywordflow}{do} i=is-\/1,ie+1 ; \textcolor{keywordflow}{if} (g\%mask2dT(i,j) > 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{768       \textcolor{keywordflow}{if} ((k<=k\_end\_srt(i,j)) .and. (h(i,j,k) > h\_exclude)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{769         num\_srt(i,j) = num\_srt(i,j) + 1 ; ns = num\_srt(i,j)}
\DoxyCodeLine{770         k0\_srt(i,ns,j) = k}
\DoxyCodeLine{771         rho\_srt(i,ns,j) = gv\%Rlay(k)}
\DoxyCodeLine{772         h\_srt(i,ns,j) = h(i,j,k)}
\DoxyCodeLine{773 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{774 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{775 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{776   \textcolor{comment}{! Sort each column by increasing density.  This should already be close,}}
\DoxyCodeLine{777   \textcolor{comment}{! and the size of the arrays are small, so straight insertion is used.}}
\DoxyCodeLine{778   \textcolor{comment}{!\$OMP do}}
\DoxyCodeLine{779    \textcolor{keywordflow}{do} j=js-\/1,je+1; \textcolor{keywordflow}{do} i=is-\/1,ie+1}
\DoxyCodeLine{780     \textcolor{keywordflow}{do} k=2,num\_srt(i,j) ; \textcolor{keywordflow}{if} (rho\_srt(i,k,j) < rho\_srt(i,k-\/1,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{781       \textcolor{comment}{! The last segment needs to be shuffled earlier in the list.}}
\DoxyCodeLine{782       \textcolor{keywordflow}{do} k2 = k,2,-\/1 ; \textcolor{keywordflow}{if} (rho\_srt(i,k2,j) >= rho\_srt(i,k2-\/1,j)) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{783         itmp = k0\_srt(i,k2-\/1,j) ; k0\_srt(i,k2-\/1,j) = k0\_srt(i,k2,j) ; k0\_srt(i,k2,j) = itmp}
\DoxyCodeLine{784         tmp = rho\_srt(i,k2-\/1,j) ; rho\_srt(i,k2-\/1,j) = rho\_srt(i,k2,j) ; rho\_srt(i,k2,j) = tmp}
\DoxyCodeLine{785         tmp = h\_srt(i,k2-\/1,j) ; h\_srt(i,k2-\/1,j) = h\_srt(i,k2,j) ; h\_srt(i,k2,j) = tmp}
\DoxyCodeLine{786 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{787 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{788 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{789   \textcolor{comment}{!\$OMP do}}
\DoxyCodeLine{790   \textcolor{keywordflow}{do} j=js-\/1,je+1}
\DoxyCodeLine{791     max\_srt(j) = 0}
\DoxyCodeLine{792     \textcolor{keywordflow}{do} i=is-\/1,ie+1 ; max\_srt(j) = max(max\_srt(j), num\_srt(i,j)) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{793 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{794   \textcolor{comment}{!\$OMP end parallel}}
\DoxyCodeLine{795 }
\DoxyCodeLine{796   \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{797     k\_size = max(2*max\_srt(j),1)}
\DoxyCodeLine{798     \textcolor{keyword}{allocate}(deep\_wt\_lu(j)\%p(isdb:iedb,k\_size))}
\DoxyCodeLine{799     \textcolor{keyword}{allocate}(deep\_wt\_ru(j)\%p(isdb:iedb,k\_size))}
\DoxyCodeLine{800     \textcolor{keyword}{allocate}(hp\_lu(j)\%p(isdb:iedb,k\_size))}
\DoxyCodeLine{801     \textcolor{keyword}{allocate}(hp\_ru(j)\%p(isdb:iedb,k\_size))}
\DoxyCodeLine{802     \textcolor{keyword}{allocate}(k0a\_lu(j)\%p(isdb:iedb,k\_size))}
\DoxyCodeLine{803     \textcolor{keyword}{allocate}(k0a\_ru(j)\%p(isdb:iedb,k\_size))}
\DoxyCodeLine{804     \textcolor{keyword}{allocate}(k0b\_lu(j)\%p(isdb:iedb,k\_size))}
\DoxyCodeLine{805     \textcolor{keyword}{allocate}(k0b\_ru(j)\%p(isdb:iedb,k\_size))}
\DoxyCodeLine{806 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{807 }
\DoxyCodeLine{808 \textcolor{comment}{!\$OMP parallel do default(none) shared(is,ie,js,je,G,num\_srt,rho\_srt,k0b\_Lu,k0\_srt, \&}}
\DoxyCodeLine{809 \textcolor{comment}{!\$OMP                                  k0b\_Ru,k0a\_Lu,k0a\_Ru,deep\_wt\_Lu,deep\_wt\_Ru,  \&}}
\DoxyCodeLine{810 \textcolor{comment}{!\$OMP                                  h\_srt,nkmb,nPu,hP\_Lu,hP\_Ru)                  \&}}
\DoxyCodeLine{811 \textcolor{comment}{!\$OMP                          private(h\_demand\_L,h\_used\_L,h\_demand\_R,h\_used\_R,     \&}}
\DoxyCodeLine{812 \textcolor{comment}{!\$OMP                                  kR,kL,nP,rho\_pair,kbs\_Lp,kbs\_Rp,rho\_a,rho\_b, \&}}
\DoxyCodeLine{813 \textcolor{comment}{!\$OMP                                  wt\_b,left\_set,right\_set,h\_supply\_frac\_R,     \&}}
\DoxyCodeLine{814 \textcolor{comment}{!\$OMP                                  h\_supply\_frac\_L)}}
\DoxyCodeLine{815   \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie ; \textcolor{keywordflow}{if} (g\%mask2dCu(i,j) > 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{816     \textcolor{comment}{! Set up the pairings for fluxes through the zonal faces.}}
\DoxyCodeLine{817 }
\DoxyCodeLine{818     \textcolor{keywordflow}{do} k=1,num\_srt(i,j)   ; h\_demand\_l(k) = 0.0 ; h\_used\_l(k) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{819     \textcolor{keywordflow}{do} k=1,num\_srt(i+1,j) ; h\_demand\_r(k) = 0.0 ; h\_used\_r(k) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{820 }
\DoxyCodeLine{821     \textcolor{comment}{! First merge the left and right lists into a single, sorted list.}}
\DoxyCodeLine{822 }
\DoxyCodeLine{823     \textcolor{comment}{!   Discard any layers that are lighter than the lightest in the other}}
\DoxyCodeLine{824     \textcolor{comment}{! column.  They can only participate in mixing as the lighter part of a}}
\DoxyCodeLine{825     \textcolor{comment}{! pair of points.}}
\DoxyCodeLine{826     \textcolor{keywordflow}{if} (rho\_srt(i,1,j) < rho\_srt(i+1,1,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{827       kr = 1}
\DoxyCodeLine{828       \textcolor{keywordflow}{do} kl=2,num\_srt(i,j) ; \textcolor{keywordflow}{if} (rho\_srt(i,kl,j) >= rho\_srt(i+1,1,j)) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{829     \textcolor{keywordflow}{elseif} (rho\_srt(i+1,1,j) < rho\_srt(i,1,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{830       kl = 1}
\DoxyCodeLine{831       \textcolor{keywordflow}{do} kr=2,num\_srt(i+1,j) ; \textcolor{keywordflow}{if} (rho\_srt(i+1,kr,j) >= rho\_srt(i,1,j)) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{832     \textcolor{keywordflow}{else}}
\DoxyCodeLine{833       kl = 1 ; kr = 1}
\DoxyCodeLine{834 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{835     np = 0}
\DoxyCodeLine{836     \textcolor{keywordflow}{do} \textcolor{comment}{! Loop to accumulate pairs of columns.}}
\DoxyCodeLine{837       \textcolor{keywordflow}{if} ((kl > num\_srt(i,j)) .or. (kr > num\_srt(i+1,j))) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{838 }
\DoxyCodeLine{839       \textcolor{keywordflow}{if} (rho\_srt(i,kl,j) > rho\_srt(i+1,kr,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{840       \textcolor{comment}{! The right point is lighter and defines the density for this trio.}}
\DoxyCodeLine{841         np = np+1 ; k = np}
\DoxyCodeLine{842         rho\_pair = rho\_srt(i+1,kr,j)}
\DoxyCodeLine{843 }
\DoxyCodeLine{844         k0b\_lu(j)\%p(i,k) = k0\_srt(i,kl,j) ; k0b\_ru(j)\%p(i,k) = k0\_srt(i+1,kr,j)}
\DoxyCodeLine{845         k0a\_lu(j)\%p(i,k) = k0\_srt(i,kl-\/1,j) ; k0a\_ru(j)\%p(i,k) = k0b\_ru(j)\%p(i,k)}
\DoxyCodeLine{846         kbs\_lp(k) = kl ; kbs\_rp(k) = kr}
\DoxyCodeLine{847 }
\DoxyCodeLine{848         rho\_a = rho\_srt(i,kl-\/1,j) ; rho\_b = rho\_srt(i,kl,j)}
\DoxyCodeLine{849         wt\_b = 1.0 ; \textcolor{keywordflow}{if} (abs(rho\_a -\/ rho\_b) > abs(rho\_pair -\/ rho\_a)) \&}
\DoxyCodeLine{850           wt\_b = (rho\_pair -\/ rho\_a) / (rho\_b -\/ rho\_a)}
\DoxyCodeLine{851         deep\_wt\_lu(j)\%p(i,k) = wt\_b ; deep\_wt\_ru(j)\%p(i,k) = 1.0}
\DoxyCodeLine{852 }
\DoxyCodeLine{853         h\_demand\_l(kl) = h\_demand\_l(kl) + 0.5*h\_srt(i+1,kr,j) * wt\_b}
\DoxyCodeLine{854         h\_demand\_l(kl-\/1) = h\_demand\_l(kl-\/1) + 0.5*h\_srt(i+1,kr,j) * (1.0-\/wt\_b)}
\DoxyCodeLine{855 }
\DoxyCodeLine{856         kr = kr+1 ; left\_set(k) = .false. ; right\_set(k) = .true.}
\DoxyCodeLine{857       \textcolor{keywordflow}{elseif} (rho\_srt(i,kl,j) < rho\_srt(i+1,kr,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{858       \textcolor{comment}{! The left point is lighter and defines the density for this trio.}}
\DoxyCodeLine{859         np = np+1 ; k = np}
\DoxyCodeLine{860         rho\_pair = rho\_srt(i,kl,j)}
\DoxyCodeLine{861         k0b\_lu(j)\%p(i,k) = k0\_srt(i,kl,j) ; k0b\_ru(j)\%p(i,k) = k0\_srt(i+1,kr,j)}
\DoxyCodeLine{862         k0a\_lu(j)\%p(i,k) = k0b\_lu(j)\%p(i,k) ; k0a\_ru(j)\%p(i,k) = k0\_srt(i+1,kr-\/1,j)}
\DoxyCodeLine{863 }
\DoxyCodeLine{864         kbs\_lp(k) = kl ; kbs\_rp(k) = kr}
\DoxyCodeLine{865 }
\DoxyCodeLine{866         rho\_a = rho\_srt(i+1,kr-\/1,j) ; rho\_b = rho\_srt(i+1,kr,j)}
\DoxyCodeLine{867         wt\_b = 1.0 ; \textcolor{keywordflow}{if} (abs(rho\_a -\/ rho\_b) > abs(rho\_pair -\/ rho\_a)) \&}
\DoxyCodeLine{868           wt\_b = (rho\_pair -\/ rho\_a) / (rho\_b -\/ rho\_a)}
\DoxyCodeLine{869         deep\_wt\_lu(j)\%p(i,k) = 1.0 ; deep\_wt\_ru(j)\%p(i,k) = wt\_b}
\DoxyCodeLine{870 }
\DoxyCodeLine{871         h\_demand\_r(kr) = h\_demand\_r(kr) + 0.5*h\_srt(i,kl,j) * wt\_b}
\DoxyCodeLine{872         h\_demand\_r(kr-\/1) = h\_demand\_r(kr-\/1) + 0.5*h\_srt(i,kl,j) * (1.0-\/wt\_b)}
\DoxyCodeLine{873 }
\DoxyCodeLine{874         kl = kl+1 ; left\_set(k) = .true. ; right\_set(k) = .false.}
\DoxyCodeLine{875       \textcolor{keywordflow}{elseif} ((k0\_srt(i,kl,j) <= nkmb) .or. (k0\_srt(i+1,kr,j) <= nkmb)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{876         \textcolor{comment}{! The densities are exactly equal and one layer is above the interior.}}
\DoxyCodeLine{877         np = np+1 ; k = np}
\DoxyCodeLine{878         k0b\_lu(j)\%p(i,k) = k0\_srt(i,kl,j) ; k0b\_ru(j)\%p(i,k) = k0\_srt(i+1,kr,j)}
\DoxyCodeLine{879         k0a\_lu(j)\%p(i,k) = k0b\_lu(j)\%p(i,k) ; k0a\_ru(j)\%p(i,k) = k0b\_ru(j)\%p(i,k)}
\DoxyCodeLine{880         kbs\_lp(k) = kl ; kbs\_rp(k) = kr}
\DoxyCodeLine{881         deep\_wt\_lu(j)\%p(i,k) = 1.0 ; deep\_wt\_ru(j)\%p(i,k) = 1.0}
\DoxyCodeLine{882 }
\DoxyCodeLine{883         h\_demand\_l(kl) = h\_demand\_l(kl) + 0.5*h\_srt(i+1,kr,j)}
\DoxyCodeLine{884         h\_demand\_r(kr) = h\_demand\_r(kr) + 0.5*h\_srt(i,kl,j)}
\DoxyCodeLine{885 }
\DoxyCodeLine{886         kl = kl+1 ; kr = kr+1 ; left\_set(k) = .true. ; right\_set(k) = .true.}
\DoxyCodeLine{887       \textcolor{keywordflow}{else} \textcolor{comment}{! The densities are exactly equal and in the interior.}}
\DoxyCodeLine{888         \textcolor{comment}{! Mixing in this case has already occurred, so accumulate the thickness}}
\DoxyCodeLine{889         \textcolor{comment}{! demanded for that mixing and skip onward.}}
\DoxyCodeLine{890         h\_demand\_l(kl) = h\_demand\_l(kl) + 0.5*h\_srt(i+1,kr,j)}
\DoxyCodeLine{891         h\_demand\_r(kr) = h\_demand\_r(kr) + 0.5*h\_srt(i,kl,j)}
\DoxyCodeLine{892 }
\DoxyCodeLine{893         kl = kl+1 ; kr = kr+1}
\DoxyCodeLine{894 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{895 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! Loop to accumulate pairs of columns.}}
\DoxyCodeLine{896     npu(i,j) = np \textcolor{comment}{! This is the number of active pairings.}}
\DoxyCodeLine{897 }
\DoxyCodeLine{898     \textcolor{comment}{! Determine what fraction of the thickness "demand" can be supplied.}}
\DoxyCodeLine{899     \textcolor{keywordflow}{do} k=1,num\_srt(i+1,j)}
\DoxyCodeLine{900       h\_supply\_frac\_r(k) = 1.0}
\DoxyCodeLine{901       \textcolor{keywordflow}{if} (h\_demand\_r(k) > 0.5*h\_srt(i+1,k,j)) \&}
\DoxyCodeLine{902         h\_supply\_frac\_r(k) = 0.5*h\_srt(i+1,k,j) / h\_demand\_r(k)}
\DoxyCodeLine{903 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{904     \textcolor{keywordflow}{do} k=1,num\_srt(i,j)}
\DoxyCodeLine{905       h\_supply\_frac\_l(k) = 1.0}
\DoxyCodeLine{906       \textcolor{keywordflow}{if} (h\_demand\_l(k) > 0.5*h\_srt(i,k,j)) \&}
\DoxyCodeLine{907         h\_supply\_frac\_l(k) = 0.5*h\_srt(i,k,j) / h\_demand\_l(k)}
\DoxyCodeLine{908 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{909 }
\DoxyCodeLine{910     \textcolor{comment}{!  Distribute the "exported" thicknesses proportionately.}}
\DoxyCodeLine{911     \textcolor{keywordflow}{do} k=1,npu(i,j)}
\DoxyCodeLine{912       kl = kbs\_lp(k) ; kr = kbs\_rp(k)}
\DoxyCodeLine{913       hp\_lu(j)\%p(i,k) = 0.0 ; hp\_ru(j)\%p(i,k) = 0.0}
\DoxyCodeLine{914       \textcolor{keywordflow}{if} (left\_set(k)) \textcolor{keywordflow}{then} \textcolor{comment}{! Add the contributing thicknesses on the right.}}
\DoxyCodeLine{915         \textcolor{keywordflow}{if} (deep\_wt\_ru(j)\%p(i,k) < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{916           hp\_ru(j)\%p(i,k) = 0.5*h\_srt(i,kl,j) * min(h\_supply\_frac\_r(kr), h\_supply\_frac\_r(kr-\/1))}
\DoxyCodeLine{917           wt\_b = deep\_wt\_ru(j)\%p(i,k)}
\DoxyCodeLine{918           h\_used\_r(kr-\/1) = h\_used\_r(kr-\/1) + (1.0 -\/ wt\_b)*hp\_ru(j)\%p(i,k)}
\DoxyCodeLine{919           h\_used\_r(kr) = h\_used\_r(kr) + wt\_b*hp\_ru(j)\%p(i,k)}
\DoxyCodeLine{920         \textcolor{keywordflow}{else}}
\DoxyCodeLine{921           hp\_ru(j)\%p(i,k) = 0.5*h\_srt(i,kl,j) * h\_supply\_frac\_r(kr)}
\DoxyCodeLine{922           h\_used\_r(kr) = h\_used\_r(kr) + hp\_ru(j)\%p(i,k)}
\DoxyCodeLine{923 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{924 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{925       \textcolor{keywordflow}{if} (right\_set(k)) \textcolor{keywordflow}{then} \textcolor{comment}{! Add the contributing thicknesses on the left.}}
\DoxyCodeLine{926         \textcolor{keywordflow}{if} (deep\_wt\_lu(j)\%p(i,k) < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{927           hp\_lu(j)\%p(i,k) = 0.5*h\_srt(i+1,kr,j) * min(h\_supply\_frac\_l(kl), h\_supply\_frac\_l(kl-\/1))}
\DoxyCodeLine{928           wt\_b = deep\_wt\_lu(j)\%p(i,k)}
\DoxyCodeLine{929           h\_used\_l(kl-\/1) = h\_used\_l(kl-\/1) + (1.0 -\/ wt\_b)*hp\_lu(j)\%p(i,k)}
\DoxyCodeLine{930           h\_used\_l(kl) = h\_used\_l(kl) + wt\_b*hp\_lu(j)\%p(i,k)}
\DoxyCodeLine{931         \textcolor{keywordflow}{else}}
\DoxyCodeLine{932           hp\_lu(j)\%p(i,k) = 0.5*h\_srt(i+1,kr,j) * h\_supply\_frac\_l(kl)}
\DoxyCodeLine{933           h\_used\_l(kl) = h\_used\_l(kl) + hp\_lu(j)\%p(i,k)}
\DoxyCodeLine{934 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{935 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{936 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{937 }
\DoxyCodeLine{938     \textcolor{comment}{!   The left-\/over thickness (at least half the layer thickness) is now}}
\DoxyCodeLine{939     \textcolor{comment}{! added to the thicknesses of the importing columns.}}
\DoxyCodeLine{940     \textcolor{keywordflow}{do} k=1,npu(i,j)}
\DoxyCodeLine{941       \textcolor{keywordflow}{if} (left\_set(k)) hp\_lu(j)\%p(i,k) = hp\_lu(j)\%p(i,k) + \&}
\DoxyCodeLine{942                            (h\_srt(i,kbs\_lp(k),j) -\/ h\_used\_l(kbs\_lp(k)))}
\DoxyCodeLine{943       \textcolor{keywordflow}{if} (right\_set(k)) hp\_ru(j)\%p(i,k) = hp\_ru(j)\%p(i,k) + \&}
\DoxyCodeLine{944                             (h\_srt(i+1,kbs\_rp(k),j) -\/ h\_used\_r(kbs\_rp(k)))}
\DoxyCodeLine{945 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{946 }
\DoxyCodeLine{947 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i-\/ \& j-\/ loops over zonal faces.}}
\DoxyCodeLine{948 }
\DoxyCodeLine{949   \textcolor{keywordflow}{do} j=js-\/1,je}
\DoxyCodeLine{950     k\_size = max(max\_srt(j)+max\_srt(j+1),1)}
\DoxyCodeLine{951     \textcolor{keyword}{allocate}(deep\_wt\_lv(j)\%p(isd:ied,k\_size))}
\DoxyCodeLine{952     \textcolor{keyword}{allocate}(deep\_wt\_rv(j)\%p(isd:ied,k\_size))}
\DoxyCodeLine{953     \textcolor{keyword}{allocate}(hp\_lv(j)\%p(isd:ied,k\_size))}
\DoxyCodeLine{954     \textcolor{keyword}{allocate}(hp\_rv(j)\%p(isd:ied,k\_size))}
\DoxyCodeLine{955     \textcolor{keyword}{allocate}(k0a\_lv(j)\%p(isd:ied,k\_size))}
\DoxyCodeLine{956     \textcolor{keyword}{allocate}(k0a\_rv(j)\%p(isd:ied,k\_size))}
\DoxyCodeLine{957     \textcolor{keyword}{allocate}(k0b\_lv(j)\%p(isd:ied,k\_size))}
\DoxyCodeLine{958     \textcolor{keyword}{allocate}(k0b\_rv(j)\%p(isd:ied,k\_size))}
\DoxyCodeLine{959 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{960 }
\DoxyCodeLine{961 \textcolor{comment}{!\$OMP parallel do default(none) shared(is,ie,js,je,G,num\_srt,rho\_srt,k0b\_Lv,k0b\_Rv, \&}}
\DoxyCodeLine{962 \textcolor{comment}{!\$OMP                                  k0\_srt,k0a\_Lv,k0a\_Rv,deep\_wt\_Lv,deep\_wt\_Rv,  \&}}
\DoxyCodeLine{963 \textcolor{comment}{!\$OMP                                  h\_srt,nkmb,nPv,hP\_Lv,hP\_Rv)                  \&}}
\DoxyCodeLine{964 \textcolor{comment}{!\$OMP                          private(h\_demand\_L,h\_used\_L,h\_demand\_R,h\_used\_R,     \&}}
\DoxyCodeLine{965 \textcolor{comment}{!\$OMP                                  kR,kL,nP,rho\_pair,kbs\_Lp,kbs\_Rp,rho\_a,rho\_b, \&}}
\DoxyCodeLine{966 \textcolor{comment}{!\$OMP                                  wt\_b,left\_set,right\_set,h\_supply\_frac\_R,     \&}}
\DoxyCodeLine{967 \textcolor{comment}{!\$OMP                                  h\_supply\_frac\_L)}}
\DoxyCodeLine{968   \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (g\%mask2dCv(i,j) > 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{969     \textcolor{comment}{! Set up the pairings for fluxes through the meridional faces.}}
\DoxyCodeLine{970 }
\DoxyCodeLine{971     \textcolor{keywordflow}{do} k=1,num\_srt(i,j)   ; h\_demand\_l(k) = 0.0 ; h\_used\_l(k) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{972     \textcolor{keywordflow}{do} k=1,num\_srt(i,j+1) ; h\_demand\_r(k) = 0.0 ; h\_used\_r(k) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{973 }
\DoxyCodeLine{974     \textcolor{comment}{! First merge the left and right lists into a single, sorted list.}}
\DoxyCodeLine{975 }
\DoxyCodeLine{976     \textcolor{comment}{!   Discard any layers that are lighter than the lightest in the other}}
\DoxyCodeLine{977     \textcolor{comment}{! column.  They can only participate in mixing as the lighter part of a}}
\DoxyCodeLine{978     \textcolor{comment}{! pair of points.}}
\DoxyCodeLine{979     \textcolor{keywordflow}{if} (rho\_srt(i,1,j) < rho\_srt(i,1,j+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{980       kr = 1}
\DoxyCodeLine{981       \textcolor{keywordflow}{do} kl=2,num\_srt(i,j) ; \textcolor{keywordflow}{if} (rho\_srt(i,kl,j) >= rho\_srt(i,1,j+1)) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{982     \textcolor{keywordflow}{elseif} (rho\_srt(i,1,j+1) < rho\_srt(i,1,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{983       kl = 1}
\DoxyCodeLine{984       \textcolor{keywordflow}{do} kr=2,num\_srt(i,j+1) ; \textcolor{keywordflow}{if} (rho\_srt(i,kr,j+1) >= rho\_srt(i,1,j)) \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{985     \textcolor{keywordflow}{else}}
\DoxyCodeLine{986       kl = 1 ; kr = 1}
\DoxyCodeLine{987 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{988     np = 0}
\DoxyCodeLine{989     \textcolor{keywordflow}{do} \textcolor{comment}{! Loop to accumulate pairs of columns.}}
\DoxyCodeLine{990       \textcolor{keywordflow}{if} ((kl > num\_srt(i,j)) .or. (kr > num\_srt(i,j+1))) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{991 }
\DoxyCodeLine{992       \textcolor{keywordflow}{if} (rho\_srt(i,kl,j) > rho\_srt(i,kr,j+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{993       \textcolor{comment}{! The right point is lighter and defines the density for this trio.}}
\DoxyCodeLine{994         np = np+1 ; k = np}
\DoxyCodeLine{995         rho\_pair = rho\_srt(i,kr,j+1)}
\DoxyCodeLine{996 }
\DoxyCodeLine{997         k0b\_lv(j)\%p(i,k) = k0\_srt(i,kl,j)   ; k0b\_rv(j)\%p(i,k) = k0\_srt(i,kr,j+1)}
\DoxyCodeLine{998         k0a\_lv(j)\%p(i,k) = k0\_srt(i,kl-\/1,j) ; k0a\_rv(j)\%p(i,k) = k0b\_rv(j)\%p(i,k)}
\DoxyCodeLine{999         kbs\_lp(k) = kl ; kbs\_rp(k) = kr}
\DoxyCodeLine{1000 }
\DoxyCodeLine{1001         rho\_a = rho\_srt(i,kl-\/1,j) ; rho\_b = rho\_srt(i,kl,j)}
\DoxyCodeLine{1002         wt\_b = 1.0 ; \textcolor{keywordflow}{if} (abs(rho\_a -\/ rho\_b) > abs(rho\_pair -\/ rho\_a)) \&}
\DoxyCodeLine{1003           wt\_b = (rho\_pair -\/ rho\_a) / (rho\_b -\/ rho\_a)}
\DoxyCodeLine{1004         deep\_wt\_lv(j)\%p(i,k) = wt\_b ; deep\_wt\_rv(j)\%p(i,k) = 1.0}
\DoxyCodeLine{1005 }
\DoxyCodeLine{1006         h\_demand\_l(kl) = h\_demand\_l(kl) + 0.5*h\_srt(i,kr,j+1) * wt\_b}
\DoxyCodeLine{1007         h\_demand\_l(kl-\/1) = h\_demand\_l(kl-\/1) + 0.5*h\_srt(i,kr,j+1) * (1.0-\/wt\_b)}
\DoxyCodeLine{1008 }
\DoxyCodeLine{1009         kr = kr+1 ; left\_set(k) = .false. ; right\_set(k) = .true.}
\DoxyCodeLine{1010       \textcolor{keywordflow}{elseif} (rho\_srt(i,kl,j) < rho\_srt(i,kr,j+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1011       \textcolor{comment}{! The left point is lighter and defines the density for this trio.}}
\DoxyCodeLine{1012         np = np+1 ; k = np}
\DoxyCodeLine{1013         rho\_pair = rho\_srt(i,kl,j)}
\DoxyCodeLine{1014         k0b\_lv(j)\%p(i,k) = k0\_srt(i,kl,j) ; k0b\_rv(j)\%p(i,k) = k0\_srt(i,kr,j+1)}
\DoxyCodeLine{1015         k0a\_lv(j)\%p(i,k) = k0b\_lv(j)\%p(i,k) ; k0a\_rv(j)\%p(i,k) = k0\_srt(i,kr-\/1,j+1)}
\DoxyCodeLine{1016 }
\DoxyCodeLine{1017         kbs\_lp(k) = kl ; kbs\_rp(k) = kr}
\DoxyCodeLine{1018 }
\DoxyCodeLine{1019         rho\_a = rho\_srt(i,kr-\/1,j+1) ; rho\_b = rho\_srt(i,kr,j+1)}
\DoxyCodeLine{1020         wt\_b = 1.0 ; \textcolor{keywordflow}{if} (abs(rho\_a -\/ rho\_b) > abs(rho\_pair -\/ rho\_a)) \&}
\DoxyCodeLine{1021           wt\_b = (rho\_pair -\/ rho\_a) / (rho\_b -\/ rho\_a)}
\DoxyCodeLine{1022         deep\_wt\_lv(j)\%p(i,k) = 1.0 ; deep\_wt\_rv(j)\%p(i,k) = wt\_b}
\DoxyCodeLine{1023 }
\DoxyCodeLine{1024         h\_demand\_r(kr) = h\_demand\_r(kr) + 0.5*h\_srt(i,kl,j) * wt\_b}
\DoxyCodeLine{1025         h\_demand\_r(kr-\/1) = h\_demand\_r(kr-\/1) + 0.5*h\_srt(i,kl,j) * (1.0-\/wt\_b)}
\DoxyCodeLine{1026 }
\DoxyCodeLine{1027         kl = kl+1 ; left\_set(k) = .true. ; right\_set(k) = .false.}
\DoxyCodeLine{1028       \textcolor{keywordflow}{elseif} ((k0\_srt(i,kl,j) <= nkmb) .or. (k0\_srt(i,kr,j+1) <= nkmb)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1029         \textcolor{comment}{! The densities are exactly equal and one layer is above the interior.}}
\DoxyCodeLine{1030         np = np+1 ; k = np}
\DoxyCodeLine{1031         k0b\_lv(j)\%p(i,k) = k0\_srt(i,kl,j) ; k0b\_rv(j)\%p(i,k) = k0\_srt(i,kr,j+1)}
\DoxyCodeLine{1032         k0a\_lv(j)\%p(i,k) = k0b\_lv(j)\%p(i,k)  ; k0a\_rv(j)\%p(i,k) = k0b\_rv(j)\%p(i,k)}
\DoxyCodeLine{1033         kbs\_lp(k) = kl ; kbs\_rp(k) = kr}
\DoxyCodeLine{1034         deep\_wt\_lv(j)\%p(i,k) = 1.0 ; deep\_wt\_rv(j)\%p(i,k) = 1.0}
\DoxyCodeLine{1035 }
\DoxyCodeLine{1036         h\_demand\_l(kl) = h\_demand\_l(kl) + 0.5*h\_srt(i,kr,j+1)}
\DoxyCodeLine{1037         h\_demand\_r(kr) = h\_demand\_r(kr) + 0.5*h\_srt(i,kl,j)}
\DoxyCodeLine{1038 }
\DoxyCodeLine{1039         kl = kl+1 ; kr = kr+1 ; left\_set(k) = .true. ; right\_set(k) = .true.}
\DoxyCodeLine{1040       \textcolor{keywordflow}{else} \textcolor{comment}{! The densities are exactly equal and in the interior.}}
\DoxyCodeLine{1041         \textcolor{comment}{! Mixing in this case has already occurred, so accumulate the thickness}}
\DoxyCodeLine{1042         \textcolor{comment}{! demanded for that mixing and skip onward.}}
\DoxyCodeLine{1043         h\_demand\_l(kl) = h\_demand\_l(kl) + 0.5*h\_srt(i,kr,j+1)}
\DoxyCodeLine{1044         h\_demand\_r(kr) = h\_demand\_r(kr) + 0.5*h\_srt(i,kl,j)}
\DoxyCodeLine{1045 }
\DoxyCodeLine{1046         kl = kl+1 ; kr = kr+1}
\DoxyCodeLine{1047 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1048 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! Loop to accumulate pairs of columns.}}
\DoxyCodeLine{1049     npv(i,j) = np \textcolor{comment}{! This is the number of active pairings.}}
\DoxyCodeLine{1050 }
\DoxyCodeLine{1051     \textcolor{comment}{! Determine what fraction of the thickness "demand" can be supplied.}}
\DoxyCodeLine{1052     \textcolor{keywordflow}{do} k=1,num\_srt(i,j+1)}
\DoxyCodeLine{1053       h\_supply\_frac\_r(k) = 1.0}
\DoxyCodeLine{1054       \textcolor{keywordflow}{if} (h\_demand\_r(k) > 0.5*h\_srt(i,k,j+1)) \&}
\DoxyCodeLine{1055         h\_supply\_frac\_r(k) = 0.5*h\_srt(i,k,j+1) / h\_demand\_r(k)}
\DoxyCodeLine{1056 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1057     \textcolor{keywordflow}{do} k=1,num\_srt(i,j)}
\DoxyCodeLine{1058       h\_supply\_frac\_l(k) = 1.0}
\DoxyCodeLine{1059       \textcolor{keywordflow}{if} (h\_demand\_l(k) > 0.5*h\_srt(i,k,j)) \&}
\DoxyCodeLine{1060         h\_supply\_frac\_l(k) = 0.5*h\_srt(i,k,j) / h\_demand\_l(k)}
\DoxyCodeLine{1061 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1062 }
\DoxyCodeLine{1063     \textcolor{comment}{!  Distribute the "exported" thicknesses proportionately.}}
\DoxyCodeLine{1064     \textcolor{keywordflow}{do} k=1,npv(i,j)}
\DoxyCodeLine{1065       kl = kbs\_lp(k) ; kr = kbs\_rp(k)}
\DoxyCodeLine{1066       hp\_lv(j)\%p(i,k) = 0.0 ; hp\_rv(j)\%p(i,k) = 0.0}
\DoxyCodeLine{1067       \textcolor{keywordflow}{if} (left\_set(k)) \textcolor{keywordflow}{then} \textcolor{comment}{! Add the contributing thicknesses on the right.}}
\DoxyCodeLine{1068         \textcolor{keywordflow}{if} (deep\_wt\_rv(j)\%p(i,k) < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1069           hp\_rv(j)\%p(i,k) = 0.5*h\_srt(i,kl,j) * min(h\_supply\_frac\_r(kr), h\_supply\_frac\_r(kr-\/1))}
\DoxyCodeLine{1070           wt\_b = deep\_wt\_rv(j)\%p(i,k)}
\DoxyCodeLine{1071           h\_used\_r(kr-\/1) = h\_used\_r(kr-\/1) + (1.0 -\/ wt\_b) * hp\_rv(j)\%p(i,k)}
\DoxyCodeLine{1072           h\_used\_r(kr) = h\_used\_r(kr) + wt\_b * hp\_rv(j)\%p(i,k)}
\DoxyCodeLine{1073         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1074           hp\_rv(j)\%p(i,k) = 0.5*h\_srt(i,kl,j) * h\_supply\_frac\_r(kr)}
\DoxyCodeLine{1075           h\_used\_r(kr) = h\_used\_r(kr) + hp\_rv(j)\%p(i,k)}
\DoxyCodeLine{1076 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1077 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1078       \textcolor{keywordflow}{if} (right\_set(k)) \textcolor{keywordflow}{then} \textcolor{comment}{! Add the contributing thicknesses on the left.}}
\DoxyCodeLine{1079         \textcolor{keywordflow}{if} (deep\_wt\_lv(j)\%p(i,k) < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1080           hp\_lv(j)\%p(i,k) = 0.5*h\_srt(i,kr,j+1) * min(h\_supply\_frac\_l(kl), h\_supply\_frac\_l(kl-\/1))}
\DoxyCodeLine{1081           wt\_b = deep\_wt\_lv(j)\%p(i,k)}
\DoxyCodeLine{1082           h\_used\_l(kl-\/1) = h\_used\_l(kl-\/1) + (1.0 -\/ wt\_b) * hp\_lv(j)\%p(i,k)}
\DoxyCodeLine{1083           h\_used\_l(kl) = h\_used\_l(kl) + wt\_b * hp\_lv(j)\%p(i,k)}
\DoxyCodeLine{1084         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1085           hp\_lv(j)\%p(i,k) = 0.5*h\_srt(i,kr,j+1) * h\_supply\_frac\_l(kl)}
\DoxyCodeLine{1086           h\_used\_l(kl) = h\_used\_l(kl) + hp\_lv(j)\%p(i,k)}
\DoxyCodeLine{1087 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1088 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1089 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1090 }
\DoxyCodeLine{1091     \textcolor{comment}{!   The left-\/over thickness (at least half the layer thickness) is now}}
\DoxyCodeLine{1092     \textcolor{comment}{! added to the thicknesses of the importing columns.}}
\DoxyCodeLine{1093     \textcolor{keywordflow}{do} k=1,npv(i,j)}
\DoxyCodeLine{1094       \textcolor{keywordflow}{if} (left\_set(k)) hp\_lv(j)\%p(i,k) = hp\_lv(j)\%p(i,k) + \&}
\DoxyCodeLine{1095                             (h\_srt(i,kbs\_lp(k),j) -\/ h\_used\_l(kbs\_lp(k)))}
\DoxyCodeLine{1096       \textcolor{keywordflow}{if} (right\_set(k)) hp\_rv(j)\%p(i,k) = hp\_rv(j)\%p(i,k) + \&}
\DoxyCodeLine{1097                              (h\_srt(i,kbs\_rp(k),j+1) -\/ h\_used\_r(kbs\_rp(k)))}
\DoxyCodeLine{1098 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1099 }
\DoxyCodeLine{1100 }
\DoxyCodeLine{1101 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i-\/ \& j-\/ loops over meridional faces.}}
\DoxyCodeLine{1102 }
\DoxyCodeLine{1103 \textcolor{comment}{! The tracer-\/specific calculations start here.}}
\DoxyCodeLine{1104 }
\DoxyCodeLine{1105   \textcolor{comment}{! Zero out tracer tendencies.}}
\DoxyCodeLine{1106   \textcolor{keywordflow}{do} k=1,pemax\_krho ; \textcolor{keywordflow}{do} j=js-\/1,je+1 ; \textcolor{keywordflow}{do} i=is-\/1,ie+1}
\DoxyCodeLine{1107     tr\_flux\_conv(i,j,k) = 0.0}
\DoxyCodeLine{1108 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1109 }
\DoxyCodeLine{1110   \textcolor{keywordflow}{do} itt=1,max\_itt}
\DoxyCodeLine{1111 }
\DoxyCodeLine{1112     \textcolor{keywordflow}{if} (itt > 1) \textcolor{keywordflow}{then} \textcolor{comment}{! The halos have already been filled if itt==1.}}
\DoxyCodeLine{1113       \textcolor{keyword}{call }do\_group\_pass(cs\%pass\_t, g\%Domain, clock=id\_clock\_pass)}
\DoxyCodeLine{1114 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1115 }
\DoxyCodeLine{1116     \textcolor{keywordflow}{do} m=1,ntr}
\DoxyCodeLine{1117 \textcolor{comment}{!\$OMP parallel do default(none) shared(is,ie,js,je,G,Tr,nkmb,nPu,m,max\_kRho,nz,h,h\_exclude, \&}}
\DoxyCodeLine{1118 \textcolor{comment}{!\$OMP                                  k0b\_Lu,k0b\_Ru,deep\_wt\_Lu,k0a\_Lu,deep\_wt\_Ru,k0a\_Ru,   \&}}
\DoxyCodeLine{1119 \textcolor{comment}{!\$OMP                                  hP\_Lu,hP\_Ru,I\_maxitt,khdt\_epi\_x,tr\_flux\_conv,Idt) \&}}
\DoxyCodeLine{1120 \textcolor{comment}{!\$OMP                          private(Tr\_min\_face,Tr\_max\_face,kLa,kLb,kRa,kRb,Tr\_La, \&}}
\DoxyCodeLine{1121 \textcolor{comment}{!\$OMP                                     Tr\_Lb,Tr\_Ra,Tr\_Rb,Tr\_av\_L,wt\_b,Tr\_av\_R,h\_L,h\_R, \&}}
\DoxyCodeLine{1122 \textcolor{comment}{!\$OMP                                     Tr\_flux,Tr\_adj\_vert,wt\_a,vol)}}
\DoxyCodeLine{1123       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie ; \textcolor{keywordflow}{if} (g\%mask2dCu(i,j) > 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1124         \textcolor{comment}{! Determine the fluxes through the zonal faces.}}
\DoxyCodeLine{1125 }
\DoxyCodeLine{1126         \textcolor{comment}{! Find the acceptable range of tracer concentration around this face.}}
\DoxyCodeLine{1127         \textcolor{keywordflow}{if} (npu(i,j) >= 1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1128           tr\_min\_face = min(tr(m)\%t(i,j,1), tr(m)\%t(i+1,j,1))}
\DoxyCodeLine{1129           tr\_max\_face = max(tr(m)\%t(i,j,1), tr(m)\%t(i+1,j,1))}
\DoxyCodeLine{1130           \textcolor{keywordflow}{do} k=2,nkmb}
\DoxyCodeLine{1131             tr\_min\_face = min(tr\_min\_face, tr(m)\%t(i,j,k), tr(m)\%t(i+1,j,k))}
\DoxyCodeLine{1132             tr\_max\_face = max(tr\_max\_face, tr(m)\%t(i,j,k), tr(m)\%t(i+1,j,k))}
\DoxyCodeLine{1133 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{1134 }
\DoxyCodeLine{1135           \textcolor{comment}{! Include the next two layers denser than the densest buffer layer.}}
\DoxyCodeLine{1136           kla = nkmb+1 ; \textcolor{keywordflow}{if} (max\_krho(i,j) < nz+1) kla = max\_krho(i,j)}
\DoxyCodeLine{1137           klb = kla ; \textcolor{keywordflow}{if} (max\_krho(i,j) < nz) klb = max\_krho(i,j)+1}
\DoxyCodeLine{1138           kra = nkmb+1 ; \textcolor{keywordflow}{if} (max\_krho(i+1,j) < nz+1) kra = max\_krho(i+1,j)}
\DoxyCodeLine{1139           krb = kra ; \textcolor{keywordflow}{if} (max\_krho(i+1,j) < nz) krb = max\_krho(i+1,j)+1}
\DoxyCodeLine{1140           tr\_la = tr\_min\_face ; tr\_lb = tr\_la ; tr\_ra = tr\_la ; tr\_rb = tr\_la}
\DoxyCodeLine{1141           \textcolor{keywordflow}{if} (h(i,j,kla) > h\_exclude) tr\_la = tr(m)\%t(i,j,kla)}
\DoxyCodeLine{1142           \textcolor{keywordflow}{if} (h(i,j,klb) > h\_exclude) tr\_la = tr(m)\%t(i,j,klb)}
\DoxyCodeLine{1143           \textcolor{keywordflow}{if} (h(i+1,j,kra) > h\_exclude) tr\_ra = tr(m)\%t(i+1,j,kra)}
\DoxyCodeLine{1144           \textcolor{keywordflow}{if} (h(i+1,j,krb) > h\_exclude) tr\_rb = tr(m)\%t(i+1,j,krb)}
\DoxyCodeLine{1145           tr\_min\_face = min(tr\_min\_face, tr\_la, tr\_lb, tr\_ra, tr\_rb)}
\DoxyCodeLine{1146           tr\_max\_face = max(tr\_max\_face, tr\_la, tr\_lb, tr\_ra, tr\_rb)}
\DoxyCodeLine{1147 }
\DoxyCodeLine{1148           \textcolor{comment}{! Include all points in diffusive pairings at this face.}}
\DoxyCodeLine{1149           \textcolor{keywordflow}{do} k=1,npu(i,j)}
\DoxyCodeLine{1150             tr\_lb = tr(m)\%t(i,j,k0b\_lu(j)\%p(i,k))}
\DoxyCodeLine{1151             tr\_rb = tr(m)\%t(i+1,j,k0b\_ru(j)\%p(i,k))}
\DoxyCodeLine{1152             tr\_la = tr\_lb ; tr\_ra = tr\_rb}
\DoxyCodeLine{1153             \textcolor{keywordflow}{if} (deep\_wt\_lu(j)\%p(i,k) < 1.0) tr\_la = tr(m)\%t(i,j,k0a\_lu(j)\%p(i,k))}
\DoxyCodeLine{1154             \textcolor{keywordflow}{if} (deep\_wt\_ru(j)\%p(i,k) < 1.0) tr\_ra = tr(m)\%t(i+1,j,k0a\_ru(j)\%p(i,k))}
\DoxyCodeLine{1155             tr\_min\_face = min(tr\_min\_face, tr\_la, tr\_lb, tr\_ra, tr\_rb)}
\DoxyCodeLine{1156             tr\_max\_face = max(tr\_max\_face, tr\_la, tr\_lb, tr\_ra, tr\_rb)}
\DoxyCodeLine{1157 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{1158 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1159 }
\DoxyCodeLine{1160         \textcolor{keywordflow}{do} k=1,npu(i,j)}
\DoxyCodeLine{1161           klb = k0b\_lu(j)\%p(i,k) ; tr\_lb = tr(m)\%t(i,j,klb) ; tr\_av\_l = tr\_lb}
\DoxyCodeLine{1162           \textcolor{keywordflow}{if} (deep\_wt\_lu(j)\%p(i,k) < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1163             kla = k0a\_lu(j)\%p(i,k) ; tr\_la = tr(m)\%t(i,j,kla)}
\DoxyCodeLine{1164             wt\_b = deep\_wt\_lu(j)\%p(i,k)}
\DoxyCodeLine{1165             tr\_av\_l = wt\_b*tr\_lb + (1.0-\/wt\_b)*tr\_la}
\DoxyCodeLine{1166 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1167 }
\DoxyCodeLine{1168           krb = k0b\_ru(j)\%p(i,k) ; tr\_rb = tr(m)\%t(i+1,j,krb) ; tr\_av\_r = tr\_rb}
\DoxyCodeLine{1169           \textcolor{keywordflow}{if} (deep\_wt\_ru(j)\%p(i,k) < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1170             kra = k0a\_ru(j)\%p(i,k) ; tr\_ra = tr(m)\%t(i+1,j,kra)}
\DoxyCodeLine{1171             wt\_b = deep\_wt\_ru(j)\%p(i,k)}
\DoxyCodeLine{1172             tr\_av\_r = wt\_b*tr\_rb + (1.0-\/wt\_b)*tr\_ra}
\DoxyCodeLine{1173 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1174 }
\DoxyCodeLine{1175           h\_l = hp\_lu(j)\%p(i,k) ; h\_r = hp\_ru(j)\%p(i,k)}
\DoxyCodeLine{1176           tr\_flux = i\_maxitt * khdt\_epi\_x(i,j) * (tr\_av\_l -\/ tr\_av\_r) * \&}
\DoxyCodeLine{1177             ((2.0 * h\_l * h\_r) / (h\_l + h\_r))}
\DoxyCodeLine{1178 }
\DoxyCodeLine{1179 }
\DoxyCodeLine{1180           \textcolor{keywordflow}{if} (deep\_wt\_lu(j)\%p(i,k) >= 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1181             tr\_flux\_conv(i,j,klb) = tr\_flux\_conv(i,j,klb) -\/ tr\_flux}
\DoxyCodeLine{1182           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1183             tr\_adj\_vert = 0.0}
\DoxyCodeLine{1184             wt\_b = deep\_wt\_lu(j)\%p(i,k) ; wt\_a = 1.0 -\/ wt\_b}
\DoxyCodeLine{1185             vol = hp\_lu(j)\%p(i,k) * g\%areaT(i,j)}
\DoxyCodeLine{1186 }
\DoxyCodeLine{1187             \textcolor{comment}{!   Ensure that the tracer flux does not drive the tracer values}}
\DoxyCodeLine{1188             \textcolor{comment}{! outside of the range Tr\_min\_face <= Tr <= Tr\_max\_face, or if it}}
\DoxyCodeLine{1189             \textcolor{comment}{! does that the concentration in both contributing pieces exceed}}
\DoxyCodeLine{1190             \textcolor{comment}{! this range equally. With down-\/gradient fluxes and the initial tracer}}
\DoxyCodeLine{1191             \textcolor{comment}{! concentrations determining the valid range, the latter condition}}
\DoxyCodeLine{1192             \textcolor{comment}{! only enters for large values of the effective diffusive CFL number.}}
\DoxyCodeLine{1193             \textcolor{keywordflow}{if} (tr\_flux > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1194               \textcolor{keywordflow}{if} (tr\_la < tr\_lb) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (vol*(tr\_la-\/tr\_min\_face) < tr\_flux) \&}
\DoxyCodeLine{1195                 tr\_adj\_vert = -\/wt\_a * min(tr\_flux -\/ vol * (tr\_la-\/tr\_min\_face), \&}
\DoxyCodeLine{1196                                           (vol*wt\_b) * (tr\_lb -\/ tr\_la))}
\DoxyCodeLine{1197               \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{if} (vol*(tr\_lb-\/tr\_min\_face) < tr\_flux) \&}
\DoxyCodeLine{1198                 tr\_adj\_vert = wt\_b * min(tr\_flux -\/ vol * (tr\_lb-\/tr\_min\_face), \&}
\DoxyCodeLine{1199                                          (vol*wt\_a) * (tr\_la -\/ tr\_lb))}
\DoxyCodeLine{1200 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1201             \textcolor{keywordflow}{elseif} (tr\_flux < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1202               \textcolor{keywordflow}{if} (tr\_la > tr\_lb) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (vol * (tr\_max\_face-\/tr\_la) < -\/tr\_flux) \&}
\DoxyCodeLine{1203                 tr\_adj\_vert = wt\_a * min(-\/tr\_flux -\/ vol * (tr\_max\_face-\/tr\_la), \&}
\DoxyCodeLine{1204                                          (vol*wt\_b) * (tr\_la -\/ tr\_lb))}
\DoxyCodeLine{1205               \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{if} (vol*(tr\_max\_face-\/tr\_lb) < -\/tr\_flux) \&}
\DoxyCodeLine{1206                 tr\_adj\_vert = -\/wt\_b * min(-\/tr\_flux -\/ vol * (tr\_max\_face-\/tr\_lb), \&}
\DoxyCodeLine{1207                                           (vol*wt\_a)*(tr\_lb -\/ tr\_la))}
\DoxyCodeLine{1208 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1209 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1210 }
\DoxyCodeLine{1211             tr\_flux\_conv(i,j,kla) = tr\_flux\_conv(i,j,kla) -\/ (wt\_a*tr\_flux + tr\_adj\_vert)}
\DoxyCodeLine{1212             tr\_flux\_conv(i,j,klb) = tr\_flux\_conv(i,j,klb) -\/ (wt\_b*tr\_flux -\/ tr\_adj\_vert)}
\DoxyCodeLine{1213 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1214 }
\DoxyCodeLine{1215           \textcolor{keywordflow}{if} (deep\_wt\_ru(j)\%p(i,k) >= 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1216             tr\_flux\_conv(i+1,j,krb) = tr\_flux\_conv(i+1,j,krb) + tr\_flux}
\DoxyCodeLine{1217           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1218             tr\_adj\_vert = 0.0}
\DoxyCodeLine{1219             wt\_b = deep\_wt\_ru(j)\%p(i,k) ; wt\_a = 1.0 -\/ wt\_b}
\DoxyCodeLine{1220             vol = hp\_ru(j)\%p(i,k) * g\%areaT(i+1,j)}
\DoxyCodeLine{1221 }
\DoxyCodeLine{1222             \textcolor{comment}{!   Ensure that the tracer flux does not drive the tracer values}}
\DoxyCodeLine{1223             \textcolor{comment}{! outside of the range Tr\_min\_face <= Tr <= Tr\_max\_face, or if it}}
\DoxyCodeLine{1224             \textcolor{comment}{! does that the concentration in both contributing pieces exceed}}
\DoxyCodeLine{1225             \textcolor{comment}{! this range equally. With down-\/gradient fluxes and the initial tracer}}
\DoxyCodeLine{1226             \textcolor{comment}{! concentrations determining the valid range, the latter condition}}
\DoxyCodeLine{1227             \textcolor{comment}{! only enters for large values of the effective diffusive CFL number.}}
\DoxyCodeLine{1228             \textcolor{keywordflow}{if} (tr\_flux < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1229               \textcolor{keywordflow}{if} (tr\_ra < tr\_rb) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (vol * (tr\_ra-\/tr\_min\_face) < -\/tr\_flux) \&}
\DoxyCodeLine{1230                 tr\_adj\_vert = -\/wt\_a * min(-\/tr\_flux -\/ vol * (tr\_ra-\/tr\_min\_face), \&}
\DoxyCodeLine{1231                                           (vol*wt\_b) * (tr\_rb -\/ tr\_ra))}
\DoxyCodeLine{1232               \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{if} (vol*(tr\_rb-\/tr\_min\_face) < (-\/tr\_flux)) \&}
\DoxyCodeLine{1233                 tr\_adj\_vert = wt\_b * min(-\/tr\_flux -\/ vol * (tr\_rb-\/tr\_min\_face), \&}
\DoxyCodeLine{1234                                          (vol*wt\_a) * (tr\_ra -\/ tr\_rb))}
\DoxyCodeLine{1235 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1236             \textcolor{keywordflow}{elseif} (tr\_flux > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1237               \textcolor{keywordflow}{if} (tr\_ra > tr\_rb) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (vol * (tr\_max\_face-\/tr\_ra) < tr\_flux) \&}
\DoxyCodeLine{1238                 tr\_adj\_vert = wt\_a * min(tr\_flux -\/ vol * (tr\_max\_face-\/tr\_ra), \&}
\DoxyCodeLine{1239                                          (vol*wt\_b) * (tr\_ra -\/ tr\_rb))}
\DoxyCodeLine{1240               \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{if} (vol*(tr\_max\_face-\/tr\_rb) < tr\_flux) \&}
\DoxyCodeLine{1241                 tr\_adj\_vert = -\/wt\_b * min(tr\_flux -\/ vol * (tr\_max\_face-\/tr\_rb), \&}
\DoxyCodeLine{1242                                           (vol*wt\_a)*(tr\_rb -\/ tr\_ra))}
\DoxyCodeLine{1243 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1244 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1245 }
\DoxyCodeLine{1246             tr\_flux\_conv(i+1,j,kra) = tr\_flux\_conv(i+1,j,kra) + \&}
\DoxyCodeLine{1247                                             (wt\_a*tr\_flux -\/ tr\_adj\_vert)}
\DoxyCodeLine{1248             tr\_flux\_conv(i+1,j,krb) = tr\_flux\_conv(i+1,j,krb) + \&}
\DoxyCodeLine{1249                                             (wt\_b*tr\_flux + tr\_adj\_vert)}
\DoxyCodeLine{1250 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1251           \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tr(m)\%df2d\_x)) \&}
\DoxyCodeLine{1252             tr(m)\%df2d\_x(i,j) = tr(m)\%df2d\_x(i,j) + tr\_flux * idt}
\DoxyCodeLine{1253 \textcolor{keywordflow}{        enddo} \textcolor{comment}{! Loop over pairings at faces.}}
\DoxyCodeLine{1254 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i-\/ \& j-\/ loops over zonal faces.}}
\DoxyCodeLine{1255 }
\DoxyCodeLine{1256 \textcolor{comment}{!\$OMP parallel do default(none) shared(is,ie,js,je,G,Tr,nkmb,nPv,m,max\_kRho,nz,h,h\_exclude, \&}}
\DoxyCodeLine{1257 \textcolor{comment}{!\$OMP                                  k0b\_Lv,k0b\_Rv,deep\_wt\_Lv,k0a\_Lv,deep\_wt\_Rv,k0a\_Rv,   \&}}
\DoxyCodeLine{1258 \textcolor{comment}{!\$OMP                                  hP\_Lv,hP\_Rv,I\_maxitt,khdt\_epi\_y,Tr\_flux\_3d,          \&}}
\DoxyCodeLine{1259 \textcolor{comment}{!\$OMP                                  Tr\_adj\_vert\_L,Tr\_adj\_vert\_R,Idt)                     \&}}
\DoxyCodeLine{1260 \textcolor{comment}{!\$OMP                          private(Tr\_min\_face,Tr\_max\_face,kLa,kLb,kRa,kRb,             \&}}
\DoxyCodeLine{1261 \textcolor{comment}{!\$OMP                                  Tr\_La,Tr\_Lb,Tr\_Ra,Tr\_Rb,Tr\_av\_L,wt\_b,Tr\_av\_R,        \&}}
\DoxyCodeLine{1262 \textcolor{comment}{!\$OMP                                  h\_L,h\_R,Tr\_flux,Tr\_adj\_vert,wt\_a,vol)}}
\DoxyCodeLine{1263       \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (g\%mask2dCv(i,j) > 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1264         \textcolor{comment}{! Determine the fluxes through the meridional faces.}}
\DoxyCodeLine{1265 }
\DoxyCodeLine{1266         \textcolor{comment}{! Find the acceptable range of tracer concentration around this face.}}
\DoxyCodeLine{1267         \textcolor{keywordflow}{if} (npv(i,j) >= 1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1268           tr\_min\_face = min(tr(m)\%t(i,j,1), tr(m)\%t(i,j+1,1))}
\DoxyCodeLine{1269           tr\_max\_face = max(tr(m)\%t(i,j,1), tr(m)\%t(i,j+1,1))}
\DoxyCodeLine{1270           \textcolor{keywordflow}{do} k=2,nkmb}
\DoxyCodeLine{1271             tr\_min\_face = min(tr\_min\_face, tr(m)\%t(i,j,k), tr(m)\%t(i,j+1,k))}
\DoxyCodeLine{1272             tr\_max\_face = max(tr\_max\_face, tr(m)\%t(i,j,k), tr(m)\%t(i,j+1,k))}
\DoxyCodeLine{1273 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{1274 }
\DoxyCodeLine{1275           \textcolor{comment}{! Include the next two layers denser than the densest buffer layer.}}
\DoxyCodeLine{1276           kla = nkmb+1 ; \textcolor{keywordflow}{if} (max\_krho(i,j) < nz+1) kla = max\_krho(i,j)}
\DoxyCodeLine{1277           klb = kla ; \textcolor{keywordflow}{if} (max\_krho(i,j) < nz) klb = max\_krho(i,j)+1}
\DoxyCodeLine{1278           kra = nkmb+1 ; \textcolor{keywordflow}{if} (max\_krho(i,j+1) < nz+1) kra = max\_krho(i,j+1)}
\DoxyCodeLine{1279           krb = kra ; \textcolor{keywordflow}{if} (max\_krho(i,j+1) < nz) krb = max\_krho(i,j+1)+1}
\DoxyCodeLine{1280           tr\_la = tr\_min\_face ; tr\_lb = tr\_la ; tr\_ra = tr\_la ; tr\_rb = tr\_la}
\DoxyCodeLine{1281           \textcolor{keywordflow}{if} (h(i,j,kla) > h\_exclude) tr\_la = tr(m)\%t(i,j,kla)}
\DoxyCodeLine{1282           \textcolor{keywordflow}{if} (h(i,j,klb) > h\_exclude) tr\_la = tr(m)\%t(i,j,klb)}
\DoxyCodeLine{1283           \textcolor{keywordflow}{if} (h(i,j+1,kra) > h\_exclude) tr\_ra = tr(m)\%t(i,j+1,kra)}
\DoxyCodeLine{1284           \textcolor{keywordflow}{if} (h(i,j+1,krb) > h\_exclude) tr\_rb = tr(m)\%t(i,j+1,krb)}
\DoxyCodeLine{1285           tr\_min\_face = min(tr\_min\_face, tr\_la, tr\_lb, tr\_ra, tr\_rb)}
\DoxyCodeLine{1286           tr\_max\_face = max(tr\_max\_face, tr\_la, tr\_lb, tr\_ra, tr\_rb)}
\DoxyCodeLine{1287 }
\DoxyCodeLine{1288           \textcolor{comment}{! Include all points in diffusive pairings at this face.}}
\DoxyCodeLine{1289           \textcolor{keywordflow}{do} k=1,npv(i,j)}
\DoxyCodeLine{1290             tr\_lb = tr(m)\%t(i,j,k0b\_lv(j)\%p(i,k)) ; tr\_rb = tr(m)\%t(i,j+1,k0b\_rv(j)\%p(i,k))}
\DoxyCodeLine{1291             tr\_la = tr\_lb ; tr\_ra = tr\_rb}
\DoxyCodeLine{1292             \textcolor{keywordflow}{if} (deep\_wt\_lv(j)\%p(i,k) < 1.0) tr\_la = tr(m)\%t(i,j,k0a\_lv(j)\%p(i,k))}
\DoxyCodeLine{1293             \textcolor{keywordflow}{if} (deep\_wt\_rv(j)\%p(i,k) < 1.0) tr\_ra = tr(m)\%t(i,j+1,k0a\_rv(j)\%p(i,k))}
\DoxyCodeLine{1294             tr\_min\_face = min(tr\_min\_face, tr\_la, tr\_lb, tr\_ra, tr\_rb)}
\DoxyCodeLine{1295             tr\_max\_face = max(tr\_max\_face, tr\_la, tr\_lb, tr\_ra, tr\_rb)}
\DoxyCodeLine{1296 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{1297 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1298 }
\DoxyCodeLine{1299         \textcolor{keywordflow}{do} k=1,npv(i,j)}
\DoxyCodeLine{1300           klb = k0b\_lv(j)\%p(i,k) ; tr\_lb = tr(m)\%t(i,j,klb) ; tr\_av\_l = tr\_lb}
\DoxyCodeLine{1301           \textcolor{keywordflow}{if} (deep\_wt\_lv(j)\%p(i,k) < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1302             kla = k0a\_lv(j)\%p(i,k) ; tr\_la = tr(m)\%t(i,j,kla)}
\DoxyCodeLine{1303             wt\_b = deep\_wt\_lv(j)\%p(i,k)}
\DoxyCodeLine{1304             tr\_av\_l = wt\_b * tr\_lb + (1.0-\/wt\_b) * tr\_la}
\DoxyCodeLine{1305 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1306 }
\DoxyCodeLine{1307           krb = k0b\_rv(j)\%p(i,k) ; tr\_rb = tr(m)\%t(i,j+1,krb) ; tr\_av\_r = tr\_rb}
\DoxyCodeLine{1308           \textcolor{keywordflow}{if} (deep\_wt\_rv(j)\%p(i,k) < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1309             kra = k0a\_rv(j)\%p(i,k) ; tr\_ra = tr(m)\%t(i,j+1,kra)}
\DoxyCodeLine{1310             wt\_b = deep\_wt\_rv(j)\%p(i,k)}
\DoxyCodeLine{1311             tr\_av\_r = wt\_b * tr\_rb + (1.0-\/wt\_b) * tr\_ra}
\DoxyCodeLine{1312 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1313 }
\DoxyCodeLine{1314           h\_l = hp\_lv(j)\%p(i,k) ; h\_r = hp\_rv(j)\%p(i,k)}
\DoxyCodeLine{1315           tr\_flux = i\_maxitt * ((2.0 * h\_l * h\_r) / (h\_l + h\_r)) * \&}
\DoxyCodeLine{1316                     khdt\_epi\_y(i,j) * (tr\_av\_l -\/ tr\_av\_r)}
\DoxyCodeLine{1317           tr\_flux\_3d(i,j,k) = tr\_flux}
\DoxyCodeLine{1318 }
\DoxyCodeLine{1319           \textcolor{keywordflow}{if} (deep\_wt\_lv(j)\%p(i,k) < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1320             tr\_adj\_vert = 0.0}
\DoxyCodeLine{1321             wt\_b = deep\_wt\_lv(j)\%p(i,k) ; wt\_a = 1.0 -\/ wt\_b}
\DoxyCodeLine{1322             vol = hp\_lv(j)\%p(i,k) * g\%areaT(i,j)}
\DoxyCodeLine{1323 }
\DoxyCodeLine{1324             \textcolor{comment}{!   Ensure that the tracer flux does not drive the tracer values}}
\DoxyCodeLine{1325             \textcolor{comment}{! outside of the range Tr\_min\_face <= Tr <= Tr\_max\_face.}}
\DoxyCodeLine{1326             \textcolor{keywordflow}{if} (tr\_flux > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1327               \textcolor{keywordflow}{if} (tr\_la < tr\_lb) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (vol * (tr\_la-\/tr\_min\_face) < tr\_flux) \&}
\DoxyCodeLine{1328                 tr\_adj\_vert = -\/wt\_a * min(tr\_flux -\/ vol * (tr\_la-\/tr\_min\_face), \&}
\DoxyCodeLine{1329                                           (vol*wt\_b) * (tr\_lb -\/ tr\_la))}
\DoxyCodeLine{1330               \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{if} (vol*(tr\_lb-\/tr\_min\_face) < tr\_flux) \&}
\DoxyCodeLine{1331                 tr\_adj\_vert = wt\_b * min(tr\_flux -\/ vol * (tr\_lb-\/tr\_min\_face), \&}
\DoxyCodeLine{1332                                          (vol*wt\_a) * (tr\_la -\/ tr\_lb))}
\DoxyCodeLine{1333 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1334             \textcolor{keywordflow}{elseif} (tr\_flux < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1335               \textcolor{keywordflow}{if} (tr\_la > tr\_lb) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (vol * (tr\_max\_face-\/tr\_la) < -\/tr\_flux) \&}
\DoxyCodeLine{1336                 tr\_adj\_vert = wt\_a * min(-\/tr\_flux -\/ vol * (tr\_max\_face-\/tr\_la), \&}
\DoxyCodeLine{1337                                          (vol*wt\_b) * (tr\_la -\/ tr\_lb))}
\DoxyCodeLine{1338               \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{if} (vol*(tr\_max\_face-\/tr\_lb) < -\/tr\_flux) \&}
\DoxyCodeLine{1339                 tr\_adj\_vert = -\/wt\_b * min(-\/tr\_flux -\/ vol * (tr\_max\_face-\/tr\_lb), \&}
\DoxyCodeLine{1340                                           (vol*wt\_a)*(tr\_lb -\/ tr\_la))}
\DoxyCodeLine{1341 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1342 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1343             tr\_adj\_vert\_l(i,j,k) = tr\_adj\_vert}
\DoxyCodeLine{1344 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1345 }
\DoxyCodeLine{1346           \textcolor{keywordflow}{if} (deep\_wt\_rv(j)\%p(i,k) < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1347             tr\_adj\_vert = 0.0}
\DoxyCodeLine{1348             wt\_b = deep\_wt\_rv(j)\%p(i,k) ; wt\_a = 1.0 -\/ wt\_b}
\DoxyCodeLine{1349             vol = hp\_rv(j)\%p(i,k) * g\%areaT(i,j+1)}
\DoxyCodeLine{1350 }
\DoxyCodeLine{1351             \textcolor{comment}{!   Ensure that the tracer flux does not drive the tracer values}}
\DoxyCodeLine{1352             \textcolor{comment}{! outside of the range Tr\_min\_face <= Tr <= Tr\_max\_face.}}
\DoxyCodeLine{1353             \textcolor{keywordflow}{if} (tr\_flux < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1354               \textcolor{keywordflow}{if} (tr\_ra < tr\_rb) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (vol * (tr\_ra-\/tr\_min\_face) < -\/tr\_flux) \&}
\DoxyCodeLine{1355                 tr\_adj\_vert = -\/wt\_a * min(-\/tr\_flux -\/ vol * (tr\_ra-\/tr\_min\_face), \&}
\DoxyCodeLine{1356                                           (vol*wt\_b) * (tr\_rb -\/ tr\_ra))}
\DoxyCodeLine{1357               \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{if} (vol*(tr\_rb-\/tr\_min\_face) < (-\/tr\_flux)) \&}
\DoxyCodeLine{1358                 tr\_adj\_vert = wt\_b * min(-\/tr\_flux -\/ vol * (tr\_rb-\/tr\_min\_face), \&}
\DoxyCodeLine{1359                                          (vol*wt\_a) * (tr\_ra -\/ tr\_rb))}
\DoxyCodeLine{1360 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1361             \textcolor{keywordflow}{elseif} (tr\_flux > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1362               \textcolor{keywordflow}{if} (tr\_ra > tr\_rb) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (vol * (tr\_max\_face-\/tr\_ra) < tr\_flux) \&}
\DoxyCodeLine{1363                 tr\_adj\_vert = wt\_a * min(tr\_flux -\/ vol * (tr\_max\_face-\/tr\_ra), \&}
\DoxyCodeLine{1364                                          (vol*wt\_b) * (tr\_ra -\/ tr\_rb))}
\DoxyCodeLine{1365               \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{if} (vol*(tr\_max\_face-\/tr\_rb) < tr\_flux) \&}
\DoxyCodeLine{1366                 tr\_adj\_vert = -\/wt\_b * min(tr\_flux -\/ vol * (tr\_max\_face-\/tr\_rb), \&}
\DoxyCodeLine{1367                                           (vol*wt\_a)*(tr\_rb -\/ tr\_ra))}
\DoxyCodeLine{1368 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{1369 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{1370             tr\_adj\_vert\_r(i,j,k) = tr\_adj\_vert}
\DoxyCodeLine{1371 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1372           \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tr(m)\%df2d\_y)) \&}
\DoxyCodeLine{1373             tr(m)\%df2d\_y(i,j) = tr(m)\%df2d\_y(i,j) + tr\_flux * idt}
\DoxyCodeLine{1374 \textcolor{keywordflow}{        enddo} \textcolor{comment}{! Loop over pairings at faces.}}
\DoxyCodeLine{1375 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i-\/ \& j-\/ loops over meridional faces.}}
\DoxyCodeLine{1376 \textcolor{comment}{!\$OMP parallel do default(none) shared(is,ie,js,je,G,nPv,k0b\_Lv,k0b\_Rv,deep\_wt\_Lv,  \&}}
\DoxyCodeLine{1377 \textcolor{comment}{!\$OMP                                  tr\_flux\_conv,Tr\_flux\_3d,k0a\_Lv,Tr\_adj\_vert\_L,\&}}
\DoxyCodeLine{1378 \textcolor{comment}{!\$OMP                                  deep\_wt\_Rv,k0a\_Rv,Tr\_adj\_vert\_R) \&}}
\DoxyCodeLine{1379 \textcolor{comment}{!\$OMP                          private(kLa,kLb,kRa,kRb,wt\_b,wt\_a)}}
\DoxyCodeLine{1380       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{if} (g\%mask2dCv(i,j) > 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1381         \textcolor{keywordflow}{do} k=1,npv(i,j)}
\DoxyCodeLine{1382           klb = k0b\_lv(j)\%p(i,k); krb = k0b\_rv(j)\%p(i,k)}
\DoxyCodeLine{1383           \textcolor{keywordflow}{if} (deep\_wt\_lv(j)\%p(i,k) >= 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1384             tr\_flux\_conv(i,j,klb) = tr\_flux\_conv(i,j,klb) -\/ tr\_flux\_3d(i,j,k)}
\DoxyCodeLine{1385           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1386             kla = k0a\_lv(j)\%p(i,k)}
\DoxyCodeLine{1387             wt\_b = deep\_wt\_lv(j)\%p(i,k) ; wt\_a = 1.0 -\/ wt\_b}
\DoxyCodeLine{1388             tr\_flux\_conv(i,j,kla) = tr\_flux\_conv(i,j,kla) -\/ (wt\_a*tr\_flux\_3d(i,j,k) + tr\_adj\_vert\_l(i,j,k))}
\DoxyCodeLine{1389             tr\_flux\_conv(i,j,klb) = tr\_flux\_conv(i,j,klb) -\/ (wt\_b*tr\_flux\_3d(i,j,k) -\/ tr\_adj\_vert\_l(i,j,k))}
\DoxyCodeLine{1390 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1391           \textcolor{keywordflow}{if} (deep\_wt\_rv(j)\%p(i,k) >= 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1392             tr\_flux\_conv(i,j+1,krb) = tr\_flux\_conv(i,j+1,krb) + tr\_flux\_3d(i,j,k)}
\DoxyCodeLine{1393           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1394             kra = k0a\_rv(j)\%p(i,k)}
\DoxyCodeLine{1395             wt\_b = deep\_wt\_rv(j)\%p(i,k) ; wt\_a = 1.0 -\/ wt\_b}
\DoxyCodeLine{1396             tr\_flux\_conv(i,j+1,kra) = tr\_flux\_conv(i,j+1,kra) + \&}
\DoxyCodeLine{1397                                             (wt\_a*tr\_flux\_3d(i,j,k) -\/ tr\_adj\_vert\_r(i,j,k))}
\DoxyCodeLine{1398             tr\_flux\_conv(i,j+1,krb) = tr\_flux\_conv(i,j+1,krb) + \&}
\DoxyCodeLine{1399                                             (wt\_b*tr\_flux\_3d(i,j,k) + tr\_adj\_vert\_r(i,j,k))}
\DoxyCodeLine{1400 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1401 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{1402 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1403 \textcolor{comment}{!\$OMP parallel do default(none) shared(PEmax\_kRho,is,ie,js,je,G,h,Tr,tr\_flux\_conv,m)}}
\DoxyCodeLine{1404       \textcolor{keywordflow}{do} k=1,pemax\_krho ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1405         \textcolor{keywordflow}{if} ((g\%mask2dT(i,j) > 0.5) .and. (h(i,j,k) > 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1406           tr(m)\%t(i,j,k) = tr(m)\%t(i,j,k) + tr\_flux\_conv(i,j,k) / \&}
\DoxyCodeLine{1407                                             (h(i,j,k)*g\%areaT(i,j))}
\DoxyCodeLine{1408           tr\_flux\_conv(i,j,k) = 0.0}
\DoxyCodeLine{1409 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1410 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1411 }
\DoxyCodeLine{1412 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! Loop over tracers}}
\DoxyCodeLine{1413 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! Loop over iterations}}
\DoxyCodeLine{1414 }
\DoxyCodeLine{1415   \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{1416     \textcolor{keyword}{deallocate}(deep\_wt\_lu(j)\%p) ; \textcolor{keyword}{deallocate}(deep\_wt\_ru(j)\%p)}
\DoxyCodeLine{1417     \textcolor{keyword}{deallocate}(hp\_lu(j)\%p)  ; \textcolor{keyword}{deallocate}(hp\_ru(j)\%p)}
\DoxyCodeLine{1418     \textcolor{keyword}{deallocate}(k0a\_lu(j)\%p) ; \textcolor{keyword}{deallocate}(k0a\_ru(j)\%p)}
\DoxyCodeLine{1419     \textcolor{keyword}{deallocate}(k0b\_lu(j)\%p) ; \textcolor{keyword}{deallocate}(k0b\_ru(j)\%p)}
\DoxyCodeLine{1420 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1421 }
\DoxyCodeLine{1422   \textcolor{keywordflow}{do} j=js-\/1,je}
\DoxyCodeLine{1423     \textcolor{keyword}{deallocate}(deep\_wt\_lv(j)\%p) ; \textcolor{keyword}{deallocate}(deep\_wt\_rv(j)\%p)}
\DoxyCodeLine{1424     \textcolor{keyword}{deallocate}(hp\_lv(j)\%p)  ; \textcolor{keyword}{deallocate}(hp\_rv(j)\%p)}
\DoxyCodeLine{1425     \textcolor{keyword}{deallocate}(k0a\_lv(j)\%p) ; \textcolor{keyword}{deallocate}(k0a\_rv(j)\%p)}
\DoxyCodeLine{1426     \textcolor{keyword}{deallocate}(k0b\_lv(j)\%p) ; \textcolor{keyword}{deallocate}(k0b\_rv(j)\%p)}
\DoxyCodeLine{1427 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1428 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__tracer__hor__diff_a715439f7286842d78d2ce52b7e5371a4}\label{namespacemom__tracer__hor__diff_a715439f7286842d78d2ce52b7e5371a4}} 
\index{mom\_tracer\_hor\_diff@{mom\_tracer\_hor\_diff}!tracer\_hor\_diff\_end@{tracer\_hor\_diff\_end}}
\index{tracer\_hor\_diff\_end@{tracer\_hor\_diff\_end}!mom\_tracer\_hor\_diff@{mom\_tracer\_hor\_diff}}
\doxysubsubsection{\texorpdfstring{tracer\_hor\_diff\_end()}{tracer\_hor\_diff\_end()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+tracer\+\_\+hor\+\_\+diff\+::tracer\+\_\+hor\+\_\+diff\+\_\+end (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__tracer__hor__diff_1_1tracer__hor__diff__cs}{tracer\+\_\+hor\+\_\+diff\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}


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


Definition at line 1553 of file M\+O\+M\+\_\+tracer\+\_\+hor\+\_\+diff.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1554   \textcolor{keywordtype}{type}(tracer\_hor\_diff\_CS), \textcolor{keywordtype}{pointer} :: CS\textcolor{comment}{ !< module control structure}}
\DoxyCodeLine{1555 }
\DoxyCodeLine{1556   \textcolor{keyword}{call }neutral\_diffusion\_end(cs\%neutral\_diffusion\_CSp)}
\DoxyCodeLine{1557   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{deallocate}(cs)}
\DoxyCodeLine{1558 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__tracer__hor__diff_a87eed0408da0c4732372732b34451b73}\label{namespacemom__tracer__hor__diff_a87eed0408da0c4732372732b34451b73}} 
\index{mom\_tracer\_hor\_diff@{mom\_tracer\_hor\_diff}!tracer\_hor\_diff\_init@{tracer\_hor\_diff\_init}}
\index{tracer\_hor\_diff\_init@{tracer\_hor\_diff\_init}!mom\_tracer\_hor\_diff@{mom\_tracer\_hor\_diff}}
\doxysubsubsection{\texorpdfstring{tracer\_hor\_diff\_init()}{tracer\_hor\_diff\_init()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+tracer\+\_\+hor\+\_\+diff\+::tracer\+\_\+hor\+\_\+diff\+\_\+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), intent(in), pointer}]{diabatic\+\_\+\+C\+Sp,  }\item[{type(\mbox{\hyperlink{structmom__tracer__hor__diff_1_1tracer__hor__diff__cs}{tracer\+\_\+hor\+\_\+diff\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Initialize lateral tracer diffusion module. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em time} & current model time \\
\hline
\mbox{\texttt{ in}}  & {\em g} & ocean grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in,out}}  & {\em diag} & diagnostic control \\
\hline
\mbox{\texttt{ in}}  & {\em eos} & Equation of state CS \\
\hline
\mbox{\texttt{ in}}  & {\em diabatic\+\_\+csp} & Equation of state CS \\
\hline
\mbox{\texttt{ in}}  & {\em param\+\_\+file} & parameter file \\
\hline
 & {\em cs} & horz diffusion control structure \\
\hline
\end{DoxyParams}


Definition at line 1433 of file M\+O\+M\+\_\+tracer\+\_\+hor\+\_\+diff.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1434   \textcolor{keywordtype}{type}(time\_type), \textcolor{keywordtype}{target},    \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{       !< current model time}}
\DoxyCodeLine{1435   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{          !< ocean grid structure}}
\DoxyCodeLine{1436   \textcolor{keywordtype}{type}(unit\_scale\_type),      \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{         !< A dimensional unit scaling type}}
\DoxyCodeLine{1437   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target},    \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{       !< diagnostic control}}
\DoxyCodeLine{1438   \textcolor{keywordtype}{type}(EOS\_type),  \textcolor{keywordtype}{target},    \textcolor{keywordtype}{intent(in)}    :: EOS\textcolor{comment}{        !< Equation of state CS}}
\DoxyCodeLine{1439   \textcolor{keywordtype}{type}(diabatic\_CS), \textcolor{keywordtype}{pointer}, \textcolor{keywordtype}{intent(in)}    :: diabatic\_CSp\textcolor{comment}{ !< Equation of state CS}}
\DoxyCodeLine{1440   \textcolor{keywordtype}{type}(param\_file\_type),      \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< parameter file}}
\DoxyCodeLine{1441   \textcolor{keywordtype}{type}(tracer\_hor\_diff\_CS),   \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{         !< horz diffusion control structure}}
\DoxyCodeLine{1442 }
\DoxyCodeLine{1443 \textcolor{comment}{! This include declares and sets the variable "version".}}
\DoxyCodeLine{1444 \textcolor{preprocessor}{\#include "version\_variable.h"}}
\DoxyCodeLine{1445 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_tracer\_hor\_diff"} \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{1446   \textcolor{keywordtype}{character(len=256)} :: mesg    \textcolor{comment}{! Message for error messages.}}
\DoxyCodeLine{1447 }
\DoxyCodeLine{1448   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1449     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"tracer\_hor\_diff\_init called with associated control structure."})}
\DoxyCodeLine{1450     \textcolor{keywordflow}{return}}
\DoxyCodeLine{1451 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1452   \textcolor{keyword}{allocate}(cs)}
\DoxyCodeLine{1453 }
\DoxyCodeLine{1454   cs\%diag => diag}
\DoxyCodeLine{1455   cs\%show\_call\_tree = calltree\_showquery()}
\DoxyCodeLine{1456 }
\DoxyCodeLine{1457   \textcolor{comment}{! Read all relevant parameters and write them to the model log.}}
\DoxyCodeLine{1458   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \textcolor{stringliteral}{""})}
\DoxyCodeLine{1459   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KHTR"}, cs\%KhTr, \&}
\DoxyCodeLine{1460                  \textcolor{stringliteral}{"The background along-\/isopycnal tracer diffusivity."}, \&}
\DoxyCodeLine{1461                  units=\textcolor{stringliteral}{"m2 s-\/1"}, default=0.0, scale=us\%m\_to\_L**2*us\%T\_to\_s)}
\DoxyCodeLine{1462   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KHTR\_SLOPE\_CFF"}, cs\%KhTr\_Slope\_Cff, \&}
\DoxyCodeLine{1463                  \textcolor{stringliteral}{"The scaling coefficient for along-\/isopycnal tracer "}//\&}
\DoxyCodeLine{1464                  \textcolor{stringliteral}{"diffusivity using a shear-\/based (Visbeck-\/like) "}//\&}
\DoxyCodeLine{1465                  \textcolor{stringliteral}{"parameterization.  A non-\/zero value enables this param."}, \&}
\DoxyCodeLine{1466                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)}
\DoxyCodeLine{1467   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KHTR\_MIN"}, cs\%KhTr\_Min, \&}
\DoxyCodeLine{1468                  \textcolor{stringliteral}{"The minimum along-\/isopycnal tracer diffusivity."}, \&}
\DoxyCodeLine{1469                  units=\textcolor{stringliteral}{"m2 s-\/1"}, default=0.0, scale=us\%m\_to\_L**2*us\%T\_to\_s)}
\DoxyCodeLine{1470   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KHTR\_MAX"}, cs\%KhTr\_Max, \&}
\DoxyCodeLine{1471                  \textcolor{stringliteral}{"The maximum along-\/isopycnal tracer diffusivity."}, \&}
\DoxyCodeLine{1472                  units=\textcolor{stringliteral}{"m2 s-\/1"}, default=0.0, scale=us\%m\_to\_L**2*us\%T\_to\_s)}
\DoxyCodeLine{1473   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KHTR\_PASSIVITY\_COEFF"}, cs\%KhTr\_passivity\_coeff, \&}
\DoxyCodeLine{1474                  \textcolor{stringliteral}{"The coefficient that scales deformation radius over "}//\&}
\DoxyCodeLine{1475                  \textcolor{stringliteral}{"grid-\/spacing in passivity, where passivity is the ratio "}//\&}
\DoxyCodeLine{1476                  \textcolor{stringliteral}{"between along isopycnal mixing of tracers to thickness mixing. "}//\&}
\DoxyCodeLine{1477                  \textcolor{stringliteral}{"A non-\/zero value enables this parameterization."}, \&}
\DoxyCodeLine{1478                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)}
\DoxyCodeLine{1479   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KHTR\_PASSIVITY\_MIN"}, cs\%KhTr\_passivity\_min, \&}
\DoxyCodeLine{1480                  \textcolor{stringliteral}{"The minimum passivity which is the ratio between "}//\&}
\DoxyCodeLine{1481                  \textcolor{stringliteral}{"along isopycnal mixing of tracers to thickness mixing."}, \&}
\DoxyCodeLine{1482                  units=\textcolor{stringliteral}{"nondim"}, default=0.5)}
\DoxyCodeLine{1483   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DIFFUSE\_ML\_TO\_INTERIOR"}, cs\%Diffuse\_ML\_interior, \&}
\DoxyCodeLine{1484                  \textcolor{stringliteral}{"If true, enable epipycnal mixing between the surface "}//\&}
\DoxyCodeLine{1485                  \textcolor{stringliteral}{"boundary layer and the interior."}, default=.false.)}
\DoxyCodeLine{1486   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CHECK\_DIFFUSIVE\_CFL"}, cs\%check\_diffusive\_CFL, \&}
\DoxyCodeLine{1487                  \textcolor{stringliteral}{"If true, use enough iterations the diffusion to ensure "}//\&}
\DoxyCodeLine{1488                  \textcolor{stringliteral}{"that the diffusive equivalent of the CFL limit is not "}//\&}
\DoxyCodeLine{1489                  \textcolor{stringliteral}{"violated.  If false, always use the greater of 1 or "}//\&}
\DoxyCodeLine{1490                  \textcolor{stringliteral}{"MAX\_TR\_DIFFUSION\_CFL iteration."}, default=.false.)}
\DoxyCodeLine{1491   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MAX\_TR\_DIFFUSION\_CFL"}, cs\%max\_diff\_CFL, \&}
\DoxyCodeLine{1492                  \textcolor{stringliteral}{"If positive, locally limit the along-\/isopycnal tracer "}//\&}
\DoxyCodeLine{1493                  \textcolor{stringliteral}{"diffusivity to keep the diffusive CFL locally at or "}//\&}
\DoxyCodeLine{1494                  \textcolor{stringliteral}{"below this value.  The number of diffusive iterations "}//\&}
\DoxyCodeLine{1495                  \textcolor{stringliteral}{"is often this value or the next greater integer."}, \&}
\DoxyCodeLine{1496                  units=\textcolor{stringliteral}{"nondim"}, default=-\/1.0)}
\DoxyCodeLine{1497   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"RECALC\_NEUTRAL\_SURF"}, cs\%recalc\_neutral\_surf, \&}
\DoxyCodeLine{1498                  \textcolor{stringliteral}{"If true, then recalculate the neutral surfaces if the \(\backslash\)n"}//\&}
\DoxyCodeLine{1499                  \textcolor{stringliteral}{"diffusive CFL is exceeded. If false, assume that the  \(\backslash\)n"}//\&}
\DoxyCodeLine{1500                  \textcolor{stringliteral}{"positions of the surfaces do not change \(\backslash\)n"}, default = .false.)}
\DoxyCodeLine{1501   cs\%ML\_KhTR\_scale = 1.0}
\DoxyCodeLine{1502   \textcolor{keywordflow}{if} (cs\%Diffuse\_ML\_interior) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1503     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_KHTR\_SCALE"}, cs\%ML\_KhTR\_scale, \&}
\DoxyCodeLine{1504                  \textcolor{stringliteral}{"With Diffuse\_ML\_interior, the ratio of the truly "}//\&}
\DoxyCodeLine{1505                  \textcolor{stringliteral}{"horizontal diffusivity in the mixed layer to the "}//\&}
\DoxyCodeLine{1506                  \textcolor{stringliteral}{"epipycnal diffusivity.  The valid range is 0 to 1."}, \&}
\DoxyCodeLine{1507                  units=\textcolor{stringliteral}{"nondim"}, default=1.0)}
\DoxyCodeLine{1508 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1509 }
\DoxyCodeLine{1510   cs\%use\_neutral\_diffusion = neutral\_diffusion\_init(time, g, us, param\_file, diag, eos, \&}
\DoxyCodeLine{1511                                                     diabatic\_csp, cs\%neutral\_diffusion\_CSp )}
\DoxyCodeLine{1512   \textcolor{keywordflow}{if} (cs\%use\_neutral\_diffusion .and. cs\%Diffuse\_ML\_interior) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_tracer\_hor\_diff: "}// \&}
\DoxyCodeLine{1513        \textcolor{stringliteral}{"USE\_NEUTRAL\_DIFFUSION and DIFFUSE\_ML\_TO\_INTERIOR are mutually exclusive!"})}
\DoxyCodeLine{1514   cs\%use\_lateral\_boundary\_diffusion = lateral\_boundary\_diffusion\_init(time, g, param\_file, diag, diabatic\_csp, \&}
\DoxyCodeLine{1515                                                                 cs\%lateral\_boundary\_diffusion\_CSp)}
\DoxyCodeLine{1516   \textcolor{keywordflow}{if} (cs\%use\_neutral\_diffusion .and. cs\%Diffuse\_ML\_interior) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_tracer\_hor\_diff: "}// \&}
\DoxyCodeLine{1517        \textcolor{stringliteral}{"USE\_LATERAL\_BOUNDARY\_DIFFUSION and DIFFUSE\_ML\_TO\_INTERIOR are mutually exclusive!"})}
\DoxyCodeLine{1518 }
\DoxyCodeLine{1519   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEBUG"}, cs\%debug, default=.false.)}
\DoxyCodeLine{1520 }
\DoxyCodeLine{1521   id\_clock\_diffuse = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean diffuse tracer)'},          grain=clock\_module)}
\DoxyCodeLine{1522   id\_clock\_epimix  = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean epipycnal diffuse tracer)'},grain=clock\_module)}
\DoxyCodeLine{1523   id\_clock\_pass    = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean tracer halo updates)'},     grain=clock\_routine)}
\DoxyCodeLine{1524   id\_clock\_sync    = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean tracer global synch)'},     grain=clock\_routine)}
\DoxyCodeLine{1525 }
\DoxyCodeLine{1526   cs\%id\_KhTr\_u = -\/1}
\DoxyCodeLine{1527   cs\%id\_KhTr\_v = -\/1}
\DoxyCodeLine{1528   cs\%id\_KhTr\_h = -\/1}
\DoxyCodeLine{1529   cs\%id\_CFL    = -\/1}
\DoxyCodeLine{1530 }
\DoxyCodeLine{1531   cs\%id\_KhTr\_u = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'KHTR\_u'}, diag\%axesCu1, time, \&}
\DoxyCodeLine{1532      \textcolor{stringliteral}{'Epipycnal tracer diffusivity at zonal faces of tracer cell'}, \textcolor{stringliteral}{'m2 s-\/1'}, conversion=us\%L\_to\_m**2*us\%s\_to\_T)}
\DoxyCodeLine{1533   cs\%id\_KhTr\_v = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'KHTR\_v'}, diag\%axesCv1, time, \&}
\DoxyCodeLine{1534      \textcolor{stringliteral}{'Epipycnal tracer diffusivity at meridional faces of tracer cell'}, \textcolor{stringliteral}{'m2 s-\/1'}, conversion=us\%L\_to\_m**2*us\%s\_to\_T)}
\DoxyCodeLine{1535   cs\%id\_KhTr\_h = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'KHTR\_h'}, diag\%axesT1, time, \&}
\DoxyCodeLine{1536      \textcolor{stringliteral}{'Epipycnal tracer diffusivity at tracer cell center'}, \textcolor{stringliteral}{'m2 s-\/1'}, conversion=us\%L\_to\_m**2*us\%s\_to\_T, \&}
\DoxyCodeLine{1537      cmor\_field\_name=\textcolor{stringliteral}{'diftrelo'},                                                \&}
\DoxyCodeLine{1538      cmor\_standard\_name= \textcolor{stringliteral}{'ocean\_tracer\_epineutral\_laplacian\_diffusivity'},       \&}
\DoxyCodeLine{1539      cmor\_long\_name = \textcolor{stringliteral}{'Ocean Tracer Epineutral Laplacian Diffusivity'})}
\DoxyCodeLine{1540 }
\DoxyCodeLine{1541   cs\%id\_khdt\_x = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'KHDT\_x'}, diag\%axesCu1, time, \&}
\DoxyCodeLine{1542      \textcolor{stringliteral}{'Epipycnal tracer diffusivity operator at zonal faces of tracer cell'}, \textcolor{stringliteral}{'m2'}, conversion=us\%L\_to\_m**2)}
\DoxyCodeLine{1543   cs\%id\_khdt\_y = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'KHDT\_y'}, diag\%axesCv1, time, \&}
\DoxyCodeLine{1544      \textcolor{stringliteral}{'Epipycnal tracer diffusivity operator at meridional faces of tracer cell'}, \textcolor{stringliteral}{'m2'}, conversion=us\%L\_to\_m**2)}
\DoxyCodeLine{1545   \textcolor{keywordflow}{if} (cs\%check\_diffusive\_CFL) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1546     cs\%id\_CFL = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'CFL\_lateral\_diff'}, diag\%axesT1, time,\&}
\DoxyCodeLine{1547        \textcolor{stringliteral}{'Grid CFL number for lateral/neutral tracer diffusion'}, \textcolor{stringliteral}{'nondim'})}
\DoxyCodeLine{1548 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1549 }
\DoxyCodeLine{1550 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__tracer__hor__diff_a098229e37012e7bd93d13036bfc864ac}\label{namespacemom__tracer__hor__diff_a098229e37012e7bd93d13036bfc864ac}} 
\index{mom\_tracer\_hor\_diff@{mom\_tracer\_hor\_diff}!tracer\_hordiff@{tracer\_hordiff}}
\index{tracer\_hordiff@{tracer\_hordiff}!mom\_tracer\_hor\_diff@{mom\_tracer\_hor\_diff}}
\doxysubsubsection{\texorpdfstring{tracer\_hordiff()}{tracer\_hordiff()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+tracer\+\_\+hor\+\_\+diff\+::tracer\+\_\+hordiff (\begin{DoxyParamCaption}\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{real, intent(in)}]{dt,  }\item[{type(meke\+\_\+type), pointer}]{M\+E\+KE,  }\item[{type(varmix\+\_\+cs), pointer}]{Var\+Mix,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(inout)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__tracer__hor__diff_1_1tracer__hor__diff__cs}{tracer\+\_\+hor\+\_\+diff\+\_\+cs}}), pointer}]{CS,  }\item[{type(tracer\+\_\+registry\+\_\+type), pointer}]{Reg,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{logical, intent(in), optional}]{do\+\_\+online\+\_\+flag,  }\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed), intent(in), optional}]{read\+\_\+khdt\+\_\+x,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb), intent(in), optional}]{read\+\_\+khdt\+\_\+y }\end{DoxyParamCaption})}



Compute along-\/coordinate diffusion of all tracers using the diffusivity in CSKh\+Tr, or using space-\/dependent diffusivity. Multiple iterations are used (if necessary) so that there is no limit on the acceptable time increment. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em g} & Grid type \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & time step \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
 & {\em meke} & M\+E\+KE type \\
\hline
 & {\em varmix} & Variable mixing type \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & ocean vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & module control structure \\
\hline
 & {\em reg} & registered tracers \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields, including potential temp and salinity or mixed layer density. Absent fields have N\+U\+LL ptrs, and these may (probably will) point to some of the same arrays as Tr does. tv is required for epipycnal mixing between mixed layer and the interior. \\
\hline
\mbox{\texttt{ in}}  & {\em do\+\_\+online\+\_\+flag} & If present and true, do online tracer transport with stored velocities. \\
\hline
\mbox{\texttt{ in}}  & {\em read\+\_\+khdt\+\_\+x} & If present, these are the zonal \\
\hline
\mbox{\texttt{ in}}  & {\em read\+\_\+khdt\+\_\+y} & If present, these are the meridional \\
\hline
\end{DoxyParams}


Definition at line 106 of file M\+O\+M\+\_\+tracer\+\_\+hor\+\_\+diff.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{107   \textcolor{keywordtype}{type}(ocean\_grid\_type),      \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{       !< Grid type}}
\DoxyCodeLine{108 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{109                               \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{       !< Layer thickness [H \string~> m or kg m-\/2]}}
\DoxyCodeLine{110 \textcolor{keywordtype}{  real},                       \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{      !< time step [T \string~> s]}}
\DoxyCodeLine{111   \textcolor{keywordtype}{type}(MEKE\_type),            \textcolor{keywordtype}{pointer}       :: MEKE\textcolor{comment}{    !< MEKE type}}
\DoxyCodeLine{112   \textcolor{keywordtype}{type}(VarMix\_CS),            \textcolor{keywordtype}{pointer}       :: VarMix\textcolor{comment}{  !< Variable mixing type}}
\DoxyCodeLine{113   \textcolor{keywordtype}{type}(verticalGrid\_type),    \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{      !< ocean vertical grid structure}}
\DoxyCodeLine{114   \textcolor{keywordtype}{type}(unit\_scale\_type),      \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{      !< A dimensional unit scaling type}}
\DoxyCodeLine{115   \textcolor{keywordtype}{type}(tracer\_hor\_diff\_CS),   \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{      !< module control structure}}
\DoxyCodeLine{116   \textcolor{keywordtype}{type}(tracer\_registry\_type), \textcolor{keywordtype}{pointer}       :: Reg\textcolor{comment}{     !< registered tracers}}
\DoxyCodeLine{117   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),      \textcolor{keywordtype}{intent(in)}    :: tv\textcolor{comment}{      !< A structure containing pointers to any available}}
\DoxyCodeLine{118 \textcolor{comment}{                                                       !! thermodynamic fields, including potential temp and}}
\DoxyCodeLine{119 \textcolor{comment}{                                                       !! salinity or mixed layer density. Absent fields have}}
\DoxyCodeLine{120 \textcolor{comment}{                                                       !! NULL ptrs, and these may (probably will) point to}}
\DoxyCodeLine{121 \textcolor{comment}{                                                       !! some of the same arrays as Tr does.  tv is required}}
\DoxyCodeLine{122 \textcolor{comment}{                                                       !! for epipycnal mixing between mixed layer and the interior.}}
\DoxyCodeLine{123   \textcolor{comment}{! Optional inputs for offline tracer transport}}
\DoxyCodeLine{124   \textcolor{keywordtype}{logical},          \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: do\_online\_flag\textcolor{comment}{ !< If present and true, do online}}
\DoxyCodeLine{125 \textcolor{comment}{                                                       !! tracer transport with stored velocities.}}
\DoxyCodeLine{126 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))}, \&}
\DoxyCodeLine{127                     \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: read\_khdt\_x\textcolor{comment}{ !< If present, these are the zonal}}
\DoxyCodeLine{128 \textcolor{comment}{                                                       !! diffusivities from previous run.}}
\DoxyCodeLine{129 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))}, \&}
\DoxyCodeLine{130                     \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: read\_khdt\_y\textcolor{comment}{ !< If present, these are the meridional}}
\DoxyCodeLine{131 \textcolor{comment}{                                                       !! diffusivities from previous run.}}
\DoxyCodeLine{132 }
\DoxyCodeLine{133 }
\DoxyCodeLine{134 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: \&}
\DoxyCodeLine{135     Ihdxdy, \&     \textcolor{comment}{! The inverse of the volume or mass of fluid in a layer in a}}
\DoxyCodeLine{136                   \textcolor{comment}{! grid cell [H-\/1 L-\/2 \string~> m-\/3 or kg-\/1].}}
\DoxyCodeLine{137     kh\_h, \&       \textcolor{comment}{! The tracer diffusivity averaged to tracer points [L2 T-\/1 \string~> m2 s-\/1].}}
\DoxyCodeLine{138     cfl, \&        \textcolor{comment}{! A diffusive CFL number for each cell [nondim].}}
\DoxyCodeLine{139     dtr           \textcolor{comment}{! The change in a tracer's concentration, in units of concentration [Conc].}}
\DoxyCodeLine{140 }
\DoxyCodeLine{141 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))} :: \&}
\DoxyCodeLine{142     khdt\_x, \&     \textcolor{comment}{! The value of Khtr*dt times the open face width divided by}}
\DoxyCodeLine{143                   \textcolor{comment}{! the distance between adjacent tracer points [L2 \string~> m2].}}
\DoxyCodeLine{144     coef\_x, \&     \textcolor{comment}{! The coefficients relating zonal tracer differences to time-\/integrated}}
\DoxyCodeLine{145                   \textcolor{comment}{! fluxes, in [L2 \string~> m2] for some schemes and [H L2 \string~> m3 or kg] for others.}}
\DoxyCodeLine{146     kh\_u          \textcolor{comment}{! Tracer mixing coefficient at u-\/points [L2 T-\/1 \string~> m2 s-\/1].}}
\DoxyCodeLine{147 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))} :: \&}
\DoxyCodeLine{148     khdt\_y, \&     \textcolor{comment}{! The value of Khtr*dt times the open face width divided by}}
\DoxyCodeLine{149                   \textcolor{comment}{! the distance between adjacent tracer points [L2 \string~> m2].}}
\DoxyCodeLine{150     coef\_y, \&     \textcolor{comment}{! The coefficients relating meridional tracer differences to time-\/integrated}}
\DoxyCodeLine{151                   \textcolor{comment}{! fluxes, in [L2 \string~> m2] for some schemes and [H L2 \string~> m3 or kg] for others.}}
\DoxyCodeLine{152     kh\_v          \textcolor{comment}{! Tracer mixing coefficient at u-\/points [L2 T-\/1 \string~> m2 s-\/1].}}
\DoxyCodeLine{153 }
\DoxyCodeLine{154 \textcolor{keywordtype}{  real} :: khdt\_max \textcolor{comment}{! The local limiting value of khdt\_x or khdt\_y [L2 \string~> m2].}}
\DoxyCodeLine{155 \textcolor{keywordtype}{  real} :: max\_CFL \textcolor{comment}{! The global maximum of the diffusive CFL number.}}
\DoxyCodeLine{156   \textcolor{keywordtype}{logical} :: use\_VarMix, Resoln\_scaled, do\_online, use\_Eady}
\DoxyCodeLine{157   \textcolor{keywordtype}{integer} :: S\_idx, T\_idx \textcolor{comment}{! Indices for temperature and salinity if needed}}
\DoxyCodeLine{158   \textcolor{keywordtype}{integer} :: i, j, k, m, is, ie, js, je, nz, ntr, itt, num\_itts}
\DoxyCodeLine{159 \textcolor{keywordtype}{  real} :: I\_numitts  \textcolor{comment}{! The inverse of the number of iterations, num\_itts.}}
\DoxyCodeLine{160 \textcolor{keywordtype}{  real} :: scale      \textcolor{comment}{! The fraction of khdt\_x or khdt\_y that is applied in this}}
\DoxyCodeLine{161                      \textcolor{comment}{! layer for this iteration [nondim].}}
\DoxyCodeLine{162 \textcolor{keywordtype}{  real} :: Idt        \textcolor{comment}{! The inverse of the time step [T-\/1 \string~> s-\/1].}}
\DoxyCodeLine{163 \textcolor{keywordtype}{  real} :: h\_neglect  \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{164                      \textcolor{comment}{! in roundoff and can be neglected [H \string~> m or kg m-\/2].}}
\DoxyCodeLine{165 \textcolor{keywordtype}{  real} :: Kh\_loc     \textcolor{comment}{! The local value of Kh [L2 T-\/1 \string~> m2 s-\/1].}}
\DoxyCodeLine{166 \textcolor{keywordtype}{  real} :: Res\_Fn     \textcolor{comment}{! The local value of the resolution function [nondim].}}
\DoxyCodeLine{167 \textcolor{keywordtype}{  real} :: Rd\_dx      \textcolor{comment}{! The local value of deformation radius over grid-\/spacing [nondim].}}
\DoxyCodeLine{168 \textcolor{keywordtype}{  real} :: normalize  \textcolor{comment}{! normalization used for diagnostic Kh\_h; diffusivity averaged to h-\/points.}}
\DoxyCodeLine{169 }
\DoxyCodeLine{170   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = gv\%ke}
\DoxyCodeLine{171 }
\DoxyCodeLine{172   do\_online = .true.}
\DoxyCodeLine{173   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(do\_online\_flag)) do\_online = do\_online\_flag}
\DoxyCodeLine{174 }
\DoxyCodeLine{175   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_tracer\_hor\_diff: "}// \&}
\DoxyCodeLine{176        \textcolor{stringliteral}{"register\_tracer must be called before tracer\_hordiff."})}
\DoxyCodeLine{177   \textcolor{keywordflow}{if} (loc(reg)==0) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_tracer\_hor\_diff: "}// \&}
\DoxyCodeLine{178        \textcolor{stringliteral}{"register\_tracer must be called before tracer\_hordiff."})}
\DoxyCodeLine{179   \textcolor{keywordflow}{if} ((reg\%ntr==0) .or. ((cs\%KhTr <= 0.0) .and. .not.\textcolor{keyword}{associated}(varmix)) ) \textcolor{keywordflow}{return}}
\DoxyCodeLine{180 }
\DoxyCodeLine{181   \textcolor{keywordflow}{if} (cs\%show\_call\_tree) \textcolor{keyword}{call }calltree\_enter(\textcolor{stringliteral}{"tracer\_hordiff(), MOM\_tracer\_hor\_diff.F90"})}
\DoxyCodeLine{182 }
\DoxyCodeLine{183   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_diffuse)}
\DoxyCodeLine{184 }
\DoxyCodeLine{185   ntr = reg\%ntr}
\DoxyCodeLine{186   idt = 1.0 / dt}
\DoxyCodeLine{187   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{188 }
\DoxyCodeLine{189   \textcolor{keywordflow}{if} (cs\%Diffuse\_ML\_interior .and. cs\%first\_call) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (is\_root\_pe()) \textcolor{keywordflow}{then}}
\DoxyCodeLine{190     \textcolor{keywordflow}{do} m=1,ntr ; \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(reg\%Tr(m)\%df\_x) .or. \textcolor{keyword}{associated}(reg\%Tr(m)\%df\_y)) \&}
\DoxyCodeLine{191       \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"tracer\_hordiff: Tracer "}//trim(reg\%Tr(m)\%name)// \&}
\DoxyCodeLine{192           \textcolor{stringliteral}{" has associated 3-\/d diffusive flux diagnostics.  These are not "}//\&}
\DoxyCodeLine{193           \textcolor{stringliteral}{"valid when DIFFUSE\_ML\_TO\_INTERIOR is defined. Use 2-\/d tracer "}//\&}
\DoxyCodeLine{194           \textcolor{stringliteral}{"diffusion diagnostics instead to get accurate total fluxes."})}
\DoxyCodeLine{195 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{196 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{197   cs\%first\_call = .false.}
\DoxyCodeLine{198 }
\DoxyCodeLine{199   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keyword}{call }mom\_tracer\_chksum(\textcolor{stringliteral}{"Before tracer diffusion "}, reg\%Tr, ntr, g)}
\DoxyCodeLine{200 }
\DoxyCodeLine{201   use\_varmix = .false. ; resoln\_scaled = .false. ; use\_eady = .false.}
\DoxyCodeLine{202   \textcolor{keywordflow}{if} (\textcolor{keyword}{Associated}(varmix)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{203     use\_varmix = varmix\%use\_variable\_mixing}
\DoxyCodeLine{204     resoln\_scaled = varmix\%Resoln\_scaled\_KhTr}
\DoxyCodeLine{205     use\_eady = cs\%KhTr\_Slope\_Cff > 0.}
\DoxyCodeLine{206 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{207 }
\DoxyCodeLine{208   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_pass)}
\DoxyCodeLine{209   \textcolor{keywordflow}{do} m=1,ntr}
\DoxyCodeLine{210     \textcolor{keyword}{call }create\_group\_pass(cs\%pass\_t, reg\%Tr(m)\%t(:,:,:), g\%Domain)}
\DoxyCodeLine{211 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{212   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_pass)}
\DoxyCodeLine{213 }
\DoxyCodeLine{214   \textcolor{keywordflow}{if} (cs\%show\_call\_tree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"Calculating diffusivity (tracer\_hordiff)"})}
\DoxyCodeLine{215 }
\DoxyCodeLine{216   \textcolor{keywordflow}{if} (do\_online) \textcolor{keywordflow}{then}}
\DoxyCodeLine{217     \textcolor{keywordflow}{if} (use\_varmix) \textcolor{keywordflow}{then}}
\DoxyCodeLine{218       \textcolor{comment}{!\$OMP parallel do default(shared) private(Kh\_loc,Rd\_dx)}}
\DoxyCodeLine{219       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{220         kh\_loc = cs\%KhTr}
\DoxyCodeLine{221         \textcolor{keywordflow}{if} (use\_eady) kh\_loc = kh\_loc + cs\%KhTr\_Slope\_Cff*varmix\%L2u(i,j)*varmix\%SN\_u(i,j)}
\DoxyCodeLine{222         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke\%Kh)) \&}
\DoxyCodeLine{223           kh\_loc = kh\_loc + meke\%KhTr\_fac*sqrt(meke\%Kh(i,j)*meke\%Kh(i+1,j))}
\DoxyCodeLine{224         \textcolor{keywordflow}{if} (cs\%KhTr\_max > 0.) kh\_loc = min(kh\_loc, cs\%KhTr\_max)}
\DoxyCodeLine{225         \textcolor{keywordflow}{if} (resoln\_scaled) \&}
\DoxyCodeLine{226           kh\_loc = kh\_loc * 0.5*(varmix\%Res\_fn\_h(i,j) + varmix\%Res\_fn\_h(i+1,j))}
\DoxyCodeLine{227         kh\_u(i,j) = max(kh\_loc, cs\%KhTr\_min)}
\DoxyCodeLine{228         \textcolor{keywordflow}{if} (cs\%KhTr\_passivity\_coeff>0.) \textcolor{keywordflow}{then} \textcolor{comment}{! Apply passivity}}
\DoxyCodeLine{229           rd\_dx=0.5*( varmix\%Rd\_dx\_h(i,j)+varmix\%Rd\_dx\_h(i+1,j) ) \textcolor{comment}{! Rd/dx at u-\/points}}
\DoxyCodeLine{230           kh\_loc = kh\_u(i,j)*max( cs\%KhTr\_passivity\_min, cs\%KhTr\_passivity\_coeff*rd\_dx )}
\DoxyCodeLine{231           \textcolor{keywordflow}{if} (cs\%KhTr\_max > 0.) kh\_loc = min(kh\_loc, cs\%KhTr\_max) \textcolor{comment}{! Re-\/apply max}}
\DoxyCodeLine{232           kh\_u(i,j) = max(kh\_loc, cs\%KhTr\_min) \textcolor{comment}{! Re-\/apply min}}
\DoxyCodeLine{233 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{234 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{235       \textcolor{comment}{!\$OMP parallel do default(shared) private(Kh\_loc,Rd\_dx)}}
\DoxyCodeLine{236       \textcolor{keywordflow}{do} j=js-\/1,je ;  \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{237         kh\_loc = cs\%KhTr}
\DoxyCodeLine{238         \textcolor{keywordflow}{if} (use\_eady) kh\_loc = kh\_loc + cs\%KhTr\_Slope\_Cff*varmix\%L2v(i,j)*varmix\%SN\_v(i,j)}
\DoxyCodeLine{239         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke\%Kh)) \&}
\DoxyCodeLine{240           kh\_loc = kh\_loc + meke\%KhTr\_fac*sqrt(meke\%Kh(i,j)*meke\%Kh(i,j+1))}
\DoxyCodeLine{241         \textcolor{keywordflow}{if} (cs\%KhTr\_max > 0.) kh\_loc = min(kh\_loc, cs\%KhTr\_max)}
\DoxyCodeLine{242         \textcolor{keywordflow}{if} (resoln\_scaled) \&}
\DoxyCodeLine{243           kh\_loc = kh\_loc * 0.5*(varmix\%Res\_fn\_h(i,j) + varmix\%Res\_fn\_h(i,j+1))}
\DoxyCodeLine{244         kh\_v(i,j) = max(kh\_loc, cs\%KhTr\_min)}
\DoxyCodeLine{245         \textcolor{keywordflow}{if} (cs\%KhTr\_passivity\_coeff>0.) \textcolor{keywordflow}{then} \textcolor{comment}{! Apply passivity}}
\DoxyCodeLine{246           rd\_dx = 0.5*( varmix\%Rd\_dx\_h(i,j)+varmix\%Rd\_dx\_h(i,j+1) ) \textcolor{comment}{! Rd/dx at v-\/points}}
\DoxyCodeLine{247           kh\_loc = kh\_v(i,j)*max( cs\%KhTr\_passivity\_min, cs\%KhTr\_passivity\_coeff*rd\_dx )}
\DoxyCodeLine{248           \textcolor{keywordflow}{if} (cs\%KhTr\_max > 0.) kh\_loc = min(kh\_loc, cs\%KhTr\_max) \textcolor{comment}{! Re-\/apply max}}
\DoxyCodeLine{249           kh\_v(i,j) = max(kh\_loc, cs\%KhTr\_min) \textcolor{comment}{! Re-\/apply min}}
\DoxyCodeLine{250 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{251 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{252 }
\DoxyCodeLine{253       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{254       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{255         khdt\_x(i,j) = dt*(kh\_u(i,j)*(g\%dy\_Cu(i,j)*g\%IdxCu(i,j)))}
\DoxyCodeLine{256 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{257       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{258       \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{259         khdt\_y(i,j) = dt*(kh\_v(i,j)*(g\%dx\_Cv(i,j)*g\%IdyCv(i,j)))}
\DoxyCodeLine{260 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{261     \textcolor{keywordflow}{elseif} (resoln\_scaled) \textcolor{keywordflow}{then}}
\DoxyCodeLine{262       \textcolor{comment}{!\$OMP parallel do default(shared) private(Res\_fn)}}
\DoxyCodeLine{263       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{264         res\_fn = 0.5 * (varmix\%Res\_fn\_h(i,j) + varmix\%Res\_fn\_h(i+1,j))}
\DoxyCodeLine{265         kh\_u(i,j) = max(cs\%KhTr * res\_fn, cs\%KhTr\_min)}
\DoxyCodeLine{266         khdt\_x(i,j) = dt*(cs\%KhTr*(g\%dy\_Cu(i,j)*g\%IdxCu(i,j))) * res\_fn}
\DoxyCodeLine{267 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{268       \textcolor{comment}{!\$OMP parallel do default(shared) private(Res\_fn)}}
\DoxyCodeLine{269       \textcolor{keywordflow}{do} j=js-\/1,je ;  \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{270         res\_fn = 0.5*(varmix\%Res\_fn\_h(i,j) + varmix\%Res\_fn\_h(i,j+1))}
\DoxyCodeLine{271         kh\_v(i,j) = max(cs\%KhTr * res\_fn, cs\%KhTr\_min)}
\DoxyCodeLine{272         khdt\_y(i,j) = dt*(cs\%KhTr*(g\%dx\_Cv(i,j)*g\%IdyCv(i,j))) * res\_fn}
\DoxyCodeLine{273 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{274     \textcolor{keywordflow}{else}  \textcolor{comment}{! Use a simple constant diffusivity.}}
\DoxyCodeLine{275       \textcolor{keywordflow}{if} (cs\%id\_KhTr\_u > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{276         \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{277         \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{278           kh\_u(i,j) = cs\%KhTr}
\DoxyCodeLine{279           khdt\_x(i,j) = dt*(cs\%KhTr*(g\%dy\_Cu(i,j)*g\%IdxCu(i,j)))}
\DoxyCodeLine{280 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{281       \textcolor{keywordflow}{else}}
\DoxyCodeLine{282         \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{283         \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{284           khdt\_x(i,j) = dt*(cs\%KhTr*(g\%dy\_Cu(i,j)*g\%IdxCu(i,j)))}
\DoxyCodeLine{285 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{286 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{287       \textcolor{keywordflow}{if} (cs\%id\_KhTr\_v > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{288         \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{289         \textcolor{keywordflow}{do} j=js-\/1,je ;  \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{290           kh\_v(i,j) = cs\%KhTr}
\DoxyCodeLine{291           khdt\_y(i,j) = dt*(cs\%KhTr*(g\%dx\_Cv(i,j)*g\%IdyCv(i,j)))}
\DoxyCodeLine{292 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{293       \textcolor{keywordflow}{else}}
\DoxyCodeLine{294         \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{295         \textcolor{keywordflow}{do} j=js-\/1,je ;  \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{296           khdt\_y(i,j) = dt*(cs\%KhTr*(g\%dx\_Cv(i,j)*g\%IdyCv(i,j)))}
\DoxyCodeLine{297 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{298 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{299 \textcolor{keywordflow}{    endif} \textcolor{comment}{! VarMix}}
\DoxyCodeLine{300 }
\DoxyCodeLine{301     \textcolor{keywordflow}{if} (cs\%max\_diff\_CFL > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{302       \textcolor{keywordflow}{if} ((cs\%id\_KhTr\_u > 0) .or. (cs\%id\_KhTr\_h > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{303         \textcolor{comment}{!\$OMP parallel do default(shared) private(khdt\_max)}}
\DoxyCodeLine{304         \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{305           khdt\_max = 0.125*cs\%max\_diff\_CFL * min(g\%areaT(i,j), g\%areaT(i+1,j))}
\DoxyCodeLine{306           \textcolor{keywordflow}{if} (khdt\_x(i,j) > khdt\_max) \textcolor{keywordflow}{then}}
\DoxyCodeLine{307             khdt\_x(i,j) = khdt\_max}
\DoxyCodeLine{308             \textcolor{keywordflow}{if} (dt*(g\%dy\_Cu(i,j)*g\%IdxCu(i,j)) > 0.0) \&}
\DoxyCodeLine{309               kh\_u(i,j) = khdt\_x(i,j) / (dt*(g\%dy\_Cu(i,j)*g\%IdxCu(i,j)))}
\DoxyCodeLine{310 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{311 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{312       \textcolor{keywordflow}{else}}
\DoxyCodeLine{313         \textcolor{comment}{!\$OMP parallel do default(shared) private(khdt\_max)}}
\DoxyCodeLine{314         \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{315           khdt\_max = 0.125*cs\%max\_diff\_CFL * min(g\%areaT(i,j), g\%areaT(i+1,j))}
\DoxyCodeLine{316           khdt\_x(i,j) = min(khdt\_x(i,j), khdt\_max)}
\DoxyCodeLine{317 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{318 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{319       \textcolor{keywordflow}{if} ((cs\%id\_KhTr\_v > 0) .or. (cs\%id\_KhTr\_h > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{320         \textcolor{comment}{!\$OMP parallel do default(shared) private(khdt\_max)}}
\DoxyCodeLine{321         \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{322           khdt\_max = 0.125*cs\%max\_diff\_CFL * min(g\%areaT(i,j), g\%areaT(i,j+1))}
\DoxyCodeLine{323           \textcolor{keywordflow}{if} (khdt\_y(i,j) > khdt\_max) \textcolor{keywordflow}{then}}
\DoxyCodeLine{324             khdt\_y(i,j) = khdt\_max}
\DoxyCodeLine{325             \textcolor{keywordflow}{if} (dt*(g\%dx\_Cv(i,j)*g\%IdyCv(i,j)) > 0.0) \&}
\DoxyCodeLine{326               kh\_v(i,j) = khdt\_y(i,j) / (dt*(g\%dx\_Cv(i,j)*g\%IdyCv(i,j)))}
\DoxyCodeLine{327 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{328 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{329       \textcolor{keywordflow}{else}}
\DoxyCodeLine{330         \textcolor{comment}{!\$OMP parallel do default(shared) private(khdt\_max)}}
\DoxyCodeLine{331         \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{332           khdt\_max = 0.125*cs\%max\_diff\_CFL * min(g\%areaT(i,j), g\%areaT(i,j+1))}
\DoxyCodeLine{333           khdt\_y(i,j) = min(khdt\_y(i,j), khdt\_max)}
\DoxyCodeLine{334 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{335 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{336 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{337 }
\DoxyCodeLine{338   \textcolor{keywordflow}{else} \textcolor{comment}{! .not. do\_online}}
\DoxyCodeLine{339     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{340     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{341       khdt\_x(i,j) = us\%m\_to\_L**2*read\_khdt\_x(i,j)}
\DoxyCodeLine{342 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{343     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{344     \textcolor{keywordflow}{do} j=js-\/1,je ;  \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{345       khdt\_y(i,j) = us\%m\_to\_L**2*read\_khdt\_y(i,j)}
\DoxyCodeLine{346 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{347     \textcolor{keyword}{call }pass\_vector(khdt\_x, khdt\_y, g\%Domain)}
\DoxyCodeLine{348 \textcolor{keywordflow}{  endif} \textcolor{comment}{! do\_online}}
\DoxyCodeLine{349 }
\DoxyCodeLine{350   \textcolor{keywordflow}{if} (cs\%check\_diffusive\_CFL) \textcolor{keywordflow}{then}}
\DoxyCodeLine{351     \textcolor{keywordflow}{if} (cs\%show\_call\_tree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"Checking diffusive CFL (tracer\_hordiff)"})}
\DoxyCodeLine{352     max\_cfl = 0.0}
\DoxyCodeLine{353     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{354       cfl(i,j) = 2.0*((khdt\_x(i-\/1,j) + khdt\_x(i,j)) + \&}
\DoxyCodeLine{355                       (khdt\_y(i,j-\/1) + khdt\_y(i,j))) * g\%IareaT(i,j)}
\DoxyCodeLine{356       \textcolor{keywordflow}{if} (max\_cfl < cfl(i,j)) max\_cfl = cfl(i,j)}
\DoxyCodeLine{357 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{358     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_sync)}
\DoxyCodeLine{359     \textcolor{keyword}{call }max\_across\_pes(max\_cfl)}
\DoxyCodeLine{360     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_sync)}
\DoxyCodeLine{361     num\_itts = max(1, ceiling(max\_cfl -\/ 4.0*epsilon(max\_cfl)))}
\DoxyCodeLine{362     i\_numitts = 1.0 / (real(num\_itts))}
\DoxyCodeLine{363     \textcolor{keywordflow}{if} (cs\%id\_CFL > 0) \textcolor{keyword}{call }post\_data(cs\%id\_CFL, cfl, cs\%diag, mask=g\%mask2dT)}
\DoxyCodeLine{364   \textcolor{keywordflow}{elseif} (cs\%max\_diff\_CFL > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{365     num\_itts = max(1, ceiling(cs\%max\_diff\_CFL -\/ 4.0*epsilon(cs\%max\_diff\_CFL)))}
\DoxyCodeLine{366     i\_numitts = 1.0 / (real(num\_itts))}
\DoxyCodeLine{367   \textcolor{keywordflow}{else}}
\DoxyCodeLine{368     num\_itts = 1 ; i\_numitts = 1.0}
\DoxyCodeLine{369 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{370 }
\DoxyCodeLine{371   \textcolor{keywordflow}{do} m=1,ntr}
\DoxyCodeLine{372     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(reg\%Tr(m)\%df\_x)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{373       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{374         reg\%Tr(m)\%df\_x(i,j,k) = 0.0}
\DoxyCodeLine{375 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{376 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{377     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(reg\%Tr(m)\%df\_y)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{378       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{379         reg\%Tr(m)\%df\_y(i,j,k) = 0.0}
\DoxyCodeLine{380 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{381 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{382     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(reg\%Tr(m)\%df2d\_x)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{383       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie ; reg\%Tr(m)\%df2d\_x(i,j) = 0.0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{384 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{385     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(reg\%Tr(m)\%df2d\_y)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{386       \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie ; reg\%Tr(m)\%df2d\_y(i,j) = 0.0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{387 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{388 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{389 }
\DoxyCodeLine{390   \textcolor{keywordflow}{if} (cs\%use\_lateral\_boundary\_diffusion) \textcolor{keywordflow}{then}}
\DoxyCodeLine{391 }
\DoxyCodeLine{392     \textcolor{keywordflow}{if} (cs\%show\_call\_tree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"Calling lateral boundary mixing (tracer\_hordiff)"})}
\DoxyCodeLine{393 }
\DoxyCodeLine{394     \textcolor{keyword}{call }do\_group\_pass(cs\%pass\_t, g\%Domain, clock=id\_clock\_pass)}
\DoxyCodeLine{395 }
\DoxyCodeLine{396     \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{397       coef\_y(i,j) = i\_numitts * khdt\_y(i,j)}
\DoxyCodeLine{398 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{399     \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{400       \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{401         coef\_x(i,j) = i\_numitts * khdt\_x(i,j)}
\DoxyCodeLine{402 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{403 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{404 }
\DoxyCodeLine{405     \textcolor{keywordflow}{do} itt=1,num\_itts}
\DoxyCodeLine{406       \textcolor{keywordflow}{if} (cs\%show\_call\_tree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"Calling lateral boundary diffusion (tracer\_hordiff)"},itt)}
\DoxyCodeLine{407       \textcolor{keywordflow}{if} (itt>1) \textcolor{keywordflow}{then} \textcolor{comment}{! Update halos for subsequent iterations}}
\DoxyCodeLine{408         \textcolor{keyword}{call }do\_group\_pass(cs\%pass\_t, g\%Domain, clock=id\_clock\_pass)}
\DoxyCodeLine{409 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{410       \textcolor{keyword}{call }lateral\_boundary\_diffusion(g, gv, us, h, coef\_x, coef\_y, i\_numitts*dt, reg, \&}
\DoxyCodeLine{411                                      cs\%lateral\_boundary\_diffusion\_CSp)}
\DoxyCodeLine{412 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! itt}}
\DoxyCodeLine{413 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{414 }
\DoxyCodeLine{415   \textcolor{keywordflow}{if} (cs\%use\_neutral\_diffusion) \textcolor{keywordflow}{then}}
\DoxyCodeLine{416 }
\DoxyCodeLine{417     \textcolor{keywordflow}{if} (cs\%show\_call\_tree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"Calling neutral diffusion coeffs (tracer\_hordiff)"})}
\DoxyCodeLine{418 }
\DoxyCodeLine{419     \textcolor{keyword}{call }do\_group\_pass(cs\%pass\_t, g\%Domain, clock=id\_clock\_pass)}
\DoxyCodeLine{420     \textcolor{comment}{! We are assuming that neutral surfaces do not evolve (much) as a result of multiple}}
\DoxyCodeLine{421     \textcolor{comment}{! lateral diffusion iterations. Otherwise the call to neutral\_diffusion\_calc\_coeffs()}}
\DoxyCodeLine{422     \textcolor{comment}{! would be inside the itt-\/loop. -\/AJA}}
\DoxyCodeLine{423 }
\DoxyCodeLine{424     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%p\_surf)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{425       \textcolor{keyword}{call }neutral\_diffusion\_calc\_coeffs(g, gv, us, h, tv\%T, tv\%S, cs\%neutral\_diffusion\_CSp, p\_surf=tv\%p\_surf)}
\DoxyCodeLine{426     \textcolor{keywordflow}{else}}
\DoxyCodeLine{427       \textcolor{keyword}{call }neutral\_diffusion\_calc\_coeffs(g, gv, us, h, tv\%T, tv\%S, cs\%neutral\_diffusion\_CSp)}
\DoxyCodeLine{428 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{429     \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{430       coef\_y(i,j) = i\_numitts * khdt\_y(i,j)}
\DoxyCodeLine{431 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{432     \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{433       \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{434         coef\_x(i,j) = i\_numitts * khdt\_x(i,j)}
\DoxyCodeLine{435 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{436 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{437 }
\DoxyCodeLine{438     \textcolor{keywordflow}{do} itt=1,num\_itts}
\DoxyCodeLine{439       \textcolor{keywordflow}{if} (cs\%show\_call\_tree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"Calling neutral diffusion (tracer\_hordiff)"},itt)}
\DoxyCodeLine{440       \textcolor{keywordflow}{if} (itt>1) \textcolor{keywordflow}{then} \textcolor{comment}{! Update halos for subsequent iterations}}
\DoxyCodeLine{441         \textcolor{keyword}{call }do\_group\_pass(cs\%pass\_t, g\%Domain, clock=id\_clock\_pass)}
\DoxyCodeLine{442         \textcolor{keywordflow}{if} (cs\%recalc\_neutral\_surf) \textcolor{keywordflow}{then}}
\DoxyCodeLine{443           \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%p\_surf)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{444             \textcolor{keyword}{call }neutral\_diffusion\_calc\_coeffs(g, gv, us, h, tv\%T, tv\%S, cs\%neutral\_diffusion\_CSp, p\_surf=tv\%p\_surf)}
\DoxyCodeLine{445           \textcolor{keywordflow}{else}}
\DoxyCodeLine{446             \textcolor{keyword}{call }neutral\_diffusion\_calc\_coeffs(g, gv, us, h, tv\%T, tv\%S, cs\%neutral\_diffusion\_CSp)}
\DoxyCodeLine{447 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{448 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{449 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{450       \textcolor{keyword}{call }neutral\_diffusion(g, gv,  h, coef\_x, coef\_y, i\_numitts*dt, reg, us, cs\%neutral\_diffusion\_CSp)}
\DoxyCodeLine{451 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! itt}}
\DoxyCodeLine{452 }
\DoxyCodeLine{453   \textcolor{keywordflow}{else}    \textcolor{comment}{! following if not using neutral diffusion, but instead along-\/surface diffusion}}
\DoxyCodeLine{454 }
\DoxyCodeLine{455     \textcolor{keywordflow}{if} (cs\%show\_call\_tree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"Calculating horizontal diffusion (tracer\_hordiff)"})}
\DoxyCodeLine{456     \textcolor{keywordflow}{do} itt=1,num\_itts}
\DoxyCodeLine{457       \textcolor{keyword}{call }do\_group\_pass(cs\%pass\_t, g\%Domain, clock=id\_clock\_pass)}
\DoxyCodeLine{458       \textcolor{comment}{!\$OMP parallel do default(shared) private(scale,Coef\_y,Coef\_x,Ihdxdy,dTr)}}
\DoxyCodeLine{459       \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{460         scale = i\_numitts}
\DoxyCodeLine{461         \textcolor{keywordflow}{if} (cs\%Diffuse\_ML\_interior) \textcolor{keywordflow}{then}}
\DoxyCodeLine{462           \textcolor{keywordflow}{if} (k<=gv\%nkml) \textcolor{keywordflow}{then}}
\DoxyCodeLine{463             \textcolor{keywordflow}{if} (cs\%ML\_KhTr\_scale <= 0.0) cycle}
\DoxyCodeLine{464             scale = i\_numitts * cs\%ML\_KhTr\_scale}
\DoxyCodeLine{465 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{466           \textcolor{keywordflow}{if} ((k>gv\%nkml) .and. (k<=gv\%nk\_rho\_varies)) cycle}
\DoxyCodeLine{467 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{468 }
\DoxyCodeLine{469         \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{470           coef\_y(i,j) = ((scale * khdt\_y(i,j))*2.0*(h(i,j,k)*h(i,j+1,k))) / \&}
\DoxyCodeLine{471                                                    (h(i,j,k)+h(i,j+1,k)+h\_neglect)}
\DoxyCodeLine{472 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{473 }
\DoxyCodeLine{474         \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{475           \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{476             coef\_x(i,j) = ((scale * khdt\_x(i,j))*2.0*(h(i,j,k)*h(i+1,j,k))) / \&}
\DoxyCodeLine{477                                                      (h(i,j,k)+h(i+1,j,k)+h\_neglect)}
\DoxyCodeLine{478 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{479 }
\DoxyCodeLine{480           \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{481             ihdxdy(i,j) = g\%IareaT(i,j) / (h(i,j,k)+h\_neglect)}
\DoxyCodeLine{482 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{483 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{484 }
\DoxyCodeLine{485         \textcolor{keywordflow}{do} m=1,ntr}
\DoxyCodeLine{486           \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{487             dtr(i,j) = ihdxdy(i,j) * \&}
\DoxyCodeLine{488               ((coef\_x(i-\/1,j) * (reg\%Tr(m)\%t(i-\/1,j,k) -\/ reg\%Tr(m)\%t(i,j,k)) -\/ \&}
\DoxyCodeLine{489                 coef\_x(i,j) * (reg\%Tr(m)\%t(i,j,k) -\/ reg\%Tr(m)\%t(i+1,j,k))) + \&}
\DoxyCodeLine{490                (coef\_y(i,j-\/1) * (reg\%Tr(m)\%t(i,j-\/1,k) -\/ reg\%Tr(m)\%t(i,j,k)) -\/ \&}
\DoxyCodeLine{491                 coef\_y(i,j) * (reg\%Tr(m)\%t(i,j,k) -\/ reg\%Tr(m)\%t(i,j+1,k))))}
\DoxyCodeLine{492 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{493           \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(reg\%Tr(m)\%df\_x)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=g\%IscB,g\%IecB}
\DoxyCodeLine{494             reg\%Tr(m)\%df\_x(i,j,k) = reg\%Tr(m)\%df\_x(i,j,k) + coef\_x(i,j) \&}
\DoxyCodeLine{495                 * (reg\%Tr(m)\%t(i,j,k) -\/ reg\%Tr(m)\%t(i+1,j,k)) * idt}
\DoxyCodeLine{496 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{497           \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(reg\%Tr(m)\%df\_y)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=g\%JscB,g\%JecB ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{498             reg\%Tr(m)\%df\_y(i,j,k) = reg\%Tr(m)\%df\_y(i,j,k) + coef\_y(i,j) \&}
\DoxyCodeLine{499                 * (reg\%Tr(m)\%t(i,j,k) -\/ reg\%Tr(m)\%t(i,j+1,k)) * idt}
\DoxyCodeLine{500 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{501           \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(reg\%Tr(m)\%df2d\_x)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=g\%IscB,g\%IecB}
\DoxyCodeLine{502             reg\%Tr(m)\%df2d\_x(i,j) = reg\%Tr(m)\%df2d\_x(i,j) + coef\_x(i,j) \&}
\DoxyCodeLine{503                 * (reg\%Tr(m)\%t(i,j,k) -\/ reg\%Tr(m)\%t(i+1,j,k)) * idt}
\DoxyCodeLine{504 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{505           \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(reg\%Tr(m)\%df2d\_y)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} j=g\%JscB,g\%JecB ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{506             reg\%Tr(m)\%df2d\_y(i,j) = reg\%Tr(m)\%df2d\_y(i,j) + coef\_y(i,j) \&}
\DoxyCodeLine{507                 * (reg\%Tr(m)\%t(i,j,k) -\/ reg\%Tr(m)\%t(i,j+1,k)) * idt}
\DoxyCodeLine{508 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{509           \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{510             reg\%Tr(m)\%t(i,j,k) = reg\%Tr(m)\%t(i,j,k) + dtr(i,j)}
\DoxyCodeLine{511 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{512 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{513 }
\DoxyCodeLine{514 \textcolor{keywordflow}{      enddo} \textcolor{comment}{! End of k loop.}}
\DoxyCodeLine{515 }
\DoxyCodeLine{516 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! End of "while" loop.}}
\DoxyCodeLine{517 }
\DoxyCodeLine{518 \textcolor{keywordflow}{  endif}   \textcolor{comment}{! endif for CS\%use\_neutral\_diffusion}}
\DoxyCodeLine{519   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_diffuse)}
\DoxyCodeLine{520 }
\DoxyCodeLine{521 }
\DoxyCodeLine{522   \textcolor{keywordflow}{if} (cs\%Diffuse\_ML\_interior) \textcolor{keywordflow}{then}}
\DoxyCodeLine{523     \textcolor{keywordflow}{if} (cs\%show\_call\_tree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"Calling epipycnal\_ML\_diff (tracer\_hordiff)"})}
\DoxyCodeLine{524     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keyword}{call }mom\_tracer\_chksum(\textcolor{stringliteral}{"Before epipycnal diff "}, reg\%Tr, ntr, g)}
\DoxyCodeLine{525 }
\DoxyCodeLine{526     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_epimix)}
\DoxyCodeLine{527     \textcolor{keyword}{call }tracer\_epipycnal\_ml\_diff(h, dt, reg\%Tr, ntr, khdt\_x, khdt\_y, g, gv, us, \&}
\DoxyCodeLine{528                                   cs, tv, num\_itts)}
\DoxyCodeLine{529     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_epimix)}
\DoxyCodeLine{530 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{531 }
\DoxyCodeLine{532   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keyword}{call }mom\_tracer\_chksum(\textcolor{stringliteral}{"After tracer diffusion "}, reg\%Tr, ntr, g)}
\DoxyCodeLine{533 }
\DoxyCodeLine{534   \textcolor{comment}{! post diagnostics for 2d tracer diffusivity}}
\DoxyCodeLine{535   \textcolor{keywordflow}{if} (cs\%id\_KhTr\_u > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{536     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{537       kh\_u(i,j) = g\%mask2dCu(i,j)*kh\_u(i,j)}
\DoxyCodeLine{538 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{539     \textcolor{keyword}{call }post\_data(cs\%id\_KhTr\_u, kh\_u, cs\%diag, mask=g\%mask2dCu)}
\DoxyCodeLine{540 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{541   \textcolor{keywordflow}{if} (cs\%id\_KhTr\_v > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{542     \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{543       kh\_v(i,j) = g\%mask2dCv(i,j)*kh\_v(i,j)}
\DoxyCodeLine{544 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{545     \textcolor{keyword}{call }post\_data(cs\%id\_KhTr\_v, kh\_v, cs\%diag, mask=g\%mask2dCv)}
\DoxyCodeLine{546 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{547   \textcolor{keywordflow}{if} (cs\%id\_KhTr\_h > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{548     kh\_h(:,:) = 0.0}
\DoxyCodeLine{549     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-\/1,ie}
\DoxyCodeLine{550       kh\_u(i,j) = g\%mask2dCu(i,j)*kh\_u(i,j)}
\DoxyCodeLine{551 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{552     \textcolor{keywordflow}{do} j=js-\/1,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{553       kh\_v(i,j) = g\%mask2dCv(i,j)*kh\_v(i,j)}
\DoxyCodeLine{554 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{555     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{556       normalize = 1.0 / ((g\%mask2dCu(i-\/1,j)+g\%mask2dCu(i,j)) + \&}
\DoxyCodeLine{557                   (g\%mask2dCv(i,j-\/1)+g\%mask2dCv(i,j)) + gv\%H\_subroundoff)}
\DoxyCodeLine{558       kh\_h(i,j) = normalize*g\%mask2dT(i,j)*((kh\_u(i-\/1,j)+kh\_u(i,j)) + \&}
\DoxyCodeLine{559                                             (kh\_v(i,j-\/1)+kh\_v(i,j)))}
\DoxyCodeLine{560 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{561     \textcolor{keyword}{call }post\_data(cs\%id\_KhTr\_h, kh\_h, cs\%diag, mask=g\%mask2dT)}
\DoxyCodeLine{562 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{563 }
\DoxyCodeLine{564 }
\DoxyCodeLine{565   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{566     \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"After tracer diffusion khdt\_[xy]"}, khdt\_x, khdt\_y, \&}
\DoxyCodeLine{567                   g\%HI, haloshift=0, symmetric=.true., scale=us\%L\_to\_m**2, \&}
\DoxyCodeLine{568                   scalar\_pair=.true.)}
\DoxyCodeLine{569     \textcolor{keywordflow}{if} (cs\%use\_neutral\_diffusion) \textcolor{keywordflow}{then}}
\DoxyCodeLine{570       \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"After tracer diffusion Coef\_[xy]"}, coef\_x, coef\_y, \&}
\DoxyCodeLine{571                     g\%HI, haloshift=0, symmetric=.true., scale=us\%L\_to\_m**2, \&}
\DoxyCodeLine{572                     scalar\_pair=.true.)}
\DoxyCodeLine{573 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{574 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{575 }
\DoxyCodeLine{576   \textcolor{keywordflow}{if} (cs\%id\_khdt\_x > 0) \textcolor{keyword}{call }post\_data(cs\%id\_khdt\_x, khdt\_x, cs\%diag)}
\DoxyCodeLine{577   \textcolor{keywordflow}{if} (cs\%id\_khdt\_y > 0) \textcolor{keyword}{call }post\_data(cs\%id\_khdt\_y, khdt\_y, cs\%diag)}
\DoxyCodeLine{578 }
\DoxyCodeLine{579   \textcolor{keywordflow}{if} (cs\%show\_call\_tree) \textcolor{keyword}{call }calltree\_leave(\textcolor{stringliteral}{"tracer\_hordiff()"})}
\DoxyCodeLine{580 }

\end{DoxyCode}
