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



\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(\mbox{\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 2849 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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



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


\begin{DoxyParams}[1]{Parameters}
\mbox{\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 3115 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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



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


\begin{DoxyParams}[1]{Parameters}
\mbox{\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 263 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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



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


\begin{DoxyParams}[1]{Parameters}
\mbox{\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 1173 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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



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


\begin{DoxyParams}[1]{Parameters}
\mbox{\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 453 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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



Routine to close the diabatic driver module. 


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


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


\begin{DoxyCode}
3733   \textcolor{keywordtype}{type}(diabatic\_CS), \textcolor{keywordtype}{pointer} :: CS\textcolor{comment}{    !< module control structure}
3734 
3735   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{return}
3736 
3737   \textcolor{keyword}{call }diabatic\_aux\_end(cs%diabatic\_aux\_CSp)
3738 
3739   \textcolor{keyword}{call }entrain\_diffusive\_end(cs%entrain\_diffusive\_CSp)
3740   \textcolor{keyword}{call }set\_diffusivity\_end(cs%set\_diff\_CSp)
3741 
3742   \textcolor{keywordflow}{if} (cs%useKPP) \textcolor{keywordflow}{then}
3743     \textcolor{keyword}{deallocate}( cs%KPP\_buoy\_flux )
3744     \textcolor{keyword}{deallocate}( cs%KPP\_temp\_flux )
3745     \textcolor{keyword}{deallocate}( cs%KPP\_salt\_flux )
3746     \textcolor{keyword}{deallocate}( cs%KPP\_NLTheat )
3747     \textcolor{keyword}{deallocate}( cs%KPP\_NLTscalar )
3748     \textcolor{keyword}{call }kpp\_end(cs%KPP\_CSp)
3749 \textcolor{keywordflow}{  endif}
3750 
3751   \textcolor{keywordflow}{if} (cs%use\_CVMix\_conv) \textcolor{keyword}{call }cvmix\_conv\_end(cs%CVMix\_conv\_csp)
3752 
3753   \textcolor{keywordflow}{if} (cs%use\_energetic\_PBL) &
3754     \textcolor{keyword}{call }energetic\_pbl\_end(cs%energetic\_PBL\_CSp)
3755   \textcolor{keywordflow}{if} (cs%debug\_energy\_req) &
3756     \textcolor{keyword}{call }diapyc\_energy\_req\_end(cs%diapyc\_en\_rec\_CSp)
3757 
3758   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs%optics)) \textcolor{keywordflow}{then}
3759     \textcolor{keyword}{call }opacity\_end(cs%opacity\_CSp, cs%optics)
3760     \textcolor{keyword}{deallocate}(cs%optics)
3761 \textcolor{keywordflow}{  endif}
3762 
3763   \textcolor{comment}{! GMM, the following is commented out because arrays in}
3764   \textcolor{comment}{! CS%diag\_grids\_prev are neither pointers or allocatables}
3765   \textcolor{comment}{! and, therefore, cannot be deallocated.}
3766 
3767   \textcolor{comment}{!call diag\_grid\_storage\_end(CS%diag\_grids\_prev)}
3768 
3769   \textcolor{keyword}{deallocate}(cs)
3770 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__diabatic__driver_a51d273bae7e5d2217fa5498620532888}\label{namespacemom__diabatic__driver_a51d273bae7e5d2217fa5498620532888}} 
\index{mom\+\_\+diabatic\+\_\+driver@{mom\+\_\+diabatic\+\_\+driver}!diabatic\+\_\+driver\+\_\+init@{diabatic\+\_\+driver\+\_\+init}}
\index{diabatic\+\_\+driver\+\_\+init@{diabatic\+\_\+driver\+\_\+init}!mom\+\_\+diabatic\+\_\+driver@{mom\+\_\+diabatic\+\_\+driver}}
\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(\mbox{\hyperlink{structmom__diabatic__driver_1_1diabatic__cs}{diabatic\+\_\+cs}}), pointer}]{CS,  }\item[{type(tracer\+\_\+flow\+\_\+control\+\_\+cs), pointer}]{tracer\+\_\+flow\+\_\+\+C\+Sp,  }\item[{type(sponge\+\_\+cs), pointer}]{sponge\+\_\+\+C\+Sp,  }\item[{type(ale\+\_\+sponge\+\_\+cs), pointer}]{A\+L\+E\+\_\+sponge\+\_\+\+C\+Sp }\end{DoxyParamCaption})}



This routine initializes the diabatic driver module. 


\begin{DoxyParams}[1]{Parameters}
 & {\em time} & model time\\
\hline
\mbox{\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 3147 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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



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


\begin{DoxyParams}[1]{Parameters}
\mbox{\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 2966 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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



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


\begin{DoxyParams}[1]{Parameters}
\mbox{\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 2873 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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



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


\begin{DoxyParams}[1]{Parameters}
\mbox{\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 3063 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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



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


\begin{DoxyParams}[1]{Parameters}
\mbox{\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 2819 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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



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


\begin{DoxyParams}[1]{Parameters}
\mbox{\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 1855 of file M\+O\+M\+\_\+diabatic\+\_\+driver.\+F90.


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