\hypertarget{namespacemom__diabatic__driver}{}\section{mom\+\_\+diabatic\+\_\+driver Module Reference}
\label{namespacemom__diabatic__driver}\index{mom\+\_\+diabatic\+\_\+driver@{mom\+\_\+diabatic\+\_\+driver}}


\subsection{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}{}\subsection{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}\subsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \hyperlink{structmom__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}
\begin{DoxyCompactList}\small\item\em Control structure for this module. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \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 \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 \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 \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 \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 \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 \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 \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 \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 \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 \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 \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}
\subsection*{Variables}
\textbf{ }\par
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{namespacemom__diabatic__driver_ac0824d4ab23eb0cff642acd5b40f38cf}\label{namespacemom__diabatic__driver_ac0824d4ab23eb0cff642acd5b40f38cf}} 
integer \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 \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 \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 \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 \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 \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 \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 \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 \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 \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 \hyperlink{namespacemom__diabatic__driver_a6b41dac58aa5c518f1affcf4423a7fb4}{id\+\_\+clock\+\_\+kpp}
\begin{DoxyCompactList}\small\item\em clock ids \end{DoxyCompactList}\end{DoxyCompactItemize}



\subsection{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}}
\subsubsection{\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(\hyperlink{structmom__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}), pointer}]{CS }\end{DoxyParamCaption})}



Routine called for adiabatic physics. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in,out}  & {\em g} & ocean grid structure\\
\hline
\mbox{\tt in,out}  & {\em h} & thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em tv} & points to thermodynamic fields\\
\hline
\mbox{\tt in,out}  & {\em fluxes} & boundary fluxes\\
\hline
\mbox{\tt in}  & {\em dt} & time step \mbox{[}T $\sim$$>$ s\mbox{]}\\
\hline
\mbox{\tt in}  & {\em gv} & ocean vertical grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em cs} & module control structure \\
\hline
\end{DoxyParams}


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


