\hypertarget{namespacemom__diabatic__driver}{}\doxysection{mom\+\_\+diabatic\+\_\+driver Module Reference}
\label{namespacemom__diabatic__driver}\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}}


\doxysubsection{Detailed Description}
This routine drives the diabatic/dianeutral physics for M\+OM. 

By Robert Hallberg, Alistair Adcroft, and Stephen Griffies

This program contains the subroutine that, along with the subroutines that it calls, implements diapycnal mass and momentum fluxes and a bulk mixed layer. The diapycnal diffusion can be used without the bulk mixed layer.\hypertarget{namespacemom__diabatic__driver_section_diabatic}{}\doxysubsection{Outline of M\+O\+M diabatic}\label{namespacemom__diabatic__driver_section_diabatic}

\begin{DoxyItemize}
\item diabatic first determines the (diffusive) diapycnal mass fluxes based on the convergence of the buoyancy fluxes within each layer.
\item The dual-\/stream entrainment scheme of Mac\+Dougall and Dewar (J\+PO, 1997) is used for combined diapycnal advection and diffusion, calculated implicitly and potentially with the Richardson number dependent mixing, as described by Hallberg (M\+WR, 2000).
\item Diapycnal advection is the residual of diapycnal diffusion, so the fully implicit upwind differencing scheme that is used is entirely appropriate.
\item The downward buoyancy flux in each layer is determined from an implicit calculation based on the previously calculated flux of the layer above and an estimated flux in the layer below. This flux is subject to the following conditions\+: (1) the flux in the top and bottom layers are set by the boundary conditions, and (2) no layer may be driven below an Angstrom thick-\/ ness. If there is a bulk mixed layer, the buffer layer is treated as a fixed density layer with vanishingly small diffusivity.

diabatic takes 5 arguments\+: the two velocities (u and v), the thicknesses (h), a structure containing the forcing fields, and the length of time over which to act (dt). The velocities and thickness are taken as inputs and modified within the subroutine. There is no limit on the time step. 
\end{DoxyItemize}\doxysubsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}
\begin{DoxyCompactList}\small\item\em Control structure for this module. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \mbox{\hyperlink{namespacemom__diabatic__driver_ada12cae1e63c6aa56775f1a235b7db95}{diabatic}} (u, v, h, tv, Hml, fluxes, visc, A\+Dp, C\+Dp, dt, Time\+\_\+end, G, GV, US, CS, O\+BC, W\+A\+V\+ES)
\begin{DoxyCompactList}\small\item\em This subroutine imposes the diapycnal mass fluxes and the accompanying diapycnal advection of momentum and tracers. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__diabatic__driver_acf7394d08f436dd9575b568d1f18e18a}{diabatic\+\_\+ale\+\_\+legacy}} (u, v, h, tv, Hml, fluxes, visc, A\+Dp, C\+Dp, dt, Time\+\_\+end, G, GV, US, CS, Waves)
\begin{DoxyCompactList}\small\item\em Applies diabatic forcing and diapycnal mixing of temperature, salinity and other tracers for use with an A\+LE algorithm. This version uses an older set of algorithms compared with diabatic\+\_\+\+A\+LE. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__diabatic__driver_ae57c48925de75712384e859a851c8c40}{diabatic\+\_\+ale}} (u, v, h, tv, Hml, fluxes, visc, A\+Dp, C\+Dp, dt, Time\+\_\+end, G, GV, US, CS, Waves)
\begin{DoxyCompactList}\small\item\em This subroutine imposes the diapycnal mass fluxes and the accompanying diapycnal advection of momentum and tracers. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__diabatic__driver_a71d8d849db16be4b87c2650b49f01c82}{layered\+\_\+diabatic}} (u, v, h, tv, Hml, fluxes, visc, A\+Dp, C\+Dp, dt, Time\+\_\+end, G, GV, US, CS, W\+A\+V\+ES)
\begin{DoxyCompactList}\small\item\em Imposes the diapycnal mass fluxes and the accompanying diapycnal advection of momentum and tracers using the original M\+O\+M6 algorithms. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__diabatic__driver_a49144b7b0c0d44fde6cd835f1001dde5}{extract\+\_\+diabatic\+\_\+member}} (CS, opacity\+\_\+\+C\+Sp, optics\+\_\+\+C\+Sp, evap\+\_\+\+C\+F\+L\+\_\+limit, minimum\+\_\+forcing\+\_\+depth, K\+P\+P\+\_\+\+C\+Sp, energetic\+\_\+\+P\+B\+L\+\_\+\+C\+Sp, diabatic\+\_\+aux\+\_\+\+C\+Sp, diabatic\+\_\+halo)
\begin{DoxyCompactList}\small\item\em Returns pointers or values of members within the diabatic\+\_\+\+CS type. For extensibility, each returned argument is an optional argument. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__diabatic__driver_aef60aa7bfe62f65408c7005b6cb613e8}{adiabatic}} (h, tv, fluxes, dt, G, GV, US, CS)
\begin{DoxyCompactList}\small\item\em Routine called for adiabatic physics. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__diabatic__driver_af0aa2526c1824517fe5cdae65b48abd9}{diagnose\+\_\+diabatic\+\_\+diff\+\_\+tendency}} (tv, h, temp\+\_\+old, saln\+\_\+old, dt, G, GV, US, CS)
\begin{DoxyCompactList}\small\item\em This routine diagnoses tendencies from application of diabatic diffusion using A\+LE algorithm. Note that layer thickness is not altered by diabatic diffusion. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__diabatic__driver_a9a32fd213024239e5818f0bd88764761}{diagnose\+\_\+boundary\+\_\+forcing\+\_\+tendency}} (tv, h, temp\+\_\+old, saln\+\_\+old, h\+\_\+old, dt, G, GV, US, CS)
\begin{DoxyCompactList}\small\item\em This routine diagnoses tendencies from application of boundary fluxes. These impacts are generally 3d, in particular for penetrative shortwave. Other fluxes contribute 3d in cases when the layers vanish or are very thin, in which case we distribute the flux into k $>$ 1 layers. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__diabatic__driver_af79993410fea86ff69ef417396798bac}{diagnose\+\_\+frazil\+\_\+tendency}} (tv, h, temp\+\_\+old, dt, G, GV, US, CS)
\begin{DoxyCompactList}\small\item\em This routine diagnoses tendencies for temperature and heat from frazil formation. This routine is called twice from within subroutine diabatic; at start and at end of the diabatic processes. The impacts from frazil are generally a function of depth. Hence, when checking heat budget, be sure to remove H\+F\+S\+I\+F\+R\+A\+Z\+IL from H\+F\+DS in k=1. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__diabatic__driver_aebe83a2ee710958a79623d1ccdde56d2}{adiabatic\+\_\+driver\+\_\+init}} (Time, G, param\+\_\+file, diag, CS, tracer\+\_\+flow\+\_\+\+C\+Sp)
\begin{DoxyCompactList}\small\item\em A simplified version of diabatic\+\_\+driver\+\_\+init that will allow tracer column functions to be called without allowing any of the diabatic processes to be used. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__diabatic__driver_a51d273bae7e5d2217fa5498620532888}{diabatic\+\_\+driver\+\_\+init}} (Time, G, GV, US, param\+\_\+file, use\+A\+L\+Ealgorithm, diag, A\+Dp, C\+Dp, CS, tracer\+\_\+flow\+\_\+\+C\+Sp, sponge\+\_\+\+C\+Sp, A\+L\+E\+\_\+sponge\+\_\+\+C\+Sp)
\begin{DoxyCompactList}\small\item\em This routine initializes the diabatic driver module. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__diabatic__driver_a6eac8317c3b569e414fb5a6678afc598}{diabatic\+\_\+driver\+\_\+end}} (CS)
\begin{DoxyCompactList}\small\item\em Routine to close the diabatic driver module. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Variables}
\textbf{ }\par
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{namespacemom__diabatic__driver_ac0824d4ab23eb0cff642acd5b40f38cf}\label{namespacemom__diabatic__driver_ac0824d4ab23eb0cff642acd5b40f38cf}} 
integer \mbox{\hyperlink{namespacemom__diabatic__driver_ac0824d4ab23eb0cff642acd5b40f38cf}{id\+\_\+clock\+\_\+entrain}}
\begin{DoxyCompactList}\small\item\em clock ids \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__diabatic__driver_a64b9dbea706cd013f6541f4240fb1133}\label{namespacemom__diabatic__driver_a64b9dbea706cd013f6541f4240fb1133}} 
integer \mbox{\hyperlink{namespacemom__diabatic__driver_a64b9dbea706cd013f6541f4240fb1133}{id\+\_\+clock\+\_\+mixedlayer}}
\begin{DoxyCompactList}\small\item\em clock ids \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__diabatic__driver_a34d2b3a2764fe2337a4701bc63ba2cb9}\label{namespacemom__diabatic__driver_a34d2b3a2764fe2337a4701bc63ba2cb9}} 
integer \mbox{\hyperlink{namespacemom__diabatic__driver_a34d2b3a2764fe2337a4701bc63ba2cb9}{id\+\_\+clock\+\_\+set\+\_\+diffusivity}}
\begin{DoxyCompactList}\small\item\em clock ids \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__diabatic__driver_aee5d35fe0a4026b135359b9f765f5fd3}\label{namespacemom__diabatic__driver_aee5d35fe0a4026b135359b9f765f5fd3}} 
integer \mbox{\hyperlink{namespacemom__diabatic__driver_aee5d35fe0a4026b135359b9f765f5fd3}{id\+\_\+clock\+\_\+tracers}}
\begin{DoxyCompactList}\small\item\em clock ids \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__diabatic__driver_a0e5ed4eea2273ac3f3b73bd20235801d}\label{namespacemom__diabatic__driver_a0e5ed4eea2273ac3f3b73bd20235801d}} 
integer \mbox{\hyperlink{namespacemom__diabatic__driver_a0e5ed4eea2273ac3f3b73bd20235801d}{id\+\_\+clock\+\_\+tridiag}}
\begin{DoxyCompactList}\small\item\em clock ids \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__diabatic__driver_a0e4dcc94de5ad908253905f1d0f9c1c8}\label{namespacemom__diabatic__driver_a0e4dcc94de5ad908253905f1d0f9c1c8}} 
integer \mbox{\hyperlink{namespacemom__diabatic__driver_a0e4dcc94de5ad908253905f1d0f9c1c8}{id\+\_\+clock\+\_\+pass}}
\begin{DoxyCompactList}\small\item\em clock ids \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__diabatic__driver_aec621b79c9fbf1cda2b82ce42eb25339}\label{namespacemom__diabatic__driver_aec621b79c9fbf1cda2b82ce42eb25339}} 
integer \mbox{\hyperlink{namespacemom__diabatic__driver_aec621b79c9fbf1cda2b82ce42eb25339}{id\+\_\+clock\+\_\+sponge}}
\begin{DoxyCompactList}\small\item\em clock ids \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__diabatic__driver_a24bf3b09c5e211c2bb42668cd946dd5a}\label{namespacemom__diabatic__driver_a24bf3b09c5e211c2bb42668cd946dd5a}} 
integer \mbox{\hyperlink{namespacemom__diabatic__driver_a24bf3b09c5e211c2bb42668cd946dd5a}{id\+\_\+clock\+\_\+geothermal}}
\begin{DoxyCompactList}\small\item\em clock ids \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__diabatic__driver_a95ee7992059c89735b7f6ef437eee13f}\label{namespacemom__diabatic__driver_a95ee7992059c89735b7f6ef437eee13f}} 
integer \mbox{\hyperlink{namespacemom__diabatic__driver_a95ee7992059c89735b7f6ef437eee13f}{id\+\_\+clock\+\_\+differential\+\_\+diff}}
\begin{DoxyCompactList}\small\item\em clock ids \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__diabatic__driver_a812f6ee12f2449c3ce656c7f14aa882e}\label{namespacemom__diabatic__driver_a812f6ee12f2449c3ce656c7f14aa882e}} 
integer \mbox{\hyperlink{namespacemom__diabatic__driver_a812f6ee12f2449c3ce656c7f14aa882e}{id\+\_\+clock\+\_\+remap}}
\begin{DoxyCompactList}\small\item\em clock ids \end{DoxyCompactList}\item 
\mbox{\Hypertarget{namespacemom__diabatic__driver_a6b41dac58aa5c518f1affcf4423a7fb4}\label{namespacemom__diabatic__driver_a6b41dac58aa5c518f1affcf4423a7fb4}} 
integer \mbox{\hyperlink{namespacemom__diabatic__driver_a6b41dac58aa5c518f1affcf4423a7fb4}{id\+\_\+clock\+\_\+kpp}}
\begin{DoxyCompactList}\small\item\em clock ids \end{DoxyCompactList}\end{DoxyCompactItemize}



\doxysubsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__diabatic__driver_aef60aa7bfe62f65408c7005b6cb613e8}\label{namespacemom__diabatic__driver_aef60aa7bfe62f65408c7005b6cb613e8}} 
\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}!adiabatic@{adiabatic}}
\index{adiabatic@{adiabatic}!mom\_diabatic\_driver@{mom\_diabatic\_driver}}
\doxysubsubsection{\texorpdfstring{adiabatic()}{adiabatic()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+diabatic\+\_\+driver\+::adiabatic (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(inout)}]{h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(inout)}]{tv,  }\item[{type(forcing), intent(inout)}]{fluxes,  }\item[{real, intent(in)}]{dt,  }\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__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Routine called for adiabatic physics. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in,out}}  & {\em g} & ocean grid structure \\
\hline
\mbox{\texttt{ in,out}}  & {\em h} & thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em tv} & points to thermodynamic fields \\
\hline
\mbox{\texttt{ in,out}}  & {\em fluxes} & boundary fluxes \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & time step \mbox{[}T $\sim$$>$ s\mbox{]} \\
\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
\end{DoxyParams}


Definition at line 2849 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{2849   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{      !< ocean grid structure}}
\DoxyCodeLine{2850 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{2851                            \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{      !< thickness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{2852   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),   \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{     !< points to thermodynamic fields}}
\DoxyCodeLine{2853   \textcolor{keywordtype}{type}(forcing),           \textcolor{keywordtype}{intent(inout)} :: fluxes\textcolor{comment}{ !< boundary fluxes}}
\DoxyCodeLine{2854 \textcolor{keywordtype}{  real},                    \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{     !< time step [T ~> s]}}
\DoxyCodeLine{2855   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{     !< ocean vertical grid structure}}
\DoxyCodeLine{2856   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{     !< A dimensional unit scaling type}}
\DoxyCodeLine{2857   \textcolor{keywordtype}{type}(diabatic\_CS),       \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{     !< module control structure}}
\DoxyCodeLine{2858 }
\DoxyCodeLine{2859 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))} :: zeros  \textcolor{comment}{! An array of zeros.}}
\DoxyCodeLine{2860 }
\DoxyCodeLine{2861   zeros(:,:,:) = 0.0}
\DoxyCodeLine{2862 }
\DoxyCodeLine{2863   \textcolor{keyword}{call }call\_tracer\_column\_fns(h, h, zeros, zeros, fluxes, zeros(:,:,1), dt, g, gv, us, tv, \&}
\DoxyCodeLine{2864                               cs\%optics, cs\%tracer\_flow\_CSp, cs\%debug)}
\DoxyCodeLine{2865 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diabatic__driver_aebe83a2ee710958a79623d1ccdde56d2}\label{namespacemom__diabatic__driver_aebe83a2ee710958a79623d1ccdde56d2}} 
\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}!adiabatic\_driver\_init@{adiabatic\_driver\_init}}
\index{adiabatic\_driver\_init@{adiabatic\_driver\_init}!mom\_diabatic\_driver@{mom\_diabatic\_driver}}
\doxysubsubsection{\texorpdfstring{adiabatic\_driver\_init()}{adiabatic\_driver\_init()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+diabatic\+\_\+driver\+::adiabatic\+\_\+driver\+\_\+init (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), intent(in)}]{Time,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file,  }\item[{type(diag\+\_\+ctrl), intent(inout), target}]{diag,  }\item[{type(\mbox{\hyperlink{structmom__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), pointer}]{CS,  }\item[{type(tracer\+\_\+flow\+\_\+control\+\_\+cs), pointer}]{tracer\+\_\+flow\+\_\+\+C\+Sp }\end{DoxyParamCaption})}



A simplified version of diabatic\+\_\+driver\+\_\+init that will allow tracer column functions to be called without allowing any of the diabatic processes to be used. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em time} & current model time \\
\hline
\mbox{\texttt{ in}}  & {\em g} & model grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em param\+\_\+file} & the file to parse for parameter values \\
\hline
\mbox{\texttt{ in,out}}  & {\em diag} & regulates diagnostic output \\
\hline
 & {\em cs} & module control structure \\
\hline
 & {\em tracer\+\_\+flow\+\_\+csp} & pointer to control structure of the tracer flow control module \\
\hline
\end{DoxyParams}


Definition at line 3115 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{3115   \textcolor{keywordtype}{type}(time\_type),         \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{             !< current model time}}
\DoxyCodeLine{3116   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{                !< model grid structure}}
\DoxyCodeLine{3117   \textcolor{keywordtype}{type}(param\_file\_type),   \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{       !< the file to parse for parameter values}}
\DoxyCodeLine{3118   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{             !< regulates diagnostic output}}
\DoxyCodeLine{3119   \textcolor{keywordtype}{type}(diabatic\_CS),       \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{               !< module control structure}}
\DoxyCodeLine{3120   \textcolor{keywordtype}{type}(tracer\_flow\_control\_CS), \textcolor{keywordtype}{pointer}  :: tracer\_flow\_CSp\textcolor{comment}{  !< pointer to control structure of the}}
\DoxyCodeLine{3121 \textcolor{comment}{                                                             !! tracer flow control module}}
\DoxyCodeLine{3122 }
\DoxyCodeLine{3123 \textcolor{comment}{! This "include" declares and sets the variable "version".}}
\DoxyCodeLine{3124 \textcolor{preprocessor}{\#include "version\_variable.h"}}
\DoxyCodeLine{3125 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_diabatic\_driver"} \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{3126 }
\DoxyCodeLine{3127   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3128     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"adiabatic\_driver\_init called with an "}// \&}
\DoxyCodeLine{3129                             \textcolor{stringliteral}{"associated control structure."})}
\DoxyCodeLine{3130     \textcolor{keywordflow}{return}}
\DoxyCodeLine{3131   \textcolor{keywordflow}{else} ; \textcolor{keyword}{allocate}(cs) ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{3132 }
\DoxyCodeLine{3133   cs\%diag => diag}
\DoxyCodeLine{3134   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tracer\_flow\_csp)) cs\%tracer\_flow\_CSp => tracer\_flow\_csp}
\DoxyCodeLine{3135 }
\DoxyCodeLine{3136 \textcolor{comment}{! Set default, read and log parameters}}
\DoxyCodeLine{3137   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \&}
\DoxyCodeLine{3138                    \textcolor{stringliteral}{"The following parameters are used for diabatic processes."})}
\DoxyCodeLine{3139 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diabatic__driver_ada12cae1e63c6aa56775f1a235b7db95}\label{namespacemom__diabatic__driver_ada12cae1e63c6aa56775f1a235b7db95}} 
\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}!diabatic@{diabatic}}
\index{diabatic@{diabatic}!mom\_diabatic\_driver@{mom\_diabatic\_driver}}
\doxysubsubsection{\texorpdfstring{diabatic()}{diabatic()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+diabatic\+\_\+driver\+::diabatic (\begin{DoxyParamCaption}\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed, g \%ke), intent(inout)}]{u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb, g \%ke), intent(inout)}]{v,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(inout)}]{h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(inout)}]{tv,  }\item[{real, dimension(\+:,\+:), pointer}]{Hml,  }\item[{type(forcing), intent(inout)}]{fluxes,  }\item[{type(vertvisc\+\_\+type), intent(inout)}]{visc,  }\item[{type(accel\+\_\+diag\+\_\+ptrs), intent(inout)}]{A\+Dp,  }\item[{type(cont\+\_\+diag\+\_\+ptrs), intent(inout)}]{C\+Dp,  }\item[{real, intent(in)}]{dt,  }\item[{type(time\+\_\+type), intent(in)}]{Time\+\_\+end,  }\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__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), pointer}]{CS,  }\item[{type(ocean\+\_\+obc\+\_\+type), optional, pointer}]{O\+BC,  }\item[{type(wave\+\_\+parameters\+\_\+cs), optional, pointer}]{W\+A\+V\+ES }\end{DoxyParamCaption})}



This subroutine imposes the diapycnal mass fluxes and the accompanying diapycnal advection of momentum and tracers. 


\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,out}}  & {\em u} & zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em v} & meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em h} & thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em tv} & points to thermodynamic fields unused have N\+U\+LL ptrs \\
\hline
 & {\em hml} & Active mixed layer depth \mbox{[}Z $\sim$$>$ m\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em fluxes} & points to forcing fields unused fields have N\+U\+LL ptrs \\
\hline
\mbox{\texttt{ in,out}}  & {\em visc} & vertical viscosities, B\+BL properies, and \\
\hline
\mbox{\texttt{ in,out}}  & {\em adp} & related points to accelerations in momentum equations, to enable the later derived diagnostics, like energy budgets \\
\hline
\mbox{\texttt{ in,out}}  & {\em cdp} & points to terms in continuity equations \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & time increment \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em time\+\_\+end} & Time at the end of the interval \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & module control structure \\
\hline
 & {\em obc} & Open boundaries control structure. \\
\hline
 & {\em waves} & Surface gravity waves \\
\hline
\end{DoxyParams}


Definition at line 263 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{263   \textcolor{keywordtype}{type}(ocean\_grid\_type),                     \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{         !< ocean grid structure}}
\DoxyCodeLine{264   \textcolor{keywordtype}{type}(verticalGrid\_type),                   \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{        !< ocean vertical grid structure}}
\DoxyCodeLine{265 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(inout)} :: u\textcolor{comment}{         !< zonal velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{266 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(inout)} :: v\textcolor{comment}{         !< meridional velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{267 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{         !< thickness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{268   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),                     \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{        !< points to thermodynamic fields}}
\DoxyCodeLine{269 \textcolor{comment}{                                                                        !! unused have NULL ptrs}}
\DoxyCodeLine{270 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(:,:)},                      \textcolor{keywordtype}{pointer}       :: Hml\textcolor{comment}{       !< Active mixed layer depth [Z ~> m]}}
\DoxyCodeLine{271   \textcolor{keywordtype}{type}(forcing),                             \textcolor{keywordtype}{intent(inout)} :: fluxes\textcolor{comment}{    !< points to forcing fields}}
\DoxyCodeLine{272 \textcolor{comment}{                                                                        !! unused fields have NULL ptrs}}
\DoxyCodeLine{273   \textcolor{keywordtype}{type}(vertvisc\_type),                       \textcolor{keywordtype}{intent(inout)} :: visc\textcolor{comment}{      !< vertical viscosities, BBL properies, and}}
\DoxyCodeLine{274   \textcolor{keywordtype}{type}(accel\_diag\_ptrs),                     \textcolor{keywordtype}{intent(inout)} :: ADp\textcolor{comment}{       !< related points to accelerations in momentum}}
\DoxyCodeLine{275 \textcolor{comment}{                                                                        !! equations, to enable the later derived}}
\DoxyCodeLine{276 \textcolor{comment}{                                                                        !! diagnostics, like energy budgets}}
\DoxyCodeLine{277   \textcolor{keywordtype}{type}(cont\_diag\_ptrs),                      \textcolor{keywordtype}{intent(inout)} :: CDp\textcolor{comment}{       !< points to terms in continuity equations}}
\DoxyCodeLine{278 \textcolor{keywordtype}{  real},                                      \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{        !< time increment [T ~> s]}}
\DoxyCodeLine{279   \textcolor{keywordtype}{type}(time\_type),                           \textcolor{keywordtype}{intent(in)}    :: Time\_end\textcolor{comment}{  !< Time at the end of the interval}}
\DoxyCodeLine{280   \textcolor{keywordtype}{type}(unit\_scale\_type),                     \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{        !< A dimensional unit scaling type}}
\DoxyCodeLine{281   \textcolor{keywordtype}{type}(diabatic\_CS),                         \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{        !< module control structure}}
\DoxyCodeLine{282   \textcolor{keywordtype}{type}(ocean\_OBC\_type),            \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}       :: OBC\textcolor{comment}{       !< Open boundaries control structure.}}
\DoxyCodeLine{283   \textcolor{keywordtype}{type}(Wave\_parameters\_CS),        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}       :: Waves\textcolor{comment}{     !< Surface gravity waves}}
\DoxyCodeLine{284 }
\DoxyCodeLine{285   \textcolor{comment}{! local variables}}
\DoxyCodeLine{286 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G)+1)} :: \&}
\DoxyCodeLine{287     eta      \textcolor{comment}{! Interface heights before diapycnal mixing [m].}}
\DoxyCodeLine{288 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),CS\%nMode)} :: \&}
\DoxyCodeLine{289     cn\_IGW   \textcolor{comment}{! baroclinic internal gravity wave speeds}}
\DoxyCodeLine{290 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),G\%ke)} :: temp\_diag             \textcolor{comment}{! diagnostic array for temp}}
\DoxyCodeLine{291   \textcolor{keywordtype}{integer} :: i, j, k, m, is, ie, js, je, nz}
\DoxyCodeLine{292   \textcolor{keywordtype}{logical} :: showCallTree \textcolor{comment}{! If true, show the call tree}}
\DoxyCodeLine{293 }
\DoxyCodeLine{294   \textcolor{keywordflow}{if} (g\%ke == 1) \textcolor{keywordflow}{return}}
\DoxyCodeLine{295 }
\DoxyCodeLine{296   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = g\%ke}
\DoxyCodeLine{297 }
\DoxyCodeLine{298   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_diabatic\_driver: "}// \&}
\DoxyCodeLine{299          \textcolor{stringliteral}{"Module must be initialized before it is used."})}
\DoxyCodeLine{300   \textcolor{keywordflow}{if} (dt == 0.0) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_diabatic\_driver: "}// \&}
\DoxyCodeLine{301         \textcolor{stringliteral}{"diabatic was called with a zero length timestep."})}
\DoxyCodeLine{302   \textcolor{keywordflow}{if} (dt < 0.0) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_diabatic\_driver: "}// \&}
\DoxyCodeLine{303         \textcolor{stringliteral}{"diabatic was called with a negative timestep."})}
\DoxyCodeLine{304 }
\DoxyCodeLine{305   showcalltree = calltree\_showquery()}
\DoxyCodeLine{306 }
\DoxyCodeLine{307   \textcolor{comment}{! Offer diagnostics of various state varables at the start of diabatic}}
\DoxyCodeLine{308   \textcolor{comment}{! these are mostly for debugging purposes.}}
\DoxyCodeLine{309   \textcolor{keywordflow}{if} (cs\%id\_u\_predia > 0) \textcolor{keyword}{call }post\_data(cs\%id\_u\_predia, u, cs\%diag)}
\DoxyCodeLine{310   \textcolor{keywordflow}{if} (cs\%id\_v\_predia > 0) \textcolor{keyword}{call }post\_data(cs\%id\_v\_predia, v, cs\%diag)}
\DoxyCodeLine{311   \textcolor{keywordflow}{if} (cs\%id\_h\_predia > 0) \textcolor{keyword}{call }post\_data(cs\%id\_h\_predia, h, cs\%diag)}
\DoxyCodeLine{312   \textcolor{keywordflow}{if} (cs\%id\_T\_predia > 0) \textcolor{keyword}{call }post\_data(cs\%id\_T\_predia, tv\%T, cs\%diag)}
\DoxyCodeLine{313   \textcolor{keywordflow}{if} (cs\%id\_S\_predia > 0) \textcolor{keyword}{call }post\_data(cs\%id\_S\_predia, tv\%S, cs\%diag)}
\DoxyCodeLine{314   \textcolor{keywordflow}{if} (cs\%id\_e\_predia > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{315     \textcolor{keyword}{call }find\_eta(h, tv, g, gv, us, eta, eta\_to\_m=1.0)}
\DoxyCodeLine{316     \textcolor{keyword}{call }post\_data(cs\%id\_e\_predia, eta, cs\%diag)}
\DoxyCodeLine{317 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{318 }
\DoxyCodeLine{319   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{320     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"Start of diabatic "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{321     \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"Start of diabatic"}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{322 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{323   \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'Start of diabatic'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{324 }
\DoxyCodeLine{325   \textcolor{keywordflow}{if} (cs\%debug\_energy\_req) \&}
\DoxyCodeLine{326     \textcolor{keyword}{call }diapyc\_energy\_req\_test(h, dt, tv, g, gv, us, cs\%diapyc\_en\_rec\_CSp)}
\DoxyCodeLine{327 }
\DoxyCodeLine{328   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_set\_diffusivity)}
\DoxyCodeLine{329   \textcolor{keyword}{call }set\_bbl\_tke(u, v, h, fluxes, visc, g, gv, us, cs\%set\_diff\_CSp, obc=obc)}
\DoxyCodeLine{330   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_set\_diffusivity)}
\DoxyCodeLine{331 }
\DoxyCodeLine{332   \textcolor{comment}{! Frazil formation keeps the temperature above the freezing point.}}
\DoxyCodeLine{333   \textcolor{comment}{! make\_frazil is deliberately called at both the beginning and at}}
\DoxyCodeLine{334   \textcolor{comment}{! the end of the diabatic processes.}}
\DoxyCodeLine{335   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%T) .AND. \textcolor{keyword}{associated}(tv\%frazil)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{336     \textcolor{comment}{! For frazil diagnostic, the first call covers the first half of the time step}}
\DoxyCodeLine{337     \textcolor{keyword}{call }enable\_averages(0.5*dt, time\_end -\/ real\_to\_time(0.5*us\%T\_to\_s*dt), cs\%diag)}
\DoxyCodeLine{338     \textcolor{keywordflow}{if} (cs\%frazil\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{339       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{340         temp\_diag(i,j,k) = tv\%T(i,j,k)}
\DoxyCodeLine{341 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{342 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{343 }
\DoxyCodeLine{344     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes\%p\_surf\_full)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{345       \textcolor{keyword}{call }make\_frazil(h, tv, g, gv, us, cs\%diabatic\_aux\_CSp, fluxes\%p\_surf\_full, halo=cs\%halo\_TS\_diff)}
\DoxyCodeLine{346     \textcolor{keywordflow}{else}}
\DoxyCodeLine{347       \textcolor{keyword}{call }make\_frazil(h, tv, g, gv, us, cs\%diabatic\_aux\_CSp, halo=cs\%halo\_TS\_diff)}
\DoxyCodeLine{348 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{349     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with 1st make\_frazil (diabatic)"})}
\DoxyCodeLine{350 }
\DoxyCodeLine{351     \textcolor{keywordflow}{if} (cs\%frazil\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{352       \textcolor{keyword}{call }diagnose\_frazil\_tendency(tv, h, temp\_diag, 0.5*dt, g, gv, us, cs)}
\DoxyCodeLine{353       \textcolor{keywordflow}{if} (cs\%id\_frazil\_h > 0) \textcolor{keyword}{call }post\_data(cs\%id\_frazil\_h, h, cs\%diag)}
\DoxyCodeLine{354 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{355     \textcolor{keyword}{call }disable\_averaging(cs\%diag)}
\DoxyCodeLine{356 \textcolor{keywordflow}{  endif} \textcolor{comment}{! associated(tv\%T) .AND. associated(tv\%frazil)}}
\DoxyCodeLine{357   \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'1st make\_frazil'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{358 }
\DoxyCodeLine{359   \textcolor{keywordflow}{if} (cs\%use\_int\_tides) \textcolor{keywordflow}{then}}
\DoxyCodeLine{360     \textcolor{comment}{! This block provides an interface for the unresolved low-\/mode internal tide module.}}
\DoxyCodeLine{361     \textcolor{keyword}{call }set\_int\_tide\_input(u, v, h, tv, fluxes, cs\%int\_tide\_input, dt, g, gv, us, \&}
\DoxyCodeLine{362                             cs\%int\_tide\_input\_CSp)}
\DoxyCodeLine{363     cn\_igw(:,:,:) = 0.0}
\DoxyCodeLine{364     \textcolor{keywordflow}{if} (cs\%uniform\_test\_cg > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{365       \textcolor{keywordflow}{do} m=1,cs\%nMode ; cn\_igw(:,:,m) = cs\%uniform\_test\_cg ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{366     \textcolor{keywordflow}{else}}
\DoxyCodeLine{367       \textcolor{keyword}{call }wave\_speeds(h, tv, g, gv, us, cs\%nMode, cn\_igw, full\_halos=.true.)}
\DoxyCodeLine{368 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{369 }
\DoxyCodeLine{370     \textcolor{keyword}{call }propagate\_int\_tide(h, tv, cn\_igw, cs\%int\_tide\_input\%TKE\_itidal\_input, cs\%int\_tide\_input\%tideamp, \&}
\DoxyCodeLine{371                             cs\%int\_tide\_input\%Nb, dt, g, gv, us, cs\%int\_tide\_CSp)}
\DoxyCodeLine{372     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with propagate\_int\_tide (diabatic)"})}
\DoxyCodeLine{373 \textcolor{keywordflow}{  endif} \textcolor{comment}{! end CS\%use\_int\_tides}}
\DoxyCodeLine{374 }
\DoxyCodeLine{375   \textcolor{keywordflow}{if} (cs\%useALEalgorithm .and. cs\%use\_legacy\_diabatic) \textcolor{keywordflow}{then}}
\DoxyCodeLine{376     \textcolor{keyword}{call }diabatic\_ale\_legacy(u, v, h, tv, hml, fluxes, visc, adp, cdp, dt, time\_end, \&}
\DoxyCodeLine{377                       g, gv, us, cs, waves)}
\DoxyCodeLine{378   \textcolor{keywordflow}{elseif} (cs\%useALEalgorithm) \textcolor{keywordflow}{then}}
\DoxyCodeLine{379     \textcolor{keyword}{call }diabatic\_ale(u, v, h, tv, hml, fluxes, visc, adp, cdp, dt, time\_end, \&}
\DoxyCodeLine{380                       g, gv, us, cs, waves)}
\DoxyCodeLine{381   \textcolor{keywordflow}{else}}
\DoxyCodeLine{382     \textcolor{keyword}{call }layered\_diabatic(u, v, h, tv, hml, fluxes, visc, adp, cdp, dt, time\_end, \&}
\DoxyCodeLine{383                           g, gv, us, cs, waves)}
\DoxyCodeLine{384 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{385 }
\DoxyCodeLine{386 }
\DoxyCodeLine{387   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_pass)}
\DoxyCodeLine{388   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Kv\_shear)) \&}
\DoxyCodeLine{389     \textcolor{keyword}{call }pass\_var(visc\%Kv\_shear, g\%Domain, to\_all+omit\_corners, halo=1)}
\DoxyCodeLine{390   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_pass)}
\DoxyCodeLine{391 }
\DoxyCodeLine{392   \textcolor{keyword}{call }disable\_averaging(cs\%diag)}
\DoxyCodeLine{393   \textcolor{comment}{! Frazil formation keeps temperature above the freezing point.}}
\DoxyCodeLine{394   \textcolor{comment}{! make\_frazil is deliberately called at both the beginning and at}}
\DoxyCodeLine{395   \textcolor{comment}{! the end of the diabatic processes.}}
\DoxyCodeLine{396   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%T) .AND. \textcolor{keyword}{associated}(tv\%frazil)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{397     \textcolor{keyword}{call }enable\_averages(0.5*dt, time\_end, cs\%diag)}
\DoxyCodeLine{398     \textcolor{keywordflow}{if} (cs\%frazil\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{399       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{400         temp\_diag(i,j,k) = tv\%T(i,j,k)}
\DoxyCodeLine{401 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{402 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{403 }
\DoxyCodeLine{404     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(fluxes\%p\_surf\_full)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{405       \textcolor{keyword}{call }make\_frazil(h, tv, g, gv, us, cs\%diabatic\_aux\_CSp, fluxes\%p\_surf\_full)}
\DoxyCodeLine{406     \textcolor{keywordflow}{else}}
\DoxyCodeLine{407       \textcolor{keyword}{call }make\_frazil(h, tv, g, gv, us, cs\%diabatic\_aux\_CSp)}
\DoxyCodeLine{408 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{409 }
\DoxyCodeLine{410     \textcolor{keywordflow}{if} (cs\%frazil\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{411       \textcolor{keyword}{call }diagnose\_frazil\_tendency(tv, h, temp\_diag, 0.5*dt, g, gv, us, cs)}
\DoxyCodeLine{412       \textcolor{keywordflow}{if} (cs\%id\_frazil\_h > 0 ) \textcolor{keyword}{call }post\_data(cs\%id\_frazil\_h, h, cs\%diag)}
\DoxyCodeLine{413 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{414 }
\DoxyCodeLine{415     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with 2nd make\_frazil (diabatic)"})}
\DoxyCodeLine{416     \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'2nd make\_frazil'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{417     \textcolor{keyword}{call }disable\_averaging(cs\%diag)}
\DoxyCodeLine{418 }
\DoxyCodeLine{419 \textcolor{keywordflow}{  endif}  \textcolor{comment}{! endif for frazil}}
\DoxyCodeLine{420 }
\DoxyCodeLine{421 }
\DoxyCodeLine{422   \textcolor{comment}{! Diagnose mixed layer depths.}}
\DoxyCodeLine{423   \textcolor{keyword}{call }enable\_averages(dt, time\_end, cs\%diag)}
\DoxyCodeLine{424   \textcolor{keywordflow}{if} (cs\%id\_MLD\_003 > 0 .or. cs\%id\_subMLN2 > 0 .or. cs\%id\_mlotstsq > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{425     \textcolor{keyword}{call }diagnosemldbydensitydifference(cs\%id\_MLD\_003, h, tv, 0.03*us\%kg\_m3\_to\_R, g, gv, us, cs\%diag, \&}
\DoxyCodeLine{426                                         id\_n2subml=cs\%id\_subMLN2, id\_mldsq=cs\%id\_mlotstsq, dz\_subml=cs\%dz\_subML\_N2)}
\DoxyCodeLine{427 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{428   \textcolor{keywordflow}{if} (cs\%id\_MLD\_0125 > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{429     \textcolor{keyword}{call }diagnosemldbydensitydifference(cs\%id\_MLD\_0125, h, tv, 0.125*us\%kg\_m3\_to\_R, g, gv, us, cs\%diag)}
\DoxyCodeLine{430 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{431   \textcolor{keywordflow}{if} (cs\%id\_MLD\_user > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{432     \textcolor{keyword}{call }diagnosemldbydensitydifference(cs\%id\_MLD\_user, h, tv, cs\%MLDdensityDifference, g, gv, us, cs\%diag)}
\DoxyCodeLine{433 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{434   \textcolor{keywordflow}{if} ((cs\%id\_MLD\_EN1 > 0) .or. (cs\%id\_MLD\_EN2 > 0) .or. (cs\%id\_MLD\_EN3 > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{435     \textcolor{keyword}{call }diagnosemldbyenergy((/cs\%id\_MLD\_EN1, cs\%id\_MLD\_EN2, cs\%id\_MLD\_EN3/),\&}
\DoxyCodeLine{436          h, tv, g, gv, us, cs\%MLD\_EN\_VALS, cs\%diag)}
\DoxyCodeLine{437 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{438   \textcolor{keywordflow}{if} (cs\%use\_int\_tides) \textcolor{keywordflow}{then}}
\DoxyCodeLine{439     \textcolor{keywordflow}{if} (cs\%id\_cg1 > 0) \textcolor{keyword}{call }post\_data(cs\%id\_cg1, cn\_igw(:,:,1),cs\%diag)}
\DoxyCodeLine{440     \textcolor{keywordflow}{do} m=1,cs\%nMode ; \textcolor{keywordflow}{if} (cs\%id\_cn(m) > 0) \textcolor{keyword}{call }post\_data(cs\%id\_cn(m), cn\_igw(:,:,m), cs\%diag) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{441 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{442   \textcolor{keyword}{call }disable\_averaging(cs\%diag)}
\DoxyCodeLine{443 }
\DoxyCodeLine{444   \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'leaving diabatic'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{445 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diabatic__driver_ae57c48925de75712384e859a851c8c40}\label{namespacemom__diabatic__driver_ae57c48925de75712384e859a851c8c40}} 
\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}!diabatic\_ale@{diabatic\_ale}}
\index{diabatic\_ale@{diabatic\_ale}!mom\_diabatic\_driver@{mom\_diabatic\_driver}}
\doxysubsubsection{\texorpdfstring{diabatic\_ale()}{diabatic\_ale()}}
{\footnotesize\ttfamily subroutine mom\+\_\+diabatic\+\_\+driver\+::diabatic\+\_\+ale (\begin{DoxyParamCaption}\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed, g \%ke), intent(inout)}]{u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb, g \%ke), intent(inout)}]{v,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(inout)}]{h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(inout)}]{tv,  }\item[{real, dimension(\+:,\+:), pointer}]{Hml,  }\item[{type(forcing), intent(inout)}]{fluxes,  }\item[{type(vertvisc\+\_\+type), intent(inout)}]{visc,  }\item[{type(accel\+\_\+diag\+\_\+ptrs), intent(inout)}]{A\+Dp,  }\item[{type(cont\+\_\+diag\+\_\+ptrs), intent(inout)}]{C\+Dp,  }\item[{real, intent(in)}]{dt,  }\item[{type(time\+\_\+type), intent(in)}]{Time\+\_\+end,  }\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__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), pointer}]{CS,  }\item[{type(wave\+\_\+parameters\+\_\+cs), optional, pointer}]{Waves }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine imposes the diapycnal mass fluxes and the accompanying diapycnal advection of momentum and tracers. 


\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 us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in,out}}  & {\em u} & zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em v} & meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em h} & thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em tv} & points to thermodynamic fields unused have N\+U\+LL ptrs \\
\hline
 & {\em hml} & Active mixed layer depth \mbox{[}Z $\sim$$>$ m\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em fluxes} & points to forcing fields unused fields have N\+U\+LL ptrs \\
\hline
\mbox{\texttt{ in,out}}  & {\em visc} & vertical viscosities, B\+BL properies, and \\
\hline
\mbox{\texttt{ in,out}}  & {\em adp} & related points to accelerations in momentum equations, to enable the later derived diagnostics, like energy budgets \\
\hline
\mbox{\texttt{ in,out}}  & {\em cdp} & points to terms in continuity equations \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & time increment \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em time\+\_\+end} & Time at the end of the interval \\
\hline
 & {\em cs} & module control structure \\
\hline
 & {\em waves} & Surface gravity waves \\
\hline
\end{DoxyParams}


Definition at line 1173 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1173   \textcolor{keywordtype}{type}(ocean\_grid\_type),                     \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{         !< ocean grid structure}}
\DoxyCodeLine{1174   \textcolor{keywordtype}{type}(verticalGrid\_type),                   \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{        !< ocean vertical grid structure}}
\DoxyCodeLine{1175   \textcolor{keywordtype}{type}(unit\_scale\_type),                     \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{        !< A dimensional unit scaling type}}
\DoxyCodeLine{1176 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(inout)} :: u\textcolor{comment}{         !< zonal velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{1177 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(inout)} :: v\textcolor{comment}{         !< meridional velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{1178 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{         !< thickness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1179   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),                     \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{        !< points to thermodynamic fields}}
\DoxyCodeLine{1180 \textcolor{comment}{                                                                        !! unused have NULL ptrs}}
\DoxyCodeLine{1181 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(:,:)},                      \textcolor{keywordtype}{pointer}       :: Hml\textcolor{comment}{       !< Active mixed layer depth [Z ~> m]}}
\DoxyCodeLine{1182   \textcolor{keywordtype}{type}(forcing),                             \textcolor{keywordtype}{intent(inout)} :: fluxes\textcolor{comment}{    !< points to forcing fields}}
\DoxyCodeLine{1183 \textcolor{comment}{                                                                        !! unused fields have NULL ptrs}}
\DoxyCodeLine{1184   \textcolor{keywordtype}{type}(vertvisc\_type),                       \textcolor{keywordtype}{intent(inout)} :: visc\textcolor{comment}{      !< vertical viscosities, BBL properies, and}}
\DoxyCodeLine{1185   \textcolor{keywordtype}{type}(accel\_diag\_ptrs),                     \textcolor{keywordtype}{intent(inout)} :: ADp\textcolor{comment}{       !< related points to accelerations in momentum}}
\DoxyCodeLine{1186 \textcolor{comment}{                                                                        !! equations, to enable the later derived}}
\DoxyCodeLine{1187 \textcolor{comment}{                                                                        !! diagnostics, like energy budgets}}
\DoxyCodeLine{1188   \textcolor{keywordtype}{type}(cont\_diag\_ptrs),                      \textcolor{keywordtype}{intent(inout)} :: CDp\textcolor{comment}{       !< points to terms in continuity equations}}
\DoxyCodeLine{1189 \textcolor{keywordtype}{  real},                                      \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{        !< time increment [T ~> s]}}
\DoxyCodeLine{1190   \textcolor{keywordtype}{type}(time\_type),                           \textcolor{keywordtype}{intent(in)}    :: Time\_end\textcolor{comment}{  !< Time at the end of the interval}}
\DoxyCodeLine{1191   \textcolor{keywordtype}{type}(diabatic\_CS),                         \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{        !< module control structure}}
\DoxyCodeLine{1192   \textcolor{keywordtype}{type}(Wave\_parameters\_CS),        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}       :: Waves\textcolor{comment}{     !< Surface gravity waves}}
\DoxyCodeLine{1193 }
\DoxyCodeLine{1194   \textcolor{comment}{! local variables}}
\DoxyCodeLine{1195 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))} :: \&}
\DoxyCodeLine{1196     ea\_s,     \&  \textcolor{comment}{! amount of fluid entrained from the layer above within}}
\DoxyCodeLine{1197                  \textcolor{comment}{! one time step  [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1198     eb\_s,     \&  \textcolor{comment}{! amount of fluid entrained from the layer below within}}
\DoxyCodeLine{1199                  \textcolor{comment}{! one time step  [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1200     ea\_t,     \&  \textcolor{comment}{! amount of fluid entrained from the layer above within}}
\DoxyCodeLine{1201                  \textcolor{comment}{! one time step  [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1202     eb\_t,     \&  \textcolor{comment}{! amount of fluid entrained from the layer below within}}
\DoxyCodeLine{1203                  \textcolor{comment}{! one time step  [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1204     h\_prebound, \& \textcolor{comment}{! initial layer thicknesses [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1205     dsv\_dt, \&    \textcolor{comment}{! The partial derivative of specific volume with temperature [R-\/1 degC-\/1 ~> m3 kg-\/1 degC-\/1]}}
\DoxyCodeLine{1206     dsv\_ds, \&    \textcolor{comment}{! The partial derivative of specific volume with salinity [R-\/1 ppt-\/1 ~> m3 kg-\/1 ppt-\/1].}}
\DoxyCodeLine{1207     ctke,   \&    \textcolor{comment}{! convective TKE requirements for each layer [R Z3 T-\/2 ~> J m-\/2].}}
\DoxyCodeLine{1208     u\_h,    \&    \textcolor{comment}{! zonal and meridional velocities at thickness points after}}
\DoxyCodeLine{1209     v\_h          \textcolor{comment}{! entrainment [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{1210 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: \&}
\DoxyCodeLine{1211     SkinBuoyFlux\textcolor{comment}{! 2d surface buoyancy flux [Z2 T-\/3 ~> m2 s-\/3], used by ePBL}}
\DoxyCodeLine{1212 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),G\%ke)} :: h\_diag                \textcolor{comment}{! diagnostic array for thickness}}
\DoxyCodeLine{1213 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),G\%ke)} :: temp\_diag             \textcolor{comment}{! diagnostic array for temp}}
\DoxyCodeLine{1214 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),G\%ke)} :: saln\_diag             \textcolor{comment}{! diagnostic array for salinity}}
\DoxyCodeLine{1215 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))}      :: tendency\_2d           \textcolor{comment}{! depth integrated content tendency for diagn}}
\DoxyCodeLine{1216 }
\DoxyCodeLine{1217 \textcolor{keywordtype}{  real} :: net\_ent  \textcolor{comment}{! The net of ea-\/eb at an interface.}}
\DoxyCodeLine{1218 }
\DoxyCodeLine{1219 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}  :: \&}
\DoxyCodeLine{1220     eatr, \&  \textcolor{comment}{! The equivalent of ea and eb for tracers, which differ from ea and}}
\DoxyCodeLine{1221     ebtr     \textcolor{comment}{! eb in that they tend to homogenize tracers in massless layers}}
\DoxyCodeLine{1222              \textcolor{comment}{! near the boundaries [H ~> m or kg m-\/2] (for Bous or non-\/Bouss)}}
\DoxyCodeLine{1223 }
\DoxyCodeLine{1224 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G)+1)} :: \&}
\DoxyCodeLine{1225     Kd\_int,   \& \textcolor{comment}{! diapycnal diffusivity of interfaces [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{1226     Kd\_heat,  \& \textcolor{comment}{! diapycnal diffusivity of heat [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{1227     Kd\_salt,  \& \textcolor{comment}{! diapycnal diffusivity of salt and passive tracers [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{1228     Kd\_extra\_T , \& \textcolor{comment}{! The extra diffusivity of temperature due to double diffusion relative to}}
\DoxyCodeLine{1229                 \textcolor{comment}{! Kd\_int [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{1230     kd\_extra\_s , \& \textcolor{comment}{!  The extra diffusivity of salinity due to double diffusion relative to}}
\DoxyCodeLine{1231                 \textcolor{comment}{! Kd\_int [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{1232     kd\_epbl,  \& \textcolor{comment}{! boundary layer or convective diapycnal diffusivities at interfaces [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{1233     tdif\_flx, \& \textcolor{comment}{! diffusive diapycnal heat flux across interfaces [degC H T-\/1 ~> degC m s-\/1 or degC kg m-\/2 s-\/1]}}
\DoxyCodeLine{1234     tadv\_flx, \& \textcolor{comment}{! advective diapycnal heat flux across interfaces [degC H T-\/1 ~> degC m s-\/1 or degC kg m-\/2 s-\/1]}}
\DoxyCodeLine{1235     sdif\_flx, \& \textcolor{comment}{! diffusive diapycnal salt flux across interfaces [ppt H T-\/1 ~> ppt m s-\/1 or ppt kg m-\/2 s-\/1]}}
\DoxyCodeLine{1236     sadv\_flx    \textcolor{comment}{! advective diapycnal salt flux across interfaces [ppt H T-\/1 ~> ppt m s-\/1 or ppt kg m-\/2 s-\/1]}}
\DoxyCodeLine{1237 }
\DoxyCodeLine{1238 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{allocatable}, \textcolor{keywordtype}{dimension(:,:)} :: \&}
\DoxyCodeLine{1239     hf\_dudt\_dia\_2d, hf\_dvdt\_dia\_2d \textcolor{comment}{! Depth sum of diapycnal mixing accelaration * fract. thickness [L T-\/2 ~> m s-\/2].}}
\DoxyCodeLine{1240 }
\DoxyCodeLine{1241   \textcolor{keywordtype}{logical} :: in\_boundary(SZI\_(G)) \textcolor{comment}{! True if there are no massive layers below,}}
\DoxyCodeLine{1242                                   \textcolor{comment}{! where massive is defined as sufficiently thick that}}
\DoxyCodeLine{1243                                   \textcolor{comment}{! the no-\/flux boundary conditions have not restricted}}
\DoxyCodeLine{1244                                   \textcolor{comment}{! the entrainment -\/ usually sqrt(Kd*dt).}}
\DoxyCodeLine{1245 }
\DoxyCodeLine{1246 \textcolor{keywordtype}{  real} :: b\_denom\_1    \textcolor{comment}{! The first term in the denominator of b1}}
\DoxyCodeLine{1247                        \textcolor{comment}{! [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1248 \textcolor{keywordtype}{  real} :: h\_neglect    \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{1249                        \textcolor{comment}{! in roundoff and can be neglected}}
\DoxyCodeLine{1250                        \textcolor{comment}{! [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1251 \textcolor{keywordtype}{  real} :: h\_neglect2   \textcolor{comment}{! h\_neglect\string^2 [H2 ~> m2 or kg2 m-\/4]}}
\DoxyCodeLine{1252 \textcolor{keywordtype}{  real} :: add\_ent      \textcolor{comment}{! Entrainment that needs to be added when mixing tracers}}
\DoxyCodeLine{1253                        \textcolor{comment}{! [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1254 \textcolor{keywordtype}{  real} :: eaval        \textcolor{comment}{! eaval is 2*ea at velocity grid points [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1255 \textcolor{keywordtype}{  real} :: hval         \textcolor{comment}{! hval is 2*h at velocity grid points [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1256 \textcolor{keywordtype}{  real} :: h\_tr         \textcolor{comment}{! h\_tr is h at tracer points with a tiny thickness}}
\DoxyCodeLine{1257                        \textcolor{comment}{! added to ensure positive definiteness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1258 \textcolor{keywordtype}{  real} :: Tr\_ea\_BBL    \textcolor{comment}{! The diffusive tracer thickness in the BBL that is}}
\DoxyCodeLine{1259                        \textcolor{comment}{! coupled to the bottom within a timestep [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1260 }
\DoxyCodeLine{1261 \textcolor{keywordtype}{  real} :: htot(SZIB\_(G))             \textcolor{comment}{! The summed thickness from the bottom [H ~> m or kg m-\/2].}}
\DoxyCodeLine{1262 \textcolor{keywordtype}{  real} :: b1(SZIB\_(G)), d1(SZIB\_(G)) \textcolor{comment}{! b1, c1, and d1 are variables used by the}}
\DoxyCodeLine{1263 \textcolor{keywordtype}{  real} :: c1(SZIB\_(G),SZK\_(G))       \textcolor{comment}{! tridiagonal solver.}}
\DoxyCodeLine{1264 }
\DoxyCodeLine{1265 \textcolor{keywordtype}{  real} :: Kd\_add\_here    \textcolor{comment}{! An added diffusivity [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{1266 \textcolor{keywordtype}{  real} :: Idt     \textcolor{comment}{! The inverse time step [T-\/1 ~> s-\/1]}}
\DoxyCodeLine{1267 }
\DoxyCodeLine{1268   \textcolor{keywordtype}{integer} :: dir\_flag     \textcolor{comment}{! An integer encoding the directions in which to do halo updates.}}
\DoxyCodeLine{1269   \textcolor{keywordtype}{logical} :: showCallTree \textcolor{comment}{! If true, show the call tree}}
\DoxyCodeLine{1270   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, Isq, Ieq, Jsq, Jeq, nz, m}
\DoxyCodeLine{1271 }
\DoxyCodeLine{1272   is   = g\%isc  ; ie  = g\%iec  ; js  = g\%jsc  ; je  = g\%jec ; nz = g\%ke}
\DoxyCodeLine{1273   isq  = g\%IscB ; ieq = g\%IecB ; jsq = g\%JscB ; jeq = g\%JecB}
\DoxyCodeLine{1274   h\_neglect = gv\%H\_subroundoff ; h\_neglect2 = h\_neglect*h\_neglect}
\DoxyCodeLine{1275   kd\_heat(:,:,:) = 0.0 ; kd\_salt(:,:,:) = 0.0}
\DoxyCodeLine{1276   ea\_s(:,:,:) = 0.0; eb\_s(:,:,:) = 0.0; ea\_t(:,:,:) = 0.0; eb\_t(:,:,:) = 0.0}
\DoxyCodeLine{1277 }
\DoxyCodeLine{1278   showcalltree = calltree\_showquery()}
\DoxyCodeLine{1279   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_enter(\textcolor{stringliteral}{"diabatic\_ALE(), MOM\_diabatic\_driver.F90"})}
\DoxyCodeLine{1280 }
\DoxyCodeLine{1281   \textcolor{keywordflow}{if} (.not. (cs\%useALEalgorithm)) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"MOM\_diabatic\_driver: "}// \&}
\DoxyCodeLine{1282          \textcolor{stringliteral}{"The ALE algorithm must be enabled when using MOM\_diabatic\_driver."})}
\DoxyCodeLine{1283 }
\DoxyCodeLine{1284   \textcolor{comment}{! For all other diabatic subroutines, the averaging window should be the entire diabatic timestep}}
\DoxyCodeLine{1285   \textcolor{keyword}{call }enable\_averages(dt, time\_end, cs\%diag)}
\DoxyCodeLine{1286 }
\DoxyCodeLine{1287   \textcolor{keywordflow}{if} (cs\%use\_geothermal) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1288     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_geothermal)}
\DoxyCodeLine{1289     \textcolor{keyword}{call }geothermal\_in\_place(h, tv, dt, g, gv, us, cs\%geothermal\_CSp, halo=cs\%halo\_TS\_diff)}
\DoxyCodeLine{1290     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_geothermal)}
\DoxyCodeLine{1291     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"geothermal (diabatic)"})}
\DoxyCodeLine{1292     \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'geothermal'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{1293 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1294 }
\DoxyCodeLine{1295   \textcolor{comment}{! Whenever thickness changes let the diag manager know, target grids}}
\DoxyCodeLine{1296   \textcolor{comment}{! for vertical remapping may need to be regenerated.}}
\DoxyCodeLine{1297   \textcolor{keyword}{call }diag\_update\_remap\_grids(cs\%diag)}
\DoxyCodeLine{1298 }
\DoxyCodeLine{1299   \textcolor{comment}{! Set\_pen\_shortwave estimates the optical properties of the water column.}}
\DoxyCodeLine{1300   \textcolor{comment}{! It will need to be modified later to include information about the}}
\DoxyCodeLine{1301   \textcolor{comment}{! biological properties and layer thicknesses.}}
\DoxyCodeLine{1302   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs\%optics)) \&}
\DoxyCodeLine{1303     \textcolor{keyword}{call }set\_pen\_shortwave(cs\%optics, fluxes, g, gv, us, cs\%diabatic\_aux\_CSp, cs\%opacity\_CSp, cs\%tracer\_flow\_CSp)}
\DoxyCodeLine{1304 }
\DoxyCodeLine{1305   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"before find\_uv\_at\_h"}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{1306 }
\DoxyCodeLine{1307   \textcolor{keywordflow}{if} (cs\%use\_kappa\_shear .or. cs\%use\_CVMix\_shear) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1308     \textcolor{keywordflow}{if} (cs\%use\_geothermal) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1309       \textcolor{comment}{! The presence of eatr and ebtr causes find\_uv\_at\_h to use a tridiagonal solver,}}
\DoxyCodeLine{1310       \textcolor{comment}{! which changes answers at the level of roundoff because ((A*B / A) /= B).}}
\DoxyCodeLine{1311       eatr(:,:,:) = 0.0 ; ebtr(:,:,:) = 0.0}
\DoxyCodeLine{1312       \textcolor{keyword}{call }find\_uv\_at\_h(u, v, h, u\_h, v\_h, g, gv, us, eatr, ebtr)}
\DoxyCodeLine{1313     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1314       \textcolor{keyword}{call }find\_uv\_at\_h(u, v, h, u\_h, v\_h, g, gv, us)}
\DoxyCodeLine{1315 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1316     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with find\_uv\_at\_h (diabatic)"})}
\DoxyCodeLine{1317 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1318 }
\DoxyCodeLine{1319   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_set\_diffusivity)}
\DoxyCodeLine{1320   \textcolor{comment}{! Sets: Kd\_int, Kd\_extra\_T, Kd\_extra\_S and visc\%TKE\_turb}}
\DoxyCodeLine{1321   \textcolor{comment}{! Also changes: visc\%Kd\_shear, visc\%Kv\_shear and visc\%Kv\_slow}}
\DoxyCodeLine{1322   \textcolor{keywordflow}{if} (cs\%debug) \&}
\DoxyCodeLine{1323     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"before set\_diffusivity"}, u, v, h, g, gv, us, haloshift=cs\%halo\_TS\_diff)}
\DoxyCodeLine{1324   \textcolor{keywordflow}{if} (cs\%double\_diffuse) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1325     \textcolor{keyword}{call }set\_diffusivity(u, v, h, u\_h, v\_h, tv, fluxes, cs\%optics, visc, dt, g, gv, us, cs\%set\_diff\_CSp, \&}
\DoxyCodeLine{1326                          kd\_int=kd\_int, kd\_extra\_t=kd\_extra\_t, kd\_extra\_s=kd\_extra\_s)}
\DoxyCodeLine{1327   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1328     \textcolor{keyword}{call }set\_diffusivity(u, v, h, u\_h, v\_h, tv, fluxes, cs\%optics, visc, dt, g, gv, us, \&}
\DoxyCodeLine{1329                          cs\%set\_diff\_CSp, kd\_int=kd\_int)}
\DoxyCodeLine{1330 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1331   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_set\_diffusivity)}
\DoxyCodeLine{1332   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with set\_diffusivity (diabatic)"})}
\DoxyCodeLine{1333 }
\DoxyCodeLine{1334   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1335     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after set\_diffusivity "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{1336     \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after set\_diffusivity "}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{1337     \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after set\_diffusivity "}, tv, g)}
\DoxyCodeLine{1338     \textcolor{keyword}{call }hchksum(kd\_int, \textcolor{stringliteral}{"after set\_diffusivity Kd\_Int"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{1339 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1340 }
\DoxyCodeLine{1341   \textcolor{comment}{! Set diffusivities for heat and salt separately}}
\DoxyCodeLine{1342 }
\DoxyCodeLine{1343   \textcolor{comment}{! Add contribution from double diffusion}}
\DoxyCodeLine{1344   \textcolor{keywordflow}{if} (cs\%double\_diffuse) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1345     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{1346     \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1347       kd\_salt(i,j,k) = kd\_int(i,j,k) + kd\_extra\_s(i,j,k)}
\DoxyCodeLine{1348       kd\_heat(i,j,k) = kd\_int(i,j,k) + kd\_extra\_t(i,j,k)}
\DoxyCodeLine{1349 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1350   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1351     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{1352     \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1353       kd\_salt(i,j,k) = kd\_int(i,j,k)}
\DoxyCodeLine{1354       kd\_heat(i,j,k) = kd\_int(i,j,k)}
\DoxyCodeLine{1355 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1356 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1357 }
\DoxyCodeLine{1358   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1359     \textcolor{keyword}{call }hchksum(kd\_heat, \textcolor{stringliteral}{"after set\_diffusivity Kd\_heat"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{1360     \textcolor{keyword}{call }hchksum(kd\_salt, \textcolor{stringliteral}{"after set\_diffusivity Kd\_salt"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{1361 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1362 }
\DoxyCodeLine{1363   \textcolor{keywordflow}{if} (cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1364     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_kpp)}
\DoxyCodeLine{1365     \textcolor{comment}{! total vertical viscosity in the interior is represented via visc\%Kv\_shear}}
\DoxyCodeLine{1366     \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1367       visc\%Kv\_shear(i,j,k) = visc\%Kv\_shear(i,j,k) + visc\%Kv\_slow(i,j,k)}
\DoxyCodeLine{1368 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1369 }
\DoxyCodeLine{1370     \textcolor{comment}{! KPP needs the surface buoyancy flux but does not update state variables.}}
\DoxyCodeLine{1371     \textcolor{comment}{! We could make this call higher up to avoid a repeat unpacking of the surface fluxes.}}
\DoxyCodeLine{1372     \textcolor{comment}{! Sets: CS\%KPP\_buoy\_flux, CS\%KPP\_temp\_flux, CS\%KPP\_salt\_flux}}
\DoxyCodeLine{1373     \textcolor{comment}{! NOTE: CS\%KPP\_buoy\_flux, CS\%KPP\_temp\_flux, CS\%KPP\_salt\_flux are returned as rates (i.e. stuff per second)}}
\DoxyCodeLine{1374     \textcolor{comment}{! unlike other instances where the fluxes are integrated in time over a time-\/step.}}
\DoxyCodeLine{1375     \textcolor{keyword}{call }calculatebuoyancyflux2d(g, gv, us, fluxes, cs\%optics, h, tv\%T, tv\%S, tv, \&}
\DoxyCodeLine{1376                                  cs\%KPP\_buoy\_flux, cs\%KPP\_temp\_flux, cs\%KPP\_salt\_flux)}
\DoxyCodeLine{1377 }
\DoxyCodeLine{1378     \textcolor{comment}{! The KPP scheme calculates boundary layer diffusivities and non-\/local transport.}}
\DoxyCodeLine{1379     \textcolor{keyword}{call }kpp\_compute\_bld(cs\%KPP\_CSp, g, gv, us, h, tv\%T, tv\%S, u, v, tv, \&}
\DoxyCodeLine{1380                          fluxes\%ustar, cs\%KPP\_buoy\_flux, waves=waves)}
\DoxyCodeLine{1381 }
\DoxyCodeLine{1382     \textcolor{keyword}{call }kpp\_calculate(cs\%KPP\_CSp, g, gv, us, h, fluxes\%ustar, cs\%KPP\_buoy\_flux, kd\_heat, \&}
\DoxyCodeLine{1383                        kd\_salt, visc\%Kv\_shear, cs\%KPP\_NLTheat, cs\%KPP\_NLTscalar, waves=waves)}
\DoxyCodeLine{1384 }
\DoxyCodeLine{1385     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(hml)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1386       \textcolor{keyword}{call }kpp\_get\_bld(cs\%KPP\_CSp, hml(:,:), g, us)}
\DoxyCodeLine{1387       \textcolor{keyword}{call }pass\_var(hml, g\%domain, halo=1)}
\DoxyCodeLine{1388       \textcolor{comment}{! If visc\%MLD exists, copy KPP's BLD into it}}
\DoxyCodeLine{1389       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%MLD)) visc\%MLD(:,:) = hml(:,:)}
\DoxyCodeLine{1390 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1391 }
\DoxyCodeLine{1392     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_kpp)}
\DoxyCodeLine{1393     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with KPP\_calculate (diabatic)"})}
\DoxyCodeLine{1394     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1395       \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after KPP"}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{1396       \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after KPP"}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{1397       \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after KPP"}, tv, g)}
\DoxyCodeLine{1398       \textcolor{keyword}{call }hchksum(kd\_heat, \textcolor{stringliteral}{"after KPP Kd\_heat"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{1399       \textcolor{keyword}{call }hchksum(kd\_salt, \textcolor{stringliteral}{"after KPP Kd\_salt"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{1400 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1401 }
\DoxyCodeLine{1402 \textcolor{keywordflow}{  endif}  \textcolor{comment}{! endif for KPP}}
\DoxyCodeLine{1403 }
\DoxyCodeLine{1404 }
\DoxyCodeLine{1405   \textcolor{keywordflow}{if} (cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1406     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_kpp)}
\DoxyCodeLine{1407     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1408       \textcolor{keyword}{call }hchksum(cs\%KPP\_temp\_flux, \textcolor{stringliteral}{"before KPP\_applyNLT netHeat"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1409       \textcolor{keyword}{call }hchksum(cs\%KPP\_salt\_flux, \textcolor{stringliteral}{"before KPP\_applyNLT netSalt"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1410       \textcolor{keyword}{call }hchksum(cs\%KPP\_NLTheat, \textcolor{stringliteral}{"before KPP\_applyNLT NLTheat"}, g\%HI, haloshift=0)}
\DoxyCodeLine{1411       \textcolor{keyword}{call }hchksum(cs\%KPP\_NLTscalar, \textcolor{stringliteral}{"before KPP\_applyNLT NLTscalar"}, g\%HI, haloshift=0)}
\DoxyCodeLine{1412 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1413     \textcolor{comment}{! Apply non-\/local transport of heat and salt}}
\DoxyCodeLine{1414     \textcolor{comment}{! Changes: tv\%T, tv\%S}}
\DoxyCodeLine{1415     \textcolor{keyword}{call }kpp\_nonlocaltransport\_temp(cs\%KPP\_CSp, g, gv, h, cs\%KPP\_NLTheat,   cs\%KPP\_temp\_flux, \&}
\DoxyCodeLine{1416                                     us\%T\_to\_s*dt, tv\%T, us\%Q\_to\_J\_kg*tv\%C\_p)}
\DoxyCodeLine{1417     \textcolor{keyword}{call }kpp\_nonlocaltransport\_saln(cs\%KPP\_CSp, g, gv, h, cs\%KPP\_NLTscalar, cs\%KPP\_salt\_flux, \&}
\DoxyCodeLine{1418                                     us\%T\_to\_s*dt, tv\%S)}
\DoxyCodeLine{1419     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_kpp)}
\DoxyCodeLine{1420     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with KPP\_applyNonLocalTransport (diabatic)"})}
\DoxyCodeLine{1421     \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'KPP\_applyNonLocalTransport'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{1422 }
\DoxyCodeLine{1423     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1424       \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after KPP\_applyNLT "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{1425       \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after KPP\_applyNLT "}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{1426       \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after KPP\_applyNLT "}, tv, g)}
\DoxyCodeLine{1427 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1428 \textcolor{keywordflow}{  endif} \textcolor{comment}{! endif for KPP}}
\DoxyCodeLine{1429 }
\DoxyCodeLine{1430   \textcolor{comment}{! This is the "old" method for applying differential diffusion.}}
\DoxyCodeLine{1431   \textcolor{comment}{! Changes: tv\%T, tv\%S}}
\DoxyCodeLine{1432   \textcolor{keywordflow}{if} (cs\%double\_diffuse .and. \textcolor{keyword}{associated}(tv\%T) .and. (.not.cs\%use\_CVMix\_ddiff)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1433 }
\DoxyCodeLine{1434     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_differential\_diff)}
\DoxyCodeLine{1435     \textcolor{keyword}{call }differential\_diffuse\_t\_s(h, tv\%T, tv\%S, kd\_extra\_t, kd\_extra\_s, dt, g, gv)}
\DoxyCodeLine{1436     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_differential\_diff)}
\DoxyCodeLine{1437 }
\DoxyCodeLine{1438     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with differential\_diffuse\_T\_S (diabatic)"})}
\DoxyCodeLine{1439     \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'differential\_diffuse\_T\_S'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{1440 }
\DoxyCodeLine{1441     \textcolor{comment}{! increment heat and salt diffusivity.}}
\DoxyCodeLine{1442     \textcolor{comment}{! CS\%useKPP==.true. already has extra\_T and extra\_S included}}
\DoxyCodeLine{1443     \textcolor{keywordflow}{if} (.not. cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1444       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{1445       \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1446         kd\_heat(i,j,k) = kd\_heat(i,j,k) + kd\_extra\_t(i,j,k)}
\DoxyCodeLine{1447         kd\_salt(i,j,k) = kd\_salt(i,j,k) + kd\_extra\_s(i,j,k)}
\DoxyCodeLine{1448 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1449 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1450 }
\DoxyCodeLine{1451 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1452 }
\DoxyCodeLine{1453   \textcolor{comment}{! Calculate vertical mixing due to convection (computed via CVMix)}}
\DoxyCodeLine{1454   \textcolor{keywordflow}{if} (cs\%use\_CVMix\_conv) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1455     \textcolor{comment}{! Increment vertical diffusion and viscosity due to convection}}
\DoxyCodeLine{1456     \textcolor{keywordflow}{if} (cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1457       \textcolor{keyword}{call }calculate\_cvmix\_conv(h, tv, g, gv, us, cs\%CVMix\_conv\_csp, hml, kd=kd\_heat, kv=visc\%Kv\_shear, kd\_aux=kd\_salt)}
\DoxyCodeLine{1458     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1459       \textcolor{keyword}{call }calculate\_cvmix\_conv(h, tv, g, gv, us, cs\%CVMix\_conv\_csp, hml, kd=kd\_heat, kv=visc\%Kv\_slow, kd\_aux=kd\_salt)}
\DoxyCodeLine{1460 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1461 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1462 }
\DoxyCodeLine{1463   \textcolor{comment}{! Save fields before boundary forcing is applied for tendency diagnostics}}
\DoxyCodeLine{1464   \textcolor{keywordflow}{if} (cs\%boundary\_forcing\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1465     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1466       h\_diag(i,j,k)    = h(i,j,k)}
\DoxyCodeLine{1467       temp\_diag(i,j,k) = tv\%T(i,j,k)}
\DoxyCodeLine{1468       saln\_diag(i,j,k) = tv\%S(i,j,k)}
\DoxyCodeLine{1469 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1470 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1471 }
\DoxyCodeLine{1472   \textcolor{comment}{! Apply forcing}}
\DoxyCodeLine{1473   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_remap)}
\DoxyCodeLine{1474 }
\DoxyCodeLine{1475   \textcolor{comment}{! Changes made to following fields:  h, tv\%T and tv\%S.}}
\DoxyCodeLine{1476   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1477     h\_prebound(i,j,k) = h(i,j,k)}
\DoxyCodeLine{1478 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1479   \textcolor{keywordflow}{if} (cs\%use\_energetic\_PBL) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1480 }
\DoxyCodeLine{1481     skinbuoyflux(:,:) = 0.0}
\DoxyCodeLine{1482     \textcolor{keyword}{call }applyboundaryfluxesinout(cs\%diabatic\_aux\_CSp, g, gv, us, dt, fluxes, cs\%optics, \&}
\DoxyCodeLine{1483             optics\_nbands(cs\%optics), h, tv, cs\%aggregate\_FW\_forcing, cs\%evap\_CFL\_limit, \&}
\DoxyCodeLine{1484             cs\%minimum\_forcing\_depth, ctke, dsv\_dt, dsv\_ds, skinbuoyflux=skinbuoyflux)}
\DoxyCodeLine{1485 }
\DoxyCodeLine{1486     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1487       \textcolor{keyword}{call }hchksum(ea\_t, \textcolor{stringliteral}{"after applyBoundaryFluxes ea\_t"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1488       \textcolor{keyword}{call }hchksum(eb\_t, \textcolor{stringliteral}{"after applyBoundaryFluxes eb\_t"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1489       \textcolor{keyword}{call }hchksum(ea\_s, \textcolor{stringliteral}{"after applyBoundaryFluxes ea\_s"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1490       \textcolor{keyword}{call }hchksum(eb\_s, \textcolor{stringliteral}{"after applyBoundaryFluxes eb\_s"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1491       \textcolor{keyword}{call }hchksum(ctke, \textcolor{stringliteral}{"after applyBoundaryFluxes cTKE"}, g\%HI, haloshift=0, \&}
\DoxyCodeLine{1492                    scale=us\%RZ3\_T3\_to\_W\_m2*us\%T\_to\_s)}
\DoxyCodeLine{1493       \textcolor{keyword}{call }hchksum(dsv\_dt, \textcolor{stringliteral}{"after applyBoundaryFluxes dSV\_dT"}, g\%HI, haloshift=0, scale=us\%kg\_m3\_to\_R)}
\DoxyCodeLine{1494       \textcolor{keyword}{call }hchksum(dsv\_ds, \textcolor{stringliteral}{"after applyBoundaryFluxes dSV\_dS"}, g\%HI, haloshift=0, scale=us\%kg\_m3\_to\_R)}
\DoxyCodeLine{1495 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1496 }
\DoxyCodeLine{1497     \textcolor{keyword}{call }find\_uv\_at\_h(u, v, h, u\_h, v\_h, g, gv, us)}
\DoxyCodeLine{1498     \textcolor{keyword}{call }energetic\_pbl(h, u\_h, v\_h, tv, fluxes, dt, kd\_epbl, g, gv, us, \&}
\DoxyCodeLine{1499                        cs\%energetic\_PBL\_CSp, dsv\_dt, dsv\_ds, ctke, skinbuoyflux, waves=waves)}
\DoxyCodeLine{1500 }
\DoxyCodeLine{1501     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(hml)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1502       \textcolor{keyword}{call }energetic\_pbl\_get\_mld(cs\%energetic\_PBL\_CSp, hml(:,:), g, us)}
\DoxyCodeLine{1503       \textcolor{keyword}{call }pass\_var(hml, g\%domain, halo=1)}
\DoxyCodeLine{1504       \textcolor{comment}{! If visc\%MLD exists, copy ePBL's MLD into it}}
\DoxyCodeLine{1505       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%MLD)) visc\%MLD(:,:) = hml(:,:)}
\DoxyCodeLine{1506     \textcolor{keywordflow}{elseif} (\textcolor{keyword}{associated}(visc\%MLD)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1507       \textcolor{keyword}{call }energetic\_pbl\_get\_mld(cs\%energetic\_PBL\_CSp, visc\%MLD, g, us)}
\DoxyCodeLine{1508       \textcolor{keyword}{call }pass\_var(visc\%MLD, g\%domain, halo=1)}
\DoxyCodeLine{1509 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1510 }
\DoxyCodeLine{1511     \textcolor{comment}{! Augment the diffusivities and viscosity due to those diagnosed in energetic\_PBL.}}
\DoxyCodeLine{1512     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1513       \textcolor{keywordflow}{if} (cs\%ePBL\_is\_additive) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1514         kd\_add\_here = kd\_epbl(i,j,k)}
\DoxyCodeLine{1515         visc\%Kv\_shear(i,j,k) = visc\%Kv\_shear(i,j,k) + cs\%ePBL\_Prandtl*kd\_epbl(i,j,k)}
\DoxyCodeLine{1516       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1517         kd\_add\_here = max(kd\_epbl(i,j,k) -\/ visc\%Kd\_shear(i,j,k), 0.0)}
\DoxyCodeLine{1518         visc\%Kv\_shear(i,j,k) = max(visc\%Kv\_shear(i,j,k), cs\%ePBL\_Prandtl*kd\_epbl(i,j,k))}
\DoxyCodeLine{1519 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1520 }
\DoxyCodeLine{1521       kd\_heat(i,j,k) = kd\_heat(i,j,k) + kd\_add\_here}
\DoxyCodeLine{1522       kd\_salt(i,j,k) = kd\_salt(i,j,k) + kd\_add\_here}
\DoxyCodeLine{1523 }
\DoxyCodeLine{1524 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1525 }
\DoxyCodeLine{1526     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1527       \textcolor{keyword}{call }hchksum(ea\_t, \textcolor{stringliteral}{"after ePBL ea\_t"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1528       \textcolor{keyword}{call }hchksum(eb\_t, \textcolor{stringliteral}{"after ePBL eb\_t"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1529       \textcolor{keyword}{call }hchksum(ea\_s, \textcolor{stringliteral}{"after ePBL ea\_s"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1530       \textcolor{keyword}{call }hchksum(eb\_s, \textcolor{stringliteral}{"after ePBL eb\_s"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1531       \textcolor{keyword}{call }hchksum(kd\_epbl, \textcolor{stringliteral}{"after ePBL Kd\_ePBL"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{1532 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1533 }
\DoxyCodeLine{1534   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1535     \textcolor{keyword}{call }applyboundaryfluxesinout(cs\%diabatic\_aux\_CSp, g, gv, us, dt, fluxes, cs\%optics, \&}
\DoxyCodeLine{1536                                   optics\_nbands(cs\%optics), h, tv, cs\%aggregate\_FW\_forcing, \&}
\DoxyCodeLine{1537                                   cs\%evap\_CFL\_limit, cs\%minimum\_forcing\_depth)}
\DoxyCodeLine{1538 }
\DoxyCodeLine{1539 \textcolor{keywordflow}{  endif}   \textcolor{comment}{! endif for CS\%use\_energetic\_PBL}}
\DoxyCodeLine{1540 }
\DoxyCodeLine{1541   \textcolor{comment}{! diagnose the tendencies due to boundary forcing}}
\DoxyCodeLine{1542   \textcolor{comment}{! At this point, the diagnostic grids have not been updated since the call to the boundary layer scheme}}
\DoxyCodeLine{1543   \textcolor{comment}{!  so all tendency diagnostics need to be posted on h\_diag, and grids rebuilt afterwards}}
\DoxyCodeLine{1544   \textcolor{keywordflow}{if} (cs\%boundary\_forcing\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1545     \textcolor{keyword}{call }diagnose\_boundary\_forcing\_tendency(tv, h, temp\_diag, saln\_diag, h\_diag, dt, g, gv, us, cs)}
\DoxyCodeLine{1546     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_h > 0) \textcolor{keyword}{call }post\_data(cs\%id\_boundary\_forcing\_h, h, cs\%diag, alt\_h = h\_diag)}
\DoxyCodeLine{1547 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1548   \textcolor{comment}{! Boundary fluxes may have changed T, S, and h}}
\DoxyCodeLine{1549   \textcolor{keyword}{call }diag\_update\_remap\_grids(cs\%diag)}
\DoxyCodeLine{1550   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_remap)}
\DoxyCodeLine{1551   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1552     \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after applyBoundaryFluxes "}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{1553     \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after applyBoundaryFluxes "}, tv, g)}
\DoxyCodeLine{1554     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after applyBoundaryFluxes "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{1555 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1556   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with applyBoundaryFluxes (diabatic)"})}
\DoxyCodeLine{1557   \textcolor{keywordflow}{if} (cs\%debugConservation)  \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'applyBoundaryFluxes'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{1558 }
\DoxyCodeLine{1559   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with h=ea-\/eb (diabatic)"})}
\DoxyCodeLine{1560   \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'h=ea-\/eb'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{1561 }
\DoxyCodeLine{1562   \textcolor{comment}{! calculate change in temperature \& salinity due to dia-\/coordinate surface diffusion}}
\DoxyCodeLine{1563   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%T)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1564 }
\DoxyCodeLine{1565     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1566       \textcolor{keyword}{call }hchksum(ea\_t, \textcolor{stringliteral}{"before triDiagTS ea\_t "}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1567       \textcolor{keyword}{call }hchksum(eb\_t, \textcolor{stringliteral}{"before triDiagTS eb\_t "}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1568       \textcolor{keyword}{call }hchksum(ea\_s, \textcolor{stringliteral}{"before triDiagTS ea\_s "}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1569       \textcolor{keyword}{call }hchksum(eb\_s, \textcolor{stringliteral}{"before triDiagTS eb\_s "}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{1570 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1571 }
\DoxyCodeLine{1572     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_tridiag)}
\DoxyCodeLine{1573     \textcolor{comment}{!  Keep salinity from falling below a small but positive threshold.}}
\DoxyCodeLine{1574     \textcolor{comment}{!  This constraint is needed for SIS1 ice model, which can extract}}
\DoxyCodeLine{1575     \textcolor{comment}{!  more salt than is present in the ocean. SIS2 does not suffer}}
\DoxyCodeLine{1576     \textcolor{comment}{!  from this limitation, in which case we can let salinity=0 and still}}
\DoxyCodeLine{1577     \textcolor{comment}{!  have salt conserved with SIS2 ice. So for SIS2, we can run with}}
\DoxyCodeLine{1578     \textcolor{comment}{!  BOUND\_SALINITY=False in MOM.F90.}}
\DoxyCodeLine{1579     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%S) .and. \textcolor{keyword}{associated}(tv\%salt\_deficit)) \&}
\DoxyCodeLine{1580       \textcolor{keyword}{call }adjust\_salt(h, tv, g, gv, cs\%diabatic\_aux\_CSp)}
\DoxyCodeLine{1581 }
\DoxyCodeLine{1582     \textcolor{keywordflow}{if} (cs\%diabatic\_diff\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1583       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1584         temp\_diag(i,j,k) = tv\%T(i,j,k)}
\DoxyCodeLine{1585         saln\_diag(i,j,k) = tv\%S(i,j,k)}
\DoxyCodeLine{1586 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1587 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1588 }
\DoxyCodeLine{1589     \textcolor{comment}{! set ea\_t=eb\_t=Kd\_heat and ea\_s=eb\_s=Kd\_salt on interfaces for use in the tridiagonal solver.}}
\DoxyCodeLine{1590     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1591       ea\_t(i,j,1) = 0.; ea\_s(i,j,1) = 0.}
\DoxyCodeLine{1592 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1593 }
\DoxyCodeLine{1594     \textcolor{comment}{!\$OMP parallel do default(shared) private(hval)}}
\DoxyCodeLine{1595     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1596       hval = 1.0 / (h\_neglect + 0.5*(h(i,j,k-\/1) + h(i,j,k)))}
\DoxyCodeLine{1597       ea\_t(i,j,k) = (gv\%Z\_to\_H**2) * dt * hval * kd\_heat(i,j,k)}
\DoxyCodeLine{1598       eb\_t(i,j,k-\/1) = ea\_t(i,j,k)}
\DoxyCodeLine{1599       ea\_s(i,j,k) = (gv\%Z\_to\_H**2) * dt * hval * kd\_salt(i,j,k)}
\DoxyCodeLine{1600       eb\_s(i,j,k-\/1) = ea\_s(i,j,k)}
\DoxyCodeLine{1601 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1602     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1603       eb\_t(i,j,nz) = 0. ; eb\_s(i,j,nz) = 0.}
\DoxyCodeLine{1604 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1605     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done setting ea\_t,ea\_s,eb\_t,eb\_s from Kd\_heat"} //\&}
\DoxyCodeLine{1606        \textcolor{stringliteral}{"and Kd\_salt (diabatic)"})}
\DoxyCodeLine{1607 }
\DoxyCodeLine{1608     \textcolor{comment}{! Initialize halo regions of ea\_t, eb\_t, ea\_s and eb\_s to default values.}}
\DoxyCodeLine{1609     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{1610     \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{1611       \textcolor{keywordflow}{do} i=is-\/1,ie+1}
\DoxyCodeLine{1612         ea\_t(i,js-\/1,k) = 0.0 ; eb\_t(i,js-\/1,k) = 0.0}
\DoxyCodeLine{1613         ea\_s(i,js-\/1,k) = 0.0 ; eb\_s(i,js-\/1,k) = 0.0}
\DoxyCodeLine{1614         ea\_t(i,je+1,k) = 0.0 ; eb\_t(i,je+1,k) = 0.0}
\DoxyCodeLine{1615         ea\_s(i,je+1,k) = 0.0 ; eb\_s(i,je+1,k) = 0.0}
\DoxyCodeLine{1616 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1617       \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{1618         ea\_t(is-\/1,j,k) = 0.0 ; eb\_t(is-\/1,j,k) = 0.0}
\DoxyCodeLine{1619         ea\_s(is-\/1,j,k) = 0.0 ; eb\_s(is-\/1,j,k) = 0.0}
\DoxyCodeLine{1620         ea\_t(ie+1,j,k) = 0.0 ; eb\_t(ie+1,j,k) = 0.0}
\DoxyCodeLine{1621         ea\_s(ie+1,j,k) = 0.0 ; eb\_s(ie+1,j,k) = 0.0}
\DoxyCodeLine{1622 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1623 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1624 }
\DoxyCodeLine{1625   \textcolor{comment}{! Changes T and S via the tridiagonal solver; no change to h}}
\DoxyCodeLine{1626     \textcolor{keyword}{call }tracer\_vertdiff(h, ea\_t, eb\_t, dt, tv\%T, g, gv)}
\DoxyCodeLine{1627     \textcolor{keyword}{call }tracer\_vertdiff(h, ea\_s, eb\_s, dt, tv\%S, g, gv)}
\DoxyCodeLine{1628 }
\DoxyCodeLine{1629 }
\DoxyCodeLine{1630     \textcolor{comment}{! In ALE-\/mode, layer thicknesses do not change. Therefore, we can use h below}}
\DoxyCodeLine{1631     \textcolor{keywordflow}{if} (cs\%diabatic\_diff\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1632       \textcolor{keyword}{call }diagnose\_diabatic\_diff\_tendency(tv, h, temp\_diag, saln\_diag, dt, g, gv, us, cs)}
\DoxyCodeLine{1633 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1634     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_tridiag)}
\DoxyCodeLine{1635 }
\DoxyCodeLine{1636     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with triDiagTS (diabatic)"})}
\DoxyCodeLine{1637 }
\DoxyCodeLine{1638 \textcolor{keywordflow}{  endif}  \textcolor{comment}{! endif corresponding to if (associated(tv\%T))}}
\DoxyCodeLine{1639 }
\DoxyCodeLine{1640   \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'triDiagTS'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{1641 }
\DoxyCodeLine{1642   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1643     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after mixed layer "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{1644     \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after mixed layer "}, tv, g)}
\DoxyCodeLine{1645 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1646 }
\DoxyCodeLine{1647   \textcolor{comment}{! Whenever thickness changes let the diag manager know, as the}}
\DoxyCodeLine{1648   \textcolor{comment}{! target grids for vertical remapping may need to be regenerated.}}
\DoxyCodeLine{1649   \textcolor{keyword}{call }diag\_update\_remap\_grids(cs\%diag)}
\DoxyCodeLine{1650 }
\DoxyCodeLine{1651   \textcolor{comment}{! diagnostics}}
\DoxyCodeLine{1652   idt = 1.0 / dt}
\DoxyCodeLine{1653   \textcolor{keywordflow}{if} ((cs\%id\_Tdif > 0) .or. (cs\%id\_Tadv > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1654     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1655       tdif\_flx(i,j,1) = 0.0 ; tdif\_flx(i,j,nz+1) = 0.0}
\DoxyCodeLine{1656       tadv\_flx(i,j,1) = 0.0 ; tadv\_flx(i,j,nz+1) = 0.0}
\DoxyCodeLine{1657 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1658     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{1659     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1660       tdif\_flx(i,j,k) = (idt * 0.5*(ea\_t(i,j,k) + eb\_t(i,j,k-\/1))) * \&}
\DoxyCodeLine{1661                         (tv\%T(i,j,k-\/1) -\/ tv\%T(i,j,k))}
\DoxyCodeLine{1662       tadv\_flx(i,j,k) = (idt * (ea\_t(i,j,k) -\/ eb\_t(i,j,k-\/1))) * \&}
\DoxyCodeLine{1663                     0.5*(tv\%T(i,j,k-\/1) + tv\%T(i,j,k))}
\DoxyCodeLine{1664 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1665 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1666   \textcolor{keywordflow}{if} ((cs\%id\_Sdif > 0) .or. (cs\%id\_Sadv > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1667     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1668       sdif\_flx(i,j,1) = 0.0 ; sdif\_flx(i,j,nz+1) = 0.0}
\DoxyCodeLine{1669       sadv\_flx(i,j,1) = 0.0 ; sadv\_flx(i,j,nz+1) = 0.0}
\DoxyCodeLine{1670 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1671     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{1672     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1673       sdif\_flx(i,j,k) = (idt * 0.5*(ea\_s(i,j,k) + eb\_s(i,j,k-\/1))) * \&}
\DoxyCodeLine{1674                         (tv\%S(i,j,k-\/1) -\/ tv\%S(i,j,k))}
\DoxyCodeLine{1675       sadv\_flx(i,j,k) = (idt * (ea\_s(i,j,k) -\/ eb\_s(i,j,k-\/1))) * \&}
\DoxyCodeLine{1676                     0.5*(tv\%S(i,j,k-\/1) + tv\%S(i,j,k))}
\DoxyCodeLine{1677 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1678 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1679 }
\DoxyCodeLine{1680   \textcolor{comment}{! mixing of passive tracers from massless boundary layers to interior}}
\DoxyCodeLine{1681   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_tracers)}
\DoxyCodeLine{1682 }
\DoxyCodeLine{1683   \textcolor{keywordflow}{if} (cs\%mix\_boundary\_tracers) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1684     tr\_ea\_bbl = gv\%Z\_to\_H * sqrt(dt*cs\%Kd\_BBL\_tr)}
\DoxyCodeLine{1685     \textcolor{comment}{!\$OMP parallel do default(shared) private(htot,in\_boundary,add\_ent)}}
\DoxyCodeLine{1686     \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{1687       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1688         ebtr(i,j,nz) = eb\_s(i,j,nz)}
\DoxyCodeLine{1689         htot(i) = 0.0}
\DoxyCodeLine{1690         in\_boundary(i) = (g\%mask2dT(i,j) > 0.0)}
\DoxyCodeLine{1691 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1692       \textcolor{keywordflow}{do} k=nz,2,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1693         \textcolor{keywordflow}{if} (in\_boundary(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1694           htot(i) = htot(i) + h(i,j,k)}
\DoxyCodeLine{1695           \textcolor{comment}{!   If diapycnal mixing has been suppressed because this is a massless}}
\DoxyCodeLine{1696           \textcolor{comment}{! layer near the bottom, add some mixing of tracers between these}}
\DoxyCodeLine{1697           \textcolor{comment}{! layers.  This flux is based on the harmonic mean of the two}}
\DoxyCodeLine{1698           \textcolor{comment}{! thicknesses, as this corresponds pretty closely (to within}}
\DoxyCodeLine{1699           \textcolor{comment}{! differences in the density jumps between layers) with what is done}}
\DoxyCodeLine{1700           \textcolor{comment}{! in the calculation of the fluxes in the first place.  Kd\_min\_tr}}
\DoxyCodeLine{1701           \textcolor{comment}{! should be much less than the values that have been set in Kd\_int,}}
\DoxyCodeLine{1702           \textcolor{comment}{! perhaps a molecular diffusivity.}}
\DoxyCodeLine{1703           add\_ent = ((dt * cs\%Kd\_min\_tr) * gv\%Z\_to\_H**2) * \&}
\DoxyCodeLine{1704                     ((h(i,j,k-\/1)+h(i,j,k)+h\_neglect) / \&}
\DoxyCodeLine{1705                      (h(i,j,k-\/1)*h(i,j,k)+h\_neglect2)) -\/ \&}
\DoxyCodeLine{1706                     0.5*(ea\_s(i,j,k) + eb\_s(i,j,k-\/1))}
\DoxyCodeLine{1707           \textcolor{keywordflow}{if} (htot(i) < tr\_ea\_bbl) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1708             add\_ent = max(0.0, add\_ent, \&}
\DoxyCodeLine{1709                           (tr\_ea\_bbl -\/ htot(i)) -\/ min(ea\_s(i,j,k),eb\_s(i,j,k-\/1)))}
\DoxyCodeLine{1710           \textcolor{keywordflow}{elseif} (add\_ent < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1711             add\_ent = 0.0 ; in\_boundary(i) = .false.}
\DoxyCodeLine{1712 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1713 }
\DoxyCodeLine{1714           ebtr(i,j,k-\/1) = eb\_s(i,j,k-\/1) + add\_ent}
\DoxyCodeLine{1715           eatr(i,j,k) = ea\_s(i,j,k) + add\_ent}
\DoxyCodeLine{1716         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1717           ebtr(i,j,k-\/1) = eb\_s(i,j,k-\/1) ; eatr(i,j,k) = ea\_s(i,j,k)}
\DoxyCodeLine{1718 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1719 }
\DoxyCodeLine{1720         \textcolor{keywordflow}{if} (cs\%double\_diffuse) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (kd\_extra\_s(i,j,k) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1721           add\_ent = ((dt * kd\_extra\_s(i,j,k)) * gv\%Z\_to\_H**2) / \&}
\DoxyCodeLine{1722              (0.5 * (h(i,j,k-\/1) + h(i,j,k)) + \&}
\DoxyCodeLine{1723               h\_neglect)}
\DoxyCodeLine{1724           ebtr(i,j,k-\/1) = ebtr(i,j,k-\/1) + add\_ent}
\DoxyCodeLine{1725           eatr(i,j,k) = eatr(i,j,k) + add\_ent}
\DoxyCodeLine{1726 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1727 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1728       \textcolor{keywordflow}{do} i=is,ie ; eatr(i,j,1) = ea\_s(i,j,1) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1729 }
\DoxyCodeLine{1730 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1731 }
\DoxyCodeLine{1732     \textcolor{comment}{! For passive tracers, the changes in thickness due to boundary fluxes has yet to be applied}}
\DoxyCodeLine{1733     \textcolor{comment}{! so use h\_prebound as the old value.}}
\DoxyCodeLine{1734     \textcolor{comment}{!\#\#\# I think that in the following, ea\_s and eb\_s should be eatr and ebtr.}}
\DoxyCodeLine{1735     \textcolor{keyword}{call }call\_tracer\_column\_fns(h\_prebound, h, ea\_s, eb\_s, fluxes, hml, dt, g, gv, us, tv, \&}
\DoxyCodeLine{1736                               cs\%optics, cs\%tracer\_flow\_CSp, cs\%debug, \&}
\DoxyCodeLine{1737                               evap\_cfl\_limit = cs\%evap\_CFL\_limit, \&}
\DoxyCodeLine{1738                               minimum\_forcing\_depth=cs\%minimum\_forcing\_depth)}
\DoxyCodeLine{1739 }
\DoxyCodeLine{1740   \textcolor{keywordflow}{elseif} (cs\%double\_diffuse) \textcolor{keywordflow}{then}  \textcolor{comment}{! extra diffusivity for passive tracers}}
\DoxyCodeLine{1741 }
\DoxyCodeLine{1742     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1743       ebtr(i,j,nz) = eb\_s(i,j,nz) ; eatr(i,j,1) = ea\_s(i,j,1)}
\DoxyCodeLine{1744 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1745     \textcolor{comment}{!\$OMP parallel do default(shared) private(add\_ent)}}
\DoxyCodeLine{1746     \textcolor{keywordflow}{do} k=nz,2,-\/1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1747       \textcolor{keywordflow}{if} (kd\_extra\_s(i,j,k) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1748         add\_ent = ((dt * kd\_extra\_s(i,j,k)) * gv\%Z\_to\_H**2) / \&}
\DoxyCodeLine{1749            (0.5 * (h(i,j,k-\/1) + h(i,j,k))  + \&}
\DoxyCodeLine{1750             h\_neglect)}
\DoxyCodeLine{1751       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1752         add\_ent = 0.0}
\DoxyCodeLine{1753 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1754       ebtr(i,j,k-\/1) = eb\_s(i,j,k-\/1) + add\_ent}
\DoxyCodeLine{1755       eatr(i,j,k) = ea\_s(i,j,k) + add\_ent}
\DoxyCodeLine{1756 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1757 }
\DoxyCodeLine{1758     \textcolor{comment}{! For passive tracers, the changes in thickness due to boundary fluxes has yet to be applied}}
\DoxyCodeLine{1759     \textcolor{keyword}{call }call\_tracer\_column\_fns(h\_prebound, h, eatr, ebtr, fluxes, hml, dt, g, gv, us, tv, \&}
\DoxyCodeLine{1760                                 cs\%optics, cs\%tracer\_flow\_CSp, cs\%debug,\&}
\DoxyCodeLine{1761                                 evap\_cfl\_limit = cs\%evap\_CFL\_limit, \&}
\DoxyCodeLine{1762                                 minimum\_forcing\_depth=cs\%minimum\_forcing\_depth)}
\DoxyCodeLine{1763   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1764     \textcolor{comment}{! For passive tracers, the changes in thickness due to boundary fluxes has yet to be applied}}
\DoxyCodeLine{1765     \textcolor{comment}{!\#\#\# eatr and ebtr may not be initialized or may be 0, depending on CS\%geothermal.}}
\DoxyCodeLine{1766     \textcolor{keyword}{call }call\_tracer\_column\_fns(h\_prebound, h, eatr, ebtr, fluxes, hml, dt, g, gv, us, tv, \&}
\DoxyCodeLine{1767                                 cs\%optics, cs\%tracer\_flow\_CSp, cs\%debug, \&}
\DoxyCodeLine{1768                                 evap\_cfl\_limit = cs\%evap\_CFL\_limit, \&}
\DoxyCodeLine{1769                                 minimum\_forcing\_depth=cs\%minimum\_forcing\_depth)}
\DoxyCodeLine{1770 \textcolor{keywordflow}{  endif}  \textcolor{comment}{! (CS\%mix\_boundary\_tracers)}}
\DoxyCodeLine{1771 }
\DoxyCodeLine{1772   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_tracers)}
\DoxyCodeLine{1773 }
\DoxyCodeLine{1774   \textcolor{comment}{! Apply ALE sponge}}
\DoxyCodeLine{1775   \textcolor{keywordflow}{if} (cs\%use\_sponge) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1776     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_sponge)}
\DoxyCodeLine{1777     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs\%ALE\_sponge\_CSp)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1778       \textcolor{keyword}{call }apply\_ale\_sponge(h, dt, g, gv, us, cs\%ALE\_sponge\_CSp, cs\%Time)}
\DoxyCodeLine{1779 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1780 }
\DoxyCodeLine{1781     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_sponge)}
\DoxyCodeLine{1782     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1783       \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"apply\_sponge "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{1784       \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"apply\_sponge "}, tv, g)}
\DoxyCodeLine{1785 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1786 \textcolor{keywordflow}{  endif} \textcolor{comment}{! CS\%use\_sponge}}
\DoxyCodeLine{1787 }
\DoxyCodeLine{1788   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_pass)}
\DoxyCodeLine{1789   \textcolor{keywordflow}{if} (g\%symmetric) \textcolor{keywordflow}{then} ; dir\_flag = to\_all+omit\_corners}
\DoxyCodeLine{1790   \textcolor{keywordflow}{else} ; dir\_flag = to\_west+to\_south+omit\_corners ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1791   \textcolor{keyword}{call }create\_group\_pass(cs\%pass\_hold\_eb\_ea, eb\_t, g\%Domain, dir\_flag, halo=1)}
\DoxyCodeLine{1792   \textcolor{keyword}{call }create\_group\_pass(cs\%pass\_hold\_eb\_ea, eb\_s, g\%Domain, dir\_flag, halo=1)}
\DoxyCodeLine{1793   \textcolor{keyword}{call }create\_group\_pass(cs\%pass\_hold\_eb\_ea, ea\_t, g\%Domain, dir\_flag, halo=1)}
\DoxyCodeLine{1794   \textcolor{keyword}{call }create\_group\_pass(cs\%pass\_hold\_eb\_ea, ea\_s, g\%Domain, dir\_flag, halo=1)}
\DoxyCodeLine{1795   \textcolor{keyword}{call }do\_group\_pass(cs\%pass\_hold\_eb\_ea, g\%Domain)}
\DoxyCodeLine{1796   \textcolor{comment}{! visc\%Kv\_slow is not in the group pass because it has larger vertical extent.}}
\DoxyCodeLine{1797   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Kv\_slow)) \&}
\DoxyCodeLine{1798     \textcolor{keyword}{call }pass\_var(visc\%Kv\_slow, g\%Domain, to\_all+omit\_corners, halo=1)}
\DoxyCodeLine{1799   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_pass)}
\DoxyCodeLine{1800 }
\DoxyCodeLine{1801   \textcolor{keyword}{call }disable\_averaging(cs\%diag)}
\DoxyCodeLine{1802 }
\DoxyCodeLine{1803   \textcolor{comment}{! Diagnose the diapycnal diffusivities and other related quantities.}}
\DoxyCodeLine{1804   \textcolor{keyword}{call }enable\_averages(dt, time\_end, cs\%diag)}
\DoxyCodeLine{1805 }
\DoxyCodeLine{1806   \textcolor{keywordflow}{if} (cs\%id\_Kd\_interface > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_interface, kd\_int,  cs\%diag)}
\DoxyCodeLine{1807   \textcolor{keywordflow}{if} (cs\%id\_Kd\_heat      > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_heat,      kd\_heat, cs\%diag)}
\DoxyCodeLine{1808   \textcolor{keywordflow}{if} (cs\%id\_Kd\_salt      > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_salt,      kd\_salt, cs\%diag)}
\DoxyCodeLine{1809   \textcolor{keywordflow}{if} (cs\%id\_Kd\_ePBL      > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_ePBL,      kd\_epbl, cs\%diag)}
\DoxyCodeLine{1810 }
\DoxyCodeLine{1811   \textcolor{keywordflow}{if} (cs\%id\_ea\_t       > 0) \textcolor{keyword}{call }post\_data(cs\%id\_ea\_t,       ea\_t, cs\%diag)}
\DoxyCodeLine{1812   \textcolor{keywordflow}{if} (cs\%id\_eb\_t       > 0) \textcolor{keyword}{call }post\_data(cs\%id\_eb\_t,       eb\_t, cs\%diag)}
\DoxyCodeLine{1813   \textcolor{keywordflow}{if} (cs\%id\_ea\_s       > 0) \textcolor{keyword}{call }post\_data(cs\%id\_ea\_s,       ea\_s, cs\%diag)}
\DoxyCodeLine{1814   \textcolor{keywordflow}{if} (cs\%id\_eb\_s       > 0) \textcolor{keyword}{call }post\_data(cs\%id\_eb\_s,       eb\_s, cs\%diag)}
\DoxyCodeLine{1815 }
\DoxyCodeLine{1816   \textcolor{keywordflow}{if} (cs\%id\_dudt\_dia > 0) \textcolor{keyword}{call }post\_data(cs\%id\_dudt\_dia, adp\%du\_dt\_dia,  cs\%diag)}
\DoxyCodeLine{1817   \textcolor{keywordflow}{if} (cs\%id\_dvdt\_dia > 0) \textcolor{keyword}{call }post\_data(cs\%id\_dvdt\_dia, adp\%dv\_dt\_dia,  cs\%diag)}
\DoxyCodeLine{1818 }
\DoxyCodeLine{1819   \textcolor{keywordflow}{if} (cs\%id\_Tdif > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Tdif, tdif\_flx, cs\%diag)}
\DoxyCodeLine{1820   \textcolor{keywordflow}{if} (cs\%id\_Tadv > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Tadv, tadv\_flx, cs\%diag)}
\DoxyCodeLine{1821   \textcolor{keywordflow}{if} (cs\%id\_Sdif > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Sdif, sdif\_flx, cs\%diag)}
\DoxyCodeLine{1822   \textcolor{keywordflow}{if} (cs\%id\_Sadv > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Sadv, sadv\_flx, cs\%diag)}
\DoxyCodeLine{1823 }
\DoxyCodeLine{1824   \textcolor{comment}{!! Diagnostics for terms multiplied by fractional thicknesses}}
\DoxyCodeLine{1825   \textcolor{keywordflow}{if} (cs\%id\_hf\_dudt\_dia\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1826     \textcolor{keyword}{allocate}(hf\_dudt\_dia\_2d(g\%IsdB:g\%IedB,g\%jsd:g\%jed))}
\DoxyCodeLine{1827     hf\_dudt\_dia\_2d(:,:) = 0.0}
\DoxyCodeLine{1828     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{1829       hf\_dudt\_dia\_2d(i,j) = hf\_dudt\_dia\_2d(i,j) + adp\%du\_dt\_dia(i,j,k) * adp\%diag\_hfrac\_u(i,j,k)}
\DoxyCodeLine{1830 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1831     \textcolor{keyword}{call }post\_data(cs\%id\_hf\_dudt\_dia\_2d, hf\_dudt\_dia\_2d, cs\%diag)}
\DoxyCodeLine{1832     \textcolor{keyword}{deallocate}(hf\_dudt\_dia\_2d)}
\DoxyCodeLine{1833 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1834 }
\DoxyCodeLine{1835   \textcolor{keywordflow}{if} (cs\%id\_hf\_dvdt\_dia\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1836     \textcolor{keyword}{allocate}(hf\_dvdt\_dia\_2d(g\%isd:g\%ied,g\%JsdB:g\%JedB))}
\DoxyCodeLine{1837     hf\_dvdt\_dia\_2d(:,:) = 0.0}
\DoxyCodeLine{1838     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=jsq,jeq ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1839       hf\_dvdt\_dia\_2d(i,j) = hf\_dvdt\_dia\_2d(i,j) + adp\%dv\_dt\_dia(i,j,k) * adp\%diag\_hfrac\_v(i,j,k)}
\DoxyCodeLine{1840 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1841     \textcolor{keyword}{call }post\_data(cs\%id\_hf\_dvdt\_dia\_2d, hf\_dvdt\_dia\_2d, cs\%diag)}
\DoxyCodeLine{1842     \textcolor{keyword}{deallocate}(hf\_dvdt\_dia\_2d)}
\DoxyCodeLine{1843 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1844 }
\DoxyCodeLine{1845   \textcolor{keyword}{call }disable\_averaging(cs\%diag)}
\DoxyCodeLine{1846 }
\DoxyCodeLine{1847   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_leave(\textcolor{stringliteral}{"diabatic\_ALE()"})}
\DoxyCodeLine{1848 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diabatic__driver_acf7394d08f436dd9575b568d1f18e18a}\label{namespacemom__diabatic__driver_acf7394d08f436dd9575b568d1f18e18a}} 
\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}!diabatic\_ale\_legacy@{diabatic\_ale\_legacy}}
\index{diabatic\_ale\_legacy@{diabatic\_ale\_legacy}!mom\_diabatic\_driver@{mom\_diabatic\_driver}}
\doxysubsubsection{\texorpdfstring{diabatic\_ale\_legacy()}{diabatic\_ale\_legacy()}}
{\footnotesize\ttfamily subroutine mom\+\_\+diabatic\+\_\+driver\+::diabatic\+\_\+ale\+\_\+legacy (\begin{DoxyParamCaption}\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed, g \%ke), intent(inout)}]{u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb, g \%ke), intent(inout)}]{v,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(inout)}]{h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(inout)}]{tv,  }\item[{real, dimension(\+:,\+:), pointer}]{Hml,  }\item[{type(forcing), intent(inout)}]{fluxes,  }\item[{type(vertvisc\+\_\+type), intent(inout)}]{visc,  }\item[{type(accel\+\_\+diag\+\_\+ptrs), intent(inout)}]{A\+Dp,  }\item[{type(cont\+\_\+diag\+\_\+ptrs), intent(inout)}]{C\+Dp,  }\item[{real, intent(in)}]{dt,  }\item[{type(time\+\_\+type), intent(in)}]{Time\+\_\+end,  }\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__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), pointer}]{CS,  }\item[{type(wave\+\_\+parameters\+\_\+cs), optional, pointer}]{Waves }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Applies diabatic forcing and diapycnal mixing of temperature, salinity and other tracers for use with an A\+LE algorithm. This version uses an older set of algorithms compared with diabatic\+\_\+\+A\+LE. 


\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 us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in,out}}  & {\em u} & zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em v} & meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em h} & thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em tv} & points to thermodynamic fields unused have N\+U\+LL ptrs \\
\hline
 & {\em hml} & Active mixed layer depth \mbox{[}Z $\sim$$>$ m\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em fluxes} & points to forcing fields unused fields have N\+U\+LL ptrs \\
\hline
\mbox{\texttt{ in,out}}  & {\em visc} & vertical viscosities, B\+BL properies, and \\
\hline
\mbox{\texttt{ in,out}}  & {\em adp} & related points to accelerations in momentum equations, to enable the later derived diagnostics, like energy budgets \\
\hline
\mbox{\texttt{ in,out}}  & {\em cdp} & points to terms in continuity equations \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & time increment \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em time\+\_\+end} & Time at the end of the interval \\
\hline
 & {\em cs} & module control structure \\
\hline
 & {\em waves} & Surface gravity waves \\
\hline
\end{DoxyParams}


Definition at line 453 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{453   \textcolor{keywordtype}{type}(ocean\_grid\_type),                     \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{         !< ocean grid structure}}
\DoxyCodeLine{454   \textcolor{keywordtype}{type}(verticalGrid\_type),                   \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{        !< ocean vertical grid structure}}
\DoxyCodeLine{455   \textcolor{keywordtype}{type}(unit\_scale\_type),                     \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{        !< A dimensional unit scaling type}}
\DoxyCodeLine{456 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(inout)} :: u\textcolor{comment}{         !< zonal velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{457 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(inout)} :: v\textcolor{comment}{         !< meridional velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{458 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{         !< thickness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{459   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),                     \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{        !< points to thermodynamic fields}}
\DoxyCodeLine{460 \textcolor{comment}{                                                                        !! unused have NULL ptrs}}
\DoxyCodeLine{461 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(:,:)},                      \textcolor{keywordtype}{pointer}       :: Hml\textcolor{comment}{       !< Active mixed layer depth [Z ~> m]}}
\DoxyCodeLine{462   \textcolor{keywordtype}{type}(forcing),                             \textcolor{keywordtype}{intent(inout)} :: fluxes\textcolor{comment}{    !< points to forcing fields}}
\DoxyCodeLine{463 \textcolor{comment}{                                                                        !! unused fields have NULL ptrs}}
\DoxyCodeLine{464   \textcolor{keywordtype}{type}(vertvisc\_type),                       \textcolor{keywordtype}{intent(inout)} :: visc\textcolor{comment}{      !< vertical viscosities, BBL properies, and}}
\DoxyCodeLine{465   \textcolor{keywordtype}{type}(accel\_diag\_ptrs),                     \textcolor{keywordtype}{intent(inout)} :: ADp\textcolor{comment}{       !< related points to accelerations in momentum}}
\DoxyCodeLine{466 \textcolor{comment}{                                                                        !! equations, to enable the later derived}}
\DoxyCodeLine{467 \textcolor{comment}{                                                                        !! diagnostics, like energy budgets}}
\DoxyCodeLine{468   \textcolor{keywordtype}{type}(cont\_diag\_ptrs),                      \textcolor{keywordtype}{intent(inout)} :: CDp\textcolor{comment}{       !< points to terms in continuity equations}}
\DoxyCodeLine{469 \textcolor{keywordtype}{  real},                                      \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{        !< time increment [T ~> s]}}
\DoxyCodeLine{470   \textcolor{keywordtype}{type}(time\_type),                           \textcolor{keywordtype}{intent(in)}    :: Time\_end\textcolor{comment}{  !< Time at the end of the interval}}
\DoxyCodeLine{471   \textcolor{keywordtype}{type}(diabatic\_CS),                         \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{        !< module control structure}}
\DoxyCodeLine{472   \textcolor{keywordtype}{type}(Wave\_parameters\_CS),        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}       :: Waves\textcolor{comment}{     !< Surface gravity waves}}
\DoxyCodeLine{473 }
\DoxyCodeLine{474   \textcolor{comment}{! local variables}}
\DoxyCodeLine{475 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))} :: \&}
\DoxyCodeLine{476     ea\_s,     \&  \textcolor{comment}{! amount of fluid entrained from the layer above within}}
\DoxyCodeLine{477                  \textcolor{comment}{! one time step  [H ~> m or kg m-\/2]}}
\DoxyCodeLine{478     eb\_s,     \&  \textcolor{comment}{! amount of fluid entrained from the layer below within}}
\DoxyCodeLine{479                  \textcolor{comment}{! one time step  [H ~> m or kg m-\/2]}}
\DoxyCodeLine{480     ea\_t,     \&  \textcolor{comment}{! amount of fluid entrained from the layer above within}}
\DoxyCodeLine{481                  \textcolor{comment}{! one time step  [H ~> m or kg m-\/2]}}
\DoxyCodeLine{482     eb\_t,     \&  \textcolor{comment}{! amount of fluid entrained from the layer below within}}
\DoxyCodeLine{483                  \textcolor{comment}{! one time step  [H ~> m or kg m-\/2]}}
\DoxyCodeLine{484     h\_orig, \&    \textcolor{comment}{! initial layer thicknesses [H ~> m or kg m-\/2]}}
\DoxyCodeLine{485     h\_prebound, \& \textcolor{comment}{! initial layer thicknesses [H ~> m or kg m-\/2]}}
\DoxyCodeLine{486     hold,   \&    \textcolor{comment}{! layer thickness before diapycnal entrainment, and later}}
\DoxyCodeLine{487                  \textcolor{comment}{! the initial layer thicknesses (if a mixed layer is used),}}
\DoxyCodeLine{488                  \textcolor{comment}{! [H ~> m or kg m-\/2]}}
\DoxyCodeLine{489     dsv\_dt, \&    \textcolor{comment}{! The partial derivative of specific volume with temperature [R-\/1 degC-\/1 ~> m3 kg-\/1 degC-\/1]}}
\DoxyCodeLine{490     dsv\_ds, \&    \textcolor{comment}{! The partial derivative of specific volume with salinity [R-\/1 ppt-\/1 ~> m3 kg-\/1 ppt-\/1].}}
\DoxyCodeLine{491     ctke,   \&    \textcolor{comment}{! convective TKE requirements for each layer [R Z3 T-\/2 ~> J m-\/2].}}
\DoxyCodeLine{492     u\_h,    \&    \textcolor{comment}{! zonal and meridional velocities at thickness points after}}
\DoxyCodeLine{493     v\_h          \textcolor{comment}{! entrainment [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{494 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: \&}
\DoxyCodeLine{495     SkinBuoyFlux\textcolor{comment}{! 2d surface buoyancy flux [Z2 T-\/3 ~> m2 s-\/3], used by ePBL}}
\DoxyCodeLine{496 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),G\%ke)} :: h\_diag                \textcolor{comment}{! diagnostic array for thickness}}
\DoxyCodeLine{497 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),G\%ke)} :: temp\_diag             \textcolor{comment}{! diagnostic array for temp}}
\DoxyCodeLine{498 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),G\%ke)} :: saln\_diag             \textcolor{comment}{! diagnostic array for salinity}}
\DoxyCodeLine{499 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))}      :: tendency\_2d           \textcolor{comment}{! depth integrated content tendency for diagn}}
\DoxyCodeLine{500 }
\DoxyCodeLine{501 \textcolor{keywordtype}{  real} :: net\_ent  \textcolor{comment}{! The net of ea-\/eb at an interface.}}
\DoxyCodeLine{502 }
\DoxyCodeLine{503 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))} :: \&}
\DoxyCodeLine{504     eatr, \&  \textcolor{comment}{! The equivalent of ea and eb for tracers, which differ from ea and}}
\DoxyCodeLine{505     ebtr     \textcolor{comment}{! eb in that they tend to homogenize tracers in massless layers}}
\DoxyCodeLine{506              \textcolor{comment}{! near the boundaries [H ~> m or kg m-\/2] (for Bous or non-\/Bouss)}}
\DoxyCodeLine{507 }
\DoxyCodeLine{508 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G)+1)} :: \&}
\DoxyCodeLine{509     Kd\_int,   \& \textcolor{comment}{! diapycnal diffusivity of interfaces [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{510     Kd\_heat,  \& \textcolor{comment}{! diapycnal diffusivity of heat [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{511     Kd\_salt,  \& \textcolor{comment}{! diapycnal diffusivity of salt and passive tracers [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{512     Kd\_extra\_T , \& \textcolor{comment}{! The extra diffusivity of temperature due to double diffusion relative to}}
\DoxyCodeLine{513                 \textcolor{comment}{! Kd\_int [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{514     kd\_extra\_s , \& \textcolor{comment}{!  The extra diffusivity of salinity due to double diffusion relative to}}
\DoxyCodeLine{515                 \textcolor{comment}{! Kd\_int [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{516     kd\_epbl,  \& \textcolor{comment}{! test array of diapycnal diffusivities at interfaces [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{517     tdif\_flx, \& \textcolor{comment}{! diffusive diapycnal heat flux across interfaces [degC H T-\/1 ~> degC m s-\/1 or degC kg m-\/2 s-\/1]}}
\DoxyCodeLine{518     tadv\_flx, \& \textcolor{comment}{! advective diapycnal heat flux across interfaces [degC H T-\/1 ~> degC m s-\/1 or degC kg m-\/2 s-\/1]}}
\DoxyCodeLine{519     sdif\_flx, \& \textcolor{comment}{! diffusive diapycnal salt flux across interfaces [ppt H T-\/1 ~> ppt m s-\/1 or ppt kg m-\/2 s-\/1]}}
\DoxyCodeLine{520     sadv\_flx    \textcolor{comment}{! advective diapycnal salt flux across interfaces [ppt H T-\/1 ~> ppt m s-\/1 or ppt kg m-\/2 s-\/1]}}
\DoxyCodeLine{521 }
\DoxyCodeLine{522 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{allocatable}, \textcolor{keywordtype}{dimension(:,:)} :: \&}
\DoxyCodeLine{523     hf\_dudt\_dia\_2d, hf\_dvdt\_dia\_2d \textcolor{comment}{! Depth sum of diapycnal mixing accelaration * fract. thickness [L T-\/2 ~> m s-\/2].}}
\DoxyCodeLine{524 }
\DoxyCodeLine{525   \textcolor{keywordtype}{logical} :: in\_boundary(SZI\_(G)) \textcolor{comment}{! True if there are no massive layers below,}}
\DoxyCodeLine{526                                   \textcolor{comment}{! where massive is defined as sufficiently thick that}}
\DoxyCodeLine{527                                   \textcolor{comment}{! the no-\/flux boundary conditions have not restricted}}
\DoxyCodeLine{528                                   \textcolor{comment}{! the entrainment -\/ usually sqrt(Kd*dt).}}
\DoxyCodeLine{529 }
\DoxyCodeLine{530 \textcolor{keywordtype}{  real} :: b\_denom\_1    \textcolor{comment}{! The first term in the denominator of b1}}
\DoxyCodeLine{531                        \textcolor{comment}{! [H ~> m or kg m-\/2]}}
\DoxyCodeLine{532 \textcolor{keywordtype}{  real} :: h\_neglect    \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{533                        \textcolor{comment}{! in roundoff and can be neglected}}
\DoxyCodeLine{534                        \textcolor{comment}{! [H ~> m or kg m-\/2]}}
\DoxyCodeLine{535 \textcolor{keywordtype}{  real} :: h\_neglect2   \textcolor{comment}{! h\_neglect\string^2 [H2 ~> m2 or kg2 m-\/4]}}
\DoxyCodeLine{536 \textcolor{keywordtype}{  real} :: add\_ent      \textcolor{comment}{! Entrainment that needs to be added when mixing tracers}}
\DoxyCodeLine{537                        \textcolor{comment}{! [H ~> m or kg m-\/2]}}
\DoxyCodeLine{538 \textcolor{keywordtype}{  real} :: eaval        \textcolor{comment}{! eaval is 2*ea at velocity grid points [H ~> m or kg m-\/2]}}
\DoxyCodeLine{539 \textcolor{keywordtype}{  real} :: hval         \textcolor{comment}{! hval is 2*h at velocity grid points [H ~> m or kg m-\/2]}}
\DoxyCodeLine{540 \textcolor{keywordtype}{  real} :: h\_tr         \textcolor{comment}{! h\_tr is h at tracer points with a tiny thickness}}
\DoxyCodeLine{541                        \textcolor{comment}{! added to ensure positive definiteness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{542 \textcolor{keywordtype}{  real} :: Tr\_ea\_BBL    \textcolor{comment}{! The diffusive tracer thickness in the BBL that is}}
\DoxyCodeLine{543                        \textcolor{comment}{! coupled to the bottom within a timestep [H ~> m or kg m-\/2]}}
\DoxyCodeLine{544 }
\DoxyCodeLine{545 \textcolor{keywordtype}{  real} :: htot(SZIB\_(G))             \textcolor{comment}{! The summed thickness from the bottom [H ~> m or kg m-\/2].}}
\DoxyCodeLine{546 \textcolor{keywordtype}{  real} :: b1(SZIB\_(G)), d1(SZIB\_(G)) \textcolor{comment}{! b1, c1, and d1 are variables used by the}}
\DoxyCodeLine{547 \textcolor{keywordtype}{  real} :: c1(SZIB\_(G),SZK\_(G))       \textcolor{comment}{! tridiagonal solver.}}
\DoxyCodeLine{548 }
\DoxyCodeLine{549 \textcolor{keywordtype}{  real} :: Ent\_int \textcolor{comment}{! The diffusive entrainment rate at an interface [H ~> m or kg m-\/2]}}
\DoxyCodeLine{550 \textcolor{keywordtype}{  real} :: Idt     \textcolor{comment}{! The inverse time step [T-\/1 ~> s-\/1]}}
\DoxyCodeLine{551 }
\DoxyCodeLine{552   \textcolor{keywordtype}{integer} :: dir\_flag     \textcolor{comment}{! An integer encoding the directions in which to do halo updates.}}
\DoxyCodeLine{553   \textcolor{keywordtype}{logical} :: showCallTree \textcolor{comment}{! If true, show the call tree}}
\DoxyCodeLine{554   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, Isq, Ieq, Jsq, Jeq, nz, m}
\DoxyCodeLine{555 }
\DoxyCodeLine{556   \textcolor{keywordtype}{integer} :: ig, jg      \textcolor{comment}{! global indices for testing testing itide point source (BDM)}}
\DoxyCodeLine{557 \textcolor{keywordtype}{  real} :: Kd\_add\_here    \textcolor{comment}{! An added diffusivity [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{558 }
\DoxyCodeLine{559   is   = g\%isc  ; ie  = g\%iec  ; js  = g\%jsc  ; je  = g\%jec ; nz = g\%ke}
\DoxyCodeLine{560   isq  = g\%IscB ; ieq = g\%IecB ; jsq = g\%JscB ; jeq = g\%JecB}
\DoxyCodeLine{561   h\_neglect = gv\%H\_subroundoff ; h\_neglect2 = h\_neglect*h\_neglect}
\DoxyCodeLine{562   kd\_heat(:,:,:) = 0.0 ; kd\_salt(:,:,:) = 0.0}
\DoxyCodeLine{563 }
\DoxyCodeLine{564   showcalltree = calltree\_showquery()}
\DoxyCodeLine{565   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_enter(\textcolor{stringliteral}{"diabatic\_ALE\_legacy(), MOM\_diabatic\_driver.F90"})}
\DoxyCodeLine{566 }
\DoxyCodeLine{567   \textcolor{comment}{! For all other diabatic subroutines, the averaging window should be the entire diabatic timestep}}
\DoxyCodeLine{568   \textcolor{keyword}{call }enable\_averages(dt, time\_end, cs\%diag)}
\DoxyCodeLine{569 }
\DoxyCodeLine{570   \textcolor{keywordflow}{if} (cs\%use\_geothermal) \textcolor{keywordflow}{then}}
\DoxyCodeLine{571     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_geothermal)}
\DoxyCodeLine{572     \textcolor{keyword}{call }geothermal\_in\_place(h, tv, dt, g, gv, us, cs\%geothermal\_CSp, halo=cs\%halo\_TS\_diff)}
\DoxyCodeLine{573     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_geothermal)}
\DoxyCodeLine{574     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"geothermal (diabatic)"})}
\DoxyCodeLine{575     \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'geothermal'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{576 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{577 }
\DoxyCodeLine{578   \textcolor{comment}{! Whenever thickness changes let the diag manager know, target grids}}
\DoxyCodeLine{579   \textcolor{comment}{! for vertical remapping may need to be regenerated.}}
\DoxyCodeLine{580   \textcolor{keyword}{call }diag\_update\_remap\_grids(cs\%diag)}
\DoxyCodeLine{581 }
\DoxyCodeLine{582   \textcolor{comment}{! Set\_pen\_shortwave estimates the optical properties of the water column.}}
\DoxyCodeLine{583   \textcolor{comment}{! It will need to be modified later to include information about the}}
\DoxyCodeLine{584   \textcolor{comment}{! biological properties and layer thicknesses.}}
\DoxyCodeLine{585   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs\%optics)) \&}
\DoxyCodeLine{586     \textcolor{keyword}{call }set\_pen\_shortwave(cs\%optics, fluxes, g, gv, us, cs\%diabatic\_aux\_CSp, cs\%opacity\_CSp, cs\%tracer\_flow\_CSp)}
\DoxyCodeLine{587 }
\DoxyCodeLine{588   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"before find\_uv\_at\_h"}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{589 }
\DoxyCodeLine{590   \textcolor{keywordflow}{if} (cs\%use\_kappa\_shear .or. cs\%use\_CVMix\_shear) \textcolor{keywordflow}{then}}
\DoxyCodeLine{591     \textcolor{keywordflow}{if} (cs\%use\_geothermal) \textcolor{keywordflow}{then}}
\DoxyCodeLine{592       \textcolor{comment}{! The presence of eatr and ebtr causes find\_uv\_at\_h to use a tridiagonal solver,}}
\DoxyCodeLine{593       \textcolor{comment}{! which changes answers at the level of roundoff because ((A*B / A) /= B).}}
\DoxyCodeLine{594       eatr(:,:,:) = 0.0 ; ebtr(:,:,:) = 0.0}
\DoxyCodeLine{595       \textcolor{keyword}{call }find\_uv\_at\_h(u, v, h, u\_h, v\_h, g, gv, us, eatr, ebtr)}
\DoxyCodeLine{596     \textcolor{keywordflow}{else}}
\DoxyCodeLine{597       \textcolor{keyword}{call }find\_uv\_at\_h(u, v, h, u\_h, v\_h, g, gv, us)}
\DoxyCodeLine{598 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{599     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with find\_uv\_at\_h (diabatic)"})}
\DoxyCodeLine{600 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{601 }
\DoxyCodeLine{602   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_set\_diffusivity)}
\DoxyCodeLine{603   \textcolor{comment}{! Sets: Kd\_int, Kd\_extra\_T, Kd\_extra\_S and visc\%TKE\_turb}}
\DoxyCodeLine{604   \textcolor{comment}{! Also changes: visc\%Kd\_shear, visc\%Kv\_shear and visc\%Kv\_slow}}
\DoxyCodeLine{605   \textcolor{keywordflow}{if} (cs\%debug) \&}
\DoxyCodeLine{606     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"before set\_diffusivity"}, u, v, h, g, gv, us, haloshift=cs\%halo\_TS\_diff)}
\DoxyCodeLine{607   \textcolor{keywordflow}{if} (cs\%double\_diffuse) \textcolor{keywordflow}{then}}
\DoxyCodeLine{608     \textcolor{keyword}{call }set\_diffusivity(u, v, h, u\_h, v\_h, tv, fluxes, cs\%optics, visc, dt, g, gv, us, cs\%set\_diff\_CSp, \&}
\DoxyCodeLine{609                          kd\_int=kd\_int, kd\_extra\_t=kd\_extra\_t, kd\_extra\_s=kd\_extra\_s)}
\DoxyCodeLine{610   \textcolor{keywordflow}{else}}
\DoxyCodeLine{611     \textcolor{keyword}{call }set\_diffusivity(u, v, h, u\_h, v\_h, tv, fluxes, cs\%optics, visc, dt, g, gv, us, \&}
\DoxyCodeLine{612                          cs\%set\_diff\_CSp, kd\_int=kd\_int)}
\DoxyCodeLine{613 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{614   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_set\_diffusivity)}
\DoxyCodeLine{615   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with set\_diffusivity (diabatic)"})}
\DoxyCodeLine{616 }
\DoxyCodeLine{617   \textcolor{comment}{! Set diffusivities for heat and salt separately}}
\DoxyCodeLine{618 }
\DoxyCodeLine{619   \textcolor{keywordflow}{if} (cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{620     \textcolor{comment}{! Add contribution from double diffusion}}
\DoxyCodeLine{621     \textcolor{keywordflow}{if} (cs\%double\_diffuse) \textcolor{keywordflow}{then}}
\DoxyCodeLine{622       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{623       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{624         kd\_salt(i,j,k) = kd\_int(i,j,k) + kd\_extra\_s(i,j,k)}
\DoxyCodeLine{625         kd\_heat(i,j,k) = kd\_int(i,j,k) + kd\_extra\_t(i,j,k)}
\DoxyCodeLine{626 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{627     \textcolor{keywordflow}{else}}
\DoxyCodeLine{628       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{629       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{630         kd\_salt(i,j,k) = kd\_int(i,j,k)}
\DoxyCodeLine{631         kd\_heat(i,j,k) = kd\_int(i,j,k)}
\DoxyCodeLine{632 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{633 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{634 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{635 }
\DoxyCodeLine{636   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{637     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after set\_diffusivity "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{638     \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after set\_diffusivity "}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{639     \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after set\_diffusivity "}, tv, g)}
\DoxyCodeLine{640     \textcolor{keyword}{call }hchksum(kd\_int, \textcolor{stringliteral}{"after set\_diffusivity Kd\_Int"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{641     \textcolor{keyword}{call }hchksum(kd\_heat, \textcolor{stringliteral}{"after set\_diffusivity Kd\_heat"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{642     \textcolor{keyword}{call }hchksum(kd\_salt, \textcolor{stringliteral}{"after set\_diffusivity Kd\_salt"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{643 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{644 }
\DoxyCodeLine{645   \textcolor{keywordflow}{if} (cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{646     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_kpp)}
\DoxyCodeLine{647     \textcolor{comment}{! total vertical viscosity in the interior is represented via visc\%Kv\_shear}}
\DoxyCodeLine{648 }
\DoxyCodeLine{649     \textcolor{comment}{! KPP needs the surface buoyancy flux but does not update state variables.}}
\DoxyCodeLine{650     \textcolor{comment}{! We could make this call higher up to avoid a repeat unpacking of the surface fluxes.}}
\DoxyCodeLine{651     \textcolor{comment}{! Sets: CS\%KPP\_buoy\_flux, CS\%KPP\_temp\_flux, CS\%KPP\_salt\_flux}}
\DoxyCodeLine{652     \textcolor{comment}{! NOTE: CS\%KPP\_buoy\_flux, CS\%KPP\_temp\_flux, CS\%KPP\_salt\_flux are returned as rates (i.e. stuff per second)}}
\DoxyCodeLine{653     \textcolor{comment}{! unlike other instances where the fluxes are integrated in time over a time-\/step.}}
\DoxyCodeLine{654     \textcolor{keyword}{call }calculatebuoyancyflux2d(g, gv, us, fluxes, cs\%optics, h, tv\%T, tv\%S, tv, \&}
\DoxyCodeLine{655                                  cs\%KPP\_buoy\_flux, cs\%KPP\_temp\_flux, cs\%KPP\_salt\_flux)}
\DoxyCodeLine{656     \textcolor{comment}{! The KPP scheme calculates boundary layer diffusivities and non-\/local transport.}}
\DoxyCodeLine{657 }
\DoxyCodeLine{658     \textcolor{keyword}{call }kpp\_compute\_bld(cs\%KPP\_CSp, g, gv, us, h, tv\%T, tv\%S, u, v, tv, \&}
\DoxyCodeLine{659                          fluxes\%ustar, cs\%KPP\_buoy\_flux, waves=waves)}
\DoxyCodeLine{660 }
\DoxyCodeLine{661     \textcolor{keyword}{call }kpp\_calculate(cs\%KPP\_CSp, g, gv, us, h, fluxes\%ustar, cs\%KPP\_buoy\_flux, kd\_heat, \&}
\DoxyCodeLine{662                        kd\_salt, visc\%Kv\_shear, cs\%KPP\_NLTheat, cs\%KPP\_NLTscalar, waves=waves)}
\DoxyCodeLine{663 }
\DoxyCodeLine{664     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(hml)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{665       \textcolor{keyword}{call }kpp\_get\_bld(cs\%KPP\_CSp, hml(:,:), g, us)}
\DoxyCodeLine{666       \textcolor{keyword}{call }pass\_var(hml, g\%domain, halo=1)}
\DoxyCodeLine{667       \textcolor{comment}{! If visc\%MLD exists, copy KPP's BLD into it}}
\DoxyCodeLine{668       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%MLD)) visc\%MLD(:,:) = hml(:,:)}
\DoxyCodeLine{669 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{670 }
\DoxyCodeLine{671     \textcolor{keywordflow}{if} (.not.cs\%KPPisPassive) \textcolor{keywordflow}{then}}
\DoxyCodeLine{672       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{673       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{674         kd\_int(i,j,k) = min( kd\_salt(i,j,k),  kd\_heat(i,j,k) )}
\DoxyCodeLine{675 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{676       \textcolor{keywordflow}{if} (cs\%double\_diffuse) \textcolor{keywordflow}{then}}
\DoxyCodeLine{677         \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{678         \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{679           kd\_extra\_s(i,j,k) = (kd\_salt(i,j,k) -\/ kd\_int(i,j,k))}
\DoxyCodeLine{680           kd\_extra\_t(i,j,k) = (kd\_heat(i,j,k) -\/ kd\_int(i,j,k))}
\DoxyCodeLine{681 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{682 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{683 \textcolor{keywordflow}{    endif} \textcolor{comment}{! not passive}}
\DoxyCodeLine{684 }
\DoxyCodeLine{685     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_kpp)}
\DoxyCodeLine{686     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with KPP\_calculate (diabatic)"})}
\DoxyCodeLine{687     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{688       \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after KPP"}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{689       \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after KPP"}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{690       \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after KPP"}, tv, g)}
\DoxyCodeLine{691       \textcolor{keyword}{call }hchksum(kd\_heat, \textcolor{stringliteral}{"after KPP Kd\_heat"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{692       \textcolor{keyword}{call }hchksum(kd\_salt, \textcolor{stringliteral}{"after KPP Kd\_salt"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{693 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{694 }
\DoxyCodeLine{695 \textcolor{keywordflow}{  endif}  \textcolor{comment}{! endif for KPP}}
\DoxyCodeLine{696 }
\DoxyCodeLine{697 }
\DoxyCodeLine{698   \textcolor{keywordflow}{if} (cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{699     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_kpp)}
\DoxyCodeLine{700     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{701       \textcolor{keyword}{call }hchksum(cs\%KPP\_temp\_flux, \textcolor{stringliteral}{"before KPP\_applyNLT netHeat"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{702       \textcolor{keyword}{call }hchksum(cs\%KPP\_salt\_flux, \textcolor{stringliteral}{"before KPP\_applyNLT netSalt"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{703       \textcolor{keyword}{call }hchksum(cs\%KPP\_NLTheat, \textcolor{stringliteral}{"before KPP\_applyNLT NLTheat"}, g\%HI, haloshift=0)}
\DoxyCodeLine{704       \textcolor{keyword}{call }hchksum(cs\%KPP\_NLTscalar, \textcolor{stringliteral}{"before KPP\_applyNLT NLTscalar"}, g\%HI, haloshift=0)}
\DoxyCodeLine{705 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{706     \textcolor{comment}{! Apply non-\/local transport of heat and salt}}
\DoxyCodeLine{707     \textcolor{comment}{! Changes: tv\%T, tv\%S}}
\DoxyCodeLine{708     \textcolor{keyword}{call }kpp\_nonlocaltransport\_temp(cs\%KPP\_CSp, g, gv, h, cs\%KPP\_NLTheat,   cs\%KPP\_temp\_flux, \&}
\DoxyCodeLine{709                                     us\%T\_to\_s*dt, tv\%T, us\%Q\_to\_J\_kg*tv\%C\_p)}
\DoxyCodeLine{710     \textcolor{keyword}{call }kpp\_nonlocaltransport\_saln(cs\%KPP\_CSp, g, gv, h, cs\%KPP\_NLTscalar, cs\%KPP\_salt\_flux, \&}
\DoxyCodeLine{711                                     us\%T\_to\_s*dt, tv\%S)}
\DoxyCodeLine{712     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_kpp)}
\DoxyCodeLine{713     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with KPP\_applyNonLocalTransport (diabatic)"})}
\DoxyCodeLine{714     \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'KPP\_applyNonLocalTransport'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{715 }
\DoxyCodeLine{716     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{717       \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after KPP\_applyNLT "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{718       \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after KPP\_applyNLT "}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{719       \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after KPP\_applyNLT "}, tv, g)}
\DoxyCodeLine{720 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{721 \textcolor{keywordflow}{  endif} \textcolor{comment}{! endif for KPP}}
\DoxyCodeLine{722 }
\DoxyCodeLine{723   \textcolor{comment}{! This is the "old" method for applying differential diffusion.}}
\DoxyCodeLine{724   \textcolor{comment}{! Changes: tv\%T, tv\%S}}
\DoxyCodeLine{725   \textcolor{keywordflow}{if} (cs\%double\_diffuse .and. \textcolor{keyword}{associated}(tv\%T)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{726 }
\DoxyCodeLine{727     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_differential\_diff)}
\DoxyCodeLine{728     \textcolor{keyword}{call }differential\_diffuse\_t\_s(h, tv\%T, tv\%S, kd\_extra\_t, kd\_extra\_s, dt, g, gv)}
\DoxyCodeLine{729     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_differential\_diff)}
\DoxyCodeLine{730 }
\DoxyCodeLine{731     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with differential\_diffuse\_T\_S (diabatic)"})}
\DoxyCodeLine{732     \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'differential\_diffuse\_T\_S'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{733 }
\DoxyCodeLine{734     \textcolor{comment}{! increment heat and salt diffusivity.}}
\DoxyCodeLine{735     \textcolor{comment}{! CS\%useKPP==.true. already has extra\_T and extra\_S included}}
\DoxyCodeLine{736     \textcolor{keywordflow}{if} (.not. cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{737       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{738       \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{739         kd\_heat(i,j,k) = kd\_heat(i,j,k) + kd\_extra\_t(i,j,k)}
\DoxyCodeLine{740         kd\_salt(i,j,k) = kd\_salt(i,j,k) + kd\_extra\_s(i,j,k)}
\DoxyCodeLine{741 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{742 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{743 }
\DoxyCodeLine{744 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{745 }
\DoxyCodeLine{746   \textcolor{comment}{! Calculate vertical mixing due to convection (computed via CVMix)}}
\DoxyCodeLine{747   \textcolor{keywordflow}{if} (cs\%use\_CVMix\_conv) \textcolor{keywordflow}{then}}
\DoxyCodeLine{748     \textcolor{comment}{! Increment vertical diffusion and viscosity due to convection}}
\DoxyCodeLine{749     \textcolor{keyword}{call }calculate\_cvmix\_conv(h, tv, g, gv, us, cs\%CVMix\_conv\_csp, hml, kd=kd\_int, kv=visc\%Kv\_slow)}
\DoxyCodeLine{750 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{751 }
\DoxyCodeLine{752   \textcolor{comment}{! This block sets ea, eb from h and Kd\_int.}}
\DoxyCodeLine{753   \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{754     ea\_s(i,j,1) = 0.0}
\DoxyCodeLine{755 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{756   \textcolor{comment}{!\$OMP parallel do default(shared)  private(hval)}}
\DoxyCodeLine{757   \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{758     hval=1.0/(h\_neglect + 0.5*(h(i,j,k-\/1) + h(i,j,k)))}
\DoxyCodeLine{759     ea\_s(i,j,k) = (gv\%Z\_to\_H**2) * dt * hval * kd\_int(i,j,k)}
\DoxyCodeLine{760     eb\_s(i,j,k-\/1) = ea\_s(i,j,k)}
\DoxyCodeLine{761     ea\_t(i,j,k-\/1) = ea\_s(i,j,k-\/1) ; eb\_t(i,j,k-\/1) = eb\_s(i,j,k-\/1)}
\DoxyCodeLine{762 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{763   \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{764     eb\_s(i,j,nz) = 0.0}
\DoxyCodeLine{765     ea\_t(i,j,nz) = ea\_s(i,j,nz) ; eb\_t(i,j,nz) = eb\_s(i,j,nz)}
\DoxyCodeLine{766 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{767   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done setting ea,eb from Kd\_int (diabatic)"})}
\DoxyCodeLine{768 }
\DoxyCodeLine{769   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{770     \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after calc\_entrain "}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{771     \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after calc\_entrain "}, tv, g)}
\DoxyCodeLine{772     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after calc\_entrain "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{773     \textcolor{keyword}{call }hchksum(ea\_s, \textcolor{stringliteral}{"after calc\_entrain ea\_s"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{774     \textcolor{keyword}{call }hchksum(eb\_s, \textcolor{stringliteral}{"after calc\_entrain eb\_s"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{775 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{776 }
\DoxyCodeLine{777   \textcolor{comment}{! Save fields before boundary forcing is applied for tendency diagnostics}}
\DoxyCodeLine{778   \textcolor{keywordflow}{if} (cs\%boundary\_forcing\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{779     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{780       h\_diag(i,j,k)    = h(i,j,k)}
\DoxyCodeLine{781       temp\_diag(i,j,k) = tv\%T(i,j,k)}
\DoxyCodeLine{782       saln\_diag(i,j,k) = tv\%S(i,j,k)}
\DoxyCodeLine{783 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{784 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{785 }
\DoxyCodeLine{786   \textcolor{comment}{! Apply forcing}}
\DoxyCodeLine{787   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_remap)}
\DoxyCodeLine{788 }
\DoxyCodeLine{789   \textcolor{comment}{! Changes made to following fields:  h, tv\%T and tv\%S.}}
\DoxyCodeLine{790   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{791     h\_prebound(i,j,k) = h(i,j,k)}
\DoxyCodeLine{792 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{793   \textcolor{keywordflow}{if} (cs\%use\_energetic\_PBL) \textcolor{keywordflow}{then}}
\DoxyCodeLine{794 }
\DoxyCodeLine{795     skinbuoyflux(:,:) = 0.0}
\DoxyCodeLine{796     \textcolor{keyword}{call }applyboundaryfluxesinout(cs\%diabatic\_aux\_CSp, g, gv, us, dt, fluxes, cs\%optics, \&}
\DoxyCodeLine{797             optics\_nbands(cs\%optics), h, tv, cs\%aggregate\_FW\_forcing, cs\%evap\_CFL\_limit,                         \&}
\DoxyCodeLine{798             cs\%minimum\_forcing\_depth, ctke, dsv\_dt, dsv\_ds, skinbuoyflux=skinbuoyflux)}
\DoxyCodeLine{799 }
\DoxyCodeLine{800     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{801       \textcolor{keyword}{call }hchksum(ea\_t, \textcolor{stringliteral}{"after applyBoundaryFluxes ea\_t"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{802       \textcolor{keyword}{call }hchksum(eb\_t, \textcolor{stringliteral}{"after applyBoundaryFluxes eb\_t"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{803       \textcolor{keyword}{call }hchksum(ea\_s, \textcolor{stringliteral}{"after applyBoundaryFluxes ea\_s"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{804       \textcolor{keyword}{call }hchksum(eb\_s, \textcolor{stringliteral}{"after applyBoundaryFluxes eb\_s"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{805       \textcolor{keyword}{call }hchksum(ctke, \textcolor{stringliteral}{"after applyBoundaryFluxes cTKE"}, g\%HI, haloshift=0, \&}
\DoxyCodeLine{806                    scale=us\%RZ3\_T3\_to\_W\_m2*us\%T\_to\_s)}
\DoxyCodeLine{807       \textcolor{keyword}{call }hchksum(dsv\_dt, \textcolor{stringliteral}{"after applyBoundaryFluxes dSV\_dT"}, g\%HI, haloshift=0, scale=us\%kg\_m3\_to\_R)}
\DoxyCodeLine{808       \textcolor{keyword}{call }hchksum(dsv\_ds, \textcolor{stringliteral}{"after applyBoundaryFluxes dSV\_dS"}, g\%HI, haloshift=0, scale=us\%kg\_m3\_to\_R)}
\DoxyCodeLine{809 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{810 }
\DoxyCodeLine{811     \textcolor{keyword}{call }find\_uv\_at\_h(u, v, h, u\_h, v\_h, g, gv, us)}
\DoxyCodeLine{812     \textcolor{keyword}{call }energetic\_pbl(h, u\_h, v\_h, tv, fluxes, dt, kd\_epbl, g, gv, us, \&}
\DoxyCodeLine{813                        cs\%energetic\_PBL\_CSp, dsv\_dt, dsv\_ds, ctke, skinbuoyflux, waves=waves)}
\DoxyCodeLine{814 }
\DoxyCodeLine{815     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(hml)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{816       \textcolor{keyword}{call }energetic\_pbl\_get\_mld(cs\%energetic\_PBL\_CSp, hml(:,:), g, us)}
\DoxyCodeLine{817       \textcolor{keyword}{call }pass\_var(hml, g\%domain, halo=1)}
\DoxyCodeLine{818       \textcolor{comment}{! If visc\%MLD exists, copy ePBL's MLD into it}}
\DoxyCodeLine{819       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%MLD)) visc\%MLD(:,:) = hml(:,:)}
\DoxyCodeLine{820     \textcolor{keywordflow}{elseif} (\textcolor{keyword}{associated}(visc\%MLD)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{821       \textcolor{keyword}{call }energetic\_pbl\_get\_mld(cs\%energetic\_PBL\_CSp, visc\%MLD, g, us)}
\DoxyCodeLine{822       \textcolor{keyword}{call }pass\_var(visc\%MLD, g\%domain, halo=1)}
\DoxyCodeLine{823 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{824 }
\DoxyCodeLine{825     \textcolor{comment}{! Augment the diffusivities and viscosity due to those diagnosed in energetic\_PBL.}}
\DoxyCodeLine{826     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{827       \textcolor{keywordflow}{if} (cs\%ePBL\_is\_additive) \textcolor{keywordflow}{then}}
\DoxyCodeLine{828         kd\_add\_here = kd\_epbl(i,j,k)}
\DoxyCodeLine{829         visc\%Kv\_shear(i,j,k) = visc\%Kv\_shear(i,j,k) + cs\%ePBL\_Prandtl*kd\_epbl(i,j,k)}
\DoxyCodeLine{830       \textcolor{keywordflow}{else}}
\DoxyCodeLine{831         kd\_add\_here = max(kd\_epbl(i,j,k) -\/ visc\%Kd\_shear(i,j,k), 0.0)}
\DoxyCodeLine{832         visc\%Kv\_shear(i,j,k) = max(visc\%Kv\_shear(i,j,k), cs\%ePBL\_Prandtl*kd\_epbl(i,j,k))}
\DoxyCodeLine{833 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{834 }
\DoxyCodeLine{835       ent\_int = kd\_add\_here * (gv\%Z\_to\_H**2 * dt) / \&}
\DoxyCodeLine{836                   (0.5*(h(i,j,k-\/1) + h(i,j,k)) + h\_neglect)}
\DoxyCodeLine{837       eb\_s(i,j,k-\/1) = eb\_s(i,j,k-\/1) + ent\_int}
\DoxyCodeLine{838       ea\_s(i,j,k) = ea\_s(i,j,k) + ent\_int}
\DoxyCodeLine{839       kd\_int(i,j,k) = kd\_int(i,j,k) + kd\_add\_here}
\DoxyCodeLine{840 }
\DoxyCodeLine{841       \textcolor{comment}{! for diagnostics}}
\DoxyCodeLine{842       kd\_heat(i,j,k) = kd\_heat(i,j,k) + kd\_int(i,j,k)}
\DoxyCodeLine{843       kd\_salt(i,j,k) = kd\_salt(i,j,k) + kd\_int(i,j,k)}
\DoxyCodeLine{844 }
\DoxyCodeLine{845 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{846 }
\DoxyCodeLine{847     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{848       \textcolor{keyword}{call }hchksum(ea\_t, \textcolor{stringliteral}{"after ePBL ea\_t"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{849       \textcolor{keyword}{call }hchksum(eb\_t, \textcolor{stringliteral}{"after ePBL eb\_t"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{850       \textcolor{keyword}{call }hchksum(ea\_s, \textcolor{stringliteral}{"after ePBL ea\_s"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{851       \textcolor{keyword}{call }hchksum(eb\_s, \textcolor{stringliteral}{"after ePBL eb\_s"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{852       \textcolor{keyword}{call }hchksum(kd\_epbl, \textcolor{stringliteral}{"after ePBL Kd\_ePBL"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{853 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{854 }
\DoxyCodeLine{855   \textcolor{keywordflow}{else}}
\DoxyCodeLine{856     \textcolor{keyword}{call }applyboundaryfluxesinout(cs\%diabatic\_aux\_CSp, g, gv, us, dt, fluxes, cs\%optics, \&}
\DoxyCodeLine{857                                   optics\_nbands(cs\%optics), h, tv, cs\%aggregate\_FW\_forcing, \&}
\DoxyCodeLine{858                                   cs\%evap\_CFL\_limit, cs\%minimum\_forcing\_depth)}
\DoxyCodeLine{859 }
\DoxyCodeLine{860 \textcolor{keywordflow}{  endif}   \textcolor{comment}{! endif for CS\%use\_energetic\_PBL}}
\DoxyCodeLine{861 }
\DoxyCodeLine{862   \textcolor{comment}{! diagnose the tendencies due to boundary forcing}}
\DoxyCodeLine{863   \textcolor{comment}{! At this point, the diagnostic grids have not been updated since the call to the boundary layer scheme}}
\DoxyCodeLine{864   \textcolor{comment}{!  so all tendency diagnostics need to be posted on h\_diag, and grids rebuilt afterwards}}
\DoxyCodeLine{865   \textcolor{keywordflow}{if} (cs\%boundary\_forcing\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{866     \textcolor{keyword}{call }diagnose\_boundary\_forcing\_tendency(tv, h, temp\_diag, saln\_diag, h\_diag, dt, g, gv, us, cs)}
\DoxyCodeLine{867     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_h > 0) \textcolor{keyword}{call }post\_data(cs\%id\_boundary\_forcing\_h, h, cs\%diag, alt\_h = h\_diag)}
\DoxyCodeLine{868 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{869   \textcolor{comment}{! Boundary fluxes may have changed T, S, and h}}
\DoxyCodeLine{870   \textcolor{keyword}{call }diag\_update\_remap\_grids(cs\%diag)}
\DoxyCodeLine{871   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_remap)}
\DoxyCodeLine{872   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{873     \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after applyBoundaryFluxes "}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{874     \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after applyBoundaryFluxes "}, tv, g)}
\DoxyCodeLine{875     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after applyBoundaryFluxes "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{876 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{877   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with applyBoundaryFluxes (diabatic)"})}
\DoxyCodeLine{878   \textcolor{keywordflow}{if} (cs\%debugConservation)  \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'applyBoundaryFluxes'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{879 }
\DoxyCodeLine{880   \textcolor{comment}{! Update h according to divergence of the difference between}}
\DoxyCodeLine{881   \textcolor{comment}{! ea and eb. We keep a record of the original h in hold.}}
\DoxyCodeLine{882   \textcolor{comment}{! In the following, the checks for negative values are to guard against}}
\DoxyCodeLine{883   \textcolor{comment}{! instances where entrainment drives a layer to negative thickness.}}
\DoxyCodeLine{884   \textcolor{comment}{!\#\#\# This code may be unnecessary, but the negative-\/thickness checks do appear to change}}
\DoxyCodeLine{885   \textcolor{comment}{!    answers slightly in some cases.}}
\DoxyCodeLine{886   \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{887   \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{888     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{889       hold(i,j,1) = h(i,j,1)}
\DoxyCodeLine{890       \textcolor{comment}{! Does nothing with ALE:  h(i,j,1) = h(i,j,1) + (eb\_s(i,j,1) -\/ ea\_s(i,j,2))}}
\DoxyCodeLine{891       hold(i,j,nz) = h(i,j,nz)}
\DoxyCodeLine{892       \textcolor{comment}{! Does nothing with ALE:  h(i,j,nz) = h(i,j,nz) + (ea\_s(i,j,nz) -\/ eb\_s(i,j,nz-\/1))}}
\DoxyCodeLine{893       \textcolor{keywordflow}{if} (h(i,j,1) <= 0.0) h(i,j,1) = gv\%Angstrom\_H}
\DoxyCodeLine{894       \textcolor{keywordflow}{if} (h(i,j,nz) <= 0.0) h(i,j,nz) = gv\%Angstrom\_H}
\DoxyCodeLine{895 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{896     \textcolor{keywordflow}{do} k=2,nz-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{897       hold(i,j,k) = h(i,j,k)}
\DoxyCodeLine{898       \textcolor{comment}{! Does nothing with ALE:  h(i,j,k) = h(i,j,k) + ((ea\_s(i,j,k) -\/ eb\_s(i,j,k-\/1)) + \&}}
\DoxyCodeLine{899       \textcolor{comment}{!                                                (eb\_s(i,j,k) -\/ ea\_s(i,j,k+1)))}}
\DoxyCodeLine{900       \textcolor{keywordflow}{if} (h(i,j,k) <= 0.0) h(i,j,k) = gv\%Angstrom\_H}
\DoxyCodeLine{901 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{902 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{903   \textcolor{comment}{! Checks for negative thickness may have changed layer thicknesses}}
\DoxyCodeLine{904   \textcolor{keyword}{call }diag\_update\_remap\_grids(cs\%diag)}
\DoxyCodeLine{905 }
\DoxyCodeLine{906   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{907     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after negative check "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{908     \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after negative check "}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{909     \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after negative check "}, tv, g)}
\DoxyCodeLine{910 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{911   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with h=ea-\/eb (diabatic)"})}
\DoxyCodeLine{912   \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'h=ea-\/eb'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{913 }
\DoxyCodeLine{914   \textcolor{comment}{! calculate change in temperature \& salinity due to dia-\/coordinate surface diffusion}}
\DoxyCodeLine{915   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%T)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{916 }
\DoxyCodeLine{917     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{918       \textcolor{keyword}{call }hchksum(ea\_t, \textcolor{stringliteral}{"before triDiagTS ea\_t "}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{919       \textcolor{keyword}{call }hchksum(eb\_t, \textcolor{stringliteral}{"before triDiagTS eb\_t "}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{920       \textcolor{keyword}{call }hchksum(ea\_s, \textcolor{stringliteral}{"before triDiagTS ea\_s "}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{921       \textcolor{keyword}{call }hchksum(eb\_s, \textcolor{stringliteral}{"before triDiagTS eb\_s "}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{922 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{923 }
\DoxyCodeLine{924     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_tridiag)}
\DoxyCodeLine{925     \textcolor{comment}{!  Keep salinity from falling below a small but positive threshold.}}
\DoxyCodeLine{926     \textcolor{comment}{!  This constraint is needed for SIS1 ice model, which can extract}}
\DoxyCodeLine{927     \textcolor{comment}{!  more salt than is present in the ocean. SIS2 does not suffer}}
\DoxyCodeLine{928     \textcolor{comment}{!  from this limitation, in which case we can let salinity=0 and still}}
\DoxyCodeLine{929     \textcolor{comment}{!  have salt conserved with SIS2 ice. So for SIS2, we can run with}}
\DoxyCodeLine{930     \textcolor{comment}{!  BOUND\_SALINITY=False in MOM.F90.}}
\DoxyCodeLine{931     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%S) .and. \textcolor{keyword}{associated}(tv\%salt\_deficit)) \&}
\DoxyCodeLine{932       \textcolor{keyword}{call }adjust\_salt(h, tv, g, gv, cs\%diabatic\_aux\_CSp)}
\DoxyCodeLine{933 }
\DoxyCodeLine{934     \textcolor{keywordflow}{if} (cs\%diabatic\_diff\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{935       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{936         temp\_diag(i,j,k) = tv\%T(i,j,k)}
\DoxyCodeLine{937         saln\_diag(i,j,k) = tv\%S(i,j,k)}
\DoxyCodeLine{938 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{939 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{940 }
\DoxyCodeLine{941     \textcolor{comment}{! Changes T and S via the tridiagonal solver; no change to h}}
\DoxyCodeLine{942     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{943       ea\_t(i,j,k) = ea\_s(i,j,k) ; eb\_t(i,j,k) = eb\_s(i,j,k)}
\DoxyCodeLine{944 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{945     \textcolor{keywordflow}{if} (cs\%tracer\_tridiag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{946       \textcolor{keyword}{call }tracer\_vertdiff(hold, ea\_t, eb\_t, dt, tv\%T, g, gv)}
\DoxyCodeLine{947       \textcolor{keyword}{call }tracer\_vertdiff(hold, ea\_s, eb\_s, dt, tv\%S, g, gv)}
\DoxyCodeLine{948     \textcolor{keywordflow}{else}}
\DoxyCodeLine{949       \textcolor{keyword}{call }tridiagts(g, gv, is, ie, js, je, hold, ea\_s, eb\_s, tv\%T, tv\%S)}
\DoxyCodeLine{950 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{951 }
\DoxyCodeLine{952     \textcolor{comment}{! diagnose temperature, salinity, heat, and salt tendencies}}
\DoxyCodeLine{953     \textcolor{comment}{! Note: hold here refers to the thicknesses from before the dual-\/entraintment when using}}
\DoxyCodeLine{954     \textcolor{comment}{! the bulk mixed layer scheme. Otherwise in ALE-\/mode, layer thicknesses will (not?) have changed}}
\DoxyCodeLine{955     \textcolor{comment}{! In either case, tendencies should be posted on hold}}
\DoxyCodeLine{956     \textcolor{keywordflow}{if} (cs\%diabatic\_diff\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{957       \textcolor{keyword}{call }diagnose\_diabatic\_diff\_tendency(tv, hold, temp\_diag, saln\_diag, dt, g, gv, us, cs)}
\DoxyCodeLine{958       \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_h > 0) \textcolor{keyword}{call }post\_data(cs\%id\_diabatic\_diff\_h, hold, cs\%diag, alt\_h = hold)}
\DoxyCodeLine{959 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{960 }
\DoxyCodeLine{961     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_tridiag)}
\DoxyCodeLine{962 }
\DoxyCodeLine{963     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with triDiagTS (diabatic)"})}
\DoxyCodeLine{964 }
\DoxyCodeLine{965 \textcolor{keywordflow}{  endif}  \textcolor{comment}{! endif corresponding to if (associated(tv\%T))}}
\DoxyCodeLine{966 }
\DoxyCodeLine{967   \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'triDiagTS'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{968 }
\DoxyCodeLine{969   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{970     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after mixed layer "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{971     \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after mixed layer "}, tv, g)}
\DoxyCodeLine{972 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{973 }
\DoxyCodeLine{974   \textcolor{comment}{! Whenever thickness changes let the diag manager know, as the}}
\DoxyCodeLine{975   \textcolor{comment}{! target grids for vertical remapping may need to be regenerated.}}
\DoxyCodeLine{976   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%du\_dt\_dia) .or. \textcolor{keyword}{associated}(adp\%dv\_dt\_dia)) \&}
\DoxyCodeLine{977     \textcolor{comment}{! Remapped d[uv]dt\_dia require east/north halo updates of h}}
\DoxyCodeLine{978     \textcolor{keyword}{call }pass\_var(h, g\%domain, to\_west+to\_south+omit\_corners, halo=1)}
\DoxyCodeLine{979   \textcolor{keyword}{call }diag\_update\_remap\_grids(cs\%diag)}
\DoxyCodeLine{980 }
\DoxyCodeLine{981   \textcolor{comment}{! diagnostics}}
\DoxyCodeLine{982   idt = 1.0 / dt}
\DoxyCodeLine{983   \textcolor{keywordflow}{if} ((cs\%id\_Tdif > 0) .or. (cs\%id\_Tadv > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{984     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{985       tdif\_flx(i,j,1) = 0.0 ; tdif\_flx(i,j,nz+1) = 0.0}
\DoxyCodeLine{986       tadv\_flx(i,j,1) = 0.0 ; tadv\_flx(i,j,nz+1) = 0.0}
\DoxyCodeLine{987 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{988     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{989     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{990       tdif\_flx(i,j,k) = (idt * 0.5*(ea\_t(i,j,k) + eb\_t(i,j,k-\/1))) * \&}
\DoxyCodeLine{991                         (tv\%T(i,j,k-\/1) -\/ tv\%T(i,j,k))}
\DoxyCodeLine{992       tadv\_flx(i,j,k) = (idt * (ea\_t(i,j,k) -\/ eb\_t(i,j,k-\/1))) * \&}
\DoxyCodeLine{993                     0.5*(tv\%T(i,j,k-\/1) + tv\%T(i,j,k))}
\DoxyCodeLine{994 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{995 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{996   \textcolor{keywordflow}{if} ((cs\%id\_Sdif > 0) .or. (cs\%id\_Sadv > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{997     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{998       sdif\_flx(i,j,1) = 0.0 ; sdif\_flx(i,j,nz+1) = 0.0}
\DoxyCodeLine{999       sadv\_flx(i,j,1) = 0.0 ; sadv\_flx(i,j,nz+1) = 0.0}
\DoxyCodeLine{1000 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1001     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{1002     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1003       sdif\_flx(i,j,k) = (idt * 0.5*(ea\_s(i,j,k) + eb\_s(i,j,k-\/1))) * \&}
\DoxyCodeLine{1004                         (tv\%S(i,j,k-\/1) -\/ tv\%S(i,j,k))}
\DoxyCodeLine{1005       sadv\_flx(i,j,k) = (idt * (ea\_s(i,j,k) -\/ eb\_s(i,j,k-\/1))) * \&}
\DoxyCodeLine{1006                     0.5*(tv\%S(i,j,k-\/1) + tv\%S(i,j,k))}
\DoxyCodeLine{1007 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1008 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1009 }
\DoxyCodeLine{1010   \textcolor{comment}{! mixing of passive tracers from massless boundary layers to interior}}
\DoxyCodeLine{1011   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_tracers)}
\DoxyCodeLine{1012 }
\DoxyCodeLine{1013   \textcolor{keywordflow}{if} (cs\%mix\_boundary\_tracers) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1014     tr\_ea\_bbl = gv\%Z\_to\_H * sqrt(dt*cs\%Kd\_BBL\_tr)}
\DoxyCodeLine{1015     \textcolor{comment}{!\$OMP parallel do default(shared) private(htot,in\_boundary,add\_ent)}}
\DoxyCodeLine{1016     \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{1017       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1018         ebtr(i,j,nz) = eb\_s(i,j,nz)}
\DoxyCodeLine{1019         htot(i) = 0.0}
\DoxyCodeLine{1020         in\_boundary(i) = (g\%mask2dT(i,j) > 0.0)}
\DoxyCodeLine{1021 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1022       \textcolor{keywordflow}{do} k=nz,2,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1023         \textcolor{keywordflow}{if} (in\_boundary(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1024           htot(i) = htot(i) + h(i,j,k)}
\DoxyCodeLine{1025           \textcolor{comment}{!   If diapycnal mixing has been suppressed because this is a massless}}
\DoxyCodeLine{1026           \textcolor{comment}{! layer near the bottom, add some mixing of tracers between these}}
\DoxyCodeLine{1027           \textcolor{comment}{! layers.  This flux is based on the harmonic mean of the two}}
\DoxyCodeLine{1028           \textcolor{comment}{! thicknesses, as this corresponds pretty closely (to within}}
\DoxyCodeLine{1029           \textcolor{comment}{! differences in the density jumps between layers) with what is done}}
\DoxyCodeLine{1030           \textcolor{comment}{! in the calculation of the fluxes in the first place.  Kd\_min\_tr}}
\DoxyCodeLine{1031           \textcolor{comment}{! should be much less than the values that have been set in Kd\_int,}}
\DoxyCodeLine{1032           \textcolor{comment}{! perhaps a molecular diffusivity.}}
\DoxyCodeLine{1033           add\_ent = ((dt * cs\%Kd\_min\_tr) * gv\%Z\_to\_H**2) * \&}
\DoxyCodeLine{1034                     ((h(i,j,k-\/1)+h(i,j,k)+h\_neglect) / \&}
\DoxyCodeLine{1035                      (h(i,j,k-\/1)*h(i,j,k)+h\_neglect2)) -\/ \&}
\DoxyCodeLine{1036                     0.5*(ea\_s(i,j,k) + eb\_s(i,j,k-\/1))}
\DoxyCodeLine{1037           \textcolor{keywordflow}{if} (htot(i) < tr\_ea\_bbl) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1038             add\_ent = max(0.0, add\_ent, \&}
\DoxyCodeLine{1039                           (tr\_ea\_bbl -\/ htot(i)) -\/ min(ea\_s(i,j,k),eb\_s(i,j,k-\/1)))}
\DoxyCodeLine{1040           \textcolor{keywordflow}{elseif} (add\_ent < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1041             add\_ent = 0.0 ; in\_boundary(i) = .false.}
\DoxyCodeLine{1042 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1043 }
\DoxyCodeLine{1044           ebtr(i,j,k-\/1) = eb\_s(i,j,k-\/1) + add\_ent}
\DoxyCodeLine{1045           eatr(i,j,k) = ea\_s(i,j,k) + add\_ent}
\DoxyCodeLine{1046         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1047           ebtr(i,j,k-\/1) = eb\_s(i,j,k-\/1) ; eatr(i,j,k) = ea\_s(i,j,k)}
\DoxyCodeLine{1048 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1049 }
\DoxyCodeLine{1050         \textcolor{keywordflow}{if} (cs\%double\_diffuse) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (kd\_extra\_s(i,j,k) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1051           add\_ent = ((dt * kd\_extra\_s(i,j,k)) * gv\%Z\_to\_H**2) / \&}
\DoxyCodeLine{1052              (0.25 * ((h(i,j,k-\/1) + h(i,j,k)) + (hold(i,j,k-\/1) + hold(i,j,k))) +  h\_neglect)}
\DoxyCodeLine{1053           ebtr(i,j,k-\/1) = ebtr(i,j,k-\/1) + add\_ent}
\DoxyCodeLine{1054           eatr(i,j,k) = eatr(i,j,k) + add\_ent}
\DoxyCodeLine{1055 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1056 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1057       \textcolor{keywordflow}{do} i=is,ie ; eatr(i,j,1) = ea\_s(i,j,1) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1058 }
\DoxyCodeLine{1059 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1060 }
\DoxyCodeLine{1061     \textcolor{comment}{! For passive tracers, the changes in thickness due to boundary fluxes has yet to be applied}}
\DoxyCodeLine{1062     \textcolor{comment}{! so h\_prebound is used for the old thickness.}}
\DoxyCodeLine{1063     \textcolor{comment}{!\#\#\# I think that in the following, ea\_s and eb\_s should be eatr and ebtr.}}
\DoxyCodeLine{1064     \textcolor{keyword}{call }call\_tracer\_column\_fns(h\_prebound, h, ea\_s, eb\_s, fluxes, hml, dt, g, gv, us, tv, \&}
\DoxyCodeLine{1065                               cs\%optics, cs\%tracer\_flow\_CSp, cs\%debug, \&}
\DoxyCodeLine{1066                               evap\_cfl\_limit = cs\%evap\_CFL\_limit, \&}
\DoxyCodeLine{1067                               minimum\_forcing\_depth=cs\%minimum\_forcing\_depth)}
\DoxyCodeLine{1068 }
\DoxyCodeLine{1069   \textcolor{keywordflow}{elseif} (cs\%double\_diffuse) \textcolor{keywordflow}{then}  \textcolor{comment}{! extra diffusivity for passive tracers}}
\DoxyCodeLine{1070 }
\DoxyCodeLine{1071     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1072       ebtr(i,j,nz) = eb\_s(i,j,nz) ; eatr(i,j,1) = ea\_s(i,j,1)}
\DoxyCodeLine{1073 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1074     \textcolor{comment}{!\$OMP parallel do default(shared) private(add\_ent)}}
\DoxyCodeLine{1075     \textcolor{keywordflow}{do} k=nz,2,-\/1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1076       \textcolor{keywordflow}{if} (kd\_extra\_s(i,j,k) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1077         add\_ent = ((dt * kd\_extra\_s(i,j,k)) * gv\%Z\_to\_H**2) / \&}
\DoxyCodeLine{1078            (0.25 * ((h(i,j,k-\/1) + h(i,j,k)) + (hold(i,j,k-\/1) + hold(i,j,k))) + h\_neglect)}
\DoxyCodeLine{1079       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1080         add\_ent = 0.0}
\DoxyCodeLine{1081 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1082       ebtr(i,j,k-\/1) = eb\_s(i,j,k-\/1) + add\_ent}
\DoxyCodeLine{1083       eatr(i,j,k) = ea\_s(i,j,k) + add\_ent}
\DoxyCodeLine{1084 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1085 }
\DoxyCodeLine{1086     \textcolor{comment}{! For passive tracers, the changes in thickness due to boundary fluxes has yet to be applied}}
\DoxyCodeLine{1087     \textcolor{keyword}{call }call\_tracer\_column\_fns(h\_prebound, h, eatr, ebtr, fluxes, hml, dt, g, gv, us, tv, \&}
\DoxyCodeLine{1088                                 cs\%optics, cs\%tracer\_flow\_CSp, cs\%debug,\&}
\DoxyCodeLine{1089                                 evap\_cfl\_limit = cs\%evap\_CFL\_limit, \&}
\DoxyCodeLine{1090                                 minimum\_forcing\_depth=cs\%minimum\_forcing\_depth)}
\DoxyCodeLine{1091   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1092     \textcolor{comment}{! For passive tracers, the changes in thickness due to boundary fluxes has yet to be applied}}
\DoxyCodeLine{1093     \textcolor{comment}{!\#\#\# eatr and ebtr may not be initialized or may be 0, depending on CS\%geothermal.}}
\DoxyCodeLine{1094     \textcolor{keyword}{call }call\_tracer\_column\_fns(h\_prebound, h, eatr, ebtr, fluxes, hml, dt, g, gv, us, tv, \&}
\DoxyCodeLine{1095                                 cs\%optics, cs\%tracer\_flow\_CSp, cs\%debug, \&}
\DoxyCodeLine{1096                                 evap\_cfl\_limit = cs\%evap\_CFL\_limit, \&}
\DoxyCodeLine{1097                                 minimum\_forcing\_depth=cs\%minimum\_forcing\_depth)}
\DoxyCodeLine{1098 \textcolor{keywordflow}{  endif}  \textcolor{comment}{! (CS\%mix\_boundary\_tracers)}}
\DoxyCodeLine{1099 }
\DoxyCodeLine{1100   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_tracers)}
\DoxyCodeLine{1101 }
\DoxyCodeLine{1102   \textcolor{comment}{! Apply ALE sponge}}
\DoxyCodeLine{1103   \textcolor{keywordflow}{if} (cs\%use\_sponge) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1104     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_sponge)}
\DoxyCodeLine{1105     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs\%ALE\_sponge\_CSp)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1106       \textcolor{keyword}{call }apply\_ale\_sponge(h, dt, g, gv, us, cs\%ALE\_sponge\_CSp, cs\%Time)}
\DoxyCodeLine{1107 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1108 }
\DoxyCodeLine{1109     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_sponge)}
\DoxyCodeLine{1110     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1111       \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"apply\_sponge "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{1112       \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"apply\_sponge "}, tv, g)}
\DoxyCodeLine{1113 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1114 \textcolor{keywordflow}{  endif} \textcolor{comment}{! CS\%use\_sponge}}
\DoxyCodeLine{1115 }
\DoxyCodeLine{1116   \textcolor{keyword}{call }disable\_averaging(cs\%diag)}
\DoxyCodeLine{1117   \textcolor{comment}{! Diagnose the diapycnal diffusivities and other related quantities.}}
\DoxyCodeLine{1118   \textcolor{keyword}{call }enable\_averages(dt, time\_end, cs\%diag)}
\DoxyCodeLine{1119 }
\DoxyCodeLine{1120   \textcolor{keywordflow}{if} (cs\%id\_Kd\_interface > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_interface, kd\_int,  cs\%diag)}
\DoxyCodeLine{1121   \textcolor{keywordflow}{if} (cs\%id\_Kd\_heat      > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_heat,      kd\_heat, cs\%diag)}
\DoxyCodeLine{1122   \textcolor{keywordflow}{if} (cs\%id\_Kd\_salt      > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_salt,      kd\_salt, cs\%diag)}
\DoxyCodeLine{1123   \textcolor{keywordflow}{if} (cs\%id\_Kd\_ePBL      > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_ePBL,      kd\_epbl, cs\%diag)}
\DoxyCodeLine{1124 }
\DoxyCodeLine{1125   \textcolor{keywordflow}{if} (cs\%id\_ea         > 0) \textcolor{keyword}{call }post\_data(cs\%id\_ea,         ea\_s, cs\%diag)}
\DoxyCodeLine{1126   \textcolor{keywordflow}{if} (cs\%id\_eb         > 0) \textcolor{keyword}{call }post\_data(cs\%id\_eb,         eb\_s, cs\%diag)}
\DoxyCodeLine{1127   \textcolor{keywordflow}{if} (cs\%id\_ea\_t       > 0) \textcolor{keyword}{call }post\_data(cs\%id\_ea\_t,       ea\_t, cs\%diag)}
\DoxyCodeLine{1128   \textcolor{keywordflow}{if} (cs\%id\_eb\_t       > 0) \textcolor{keyword}{call }post\_data(cs\%id\_eb\_t,       eb\_t, cs\%diag)}
\DoxyCodeLine{1129   \textcolor{keywordflow}{if} (cs\%id\_ea\_s       > 0) \textcolor{keyword}{call }post\_data(cs\%id\_ea\_s,       ea\_s, cs\%diag)}
\DoxyCodeLine{1130   \textcolor{keywordflow}{if} (cs\%id\_eb\_s       > 0) \textcolor{keyword}{call }post\_data(cs\%id\_eb\_s,       eb\_s, cs\%diag)}
\DoxyCodeLine{1131 }
\DoxyCodeLine{1132   \textcolor{keywordflow}{if} (cs\%id\_dudt\_dia > 0) \textcolor{keyword}{call }post\_data(cs\%id\_dudt\_dia, adp\%du\_dt\_dia,  cs\%diag)}
\DoxyCodeLine{1133   \textcolor{keywordflow}{if} (cs\%id\_dvdt\_dia > 0) \textcolor{keyword}{call }post\_data(cs\%id\_dvdt\_dia, adp\%dv\_dt\_dia,  cs\%diag)}
\DoxyCodeLine{1134   \textcolor{keywordflow}{if} (cs\%id\_wd       > 0) \textcolor{keyword}{call }post\_data(cs\%id\_wd,       cdp\%diapyc\_vel, cs\%diag)}
\DoxyCodeLine{1135 }
\DoxyCodeLine{1136   \textcolor{keywordflow}{if} (cs\%id\_Tdif > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Tdif, tdif\_flx, cs\%diag)}
\DoxyCodeLine{1137   \textcolor{keywordflow}{if} (cs\%id\_Tadv > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Tadv, tadv\_flx, cs\%diag)}
\DoxyCodeLine{1138   \textcolor{keywordflow}{if} (cs\%id\_Sdif > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Sdif, sdif\_flx, cs\%diag)}
\DoxyCodeLine{1139   \textcolor{keywordflow}{if} (cs\%id\_Sadv > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Sadv, sadv\_flx, cs\%diag)}
\DoxyCodeLine{1140 }
\DoxyCodeLine{1141   \textcolor{comment}{!! Diagnostics for terms multiplied by fractional thicknesses}}
\DoxyCodeLine{1142   \textcolor{keywordflow}{if} (cs\%id\_hf\_dudt\_dia\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1143     \textcolor{keyword}{allocate}(hf\_dudt\_dia\_2d(g\%IsdB:g\%IedB,g\%jsd:g\%jed))}
\DoxyCodeLine{1144     hf\_dudt\_dia\_2d(:,:) = 0.0}
\DoxyCodeLine{1145     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{1146       hf\_dudt\_dia\_2d(i,j) = hf\_dudt\_dia\_2d(i,j) + adp\%du\_dt\_dia(i,j,k) * adp\%diag\_hfrac\_u(i,j,k)}
\DoxyCodeLine{1147 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1148     \textcolor{keyword}{call }post\_data(cs\%id\_hf\_dudt\_dia\_2d, hf\_dudt\_dia\_2d, cs\%diag)}
\DoxyCodeLine{1149     \textcolor{keyword}{deallocate}(hf\_dudt\_dia\_2d)}
\DoxyCodeLine{1150 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1151 }
\DoxyCodeLine{1152   \textcolor{keywordflow}{if} (cs\%id\_hf\_dvdt\_dia\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1153     \textcolor{keyword}{allocate}(hf\_dvdt\_dia\_2d(g\%isd:g\%ied,g\%JsdB:g\%JedB))}
\DoxyCodeLine{1154     hf\_dvdt\_dia\_2d(:,:) = 0.0}
\DoxyCodeLine{1155     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=jsq,jeq ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1156       hf\_dvdt\_dia\_2d(i,j) = hf\_dvdt\_dia\_2d(i,j) + adp\%dv\_dt\_dia(i,j,k) * adp\%diag\_hfrac\_v(i,j,k)}
\DoxyCodeLine{1157 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1158     \textcolor{keyword}{call }post\_data(cs\%id\_hf\_dvdt\_dia\_2d, hf\_dvdt\_dia\_2d, cs\%diag)}
\DoxyCodeLine{1159     \textcolor{keyword}{deallocate}(hf\_dvdt\_dia\_2d)}
\DoxyCodeLine{1160 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1161 }
\DoxyCodeLine{1162   \textcolor{keyword}{call }disable\_averaging(cs\%diag)}
\DoxyCodeLine{1163 }
\DoxyCodeLine{1164   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_leave(\textcolor{stringliteral}{"diabatic\_ALE\_legacy()"})}
\DoxyCodeLine{1165 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diabatic__driver_a6eac8317c3b569e414fb5a6678afc598}\label{namespacemom__diabatic__driver_a6eac8317c3b569e414fb5a6678afc598}} 
\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}!diabatic\_driver\_end@{diabatic\_driver\_end}}
\index{diabatic\_driver\_end@{diabatic\_driver\_end}!mom\_diabatic\_driver@{mom\_diabatic\_driver}}
\doxysubsubsection{\texorpdfstring{diabatic\_driver\_end()}{diabatic\_driver\_end()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+diabatic\+\_\+driver\+::diabatic\+\_\+driver\+\_\+end (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Routine to close the diabatic driver module. 


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


Definition at line 3733 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{3733   \textcolor{keywordtype}{type}(diabatic\_CS), \textcolor{keywordtype}{pointer} :: CS\textcolor{comment}{    !< module control structure}}
\DoxyCodeLine{3734 }
\DoxyCodeLine{3735   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{return}}
\DoxyCodeLine{3736 }
\DoxyCodeLine{3737   \textcolor{keyword}{call }diabatic\_aux\_end(cs\%diabatic\_aux\_CSp)}
\DoxyCodeLine{3738 }
\DoxyCodeLine{3739   \textcolor{keyword}{call }entrain\_diffusive\_end(cs\%entrain\_diffusive\_CSp)}
\DoxyCodeLine{3740   \textcolor{keyword}{call }set\_diffusivity\_end(cs\%set\_diff\_CSp)}
\DoxyCodeLine{3741 }
\DoxyCodeLine{3742   \textcolor{keywordflow}{if} (cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3743     \textcolor{keyword}{deallocate}( cs\%KPP\_buoy\_flux )}
\DoxyCodeLine{3744     \textcolor{keyword}{deallocate}( cs\%KPP\_temp\_flux )}
\DoxyCodeLine{3745     \textcolor{keyword}{deallocate}( cs\%KPP\_salt\_flux )}
\DoxyCodeLine{3746     \textcolor{keyword}{deallocate}( cs\%KPP\_NLTheat )}
\DoxyCodeLine{3747     \textcolor{keyword}{deallocate}( cs\%KPP\_NLTscalar )}
\DoxyCodeLine{3748     \textcolor{keyword}{call }kpp\_end(cs\%KPP\_CSp)}
\DoxyCodeLine{3749 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3750 }
\DoxyCodeLine{3751   \textcolor{keywordflow}{if} (cs\%use\_CVMix\_conv) \textcolor{keyword}{call }cvmix\_conv\_end(cs\%CVMix\_conv\_csp)}
\DoxyCodeLine{3752 }
\DoxyCodeLine{3753   \textcolor{keywordflow}{if} (cs\%use\_energetic\_PBL) \&}
\DoxyCodeLine{3754     \textcolor{keyword}{call }energetic\_pbl\_end(cs\%energetic\_PBL\_CSp)}
\DoxyCodeLine{3755   \textcolor{keywordflow}{if} (cs\%debug\_energy\_req) \&}
\DoxyCodeLine{3756     \textcolor{keyword}{call }diapyc\_energy\_req\_end(cs\%diapyc\_en\_rec\_CSp)}
\DoxyCodeLine{3757 }
\DoxyCodeLine{3758   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs\%optics)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3759     \textcolor{keyword}{call }opacity\_end(cs\%opacity\_CSp, cs\%optics)}
\DoxyCodeLine{3760     \textcolor{keyword}{deallocate}(cs\%optics)}
\DoxyCodeLine{3761 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3762 }
\DoxyCodeLine{3763   \textcolor{comment}{! GMM, the following is commented out because arrays in}}
\DoxyCodeLine{3764   \textcolor{comment}{! CS\%diag\_grids\_prev are neither pointers or allocatables}}
\DoxyCodeLine{3765   \textcolor{comment}{! and, therefore, cannot be deallocated.}}
\DoxyCodeLine{3766 }
\DoxyCodeLine{3767   \textcolor{comment}{!call diag\_grid\_storage\_end(CS\%diag\_grids\_prev)}}
\DoxyCodeLine{3768 }
\DoxyCodeLine{3769   \textcolor{keyword}{deallocate}(cs)}
\DoxyCodeLine{3770 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diabatic__driver_a51d273bae7e5d2217fa5498620532888}\label{namespacemom__diabatic__driver_a51d273bae7e5d2217fa5498620532888}} 
\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}!diabatic\_driver\_init@{diabatic\_driver\_init}}
\index{diabatic\_driver\_init@{diabatic\_driver\_init}!mom\_diabatic\_driver@{mom\_diabatic\_driver}}
\doxysubsubsection{\texorpdfstring{diabatic\_driver\_init()}{diabatic\_driver\_init()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+diabatic\+\_\+driver\+::diabatic\+\_\+driver\+\_\+init (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), target}]{Time,  }\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(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file,  }\item[{logical, intent(in)}]{use\+A\+L\+Ealgorithm,  }\item[{type(diag\+\_\+ctrl), intent(inout), target}]{diag,  }\item[{type(accel\+\_\+diag\+\_\+ptrs), intent(inout)}]{A\+Dp,  }\item[{type(cont\+\_\+diag\+\_\+ptrs), intent(inout)}]{C\+Dp,  }\item[{type(\mbox{\hyperlink{structmom__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), pointer}]{CS,  }\item[{type(tracer\+\_\+flow\+\_\+control\+\_\+cs), pointer}]{tracer\+\_\+flow\+\_\+\+C\+Sp,  }\item[{type(sponge\+\_\+cs), pointer}]{sponge\+\_\+\+C\+Sp,  }\item[{type(ale\+\_\+sponge\+\_\+cs), pointer}]{A\+L\+E\+\_\+sponge\+\_\+\+C\+Sp }\end{DoxyParamCaption})}



This routine initializes the diabatic driver module. 


\begin{DoxyParams}[1]{Parameters}
 & {\em time} & model time \\
\hline
\mbox{\texttt{ in,out}}  & {\em g} & model grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & model vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em param\+\_\+file} & file to parse for parameter values \\
\hline
\mbox{\texttt{ in}}  & {\em usealealgorithm} & logical for whether to use A\+LE remapping \\
\hline
\mbox{\texttt{ in,out}}  & {\em diag} & structure to regulate diagnostic output \\
\hline
\mbox{\texttt{ in,out}}  & {\em adp} & pointers to accelerations in momentum equations, to enable diagnostics, like energy budgets \\
\hline
\mbox{\texttt{ in,out}}  & {\em cdp} & pointers to terms in continuity equations \\
\hline
 & {\em cs} & module control structure \\
\hline
 & {\em tracer\+\_\+flow\+\_\+csp} & pointer to control structure of the tracer flow control module \\
\hline
 & {\em sponge\+\_\+csp} & pointer to the sponge module control structure \\
\hline
 & {\em ale\+\_\+sponge\+\_\+csp} & pointer to the A\+LE sponge module control structure \\
\hline
\end{DoxyParams}


Definition at line 3147 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{3147   \textcolor{keywordtype}{type}(time\_type), \textcolor{keywordtype}{target}                :: Time\textcolor{comment}{             !< model time}}
\DoxyCodeLine{3148   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{                !< model grid structure}}
\DoxyCodeLine{3149   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{               !< model vertical grid structure}}
\DoxyCodeLine{3150   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{               !< A dimensional unit scaling type}}
\DoxyCodeLine{3151   \textcolor{keywordtype}{type}(param\_file\_type),   \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{       !< file to parse for parameter values}}
\DoxyCodeLine{3152   \textcolor{keywordtype}{logical},                 \textcolor{keywordtype}{intent(in)}    :: useALEalgorithm\textcolor{comment}{  !< logical for whether to use ALE remapping}}
\DoxyCodeLine{3153   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{             !< structure to regulate diagnostic output}}
\DoxyCodeLine{3154   \textcolor{keywordtype}{type}(accel\_diag\_ptrs),   \textcolor{keywordtype}{intent(inout)} :: ADp\textcolor{comment}{              !< pointers to accelerations in momentum equations,}}
\DoxyCodeLine{3155 \textcolor{comment}{                                                             !! to enable diagnostics, like energy budgets}}
\DoxyCodeLine{3156   \textcolor{keywordtype}{type}(cont\_diag\_ptrs),    \textcolor{keywordtype}{intent(inout)} :: CDp\textcolor{comment}{              !< pointers to terms in continuity equations}}
\DoxyCodeLine{3157   \textcolor{keywordtype}{type}(diabatic\_CS),       \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{               !< module control structure}}
\DoxyCodeLine{3158   \textcolor{keywordtype}{type}(tracer\_flow\_control\_CS), \textcolor{keywordtype}{pointer}  :: tracer\_flow\_CSp\textcolor{comment}{  !< pointer to control structure of the}}
\DoxyCodeLine{3159 \textcolor{comment}{                                                             !! tracer flow control module}}
\DoxyCodeLine{3160   \textcolor{keywordtype}{type}(sponge\_CS),         \textcolor{keywordtype}{pointer}       :: sponge\_CSp\textcolor{comment}{       !< pointer to the sponge module control structure}}
\DoxyCodeLine{3161   \textcolor{keywordtype}{type}(ALE\_sponge\_CS),     \textcolor{keywordtype}{pointer}       :: ALE\_sponge\_CSp\textcolor{comment}{   !< pointer to the ALE sponge module control structure}}
\DoxyCodeLine{3162 }
\DoxyCodeLine{3163 \textcolor{keywordtype}{  real}    :: Kd  \textcolor{comment}{! A diffusivity used in the default for other tracer diffusivities, in MKS units [m2 s-\/1]}}
\DoxyCodeLine{3164   \textcolor{keywordtype}{integer} :: num\_mode}
\DoxyCodeLine{3165   \textcolor{keywordtype}{logical} :: use\_temperature}
\DoxyCodeLine{3166   \textcolor{keywordtype}{character(len=20)} :: EN1, EN2, EN3}
\DoxyCodeLine{3167 }
\DoxyCodeLine{3168 \textcolor{comment}{! This "include" declares and sets the variable "version".}}
\DoxyCodeLine{3169 \textcolor{preprocessor}{\#include "version\_variable.h"}}
\DoxyCodeLine{3170 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_diabatic\_driver"} \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{3171   \textcolor{keywordtype}{character(len=48)}  :: thickness\_units}
\DoxyCodeLine{3172   \textcolor{keywordtype}{character(len=40)}  :: var\_name}
\DoxyCodeLine{3173   \textcolor{keywordtype}{character(len=160)} :: var\_descript}
\DoxyCodeLine{3174   \textcolor{keywordtype}{integer} :: isd, ied, jsd, jed, IsdB, IedB, JsdB, JedB, nz, nbands, m}
\DoxyCodeLine{3175   isd  = g\%isd  ; ied  = g\%ied  ; jsd  = g\%jsd  ; jed  = g\%jed ; nz = g\%ke}
\DoxyCodeLine{3176   isdb = g\%IsdB ; iedb = g\%IedB ; jsdb = g\%JsdB ; jedb = g\%JedB}
\DoxyCodeLine{3177 }
\DoxyCodeLine{3178   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3179     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"diabatic\_driver\_init called with an "}// \&}
\DoxyCodeLine{3180                             \textcolor{stringliteral}{"associated control structure."})}
\DoxyCodeLine{3181     \textcolor{keywordflow}{return}}
\DoxyCodeLine{3182   \textcolor{keywordflow}{else}}
\DoxyCodeLine{3183     \textcolor{keyword}{allocate}(cs)}
\DoxyCodeLine{3184 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3185 }
\DoxyCodeLine{3186   cs\%diag => diag}
\DoxyCodeLine{3187   cs\%Time => time}
\DoxyCodeLine{3188 }
\DoxyCodeLine{3189   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tracer\_flow\_csp)) cs\%tracer\_flow\_CSp => tracer\_flow\_csp}
\DoxyCodeLine{3190   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(sponge\_csp))      cs\%sponge\_CSp      => sponge\_csp}
\DoxyCodeLine{3191   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(ale\_sponge\_csp))  cs\%ALE\_sponge\_CSp  => ale\_sponge\_csp}
\DoxyCodeLine{3192 }
\DoxyCodeLine{3193   cs\%useALEalgorithm = usealealgorithm}
\DoxyCodeLine{3194   cs\%bulkmixedlayer = (gv\%nkml > 0)}
\DoxyCodeLine{3195 }
\DoxyCodeLine{3196   \textcolor{comment}{! Set default, read and log parameters}}
\DoxyCodeLine{3197   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \&}
\DoxyCodeLine{3198                    \textcolor{stringliteral}{"The following parameters are used for diabatic processes."}, \&}
\DoxyCodeLine{3199                    log\_to\_all=.true., debugging=.true.)}
\DoxyCodeLine{3200   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_LEGACY\_DIABATIC\_DRIVER"}, cs\%use\_legacy\_diabatic, \&}
\DoxyCodeLine{3201                  \textcolor{stringliteral}{"If true, use a legacy version of the diabatic subroutine. "}//\&}
\DoxyCodeLine{3202                  \textcolor{stringliteral}{"This is temporary and is needed to avoid change in answers."}, \&}
\DoxyCodeLine{3203                  default=.true.)}
\DoxyCodeLine{3204   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"SPONGE"}, cs\%use\_sponge, \&}
\DoxyCodeLine{3205                  \textcolor{stringliteral}{"If true, sponges may be applied anywhere in the domain. "}//\&}
\DoxyCodeLine{3206                  \textcolor{stringliteral}{"The exact location and properties of those sponges are "}//\&}
\DoxyCodeLine{3207                  \textcolor{stringliteral}{"specified via calls to initialize\_sponge and possibly "}//\&}
\DoxyCodeLine{3208                  \textcolor{stringliteral}{"set\_up\_sponge\_field."}, default=.false.)}
\DoxyCodeLine{3209   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENABLE\_THERMODYNAMICS"}, use\_temperature, \&}
\DoxyCodeLine{3210                  \textcolor{stringliteral}{"If true, temperature and salinity are used as state "}//\&}
\DoxyCodeLine{3211                  \textcolor{stringliteral}{"variables."}, default=.true.)}
\DoxyCodeLine{3212   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ENERGETICS\_SFC\_PBL"}, cs\%use\_energetic\_PBL, \&}
\DoxyCodeLine{3213                  \textcolor{stringliteral}{"If true, use an implied energetics planetary boundary "}//\&}
\DoxyCodeLine{3214                  \textcolor{stringliteral}{"layer scheme to determine the diffusivity and viscosity "}//\&}
\DoxyCodeLine{3215                  \textcolor{stringliteral}{"in the surface boundary layer."}, default=.false.)}
\DoxyCodeLine{3216   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"EPBL\_IS\_ADDITIVE"}, cs\%ePBL\_is\_additive, \&}
\DoxyCodeLine{3217                  \textcolor{stringliteral}{"If true, the diffusivity from ePBL is added to all "}//\&}
\DoxyCodeLine{3218                  \textcolor{stringliteral}{"other diffusivities. Otherwise, the larger of kappa-\/shear "}//\&}
\DoxyCodeLine{3219                  \textcolor{stringliteral}{"and ePBL diffusivities are used."}, default=.true.)}
\DoxyCodeLine{3220   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"PRANDTL\_EPBL"}, cs\%ePBL\_Prandtl, \&}
\DoxyCodeLine{3221                  \textcolor{stringliteral}{"The Prandtl number used by ePBL to convert vertical diffusivities into "}//\&}
\DoxyCodeLine{3222                  \textcolor{stringliteral}{"viscosities."}, default=1.0, units=\textcolor{stringliteral}{"nondim"}, do\_not\_log=.not.cs\%use\_energetic\_PBL)}
\DoxyCodeLine{3223   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_KPP"}, cs\%use\_KPP, \&}
\DoxyCodeLine{3224                  \textcolor{stringliteral}{"If true, turns on the [CVMix] KPP scheme of Large et al., 1994, "}//\&}
\DoxyCodeLine{3225                  \textcolor{stringliteral}{"to calculate diffusivities and non-\/local transport in the OBL."}, \&}
\DoxyCodeLine{3226                  default=.false., do\_not\_log=.true.)}
\DoxyCodeLine{3227   cs\%use\_CVMix\_ddiff = cvmix\_ddiff\_is\_used(param\_file)}
\DoxyCodeLine{3228 }
\DoxyCodeLine{3229   cs\%use\_kappa\_shear = kappa\_shear\_is\_used(param\_file)}
\DoxyCodeLine{3230   cs\%use\_CVMix\_shear = cvmix\_shear\_is\_used(param\_file)}
\DoxyCodeLine{3231 }
\DoxyCodeLine{3232   \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3233     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"ML\_MIX\_FIRST"}, cs\%ML\_mix\_first, \&}
\DoxyCodeLine{3234                  \textcolor{stringliteral}{"The fraction of the mixed layer mixing that is applied "}//\&}
\DoxyCodeLine{3235                  \textcolor{stringliteral}{"before interior diapycnal mixing.  0 by default."}, \&}
\DoxyCodeLine{3236                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)}
\DoxyCodeLine{3237     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"NKBL"}, cs\%nkbl, default=2, do\_not\_log=.true.)}
\DoxyCodeLine{3238   \textcolor{keywordflow}{else}}
\DoxyCodeLine{3239     cs\%ML\_mix\_first = 0.0}
\DoxyCodeLine{3240 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3241   \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3242     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DO\_GEOTHERMAL"}, cs\%use\_geothermal, \&}
\DoxyCodeLine{3243                  \textcolor{stringliteral}{"If true, apply geothermal heating."}, default=.false.)}
\DoxyCodeLine{3244   \textcolor{keywordflow}{else}}
\DoxyCodeLine{3245     cs\%use\_geothermal = .false.}
\DoxyCodeLine{3246 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3247   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"INTERNAL\_TIDES"}, cs\%use\_int\_tides, \&}
\DoxyCodeLine{3248                  \textcolor{stringliteral}{"If true, use the code that advances a separate set of "}//\&}
\DoxyCodeLine{3249                  \textcolor{stringliteral}{"equations for the internal tide energy density."}, default=.false.)}
\DoxyCodeLine{3250   cs\%nMode = 1}
\DoxyCodeLine{3251   \textcolor{keywordflow}{if} (cs\%use\_int\_tides) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3252     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"INTERNAL\_TIDE\_MODES"}, cs\%nMode, \&}
\DoxyCodeLine{3253                  \textcolor{stringliteral}{"The number of distinct internal tide modes "}//\&}
\DoxyCodeLine{3254                  \textcolor{stringliteral}{"that will be calculated."}, default=1, do\_not\_log=.true.)}
\DoxyCodeLine{3255     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"UNIFORM\_TEST\_CG"}, cs\%uniform\_test\_cg, \&}
\DoxyCodeLine{3256                  \textcolor{stringliteral}{"If positive, a uniform group velocity of internal tide for test case"}, \&}
\DoxyCodeLine{3257                  default=-\/1., units=\textcolor{stringliteral}{"m s-\/1"}, scale=us\%m\_s\_to\_L\_T)}
\DoxyCodeLine{3258 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3259 }
\DoxyCodeLine{3260   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MASSLESS\_MATCH\_TARGETS"}, \&}
\DoxyCodeLine{3261                                 cs\%massless\_match\_targets, \&}
\DoxyCodeLine{3262                  \textcolor{stringliteral}{"If true, the temperature and salinity of massless layers "}//\&}
\DoxyCodeLine{3263                  \textcolor{stringliteral}{"are kept consistent with their target densities. "}//\&}
\DoxyCodeLine{3264                  \textcolor{stringliteral}{"Otherwise the properties of massless layers evolve "}//\&}
\DoxyCodeLine{3265                  \textcolor{stringliteral}{"diffusively to match massive neighboring layers."}, \&}
\DoxyCodeLine{3266                  default=.true.)}
\DoxyCodeLine{3267 }
\DoxyCodeLine{3268   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"AGGREGATE\_FW\_FORCING"}, cs\%aggregate\_FW\_forcing, \&}
\DoxyCodeLine{3269                  \textcolor{stringliteral}{"If true, the net incoming and outgoing fresh water fluxes are combined "}//\&}
\DoxyCodeLine{3270                  \textcolor{stringliteral}{"and applied as either incoming or outgoing depending on the sign of the net. "}//\&}
\DoxyCodeLine{3271                  \textcolor{stringliteral}{"If false, the net incoming fresh water flux is added to the model and "}//\&}
\DoxyCodeLine{3272                  \textcolor{stringliteral}{"thereafter the net outgoing is removed from the topmost non-\/vanished "}//\&}
\DoxyCodeLine{3273                  \textcolor{stringliteral}{"layers of the updated state."}, default=.true.)}
\DoxyCodeLine{3274 }
\DoxyCodeLine{3275   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEBUG"}, cs\%debug, \&}
\DoxyCodeLine{3276                  \textcolor{stringliteral}{"If true, write out verbose debugging data."}, \&}
\DoxyCodeLine{3277                  default=.false., debuggingparam=.true.)}
\DoxyCodeLine{3278   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEBUG\_CONSERVATION"}, cs\%debugConservation, \&}
\DoxyCodeLine{3279                  \textcolor{stringliteral}{"If true, monitor conservation and extrema."}, \&}
\DoxyCodeLine{3280                  default=.false., debuggingparam=.true.)}
\DoxyCodeLine{3281 }
\DoxyCodeLine{3282   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEBUG\_ENERGY\_REQ"}, cs\%debug\_energy\_req, \&}
\DoxyCodeLine{3283                  \textcolor{stringliteral}{"If true, debug the energy requirements."}, default=.false., do\_not\_log=.true.)}
\DoxyCodeLine{3284   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MIX\_BOUNDARY\_TRACERS"}, cs\%mix\_boundary\_tracers, \&}
\DoxyCodeLine{3285                  \textcolor{stringliteral}{"If true, mix the passive tracers in massless layers at "}//\&}
\DoxyCodeLine{3286                  \textcolor{stringliteral}{"the bottom into the interior as though a diffusivity of "}//\&}
\DoxyCodeLine{3287                  \textcolor{stringliteral}{"KD\_MIN\_TR were operating."}, default=.true.)}
\DoxyCodeLine{3288 }
\DoxyCodeLine{3289   \textcolor{keywordflow}{if} (cs\%mix\_boundary\_tracers) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3290     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KD"}, kd, default=0.0)}
\DoxyCodeLine{3291     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KD\_MIN\_TR"}, cs\%Kd\_min\_tr, \&}
\DoxyCodeLine{3292                  \textcolor{stringliteral}{"A minimal diffusivity that should always be applied to "}//\&}
\DoxyCodeLine{3293                  \textcolor{stringliteral}{"tracers, especially in massless layers near the bottom. "}//\&}
\DoxyCodeLine{3294                  \textcolor{stringliteral}{"The default is 0.1*KD."}, units=\textcolor{stringliteral}{"m2 s-\/1"}, default=0.1*kd, scale=us\%m2\_s\_to\_Z2\_T)}
\DoxyCodeLine{3295     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KD\_BBL\_TR"}, cs\%Kd\_BBL\_tr, \&}
\DoxyCodeLine{3296                  \textcolor{stringliteral}{"A bottom boundary layer tracer diffusivity that will "}//\&}
\DoxyCodeLine{3297                  \textcolor{stringliteral}{"allow for explicitly specified bottom fluxes. The "}//\&}
\DoxyCodeLine{3298                  \textcolor{stringliteral}{"entrainment at the bottom is at least sqrt(Kd\_BBL\_tr*dt) "}//\&}
\DoxyCodeLine{3299                  \textcolor{stringliteral}{"over the same distance."}, units=\textcolor{stringliteral}{"m2 s-\/1"}, default=0., scale=us\%m2\_s\_to\_Z2\_T)}
\DoxyCodeLine{3300 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3301 }
\DoxyCodeLine{3302   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"TRACER\_TRIDIAG"}, cs\%tracer\_tridiag, \&}
\DoxyCodeLine{3303                  \textcolor{stringliteral}{"If true, use the passive tracer tridiagonal solver for T and S"}, \&}
\DoxyCodeLine{3304                  default=.false.)}
\DoxyCodeLine{3305 }
\DoxyCodeLine{3306   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MINIMUM\_FORCING\_DEPTH"}, cs\%minimum\_forcing\_depth, \&}
\DoxyCodeLine{3307                  \textcolor{stringliteral}{"The smallest depth over which forcing can be applied. This "}//\&}
\DoxyCodeLine{3308                  \textcolor{stringliteral}{"only takes effect when near-\/surface layers become thin "}//\&}
\DoxyCodeLine{3309                  \textcolor{stringliteral}{"relative to this scale, in which case the forcing tendencies "}//\&}
\DoxyCodeLine{3310                  \textcolor{stringliteral}{"scaled down by distributing the forcing over this depth scale."}, \&}
\DoxyCodeLine{3311                  units=\textcolor{stringliteral}{"m"}, default=0.001, scale=gv\%m\_to\_H)}
\DoxyCodeLine{3312   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"EVAP\_CFL\_LIMIT"}, cs\%evap\_CFL\_limit, \&}
\DoxyCodeLine{3313                  \textcolor{stringliteral}{"The largest fraction of a layer than can be lost to forcing "}//\&}
\DoxyCodeLine{3314                  \textcolor{stringliteral}{"(e.g. evaporation, sea-\/ice formation) in one time-\/step. The unused "}//\&}
\DoxyCodeLine{3315                  \textcolor{stringliteral}{"mass loss is passed down through the column."}, \&}
\DoxyCodeLine{3316                  units=\textcolor{stringliteral}{"nondim"}, default=0.8)}
\DoxyCodeLine{3317 }
\DoxyCodeLine{3318 }
\DoxyCodeLine{3319   \textcolor{comment}{! Register all available diagnostics for this module.}}
\DoxyCodeLine{3320   thickness\_units = get\_thickness\_units(gv)}
\DoxyCodeLine{3321 }
\DoxyCodeLine{3322   cs\%id\_ea\_t = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'ea\_t'}, diag\%axesTL, time, \&}
\DoxyCodeLine{3323       \textcolor{stringliteral}{'Layer (heat) entrainment from above per timestep'}, \textcolor{stringliteral}{'m'}, conversion=gv\%H\_to\_m)}
\DoxyCodeLine{3324   cs\%id\_eb\_t = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'eb\_t'}, diag\%axesTL, time, \&}
\DoxyCodeLine{3325       \textcolor{stringliteral}{'Layer (heat) entrainment from below per timestep'}, \textcolor{stringliteral}{'m'}, conversion=gv\%H\_to\_m)}
\DoxyCodeLine{3326   cs\%id\_ea\_s = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'ea\_s'}, diag\%axesTL, time, \&}
\DoxyCodeLine{3327       \textcolor{stringliteral}{'Layer (salt) entrainment from above per timestep'}, \textcolor{stringliteral}{'m'}, conversion=gv\%H\_to\_m)}
\DoxyCodeLine{3328   cs\%id\_eb\_s = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'eb\_s'}, diag\%axesTL, time, \&}
\DoxyCodeLine{3329       \textcolor{stringliteral}{'Layer (salt) entrainment from below per timestep'}, \textcolor{stringliteral}{'m'}, conversion=gv\%H\_to\_m)}
\DoxyCodeLine{3330   \textcolor{comment}{! used by layer diabatic}}
\DoxyCodeLine{3331   cs\%id\_ea = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'ea'}, diag\%axesTL, time, \&}
\DoxyCodeLine{3332       \textcolor{stringliteral}{'Layer entrainment from above per timestep'}, \textcolor{stringliteral}{'m'}, conversion=gv\%H\_to\_m)}
\DoxyCodeLine{3333   cs\%id\_eb = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'eb'}, diag\%axesTL, time, \&}
\DoxyCodeLine{3334       \textcolor{stringliteral}{'Layer entrainment from below per timestep'}, \textcolor{stringliteral}{'m'}, conversion=gv\%H\_to\_m)}
\DoxyCodeLine{3335   cs\%id\_wd = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'wd'}, diag\%axesTi, time, \&}
\DoxyCodeLine{3336     \textcolor{stringliteral}{'Diapycnal velocity'}, \textcolor{stringliteral}{'m s-\/1'}, conversion=gv\%H\_to\_m)}
\DoxyCodeLine{3337   \textcolor{keywordflow}{if} (cs\%id\_wd > 0) \textcolor{keyword}{call }safe\_alloc\_ptr(cdp\%diapyc\_vel,isd,ied,jsd,jed,nz+1)}
\DoxyCodeLine{3338 }
\DoxyCodeLine{3339   cs\%id\_dudt\_dia = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'dudt\_dia'}, diag\%axesCuL, time, \&}
\DoxyCodeLine{3340       \textcolor{stringliteral}{'Zonal Acceleration from Diapycnal Mixing'}, \textcolor{stringliteral}{'m s-\/2'}, conversion=us\%L\_T2\_to\_m\_s2)}
\DoxyCodeLine{3341   cs\%id\_dvdt\_dia = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'dvdt\_dia'}, diag\%axesCvL, time, \&}
\DoxyCodeLine{3342       \textcolor{stringliteral}{'Meridional Acceleration from Diapycnal Mixing'}, \textcolor{stringliteral}{'m s-\/2'}, conversion=us\%L\_T2\_to\_m\_s2)}
\DoxyCodeLine{3343 }
\DoxyCodeLine{3344   cs\%id\_hf\_dudt\_dia\_2d = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'hf\_dudt\_dia\_2d'}, diag\%axesCu1, time, \&}
\DoxyCodeLine{3345       \textcolor{stringliteral}{'Depth-\/sum Fractional Thickness-\/weighted Zonal Acceleration from Diapycnal Mixing'}, \textcolor{stringliteral}{'m s-\/2'}, \&}
\DoxyCodeLine{3346       conversion=us\%L\_T2\_to\_m\_s2)}
\DoxyCodeLine{3347   \textcolor{keywordflow}{if} (cs\%id\_hf\_dudt\_dia\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3348     \textcolor{keyword}{call }safe\_alloc\_ptr(adp\%diag\_hfrac\_u,isdb,iedb,jsd,jed,nz)}
\DoxyCodeLine{3349     \textcolor{keyword}{call }safe\_alloc\_ptr(adp\%du\_dt\_dia,isdb,iedb,jsd,jed,nz)}
\DoxyCodeLine{3350 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3351 }
\DoxyCodeLine{3352   cs\%id\_hf\_dvdt\_dia\_2d = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'hf\_dvdt\_dia\_2d'}, diag\%axesCv1, time, \&}
\DoxyCodeLine{3353       \textcolor{stringliteral}{'Depth-\/sum Fractional Thickness-\/weighted Meridional Acceleration from Diapycnal Mixing'}, \textcolor{stringliteral}{'m s-\/2'}, \&}
\DoxyCodeLine{3354       conversion=us\%L\_T2\_to\_m\_s2)}
\DoxyCodeLine{3355   \textcolor{keywordflow}{if} (cs\%id\_hf\_dvdt\_dia\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3356     \textcolor{keyword}{call }safe\_alloc\_ptr(adp\%diag\_hfrac\_v,isd,ied,jsd,jedb,nz)}
\DoxyCodeLine{3357     \textcolor{keyword}{call }safe\_alloc\_ptr(adp\%dv\_dt\_dia,isd,ied,jsdb,jedb,nz)}
\DoxyCodeLine{3358 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3359 }
\DoxyCodeLine{3360   \textcolor{keywordflow}{if} (cs\%use\_int\_tides) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3361     cs\%id\_cg1 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'cn1'}, diag\%axesT1, \&}
\DoxyCodeLine{3362                  time, \textcolor{stringliteral}{'First baroclinic mode (eigen) speed'}, \textcolor{stringliteral}{'m s-\/1'}, conversion=us\%L\_T\_to\_m\_s)}
\DoxyCodeLine{3363     \textcolor{keyword}{allocate}(cs\%id\_cn(cs\%nMode)) ; cs\%id\_cn(:) = -\/1}
\DoxyCodeLine{3364     \textcolor{keywordflow}{do} m=1,cs\%nMode}
\DoxyCodeLine{3365       \textcolor{keyword}{write}(var\_name, \textcolor{stringliteral}{'("cn\_mode",i1)'}) m}
\DoxyCodeLine{3366       \textcolor{keyword}{write}(var\_descript, \textcolor{stringliteral}{'("Baroclinic (eigen) speed of mode ",i1)'}) m}
\DoxyCodeLine{3367       cs\%id\_cn(m) = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},var\_name, diag\%axesT1, \&}
\DoxyCodeLine{3368                    time, var\_descript, \textcolor{stringliteral}{'m s-\/1'}, conversion=us\%L\_T\_to\_m\_s)}
\DoxyCodeLine{3369       \textcolor{keyword}{call }mom\_mesg(\textcolor{stringliteral}{"Registering "}//trim(var\_name)//\textcolor{stringliteral}{", Described as: "}//var\_descript, 5)}
\DoxyCodeLine{3370 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{3371 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3372 }
\DoxyCodeLine{3373   \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3374     cs\%id\_Tdif = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\textcolor{stringliteral}{"Tflx\_dia\_diff"}, diag\%axesTi, \&}
\DoxyCodeLine{3375         time, \textcolor{stringliteral}{"Diffusive diapycnal temperature flux across interfaces"}, \&}
\DoxyCodeLine{3376         \textcolor{stringliteral}{"degC m s-\/1"}, conversion=gv\%H\_to\_m*us\%s\_to\_T)}
\DoxyCodeLine{3377     cs\%id\_Tadv = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\textcolor{stringliteral}{"Tflx\_dia\_adv"}, diag\%axesTi, \&}
\DoxyCodeLine{3378         time, \textcolor{stringliteral}{"Advective diapycnal temperature flux across interfaces"}, \&}
\DoxyCodeLine{3379         \textcolor{stringliteral}{"degC m s-\/1"}, conversion=gv\%H\_to\_m*us\%s\_to\_T)}
\DoxyCodeLine{3380     cs\%id\_Sdif = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\textcolor{stringliteral}{"Sflx\_dia\_diff"}, diag\%axesTi, \&}
\DoxyCodeLine{3381         time, \textcolor{stringliteral}{"Diffusive diapycnal salnity flux across interfaces"}, \&}
\DoxyCodeLine{3382         \textcolor{stringliteral}{"psu m s-\/1"}, conversion=gv\%H\_to\_m*us\%s\_to\_T)}
\DoxyCodeLine{3383     cs\%id\_Sadv = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\textcolor{stringliteral}{"Sflx\_dia\_adv"}, diag\%axesTi, \&}
\DoxyCodeLine{3384         time, \textcolor{stringliteral}{"Advective diapycnal salnity flux across interfaces"}, \&}
\DoxyCodeLine{3385         \textcolor{stringliteral}{"psu m s-\/1"}, conversion=gv\%H\_to\_m*us\%s\_to\_T)}
\DoxyCodeLine{3386     cs\%id\_MLD\_003 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MLD\_003'}, diag\%axesT1, time, \&}
\DoxyCodeLine{3387         \textcolor{stringliteral}{'Mixed layer depth (delta rho = 0.03)'}, \textcolor{stringliteral}{'m'}, conversion=us\%Z\_to\_m, \&}
\DoxyCodeLine{3388         cmor\_field\_name=\textcolor{stringliteral}{'mlotst'}, cmor\_long\_name=\textcolor{stringliteral}{'Ocean Mixed Layer Thickness Defined by Sigma T'}, \&}
\DoxyCodeLine{3389         cmor\_standard\_name=\textcolor{stringliteral}{'ocean\_mixed\_layer\_thickness\_defined\_by\_sigma\_t'})}
\DoxyCodeLine{3390     cs\%id\_mlotstsq = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'mlotstsq'}, diag\%axesT1, time, \&}
\DoxyCodeLine{3391         long\_name=\textcolor{stringliteral}{'Square of Ocean Mixed Layer Thickness Defined by Sigma T'}, \&}
\DoxyCodeLine{3392         standard\_name=\textcolor{stringliteral}{'square\_of\_ocean\_mixed\_layer\_thickness\_defined\_by\_sigma\_t'}, \&}
\DoxyCodeLine{3393         units=\textcolor{stringliteral}{'m2'}, conversion=us\%Z\_to\_m**2)}
\DoxyCodeLine{3394     cs\%id\_MLD\_0125 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MLD\_0125'}, diag\%axesT1, time, \&}
\DoxyCodeLine{3395         \textcolor{stringliteral}{'Mixed layer depth (delta rho = 0.125)'}, \textcolor{stringliteral}{'m'}, conversion=us\%Z\_to\_m)}
\DoxyCodeLine{3396     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MLD\_EN\_VALS"}, cs\%MLD\_EN\_VALS, \&}
\DoxyCodeLine{3397          \textcolor{stringliteral}{"The energy values used to compute MLDs.  If not set (or all set to 0.), the "}//\&}
\DoxyCodeLine{3398          \textcolor{stringliteral}{"default will overwrite to 25., 2500., 250000."},units=\textcolor{stringliteral}{'J/m2'}, default=0., \&}
\DoxyCodeLine{3399          scale=us\%kg\_m3\_to\_R*us\%m\_to\_Z**3*us\%T\_to\_s**2)}
\DoxyCodeLine{3400     \textcolor{keywordflow}{if} ((cs\%MLD\_EN\_VALS(1)==0.).and.(cs\%MLD\_EN\_VALS(2)==0.).and.(cs\%MLD\_EN\_VALS(3)==0.)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3401       cs\%MLD\_EN\_VALS = (/25.*us\%kg\_m3\_to\_R*us\%m\_to\_Z*us\%m\_to\_L**2*us\%T\_to\_s**2,\&}
\DoxyCodeLine{3402            2500.*us\%kg\_m3\_to\_R*us\%m\_to\_Z*us\%m\_to\_L**2*us\%T\_to\_s**2,\&}
\DoxyCodeLine{3403            250000.*us\%kg\_m3\_to\_R*us\%m\_to\_Z*us\%m\_to\_L**2*us\%T\_to\_s**2/)}
\DoxyCodeLine{3404 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3405     \textcolor{keyword}{write}(en1,\textcolor{stringliteral}{'(F10.2)'}) cs\%MLD\_EN\_VALS(1)*us\%R\_to\_kg\_m3*us\%Z\_to\_m*us\%L\_to\_m**2*us\%s\_to\_T**2}
\DoxyCodeLine{3406     \textcolor{keyword}{write}(en2,\textcolor{stringliteral}{'(F10.2)'}) cs\%MLD\_EN\_VALS(2)*us\%R\_to\_kg\_m3*us\%Z\_to\_m*us\%L\_to\_m**2*us\%s\_to\_T**2}
\DoxyCodeLine{3407     \textcolor{keyword}{write}(en3,\textcolor{stringliteral}{'(F10.2)'}) cs\%MLD\_EN\_VALS(3)*us\%R\_to\_kg\_m3*us\%Z\_to\_m*us\%L\_to\_m**2*us\%s\_to\_T**2}
\DoxyCodeLine{3408     cs\%id\_MLD\_EN1 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MLD\_EN1'}, diag\%axesT1, time, \&}
\DoxyCodeLine{3409          \textcolor{stringliteral}{'Mixed layer depth for energy value set to '}//trim(en1)//\textcolor{stringliteral}{' J/m2 (Energy set by 1st MLD\_EN\_VALS)'}, \&}
\DoxyCodeLine{3410          \textcolor{stringliteral}{'m'}, conversion=us\%Z\_to\_m)}
\DoxyCodeLine{3411     cs\%id\_MLD\_EN2 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MLD\_EN2'}, diag\%axesT1, time, \&}
\DoxyCodeLine{3412          \textcolor{stringliteral}{'Mixed layer depth for energy value set to '}//trim(en2)//\textcolor{stringliteral}{' J/m2 (Energy set by 2nd MLD\_EN\_VALS)'}, \&}
\DoxyCodeLine{3413          \textcolor{stringliteral}{'m'}, conversion=us\%Z\_to\_m)}
\DoxyCodeLine{3414     cs\%id\_MLD\_EN3 = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MLD\_EN3'}, diag\%axesT1, time, \&}
\DoxyCodeLine{3415          \textcolor{stringliteral}{'Mixed layer depth for energy value set to '}//trim(en3)//\textcolor{stringliteral}{' J/m2 (Energy set by 3rd MLD\_EN\_VALS)'}, \&}
\DoxyCodeLine{3416          \textcolor{stringliteral}{'m'}, conversion=us\%Z\_to\_m)}
\DoxyCodeLine{3417     cs\%id\_subMLN2  = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'subML\_N2'}, diag\%axesT1, time, \&}
\DoxyCodeLine{3418         \textcolor{stringliteral}{'Squared buoyancy frequency below mixed layer'}, \textcolor{stringliteral}{'s-\/2'}, conversion=us\%s\_to\_T**2)}
\DoxyCodeLine{3419     cs\%id\_MLD\_user = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MLD\_user'}, diag\%axesT1, time, \&}
\DoxyCodeLine{3420         \textcolor{stringliteral}{'Mixed layer depth (used defined)'}, \textcolor{stringliteral}{'m'}, conversion=us\%Z\_to\_m)}
\DoxyCodeLine{3421 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3422   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DIAG\_MLD\_DENSITY\_DIFF"}, cs\%MLDdensityDifference, \&}
\DoxyCodeLine{3423                  \textcolor{stringliteral}{"The density difference used to determine a diagnostic mixed "}//\&}
\DoxyCodeLine{3424                  \textcolor{stringliteral}{"layer depth, MLD\_user, following the definition of Levitus 1982. "}//\&}
\DoxyCodeLine{3425                  \textcolor{stringliteral}{"The MLD is the depth at which the density is larger than the "}//\&}
\DoxyCodeLine{3426                  \textcolor{stringliteral}{"surface density by the specified amount."}, \&}
\DoxyCodeLine{3427                  units=\textcolor{stringliteral}{'kg/m3'}, default=0.1, scale=us\%kg\_m3\_to\_R)}
\DoxyCodeLine{3428   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DIAG\_DEPTH\_SUBML\_N2"}, cs\%dz\_subML\_N2, \&}
\DoxyCodeLine{3429                  \textcolor{stringliteral}{"The distance over which to calculate a diagnostic of the "}//\&}
\DoxyCodeLine{3430                  \textcolor{stringliteral}{"stratification at the base of the mixed layer."}, \&}
\DoxyCodeLine{3431                  units=\textcolor{stringliteral}{'m'}, default=50.0, scale=us\%m\_to\_Z)}
\DoxyCodeLine{3432 }
\DoxyCodeLine{3433   \textcolor{keywordflow}{if} (cs\%id\_dudt\_dia > 0) \textcolor{keyword}{call }safe\_alloc\_ptr(adp\%du\_dt\_dia,isdb,iedb,jsd,jed,nz)}
\DoxyCodeLine{3434   \textcolor{keywordflow}{if} (cs\%id\_dvdt\_dia > 0) \textcolor{keyword}{call }safe\_alloc\_ptr(adp\%dv\_dt\_dia,isd,ied,jsdb,jedb,nz)}
\DoxyCodeLine{3435 }
\DoxyCodeLine{3436   \textcolor{comment}{! diagnostics for values prior to diabatic and prior to ALE}}
\DoxyCodeLine{3437   cs\%id\_u\_predia = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'u\_predia'}, diag\%axesCuL, time, \&}
\DoxyCodeLine{3438       \textcolor{stringliteral}{'Zonal velocity before diabatic forcing'}, \textcolor{stringliteral}{'m s-\/1'}, conversion=us\%L\_T\_to\_m\_s)}
\DoxyCodeLine{3439   cs\%id\_v\_predia = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'v\_predia'}, diag\%axesCvL, time, \&}
\DoxyCodeLine{3440       \textcolor{stringliteral}{'Meridional velocity before diabatic forcing'}, \textcolor{stringliteral}{'m s-\/1'}, conversion=us\%L\_T\_to\_m\_s)}
\DoxyCodeLine{3441   cs\%id\_h\_predia = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'h\_predia'}, diag\%axesTL, time, \&}
\DoxyCodeLine{3442       \textcolor{stringliteral}{'Layer Thickness before diabatic forcing'}, \&}
\DoxyCodeLine{3443       trim(thickness\_units), conversion=gv\%H\_to\_MKS, v\_extensive=.true.)}
\DoxyCodeLine{3444   cs\%id\_e\_predia = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'e\_predia'}, diag\%axesTi, time, \&}
\DoxyCodeLine{3445       \textcolor{stringliteral}{'Interface Heights before diabatic forcing'}, \textcolor{stringliteral}{'m'})}
\DoxyCodeLine{3446   \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3447     cs\%id\_T\_predia = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'temp\_predia'}, diag\%axesTL, time, \&}
\DoxyCodeLine{3448         \textcolor{stringliteral}{'Potential Temperature'}, \textcolor{stringliteral}{'degC'})}
\DoxyCodeLine{3449     cs\%id\_S\_predia = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'salt\_predia'}, diag\%axesTL, time, \&}
\DoxyCodeLine{3450         \textcolor{stringliteral}{'Salinity'}, \textcolor{stringliteral}{'PSU'})}
\DoxyCodeLine{3451 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3452 }
\DoxyCodeLine{3453 }
\DoxyCodeLine{3454   \textcolor{comment}{!call set\_diffusivity\_init(Time, G, param\_file, diag, CS\%set\_diff\_CSp, CS\%int\_tide\_CSp)}}
\DoxyCodeLine{3455   cs\%id\_Kd\_interface = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kd\_interface'}, diag\%axesTi, time, \&}
\DoxyCodeLine{3456       \textcolor{stringliteral}{'Total diapycnal diffusivity at interfaces'}, \textcolor{stringliteral}{'m2 s-\/1'}, conversion=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{3457   \textcolor{keywordflow}{if} (cs\%use\_energetic\_PBL) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3458       cs\%id\_Kd\_ePBL = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kd\_ePBL'}, diag\%axesTi, time, \&}
\DoxyCodeLine{3459           \textcolor{stringliteral}{'ePBL diapycnal diffusivity at interfaces'}, \textcolor{stringliteral}{'m2 s-\/1'}, conversion=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{3460 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3461 }
\DoxyCodeLine{3462   cs\%id\_Kd\_heat = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kd\_heat'}, diag\%axesTi, time, \&}
\DoxyCodeLine{3463       \textcolor{stringliteral}{'Total diapycnal diffusivity for heat at interfaces'}, \textcolor{stringliteral}{'m2 s-\/1'}, conversion=us\%Z2\_T\_to\_m2\_s, \&}
\DoxyCodeLine{3464        cmor\_field\_name=\textcolor{stringliteral}{'difvho'},                                                   \&}
\DoxyCodeLine{3465        cmor\_standard\_name=\textcolor{stringliteral}{'ocean\_vertical\_heat\_diffusivity'},                       \&}
\DoxyCodeLine{3466        cmor\_long\_name=\textcolor{stringliteral}{'Ocean vertical heat diffusivity'})}
\DoxyCodeLine{3467   cs\%id\_Kd\_salt = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kd\_salt'}, diag\%axesTi, time, \&}
\DoxyCodeLine{3468       \textcolor{stringliteral}{'Total diapycnal diffusivity for salt at interfaces'}, \textcolor{stringliteral}{'m2 s-\/1'},  conversion=us\%Z2\_T\_to\_m2\_s, \&}
\DoxyCodeLine{3469        cmor\_field\_name=\textcolor{stringliteral}{'difvso'},                                                   \&}
\DoxyCodeLine{3470        cmor\_standard\_name=\textcolor{stringliteral}{'ocean\_vertical\_salt\_diffusivity'},                       \&}
\DoxyCodeLine{3471        cmor\_long\_name=\textcolor{stringliteral}{'Ocean vertical salt diffusivity'})}
\DoxyCodeLine{3472 }
\DoxyCodeLine{3473   \textcolor{comment}{! CS\%useKPP is set to True if KPP-\/scheme is to be used, False otherwise.}}
\DoxyCodeLine{3474   \textcolor{comment}{! KPP\_init() allocated CS\%KPP\_Csp and also sets CS\%KPPisPassive}}
\DoxyCodeLine{3475   cs\%useKPP = kpp\_init(param\_file, g, gv, us, diag, time, cs\%KPP\_CSp, passive=cs\%KPPisPassive)}
\DoxyCodeLine{3476   \textcolor{keywordflow}{if} (cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3477     \textcolor{keyword}{allocate}( cs\%KPP\_NLTheat(isd:ied,jsd:jed,nz+1) )   ; cs\%KPP\_NLTheat(:,:,:)   = 0.}
\DoxyCodeLine{3478     \textcolor{keyword}{allocate}( cs\%KPP\_NLTscalar(isd:ied,jsd:jed,nz+1) ) ; cs\%KPP\_NLTscalar(:,:,:) = 0.}
\DoxyCodeLine{3479 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3480   \textcolor{keywordflow}{if} (cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3481     \textcolor{keyword}{allocate}( cs\%KPP\_buoy\_flux(isd:ied,jsd:jed,nz+1) ) ; cs\%KPP\_buoy\_flux(:,:,:) = 0.}
\DoxyCodeLine{3482     \textcolor{keyword}{allocate}( cs\%KPP\_temp\_flux(isd:ied,jsd:jed) )      ; cs\%KPP\_temp\_flux(:,:)   = 0.}
\DoxyCodeLine{3483     \textcolor{keyword}{allocate}( cs\%KPP\_salt\_flux(isd:ied,jsd:jed) )      ; cs\%KPP\_salt\_flux(:,:)   = 0.}
\DoxyCodeLine{3484 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3485 }
\DoxyCodeLine{3486 }
\DoxyCodeLine{3487   \textcolor{comment}{! diagnostics for tendencies of temp and saln due to diabatic processes}}
\DoxyCodeLine{3488   \textcolor{comment}{! available only for ALE algorithm.}}
\DoxyCodeLine{3489   \textcolor{comment}{! diagnostics for tendencies of temp and heat due to frazil}}
\DoxyCodeLine{3490   cs\%id\_diabatic\_diff\_h = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'diabatic\_diff\_h'}, diag\%axesTL, time, \&}
\DoxyCodeLine{3491       long\_name=\textcolor{stringliteral}{'Cell thickness used during diabatic diffusion'}, \&}
\DoxyCodeLine{3492       units=\textcolor{stringliteral}{'m'}, conversion=gv\%H\_to\_m, v\_extensive=.true.)}
\DoxyCodeLine{3493   \textcolor{keywordflow}{if} (cs\%useALEalgorithm) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3494     cs\%id\_diabatic\_diff\_temp\_tend = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \&}
\DoxyCodeLine{3495         \textcolor{stringliteral}{'diabatic\_diff\_temp\_tendency'}, diag\%axesTL, time,              \&}
\DoxyCodeLine{3496         \textcolor{stringliteral}{'Diabatic diffusion temperature tendency'}, \textcolor{stringliteral}{'degC s-\/1'}, conversion=us\%s\_to\_T)}
\DoxyCodeLine{3497     \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_temp\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3498       cs\%diabatic\_diff\_tendency\_diag = .true.}
\DoxyCodeLine{3499 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3500 }
\DoxyCodeLine{3501     cs\%id\_diabatic\_diff\_saln\_tend = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\&}
\DoxyCodeLine{3502         \textcolor{stringliteral}{'diabatic\_diff\_saln\_tendency'}, diag\%axesTL, time,             \&}
\DoxyCodeLine{3503         \textcolor{stringliteral}{'Diabatic diffusion salinity tendency'}, \textcolor{stringliteral}{'psu s-\/1'}, conversion=us\%s\_to\_T)}
\DoxyCodeLine{3504     \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_saln\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3505       cs\%diabatic\_diff\_tendency\_diag = .true.}
\DoxyCodeLine{3506 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3507 }
\DoxyCodeLine{3508     cs\%id\_diabatic\_diff\_heat\_tend = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},                             \&}
\DoxyCodeLine{3509         \textcolor{stringliteral}{'diabatic\_heat\_tendency'}, diag\%axesTL, time,                                               \&}
\DoxyCodeLine{3510         \textcolor{stringliteral}{'Diabatic diffusion heat tendency'},                                                        \&}
\DoxyCodeLine{3511         \textcolor{stringliteral}{'W m-\/2'}, conversion=us\%QRZ\_T\_to\_W\_m2, cmor\_field\_name=\textcolor{stringliteral}{'opottempdiff'},            \&}
\DoxyCodeLine{3512         cmor\_standard\_name=\textcolor{stringliteral}{'tendency\_of\_sea\_water\_potential\_temperature\_expressed\_as\_heat\_content\_'}// \&}
\DoxyCodeLine{3513                            \textcolor{stringliteral}{'due\_to\_parameterized\_dianeutral\_mixing'},                               \&}
\DoxyCodeLine{3514         cmor\_long\_name=\textcolor{stringliteral}{'Tendency of sea water potential temperature expressed as heat content '}//  \&}
\DoxyCodeLine{3515                        \textcolor{stringliteral}{'due to parameterized dianeutral mixing'}, \&}
\DoxyCodeLine{3516         v\_extensive=.true.)}
\DoxyCodeLine{3517     \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_heat\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3518       cs\%diabatic\_diff\_tendency\_diag = .true.}
\DoxyCodeLine{3519 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3520 }
\DoxyCodeLine{3521     cs\%id\_diabatic\_diff\_salt\_tend = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},                   \&}
\DoxyCodeLine{3522         \textcolor{stringliteral}{'diabatic\_salt\_tendency'}, diag\%axesTL, time,                                     \&}
\DoxyCodeLine{3523         \textcolor{stringliteral}{'Diabatic diffusion of salt tendency'},                                           \&}
\DoxyCodeLine{3524         \textcolor{stringliteral}{'kg m-\/2 s-\/1'}, conversion=us\%RZ\_T\_to\_kg\_m2s, cmor\_field\_name=\textcolor{stringliteral}{'osaltdiff'}, \&}
\DoxyCodeLine{3525         cmor\_standard\_name=\textcolor{stringliteral}{'tendency\_of\_sea\_water\_salinity\_expressed\_as\_salt\_content\_'}// \&}
\DoxyCodeLine{3526                            \textcolor{stringliteral}{'due\_to\_parameterized\_dianeutral\_mixing'},                     \&}
\DoxyCodeLine{3527         cmor\_long\_name=\textcolor{stringliteral}{'Tendency of sea water salinity expressed as salt content '}//     \&}
\DoxyCodeLine{3528                        \textcolor{stringliteral}{'due to parameterized dianeutral mixing'}, \&}
\DoxyCodeLine{3529         v\_extensive=.true.)}
\DoxyCodeLine{3530     \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_salt\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3531       cs\%diabatic\_diff\_tendency\_diag = .true.}
\DoxyCodeLine{3532 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3533 }
\DoxyCodeLine{3534     \textcolor{comment}{! This diagnostic should equal to roundoff if all is working well.}}
\DoxyCodeLine{3535     cs\%id\_diabatic\_diff\_heat\_tend\_2d = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},                        \&}
\DoxyCodeLine{3536         \textcolor{stringliteral}{'diabatic\_heat\_tendency\_2d'}, diag\%axesT1, time,                                          \&}
\DoxyCodeLine{3537         \textcolor{stringliteral}{'Depth integrated diabatic diffusion heat tendency'},                                     \&}
\DoxyCodeLine{3538         \textcolor{stringliteral}{'W m-\/2'}, conversion=us\%QRZ\_T\_to\_W\_m2, cmor\_field\_name=\textcolor{stringliteral}{'opottempdiff\_2d'},      \&}
\DoxyCodeLine{3539         cmor\_standard\_name=\textcolor{stringliteral}{'tendency\_of\_sea\_water\_potential\_temperature\_expressed\_as\_heat\_content\_'}//\&}
\DoxyCodeLine{3540                            \textcolor{stringliteral}{'due\_to\_parameterized\_dianeutral\_mixing\_depth\_integrated'},            \&}
\DoxyCodeLine{3541         cmor\_long\_name=\textcolor{stringliteral}{'Tendency of sea water potential temperature expressed as heat content '}//\&}
\DoxyCodeLine{3542                        \textcolor{stringliteral}{'due to parameterized dianeutral mixing depth integrated'})}
\DoxyCodeLine{3543     \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_heat\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3544       cs\%diabatic\_diff\_tendency\_diag = .true.}
\DoxyCodeLine{3545 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3546 }
\DoxyCodeLine{3547     \textcolor{comment}{! This diagnostic should equal to roundoff if all is working well.}}
\DoxyCodeLine{3548     cs\%id\_diabatic\_diff\_salt\_tend\_2d = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},                \&}
\DoxyCodeLine{3549         \textcolor{stringliteral}{'diabatic\_salt\_tendency\_2d'}, diag\%axesT1, time,                                  \&}
\DoxyCodeLine{3550         \textcolor{stringliteral}{'Depth integrated diabatic diffusion salt tendency'},                             \&}
\DoxyCodeLine{3551         \textcolor{stringliteral}{'kg m-\/2 s-\/1'}, conversion=us\%RZ\_T\_to\_kg\_m2s, cmor\_field\_name=\textcolor{stringliteral}{'osaltdiff\_2d'},              \&}
\DoxyCodeLine{3552         cmor\_standard\_name=\textcolor{stringliteral}{'tendency\_of\_sea\_water\_salinity\_expressed\_as\_salt\_content\_'}// \&}
\DoxyCodeLine{3553                            \textcolor{stringliteral}{'due\_to\_parameterized\_dianeutral\_mixing\_depth\_integrated'},    \&}
\DoxyCodeLine{3554         cmor\_long\_name=\textcolor{stringliteral}{'Tendency of sea water salinity expressed as salt content '}//     \&}
\DoxyCodeLine{3555                        \textcolor{stringliteral}{'due to parameterized dianeutral mixing depth integrated'})}
\DoxyCodeLine{3556     \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_salt\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3557       cs\%diabatic\_diff\_tendency\_diag = .true.}
\DoxyCodeLine{3558 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3559 }
\DoxyCodeLine{3560     \textcolor{comment}{! diagnostics for tendencies of thickness temp and saln due to boundary forcing}}
\DoxyCodeLine{3561     \textcolor{comment}{! available only for ALE algorithm.}}
\DoxyCodeLine{3562   \textcolor{comment}{! diagnostics for tendencies of temp and heat due to frazil}}
\DoxyCodeLine{3563     cs\%id\_boundary\_forcing\_h = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'boundary\_forcing\_h'}, diag\%axesTL, time, \&}
\DoxyCodeLine{3564         long\_name=\textcolor{stringliteral}{'Cell thickness after applying boundary forcing'}, \&}
\DoxyCodeLine{3565         units=\textcolor{stringliteral}{'m'}, conversion=gv\%H\_to\_m, v\_extensive=.true.)}
\DoxyCodeLine{3566     cs\%id\_boundary\_forcing\_h\_tendency = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},   \&}
\DoxyCodeLine{3567         \textcolor{stringliteral}{'boundary\_forcing\_h\_tendency'}, diag\%axesTL, time,                \&}
\DoxyCodeLine{3568         \textcolor{stringliteral}{'Cell thickness tendency due to boundary forcing'}, \textcolor{stringliteral}{'m s-\/1'}, \&}
\DoxyCodeLine{3569         conversion=gv\%H\_to\_m*us\%s\_to\_T, v\_extensive=.true.)}
\DoxyCodeLine{3570     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_h\_tendency > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3571       cs\%boundary\_forcing\_tendency\_diag = .true.}
\DoxyCodeLine{3572 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3573 }
\DoxyCodeLine{3574     cs\%id\_boundary\_forcing\_temp\_tend = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\&}
\DoxyCodeLine{3575         \textcolor{stringliteral}{'boundary\_forcing\_temp\_tendency'}, diag\%axesTL, time,             \&}
\DoxyCodeLine{3576         \textcolor{stringliteral}{'Boundary forcing temperature tendency'}, \textcolor{stringliteral}{'degC s-\/1'}, conversion=us\%s\_to\_T)}
\DoxyCodeLine{3577     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_temp\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3578       cs\%boundary\_forcing\_tendency\_diag = .true.}
\DoxyCodeLine{3579 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3580 }
\DoxyCodeLine{3581     cs\%id\_boundary\_forcing\_saln\_tend = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\&}
\DoxyCodeLine{3582         \textcolor{stringliteral}{'boundary\_forcing\_saln\_tendency'}, diag\%axesTL, time,             \&}
\DoxyCodeLine{3583         \textcolor{stringliteral}{'Boundary forcing saln tendency'}, \textcolor{stringliteral}{'psu s-\/1'}, conversion=us\%s\_to\_T)}
\DoxyCodeLine{3584     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_saln\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3585       cs\%boundary\_forcing\_tendency\_diag = .true.}
\DoxyCodeLine{3586 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3587 }
\DoxyCodeLine{3588     cs\%id\_boundary\_forcing\_heat\_tend = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\&}
\DoxyCodeLine{3589         \textcolor{stringliteral}{'boundary\_forcing\_heat\_tendency'}, diag\%axesTL, time,             \&}
\DoxyCodeLine{3590         \textcolor{stringliteral}{'Boundary forcing heat tendency'}, \&}
\DoxyCodeLine{3591         \textcolor{stringliteral}{'W m-\/2'}, conversion=us\%QRZ\_T\_to\_W\_m2, v\_extensive = .true.)}
\DoxyCodeLine{3592     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_heat\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3593       cs\%boundary\_forcing\_tendency\_diag = .true.}
\DoxyCodeLine{3594 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3595 }
\DoxyCodeLine{3596     cs\%id\_boundary\_forcing\_salt\_tend = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\&}
\DoxyCodeLine{3597         \textcolor{stringliteral}{'boundary\_forcing\_salt\_tendency'}, diag\%axesTL, time,             \&}
\DoxyCodeLine{3598         \textcolor{stringliteral}{'Boundary forcing salt tendency'}, \textcolor{stringliteral}{'kg m-\/2 s-\/1'}, conversion=us\%RZ\_T\_to\_kg\_m2s, \&}
\DoxyCodeLine{3599         v\_extensive = .true.)}
\DoxyCodeLine{3600     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_salt\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3601       cs\%boundary\_forcing\_tendency\_diag = .true.}
\DoxyCodeLine{3602 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3603 }
\DoxyCodeLine{3604     \textcolor{comment}{! This diagnostic should equal to surface heat flux if all is working well.}}
\DoxyCodeLine{3605     cs\%id\_boundary\_forcing\_heat\_tend\_2d = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\&}
\DoxyCodeLine{3606         \textcolor{stringliteral}{'boundary\_forcing\_heat\_tendency\_2d'}, diag\%axesT1, time,             \&}
\DoxyCodeLine{3607         \textcolor{stringliteral}{'Depth integrated boundary forcing of ocean heat'}, \&}
\DoxyCodeLine{3608         \textcolor{stringliteral}{'W m-\/2'}, conversion=us\%QRZ\_T\_to\_W\_m2)}
\DoxyCodeLine{3609     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_heat\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3610       cs\%boundary\_forcing\_tendency\_diag = .true.}
\DoxyCodeLine{3611 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3612 }
\DoxyCodeLine{3613     \textcolor{comment}{! This diagnostic should equal to surface salt flux if all is working well.}}
\DoxyCodeLine{3614     cs\%id\_boundary\_forcing\_salt\_tend\_2d = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\&}
\DoxyCodeLine{3615         \textcolor{stringliteral}{'boundary\_forcing\_salt\_tendency\_2d'}, diag\%axesT1, time,             \&}
\DoxyCodeLine{3616         \textcolor{stringliteral}{'Depth integrated boundary forcing of ocean salt'}, \&}
\DoxyCodeLine{3617         \textcolor{stringliteral}{'kg m-\/2 s-\/1'}, conversion=us\%RZ\_T\_to\_kg\_m2s)}
\DoxyCodeLine{3618     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_salt\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3619       cs\%boundary\_forcing\_tendency\_diag = .true.}
\DoxyCodeLine{3620 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3621 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3622 }
\DoxyCodeLine{3623   \textcolor{comment}{! diagnostics for tendencies of temp and heat due to frazil}}
\DoxyCodeLine{3624   cs\%id\_frazil\_h = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'frazil\_h'}, diag\%axesTL, time, \&}
\DoxyCodeLine{3625       long\_name=\textcolor{stringliteral}{'Cell Thickness'}, standard\_name=\textcolor{stringliteral}{'cell\_thickness'}, \&}
\DoxyCodeLine{3626       units=\textcolor{stringliteral}{'m'}, conversion=gv\%H\_to\_m, v\_extensive=.true.)}
\DoxyCodeLine{3627 }
\DoxyCodeLine{3628   \textcolor{comment}{! diagnostic for tendency of temp due to frazil}}
\DoxyCodeLine{3629   cs\%id\_frazil\_temp\_tend = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\&}
\DoxyCodeLine{3630       \textcolor{stringliteral}{'frazil\_temp\_tendency'}, diag\%axesTL, time,             \&}
\DoxyCodeLine{3631       \textcolor{stringliteral}{'Temperature tendency due to frazil formation'}, \textcolor{stringliteral}{'degC s-\/1'}, conversion=us\%s\_to\_T)}
\DoxyCodeLine{3632   \textcolor{keywordflow}{if} (cs\%id\_frazil\_temp\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3633     cs\%frazil\_tendency\_diag = .true.}
\DoxyCodeLine{3634 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3635 }
\DoxyCodeLine{3636   \textcolor{comment}{! diagnostic for tendency of heat due to frazil}}
\DoxyCodeLine{3637   cs\%id\_frazil\_heat\_tend = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\&}
\DoxyCodeLine{3638       \textcolor{stringliteral}{'frazil\_heat\_tendency'}, diag\%axesTL, time,             \&}
\DoxyCodeLine{3639       \textcolor{stringliteral}{'Heat tendency due to frazil formation'}, \&}
\DoxyCodeLine{3640       \textcolor{stringliteral}{'W m-\/2'}, conversion=us\%QRZ\_T\_to\_W\_m2, v\_extensive=.true.)}
\DoxyCodeLine{3641   \textcolor{keywordflow}{if} (cs\%id\_frazil\_heat\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3642     cs\%frazil\_tendency\_diag = .true.}
\DoxyCodeLine{3643 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3644 }
\DoxyCodeLine{3645   \textcolor{comment}{! if all is working propertly, this diagnostic should equal to hfsifrazil}}
\DoxyCodeLine{3646   cs\%id\_frazil\_heat\_tend\_2d = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\&}
\DoxyCodeLine{3647       \textcolor{stringliteral}{'frazil\_heat\_tendency\_2d'}, diag\%axesT1, time,             \&}
\DoxyCodeLine{3648       \textcolor{stringliteral}{'Depth integrated heat tendency due to frazil formation'}, \&}
\DoxyCodeLine{3649       \textcolor{stringliteral}{'W m-\/2'}, conversion=us\%QRZ\_T\_to\_W\_m2)}
\DoxyCodeLine{3650   \textcolor{keywordflow}{if} (cs\%id\_frazil\_heat\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3651     cs\%frazil\_tendency\_diag = .true.}
\DoxyCodeLine{3652 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3653 }
\DoxyCodeLine{3654   \textcolor{keywordflow}{if} (cs\%frazil\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3655     \textcolor{keyword}{allocate}(cs\%frazil\_temp\_diag(isd:ied,jsd:jed,nz) ) ; cs\%frazil\_temp\_diag(:,:,:) = 0.}
\DoxyCodeLine{3656     \textcolor{keyword}{allocate}(cs\%frazil\_heat\_diag(isd:ied,jsd:jed,nz) ) ; cs\%frazil\_heat\_diag(:,:,:) = 0.}
\DoxyCodeLine{3657 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3658 }
\DoxyCodeLine{3659   \textcolor{comment}{! CS\%use\_CVMix\_conv is set to True if CVMix convection will be used, otherwise it is False.}}
\DoxyCodeLine{3660   cs\%use\_CVMix\_conv = cvmix\_conv\_init(time, g, gv, us, param\_file, diag, cs\%CVMix\_conv\_csp)}
\DoxyCodeLine{3661 }
\DoxyCodeLine{3662   \textcolor{keyword}{call }entrain\_diffusive\_init(time, g, gv, us, param\_file, diag, cs\%entrain\_diffusive\_CSp, \&}
\DoxyCodeLine{3663                               just\_read\_params=cs\%useALEalgorithm)}
\DoxyCodeLine{3664 }
\DoxyCodeLine{3665   \textcolor{comment}{! initialize the geothermal heating module}}
\DoxyCodeLine{3666   \textcolor{keywordflow}{if} (cs\%use\_geothermal) \&}
\DoxyCodeLine{3667     \textcolor{keyword}{call }geothermal\_init(time, g, gv, us, param\_file, diag, cs\%geothermal\_CSp, usealealgorithm)}
\DoxyCodeLine{3668 }
\DoxyCodeLine{3669   \textcolor{comment}{! initialize module for internal tide induced mixing}}
\DoxyCodeLine{3670   \textcolor{keywordflow}{if} (cs\%use\_int\_tides) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3671     \textcolor{keyword}{call }int\_tide\_input\_init(time, g, gv, us, param\_file, diag, cs\%int\_tide\_input\_CSp, \&}
\DoxyCodeLine{3672                              cs\%int\_tide\_input)}
\DoxyCodeLine{3673     \textcolor{keyword}{call }internal\_tides\_init(time, g, gv, us, param\_file, diag, cs\%int\_tide\_CSp)}
\DoxyCodeLine{3674 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3675 }
\DoxyCodeLine{3676   \textcolor{comment}{! initialize module for setting diffusivities}}
\DoxyCodeLine{3677   \textcolor{keyword}{call }set\_diffusivity\_init(time, g, gv, us, param\_file, diag, cs\%set\_diff\_CSp, cs\%int\_tide\_CSp, \&}
\DoxyCodeLine{3678                             halo\_ts=cs\%halo\_TS\_diff, double\_diffuse=cs\%double\_diffuse)}
\DoxyCodeLine{3679 }
\DoxyCodeLine{3680   \textcolor{keywordflow}{if} (cs\%useKPP .and. (cs\%double\_diffuse .and. .not.cs\%use\_CVMix\_ddiff)) \&}
\DoxyCodeLine{3681     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{'diabatic\_driver\_init: DOUBLE\_DIFFUSION (old method) does not work '}//\&}
\DoxyCodeLine{3682                           \textcolor{stringliteral}{'with KPP. Please set DOUBLE\_DIFFUSION=False and USE\_CVMIX\_DDIFF=True.'})}
\DoxyCodeLine{3683 }
\DoxyCodeLine{3684   \textcolor{comment}{! set up the clocks for this module}}
\DoxyCodeLine{3685   id\_clock\_entrain = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean diabatic entrain)'}, grain=clock\_module)}
\DoxyCodeLine{3686   \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \&}
\DoxyCodeLine{3687     id\_clock\_mixedlayer = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean mixed layer)'}, grain=clock\_module)}
\DoxyCodeLine{3688   id\_clock\_remap = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean vert remap)'}, grain=clock\_module)}
\DoxyCodeLine{3689   \textcolor{keywordflow}{if} (cs\%use\_geothermal) \&}
\DoxyCodeLine{3690     id\_clock\_geothermal = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean geothermal)'}, grain=clock\_routine)}
\DoxyCodeLine{3691   id\_clock\_set\_diffusivity = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean set\_diffusivity)'}, grain=clock\_module)}
\DoxyCodeLine{3692   id\_clock\_kpp = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean KPP)'}, grain=clock\_module)}
\DoxyCodeLine{3693   id\_clock\_tracers = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean tracer\_columns)'}, grain=clock\_module\_driver+5)}
\DoxyCodeLine{3694   \textcolor{keywordflow}{if} (cs\%use\_sponge) \&}
\DoxyCodeLine{3695     id\_clock\_sponge = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean sponges)'}, grain=clock\_module)}
\DoxyCodeLine{3696   id\_clock\_tridiag = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean diabatic tridiag)'}, grain=clock\_routine)}
\DoxyCodeLine{3697   id\_clock\_pass = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean diabatic message passing)'}, grain=clock\_routine)}
\DoxyCodeLine{3698   id\_clock\_differential\_diff = -\/1 ; \textcolor{keywordflow}{if} (cs\%double\_diffuse .and. .not.cs\%use\_CVMix\_ddiff) \&}
\DoxyCodeLine{3699     id\_clock\_differential\_diff = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean differential diffusion)'}, grain=clock\_routine)}
\DoxyCodeLine{3700 }
\DoxyCodeLine{3701   \textcolor{comment}{! initialize the auxiliary diabatic driver module}}
\DoxyCodeLine{3702   \textcolor{keyword}{call }diabatic\_aux\_init(time, g, gv, us, param\_file, diag, cs\%diabatic\_aux\_CSp, \&}
\DoxyCodeLine{3703                          cs\%useALEalgorithm, cs\%use\_energetic\_PBL)}
\DoxyCodeLine{3704 }
\DoxyCodeLine{3705   \textcolor{comment}{! initialize the boundary layer modules}}
\DoxyCodeLine{3706   \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \&}
\DoxyCodeLine{3707     \textcolor{keyword}{call }bulkmixedlayer\_init(time, g, gv, us, param\_file, diag, cs\%bulkmixedlayer\_CSp)}
\DoxyCodeLine{3708   \textcolor{keywordflow}{if} (cs\%use\_energetic\_PBL) \&}
\DoxyCodeLine{3709     \textcolor{keyword}{call }energetic\_pbl\_init(time, g, gv, us, param\_file, diag, cs\%energetic\_PBL\_CSp)}
\DoxyCodeLine{3710 }
\DoxyCodeLine{3711   \textcolor{keyword}{call }regularize\_layers\_init(time, g, gv, param\_file, diag, cs\%regularize\_layers\_CSp)}
\DoxyCodeLine{3712 }
\DoxyCodeLine{3713   \textcolor{keywordflow}{if} (cs\%debug\_energy\_req) \&}
\DoxyCodeLine{3714     \textcolor{keyword}{call }diapyc\_energy\_req\_init(time, g, gv, us, param\_file, diag, cs\%diapyc\_en\_rec\_CSp)}
\DoxyCodeLine{3715 }
\DoxyCodeLine{3716   \textcolor{comment}{! obtain information about the number of bands for penetrative shortwave}}
\DoxyCodeLine{3717   \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3718     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"PEN\_SW\_NBANDS"}, nbands, default=1)}
\DoxyCodeLine{3719     \textcolor{keywordflow}{if} (nbands > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3720       \textcolor{keyword}{allocate}(cs\%optics)}
\DoxyCodeLine{3721       \textcolor{keyword}{call }opacity\_init(time, g, gv, us, param\_file, diag, cs\%opacity\_CSp, cs\%optics)}
\DoxyCodeLine{3722 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3723 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3724 }
\DoxyCodeLine{3725   \textcolor{comment}{! Initialize the diagnostic grid storage}}
\DoxyCodeLine{3726   \textcolor{keyword}{call }diag\_grid\_storage\_init(cs\%diag\_grids\_prev, g, diag)}
\DoxyCodeLine{3727 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diabatic__driver_a9a32fd213024239e5818f0bd88764761}\label{namespacemom__diabatic__driver_a9a32fd213024239e5818f0bd88764761}} 
\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}!diagnose\_boundary\_forcing\_tendency@{diagnose\_boundary\_forcing\_tendency}}
\index{diagnose\_boundary\_forcing\_tendency@{diagnose\_boundary\_forcing\_tendency}!mom\_diabatic\_driver@{mom\_diabatic\_driver}}
\doxysubsubsection{\texorpdfstring{diagnose\_boundary\_forcing\_tendency()}{diagnose\_boundary\_forcing\_tendency()}}
{\footnotesize\ttfamily subroutine mom\+\_\+diabatic\+\_\+driver\+::diagnose\+\_\+boundary\+\_\+forcing\+\_\+tendency (\begin{DoxyParamCaption}\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{temp\+\_\+old,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{saln\+\_\+old,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h\+\_\+old,  }\item[{real, intent(in)}]{dt,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This routine diagnoses tendencies from application of boundary fluxes. These impacts are generally 3d, in particular for penetrative shortwave. Other fluxes contribute 3d in cases when the layers vanish or are very thin, in which case we distribute the flux into k $>$ 1 layers. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & ocean grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & ocean vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & points to updated thermodynamic fields \\
\hline
\mbox{\texttt{ in}}  & {\em h} & thickness after boundary flux application \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em temp\+\_\+old} & temperature prior to boundary flux application \mbox{[}degC\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em saln\+\_\+old} & salinity prior to boundary flux application \mbox{[}ppt\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em h\+\_\+old} & thickness prior to boundary flux application \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}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & module control structure \\
\hline
\end{DoxyParams}


Definition at line 2966 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{2966   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)} :: G\textcolor{comment}{        !< ocean grid structure}}
\DoxyCodeLine{2967   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)} :: GV\textcolor{comment}{       !< ocean vertical grid structure}}
\DoxyCodeLine{2968   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),   \textcolor{keywordtype}{intent(in)} :: tv\textcolor{comment}{       !< points to updated thermodynamic fields}}
\DoxyCodeLine{2969 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{2970                            \textcolor{keywordtype}{intent(in)} :: h\textcolor{comment}{        !< thickness after boundary flux application [H ~> m or kg m-\/2]}}
\DoxyCodeLine{2971 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{2972                            \textcolor{keywordtype}{intent(in)} :: temp\_old\textcolor{comment}{ !< temperature prior to boundary flux application [degC]}}
\DoxyCodeLine{2973 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{2974                            \textcolor{keywordtype}{intent(in)} :: saln\_old\textcolor{comment}{ !< salinity prior to boundary flux application [ppt]}}
\DoxyCodeLine{2975 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \&}
\DoxyCodeLine{2976                            \textcolor{keywordtype}{intent(in)} :: h\_old\textcolor{comment}{    !< thickness prior to boundary flux application [H ~> m or kg m-\/2]}}
\DoxyCodeLine{2977 \textcolor{keywordtype}{  real},                    \textcolor{keywordtype}{intent(in)} :: dt\textcolor{comment}{       !< time step [T ~> s]}}
\DoxyCodeLine{2978   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)} :: US\textcolor{comment}{       !< A dimensional unit scaling type}}
\DoxyCodeLine{2979   \textcolor{keywordtype}{type}(diabatic\_CS),       \textcolor{keywordtype}{pointer}    :: CS\textcolor{comment}{       !< module control structure}}
\DoxyCodeLine{2980 }
\DoxyCodeLine{2981   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{2982 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))} :: work\_3d}
\DoxyCodeLine{2983 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))}         :: work\_2d}
\DoxyCodeLine{2984 \textcolor{keywordtype}{  real} :: Idt  \textcolor{comment}{! The inverse of the timestep [T-\/1 ~> s-\/1]}}
\DoxyCodeLine{2985 \textcolor{keywordtype}{  real} :: ppt2mks = 0.001  \textcolor{comment}{! Conversion factor from g/kg to kg/kg.}}
\DoxyCodeLine{2986   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz}
\DoxyCodeLine{2987 }
\DoxyCodeLine{2988   is  = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = g\%ke}
\DoxyCodeLine{2989   idt = 0.0 ; \textcolor{keywordflow}{if} (dt > 0.0) idt = 1. / dt}
\DoxyCodeLine{2990   work\_3d(:,:,:) = 0.0}
\DoxyCodeLine{2991   work\_2d(:,:)   = 0.0}
\DoxyCodeLine{2992 }
\DoxyCodeLine{2993   \textcolor{comment}{! Thickness tendency}}
\DoxyCodeLine{2994   \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_h\_tendency > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2995     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2996       work\_3d(i,j,k) = (h(i,j,k) -\/ h\_old(i,j,k))*idt}
\DoxyCodeLine{2997 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2998     \textcolor{keyword}{call }post\_data(cs\%id\_boundary\_forcing\_h\_tendency, work\_3d, cs\%diag, alt\_h=h\_old)}
\DoxyCodeLine{2999 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3000 }
\DoxyCodeLine{3001   \textcolor{comment}{! temperature tendency}}
\DoxyCodeLine{3002   \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_temp\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3003     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3004       work\_3d(i,j,k) = (tv\%T(i,j,k)-\/temp\_old(i,j,k))*idt}
\DoxyCodeLine{3005 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{3006     \textcolor{keyword}{call }post\_data(cs\%id\_boundary\_forcing\_temp\_tend, work\_3d, cs\%diag, alt\_h=h\_old)}
\DoxyCodeLine{3007 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3008 }
\DoxyCodeLine{3009   \textcolor{comment}{! heat tendency}}
\DoxyCodeLine{3010   \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_heat\_tend > 0 .or. cs\%id\_boundary\_forcing\_heat\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3011     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3012       work\_3d(i,j,k) = gv\%H\_to\_RZ * tv\%C\_p * idt * (h(i,j,k) * tv\%T(i,j,k) -\/ h\_old(i,j,k) * temp\_old(i,j,k))}
\DoxyCodeLine{3013 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{3014     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_heat\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3015       \textcolor{keyword}{call }post\_data(cs\%id\_boundary\_forcing\_heat\_tend, work\_3d, cs\%diag, alt\_h = h\_old)}
\DoxyCodeLine{3016 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3017     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_heat\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3018       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3019         work\_2d(i,j) = 0.0}
\DoxyCodeLine{3020         \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{3021           work\_2d(i,j) = work\_2d(i,j) + work\_3d(i,j,k)}
\DoxyCodeLine{3022 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{3023 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{3024       \textcolor{keyword}{call }post\_data(cs\%id\_boundary\_forcing\_heat\_tend\_2d, work\_2d, cs\%diag)}
\DoxyCodeLine{3025 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3026 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3027 }
\DoxyCodeLine{3028   \textcolor{comment}{! salinity tendency}}
\DoxyCodeLine{3029   \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_saln\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3030     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3031       work\_3d(i,j,k) = (tv\%S(i,j,k)-\/saln\_old(i,j,k))*idt}
\DoxyCodeLine{3032 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{3033     \textcolor{keyword}{call }post\_data(cs\%id\_boundary\_forcing\_saln\_tend, work\_3d, cs\%diag, alt\_h = h\_old)}
\DoxyCodeLine{3034 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3035 }
\DoxyCodeLine{3036   \textcolor{comment}{! salt tendency}}
\DoxyCodeLine{3037   \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_salt\_tend > 0 .or. cs\%id\_boundary\_forcing\_salt\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3038     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3039       work\_3d(i,j,k) = gv\%H\_to\_RZ * ppt2mks * idt * (h(i,j,k) * tv\%S(i,j,k) -\/ h\_old(i,j,k) * saln\_old(i,j,k))}
\DoxyCodeLine{3040 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{3041     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_salt\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3042       \textcolor{keyword}{call }post\_data(cs\%id\_boundary\_forcing\_salt\_tend, work\_3d, cs\%diag, alt\_h = h\_old)}
\DoxyCodeLine{3043 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3044     \textcolor{keywordflow}{if} (cs\%id\_boundary\_forcing\_salt\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3045       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3046         work\_2d(i,j) = 0.0}
\DoxyCodeLine{3047         \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{3048           work\_2d(i,j) = work\_2d(i,j) + work\_3d(i,j,k)}
\DoxyCodeLine{3049 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{3050 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{3051       \textcolor{keyword}{call }post\_data(cs\%id\_boundary\_forcing\_salt\_tend\_2d, work\_2d, cs\%diag)}
\DoxyCodeLine{3052 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3053 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3054 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diabatic__driver_af0aa2526c1824517fe5cdae65b48abd9}\label{namespacemom__diabatic__driver_af0aa2526c1824517fe5cdae65b48abd9}} 
\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}!diagnose\_diabatic\_diff\_tendency@{diagnose\_diabatic\_diff\_tendency}}
\index{diagnose\_diabatic\_diff\_tendency@{diagnose\_diabatic\_diff\_tendency}!mom\_diabatic\_driver@{mom\_diabatic\_driver}}
\doxysubsubsection{\texorpdfstring{diagnose\_diabatic\_diff\_tendency()}{diagnose\_diabatic\_diff\_tendency()}}
{\footnotesize\ttfamily subroutine mom\+\_\+diabatic\+\_\+driver\+::diagnose\+\_\+diabatic\+\_\+diff\+\_\+tendency (\begin{DoxyParamCaption}\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{temp\+\_\+old,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{saln\+\_\+old,  }\item[{real, intent(in)}]{dt,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This routine diagnoses tendencies from application of diabatic diffusion using A\+LE algorithm. Note that layer thickness is not altered by diabatic diffusion. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & ocean grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & ocean vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & points to updated thermodynamic fields \\
\hline
\mbox{\texttt{ in}}  & {\em h} & thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em temp\+\_\+old} & temperature prior to diabatic physics \\
\hline
\mbox{\texttt{ in}}  & {\em saln\+\_\+old} & salinity prior to diabatic physics \mbox{[}ppt\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & time step \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & module control structure \\
\hline
\end{DoxyParams}


Definition at line 2873 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{2873   \textcolor{keywordtype}{type}(ocean\_grid\_type),                     \textcolor{keywordtype}{intent(in)} :: G\textcolor{comment}{        !< ocean grid structure}}
\DoxyCodeLine{2874   \textcolor{keywordtype}{type}(verticalGrid\_type),                   \textcolor{keywordtype}{intent(in)} :: GV\textcolor{comment}{       !< ocean vertical grid structure}}
\DoxyCodeLine{2875   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),                     \textcolor{keywordtype}{intent(in)} :: tv\textcolor{comment}{       !< points to updated thermodynamic fields}}
\DoxyCodeLine{2876 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  \textcolor{keywordtype}{intent(in)} :: h\textcolor{comment}{        !< thickness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{2877 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  \textcolor{keywordtype}{intent(in)} :: temp\_old\textcolor{comment}{ !< temperature prior to diabatic physics}}
\DoxyCodeLine{2878 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  \textcolor{keywordtype}{intent(in)} :: saln\_old\textcolor{comment}{ !< salinity prior to diabatic physics [ppt]}}
\DoxyCodeLine{2879 \textcolor{keywordtype}{  real},                                      \textcolor{keywordtype}{intent(in)} :: dt\textcolor{comment}{       !< time step [T ~> s]}}
\DoxyCodeLine{2880   \textcolor{keywordtype}{type}(unit\_scale\_type),                     \textcolor{keywordtype}{intent(in)} :: US\textcolor{comment}{       !< A dimensional unit scaling type}}
\DoxyCodeLine{2881   \textcolor{keywordtype}{type}(diabatic\_CS),                         \textcolor{keywordtype}{pointer}    :: CS\textcolor{comment}{       !< module control structure}}
\DoxyCodeLine{2882 }
\DoxyCodeLine{2883   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{2884 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))} :: work\_3d}
\DoxyCodeLine{2885 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))}         :: work\_2d}
\DoxyCodeLine{2886 \textcolor{keywordtype}{  real} :: Idt  \textcolor{comment}{! The inverse of the timestep [T-\/1 ~> s-\/1]}}
\DoxyCodeLine{2887 \textcolor{keywordtype}{  real} :: ppt2mks = 0.001  \textcolor{comment}{! Conversion factor from g/kg to kg/kg.}}
\DoxyCodeLine{2888   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz}
\DoxyCodeLine{2889   \textcolor{keywordtype}{logical} :: do\_saln\_tend   \textcolor{comment}{! Calculate salinity-\/based tendency diagnosics}}
\DoxyCodeLine{2890 }
\DoxyCodeLine{2891   is  = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = g\%ke}
\DoxyCodeLine{2892   idt = 0.0 ; \textcolor{keywordflow}{if} (dt > 0.0) idt = 1. / dt}
\DoxyCodeLine{2893   work\_3d(:,:,:) = 0.0}
\DoxyCodeLine{2894   work\_2d(:,:)   = 0.0}
\DoxyCodeLine{2895 }
\DoxyCodeLine{2896 }
\DoxyCodeLine{2897   \textcolor{comment}{! temperature tendency}}
\DoxyCodeLine{2898   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2899     work\_3d(i,j,k) = (tv\%T(i,j,k)-\/temp\_old(i,j,k))*idt}
\DoxyCodeLine{2900 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2901   \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_temp\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2902     \textcolor{keyword}{call }post\_data(cs\%id\_diabatic\_diff\_temp\_tend, work\_3d, cs\%diag, alt\_h=h)}
\DoxyCodeLine{2903 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2904 }
\DoxyCodeLine{2905   \textcolor{comment}{! heat tendency}}
\DoxyCodeLine{2906   \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_heat\_tend > 0 .or. cs\%id\_diabatic\_diff\_heat\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2907     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2908       work\_3d(i,j,k) = h(i,j,k)*gv\%H\_to\_RZ * tv\%C\_p * work\_3d(i,j,k)}
\DoxyCodeLine{2909 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2910     \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_heat\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2911       \textcolor{keyword}{call }post\_data(cs\%id\_diabatic\_diff\_heat\_tend, work\_3d, cs\%diag, alt\_h=h)}
\DoxyCodeLine{2912 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2913     \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_heat\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2914       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2915         work\_2d(i,j) = 0.0}
\DoxyCodeLine{2916         \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{2917           work\_2d(i,j) = work\_2d(i,j) + work\_3d(i,j,k)}
\DoxyCodeLine{2918 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{2919 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2920       \textcolor{keyword}{call }post\_data(cs\%id\_diabatic\_diff\_heat\_tend\_2d, work\_2d, cs\%diag)}
\DoxyCodeLine{2921 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2922 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2923 }
\DoxyCodeLine{2924   \textcolor{comment}{! salinity tendency}}
\DoxyCodeLine{2925   do\_saln\_tend = cs\%id\_diabatic\_diff\_saln\_tend > 0 \&}
\DoxyCodeLine{2926     .or. cs\%id\_diabatic\_diff\_salt\_tend > 0 \&}
\DoxyCodeLine{2927     .or. cs\%id\_diabatic\_diff\_salt\_tend\_2d > 0}
\DoxyCodeLine{2928 }
\DoxyCodeLine{2929   \textcolor{keywordflow}{if} (do\_saln\_tend) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2930     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2931       work\_3d(i,j,k) = (tv\%S(i,j,k) -\/ saln\_old(i,j,k)) * idt}
\DoxyCodeLine{2932 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2933 }
\DoxyCodeLine{2934     \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_saln\_tend > 0) \&}
\DoxyCodeLine{2935       \textcolor{keyword}{call }post\_data(cs\%id\_diabatic\_diff\_saln\_tend, work\_3d, cs\%diag, alt\_h=h)}
\DoxyCodeLine{2936 }
\DoxyCodeLine{2937     \textcolor{comment}{! salt tendency}}
\DoxyCodeLine{2938     \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_salt\_tend > 0 .or. cs\%id\_diabatic\_diff\_salt\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2939       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2940         work\_3d(i,j,k) = h(i,j,k)*gv\%H\_to\_RZ * ppt2mks * work\_3d(i,j,k)}
\DoxyCodeLine{2941 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2942       \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_salt\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2943         \textcolor{keyword}{call }post\_data(cs\%id\_diabatic\_diff\_salt\_tend, work\_3d, cs\%diag, alt\_h=h)}
\DoxyCodeLine{2944 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2945       \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_salt\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2946         \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2947           work\_2d(i,j) = 0.0}
\DoxyCodeLine{2948           \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{2949             work\_2d(i,j) = work\_2d(i,j) + work\_3d(i,j,k)}
\DoxyCodeLine{2950 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{2951 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2952         \textcolor{keyword}{call }post\_data(cs\%id\_diabatic\_diff\_salt\_tend\_2d, work\_2d, cs\%diag)}
\DoxyCodeLine{2953 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2954 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2955 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2956 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diabatic__driver_af79993410fea86ff69ef417396798bac}\label{namespacemom__diabatic__driver_af79993410fea86ff69ef417396798bac}} 
\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}!diagnose\_frazil\_tendency@{diagnose\_frazil\_tendency}}
\index{diagnose\_frazil\_tendency@{diagnose\_frazil\_tendency}!mom\_diabatic\_driver@{mom\_diabatic\_driver}}
\doxysubsubsection{\texorpdfstring{diagnose\_frazil\_tendency()}{diagnose\_frazil\_tendency()}}
{\footnotesize\ttfamily subroutine mom\+\_\+diabatic\+\_\+driver\+::diagnose\+\_\+frazil\+\_\+tendency (\begin{DoxyParamCaption}\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{h,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(g)), intent(in)}]{temp\+\_\+old,  }\item[{real, intent(in)}]{dt,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This routine diagnoses tendencies for temperature and heat from frazil formation. This routine is called twice from within subroutine diabatic; at start and at end of the diabatic processes. The impacts from frazil are generally a function of depth. Hence, when checking heat budget, be sure to remove H\+F\+S\+I\+F\+R\+A\+Z\+IL from H\+F\+DS in k=1. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & ocean grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & ocean vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & points to updated thermodynamic fields \\
\hline
\mbox{\texttt{ in}}  & {\em h} & thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em temp\+\_\+old} & temperature prior to frazil formation \mbox{[}degC\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & time step \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & module control structure \\
\hline
\end{DoxyParams}


Definition at line 3063 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{3063   \textcolor{keywordtype}{type}(ocean\_grid\_type),                    \textcolor{keywordtype}{intent(in)} :: G\textcolor{comment}{        !< ocean grid structure}}
\DoxyCodeLine{3064   \textcolor{keywordtype}{type}(verticalGrid\_type),                  \textcolor{keywordtype}{intent(in)} :: GV\textcolor{comment}{       !< ocean vertical grid structure}}
\DoxyCodeLine{3065   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),                    \textcolor{keywordtype}{intent(in)} :: tv\textcolor{comment}{       !< points to updated thermodynamic fields}}
\DoxyCodeLine{3066 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)} :: h\textcolor{comment}{        !< thickness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{3067 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)} :: temp\_old\textcolor{comment}{ !< temperature prior to frazil formation [degC]}}
\DoxyCodeLine{3068 \textcolor{keywordtype}{  real},                                     \textcolor{keywordtype}{intent(in)} :: dt\textcolor{comment}{       !< time step [T ~> s]}}
\DoxyCodeLine{3069   \textcolor{keywordtype}{type}(unit\_scale\_type),                    \textcolor{keywordtype}{intent(in)} :: US\textcolor{comment}{       !< A dimensional unit scaling type}}
\DoxyCodeLine{3070   \textcolor{keywordtype}{type}(diabatic\_CS),                        \textcolor{keywordtype}{pointer}    :: CS\textcolor{comment}{       !< module control structure}}
\DoxyCodeLine{3071 }
\DoxyCodeLine{3072 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))}         :: work\_2d}
\DoxyCodeLine{3073 \textcolor{keywordtype}{  real}    :: Idt \textcolor{comment}{! The inverse of the timestep [T-\/1 ~> s-\/1]}}
\DoxyCodeLine{3074   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz}
\DoxyCodeLine{3075 }
\DoxyCodeLine{3076   is  = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = g\%ke}
\DoxyCodeLine{3077   idt = 0.0 ; \textcolor{keywordflow}{if} (dt > 0.0) idt = 1. / dt}
\DoxyCodeLine{3078 }
\DoxyCodeLine{3079   \textcolor{comment}{! temperature tendency}}
\DoxyCodeLine{3080   \textcolor{keywordflow}{if} (cs\%id\_frazil\_temp\_tend > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3081     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3082       cs\%frazil\_temp\_diag(i,j,k) = idt * (tv\%T(i,j,k)-\/temp\_old(i,j,k))}
\DoxyCodeLine{3083 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{3084     \textcolor{keyword}{call }post\_data(cs\%id\_frazil\_temp\_tend, cs\%frazil\_temp\_diag(:,:,:), cs\%diag)}
\DoxyCodeLine{3085 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3086 }
\DoxyCodeLine{3087   \textcolor{comment}{! heat tendency}}
\DoxyCodeLine{3088   \textcolor{keywordflow}{if} (cs\%id\_frazil\_heat\_tend > 0 .or. cs\%id\_frazil\_heat\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3089     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3090       cs\%frazil\_heat\_diag(i,j,k) = gv\%H\_to\_RZ * tv\%C\_p * h(i,j,k) * idt * (tv\%T(i,j,k)-\/temp\_old(i,j,k))}
\DoxyCodeLine{3091 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{3092     \textcolor{keywordflow}{if} (cs\%id\_frazil\_heat\_tend > 0) \textcolor{keyword}{call }post\_data(cs\%id\_frazil\_heat\_tend, cs\%frazil\_heat\_diag(:,:,:), cs\%diag)}
\DoxyCodeLine{3093 }
\DoxyCodeLine{3094     \textcolor{comment}{! As a consistency check, we must have}}
\DoxyCodeLine{3095     \textcolor{comment}{! FRAZIL\_HEAT\_TENDENCY\_2d = HFSIFRAZIL}}
\DoxyCodeLine{3096     \textcolor{keywordflow}{if} (cs\%id\_frazil\_heat\_tend\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{3097       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{3098         work\_2d(i,j) = 0.0}
\DoxyCodeLine{3099         \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{3100           work\_2d(i,j) = work\_2d(i,j) + cs\%frazil\_heat\_diag(i,j,k)}
\DoxyCodeLine{3101 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{3102 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{3103       \textcolor{keyword}{call }post\_data(cs\%id\_frazil\_heat\_tend\_2d, work\_2d, cs\%diag)}
\DoxyCodeLine{3104 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{3105 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{3106 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diabatic__driver_a49144b7b0c0d44fde6cd835f1001dde5}\label{namespacemom__diabatic__driver_a49144b7b0c0d44fde6cd835f1001dde5}} 
\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}!extract\_diabatic\_member@{extract\_diabatic\_member}}
\index{extract\_diabatic\_member@{extract\_diabatic\_member}!mom\_diabatic\_driver@{mom\_diabatic\_driver}}
\doxysubsubsection{\texorpdfstring{extract\_diabatic\_member()}{extract\_diabatic\_member()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+diabatic\+\_\+driver\+::extract\+\_\+diabatic\+\_\+member (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), intent(in)}]{CS,  }\item[{type(opacity\+\_\+cs), optional, pointer}]{opacity\+\_\+\+C\+Sp,  }\item[{type(optics\+\_\+type), optional, pointer}]{optics\+\_\+\+C\+Sp,  }\item[{real, intent(out), optional}]{evap\+\_\+\+C\+F\+L\+\_\+limit,  }\item[{real, intent(out), optional}]{minimum\+\_\+forcing\+\_\+depth,  }\item[{type(kpp\+\_\+cs), optional, pointer}]{K\+P\+P\+\_\+\+C\+Sp,  }\item[{type(energetic\+\_\+pbl\+\_\+cs), optional, pointer}]{energetic\+\_\+\+P\+B\+L\+\_\+\+C\+Sp,  }\item[{type(diabatic\+\_\+aux\+\_\+cs), optional, pointer}]{diabatic\+\_\+aux\+\_\+\+C\+Sp,  }\item[{integer, intent(out), optional}]{diabatic\+\_\+halo }\end{DoxyParamCaption})}



Returns pointers or values of members within the diabatic\+\_\+\+CS type. For extensibility, each returned argument is an optional argument. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em cs} & module control structure \\
\hline
 & {\em opacity\+\_\+csp} & A pointer to be set to the opacity control structure \\
\hline
 & {\em optics\+\_\+csp} & A pointer to be set to the optics control structure \\
\hline
 & {\em kpp\+\_\+csp} & A pointer to be set to the K\+PP CS \\
\hline
 & {\em energetic\+\_\+pbl\+\_\+csp} & A pointer to be set to the e\+P\+BL CS \\
\hline
\mbox{\texttt{ out}}  & {\em evap\+\_\+cfl\+\_\+limit} & The largest fraction of a layer that can be evaporated in one time-\/step \mbox{[}nondim\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em minimum\+\_\+forcing\+\_\+depth} & The smallest depth over which heat and freshwater fluxes are applied \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
 & {\em diabatic\+\_\+aux\+\_\+csp} & A pointer to be set to the diabatic\+\_\+aux control structure \\
\hline
\mbox{\texttt{ out}}  & {\em diabatic\+\_\+halo} & The halo size where the diabatic algorithms assume thermodynamics properties are valid. \\
\hline
\end{DoxyParams}


Definition at line 2819 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{2819   \textcolor{keywordtype}{type}(diabatic\_CS), \textcolor{keywordtype}{intent(in   )}           :: CS\textcolor{comment}{ !< module control structure}}
\DoxyCodeLine{2820   \textcolor{comment}{! All output arguments are optional}}
\DoxyCodeLine{2821   \textcolor{keywordtype}{type}(opacity\_CS),  \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}       :: opacity\_CSp\textcolor{comment}{ !< A pointer to be set to the opacity control structure}}
\DoxyCodeLine{2822   \textcolor{keywordtype}{type}(optics\_type), \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}       :: optics\_CSp\textcolor{comment}{  !< A pointer to be set to the optics control structure}}
\DoxyCodeLine{2823   \textcolor{keywordtype}{type}(KPP\_CS),      \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}       :: KPP\_CSp\textcolor{comment}{     !< A pointer to be set to the KPP CS}}
\DoxyCodeLine{2824   \textcolor{keywordtype}{type}(energetic\_PBL\_CS), \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}  :: energetic\_PBL\_CSp\textcolor{comment}{ !< A pointer to be set to the ePBL CS}}
\DoxyCodeLine{2825 \textcolor{keywordtype}{  real},              \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(  out)} :: evap\_CFL\_limit\textcolor{comment}{ !<The largest fraction of a layer that can be}}
\DoxyCodeLine{2826 \textcolor{comment}{                                                            !! evaporated in one time-\/step [nondim].}}
\DoxyCodeLine{2827 \textcolor{keywordtype}{  real},              \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(  out)} :: minimum\_forcing\_depth\textcolor{comment}{ !< The smallest depth over which heat}}
\DoxyCodeLine{2828 \textcolor{comment}{                                                            !! and freshwater fluxes are applied [H ~> m or kg m-\/2].}}
\DoxyCodeLine{2829   \textcolor{keywordtype}{type}(diabatic\_aux\_CS), \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}   :: diabatic\_aux\_CSp\textcolor{comment}{ !< A pointer to be set to the diabatic\_aux}}
\DoxyCodeLine{2830 \textcolor{comment}{                                                            !! control structure}}
\DoxyCodeLine{2831   \textcolor{keywordtype}{integer},           \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(  out)} :: diabatic\_halo\textcolor{comment}{ !< The halo size where the diabatic algorithms}}
\DoxyCodeLine{2832 \textcolor{comment}{                                                            !! assume thermodynamics properties are valid.}}
\DoxyCodeLine{2833 }
\DoxyCodeLine{2834   \textcolor{comment}{! Pointers to control structures}}
\DoxyCodeLine{2835   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(opacity\_csp))       opacity\_csp => cs\%opacity\_CSp}
\DoxyCodeLine{2836   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(optics\_csp))        optics\_csp  => cs\%optics}
\DoxyCodeLine{2837   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kpp\_csp))           kpp\_csp     => cs\%KPP\_CSp}
\DoxyCodeLine{2838   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(energetic\_pbl\_csp)) energetic\_pbl\_csp => cs\%energetic\_PBL\_CSp}
\DoxyCodeLine{2839 }
\DoxyCodeLine{2840   \textcolor{comment}{! Constants within diabatic\_CS}}
\DoxyCodeLine{2841   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(evap\_cfl\_limit))        evap\_cfl\_limit = cs\%evap\_CFL\_limit}
\DoxyCodeLine{2842   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(minimum\_forcing\_depth)) minimum\_forcing\_depth = cs\%minimum\_forcing\_depth}
\DoxyCodeLine{2843   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(diabatic\_halo)) diabatic\_halo = cs\%halo\_TS\_diff}
\DoxyCodeLine{2844 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diabatic__driver_a71d8d849db16be4b87c2650b49f01c82}\label{namespacemom__diabatic__driver_a71d8d849db16be4b87c2650b49f01c82}} 
\index{mom\_diabatic\_driver@{mom\_diabatic\_driver}!layered\_diabatic@{layered\_diabatic}}
\index{layered\_diabatic@{layered\_diabatic}!mom\_diabatic\_driver@{mom\_diabatic\_driver}}
\doxysubsubsection{\texorpdfstring{layered\_diabatic()}{layered\_diabatic()}}
{\footnotesize\ttfamily subroutine mom\+\_\+diabatic\+\_\+driver\+::layered\+\_\+diabatic (\begin{DoxyParamCaption}\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed, g \%ke), intent(inout)}]{u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb, g \%ke), intent(inout)}]{v,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(inout)}]{h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(inout)}]{tv,  }\item[{real, dimension(\+:,\+:), pointer}]{Hml,  }\item[{type(forcing), intent(inout)}]{fluxes,  }\item[{type(vertvisc\+\_\+type), intent(inout)}]{visc,  }\item[{type(accel\+\_\+diag\+\_\+ptrs), intent(inout)}]{A\+Dp,  }\item[{type(cont\+\_\+diag\+\_\+ptrs), intent(inout)}]{C\+Dp,  }\item[{real, intent(in)}]{dt,  }\item[{type(time\+\_\+type), intent(in)}]{Time\+\_\+end,  }\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__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), pointer}]{CS,  }\item[{type(wave\+\_\+parameters\+\_\+cs), optional, pointer}]{W\+A\+V\+ES }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Imposes the diapycnal mass fluxes and the accompanying diapycnal advection of momentum and tracers using the original M\+O\+M6 algorithms. 


\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 us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in,out}}  & {\em u} & zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em v} & meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em h} & thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em tv} & points to thermodynamic fields unused have N\+U\+LL ptrs \\
\hline
 & {\em hml} & Active mixed layer depth \mbox{[}Z $\sim$$>$ m\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em fluxes} & points to forcing fields unused fields have N\+U\+LL ptrs \\
\hline
\mbox{\texttt{ in,out}}  & {\em visc} & vertical viscosities, B\+BL properies, and \\
\hline
\mbox{\texttt{ in,out}}  & {\em adp} & related points to accelerations in momentum equations, to enable the later derived diagnostics, like energy budgets \\
\hline
\mbox{\texttt{ in,out}}  & {\em cdp} & points to terms in continuity equations \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & time increment \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em time\+\_\+end} & Time at the end of the interval \\
\hline
 & {\em cs} & module control structure \\
\hline
 & {\em waves} & Surface gravity waves \\
\hline
\end{DoxyParams}


Definition at line 1855 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1855   \textcolor{keywordtype}{type}(ocean\_grid\_type),                     \textcolor{keywordtype}{intent(inout)} :: G\textcolor{comment}{         !< ocean grid structure}}
\DoxyCodeLine{1856   \textcolor{keywordtype}{type}(verticalGrid\_type),                   \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{        !< ocean vertical grid structure}}
\DoxyCodeLine{1857   \textcolor{keywordtype}{type}(unit\_scale\_type),                     \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{        !< A dimensional unit scaling type}}
\DoxyCodeLine{1858 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(inout)} :: u\textcolor{comment}{         !< zonal velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{1859 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(inout)} :: v\textcolor{comment}{         !< meridional velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{1860 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))},  \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{         !< thickness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1861   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),                     \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{        !< points to thermodynamic fields}}
\DoxyCodeLine{1862 \textcolor{comment}{                                                                        !! unused have NULL ptrs}}
\DoxyCodeLine{1863 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(:,:)},                      \textcolor{keywordtype}{pointer}       :: Hml\textcolor{comment}{       !< Active mixed layer depth [Z ~> m]}}
\DoxyCodeLine{1864   \textcolor{keywordtype}{type}(forcing),                             \textcolor{keywordtype}{intent(inout)} :: fluxes\textcolor{comment}{    !< points to forcing fields}}
\DoxyCodeLine{1865 \textcolor{comment}{                                                                        !! unused fields have NULL ptrs}}
\DoxyCodeLine{1866   \textcolor{keywordtype}{type}(vertvisc\_type),                       \textcolor{keywordtype}{intent(inout)} :: visc\textcolor{comment}{      !< vertical viscosities, BBL properies, and}}
\DoxyCodeLine{1867   \textcolor{keywordtype}{type}(accel\_diag\_ptrs),                     \textcolor{keywordtype}{intent(inout)} :: ADp\textcolor{comment}{       !< related points to accelerations in momentum}}
\DoxyCodeLine{1868 \textcolor{comment}{                                                                        !! equations, to enable the later derived}}
\DoxyCodeLine{1869 \textcolor{comment}{                                                                        !! diagnostics, like energy budgets}}
\DoxyCodeLine{1870   \textcolor{keywordtype}{type}(cont\_diag\_ptrs),                      \textcolor{keywordtype}{intent(inout)} :: CDp\textcolor{comment}{       !< points to terms in continuity equations}}
\DoxyCodeLine{1871 \textcolor{keywordtype}{  real},                                      \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{        !< time increment [T ~> s]}}
\DoxyCodeLine{1872   \textcolor{keywordtype}{type}(time\_type),                           \textcolor{keywordtype}{intent(in)}    :: Time\_end\textcolor{comment}{  !< Time at the end of the interval}}
\DoxyCodeLine{1873   \textcolor{keywordtype}{type}(diabatic\_CS),                         \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{        !< module control structure}}
\DoxyCodeLine{1874   \textcolor{keywordtype}{type}(Wave\_parameters\_CS),        \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}       :: Waves\textcolor{comment}{     !< Surface gravity waves}}
\DoxyCodeLine{1875 }
\DoxyCodeLine{1876 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))} :: \&}
\DoxyCodeLine{1877     ea,     \&    \textcolor{comment}{! amount of fluid entrained from the layer above within}}
\DoxyCodeLine{1878                  \textcolor{comment}{! one time step  [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1879     eb,     \&    \textcolor{comment}{! amount of fluid entrained from the layer below within}}
\DoxyCodeLine{1880                  \textcolor{comment}{! one time step  [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1881     kd\_lay, \&    \textcolor{comment}{! diapycnal diffusivity of layers [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{1882     h\_orig, \&    \textcolor{comment}{! initial layer thicknesses [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1883     h\_prebound, \& \textcolor{comment}{! initial layer thicknesses [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1884     hold,   \&    \textcolor{comment}{! layer thickness before diapycnal entrainment, and later}}
\DoxyCodeLine{1885                  \textcolor{comment}{! the initial layer thicknesses (if a mixed layer is used),}}
\DoxyCodeLine{1886                  \textcolor{comment}{! [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1887     dsv\_dt, \&    \textcolor{comment}{! The partial derivative of specific volume with temperature [R-\/1 degC-\/1 ~> m3 kg-\/1 degC-\/1]}}
\DoxyCodeLine{1888     dsv\_ds, \&    \textcolor{comment}{! The partial derivative of specific volume with salinity [R-\/1 ppt-\/1 ~> m3 kg-\/1 ppt-\/1].}}
\DoxyCodeLine{1889     u\_h,    \&    \textcolor{comment}{! zonal and meridional velocities at thickness points after}}
\DoxyCodeLine{1890     v\_h          \textcolor{comment}{! entrainment [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{1891 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: \&}
\DoxyCodeLine{1892     Rcv\_ml, \&   \textcolor{comment}{! coordinate density of mixed layer, used for applying sponges}}
\DoxyCodeLine{1893     SkinBuoyFlux\textcolor{comment}{! 2d surface buoyancy flux [Z2 T-\/3 ~> m2 s-\/3], used by ePBL}}
\DoxyCodeLine{1894 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),G\%ke)} :: h\_diag                \textcolor{comment}{! diagnostic array for thickness}}
\DoxyCodeLine{1895 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),G\%ke)} :: temp\_diag             \textcolor{comment}{! diagnostic array for temp}}
\DoxyCodeLine{1896 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),G\%ke)} :: saln\_diag             \textcolor{comment}{! diagnostic array for salinity}}
\DoxyCodeLine{1897 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))}      :: tendency\_2d           \textcolor{comment}{! depth integrated content tendency for diagn}}
\DoxyCodeLine{1898 }
\DoxyCodeLine{1899 \textcolor{keywordtype}{  real} :: net\_ent  \textcolor{comment}{! The net of ea-\/eb at an interface.}}
\DoxyCodeLine{1900 }
\DoxyCodeLine{1901 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{target} :: \&}
\DoxyCodeLine{1902              \textcolor{comment}{! These are targets so that the space can be shared with eaml \& ebml.}}
\DoxyCodeLine{1903     eatr, \&  \textcolor{comment}{! The equivalent of ea and eb for tracers, which differ from ea and}}
\DoxyCodeLine{1904     ebtr     \textcolor{comment}{! eb in that they tend to homogenize tracers in massless layers}}
\DoxyCodeLine{1905              \textcolor{comment}{! near the boundaries [H ~> m or kg m-\/2] (for Bous or non-\/Bouss)}}
\DoxyCodeLine{1906 }
\DoxyCodeLine{1907 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G)+1)} :: \&}
\DoxyCodeLine{1908     Kd\_int,   \& \textcolor{comment}{! diapycnal diffusivity of interfaces [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{1909     Kd\_heat,  \& \textcolor{comment}{! diapycnal diffusivity of heat [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{1910     Kd\_salt,  \& \textcolor{comment}{! diapycnal diffusivity of salt and passive tracers [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{1911     Kd\_extra\_T , \& \textcolor{comment}{! The extra diffusivity of temperature due to double diffusion relative to}}
\DoxyCodeLine{1912                 \textcolor{comment}{! Kd\_int [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{1913     kd\_extra\_s , \& \textcolor{comment}{!  The extra diffusivity of salinity due to double diffusion relative to}}
\DoxyCodeLine{1914                 \textcolor{comment}{! Kd\_int [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{1915     kd\_epbl,  \& \textcolor{comment}{! test array of diapycnal diffusivities at interfaces [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{1916     tdif\_flx, \& \textcolor{comment}{! diffusive diapycnal heat flux across interfaces [degC H T-\/1 ~> degC m s-\/1 or degC kg m-\/2 s-\/1]}}
\DoxyCodeLine{1917     tadv\_flx, \& \textcolor{comment}{! advective diapycnal heat flux across interfaces [degC H T-\/1 ~> degC m s-\/1 or degC kg m-\/2 s-\/1]}}
\DoxyCodeLine{1918     sdif\_flx, \& \textcolor{comment}{! diffusive diapycnal salt flux across interfaces [ppt H T-\/1 ~> ppt m s-\/1 or ppt kg m-\/2 s-\/1]}}
\DoxyCodeLine{1919     sadv\_flx    \textcolor{comment}{! advective diapycnal salt flux across interfaces [ppt H T-\/1 ~> ppt m s-\/1 or ppt kg m-\/2 s-\/1]}}
\DoxyCodeLine{1920 }
\DoxyCodeLine{1921 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{allocatable}, \textcolor{keywordtype}{dimension(:,:)} :: \&}
\DoxyCodeLine{1922     hf\_dudt\_dia\_2d, hf\_dvdt\_dia\_2d \textcolor{comment}{! Depth sum of diapycnal mixing accelaration * fract. thickness [L T-\/2 ~> m s-\/2].}}
\DoxyCodeLine{1923 }
\DoxyCodeLine{1924   \textcolor{comment}{! The following 5 variables are only used with a bulk mixed layer.}}
\DoxyCodeLine{1925 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{pointer}, \textcolor{keywordtype}{dimension(:,:,:)} :: \&}
\DoxyCodeLine{1926     eaml, \&  \textcolor{comment}{! The equivalent of ea due to mixed layer processes [H ~> m or kg m-\/2].}}
\DoxyCodeLine{1927     ebml     \textcolor{comment}{! The equivalent of eb due to mixed layer processes [H ~> m or kg m-\/2].}}
\DoxyCodeLine{1928              \textcolor{comment}{! eaml and ebml are pointers to eatr and ebtr so as to reuse the memory as}}
\DoxyCodeLine{1929              \textcolor{comment}{! the arrays are not needed at the same time.}}
\DoxyCodeLine{1930 }
\DoxyCodeLine{1931   \textcolor{keywordtype}{integer} :: kb(SZI\_(G),SZJ\_(G)) \textcolor{comment}{! index of the lightest layer denser}}
\DoxyCodeLine{1932                                  \textcolor{comment}{! than the buffer layer [nondim]}}
\DoxyCodeLine{1933 }
\DoxyCodeLine{1934 \textcolor{keywordtype}{  real} :: p\_ref\_cv(SZI\_(G))      \textcolor{comment}{! Reference pressure for the potential density that defines the}}
\DoxyCodeLine{1935                                  \textcolor{comment}{! coordinate variable, set to P\_Ref [R L2 T-\/2 ~> Pa].}}
\DoxyCodeLine{1936 }
\DoxyCodeLine{1937   \textcolor{keywordtype}{logical} :: in\_boundary(SZI\_(G)) \textcolor{comment}{! True if there are no massive layers below,}}
\DoxyCodeLine{1938                                   \textcolor{comment}{! where massive is defined as sufficiently thick that}}
\DoxyCodeLine{1939                                   \textcolor{comment}{! the no-\/flux boundary conditions have not restricted}}
\DoxyCodeLine{1940                                   \textcolor{comment}{! the entrainment -\/ usually sqrt(Kd*dt).}}
\DoxyCodeLine{1941 }
\DoxyCodeLine{1942 \textcolor{keywordtype}{  real} :: b\_denom\_1    \textcolor{comment}{! The first term in the denominator of b1}}
\DoxyCodeLine{1943                        \textcolor{comment}{! [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1944 \textcolor{keywordtype}{  real} :: h\_neglect    \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{1945                        \textcolor{comment}{! in roundoff and can be neglected}}
\DoxyCodeLine{1946                        \textcolor{comment}{! [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1947 \textcolor{keywordtype}{  real} :: h\_neglect2   \textcolor{comment}{! h\_neglect\string^2 [H2 ~> m2 or kg2 m-\/4]}}
\DoxyCodeLine{1948 \textcolor{keywordtype}{  real} :: add\_ent      \textcolor{comment}{! Entrainment that needs to be added when mixing tracers}}
\DoxyCodeLine{1949                        \textcolor{comment}{! [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1950 \textcolor{keywordtype}{  real} :: eaval        \textcolor{comment}{! eaval is 2*ea at velocity grid points [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1951 \textcolor{keywordtype}{  real} :: hval         \textcolor{comment}{! hval is 2*h at velocity grid points [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1952 \textcolor{keywordtype}{  real} :: h\_tr         \textcolor{comment}{! h\_tr is h at tracer points with a tiny thickness}}
\DoxyCodeLine{1953                        \textcolor{comment}{! added to ensure positive definiteness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1954 \textcolor{keywordtype}{  real} :: Tr\_ea\_BBL    \textcolor{comment}{! The diffusive tracer thickness in the BBL that is}}
\DoxyCodeLine{1955                        \textcolor{comment}{! coupled to the bottom within a timestep [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1956 }
\DoxyCodeLine{1957 \textcolor{keywordtype}{  real} :: htot(SZIB\_(G))             \textcolor{comment}{! The summed thickness from the bottom [H ~> m or kg m-\/2].}}
\DoxyCodeLine{1958 \textcolor{keywordtype}{  real} :: b1(SZIB\_(G)), d1(SZIB\_(G)) \textcolor{comment}{! b1, c1, and d1 are variables used by the}}
\DoxyCodeLine{1959 \textcolor{keywordtype}{  real} :: c1(SZIB\_(G),SZK\_(G))       \textcolor{comment}{! tridiagonal solver.}}
\DoxyCodeLine{1960 }
\DoxyCodeLine{1961 \textcolor{keywordtype}{  real} :: dt\_mix  \textcolor{comment}{! The amount of time over which to apply mixing [T ~> s]}}
\DoxyCodeLine{1962 \textcolor{keywordtype}{  real} :: Idt     \textcolor{comment}{! The inverse time step [T-\/1 ~> s-\/1]}}
\DoxyCodeLine{1963 \textcolor{keywordtype}{  real} :: Idt\_accel  \textcolor{comment}{! The inverse time step times rescaling factors [T-\/1 ~> s-\/1]}}
\DoxyCodeLine{1964 }
\DoxyCodeLine{1965   \textcolor{keywordtype}{integer} :: dir\_flag     \textcolor{comment}{! An integer encoding the directions in which to do halo updates.}}
\DoxyCodeLine{1966   \textcolor{keywordtype}{logical} :: showCallTree \textcolor{comment}{! If true, show the call tree}}
\DoxyCodeLine{1967   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(2)} :: EOSdom \textcolor{comment}{! The i-\/computational domain for the equation of state}}
\DoxyCodeLine{1968   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, Isq, Ieq, Jsq, Jeq, nz, nkmb, m, halo}
\DoxyCodeLine{1969 }
\DoxyCodeLine{1970   is   = g\%isc  ; ie  = g\%iec  ; js  = g\%jsc  ; je  = g\%jec ; nz = g\%ke}
\DoxyCodeLine{1971   isq  = g\%IscB ; ieq = g\%IecB ; jsq = g\%JscB ; jeq = g\%JecB}
\DoxyCodeLine{1972   nkmb = gv\%nk\_rho\_varies}
\DoxyCodeLine{1973   h\_neglect = gv\%H\_subroundoff ; h\_neglect2 = h\_neglect*h\_neglect}
\DoxyCodeLine{1974   kd\_heat(:,:,:) = 0.0 ; kd\_salt(:,:,:) = 0.0}
\DoxyCodeLine{1975 }
\DoxyCodeLine{1976 }
\DoxyCodeLine{1977   showcalltree = calltree\_showquery()}
\DoxyCodeLine{1978   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_enter(\textcolor{stringliteral}{"layered\_diabatic(), MOM\_diabatic\_driver.F90"})}
\DoxyCodeLine{1979 }
\DoxyCodeLine{1980   \textcolor{comment}{! set equivalence between the same bits of memory for these arrays}}
\DoxyCodeLine{1981   eaml => eatr ; ebml => ebtr}
\DoxyCodeLine{1982 }
\DoxyCodeLine{1983   \textcolor{comment}{! For all other diabatic subroutines, the averaging window should be the entire diabatic timestep}}
\DoxyCodeLine{1984   \textcolor{keyword}{call }enable\_averages(dt, time\_end, cs\%diag)}
\DoxyCodeLine{1985 }
\DoxyCodeLine{1986   \textcolor{keywordflow}{if} ((cs\%ML\_mix\_first > 0.0) .or. cs\%use\_geothermal) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1987     halo = cs\%halo\_TS\_diff}
\DoxyCodeLine{1988     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{1989     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js-\/halo,je+halo ; \textcolor{keywordflow}{do} i=is-\/halo,ie+halo}
\DoxyCodeLine{1990       h\_orig(i,j,k) = h(i,j,k) ; eaml(i,j,k) = 0.0 ; ebml(i,j,k) = 0.0}
\DoxyCodeLine{1991 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1992 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1993 }
\DoxyCodeLine{1994   \textcolor{keywordflow}{if} (cs\%use\_geothermal) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1995     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_geothermal)}
\DoxyCodeLine{1996     \textcolor{keyword}{call }geothermal\_entraining(h, tv, dt, eaml, ebml, g, gv, us, cs\%geothermal\_CSp, halo=cs\%halo\_TS\_diff)}
\DoxyCodeLine{1997     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_geothermal)}
\DoxyCodeLine{1998     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"geothermal (diabatic)"})}
\DoxyCodeLine{1999     \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'geothermal'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{2000 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2001 }
\DoxyCodeLine{2002   \textcolor{comment}{! Whenever thickness changes let the diag manager know, target grids}}
\DoxyCodeLine{2003   \textcolor{comment}{! for vertical remapping may need to be regenerated.}}
\DoxyCodeLine{2004   \textcolor{keyword}{call }diag\_update\_remap\_grids(cs\%diag)}
\DoxyCodeLine{2005 }
\DoxyCodeLine{2006   \textcolor{comment}{! Set\_pen\_shortwave estimates the optical properties of the water column.}}
\DoxyCodeLine{2007   \textcolor{comment}{! It will need to be modified later to include information about the}}
\DoxyCodeLine{2008   \textcolor{comment}{! biological properties and layer thicknesses.}}
\DoxyCodeLine{2009   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs\%optics)) \&}
\DoxyCodeLine{2010     \textcolor{keyword}{call }set\_pen\_shortwave(cs\%optics, fluxes, g, gv, us, cs\%diabatic\_aux\_CSp, cs\%opacity\_CSp, cs\%tracer\_flow\_CSp)}
\DoxyCodeLine{2011 }
\DoxyCodeLine{2012   \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2013     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"Before mixedlayer"}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{2014 }
\DoxyCodeLine{2015     \textcolor{keywordflow}{if} (cs\%ML\_mix\_first > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2016 \textcolor{comment}{!  This subroutine}}
\DoxyCodeLine{2017 \textcolor{comment}{!    (1) Cools the mixed layer.}}
\DoxyCodeLine{2018 \textcolor{comment}{!    (2) Performs convective adjustment by mixed layer entrainment.}}
\DoxyCodeLine{2019 \textcolor{comment}{!    (3) Heats the mixed layer and causes it to detrain to}}
\DoxyCodeLine{2020 \textcolor{comment}{!        Monin-\/Obukhov depth or minimum mixed layer depth.}}
\DoxyCodeLine{2021 \textcolor{comment}{!    (4) Uses any remaining TKE to drive mixed layer entrainment.}}
\DoxyCodeLine{2022 \textcolor{comment}{!    (5) Possibly splits buffer layer into two isopycnal layers (when using isopycnal coordinate)}}
\DoxyCodeLine{2023       \textcolor{keyword}{call }find\_uv\_at\_h(u, v, h, u\_h, v\_h, g, gv, us)}
\DoxyCodeLine{2024 }
\DoxyCodeLine{2025       \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_mixedlayer)}
\DoxyCodeLine{2026       \textcolor{keywordflow}{if} (cs\%ML\_mix\_first < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2027         \textcolor{comment}{! Changes: h, tv\%T, tv\%S, eaml and ebml  (G is also inout???)}}
\DoxyCodeLine{2028         \textcolor{keyword}{call }bulkmixedlayer(h, u\_h, v\_h, tv, fluxes, dt*cs\%ML\_mix\_first, \&}
\DoxyCodeLine{2029                             eaml,ebml, g, gv, us, cs\%bulkmixedlayer\_CSp, cs\%optics, \&}
\DoxyCodeLine{2030                             hml, cs\%aggregate\_FW\_forcing, dt, last\_call=.false.)}
\DoxyCodeLine{2031         \textcolor{comment}{! Changes: h, tv\%T, tv\%S, eaml and ebml  (G is also inout???)}}
\DoxyCodeLine{2032         \textcolor{keyword}{call }bulkmixedlayer(h, u\_h, v\_h, tv, fluxes, dt, eaml, ebml, \&}
\DoxyCodeLine{2033                             g, gv, us, cs\%bulkmixedlayer\_CSp, cs\%optics, \&}
\DoxyCodeLine{2034                             hml, cs\%aggregate\_FW\_forcing, dt, last\_call=.true.)}
\DoxyCodeLine{2035 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2036 }
\DoxyCodeLine{2037       \textcolor{comment}{!  Keep salinity from falling below a small but positive threshold.}}
\DoxyCodeLine{2038       \textcolor{comment}{!  This constraint is needed for SIS1 ice model, which can extract}}
\DoxyCodeLine{2039       \textcolor{comment}{!  more salt than is present in the ocean. SIS2 does not suffer}}
\DoxyCodeLine{2040       \textcolor{comment}{!  from this limitation, in which case we can let salinity=0 and still}}
\DoxyCodeLine{2041       \textcolor{comment}{!  have salt conserved with SIS2 ice. So for SIS2, we can run with}}
\DoxyCodeLine{2042       \textcolor{comment}{!  BOUND\_SALINITY=False in MOM.F90.}}
\DoxyCodeLine{2043       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%S) .and. \textcolor{keyword}{associated}(tv\%salt\_deficit)) \&}
\DoxyCodeLine{2044         \textcolor{keyword}{call }adjust\_salt(h, tv, g, gv, cs\%diabatic\_aux\_CSp)}
\DoxyCodeLine{2045       \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_mixedlayer)}
\DoxyCodeLine{2046       \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2047         \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"After mixedlayer "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2048         \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"After mixedlayer"}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{2049 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2050       \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with 1st bulkmixedlayer (diabatic)"})}
\DoxyCodeLine{2051       \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'1st bulkmixedlayer'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{2052 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2053 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2054 }
\DoxyCodeLine{2055   \textcolor{keywordflow}{if} (cs\%debug) \&}
\DoxyCodeLine{2056     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"before find\_uv\_at\_h"}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2057   \textcolor{keywordflow}{if} (cs\%use\_kappa\_shear .or. cs\%use\_CVMix\_shear) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2058     \textcolor{keywordflow}{if} ((cs\%ML\_mix\_first > 0.0) .or. cs\%use\_geothermal) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2059       \textcolor{keyword}{call }find\_uv\_at\_h(u, v, h\_orig, u\_h, v\_h, g, gv, us, eaml, ebml)}
\DoxyCodeLine{2060       \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2061         \textcolor{keyword}{call }hchksum(eaml, \textcolor{stringliteral}{"after find\_uv\_at\_h eaml"}, g\%HI, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2062         \textcolor{keyword}{call }hchksum(ebml, \textcolor{stringliteral}{"after find\_uv\_at\_h ebml"}, g\%HI, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2063 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2064     \textcolor{keywordflow}{else}}
\DoxyCodeLine{2065       \textcolor{keyword}{call }find\_uv\_at\_h(u, v, h, u\_h, v\_h, g, gv, us)}
\DoxyCodeLine{2066 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2067     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with find\_uv\_at\_h (diabatic)"})}
\DoxyCodeLine{2068 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2069 }
\DoxyCodeLine{2070   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_set\_diffusivity)}
\DoxyCodeLine{2071   \textcolor{comment}{! Sets: Kd\_lay, Kd\_int, Kd\_extra\_T, Kd\_extra\_S and visc\%TKE\_turb}}
\DoxyCodeLine{2072   \textcolor{comment}{! Also changes: visc\%Kd\_shear and visc\%Kv\_shear}}
\DoxyCodeLine{2073   \textcolor{keywordflow}{if} ((cs\%halo\_TS\_diff > 0) .and. (cs\%ML\_mix\_first > 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2074     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%T)) \textcolor{keyword}{call }pass\_var(tv\%T, g\%Domain, halo=cs\%halo\_TS\_diff, complete=.false.)}
\DoxyCodeLine{2075     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%T)) \textcolor{keyword}{call }pass\_var(tv\%S, g\%Domain, halo=cs\%halo\_TS\_diff, complete=.false.)}
\DoxyCodeLine{2076     \textcolor{keyword}{call }pass\_var(h, g\%domain, halo=cs\%halo\_TS\_diff, complete=.true.)}
\DoxyCodeLine{2077 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2078   \textcolor{keywordflow}{if} (cs\%debug) \&}
\DoxyCodeLine{2079     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"before set\_diffusivity"}, u, v, h, g, gv, us, haloshift=cs\%halo\_TS\_diff)}
\DoxyCodeLine{2080   \textcolor{keywordflow}{if} (cs\%double\_diffuse) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2081     \textcolor{keyword}{call }set\_diffusivity(u, v, h, u\_h, v\_h, tv, fluxes, cs\%optics, visc, dt, g, gv, us, cs\%set\_diff\_CSp, \&}
\DoxyCodeLine{2082                          kd\_lay=kd\_lay, kd\_int=kd\_int, kd\_extra\_t=kd\_extra\_t, kd\_extra\_s=kd\_extra\_s)}
\DoxyCodeLine{2083   \textcolor{keywordflow}{else}}
\DoxyCodeLine{2084     \textcolor{keyword}{call }set\_diffusivity(u, v, h, u\_h, v\_h, tv, fluxes, cs\%optics, visc, dt, g, gv, us, \&}
\DoxyCodeLine{2085                          cs\%set\_diff\_CSp, kd\_lay=kd\_lay, kd\_int=kd\_int)}
\DoxyCodeLine{2086 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2087   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_set\_diffusivity)}
\DoxyCodeLine{2088   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with set\_diffusivity (diabatic)"})}
\DoxyCodeLine{2089 }
\DoxyCodeLine{2090   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2091     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after set\_diffusivity "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2092     \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after set\_diffusivity "}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{2093     \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after set\_diffusivity "}, tv, g)}
\DoxyCodeLine{2094     \textcolor{keyword}{call }hchksum(kd\_lay, \textcolor{stringliteral}{"after set\_diffusivity Kd\_lay"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{2095     \textcolor{keyword}{call }hchksum(kd\_int, \textcolor{stringliteral}{"after set\_diffusivity Kd\_Int"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{2096 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2097 }
\DoxyCodeLine{2098 }
\DoxyCodeLine{2099   \textcolor{keywordflow}{if} (cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2100     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_kpp)}
\DoxyCodeLine{2101     \textcolor{comment}{! KPP needs the surface buoyancy flux but does not update state variables.}}
\DoxyCodeLine{2102     \textcolor{comment}{! We could make this call higher up to avoid a repeat unpacking of the surface fluxes.}}
\DoxyCodeLine{2103     \textcolor{comment}{! Sets: CS\%KPP\_buoy\_flux, CS\%KPP\_temp\_flux, CS\%KPP\_salt\_flux}}
\DoxyCodeLine{2104     \textcolor{comment}{! NOTE: CS\%KPP\_buoy\_flux, CS\%KPP\_temp\_flux, CS\%KPP\_salt\_flux are returned as rates (i.e. stuff per second)}}
\DoxyCodeLine{2105     \textcolor{comment}{! unlike other instances where the fluxes are integrated in time over a time-\/step.}}
\DoxyCodeLine{2106     \textcolor{keyword}{call }calculatebuoyancyflux2d(g, gv, us, fluxes, cs\%optics, h, tv\%T, tv\%S, tv, \&}
\DoxyCodeLine{2107                                  cs\%KPP\_buoy\_flux, cs\%KPP\_temp\_flux, cs\%KPP\_salt\_flux)}
\DoxyCodeLine{2108     \textcolor{comment}{! The KPP scheme calculates boundary layer diffusivities and non-\/local transport.}}
\DoxyCodeLine{2109 }
\DoxyCodeLine{2110     \textcolor{comment}{! Set diffusivities for heat and salt separately}}
\DoxyCodeLine{2111 }
\DoxyCodeLine{2112     \textcolor{keywordflow}{if} (cs\%double\_diffuse) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2113       \textcolor{comment}{! Add contribution from double diffusion}}
\DoxyCodeLine{2114       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{2115       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2116         kd\_salt(i,j,k) = kd\_int(i,j,k) + kd\_extra\_s(i,j,k)}
\DoxyCodeLine{2117         kd\_heat(i,j,k) = kd\_int(i,j,k) + kd\_extra\_t(i,j,k)}
\DoxyCodeLine{2118 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2119     \textcolor{keywordflow}{else}}
\DoxyCodeLine{2120       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{2121       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2122         kd\_salt(i,j,k) = kd\_int(i,j,k)}
\DoxyCodeLine{2123         kd\_heat(i,j,k) = kd\_int(i,j,k)}
\DoxyCodeLine{2124 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2125 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2126 }
\DoxyCodeLine{2127     \textcolor{keyword}{call }kpp\_compute\_bld(cs\%KPP\_CSp, g, gv, us, h, tv\%T, tv\%S, u, v, tv, \&}
\DoxyCodeLine{2128                          fluxes\%ustar, cs\%KPP\_buoy\_flux, waves=waves)}
\DoxyCodeLine{2129 }
\DoxyCodeLine{2130     \textcolor{keyword}{call }kpp\_calculate(cs\%KPP\_CSp, g, gv, us, h, fluxes\%ustar, cs\%KPP\_buoy\_flux, kd\_heat, \&}
\DoxyCodeLine{2131                        kd\_salt, visc\%Kv\_shear, cs\%KPP\_NLTheat, cs\%KPP\_NLTscalar, waves=waves)}
\DoxyCodeLine{2132 }
\DoxyCodeLine{2133     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(hml)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2134       \textcolor{keyword}{call }kpp\_get\_bld(cs\%KPP\_CSp, hml(:,:), g, us)}
\DoxyCodeLine{2135       \textcolor{keyword}{call }pass\_var(hml, g\%domain, halo=1)}
\DoxyCodeLine{2136       \textcolor{comment}{! If visc\%MLD exists, copy KPP's BLD into it}}
\DoxyCodeLine{2137       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%MLD)) visc\%MLD(:,:) = hml(:,:)}
\DoxyCodeLine{2138 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2139 }
\DoxyCodeLine{2140     \textcolor{keywordflow}{if} (.not. cs\%KPPisPassive) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2141       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{2142       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2143         kd\_int(i,j,k) = min( kd\_salt(i,j,k),  kd\_heat(i,j,k) )}
\DoxyCodeLine{2144 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2145       \textcolor{keywordflow}{if} (cs\%double\_diffuse) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2146         \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{2147         \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2148           kd\_extra\_s(i,j,k) = (kd\_salt(i,j,k) -\/ kd\_int(i,j,k))}
\DoxyCodeLine{2149           kd\_extra\_t(i,j,k) = (kd\_heat(i,j,k) -\/ kd\_int(i,j,k))}
\DoxyCodeLine{2150 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2151 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2152 \textcolor{keywordflow}{    endif} \textcolor{comment}{! not passive}}
\DoxyCodeLine{2153 }
\DoxyCodeLine{2154     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_kpp)}
\DoxyCodeLine{2155     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with KPP\_calculate (diabatic)"})}
\DoxyCodeLine{2156     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2157       \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after KPP"}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2158       \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after KPP"}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{2159       \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after KPP"}, tv, g)}
\DoxyCodeLine{2160       \textcolor{keyword}{call }hchksum(kd\_lay, \textcolor{stringliteral}{"after KPP Kd\_lay"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{2161       \textcolor{keyword}{call }hchksum(kd\_int, \textcolor{stringliteral}{"after KPP Kd\_Int"}, g\%HI, haloshift=0, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{2162 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2163 }
\DoxyCodeLine{2164 \textcolor{keywordflow}{  endif}  \textcolor{comment}{! endif for KPP}}
\DoxyCodeLine{2165 }
\DoxyCodeLine{2166   \textcolor{comment}{! Add vertical diff./visc. due to convection (computed via CVMix)}}
\DoxyCodeLine{2167   \textcolor{keywordflow}{if} (cs\%use\_CVMix\_conv) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2168     \textcolor{keyword}{call }calculate\_cvmix\_conv(h, tv, g, gv, us, cs\%CVMix\_conv\_csp, hml, kd=kd\_int, kv=visc\%Kv\_slow)}
\DoxyCodeLine{2169 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2170 }
\DoxyCodeLine{2171   \textcolor{keywordflow}{if} (cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2172     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_kpp)}
\DoxyCodeLine{2173     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2174       \textcolor{keyword}{call }hchksum(cs\%KPP\_temp\_flux, \textcolor{stringliteral}{"before KPP\_applyNLT netHeat"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2175       \textcolor{keyword}{call }hchksum(cs\%KPP\_salt\_flux, \textcolor{stringliteral}{"before KPP\_applyNLT netSalt"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2176       \textcolor{keyword}{call }hchksum(cs\%KPP\_NLTheat, \textcolor{stringliteral}{"before KPP\_applyNLT NLTheat"}, g\%HI, haloshift=0)}
\DoxyCodeLine{2177       \textcolor{keyword}{call }hchksum(cs\%KPP\_NLTscalar, \textcolor{stringliteral}{"before KPP\_applyNLT NLTscalar"}, g\%HI, haloshift=0)}
\DoxyCodeLine{2178 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2179     \textcolor{comment}{! Apply non-\/local transport of heat and salt}}
\DoxyCodeLine{2180     \textcolor{comment}{! Changes: tv\%T, tv\%S}}
\DoxyCodeLine{2181     \textcolor{keyword}{call }kpp\_nonlocaltransport\_temp(cs\%KPP\_CSp, g, gv, h, cs\%KPP\_NLTheat,   cs\%KPP\_temp\_flux, \&}
\DoxyCodeLine{2182                                     us\%T\_to\_s*dt, tv\%T, us\%Q\_to\_J\_kg*tv\%C\_p)}
\DoxyCodeLine{2183     \textcolor{keyword}{call }kpp\_nonlocaltransport\_saln(cs\%KPP\_CSp, g, gv, h, cs\%KPP\_NLTscalar, cs\%KPP\_salt\_flux, \&}
\DoxyCodeLine{2184                                     us\%T\_to\_s*dt, tv\%S)}
\DoxyCodeLine{2185     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_kpp)}
\DoxyCodeLine{2186     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with KPP\_applyNonLocalTransport (diabatic)"})}
\DoxyCodeLine{2187     \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'KPP\_applyNonLocalTransport'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{2188 }
\DoxyCodeLine{2189     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2190       \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after KPP\_applyNLT "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2191       \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after KPP\_applyNLT "}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{2192       \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after KPP\_applyNLT "}, tv, g)}
\DoxyCodeLine{2193 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2194 \textcolor{keywordflow}{  endif} \textcolor{comment}{! endif for KPP}}
\DoxyCodeLine{2195 }
\DoxyCodeLine{2196   \textcolor{comment}{! Differential diffusion done here.}}
\DoxyCodeLine{2197   \textcolor{comment}{! Changes: tv\%T, tv\%S}}
\DoxyCodeLine{2198   \textcolor{keywordflow}{if} (cs\%double\_diffuse .and. \textcolor{keyword}{associated}(tv\%T)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2199 }
\DoxyCodeLine{2200     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_differential\_diff)}
\DoxyCodeLine{2201     \textcolor{keyword}{call }differential\_diffuse\_t\_s(h, tv\%T, tv\%S, kd\_extra\_t, kd\_extra\_s, dt, g, gv)}
\DoxyCodeLine{2202     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_differential\_diff)}
\DoxyCodeLine{2203     \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with differential\_diffuse\_T\_S (diabatic)"})}
\DoxyCodeLine{2204     \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'differential\_diffuse\_T\_S'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{2205 }
\DoxyCodeLine{2206     \textcolor{comment}{! increment heat and salt diffusivity.}}
\DoxyCodeLine{2207     \textcolor{comment}{! CS\%useKPP==.true. already has extra\_T and extra\_S included}}
\DoxyCodeLine{2208     \textcolor{keywordflow}{if} (.not. cs\%useKPP) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2209       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{2210       \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2211         kd\_heat(i,j,k) = kd\_heat(i,j,k) + kd\_extra\_t(i,j,k)}
\DoxyCodeLine{2212         kd\_salt(i,j,k) = kd\_salt(i,j,k) + kd\_extra\_s(i,j,k)}
\DoxyCodeLine{2213 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2214 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2215 }
\DoxyCodeLine{2216 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2217 }
\DoxyCodeLine{2218   \textcolor{comment}{! Calculate layer entrainments and detrainments from diffusivities and differences between}}
\DoxyCodeLine{2219   \textcolor{comment}{! layer and target densities (i.e. do remapping as well as diffusion).}}
\DoxyCodeLine{2220   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_entrain)}
\DoxyCodeLine{2221   \textcolor{comment}{! Calculate appropriately limited diapycnal mass fluxes to account}}
\DoxyCodeLine{2222   \textcolor{comment}{! for diapycnal diffusion and advection.  Sets: ea, eb. Changes: kb}}
\DoxyCodeLine{2223   \textcolor{keyword}{call }entrainment\_diffusive(h, tv, fluxes, dt, g, gv, us, cs\%entrain\_diffusive\_CSp, \&}
\DoxyCodeLine{2224                              ea, eb, kb, kd\_lay=kd\_lay, kd\_int=kd\_int)}
\DoxyCodeLine{2225   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_entrain)}
\DoxyCodeLine{2226   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with Entrainment\_diffusive (diabatic)"})}
\DoxyCodeLine{2227 }
\DoxyCodeLine{2228   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2229     \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after calc\_entrain "}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{2230     \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after calc\_entrain "}, tv, g)}
\DoxyCodeLine{2231     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after calc\_entrain "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2232     \textcolor{keyword}{call }hchksum(ea, \textcolor{stringliteral}{"after calc\_entrain ea"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2233     \textcolor{keyword}{call }hchksum(eb, \textcolor{stringliteral}{"after calc\_entrain eb"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2234 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2235 }
\DoxyCodeLine{2236   \textcolor{comment}{! Save fields before boundary forcing is applied for tendency diagnostics}}
\DoxyCodeLine{2237   \textcolor{keywordflow}{if} (cs\%boundary\_forcing\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2238     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2239       h\_diag(i,j,k)    = h(i,j,k)}
\DoxyCodeLine{2240       temp\_diag(i,j,k) = tv\%T(i,j,k)}
\DoxyCodeLine{2241       saln\_diag(i,j,k) = tv\%S(i,j,k)}
\DoxyCodeLine{2242 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2243 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2244 }
\DoxyCodeLine{2245   \textcolor{comment}{! Update h according to divergence of the difference between}}
\DoxyCodeLine{2246   \textcolor{comment}{! ea and eb. We keep a record of the original h in hold.}}
\DoxyCodeLine{2247   \textcolor{comment}{! In the following, the checks for negative values are to guard}}
\DoxyCodeLine{2248   \textcolor{comment}{! against instances where entrainment drives a layer to}}
\DoxyCodeLine{2249   \textcolor{comment}{! negative thickness.  This situation will never happen if}}
\DoxyCodeLine{2250   \textcolor{comment}{! enough iterations are permitted in Calculate\_Entrainment.}}
\DoxyCodeLine{2251   \textcolor{comment}{! Even if too few iterations are allowed, it is still guarded}}
\DoxyCodeLine{2252   \textcolor{comment}{! against.  In other words the checks are probably unnecessary.}}
\DoxyCodeLine{2253   \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{2254   \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{2255     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2256       hold(i,j,1) = h(i,j,1)}
\DoxyCodeLine{2257       h(i,j,1) = h(i,j,1) + (eb(i,j,1) -\/ ea(i,j,2))}
\DoxyCodeLine{2258       hold(i,j,nz) = h(i,j,nz)}
\DoxyCodeLine{2259       h(i,j,nz) = h(i,j,nz) + (ea(i,j,nz) -\/ eb(i,j,nz-\/1))}
\DoxyCodeLine{2260       \textcolor{keywordflow}{if} (h(i,j,1) <= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2261         h(i,j,1) = gv\%Angstrom\_H}
\DoxyCodeLine{2262 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2263       \textcolor{keywordflow}{if} (h(i,j,nz) <= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2264         h(i,j,nz) = gv\%Angstrom\_H}
\DoxyCodeLine{2265 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2266 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2267     \textcolor{keywordflow}{do} k=2,nz-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2268       hold(i,j,k) = h(i,j,k)}
\DoxyCodeLine{2269       h(i,j,k) = h(i,j,k) + ((ea(i,j,k) -\/ eb(i,j,k-\/1)) + \&}
\DoxyCodeLine{2270                     (eb(i,j,k) -\/ ea(i,j,k+1)))}
\DoxyCodeLine{2271       \textcolor{keywordflow}{if} (h(i,j,k) <= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2272         h(i,j,k) = gv\%Angstrom\_H}
\DoxyCodeLine{2273 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2274 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2275 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{2276   \textcolor{comment}{! Checks for negative thickness may have changed layer thicknesses}}
\DoxyCodeLine{2277   \textcolor{keyword}{call }diag\_update\_remap\_grids(cs\%diag)}
\DoxyCodeLine{2278 }
\DoxyCodeLine{2279   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2280     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after negative check "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2281     \textcolor{keyword}{call }mom\_forcing\_chksum(\textcolor{stringliteral}{"after negative check "}, fluxes, g, us, haloshift=0)}
\DoxyCodeLine{2282     \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after negative check "}, tv, g)}
\DoxyCodeLine{2283 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2284   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with h=ea-\/eb (diabatic)"})}
\DoxyCodeLine{2285   \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'h=ea-\/eb'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{2286 }
\DoxyCodeLine{2287   \textcolor{comment}{! Here, T and S are updated according to ea and eb.}}
\DoxyCodeLine{2288   \textcolor{comment}{! If using the bulk mixed layer, T and S are also updated}}
\DoxyCodeLine{2289   \textcolor{comment}{! by surface fluxes (in fluxes\%*).}}
\DoxyCodeLine{2290   \textcolor{comment}{! This is a very long block.}}
\DoxyCodeLine{2291   \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2292 }
\DoxyCodeLine{2293     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%T)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2294       \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_tridiag)}
\DoxyCodeLine{2295       \textcolor{comment}{! Temperature and salinity (as state variables) are treated}}
\DoxyCodeLine{2296       \textcolor{comment}{! differently from other tracers to insure massless layers that}}
\DoxyCodeLine{2297       \textcolor{comment}{! are lighter than the mixed layer have temperatures and salinities}}
\DoxyCodeLine{2298       \textcolor{comment}{! that correspond to their prescribed densities.}}
\DoxyCodeLine{2299       \textcolor{keywordflow}{if} (cs\%massless\_match\_targets) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2300         \textcolor{comment}{!\$OMP parallel do default (shared) private(h\_tr,b1,d1,c1,b\_denom\_1)}}
\DoxyCodeLine{2301         \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{2302           \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2303             h\_tr = hold(i,j,1) + h\_neglect}
\DoxyCodeLine{2304             b1(i) = 1.0 / (h\_tr + eb(i,j,1))}
\DoxyCodeLine{2305             d1(i) = h\_tr * b1(i)}
\DoxyCodeLine{2306             tv\%T(i,j,1) = b1(i) * (h\_tr*tv\%T(i,j,1))}
\DoxyCodeLine{2307             tv\%S(i,j,1) = b1(i) * (h\_tr*tv\%S(i,j,1))}
\DoxyCodeLine{2308 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{2309           \textcolor{keywordflow}{do} k=2,nkmb ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2310             c1(i,k) = eb(i,j,k-\/1) * b1(i)}
\DoxyCodeLine{2311             h\_tr = hold(i,j,k) + h\_neglect}
\DoxyCodeLine{2312             b\_denom\_1 = h\_tr + d1(i)*ea(i,j,k)}
\DoxyCodeLine{2313             b1(i) = 1.0 / (b\_denom\_1 + eb(i,j,k))}
\DoxyCodeLine{2314             \textcolor{keywordflow}{if} (k<nkmb) d1(i) = b\_denom\_1 * b1(i)}
\DoxyCodeLine{2315             tv\%T(i,j,k) = b1(i) * (h\_tr*tv\%T(i,j,k) + ea(i,j,k)*tv\%T(i,j,k-\/1))}
\DoxyCodeLine{2316             tv\%S(i,j,k) = b1(i) * (h\_tr*tv\%S(i,j,k) + ea(i,j,k)*tv\%S(i,j,k-\/1))}
\DoxyCodeLine{2317 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2318 }
\DoxyCodeLine{2319           \textcolor{keywordflow}{do} k=nkmb+1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2320             \textcolor{keywordflow}{if} (k == kb(i,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2321               c1(i,k) = eb(i,j,k-\/1) * b1(i)}
\DoxyCodeLine{2322               d1(i) = (((eb(i,j,nkmb)-\/eb(i,j,k-\/1)) + hold(i,j,nkmb) + h\_neglect) + \&}
\DoxyCodeLine{2323                        d1(i)*ea(i,j,nkmb)) * b1(i)}
\DoxyCodeLine{2324               h\_tr = hold(i,j,k) + h\_neglect}
\DoxyCodeLine{2325               b\_denom\_1 = h\_tr + d1(i)*ea(i,j,k)}
\DoxyCodeLine{2326               b1(i) = 1.0 / (b\_denom\_1 + eb(i,j,k))}
\DoxyCodeLine{2327               d1(i) = b\_denom\_1 * b1(i)}
\DoxyCodeLine{2328               tv\%T(i,j,k) = b1(i) * (h\_tr*tv\%T(i,j,k) + ea(i,j,k)*tv\%T(i,j,nkmb))}
\DoxyCodeLine{2329               tv\%S(i,j,k) = b1(i) * (h\_tr*tv\%S(i,j,k) + ea(i,j,k)*tv\%S(i,j,nkmb))}
\DoxyCodeLine{2330             \textcolor{keywordflow}{elseif} (k > kb(i,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2331               c1(i,k) = eb(i,j,k-\/1) * b1(i)}
\DoxyCodeLine{2332               h\_tr = hold(i,j,k) + h\_neglect}
\DoxyCodeLine{2333               b\_denom\_1 = h\_tr + d1(i)*ea(i,j,k)}
\DoxyCodeLine{2334               b1(i) = 1.0 / (b\_denom\_1 + eb(i,j,k))}
\DoxyCodeLine{2335               d1(i) = b\_denom\_1 * b1(i)}
\DoxyCodeLine{2336               tv\%T(i,j,k) = b1(i) * (h\_tr*tv\%T(i,j,k) + ea(i,j,k)*tv\%T(i,j,k-\/1))}
\DoxyCodeLine{2337               tv\%S(i,j,k) = b1(i) * (h\_tr*tv\%S(i,j,k) + ea(i,j,k)*tv\%S(i,j,k-\/1))}
\DoxyCodeLine{2338             \textcolor{keywordflow}{elseif} (eb(i,j,k) < eb(i,j,k-\/1)) \textcolor{keywordflow}{then} \textcolor{comment}{! (note that k < kb(i,j))}}
\DoxyCodeLine{2339               \textcolor{comment}{!   The bottommost buffer layer might entrain all the mass from some}}
\DoxyCodeLine{2340               \textcolor{comment}{! of the interior layers that are thin and lighter in the coordinate}}
\DoxyCodeLine{2341               \textcolor{comment}{! density than that buffer layer.  The T and S of these newly}}
\DoxyCodeLine{2342               \textcolor{comment}{! massless interior layers are unchanged.}}
\DoxyCodeLine{2343               tv\%T(i,j,nkmb) = tv\%T(i,j,nkmb) + b1(i) * (eb(i,j,k-\/1) -\/ eb(i,j,k)) * tv\%T(i,j,k)}
\DoxyCodeLine{2344               tv\%S(i,j,nkmb) = tv\%S(i,j,nkmb) + b1(i) * (eb(i,j,k-\/1) -\/ eb(i,j,k)) * tv\%S(i,j,k)}
\DoxyCodeLine{2345 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{2346 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2347 }
\DoxyCodeLine{2348           \textcolor{keywordflow}{do} k=nz-\/1,nkmb,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2349             \textcolor{keywordflow}{if} (k >= kb(i,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2350               tv\%T(i,j,k) = tv\%T(i,j,k) + c1(i,k+1)*tv\%T(i,j,k+1)}
\DoxyCodeLine{2351               tv\%S(i,j,k) = tv\%S(i,j,k) + c1(i,k+1)*tv\%S(i,j,k+1)}
\DoxyCodeLine{2352 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{2353 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2354           \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (kb(i,j) <= nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2355             tv\%T(i,j,nkmb) = tv\%T(i,j,nkmb) + c1(i,kb(i,j))*tv\%T(i,j,kb(i,j))}
\DoxyCodeLine{2356             tv\%S(i,j,nkmb) = tv\%S(i,j,nkmb) + c1(i,kb(i,j))*tv\%S(i,j,kb(i,j))}
\DoxyCodeLine{2357 \textcolor{keywordflow}{          endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2358           \textcolor{keywordflow}{do} k=nkmb-\/1,1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2359             tv\%T(i,j,k) = tv\%T(i,j,k) + c1(i,k+1)*tv\%T(i,j,k+1)}
\DoxyCodeLine{2360             tv\%S(i,j,k) = tv\%S(i,j,k) + c1(i,k+1)*tv\%S(i,j,k+1)}
\DoxyCodeLine{2361 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2362 \textcolor{keywordflow}{        enddo} \textcolor{comment}{! end of j loop}}
\DoxyCodeLine{2363       \textcolor{keywordflow}{else} \textcolor{comment}{! .not. massless\_match\_targets}}
\DoxyCodeLine{2364         \textcolor{comment}{! This simpler form allows T \& S to be too dense for the layers}}
\DoxyCodeLine{2365         \textcolor{comment}{! between the buffer layers and the interior.}}
\DoxyCodeLine{2366         \textcolor{comment}{! Changes: T, S}}
\DoxyCodeLine{2367         \textcolor{keywordflow}{if} (cs\%tracer\_tridiag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2368           \textcolor{keyword}{call }tracer\_vertdiff(hold, ea, eb, dt, tv\%T, g, gv)}
\DoxyCodeLine{2369           \textcolor{keyword}{call }tracer\_vertdiff(hold, ea, eb, dt, tv\%S, g, gv)}
\DoxyCodeLine{2370         \textcolor{keywordflow}{else}}
\DoxyCodeLine{2371           \textcolor{keyword}{call }tridiagts(g, gv, is, ie, js, je, hold, ea, eb, tv\%T, tv\%S)}
\DoxyCodeLine{2372 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2373 \textcolor{keywordflow}{      endif} \textcolor{comment}{! massless\_match\_targets}}
\DoxyCodeLine{2374       \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_tridiag)}
\DoxyCodeLine{2375 }
\DoxyCodeLine{2376 \textcolor{keywordflow}{    endif} \textcolor{comment}{! endif for associated(T)}}
\DoxyCodeLine{2377     \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'BML tridiag'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{2378 }
\DoxyCodeLine{2379     \textcolor{keywordflow}{if} ((cs\%ML\_mix\_first > 0.0) .or. cs\%use\_geothermal) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2380       \textcolor{comment}{! The mixed layer code has already been called, but there is some needed}}
\DoxyCodeLine{2381       \textcolor{comment}{! bookkeeping.}}
\DoxyCodeLine{2382       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{2383       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2384         hold(i,j,k) = h\_orig(i,j,k)}
\DoxyCodeLine{2385         ea(i,j,k) = ea(i,j,k) + eaml(i,j,k)}
\DoxyCodeLine{2386         eb(i,j,k) = eb(i,j,k) + ebml(i,j,k)}
\DoxyCodeLine{2387 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2388       \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2389         \textcolor{keyword}{call }hchksum(ea, \textcolor{stringliteral}{"after ea = ea + eaml"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2390         \textcolor{keyword}{call }hchksum(eb, \textcolor{stringliteral}{"after eb = eb + ebml"}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2391 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2392 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2393 }
\DoxyCodeLine{2394     \textcolor{keywordflow}{if} (cs\%ML\_mix\_first < 1.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2395     \textcolor{comment}{!  Call the mixed layer code now, perhaps for a second time.}}
\DoxyCodeLine{2396     \textcolor{comment}{!  This subroutine (1)  Cools the mixed layer.}}
\DoxyCodeLine{2397     \textcolor{comment}{!    (2) Performs convective adjustment by mixed layer entrainment.}}
\DoxyCodeLine{2398     \textcolor{comment}{!    (3) Heats the mixed layer and causes it to detrain to}}
\DoxyCodeLine{2399     \textcolor{comment}{!        Monin-\/Obukhov depth or minimum mixed layer depth.}}
\DoxyCodeLine{2400     \textcolor{comment}{!    (4) Uses any remaining TKE to drive mixed layer entrainment.}}
\DoxyCodeLine{2401     \textcolor{comment}{!    (5) Possibly splits the buffer layer into two isopycnal layers.}}
\DoxyCodeLine{2402 }
\DoxyCodeLine{2403       \textcolor{keyword}{call }find\_uv\_at\_h(u, v, hold, u\_h, v\_h, g, gv, us, ea, eb)}
\DoxyCodeLine{2404       \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"find\_uv\_at\_h1 "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2405 }
\DoxyCodeLine{2406       dt\_mix = min(dt, dt*(1.0 -\/ cs\%ML\_mix\_first))}
\DoxyCodeLine{2407       \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_mixedlayer)}
\DoxyCodeLine{2408       \textcolor{comment}{! Changes: h, tv\%T, tv\%S, ea and eb  (G is also inout???)}}
\DoxyCodeLine{2409       \textcolor{keyword}{call }bulkmixedlayer(h, u\_h, v\_h, tv, fluxes, dt\_mix, ea, eb, \&}
\DoxyCodeLine{2410                           g, gv, us, cs\%bulkmixedlayer\_CSp, cs\%optics, \&}
\DoxyCodeLine{2411                           hml, cs\%aggregate\_FW\_forcing, dt, last\_call=.true.)}
\DoxyCodeLine{2412 }
\DoxyCodeLine{2413       \textcolor{comment}{!  Keep salinity from falling below a small but positive threshold.}}
\DoxyCodeLine{2414       \textcolor{comment}{!  This constraint is needed for SIS1 ice model, which can extract}}
\DoxyCodeLine{2415       \textcolor{comment}{!  more salt than is present in the ocean. SIS2 does not suffer}}
\DoxyCodeLine{2416       \textcolor{comment}{!  from this limitation, in which case we can let salinity=0 and still}}
\DoxyCodeLine{2417       \textcolor{comment}{!  have salt conserved with SIS2 ice. So for SIS2, we can run with}}
\DoxyCodeLine{2418       \textcolor{comment}{!  BOUND\_SALINITY=False in MOM.F90.}}
\DoxyCodeLine{2419       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%S) .and. \textcolor{keyword}{associated}(tv\%salt\_deficit)) \&}
\DoxyCodeLine{2420         \textcolor{keyword}{call }adjust\_salt(h, tv, g, gv, cs\%diabatic\_aux\_CSp)}
\DoxyCodeLine{2421 }
\DoxyCodeLine{2422       \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_mixedlayer)}
\DoxyCodeLine{2423       \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with 2nd bulkmixedlayer (diabatic)"})}
\DoxyCodeLine{2424       \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'2nd bulkmixedlayer'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{2425 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2426 }
\DoxyCodeLine{2427   \textcolor{keywordflow}{else}  \textcolor{comment}{! following block for when NOT using BULKMIXEDLAYER}}
\DoxyCodeLine{2428 }
\DoxyCodeLine{2429     \textcolor{comment}{! calculate change in temperature \& salinity due to dia-\/coordinate surface diffusion}}
\DoxyCodeLine{2430     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%T)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2431 }
\DoxyCodeLine{2432       \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2433         \textcolor{keyword}{call }hchksum(ea, \textcolor{stringliteral}{"before triDiagTS ea "}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2434         \textcolor{keyword}{call }hchksum(eb, \textcolor{stringliteral}{"before triDiagTS eb "}, g\%HI, haloshift=0, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2435 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2436       \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_tridiag)}
\DoxyCodeLine{2437 }
\DoxyCodeLine{2438       \textcolor{comment}{!  Keep salinity from falling below a small but positive threshold.}}
\DoxyCodeLine{2439       \textcolor{comment}{!  This constraint is needed for SIS1 ice model, which can extract}}
\DoxyCodeLine{2440       \textcolor{comment}{!  more salt than is present in the ocean. SIS2 does not suffer}}
\DoxyCodeLine{2441       \textcolor{comment}{!  from this limitation, in which case we can let salinity=0 and still}}
\DoxyCodeLine{2442       \textcolor{comment}{!  have salt conserved with SIS2 ice. So for SIS2, we can run with}}
\DoxyCodeLine{2443       \textcolor{comment}{!  BOUND\_SALINITY=False in MOM.F90.}}
\DoxyCodeLine{2444       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%S) .and. \textcolor{keyword}{associated}(tv\%salt\_deficit)) \&}
\DoxyCodeLine{2445         \textcolor{keyword}{call }adjust\_salt(h, tv, g, gv, cs\%diabatic\_aux\_CSp)}
\DoxyCodeLine{2446 }
\DoxyCodeLine{2447       \textcolor{keywordflow}{if} (cs\%diabatic\_diff\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2448         \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2449           temp\_diag(i,j,k) = tv\%T(i,j,k)}
\DoxyCodeLine{2450           saln\_diag(i,j,k) = tv\%S(i,j,k)}
\DoxyCodeLine{2451 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2452 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2453 }
\DoxyCodeLine{2454       \textcolor{comment}{! Changes T and S via the tridiagonal solver; no change to h}}
\DoxyCodeLine{2455       \textcolor{keywordflow}{if} (cs\%tracer\_tridiag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2456         \textcolor{keyword}{call }tracer\_vertdiff(hold, ea, eb, dt, tv\%T, g, gv)}
\DoxyCodeLine{2457         \textcolor{keyword}{call }tracer\_vertdiff(hold, ea, eb, dt, tv\%S, g, gv)}
\DoxyCodeLine{2458       \textcolor{keywordflow}{else}}
\DoxyCodeLine{2459         \textcolor{keyword}{call }tridiagts(g, gv, is, ie, js, je, hold, ea, eb, tv\%T, tv\%S)}
\DoxyCodeLine{2460 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2461 }
\DoxyCodeLine{2462       \textcolor{comment}{! diagnose temperature, salinity, heat, and salt tendencies}}
\DoxyCodeLine{2463       \textcolor{comment}{! Note: hold here refers to the thicknesses from before the dual-\/entraintment when using}}
\DoxyCodeLine{2464       \textcolor{comment}{! the bulk mixed layer scheme, so tendencies should be posted on hold.}}
\DoxyCodeLine{2465       \textcolor{keywordflow}{if} (cs\%diabatic\_diff\_tendency\_diag) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2466         \textcolor{keyword}{call }diagnose\_diabatic\_diff\_tendency(tv, hold, temp\_diag, saln\_diag, dt, g, gv, us, cs)}
\DoxyCodeLine{2467         \textcolor{keywordflow}{if} (cs\%id\_diabatic\_diff\_h > 0) \textcolor{keyword}{call }post\_data(cs\%id\_diabatic\_diff\_h, hold, cs\%diag, alt\_h=hold)}
\DoxyCodeLine{2468 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2469 }
\DoxyCodeLine{2470       \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_tridiag)}
\DoxyCodeLine{2471       \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with triDiagTS (diabatic)"})}
\DoxyCodeLine{2472 }
\DoxyCodeLine{2473 \textcolor{keywordflow}{    endif}  \textcolor{comment}{! endif corresponding to if (associated(tv\%T))}}
\DoxyCodeLine{2474     \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'triDiagTS'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{2475 }
\DoxyCodeLine{2476 \textcolor{keywordflow}{  endif}  \textcolor{comment}{! endif for the BULKMIXEDLAYER block}}
\DoxyCodeLine{2477 }
\DoxyCodeLine{2478   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2479     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after mixed layer "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2480     \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"after mixed layer "}, tv, g)}
\DoxyCodeLine{2481     \textcolor{keyword}{call }hchksum(ea, \textcolor{stringliteral}{"after mixed layer ea"}, g\%HI, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2482     \textcolor{keyword}{call }hchksum(eb, \textcolor{stringliteral}{"after mixed layer eb"}, g\%HI, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2483 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2484 }
\DoxyCodeLine{2485   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_remap)}
\DoxyCodeLine{2486   \textcolor{keyword}{call }regularize\_layers(h, tv, dt, ea, eb, g, gv, us, cs\%regularize\_layers\_CSp)}
\DoxyCodeLine{2487   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_remap)}
\DoxyCodeLine{2488   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_waypoint(\textcolor{stringliteral}{"done with regularize\_layers (diabatic)"})}
\DoxyCodeLine{2489   \textcolor{keywordflow}{if} (cs\%debugConservation) \textcolor{keyword}{call }mom\_state\_stats(\textcolor{stringliteral}{'regularize\_layers'}, u, v, h, tv\%T, tv\%S, g, gv, us)}
\DoxyCodeLine{2490 }
\DoxyCodeLine{2491   \textcolor{comment}{! Whenever thickness changes let the diag manager know, as the}}
\DoxyCodeLine{2492   \textcolor{comment}{! target grids for vertical remapping may need to be regenerated.}}
\DoxyCodeLine{2493   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%du\_dt\_dia) .or. \textcolor{keyword}{associated}(adp\%dv\_dt\_dia)) \&}
\DoxyCodeLine{2494     \textcolor{comment}{! Remapped d[uv]dt\_dia require east/north halo updates of h}}
\DoxyCodeLine{2495     \textcolor{keyword}{call }pass\_var(h, g\%domain, to\_west+to\_south+omit\_corners, halo=1)}
\DoxyCodeLine{2496   \textcolor{keyword}{call }diag\_update\_remap\_grids(cs\%diag)}
\DoxyCodeLine{2497 }
\DoxyCodeLine{2498   \textcolor{comment}{! diagnostics}}
\DoxyCodeLine{2499   idt = 1.0 / dt}
\DoxyCodeLine{2500   \textcolor{keywordflow}{if} ((cs\%id\_Tdif > 0) .or. (cs\%id\_Tadv > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2501     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2502       tdif\_flx(i,j,1) = 0.0 ; tdif\_flx(i,j,nz+1) = 0.0}
\DoxyCodeLine{2503       tadv\_flx(i,j,1) = 0.0 ; tadv\_flx(i,j,nz+1) = 0.0}
\DoxyCodeLine{2504 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2505     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{2506     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2507       tdif\_flx(i,j,k) = (idt * 0.5*(ea(i,j,k) + eb(i,j,k-\/1))) * \&}
\DoxyCodeLine{2508                         (tv\%T(i,j,k-\/1) -\/ tv\%T(i,j,k))}
\DoxyCodeLine{2509       tadv\_flx(i,j,k) = (idt * (ea(i,j,k) -\/ eb(i,j,k-\/1))) * \&}
\DoxyCodeLine{2510                     0.5*(tv\%T(i,j,k-\/1) + tv\%T(i,j,k))}
\DoxyCodeLine{2511 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2512 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2513   \textcolor{keywordflow}{if} ((cs\%id\_Sdif > 0) .or. (cs\%id\_Sadv > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2514     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2515       sdif\_flx(i,j,1) = 0.0 ; sdif\_flx(i,j,nz+1) = 0.0}
\DoxyCodeLine{2516       sadv\_flx(i,j,1) = 0.0 ; sadv\_flx(i,j,nz+1) = 0.0}
\DoxyCodeLine{2517 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2518     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{2519     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2520       sdif\_flx(i,j,k) = (idt * 0.5*(ea(i,j,k) + eb(i,j,k-\/1))) * \&}
\DoxyCodeLine{2521                         (tv\%S(i,j,k-\/1) -\/ tv\%S(i,j,k))}
\DoxyCodeLine{2522       sadv\_flx(i,j,k) = (idt * (ea(i,j,k) -\/ eb(i,j,k-\/1))) * \&}
\DoxyCodeLine{2523                     0.5*(tv\%S(i,j,k-\/1) + tv\%S(i,j,k))}
\DoxyCodeLine{2524 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2525 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2526 }
\DoxyCodeLine{2527   \textcolor{comment}{! mixing of passive tracers from massless boundary layers to interior}}
\DoxyCodeLine{2528   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_tracers)}
\DoxyCodeLine{2529   \textcolor{keywordflow}{if} (cs\%mix\_boundary\_tracers) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2530     tr\_ea\_bbl = gv\%Z\_to\_H * sqrt(dt*cs\%Kd\_BBL\_tr)}
\DoxyCodeLine{2531     \textcolor{comment}{!\$OMP parallel do default(shared) private(htot,in\_boundary,add\_ent)}}
\DoxyCodeLine{2532     \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{2533       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2534         ebtr(i,j,nz) = eb(i,j,nz)}
\DoxyCodeLine{2535         htot(i) = 0.0}
\DoxyCodeLine{2536         in\_boundary(i) = (g\%mask2dT(i,j) > 0.0)}
\DoxyCodeLine{2537 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{2538       \textcolor{keywordflow}{do} k=nz,2,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2539         \textcolor{keywordflow}{if} (in\_boundary(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2540           htot(i) = htot(i) + h(i,j,k)}
\DoxyCodeLine{2541           \textcolor{comment}{!   If diapycnal mixing has been suppressed because this is a massless}}
\DoxyCodeLine{2542           \textcolor{comment}{! layer near the bottom, add some mixing of tracers between these}}
\DoxyCodeLine{2543           \textcolor{comment}{! layers.  This flux is based on the harmonic mean of the two}}
\DoxyCodeLine{2544           \textcolor{comment}{! thicknesses, as this corresponds pretty closely (to within}}
\DoxyCodeLine{2545           \textcolor{comment}{! differences in the density jumps between layers) with what is done}}
\DoxyCodeLine{2546           \textcolor{comment}{! in the calculation of the fluxes in the first place.  Kd\_min\_tr}}
\DoxyCodeLine{2547           \textcolor{comment}{! should be much less than the values that have been set in Kd\_lay,}}
\DoxyCodeLine{2548           \textcolor{comment}{! perhaps a molecular diffusivity.}}
\DoxyCodeLine{2549           add\_ent = ((dt * cs\%Kd\_min\_tr) * gv\%Z\_to\_H**2) * \&}
\DoxyCodeLine{2550                     ((h(i,j,k-\/1)+h(i,j,k)+h\_neglect) / \&}
\DoxyCodeLine{2551                      (h(i,j,k-\/1)*h(i,j,k)+h\_neglect2)) -\/ \&}
\DoxyCodeLine{2552                     0.5*(ea(i,j,k) + eb(i,j,k-\/1))}
\DoxyCodeLine{2553           \textcolor{keywordflow}{if} (htot(i) < tr\_ea\_bbl) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2554             add\_ent = max(0.0, add\_ent, \&}
\DoxyCodeLine{2555                           (tr\_ea\_bbl -\/ htot(i)) -\/ min(ea(i,j,k), eb(i,j,k-\/1)))}
\DoxyCodeLine{2556           \textcolor{keywordflow}{elseif} (add\_ent < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2557             add\_ent = 0.0 ; in\_boundary(i) = .false.}
\DoxyCodeLine{2558 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{2559 }
\DoxyCodeLine{2560           ebtr(i,j,k-\/1) = eb(i,j,k-\/1) + add\_ent}
\DoxyCodeLine{2561           eatr(i,j,k) = ea(i,j,k) + add\_ent}
\DoxyCodeLine{2562         \textcolor{keywordflow}{else}}
\DoxyCodeLine{2563           ebtr(i,j,k-\/1) = eb(i,j,k-\/1) ; eatr(i,j,k) = ea(i,j,k)}
\DoxyCodeLine{2564 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{2565         \textcolor{keywordflow}{if} (cs\%double\_diffuse) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (kd\_extra\_s(i,j,k) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2566           add\_ent = ((dt * kd\_extra\_s(i,j,k)) * gv\%Z\_to\_H**2) / \&}
\DoxyCodeLine{2567              (0.25 * ((h(i,j,k-\/1) + h(i,j,k)) + (hold(i,j,k-\/1) + hold(i,j,k))) + \&}
\DoxyCodeLine{2568               h\_neglect)}
\DoxyCodeLine{2569           ebtr(i,j,k-\/1) = ebtr(i,j,k-\/1) + add\_ent}
\DoxyCodeLine{2570           eatr(i,j,k) = eatr(i,j,k) + add\_ent}
\DoxyCodeLine{2571 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{2572 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2573       \textcolor{keywordflow}{do} i=is,ie ; eatr(i,j,1) = ea(i,j,1) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2574 }
\DoxyCodeLine{2575 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2576 }
\DoxyCodeLine{2577     \textcolor{keyword}{call }call\_tracer\_column\_fns(hold, h, eatr, ebtr, fluxes, hml, dt, g, gv, us, tv, \&}
\DoxyCodeLine{2578                               cs\%optics, cs\%tracer\_flow\_CSp, cs\%debug)}
\DoxyCodeLine{2579 }
\DoxyCodeLine{2580   \textcolor{keywordflow}{elseif} (cs\%double\_diffuse) \textcolor{keywordflow}{then}  \textcolor{comment}{! extra diffusivity for passive tracers}}
\DoxyCodeLine{2581 }
\DoxyCodeLine{2582     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2583       ebtr(i,j,nz) = eb(i,j,nz) ; eatr(i,j,1) = ea(i,j,1)}
\DoxyCodeLine{2584 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2585     \textcolor{comment}{!\$OMP parallel do default(shared) private(add\_ent)}}
\DoxyCodeLine{2586     \textcolor{keywordflow}{do} k=nz,2,-\/1 ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2587       \textcolor{keywordflow}{if} (kd\_extra\_s(i,j,k) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2588         add\_ent = ((dt * kd\_extra\_s(i,j,k)) * gv\%Z\_to\_H**2) / \&}
\DoxyCodeLine{2589            (0.25 * ((h(i,j,k-\/1) + h(i,j,k)) + (hold(i,j,k-\/1) + hold(i,j,k))) + \&}
\DoxyCodeLine{2590             h\_neglect)}
\DoxyCodeLine{2591       \textcolor{keywordflow}{else}}
\DoxyCodeLine{2592         add\_ent = 0.0}
\DoxyCodeLine{2593 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{2594       ebtr(i,j,k-\/1) = eb(i,j,k-\/1) + add\_ent}
\DoxyCodeLine{2595       eatr(i,j,k) = ea(i,j,k) + add\_ent}
\DoxyCodeLine{2596 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2597 }
\DoxyCodeLine{2598     \textcolor{keyword}{call }call\_tracer\_column\_fns(hold, h, eatr, ebtr, fluxes, hml, dt, g, gv, us, tv, \&}
\DoxyCodeLine{2599                                 cs\%optics, cs\%tracer\_flow\_CSp, cs\%debug)}
\DoxyCodeLine{2600 }
\DoxyCodeLine{2601   \textcolor{keywordflow}{else}}
\DoxyCodeLine{2602     \textcolor{keyword}{call }call\_tracer\_column\_fns(hold, h, ea, eb, fluxes, hml, dt, g, gv, us, tv, \&}
\DoxyCodeLine{2603                                 cs\%optics, cs\%tracer\_flow\_CSp, cs\%debug)}
\DoxyCodeLine{2604 }
\DoxyCodeLine{2605 \textcolor{keywordflow}{  endif}  \textcolor{comment}{! (CS\%mix\_boundary\_tracers)}}
\DoxyCodeLine{2606 }
\DoxyCodeLine{2607   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_tracers)}
\DoxyCodeLine{2608 }
\DoxyCodeLine{2609   \textcolor{comment}{! sponges}}
\DoxyCodeLine{2610   \textcolor{keywordflow}{if} (cs\%use\_sponge) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2611     \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_sponge)}
\DoxyCodeLine{2612     \textcolor{comment}{! Layer mode sponge}}
\DoxyCodeLine{2613     \textcolor{keywordflow}{if} (cs\%bulkmixedlayer .and. \textcolor{keyword}{associated}(tv\%eqn\_of\_state)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2614       \textcolor{keywordflow}{do} i=is,ie ; p\_ref\_cv(i) = tv\%P\_Ref ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2615       eosdom(:) = eos\_domain(g\%HI)}
\DoxyCodeLine{2616       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{2617       \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{2618         \textcolor{keyword}{call }calculate\_density(tv\%T(:,j,1), tv\%S(:,j,1), p\_ref\_cv, rcv\_ml(:,j), \&}
\DoxyCodeLine{2619                                tv\%eqn\_of\_state, eosdom)}
\DoxyCodeLine{2620 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{2621       \textcolor{keyword}{call }apply\_sponge(h, dt, g, gv, us, ea, eb, cs\%sponge\_CSp, rcv\_ml)}
\DoxyCodeLine{2622     \textcolor{keywordflow}{else}}
\DoxyCodeLine{2623       \textcolor{keyword}{call }apply\_sponge(h, dt, g, gv, us, ea, eb, cs\%sponge\_CSp)}
\DoxyCodeLine{2624 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2625     \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_sponge)}
\DoxyCodeLine{2626     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2627       \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"apply\_sponge "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2628       \textcolor{keyword}{call }mom\_thermovar\_chksum(\textcolor{stringliteral}{"apply\_sponge "}, tv, g)}
\DoxyCodeLine{2629 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2630 \textcolor{keywordflow}{  endif} \textcolor{comment}{! CS\%use\_sponge}}
\DoxyCodeLine{2631 }
\DoxyCodeLine{2632 \textcolor{comment}{!   Save the diapycnal mass fluxes as a diagnostic field.}}
\DoxyCodeLine{2633   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cdp\%diapyc\_vel)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2634     \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{2635     \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{2636       \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2637         cdp\%diapyc\_vel(i,j,k) = us\%s\_to\_T*idt * (ea(i,j,k) -\/ eb(i,j,k-\/1))}
\DoxyCodeLine{2638 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2639       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2640         cdp\%diapyc\_vel(i,j,1) = 0.0}
\DoxyCodeLine{2641         cdp\%diapyc\_vel(i,j,nz+1) = 0.0}
\DoxyCodeLine{2642 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{2643 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2644 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2645 }
\DoxyCodeLine{2646 \textcolor{comment}{! For momentum, it is only the net flux that homogenizes within}}
\DoxyCodeLine{2647 \textcolor{comment}{! the mixed layer.  Vertical viscosity that is proportional to the}}
\DoxyCodeLine{2648 \textcolor{comment}{! mixed layer turbulence is applied elsewhere.}}
\DoxyCodeLine{2649   \textcolor{keywordflow}{if} (cs\%bulkmixedlayer) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2650     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2651       \textcolor{keyword}{call }hchksum(ea, \textcolor{stringliteral}{"before net flux rearrangement ea"}, g\%HI, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2652       \textcolor{keyword}{call }hchksum(eb, \textcolor{stringliteral}{"before net flux rearrangement eb"}, g\%HI, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2653 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2654     \textcolor{comment}{!\$OMP parallel do default(shared) private(net\_ent)}}
\DoxyCodeLine{2655     \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{2656       \textcolor{keywordflow}{do} k=2,gv\%nkml ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2657         net\_ent = ea(i,j,k) -\/ eb(i,j,k-\/1)}
\DoxyCodeLine{2658         ea(i,j,k) = max(net\_ent, 0.0)}
\DoxyCodeLine{2659         eb(i,j,k-\/1) = max(-\/net\_ent, 0.0)}
\DoxyCodeLine{2660 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2661 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2662     \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2663       \textcolor{keyword}{call }hchksum(ea, \textcolor{stringliteral}{"after net flux rearrangement ea"}, g\%HI, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2664       \textcolor{keyword}{call }hchksum(eb, \textcolor{stringliteral}{"after net flux rearrangement eb"}, g\%HI, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2665 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2666 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2667 }
\DoxyCodeLine{2668 \textcolor{comment}{! Initialize halo regions of ea, eb, and hold to default values.}}
\DoxyCodeLine{2669   \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{2670   \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{2671     \textcolor{keywordflow}{do} i=is-\/1,ie+1}
\DoxyCodeLine{2672       hold(i,js-\/1,k) = gv\%Angstrom\_H ; ea(i,js-\/1,k) = 0.0 ; eb(i,js-\/1,k) = 0.0}
\DoxyCodeLine{2673       hold(i,je+1,k) = gv\%Angstrom\_H ; ea(i,je+1,k) = 0.0 ; eb(i,je+1,k) = 0.0}
\DoxyCodeLine{2674 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2675     \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{2676       hold(is-\/1,j,k) = gv\%Angstrom\_H ; ea(is-\/1,j,k) = 0.0 ; eb(is-\/1,j,k) = 0.0}
\DoxyCodeLine{2677       hold(ie+1,j,k) = gv\%Angstrom\_H ; ea(ie+1,j,k) = 0.0 ; eb(ie+1,j,k) = 0.0}
\DoxyCodeLine{2678 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2679 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{2680 }
\DoxyCodeLine{2681   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_pass)}
\DoxyCodeLine{2682   \textcolor{keywordflow}{if} (g\%symmetric) \textcolor{keywordflow}{then} ; dir\_flag = to\_all+omit\_corners}
\DoxyCodeLine{2683   \textcolor{keywordflow}{else} ; dir\_flag = to\_west+to\_south+omit\_corners ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{2684   \textcolor{keyword}{call }create\_group\_pass(cs\%pass\_hold\_eb\_ea, hold, g\%Domain, dir\_flag, halo=1)}
\DoxyCodeLine{2685   \textcolor{keyword}{call }create\_group\_pass(cs\%pass\_hold\_eb\_ea, eb, g\%Domain, dir\_flag, halo=1)}
\DoxyCodeLine{2686   \textcolor{keyword}{call }create\_group\_pass(cs\%pass\_hold\_eb\_ea, ea, g\%Domain, dir\_flag, halo=1)}
\DoxyCodeLine{2687   \textcolor{keyword}{call }do\_group\_pass(cs\%pass\_hold\_eb\_ea, g\%Domain)}
\DoxyCodeLine{2688   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_pass)}
\DoxyCodeLine{2689 }
\DoxyCodeLine{2690   \textcolor{comment}{!  Use a tridiagonal solver to determine effect of the diapycnal}}
\DoxyCodeLine{2691   \textcolor{comment}{!  advection on velocity field. It is assumed that water leaves}}
\DoxyCodeLine{2692   \textcolor{comment}{!  or enters the ocean with the surface velocity.}}
\DoxyCodeLine{2693   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2694     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"before u/v tridiag "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2695     \textcolor{keyword}{call }hchksum(ea, \textcolor{stringliteral}{"before u/v tridiag ea"}, g\%HI, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2696     \textcolor{keyword}{call }hchksum(eb, \textcolor{stringliteral}{"before u/v tridiag eb"}, g\%HI, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2697     \textcolor{keyword}{call }hchksum(hold, \textcolor{stringliteral}{"before u/v tridiag hold"}, g\%HI, scale=gv\%H\_to\_m)}
\DoxyCodeLine{2698 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2699   \textcolor{keyword}{call }cpu\_clock\_begin(id\_clock\_tridiag)}
\DoxyCodeLine{2700   idt\_accel = 1.0 / dt}
\DoxyCodeLine{2701   \textcolor{comment}{!\$OMP parallel do default(shared) private(hval,b1,d1,c1,eaval)}}
\DoxyCodeLine{2702   \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{2703     \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{2704       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%du\_dt\_dia)) adp\%du\_dt\_dia(i,j,1) = u(i,j,1)}
\DoxyCodeLine{2705       hval = (hold(i,j,1) + hold(i+1,j,1)) + (ea(i,j,1) + ea(i+1,j,1)) + h\_neglect}
\DoxyCodeLine{2706       b1(i) = 1.0 / (hval + (eb(i,j,1) + eb(i+1,j,1)))}
\DoxyCodeLine{2707       d1(i) = hval * b1(i)}
\DoxyCodeLine{2708       u(i,j,1) = b1(i) * (hval * u(i,j,1))}
\DoxyCodeLine{2709 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2710     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{2711       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%du\_dt\_dia)) adp\%du\_dt\_dia(i,j,k) = u(i,j,k)}
\DoxyCodeLine{2712       c1(i,k) = (eb(i,j,k-\/1)+eb(i+1,j,k-\/1)) * b1(i)}
\DoxyCodeLine{2713       eaval = ea(i,j,k) + ea(i+1,j,k)}
\DoxyCodeLine{2714       hval = hold(i,j,k) + hold(i+1,j,k) + h\_neglect}
\DoxyCodeLine{2715       b1(i) = 1.0 / ((eb(i,j,k) + eb(i+1,j,k)) + (hval + d1(i)*eaval))}
\DoxyCodeLine{2716       d1(i) = (hval + d1(i)*eaval) * b1(i)}
\DoxyCodeLine{2717       u(i,j,k) = (hval*u(i,j,k) + eaval*u(i,j,k-\/1))*b1(i)}
\DoxyCodeLine{2718 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2719     \textcolor{keywordflow}{do} k=nz-\/1,1,-\/1 ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{2720       u(i,j,k) = u(i,j,k) + c1(i,k+1)*u(i,j,k+1)}
\DoxyCodeLine{2721       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%du\_dt\_dia)) \&}
\DoxyCodeLine{2722         adp\%du\_dt\_dia(i,j,k) = (u(i,j,k) -\/ adp\%du\_dt\_dia(i,j,k)) * idt\_accel}
\DoxyCodeLine{2723 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2724     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%du\_dt\_dia)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2725       \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{2726         adp\%du\_dt\_dia(i,j,nz) = (u(i,j,nz)-\/adp\%du\_dt\_dia(i,j,nz)) * idt\_accel}
\DoxyCodeLine{2727 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{2728 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2729 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{2730   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2731     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"aft 1st loop tridiag "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2732 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2733   \textcolor{comment}{!\$OMP parallel do default(shared) private(hval,b1,d1,c1,eaval)}}
\DoxyCodeLine{2734   \textcolor{keywordflow}{do} j=jsq,jeq}
\DoxyCodeLine{2735     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2736       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%dv\_dt\_dia)) adp\%dv\_dt\_dia(i,j,1) = v(i,j,1)}
\DoxyCodeLine{2737       hval = (hold(i,j,1) + hold(i,j+1,1)) + (ea(i,j,1) + ea(i,j+1,1)) + h\_neglect}
\DoxyCodeLine{2738       b1(i) = 1.0 / (hval + (eb(i,j,1) + eb(i,j+1,1)))}
\DoxyCodeLine{2739       d1(i) = hval * b1(i)}
\DoxyCodeLine{2740       v(i,j,1) = b1(i) * (hval * v(i,j,1))}
\DoxyCodeLine{2741 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{2742     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2743       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%dv\_dt\_dia)) adp\%dv\_dt\_dia(i,j,k) = v(i,j,k)}
\DoxyCodeLine{2744       c1(i,k) = (eb(i,j,k-\/1)+eb(i,j+1,k-\/1)) * b1(i)}
\DoxyCodeLine{2745       eaval = ea(i,j,k) + ea(i,j+1,k)}
\DoxyCodeLine{2746       hval = hold(i,j,k) + hold(i,j+1,k) + h\_neglect}
\DoxyCodeLine{2747       b1(i) = 1.0 / ((eb(i,j,k) + eb(i,j+1,k)) + (hval + d1(i)*eaval))}
\DoxyCodeLine{2748       d1(i) = (hval + d1(i)*eaval) * b1(i)}
\DoxyCodeLine{2749       v(i,j,k) = (hval*v(i,j,k) + eaval*v(i,j,k-\/1))*b1(i)}
\DoxyCodeLine{2750 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2751     \textcolor{keywordflow}{do} k=nz-\/1,1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2752       v(i,j,k) = v(i,j,k) + c1(i,k+1)*v(i,j,k+1)}
\DoxyCodeLine{2753       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%dv\_dt\_dia)) \&}
\DoxyCodeLine{2754         adp\%dv\_dt\_dia(i,j,k) = (v(i,j,k) -\/ adp\%dv\_dt\_dia(i,j,k)) * idt\_accel}
\DoxyCodeLine{2755 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2756     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%dv\_dt\_dia)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2757       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2758         adp\%dv\_dt\_dia(i,j,nz) = (v(i,j,nz)-\/adp\%dv\_dt\_dia(i,j,nz)) * idt\_accel}
\DoxyCodeLine{2759 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{2760 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{2761 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{2762   \textcolor{keyword}{call }cpu\_clock\_end(id\_clock\_tridiag)}
\DoxyCodeLine{2763   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2764     \textcolor{keyword}{call }mom\_state\_chksum(\textcolor{stringliteral}{"after u/v tridiag "}, u, v, h, g, gv, us, haloshift=0)}
\DoxyCodeLine{2765 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2766 }
\DoxyCodeLine{2767   \textcolor{keyword}{call }disable\_averaging(cs\%diag)}
\DoxyCodeLine{2768   \textcolor{comment}{! Diagnose the diapycnal diffusivities and other related quantities.}}
\DoxyCodeLine{2769   \textcolor{keyword}{call }enable\_averages(dt, time\_end, cs\%diag)}
\DoxyCodeLine{2770 }
\DoxyCodeLine{2771   \textcolor{keywordflow}{if} (cs\%id\_Kd\_interface > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_interface, kd\_int,  cs\%diag)}
\DoxyCodeLine{2772   \textcolor{keywordflow}{if} (cs\%id\_Kd\_heat      > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_heat,      kd\_heat, cs\%diag)}
\DoxyCodeLine{2773   \textcolor{keywordflow}{if} (cs\%id\_Kd\_salt      > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_salt,      kd\_salt, cs\%diag)}
\DoxyCodeLine{2774   \textcolor{keywordflow}{if} (cs\%id\_Kd\_ePBL      > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_ePBL,      kd\_epbl, cs\%diag)}
\DoxyCodeLine{2775 }
\DoxyCodeLine{2776   \textcolor{keywordflow}{if} (cs\%id\_ea       > 0) \textcolor{keyword}{call }post\_data(cs\%id\_ea,       ea, cs\%diag)}
\DoxyCodeLine{2777   \textcolor{keywordflow}{if} (cs\%id\_eb       > 0) \textcolor{keyword}{call }post\_data(cs\%id\_eb,       eb, cs\%diag)}
\DoxyCodeLine{2778 }
\DoxyCodeLine{2779   \textcolor{keywordflow}{if} (cs\%id\_dudt\_dia > 0) \textcolor{keyword}{call }post\_data(cs\%id\_dudt\_dia, adp\%du\_dt\_dia,  cs\%diag)}
\DoxyCodeLine{2780   \textcolor{keywordflow}{if} (cs\%id\_dvdt\_dia > 0) \textcolor{keyword}{call }post\_data(cs\%id\_dvdt\_dia, adp\%dv\_dt\_dia,  cs\%diag)}
\DoxyCodeLine{2781   \textcolor{keywordflow}{if} (cs\%id\_wd       > 0) \textcolor{keyword}{call }post\_data(cs\%id\_wd,       cdp\%diapyc\_vel, cs\%diag)}
\DoxyCodeLine{2782 }
\DoxyCodeLine{2783   \textcolor{keywordflow}{if} (cs\%id\_Tdif > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Tdif, tdif\_flx, cs\%diag)}
\DoxyCodeLine{2784   \textcolor{keywordflow}{if} (cs\%id\_Tadv > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Tadv, tadv\_flx, cs\%diag)}
\DoxyCodeLine{2785   \textcolor{keywordflow}{if} (cs\%id\_Sdif > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Sdif, sdif\_flx, cs\%diag)}
\DoxyCodeLine{2786   \textcolor{keywordflow}{if} (cs\%id\_Sadv > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Sadv, sadv\_flx, cs\%diag)}
\DoxyCodeLine{2787 }
\DoxyCodeLine{2788   \textcolor{comment}{!! Diagnostics for terms multiplied by fractional thicknesses}}
\DoxyCodeLine{2789   \textcolor{keywordflow}{if} (cs\%id\_hf\_dudt\_dia\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2790     \textcolor{keyword}{allocate}(hf\_dudt\_dia\_2d(g\%IsdB:g\%IedB,g\%jsd:g\%jed))}
\DoxyCodeLine{2791     hf\_dudt\_dia\_2d(:,:) = 0.0}
\DoxyCodeLine{2792     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{2793       hf\_dudt\_dia\_2d(i,j) = hf\_dudt\_dia\_2d(i,j) + adp\%du\_dt\_dia(i,j,k) * adp\%diag\_hfrac\_u(i,j,k)}
\DoxyCodeLine{2794 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2795     \textcolor{keyword}{call }post\_data(cs\%id\_hf\_dudt\_dia\_2d, hf\_dudt\_dia\_2d, cs\%diag)}
\DoxyCodeLine{2796     \textcolor{keyword}{deallocate}(hf\_dudt\_dia\_2d)}
\DoxyCodeLine{2797 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2798 }
\DoxyCodeLine{2799   \textcolor{keywordflow}{if} (cs\%id\_hf\_dvdt\_dia\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{2800     \textcolor{keyword}{allocate}(hf\_dvdt\_dia\_2d(g\%isd:g\%ied,g\%JsdB:g\%JedB))}
\DoxyCodeLine{2801     hf\_dvdt\_dia\_2d(:,:) = 0.0}
\DoxyCodeLine{2802     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=jsq,jeq ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{2803       hf\_dvdt\_dia\_2d(i,j) = hf\_dvdt\_dia\_2d(i,j) + adp\%dv\_dt\_dia(i,j,k) * adp\%diag\_hfrac\_v(i,j,k)}
\DoxyCodeLine{2804 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{2805     \textcolor{keyword}{call }post\_data(cs\%id\_hf\_dvdt\_dia\_2d, hf\_dvdt\_dia\_2d, cs\%diag)}
\DoxyCodeLine{2806     \textcolor{keyword}{deallocate}(hf\_dvdt\_dia\_2d)}
\DoxyCodeLine{2807 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{2808 }
\DoxyCodeLine{2809   \textcolor{keyword}{call }disable\_averaging(cs\%diag)}
\DoxyCodeLine{2810 }
\DoxyCodeLine{2811   \textcolor{keywordflow}{if} (showcalltree) \textcolor{keyword}{call }calltree\_leave(\textcolor{stringliteral}{"layered\_diabatic()"})}
\DoxyCodeLine{2812 }

\end{DoxyCode}