\begin{DoxyCode}
2856   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(inout)} :: g\textcolor{comment}{      !< ocean grid structure}
2857   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, &
2858                            \textcolor{keywordtype}{intent(inout)} :: h\textcolor{comment}{      !< thickness [H ~> m or kg m-2]}
2859   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),   \textcolor{keywordtype}{intent(inout)} :: tv\textcolor{comment}{     !< points to thermodynamic fields}
2860   \textcolor{keywordtype}{type}(forcing),           \textcolor{keywordtype}{intent(inout)} :: fluxes\textcolor{comment}{ !< boundary fluxes}
2861   \textcolor{keywordtype}{real},                    \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{     !< time step [T ~> s]}
2862   \textcolor{keywordtype}{type}(verticalgrid\_type), \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{     !< ocean vertical grid structure}
2863   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{     !< A dimensional unit scaling type}
2864   \textcolor{keywordtype}{type}(diabatic\_cs),       \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{     !< module control structure}
2865 
2866   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))} :: zeros  \textcolor{comment}{! An array of zeros.}
2867 
2868   zeros(:,:,:) = 0.0
2869 
2870   \textcolor{keyword}{call }call\_tracer\_column\_fns(h, h, zeros, zeros, fluxes, zeros(:,:,1), dt, g, gv, us, tv, &
2871                               cs%optics, cs%tracer\_flow\_CSp, cs%debug)
2872 
\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}}
\subsubsection{\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(\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{\tt in}  & {\em time} & current model time\\
\hline
\mbox{\tt in}  & {\em g} & model grid structure\\
\hline
\mbox{\tt in}  & {\em param\+\_\+file} & the file to parse for parameter values\\
\hline
\mbox{\tt 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 3122 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


\begin{DoxyCode}
3122   \textcolor{keywordtype}{type}(time\_type),         \textcolor{keywordtype}{intent(in)}    :: time\textcolor{comment}{             !< current model time}
3123   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: g\textcolor{comment}{                !< model grid structure}
3124   \textcolor{keywordtype}{type}(param\_file\_type),   \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{       !< the file to parse for parameter values}
3125   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{             !< regulates diagnostic output}
3126   \textcolor{keywordtype}{type}(diabatic\_cs),       \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{               !< module control structure}
3127   \textcolor{keywordtype}{type}(tracer\_flow\_control\_cs), \textcolor{keywordtype}{pointer}  :: tracer\_flow\_csp\textcolor{comment}{  !< pointer to control structure of the}
3128 \textcolor{comment}{                                                             !! tracer flow control module}
3129 
3130 \textcolor{comment}{! This "include" declares and sets the variable "version".}
3131 \textcolor{preprocessor}{#include "version\_variable.h"}
3132 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_diabatic\_driver"} \textcolor{comment}{! This module's name.}
3133 
3134   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}
3135     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"adiabatic\_driver\_init called with an "}// &
3136                             \textcolor{stringliteral}{"associated control structure."})
3137     \textcolor{keywordflow}{return}
3138   \textcolor{keywordflow}{else} ; \textcolor{keyword}{allocate}(cs) ;\textcolor{keywordflow}{ endif}
3139 
3140   cs%diag => diag
3141   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tracer\_flow\_csp)) cs%tracer\_flow\_CSp => tracer\_flow\_csp
3142 
3143 \textcolor{comment}{! Set default, read and log parameters}
3144   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, &
3145                    \textcolor{stringliteral}{"The following parameters are used for diabatic processes."})
3146 
\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}}
\subsubsection{\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(\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{\tt in,out}  & {\em g} & ocean grid structure\\
\hline
\mbox{\tt in}  & {\em gv} & ocean vertical grid structure\\
\hline
\mbox{\tt in,out}  & {\em u} & zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em v} & meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em h} & thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt 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{\tt in,out}  & {\em fluxes} & points to forcing fields unused fields have N\+U\+LL ptrs\\
\hline
\mbox{\tt in,out}  & {\em visc} & vertical viscosities, B\+BL properies, and\\
\hline
\mbox{\tt in,out}  & {\em adp} & related points to accelerations in momentum equations, to enable the later derived diagnostics, like energy budgets\\
\hline
\mbox{\tt in,out}  & {\em cdp} & points to terms in continuity equations\\
\hline
\mbox{\tt in}  & {\em dt} & time increment \mbox{[}T $\sim$$>$ s\mbox{]}\\
\hline
\mbox{\tt in}  & {\em time\+\_\+end} & Time at the end of the interval\\
\hline
\mbox{\tt 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 262 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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


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


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


\begin{DoxyCode}
3750   \textcolor{keywordtype}{type}(diabatic\_cs), \textcolor{keywordtype}{pointer} :: cs\textcolor{comment}{    !< module control structure}
3751 
3752   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{return}
3753 
3754   \textcolor{keyword}{call }diabatic\_aux\_end(cs%diabatic\_aux\_CSp)
3755 
3756   \textcolor{keyword}{call }entrain\_diffusive\_end(cs%entrain\_diffusive\_CSp)
3757   \textcolor{keyword}{call }set\_diffusivity\_end(cs%set\_diff\_CSp)
3758 
3759   \textcolor{keywordflow}{if} (cs%useKPP) \textcolor{keywordflow}{then}
3760     \textcolor{keyword}{deallocate}( cs%KPP\_buoy\_flux )
3761     \textcolor{keyword}{deallocate}( cs%KPP\_temp\_flux )
3762     \textcolor{keyword}{deallocate}( cs%KPP\_salt\_flux )
3763     \textcolor{keyword}{deallocate}( cs%KPP\_NLTheat )
3764     \textcolor{keyword}{deallocate}( cs%KPP\_NLTscalar )
3765     \textcolor{keyword}{call }kpp\_end(cs%KPP\_CSp)
3766 \textcolor{keywordflow}{  endif}
3767 
3768   \textcolor{keywordflow}{if} (cs%use\_CVMix\_conv) \textcolor{keyword}{call }cvmix\_conv\_end(cs%CVMix\_conv\_csp)
3769 
3770   \textcolor{keywordflow}{if} (cs%use\_energetic\_PBL) &
3771     \textcolor{keyword}{call }energetic\_pbl\_end(cs%energetic\_PBL\_CSp)
3772   \textcolor{keywordflow}{if} (cs%debug\_energy\_req) &
3773     \textcolor{keyword}{call }diapyc\_energy\_req\_end(cs%diapyc\_en\_rec\_CSp)
3774 
3775   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs%optics)) \textcolor{keywordflow}{then}
3776     \textcolor{keyword}{call }opacity\_end(cs%opacity\_CSp, cs%optics)
3777     \textcolor{keyword}{deallocate}(cs%optics)
3778 \textcolor{keywordflow}{  endif}
3779 
3780   \textcolor{comment}{! GMM, the following is commented out because arrays in}
3781   \textcolor{comment}{! CS%diag\_grids\_prev are neither pointers or allocatables}
3782   \textcolor{comment}{! and, therefore, cannot be deallocated.}
3783 
3784   \textcolor{comment}{!call diag\_grid\_storage\_end(CS%diag\_grids\_prev)}
3785 
3786   \textcolor{keyword}{deallocate}(cs)
3787 
\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}}
\subsubsection{\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(\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{\tt in,out}  & {\em g} & model grid structure\\
\hline
\mbox{\tt in}  & {\em gv} & model vertical grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em param\+\_\+file} & file to parse for parameter values\\
\hline
\mbox{\tt in}  & {\em usealealgorithm} & logical for whether to use A\+LE remapping\\
\hline
\mbox{\tt in,out}  & {\em diag} & structure to regulate diagnostic output\\
\hline
\mbox{\tt in,out}  & {\em adp} & pointers to accelerations in momentum equations, to enable diagnostics, like energy budgets\\
\hline
\mbox{\tt 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 3154 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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


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


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


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


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


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


\begin{DoxyCode}
3070   \textcolor{keywordtype}{type}(ocean\_grid\_type),                    \textcolor{keywordtype}{intent(in)} :: g\textcolor{comment}{        !< ocean grid structure}
3071   \textcolor{keywordtype}{type}(verticalgrid\_type),                  \textcolor{keywordtype}{intent(in)} :: gv\textcolor{comment}{       !< ocean vertical grid structure}
3072   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),                    \textcolor{keywordtype}{intent(in)} :: tv\textcolor{comment}{       !< points to updated thermodynamic
       fields}
3073   \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]}
3074   \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]}
3075   \textcolor{keywordtype}{real},                                     \textcolor{keywordtype}{intent(in)} :: dt\textcolor{comment}{       !< time step [T ~> s]}
3076   \textcolor{keywordtype}{type}(unit\_scale\_type),                    \textcolor{keywordtype}{intent(in)} :: us\textcolor{comment}{       !< A dimensional unit scaling type}
3077   \textcolor{keywordtype}{type}(diabatic\_cs),                        \textcolor{keywordtype}{pointer}    :: cs\textcolor{comment}{       !< module control structure}
3078 
3079   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))}         :: work\_2d
3080   \textcolor{keywordtype}{real}    :: idt \textcolor{comment}{! The inverse of the timestep [T-1 ~> s-1]}
3081   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, nz
3082 
3083   is  = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec ; nz = g%ke
3084   idt = 0.0 ; \textcolor{keywordflow}{if} (dt > 0.0) idt = 1. / dt
3085 
3086   \textcolor{comment}{! temperature tendency}
3087   \textcolor{keywordflow}{if} (cs%id\_frazil\_temp\_tend > 0) \textcolor{keywordflow}{then}
3088     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
3089       cs%frazil\_temp\_diag(i,j,k) = idt * (tv%T(i,j,k)-temp\_old(i,j,k))
3090 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
3091     \textcolor{keyword}{call }post\_data(cs%id\_frazil\_temp\_tend, cs%frazil\_temp\_diag(:,:,:), cs%diag)
3092 \textcolor{keywordflow}{  endif}
3093 
3094   \textcolor{comment}{! heat tendency}
3095   \textcolor{keywordflow}{if} (cs%id\_frazil\_heat\_tend > 0 .or. cs%id\_frazil\_heat\_tend\_2d > 0) \textcolor{keywordflow}{then}
3096     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
3097       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))
3098 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}
3099     \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)
3100 
3101     \textcolor{comment}{! As a consistency check, we must have}
3102     \textcolor{comment}{! FRAZIL\_HEAT\_TENDENCY\_2d = HFSIFRAZIL}
3103     \textcolor{keywordflow}{if} (cs%id\_frazil\_heat\_tend\_2d > 0) \textcolor{keywordflow}{then}
3104       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
3105         work\_2d(i,j) = 0.0
3106         \textcolor{keywordflow}{do} k=1,nz
3107           work\_2d(i,j) = work\_2d(i,j) + cs%frazil\_heat\_diag(i,j,k)
3108 \textcolor{keywordflow}{        enddo}
3109 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
3110       \textcolor{keyword}{call }post\_data(cs%id\_frazil\_heat\_tend\_2d, work\_2d, cs%diag)
3111 \textcolor{keywordflow}{    endif}
3112 \textcolor{keywordflow}{  endif}
3113 
\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}}
\subsubsection{\texorpdfstring{extract\+\_\+diabatic\+\_\+member()}{extract\_diabatic\_member()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+diabatic\+\_\+driver\+::extract\+\_\+diabatic\+\_\+member (\begin{DoxyParamCaption}\item[{type(\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{\tt 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{\tt out}  & {\em evap\+\_\+cfl\+\_\+limit} & The largest fraction of a layer that can be evaporated in one time-\/step \mbox{[}nondim\mbox{]}.\\
\hline
\mbox{\tt 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{\tt out}  & {\em diabatic\+\_\+halo} & The halo size where the diabatic algorithms assume thermodynamics properties are valid. \\
\hline
\end{DoxyParams}


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


\begin{DoxyCode}
2826   \textcolor{keywordtype}{type}(diabatic\_cs), \textcolor{keywordtype}{intent(in   )}           :: cs\textcolor{comment}{ !< module control structure}
2827   \textcolor{comment}{! All output arguments are optional}
2828   \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}
2829   \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}
2830   \textcolor{keywordtype}{type}(kpp\_cs),      \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}       :: kpp\_csp\textcolor{comment}{     !< A pointer to be set to the KPP CS}
2831   \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}
2832   \textcolor{keywordtype}{real},              \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(  out)} :: evap\_cfl\_limit\textcolor{comment}{ !<The largest fraction of a layer that can
       be}
2833 \textcolor{comment}{                                                            !! evaporated in one time-step [nondim].}
2834   \textcolor{keywordtype}{real},              \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(  out)} :: minimum\_forcing\_depth\textcolor{comment}{ !< The smallest depth over which heat}
2835 \textcolor{comment}{                                                            !! and freshwater fluxes are applied [H ~> m or
       kg m-2].}
2836   \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}
2837 \textcolor{comment}{                                                            !! control structure}
2838   \textcolor{keywordtype}{integer},           \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(  out)} :: diabatic\_halo\textcolor{comment}{ !< The halo size where the diabatic
       algorithms}
2839 \textcolor{comment}{                                                            !! assume thermodynamics properties are valid.}
2840 
2841   \textcolor{comment}{! Pointers to control structures}
2842   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(opacity\_csp))       opacity\_csp => cs%opacity\_CSp
2843   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(optics\_csp))        optics\_csp  => cs%optics
2844   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kpp\_csp))           kpp\_csp     => cs%KPP\_CSp
2845   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(energetic\_pbl\_csp)) energetic\_pbl\_csp => cs%energetic\_PBL\_CSp
2846 
2847   \textcolor{comment}{! Constants within diabatic\_CS}
2848   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(evap\_cfl\_limit))        evap\_cfl\_limit = cs%evap\_CFL\_limit
2849   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(minimum\_forcing\_depth)) minimum\_forcing\_depth = cs%minimum\_forcing\_depth
2850   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(diabatic\_halo)) diabatic\_halo = cs%halo\_TS\_diff
2851 
\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}}
\subsubsection{\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(\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{\tt in,out}  & {\em g} & ocean grid structure\\
\hline
\mbox{\tt in}  & {\em gv} & ocean vertical grid structure\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in,out}  & {\em u} & zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em v} & meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}\\
\hline
\mbox{\tt in,out}  & {\em h} & thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}\\
\hline
\mbox{\tt 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{\tt in,out}  & {\em fluxes} & points to forcing fields unused fields have N\+U\+LL ptrs\\
\hline
\mbox{\tt in,out}  & {\em visc} & vertical viscosities, B\+BL properies, and\\
\hline
\mbox{\tt in,out}  & {\em adp} & related points to accelerations in momentum equations, to enable the later derived diagnostics, like energy budgets\\
\hline
\mbox{\tt in,out}  & {\em cdp} & points to terms in continuity equations\\
\hline
\mbox{\tt in}  & {\em dt} & time increment \mbox{[}T $\sim$$>$ s\mbox{]}\\
\hline
\mbox{\tt 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 1857 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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