\hypertarget{namespacemom__meke}{}\section{mom\+\_\+meke Module Reference}
\label{namespacemom__meke}\index{mom\+\_\+meke@{mom\+\_\+meke}}


\subsection{Detailed Description}
Implements the Mesoscale Eddy Kinetic Energy framework with topographic beta effect included in computing beta in Rhines scale. 

\hypertarget{namespacemom__meke_section_MEKE}{}\subsection{The Mesoscale Eddy Kinetic Energy (\+M\+E\+K\+E) framework}\label{namespacemom__meke_section_MEKE}
The M\+E\+KE framework accounts for the mean potential energy removed by the first order closures used to parameterize mesoscale eddies. It requires closure at the second order, namely dissipation and transport of eddy energy.

Monitoring the sub-\/grid scale eddy energy budget provides a means to predict a sub-\/grid eddy-\/velocity scale which can be used in the lower order closures.\hypertarget{namespacemom__meke_section_MEKE_equations}{}\subsubsection{M\+E\+K\+E equations}\label{namespacemom__meke_section_MEKE_equations}
The eddy kinetic energy equation is\+: \[ \partial_{\tilde{t}} E = \overbrace{ \dot{E}_b + \gamma_\eta \dot{E}_\eta + \gamma_v \dot{E}_v }^\text{sources} - \overbrace{ ( \lambda + C_d | U_d | \gamma_b^2 ) E }^\text{local dissipation} + \overbrace{ \nabla \cdot ( ( \kappa_E + \gamma_M \kappa_M ) \nabla E - \kappa_4 \nabla^3 E ) }^\text{smoothing} \] where $ E $ is the eddy kinetic energy (variable {\ttfamily M\+E\+KE}) with units of m\textsuperscript{2}s\textsuperscript{-\/2}, and $\tilde{t} = a t$ is a scaled time. The non-\/dimensional factor $ a\geq 1 $ is used to accelerate towards equilibrium.

The M\+E\+KE equation is two-\/dimensional and obtained by depth averaging the the three-\/dimensional eddy energy equation. In the following expressions $ \left< \phi \right> = \frac{1}{H} \int^\eta_{-D} \phi \, dz $ maps three dimensional terms into the two-\/dimensional quantities needed.\hypertarget{namespacemom__meke_section_MEKE_source_terms}{}\paragraph{M\+E\+K\+E source terms}\label{namespacemom__meke_section_MEKE_source_terms}
The source term $ \dot{E}_b $ is a constant background source of energy intended to avoid the limit $E\rightarrow 0$.

The \char`\"{}\+G\+M\char`\"{} source term \[ \dot{E}_\eta = - \left< \overline{w^\prime b^\prime} \right> = \left< \kappa_h N^2S^2 \right> \approx \left< \kappa_h g\prime |\nabla_\sigma \eta|^2 \right>\] equals the mean potential energy removed by the Gent-\/\+Mc\+Williams closure, and is excluded/included in the M\+E\+KE budget by the efficiency parameter $ \gamma_\eta \in [0,1] $.

The \char`\"{}frictional\char`\"{} source term \[ \dot{E}_{v} = \left< \partial_i u_j \tau_{ij} \right> \] equals the mean kinetic energy removed by lateral viscous fluxes, and is excluded/included in the M\+E\+KE budget by the efficiency parameter $ \gamma_v \in [0,1] $.\hypertarget{namespacemom__meke_section_MEKE_dissipation_terms}{}\paragraph{M\+E\+K\+E dissipation terms}\label{namespacemom__meke_section_MEKE_dissipation_terms}
The local dissipation of $ E $ is parameterized through a linear damping, $\lambda$, and bottom drag, $ C_d | U_d | \gamma_b^2 $. The $ \gamma_b $ accounts for the weak projection of the column-\/mean eddy velocty to the bottom. In other words, the bottom velocity is estimated as $ \gamma_b U_e $. The bottom drag coefficient, $ C_d $ is the same as that used in the bottom friction in the mean model equations.

The bottom drag velocity scale, $ U_d $, has contributions from the resolved state and $ E $\+: \[ U_d = \sqrt{ U_b^2 + |u|^2_{z=-D} + |\gamma_b U_e|^2 } .\] where the eddy velocity scale, $ U_e $, is given by\+: \[ U_e = \sqrt{ 2 E } .\] $ U_b $ is a constant background bottom velocity scale and is typically not used (i.\+e. set to zero).

Following Jansen et al., 2015, the projection of eddy energy on to the bottom is given by the ratio of bottom energy to column mean energy\+: \[ \gamma_b^2 = \frac{E_b}{E} = \gamma_{d0} + \left( 1 + c_{b} \frac{L_d}{L_f} \right)^{-\frac{4}{5}} , \] \[ \gamma_b^2 \leftarrow \max{\left( \gamma_b^2, \gamma_{min}^2 \right)} . \]\hypertarget{namespacemom__meke_section_MEKE_smoothing}{}\subsubsection{M\+E\+K\+E smoothing terms}\label{namespacemom__meke_section_MEKE_smoothing}
$ E $ is laterally diffused by a diffusivity $ \kappa_E + \gamma_M \kappa_M $ where $ \kappa_E $ is a constant diffusivity and the term $ \gamma_M \kappa_M $ is a \char`\"{}self diffusion\char`\"{} using the diffusivity calculated in the section \hyperlink{namespacemom__meke_section_MEKE_diffusivity}{Diffusivity derived from M\+E\+KE}. $ \kappa_4 $ is a constant bi-\/harmonic diffusivity.\hypertarget{namespacemom__meke_section_MEKE_diffusivity}{}\subsubsection{Diffusivity derived from M\+E\+KE}\label{namespacemom__meke_section_MEKE_diffusivity}
The predicted eddy velocity scale, $ U_e $, can be combined with a mixing length scale to form a diffusivity. The primary use of a M\+E\+KE derived diffusivity is for use in thickness diffusion (module \hyperlink{namespacemom__thickness__diffuse}{mom\+\_\+thickness\+\_\+diffuse}) and optionally in along isopycnal mixing of tracers (module \hyperlink{namespacemom__tracer__hor__diff}{mom\+\_\+tracer\+\_\+hor\+\_\+diff}). The original form used (enabled with M\+E\+K\+E\+\_\+\+O\+L\+D\+\_\+\+L\+S\+C\+A\+LE=True)\+:

\[ \kappa_M = \gamma_\kappa \sqrt{ \gamma_t^2 U_e^2 A_\Delta } \]

where $ A_\Delta $ is the area of the grid cell. Following Jansen et al., 2015, we now use

\[ \kappa_M = \gamma_\kappa l_M \sqrt{ \gamma_t^2 U_e^2 } \]

where $ \gamma_\kappa \in [0,1] $ is a non-\/dimensional factor and, following Jansen et al., 2015, $\gamma_t^2$ is the ratio of barotropic eddy energy to column mean eddy energy given by \[ \gamma_t^2 = \frac{E_t}{E} = \left( 1 + c_{t} \frac{L_d}{L_f} \right)^{-\frac{1}{4}} , \] \[ \gamma_t^2 \leftarrow \max{\left( \gamma_t^2, \gamma_{min}^2 \right)} . \]

The length-\/scale is a configurable combination of multiple length scales\+:

\[ l_M = \left( \frac{\alpha_d}{L_d} + \frac{\alpha_f}{L_f} + \frac{\alpha_R}{L_R} + \frac{\alpha_e}{L_e} + \frac{\alpha_\Delta}{L_\Delta} + \frac{\delta[L_c]}{L_c} \right)^{-1} \]

where

\begin{eqnarray*} L_d & = & \sqrt{\frac{c_g^2}{f^2+2\beta c_g}} \sim \frac{ c_g }{f} \\\\ L_R & = & \sqrt{\frac{U_e}{\beta^*}} \\\\ L_e & = & \frac{U_e}{|S| N} \\\\ L_f & = & \frac{H}{c_d} \\\\ L_\Delta & = & \sqrt{A_\Delta} . \end{eqnarray*}

$L_c$ is a constant and $\delta[L_c]$ is the impulse function so that the term $\frac{\delta[L_c]}{L_c}$ evaluates to $\frac{1}{L_c}$ when $L_c$ is non-\/zero but is dropped if $L_c=0$.

$\beta^*$ is the effective $\beta$ that combines both the planetary vorticity gradient (i.\+e. $\beta=\nabla f$) and the topographic $\beta$ effect, with the latter weighed by a weighting constant, $c_\beta$, that varies from 0 to 1, so that $c_\beta=0$ means the topographic $\beta$ effect is ignored, while $c_\beta=1$ means it is fully considered. The new $\beta^*$ therefore takes the form of

\[ \beta^* = \sqrt{( \partial_xf - c_\beta\frac{f}{D}\partial_xD )^2 + ( \partial_yf - c_\beta\frac{f}{D}\partial_yD )^2} \] where $D$ is water column depth at T points.\hypertarget{namespacemom__meke_section_MEKE_viscosity}{}\subsubsection{Viscosity derived from M\+E\+KE}\label{namespacemom__meke_section_MEKE_viscosity}
As for $ \kappa_M $, the predicted eddy velocity scale can be used to form a harmonic eddy viscosity,

\[ \kappa_u = \gamma_u \sqrt{ U_e^2 A_\Delta } \]

as well as a biharmonic eddy viscosity,

\[ \kappa_4 = \gamma_4 \sqrt{ U_e^2 A_\Delta^3 } \]\hypertarget{namespacemom__meke_section_MEKE_limit_case}{}\subsubsection{Limit cases for local source-\/dissipative balance}\label{namespacemom__meke_section_MEKE_limit_case}
Note that in steady-\/state (or when $ a>>1 $) and there is no diffusion of $ E $ then \[ \overline{E} \approx \frac{ \dot{E}_b + \gamma_\eta \dot{E}_\eta + \gamma_v \dot{E}_v }{ \lambda + C_d|U_d|\gamma_b^2 } . \]

In the linear drag limit, where $ U_e << \min(U_b, |u|_{z=-D}, C_d^{-1}\lambda) $, the equilibrium becomes $ \overline{E} \approx \frac{ \dot{E}_b + \gamma_\eta \dot{E}_\eta + \gamma_v \dot{E}_v }{ \lambda + C_d \sqrt{ U_b^2 + |u|^2_{z=-D} } } $.

In the nonlinear drag limit, where $ U_e >> \max(U_b, |u|_{z=-D}, C_d^{-1}\lambda) $, the equilibrium becomes $ \overline{E} \approx \left( \frac{ \dot{E}_b + \gamma_\eta \dot{E}_\eta + \gamma_v \dot{E}_v }{ \sqrt{2} C_d \gamma_b^3 } \right)^\frac{2}{3} $.\hypertarget{namespacemom__meke_section_MEKE_module_parameters}{}\paragraph{M\+E\+K\+E module parameters}\label{namespacemom__meke_section_MEKE_module_parameters}
\tabulinesep=1mm
\begin{longtabu} spread 0pt [c]{*{2}{|X[-1]}|}
\hline
\rowcolor{\tableheadbgcolor}\textbf{ Symbol }&\textbf{ Module parameter  }\\\cline{1-2}
\endfirsthead
\hline
\endfoot
\hline
\rowcolor{\tableheadbgcolor}\textbf{ Symbol }&\textbf{ Module parameter  }\\\cline{1-2}
\endhead
-\/ &{\ttfamily U\+S\+E\+\_\+\+M\+E\+KE} \\\cline{1-2}
$ a $ &{\ttfamily M\+E\+K\+E\+\_\+\+D\+T\+S\+C\+A\+LE} \\\cline{1-2}
$ \dot{E}_b $ &{\ttfamily M\+E\+K\+E\+\_\+\+B\+G\+S\+RC} \\\cline{1-2}
$ \gamma_\eta $ &{\ttfamily M\+E\+K\+E\+\_\+\+G\+M\+C\+O\+E\+FF} \\\cline{1-2}
$ \gamma_v $ &{\ttfamily M\+E\+K\+E\+\_\+\+Fr\+C\+O\+E\+FF} \\\cline{1-2}
$ \lambda $ &{\ttfamily M\+E\+K\+E\+\_\+\+D\+A\+M\+P\+I\+NG} \\\cline{1-2}
$ U_b $ &{\ttfamily M\+E\+K\+E\+\_\+\+U\+S\+C\+A\+LE} \\\cline{1-2}
$ \gamma_{d0} $ &{\ttfamily M\+E\+K\+E\+\_\+\+C\+D\+\_\+\+S\+C\+A\+LE} \\\cline{1-2}
$ c_{b} $ &{\ttfamily M\+E\+K\+E\+\_\+\+CB} \\\cline{1-2}
$ c_{t} $ &{\ttfamily M\+E\+K\+E\+\_\+\+CT} \\\cline{1-2}
$ \kappa_E $ &{\ttfamily M\+E\+K\+E\+\_\+\+KH} \\\cline{1-2}
$ \kappa_4 $ &{\ttfamily M\+E\+K\+E\+\_\+\+K4} \\\cline{1-2}
$ \gamma_\kappa $ &{\ttfamily M\+E\+K\+E\+\_\+\+K\+H\+C\+O\+E\+FF} \\\cline{1-2}
$ \gamma_M $ &{\ttfamily M\+E\+K\+E\+\_\+\+K\+H\+M\+E\+K\+E\+\_\+\+F\+AC} \\\cline{1-2}
$ \gamma_u $ &{\ttfamily M\+E\+K\+E\+\_\+\+V\+I\+S\+C\+O\+S\+I\+T\+Y\+\_\+\+C\+O\+E\+F\+F\+\_\+\+KU} \\\cline{1-2}
$ \gamma_4 $ &{\ttfamily M\+E\+K\+E\+\_\+\+V\+I\+S\+C\+O\+S\+I\+T\+Y\+\_\+\+C\+O\+E\+F\+F\+\_\+\+AU} \\\cline{1-2}
$ \gamma_{min}^2 $&{\ttfamily M\+E\+K\+E\+\_\+\+M\+I\+N\+\_\+\+G\+A\+M\+M\+A2} \\\cline{1-2}
$ \alpha_d $ &{\ttfamily M\+E\+K\+E\+\_\+\+A\+L\+P\+H\+A\+\_\+\+D\+E\+F\+O\+RM} \\\cline{1-2}
$ \alpha_f $ &{\ttfamily M\+E\+K\+E\+\_\+\+A\+L\+P\+H\+A\+\_\+\+F\+R\+I\+CT} \\\cline{1-2}
$ \alpha_R $ &{\ttfamily M\+E\+K\+E\+\_\+\+A\+L\+P\+H\+A\+\_\+\+R\+H\+I\+N\+ES} \\\cline{1-2}
$ \alpha_e $ &{\ttfamily M\+E\+K\+E\+\_\+\+A\+L\+P\+H\+A\+\_\+\+E\+A\+DY} \\\cline{1-2}
$ \alpha_\Delta $ &{\ttfamily M\+E\+K\+E\+\_\+\+A\+L\+P\+H\+A\+\_\+\+G\+R\+ID} \\\cline{1-2}
$ L_c $ &{\ttfamily M\+E\+K\+E\+\_\+\+F\+I\+X\+E\+D\+\_\+\+M\+I\+X\+I\+N\+G\+\_\+\+L\+E\+N\+G\+TH} \\\cline{1-2}
$ c_\beta $ &{\ttfamily M\+E\+K\+E\+\_\+\+T\+O\+P\+O\+G\+R\+A\+P\+H\+I\+C\+\_\+\+B\+E\+TA} \\\cline{1-2}
-\/ &{\ttfamily M\+E\+K\+E\+\_\+\+K\+H\+T\+H\+\_\+\+F\+AC} \\\cline{1-2}
-\/ &{\ttfamily M\+E\+K\+E\+\_\+\+K\+H\+T\+R\+\_\+\+F\+AC} \\\cline{1-2}
\end{longtabu}
\tabulinesep=1mm
\begin{longtabu} spread 0pt [c]{*{2}{|X[-1]}|}
\hline
\rowcolor{\tableheadbgcolor}\textbf{ Symbol }&\textbf{ Model parameter  }\\\cline{1-2}
\endfirsthead
\hline
\endfoot
\hline
\rowcolor{\tableheadbgcolor}\textbf{ Symbol }&\textbf{ Model parameter  }\\\cline{1-2}
\endhead
$ C_d $ &{\ttfamily C\+D\+R\+AG} \\\cline{1-2}
\end{longtabu}
\hypertarget{namespacemom__meke_section_MEKE_references}{}\subsubsection{References}\label{namespacemom__meke_section_MEKE_references}
Jansen, M. F., A. J. Adcroft, R. Hallberg, and I. M. Held, 2015\+: Parameterization of eddy fluxes based on a mesoscale energy budget. Ocean Modelling, 92, 28--41, \href{http://doi.org/10.1016/j.ocemod.2015.05.007}{\tt http\+://doi.\+org/10.\+1016/j.\+ocemod.\+2015.\+05.\+007} .

Marshall, D. P., and A. J. Adcroft, 2010\+: Parameterization of ocean eddies\+: Potential vorticity mixing, energetics and Arnold first stability theorem. Ocean Modelling, 32, 188--204, \href{http://doi.org/10.1016/j.ocemod.2010.02.001}{\tt http\+://doi.\+org/10.\+1016/j.\+ocemod.\+2010.\+02.\+001} . \subsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \hyperlink{structmom__meke_1_1meke__cs}{meke\+\_\+cs}
\begin{DoxyCompactList}\small\item\em Control structure that contains M\+E\+KE parameters and diagnostics handles. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \hyperlink{namespacemom__meke_a5f752f097ddeba7071e1703110e51bc2}{step\+\_\+forward\+\_\+meke} (M\+E\+KE, h, S\+N\+\_\+u, S\+N\+\_\+v, visc, dt, G, GV, US, CS, hu, hv)
\begin{DoxyCompactList}\small\item\em Integrates forward-\/in-\/time the M\+E\+KE eddy energy equation. See \hyperlink{namespacemom__meke_section_MEKE_equations}{M\+E\+KE equations}. \end{DoxyCompactList}\item 
subroutine \hyperlink{namespacemom__meke_a0ef9a8bcdf705b544db9b8c28a5e6a56}{meke\+\_\+equilibrium} (CS, M\+E\+KE, G, GV, US, S\+N\+\_\+u, S\+N\+\_\+v, drag\+\_\+rate\+\_\+visc, I\+\_\+mass)
\begin{DoxyCompactList}\small\item\em Calculates the equilibrium solutino where the source depends only on M\+E\+KE diffusivity and there is no lateral diffusion of M\+E\+KE. Results is in M\+E\+KEM\+E\+KE. \end{DoxyCompactList}\item 
subroutine \hyperlink{namespacemom__meke_a843244b0cc72a08489920dcda464b063}{meke\+\_\+equilibrium\+\_\+restoring} (CS, G, US, S\+N\+\_\+u, S\+N\+\_\+v)
\item 
subroutine \hyperlink{namespacemom__meke_a8180d5d0cacf48bcbdffead9e6a06efd}{meke\+\_\+lengthscales} (CS, M\+E\+KE, G, GV, US, S\+N\+\_\+u, S\+N\+\_\+v, E\+KE, bottom\+Fac2, barotr\+Fac2, Lmix\+Scale)
\begin{DoxyCompactList}\small\item\em Calculates the eddy mixing length scale and $\gamma_b$ and $\gamma_t$ functions that are ratios of either bottom or barotropic eddy energy to the column eddy energy, respectively. See \hyperlink{namespacemom__meke_section_MEKE_equations}{M\+E\+KE equations}. \end{DoxyCompactList}\item 
subroutine \hyperlink{namespacemom__meke_aed5885cde342caa59b2b9cde72a3e1e7}{meke\+\_\+lengthscales\+\_\+0d} (CS, US, area, beta, depth, Rd\+\_\+dx, SN, E\+KE, bottom\+Fac2, barotr\+Fac2, Lmix\+Scale, Lrhines, Leady)
\begin{DoxyCompactList}\small\item\em Calculates the eddy mixing length scale and $\gamma_b$ and $\gamma_t$ functions that are ratios of either bottom or barotropic eddy energy to the column eddy energy, respectively. See \hyperlink{namespacemom__meke_section_MEKE_equations}{M\+E\+KE equations}. \end{DoxyCompactList}\item 
logical function, public \hyperlink{namespacemom__meke_a099f1cfad37430ef1bd60972a92b1be4}{meke\+\_\+init} (Time, G, US, param\+\_\+file, diag, CS, M\+E\+KE, restart\+\_\+\+CS)
\begin{DoxyCompactList}\small\item\em Initializes the M\+O\+M\+\_\+\+M\+E\+KE module and reads parameters. Returns True if module is to be used, otherwise returns False. \end{DoxyCompactList}\item 
subroutine, public \hyperlink{namespacemom__meke_a1900316331157e48f1a6029bac63fbd0}{meke\+\_\+alloc\+\_\+register\+\_\+restart} (HI, param\+\_\+file, M\+E\+KE, restart\+\_\+\+CS)
\begin{DoxyCompactList}\small\item\em Allocates memory and register restart fields for the M\+O\+M\+\_\+\+M\+E\+KE module. \end{DoxyCompactList}\item 
subroutine, public \hyperlink{namespacemom__meke_acc007bf1aa24263f699b059d3e9cc6eb}{meke\+\_\+end} (M\+E\+KE, CS)
\begin{DoxyCompactList}\small\item\em Deallocates any variables allocated in M\+E\+K\+E\+\_\+init or M\+E\+K\+E\+\_\+alloc\+\_\+register\+\_\+restart. \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__meke_a1900316331157e48f1a6029bac63fbd0}\label{namespacemom__meke_a1900316331157e48f1a6029bac63fbd0}} 
\index{mom\+\_\+meke@{mom\+\_\+meke}!meke\+\_\+alloc\+\_\+register\+\_\+restart@{meke\+\_\+alloc\+\_\+register\+\_\+restart}}
\index{meke\+\_\+alloc\+\_\+register\+\_\+restart@{meke\+\_\+alloc\+\_\+register\+\_\+restart}!mom\+\_\+meke@{mom\+\_\+meke}}
\subsubsection{\texorpdfstring{meke\+\_\+alloc\+\_\+register\+\_\+restart()}{meke\_alloc\_register\_restart()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+meke\+::meke\+\_\+alloc\+\_\+register\+\_\+restart (\begin{DoxyParamCaption}\item[{type(hor\+\_\+index\+\_\+type), intent(in)}]{HI,  }\item[{type(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file,  }\item[{type(meke\+\_\+type), pointer}]{M\+E\+KE,  }\item[{type(mom\+\_\+restart\+\_\+cs), pointer}]{restart\+\_\+\+CS }\end{DoxyParamCaption})}



Allocates memory and register restart fields for the M\+O\+M\+\_\+\+M\+E\+KE module. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em hi} & Horizontal index structure\\
\hline
\mbox{\tt in}  & {\em param\+\_\+file} & Parameter file parser structure.\\
\hline
 & {\em meke} & A structure with M\+E\+K\+E-\/related fields.\\
\hline
 & {\em restart\+\_\+cs} & Restart control structure for M\+O\+M\+\_\+\+M\+E\+KE. \\
\hline
\end{DoxyParams}


Definition at line 1347 of file M\+O\+M\+\_\+\+M\+E\+K\+E.\+F90.


\begin{DoxyCode}
1347 \textcolor{comment}{! Arguments}
1348   \textcolor{keywordtype}{type}(hor\_index\_type),  \textcolor{keywordtype}{intent(in)}    :: hi\textcolor{comment}{         !< Horizontal index structure}
1349   \textcolor{keywordtype}{type}(param\_file\_type), \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< Parameter file parser structure.}
1350   \textcolor{keywordtype}{type}(meke\_type),       \textcolor{keywordtype}{pointer}       :: meke\textcolor{comment}{       !< A structure with MEKE-related fields.}
1351   \textcolor{keywordtype}{type}(mom\_restart\_cs),  \textcolor{keywordtype}{pointer}       :: restart\_cs\textcolor{comment}{ !< Restart control structure for MOM\_MEKE.}
1352 \textcolor{comment}{! Local variables}
1353   \textcolor{keywordtype}{type}(vardesc) :: vd
1354   \textcolor{keywordtype}{real} :: meke\_gmcoeff, meke\_frcoeff, meke\_gmecoeff, meke\_khcoeff, meke\_visccoeff\_ku, meke\_visccoeff\_au
1355   \textcolor{keywordtype}{logical} :: use\_kh\_in\_meke
1356   \textcolor{keywordtype}{logical} :: usemeke
1357   \textcolor{keywordtype}{integer} :: isd, ied, jsd, jed
1358 
1359 \textcolor{comment}{! Determine whether this module will be used}
1360   usemeke = .false.; \textcolor{keyword}{call }read\_param(param\_file,\textcolor{stringliteral}{"USE\_MEKE"},usemeke)
1361 
1362 \textcolor{comment}{! Read these parameters to determine what should be in the restarts}
1363   meke\_gmcoeff =-1.; \textcolor{keyword}{call }read\_param(param\_file,\textcolor{stringliteral}{"MEKE\_GMCOEFF"},meke\_gmcoeff)
1364   meke\_frcoeff =-1.; \textcolor{keyword}{call }read\_param(param\_file,\textcolor{stringliteral}{"MEKE\_FRCOEFF"},meke\_frcoeff)
1365   meke\_gmecoeff =-1.; \textcolor{keyword}{call }read\_param(param\_file,\textcolor{stringliteral}{"MEKE\_GMECOEFF"},meke\_gmecoeff)
1366   meke\_khcoeff =1.; \textcolor{keyword}{call }read\_param(param\_file,\textcolor{stringliteral}{"MEKE\_KHCOEFF"},meke\_khcoeff)
1367   meke\_visccoeff\_ku =0.; \textcolor{keyword}{call }read\_param(param\_file,\textcolor{stringliteral}{"MEKE\_VISCOSITY\_COEFF\_KU"},meke\_visccoeff\_ku)
1368   meke\_visccoeff\_au =0.; \textcolor{keyword}{call }read\_param(param\_file,\textcolor{stringliteral}{"MEKE\_VISCOSITY\_COEFF\_AU"},meke\_visccoeff\_au)
1369   use\_kh\_in\_meke = .false.; \textcolor{keyword}{call }read\_param(param\_file,\textcolor{stringliteral}{"USE\_KH\_IN\_MEKE"}, use\_kh\_in\_meke)
1370 \textcolor{comment}{! Allocate control structure}
1371   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke)) \textcolor{keywordflow}{then}
1372     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"MEKE\_alloc\_register\_restart called with an associated "}// &
1373                              \textcolor{stringliteral}{"MEKE type."})
1374     \textcolor{keywordflow}{return}
1375   else; \textcolor{keyword}{allocate}(meke);\textcolor{keywordflow}{ endif}
1376 
1377   \textcolor{keywordflow}{if} (.not. usemeke) \textcolor{keywordflow}{return}
1378 
1379 \textcolor{comment}{! Allocate memory}
1380   \textcolor{keyword}{call }mom\_mesg(\textcolor{stringliteral}{"MEKE\_alloc\_register\_restart: allocating and registering"}, 5)
1381   isd = hi%isd ; ied = hi%ied ; jsd = hi%jsd ; jed = hi%jed
1382   \textcolor{keyword}{allocate}(meke%MEKE(isd:ied,jsd:jed)) ; meke%MEKE(:,:) = 0.0
1383   vd = var\_desc(\textcolor{stringliteral}{"MEKE"}, \textcolor{stringliteral}{"m2 s-2"}, hor\_grid=\textcolor{stringliteral}{'h'}, z\_grid=\textcolor{stringliteral}{'1'}, &
1384            longname=\textcolor{stringliteral}{"Mesoscale Eddy Kinetic Energy"})
1385   \textcolor{keyword}{call }register\_restart\_field(meke%MEKE, vd, .false., restart\_cs)
1386   \textcolor{keywordflow}{if} (meke\_gmcoeff>=0.) \textcolor{keywordflow}{then}
1387     \textcolor{keyword}{allocate}(meke%GM\_src(isd:ied,jsd:jed)) ; meke%GM\_src(:,:) = 0.0
1388 \textcolor{keywordflow}{  endif}
1389   \textcolor{keywordflow}{if} (meke\_frcoeff>=0. .or. meke\_gmecoeff>=0.)  \textcolor{keywordflow}{then}
1390     \textcolor{keyword}{allocate}(meke%mom\_src(isd:ied,jsd:jed)) ; meke%mom\_src(:,:) = 0.0
1391 \textcolor{keywordflow}{  endif}
1392   \textcolor{keywordflow}{if} (meke\_gmecoeff>=0.) \textcolor{keywordflow}{then}
1393     \textcolor{keyword}{allocate}(meke%GME\_snk(isd:ied,jsd:jed)) ; meke%GME\_snk(:,:) = 0.0
1394 \textcolor{keywordflow}{  endif}
1395   \textcolor{keywordflow}{if} (meke\_khcoeff>=0.) \textcolor{keywordflow}{then}
1396     \textcolor{keyword}{allocate}(meke%Kh(isd:ied,jsd:jed)) ; meke%Kh(:,:) = 0.0
1397     vd = var\_desc(\textcolor{stringliteral}{"MEKE\_Kh"}, \textcolor{stringliteral}{"m2 s-1"}, hor\_grid=\textcolor{stringliteral}{'h'}, z\_grid=\textcolor{stringliteral}{'1'}, &
1398              longname=\textcolor{stringliteral}{"Lateral diffusivity from Mesoscale Eddy Kinetic Energy"})
1399     \textcolor{keyword}{call }register\_restart\_field(meke%Kh, vd, .false., restart\_cs)
1400 \textcolor{keywordflow}{  endif}
1401   \textcolor{keyword}{allocate}(meke%Rd\_dx\_h(isd:ied,jsd:jed)) ; meke%Rd\_dx\_h(:,:) = 0.0
1402   \textcolor{keywordflow}{if} (meke\_visccoeff\_ku/=0.) \textcolor{keywordflow}{then}
1403     \textcolor{keyword}{allocate}(meke%Ku(isd:ied,jsd:jed)) ; meke%Ku(:,:) = 0.0
1404     vd = var\_desc(\textcolor{stringliteral}{"MEKE\_Ku"}, \textcolor{stringliteral}{"m2 s-1"}, hor\_grid=\textcolor{stringliteral}{'h'}, z\_grid=\textcolor{stringliteral}{'1'}, &
1405              longname=\textcolor{stringliteral}{"Lateral viscosity from Mesoscale Eddy Kinetic Energy"})
1406     \textcolor{keyword}{call }register\_restart\_field(meke%Ku, vd, .false., restart\_cs)
1407 \textcolor{keywordflow}{  endif}
1408   \textcolor{keywordflow}{if} (use\_kh\_in\_meke) \textcolor{keywordflow}{then}
1409     \textcolor{keyword}{allocate}(meke%Kh\_diff(isd:ied,jsd:jed)) ; meke%Kh\_diff(:,:) = 0.0
1410     vd = var\_desc(\textcolor{stringliteral}{"MEKE\_Kh\_diff"}, \textcolor{stringliteral}{"m2 s-1"},hor\_grid=\textcolor{stringliteral}{'h'},z\_grid=\textcolor{stringliteral}{'1'}, &
1411              longname=\textcolor{stringliteral}{"Copy of thickness diffusivity for diffusing MEKE"})
1412     \textcolor{keyword}{call }register\_restart\_field(meke%Kh\_diff, vd, .false., restart\_cs)
1413 \textcolor{keywordflow}{  endif}
1414 
1415   \textcolor{keywordflow}{if} (meke\_visccoeff\_au/=0.) \textcolor{keywordflow}{then}
1416     \textcolor{keyword}{allocate}(meke%Au(isd:ied,jsd:jed)) ; meke%Au(:,:) = 0.0
1417     vd = var\_desc(\textcolor{stringliteral}{"MEKE\_Au"}, \textcolor{stringliteral}{"m4 s-1"}, hor\_grid=\textcolor{stringliteral}{'h'}, z\_grid=\textcolor{stringliteral}{'1'}, &
1418              longname=\textcolor{stringliteral}{"Lateral biharmonic viscosity from Mesoscale Eddy Kinetic Energy"})
1419     \textcolor{keyword}{call }register\_restart\_field(meke%Au, vd, .false., restart\_cs)
1420 \textcolor{keywordflow}{  endif}
1421 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__meke_acc007bf1aa24263f699b059d3e9cc6eb}\label{namespacemom__meke_acc007bf1aa24263f699b059d3e9cc6eb}} 
\index{mom\+\_\+meke@{mom\+\_\+meke}!meke\+\_\+end@{meke\+\_\+end}}
\index{meke\+\_\+end@{meke\+\_\+end}!mom\+\_\+meke@{mom\+\_\+meke}}
\subsubsection{\texorpdfstring{meke\+\_\+end()}{meke\_end()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+meke\+::meke\+\_\+end (\begin{DoxyParamCaption}\item[{type(meke\+\_\+type), pointer}]{M\+E\+KE,  }\item[{type(\hyperlink{structmom__meke_1_1meke__cs}{meke\+\_\+cs}), pointer}]{CS }\end{DoxyParamCaption})}



Deallocates any variables allocated in M\+E\+K\+E\+\_\+init or M\+E\+K\+E\+\_\+alloc\+\_\+register\+\_\+restart. 


\begin{DoxyParams}{Parameters}
{\em meke} & A structure with M\+E\+K\+E-\/related fields.\\
\hline
{\em cs} & The control structure for M\+O\+M\+\_\+\+M\+E\+KE. \\
\hline
\end{DoxyParams}


Definition at line 1427 of file M\+O\+M\+\_\+\+M\+E\+K\+E.\+F90.


\begin{DoxyCode}
1427   \textcolor{keywordtype}{type}(meke\_type), \textcolor{keywordtype}{pointer} :: meke\textcolor{comment}{ !< A structure with MEKE-related fields.}
1428   \textcolor{keywordtype}{type}(meke\_cs),   \textcolor{keywordtype}{pointer} :: cs\textcolor{comment}{   !< The control structure for MOM\_MEKE.}
1429 
1430   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{deallocate}(cs)
1431 
1432   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(meke)) \textcolor{keywordflow}{return}
1433 
1434   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%MEKE)) \textcolor{keyword}{deallocate}(meke%MEKE)
1435   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%GM\_src)) \textcolor{keyword}{deallocate}(meke%GM\_src)
1436   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%mom\_src)) \textcolor{keyword}{deallocate}(meke%mom\_src)
1437   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%GME\_snk)) \textcolor{keyword}{deallocate}(meke%GME\_snk)
1438   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Kh)) \textcolor{keyword}{deallocate}(meke%Kh)
1439   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Kh\_diff)) \textcolor{keyword}{deallocate}(meke%Kh\_diff)
1440   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Ku)) \textcolor{keyword}{deallocate}(meke%Ku)
1441   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Au)) \textcolor{keyword}{deallocate}(meke%Au)
1442   \textcolor{keyword}{deallocate}(meke)
1443 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__meke_a0ef9a8bcdf705b544db9b8c28a5e6a56}\label{namespacemom__meke_a0ef9a8bcdf705b544db9b8c28a5e6a56}} 
\index{mom\+\_\+meke@{mom\+\_\+meke}!meke\+\_\+equilibrium@{meke\+\_\+equilibrium}}
\index{meke\+\_\+equilibrium@{meke\+\_\+equilibrium}!mom\+\_\+meke@{mom\+\_\+meke}}
\subsubsection{\texorpdfstring{meke\+\_\+equilibrium()}{meke\_equilibrium()}}
{\footnotesize\ttfamily subroutine mom\+\_\+meke\+::meke\+\_\+equilibrium (\begin{DoxyParamCaption}\item[{type(\hyperlink{structmom__meke_1_1meke__cs}{meke\+\_\+cs}), pointer}]{CS,  }\item[{type(meke\+\_\+type), pointer}]{M\+E\+KE,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(inout)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed), intent(in)}]{S\+N\+\_\+u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb), intent(in)}]{S\+N\+\_\+v,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed), intent(in)}]{drag\+\_\+rate\+\_\+visc,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed), intent(in)}]{I\+\_\+mass }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Calculates the equilibrium solutino where the source depends only on M\+E\+KE diffusivity and there is no lateral diffusion of M\+E\+KE. Results is in M\+E\+KEM\+E\+KE. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in,out}  & {\em g} & Ocean grid.\\
\hline
\mbox{\tt in}  & {\em gv} & Ocean vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
 & {\em cs} & M\+E\+KE control structure.\\
\hline
 & {\em meke} & A structure with M\+E\+KE data.\\
\hline
\mbox{\tt in}  & {\em sn\+\_\+u} & Eady growth rate at u-\/points \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em sn\+\_\+v} & Eady growth rate at v-\/points \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em drag\+\_\+rate\+\_\+visc} & Mean flow velocity contribution to the M\+E\+KE drag rate \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em i\+\_\+mass} & Inverse of column mass \mbox{[}R-\/1 Z-\/1 $\sim$$>$ m2 kg-\/1\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 644 of file M\+O\+M\+\_\+\+M\+E\+K\+E.\+F90.


\begin{DoxyCode}
644   \textcolor{keywordtype}{type}(ocean\_grid\_type),             \textcolor{keywordtype}{intent(inout)} :: g\textcolor{comment}{    !< Ocean grid.}
645   \textcolor{keywordtype}{type}(verticalgrid\_type),           \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{   !< Ocean vertical grid structure.}
646   \textcolor{keywordtype}{type}(unit\_scale\_type),             \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{   !< A dimensional unit scaling type}
647   \textcolor{keywordtype}{type}(meke\_cs),                     \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{   !< MEKE control structure.}
648   \textcolor{keywordtype}{type}(meke\_type),                   \textcolor{keywordtype}{pointer}       :: meke\textcolor{comment}{ !< A structure with MEKE data.}
649   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))}, \textcolor{keywordtype}{intent(in)}    :: sn\_u\textcolor{comment}{ !< Eady growth rate at u-points [T-1 ~> s-1].}
650   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))}, \textcolor{keywordtype}{intent(in)}    :: sn\_v\textcolor{comment}{ !< Eady growth rate at v-points [T-1 ~> s-1].}
651   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))},  \textcolor{keywordtype}{intent(in)}    :: drag\_rate\_visc\textcolor{comment}{ !< Mean flow velocity contribution}
652 \textcolor{comment}{                                                           !! to the MEKE drag rate [L T-1 ~> m s-1]}
653   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))},  \textcolor{keywordtype}{intent(in)}    :: i\_mass\textcolor{comment}{  !< Inverse of column mass [R-1 Z-1 ~> m2
       kg-1].}
654   \textcolor{comment}{! Local variables}
655   \textcolor{keywordtype}{real} :: beta \textcolor{comment}{! Combined topograpic and planetary vorticity gradient [T-1 L-1 ~> s-1 m-1]}
656   \textcolor{keywordtype}{real} :: sn   \textcolor{comment}{! The local Eady growth rate [T-1 ~> s-1]}
657   \textcolor{keywordtype}{real} :: bottomfac2, barotrfac2    \textcolor{comment}{! Vertical structure factors [nondim]}
658   \textcolor{keywordtype}{real} :: lmixscale, lrhines, leady \textcolor{comment}{! Various mixing length scales [L ~> m]}
659   \textcolor{keywordtype}{real} :: i\_h, khcoeff
660   \textcolor{keywordtype}{real} :: kh    \textcolor{comment}{! A lateral diffusivity [L2 T-1 ~> m2 s-1]}
661   \textcolor{keywordtype}{real} :: ubg2  \textcolor{comment}{! Background (tidal?) velocity squared [L2 T-2 ~> m2 s-2]}
662   \textcolor{keywordtype}{real} :: cd2
663   \textcolor{keywordtype}{real} :: drag\_rate \textcolor{comment}{! The MEKE spindown timescale due to bottom drag [T-1 ~> s-1].}
664   \textcolor{keywordtype}{real} :: src   \textcolor{comment}{! The sum of MEKE sources [L2 T-3 ~> W kg-1]}
665   \textcolor{keywordtype}{real} :: ldamping  \textcolor{comment}{! The MEKE damping rate [T-1 ~> s-1].}
666   \textcolor{keywordtype}{real} :: eke, ekemin, ekemax, ekeerr \textcolor{comment}{! [L2 T-2 ~> m2 s-2]}
667   \textcolor{keywordtype}{real} :: resid, resmin, resmax \textcolor{comment}{! Residuals [L2 T-3 ~> W kg-1]}
668   \textcolor{keywordtype}{real} :: fath    \textcolor{comment}{! Coriolis parameter at h points; to compute topographic beta [T-1 ~> s-1]}
669   \textcolor{keywordtype}{real} :: beta\_topo\_x, beta\_topo\_y    \textcolor{comment}{! Topographic PV gradients in x and y [T-1 L-1 ~> s-1 m-1]}
670   \textcolor{keywordtype}{integer} :: i, j, is, ie, js, je, n1, n2
671   \textcolor{keywordtype}{real} :: tolerance \textcolor{comment}{! Width of EKE bracket [L2 T-2 ~> m2 s-2].}
672   \textcolor{keywordtype}{logical} :: usesecant, debugiteration
673 
674   \textcolor{keywordtype}{real} :: lgrid, ldeform, lfrict
675 
676   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec
677 
678   debugiteration = .false.
679   khcoeff = cs%MEKE\_KhCoeff
680   ubg2 = cs%MEKE\_Uscale**2
681   cd2 = cs%cdrag**2
682   tolerance = 1.0e-12*us%m\_s\_to\_L\_T**2
683 
684 \textcolor{comment}{!$OMP do}
685   \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
686     \textcolor{comment}{! SN = 0.25*max( (SN\_u(I,j) + SN\_u(I-1,j)) + (SN\_v(i,J) + SN\_v(i,J-1)), 0.)}
687     \textcolor{comment}{! This avoids extremes values in equilibrium solution due to bad values in SN\_u, SN\_v}
688     sn = min(sn\_u(i,j), sn\_u(i-1,j), sn\_v(i,j), sn\_v(i,j-1))
689 
690     \textcolor{keywordflow}{if} (cs%MEKE\_equilibrium\_alt) \textcolor{keywordflow}{then}
691       meke%MEKE(i,j) = (cs%MEKE\_GEOMETRIC\_alpha * sn * us%Z\_to\_m*g%bathyT(i,j))**2 / cd2
692     \textcolor{keywordflow}{else}
693       fath = 0.25*((g%CoriolisBu(i,j) + g%CoriolisBu(i-1,j-1)) + &
694                    (g%CoriolisBu(i-1,j) + g%CoriolisBu(i,j-1))) \textcolor{comment}{! Coriolis parameter at h points}
695 
696       \textcolor{comment}{! Since zero-bathymetry cells are masked, this avoids calculations on land}
697       \textcolor{keywordflow}{if} (cs%MEKE\_topographic\_beta == 0. .or. g%bathyT(i,j) == 0.) \textcolor{keywordflow}{then}
698         beta\_topo\_x = 0. ; beta\_topo\_y = 0.
699       \textcolor{keywordflow}{else}
700         \textcolor{comment}{!### Consider different combinations of these estimates of topographic beta, and the use}
701         \textcolor{comment}{!    of the water column thickness instead of the bathymetric depth.}
702         beta\_topo\_x = -cs%MEKE\_topographic\_beta * fath * 0.5 * ( &
703                       (g%bathyT(i+1,j)-g%bathyT(i,j)) * g%IdxCu(i,j)  &
704                   / max(g%bathyT(i+1,j),g%bathyT(i,j), gv%H\_subroundoff) &
705               +       (g%bathyT(i,j)-g%bathyT(i-1,j)) * g%IdxCu(i-1,j) &
706                   / max(g%bathyT(i,j),g%bathyT(i-1,j), gv%H\_subroundoff) )
707         beta\_topo\_y = -cs%MEKE\_topographic\_beta * fath * 0.5 * ( &
708                       (g%bathyT(i,j+1)-g%bathyT(i,j)) * g%IdyCv(i,j)  &
709                   / max(g%bathyT(i,j+1),g%bathyT(i,j), gv%H\_subroundoff) + &
710                       (g%bathyT(i,j)-g%bathyT(i,j-1)) * g%IdyCv(i,j-1) &
711                   / max(g%bathyT(i,j),g%bathyT(i,j-1), gv%H\_subroundoff) )
712 \textcolor{keywordflow}{      endif}
713       beta =  sqrt((g%dF\_dx(i,j) + beta\_topo\_x)**2 + &
714                    (g%dF\_dy(i,j) + beta\_topo\_y)**2 )
715 
716       i\_h = us%L\_to\_Z*gv%Rho0 * i\_mass(i,j)
717 
718       \textcolor{keywordflow}{if} (khcoeff*sn*i\_h>0.) \textcolor{keywordflow}{then}
719         \textcolor{comment}{! Solve resid(E) = 0, where resid = Kh(E) * (SN)^2 - damp\_rate(E) E}
720         ekemin = 0.   \textcolor{comment}{! Use the trivial root as the left bracket}
721         resmin = 0.   \textcolor{comment}{! Need to detect direction of left residual}
722         ekemax = 0.01*us%m\_s\_to\_L\_T**2 \textcolor{comment}{! First guess at right bracket}
723         usesecant = .false. \textcolor{comment}{! Start using a bisection method}
724 
725         \textcolor{comment}{! First find right bracket for which resid<0}
726         resid = 1.0*us%m\_to\_L**2*us%T\_to\_s**3 ; n1 = 0
727         \textcolor{keywordflow}{do} \textcolor{keywordflow}{while} (resid>0.)
728           n1 = n1 + 1
729           eke = ekemax
730           \textcolor{keyword}{call }meke\_lengthscales\_0d(cs, us, g%areaT(i,j), beta, g%bathyT(i,j), &
731                                     meke%Rd\_dx\_h(i,j), sn, eke, &
732                                     bottomfac2, barotrfac2, lmixscale, lrhines, leady)
733           \textcolor{comment}{! TODO: Should include resolution function in Kh}
734           kh = (khcoeff * sqrt(2.*barotrfac2*eke) * lmixscale)
735           src = kh * (sn * sn)
736           drag\_rate = i\_h * sqrt(drag\_rate\_visc(i,j)**2 + cd2 * ( 2.0*bottomfac2*eke + ubg2 ) )
737           ldamping = cs%MEKE\_damping + drag\_rate * bottomfac2
738           resid = src - ldamping * eke
739           \textcolor{comment}{! if (debugIteration) then}
740           \textcolor{comment}{!   write(0,*) n1, 'EKE=',EKE,'resid=',resid}
741           \textcolor{comment}{!   write(0,*) 'EKEmin=',EKEmin,'ResMin=',ResMin}
742           \textcolor{comment}{!   write(0,*) 'src=',src,'ldamping=',ldamping}
743           \textcolor{comment}{!   write(0,*) 'gamma-b=',bottomFac2,'gamma-t=',barotrFac2}
744           \textcolor{comment}{!   write(0,*) 'drag\_visc=',drag\_rate\_visc(i,j),'Ubg2=',Ubg2}
745           \textcolor{comment}{! endif}
746           \textcolor{keywordflow}{if} (resid>0.) \textcolor{keywordflow}{then}    \textcolor{comment}{! EKE is to the left of the root}
747             ekemin = eke        \textcolor{comment}{! so we move the left bracket here}
748             ekemax = 10. * eke  \textcolor{comment}{! and guess again for the right bracket}
749             \textcolor{keywordflow}{if} (resid<resmin) usesecant = .true.
750             resmin = resid
751             \textcolor{keywordflow}{if} (ekemax > 2.e17*us%m\_s\_to\_L\_T**2) \textcolor{keywordflow}{then}
752               \textcolor{keywordflow}{if} (debugiteration) stop \textcolor{stringliteral}{'Something has gone very wrong'}
753               debugiteration = .true.
754               resid = 1. ; n1 = 0
755               ekemin = 0. ; resmin = 0.
756               ekemax = 0.01*us%m\_s\_to\_L\_T**2
757               usesecant = .false.
758 \textcolor{keywordflow}{            endif}
759 \textcolor{keywordflow}{          endif}
760 \textcolor{keywordflow}{        enddo} \textcolor{comment}{! while(resid>0.) searching for right bracket}
761         resmax = resid
762 
763         \textcolor{comment}{! Bisect the bracket}
764         n2 = 0 ; ekeerr = ekemax - ekemin
765         \textcolor{keywordflow}{do} \textcolor{keywordflow}{while} (ekeerr > tolerance)
766           n2 = n2 + 1
767           \textcolor{keywordflow}{if} (usesecant) \textcolor{keywordflow}{then}
768             eke = ekemin + (ekemax - ekemin) * (resmin / (resmin - resmax))
769           \textcolor{keywordflow}{else}
770             eke = 0.5 * (ekemin + ekemax)
771 \textcolor{keywordflow}{          endif}
772           ekeerr = min( eke-ekemin, ekemax-eke )
773           \textcolor{comment}{! TODO: Should include resolution function in Kh}
774           kh = (khcoeff * sqrt(2.*barotrfac2*eke) * lmixscale)
775           src = kh * (sn * sn)
776           drag\_rate = i\_h * sqrt( drag\_rate\_visc(i,j)**2 + cd2 * ( 2.0*bottomfac2*eke + ubg2 ) )
777           ldamping = cs%MEKE\_damping + drag\_rate * bottomfac2
778           resid = src - ldamping * eke
779           \textcolor{keywordflow}{if} (usesecant .and. resid>resmin) usesecant = .false.
780           \textcolor{keywordflow}{if} (resid>0.) \textcolor{keywordflow}{then}              \textcolor{comment}{! EKE is to the left of the root}
781             ekemin = eke                  \textcolor{comment}{! so we move the left bracket here}
782             \textcolor{keywordflow}{if} (resid<resmin) usesecant = .true.
783             resmin = resid                \textcolor{comment}{! Save this for the secant method}
784           \textcolor{keywordflow}{elseif} (resid<0.) \textcolor{keywordflow}{then}          \textcolor{comment}{! EKE is to the right of the root}
785             ekemax = eke                  \textcolor{comment}{! so we move the right bracket here}
786             resmax = resid                \textcolor{comment}{! Save this for the secant method}
787           \textcolor{keywordflow}{else}
788             \textcolor{keywordflow}{exit}                          \textcolor{comment}{! resid=0 => EKE is exactly at the root}
789 \textcolor{keywordflow}{          endif}
790           \textcolor{keywordflow}{if} (n2>200) stop \textcolor{stringliteral}{'Failing to converge?'}
791 \textcolor{keywordflow}{        enddo} \textcolor{comment}{! while(EKEmax-EKEmin>tolerance)}
792 
793       \textcolor{keywordflow}{else}
794         eke = 0.
795 \textcolor{keywordflow}{      endif}
796       meke%MEKE(i,j) = eke
797 \textcolor{keywordflow}{    endif}
798 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
799 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__meke_a843244b0cc72a08489920dcda464b063}\label{namespacemom__meke_a843244b0cc72a08489920dcda464b063}} 
\index{mom\+\_\+meke@{mom\+\_\+meke}!meke\+\_\+equilibrium\+\_\+restoring@{meke\+\_\+equilibrium\+\_\+restoring}}
\index{meke\+\_\+equilibrium\+\_\+restoring@{meke\+\_\+equilibrium\+\_\+restoring}!mom\+\_\+meke@{mom\+\_\+meke}}
\subsubsection{\texorpdfstring{meke\+\_\+equilibrium\+\_\+restoring()}{meke\_equilibrium\_restoring()}}
{\footnotesize\ttfamily subroutine mom\+\_\+meke\+::meke\+\_\+equilibrium\+\_\+restoring (\begin{DoxyParamCaption}\item[{type(\hyperlink{structmom__meke_1_1meke__cs}{meke\+\_\+cs}), pointer}]{CS,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(inout)}]{G,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed), intent(in)}]{S\+N\+\_\+u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb), intent(in)}]{S\+N\+\_\+v }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in,out}  & {\em g} & Ocean grid.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type.\\
\hline
 & {\em cs} & M\+E\+KE control structure.\\
\hline
\mbox{\tt in}  & {\em sn\+\_\+u} & Eady growth rate at u-\/points \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em sn\+\_\+v} & Eady growth rate at v-\/points \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 806 of file M\+O\+M\+\_\+\+M\+E\+K\+E.\+F90.


\begin{DoxyCode}
806   \textcolor{keywordtype}{type}(ocean\_grid\_type),             \textcolor{keywordtype}{intent(inout)} :: g\textcolor{comment}{    !< Ocean grid.}
807   \textcolor{keywordtype}{type}(unit\_scale\_type),             \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{   !< A dimensional unit scaling type.}
808   \textcolor{keywordtype}{type}(meke\_cs),                     \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{   !< MEKE control structure.}
809   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))}, \textcolor{keywordtype}{intent(in)}    :: sn\_u\textcolor{comment}{ !< Eady growth rate at u-points [T-1 ~> s-1].}
810   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))}, \textcolor{keywordtype}{intent(in)}    :: sn\_v\textcolor{comment}{ !< Eady growth rate at v-points [T-1 ~> s-1].}
811   \textcolor{comment}{! Local variables}
812   \textcolor{keywordtype}{real} :: sn                      \textcolor{comment}{! The local Eady growth rate [T-1 ~> s-1]}
813   \textcolor{keywordtype}{integer} :: i, j, is, ie, js, je \textcolor{comment}{! local indices}
814   \textcolor{keywordtype}{real} :: cd2                     \textcolor{comment}{! bottom drag}
815 
816   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec
817   cd2 = cs%cdrag**2
818 
819   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(cs%equilibrium\_value)) \textcolor{keyword}{allocate}(cs%equilibrium\_value(szi\_(g),szj\_(g)))
820   cs%equilibrium\_value(:,:) = 0.0
821 
822 \textcolor{comment}{!$OMP do}
823   \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
824     \textcolor{comment}{! SN = 0.25*max( (SN\_u(I,j) + SN\_u(I-1,j)) + (SN\_v(i,J) + SN\_v(i,J-1)), 0.)}
825     \textcolor{comment}{! This avoids extremes values in equilibrium solution due to bad values in SN\_u, SN\_v}
826     sn = min(sn\_u(i,j), sn\_u(i-1,j), sn\_v(i,j), sn\_v(i,j-1))
827     cs%equilibrium\_value(i,j) = (cs%MEKE\_GEOMETRIC\_alpha * sn * us%Z\_to\_m*g%bathyT(i,j))**2 / cd2
828 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
829 
830   \textcolor{keywordflow}{if} (cs%id\_MEKE\_equilibrium>0) \textcolor{keyword}{call }post\_data(cs%id\_MEKE\_equilibrium, cs%equilibrium\_value, cs%diag)
831 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__meke_a099f1cfad37430ef1bd60972a92b1be4}\label{namespacemom__meke_a099f1cfad37430ef1bd60972a92b1be4}} 
\index{mom\+\_\+meke@{mom\+\_\+meke}!meke\+\_\+init@{meke\+\_\+init}}
\index{meke\+\_\+init@{meke\+\_\+init}!mom\+\_\+meke@{mom\+\_\+meke}}
\subsubsection{\texorpdfstring{meke\+\_\+init()}{meke\_init()}}
{\footnotesize\ttfamily logical function, public mom\+\_\+meke\+::meke\+\_\+init (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), intent(in)}]{Time,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(inout)}]{G,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file,  }\item[{type(diag\+\_\+ctrl), intent(inout), target}]{diag,  }\item[{type(\hyperlink{structmom__meke_1_1meke__cs}{meke\+\_\+cs}), pointer}]{CS,  }\item[{type(meke\+\_\+type), pointer}]{M\+E\+KE,  }\item[{type(mom\+\_\+restart\+\_\+cs), pointer}]{restart\+\_\+\+CS }\end{DoxyParamCaption})}



Initializes the M\+O\+M\+\_\+\+M\+E\+KE module and reads parameters. Returns True if module is to be used, otherwise returns False. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\tt in}  & {\em time} & The current model time.\\
\hline
\mbox{\tt in,out}  & {\em g} & The ocean\textquotesingle{}s grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em param\+\_\+file} & Parameter file parser structure.\\
\hline
\mbox{\tt in,out}  & {\em diag} & Diagnostics structure.\\
\hline
 & {\em cs} & M\+E\+KE control structure.\\
\hline
 & {\em meke} & M\+E\+K\+E-\/related fields.\\
\hline
 & {\em restart\+\_\+cs} & Restart control structure for M\+O\+M\+\_\+\+M\+E\+KE. \\
\hline
\end{DoxyParams}


Definition at line 984 of file M\+O\+M\+\_\+\+M\+E\+K\+E.\+F90.


\begin{DoxyCode}
984   \textcolor{keywordtype}{type}(time\_type),         \textcolor{keywordtype}{intent(in)}    :: time\textcolor{comment}{       !< The current model time.}
985   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(inout)} :: g\textcolor{comment}{          !< The ocean's grid structure.}
986   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{         !< A dimensional unit scaling type}
987   \textcolor{keywordtype}{type}(param\_file\_type),   \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< Parameter file parser structure.}
988   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{       !< Diagnostics structure.}
989   \textcolor{keywordtype}{type}(meke\_cs),           \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{         !< MEKE control structure.}
990   \textcolor{keywordtype}{type}(meke\_type),         \textcolor{keywordtype}{pointer}       :: meke\textcolor{comment}{       !< MEKE-related fields.}
991   \textcolor{keywordtype}{type}(mom\_restart\_cs),    \textcolor{keywordtype}{pointer}       :: restart\_cs\textcolor{comment}{ !< Restart control structure for MOM\_MEKE.}
992 
993   \textcolor{comment}{! Local variables}
994   \textcolor{keywordtype}{real}    :: i\_t\_rescale   \textcolor{comment}{! A rescaling factor for time from the internal representation in this}
995                            \textcolor{comment}{! run to the representation in a restart file.}
996   \textcolor{keywordtype}{real}    :: l\_rescale     \textcolor{comment}{! A rescaling factor for length from the internal representation in this}
997                            \textcolor{comment}{! run to the representation in a restart file.}
998   \textcolor{keywordtype}{real}    :: meke\_restoring\_timescale \textcolor{comment}{! The timescale used to nudge MEKE toward its equilibrium value.}
999   \textcolor{keywordtype}{real} :: cdrag            \textcolor{comment}{! The default bottom drag coefficient [nondim].}
1000   \textcolor{keywordtype}{integer} :: i, j, is, ie, js, je, isd, ied, jsd, jed
1001   \textcolor{keywordtype}{logical} :: laplacian, biharmonic, usevarmix, coldstart
1002   \textcolor{comment}{! This include declares and sets the variable "version".}
1003 \textcolor{preprocessor}{# include "version\_variable.h"}
1004 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_MEKE"} \textcolor{comment}{! This module's name.}
1005 
1006   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec
1007   isd = g%isd ; ied = g%ied ; jsd = g%jsd ; jed = g%jed
1008 
1009   \textcolor{comment}{! Determine whether this module will be used}
1010   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_MEKE"}, meke\_init, default=.false., do\_not\_log=.true.)
1011   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \textcolor{stringliteral}{""}, all\_default=.not.meke\_init)
1012   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_MEKE"}, meke\_init, &
1013                  \textcolor{stringliteral}{"If true, turns on the MEKE scheme which calculates "}// &
1014                  \textcolor{stringliteral}{"a sub-grid mesoscale eddy kinetic energy budget."}, &
1015                  default=.false.)
1016   \textcolor{keywordflow}{if} (.not. meke\_init) \textcolor{keywordflow}{return}
1017 
1018   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(meke)) \textcolor{keywordflow}{then}
1019     \textcolor{comment}{! The MEKE structure should have been allocated in MEKE\_alloc\_register\_restart()}
1020     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"MEKE\_init called with NO associated "}// &
1021                             \textcolor{stringliteral}{"MEKE-type structure."})
1022     \textcolor{keywordflow}{return}
1023 \textcolor{keywordflow}{  endif}
1024   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}
1025     \textcolor{keyword}{call }mom\_error(warning, &
1026       \textcolor{stringliteral}{"MEKE\_init called with an associated control structure."})
1027     \textcolor{keywordflow}{return}
1028   \textcolor{keywordflow}{else} ; \textcolor{keyword}{allocate}(cs) ;\textcolor{keywordflow}{ endif}
1029 
1030   \textcolor{keyword}{call }mom\_mesg(\textcolor{stringliteral}{"MEKE\_init: reading parameters "}, 5)
1031 
1032   \textcolor{comment}{! Read all relevant parameters and write them to the model log.}
1033   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_DAMPING"}, cs%MEKE\_damping, &
1034                  \textcolor{stringliteral}{"The local depth-independent MEKE dissipation rate."}, &
1035                  units=\textcolor{stringliteral}{"s-1"}, default=0.0, scale=us%T\_to\_s)
1036   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_CD\_SCALE"}, cs%MEKE\_Cd\_scale, &
1037                  \textcolor{stringliteral}{"The ratio of the bottom eddy velocity to the column mean "}//&
1038                  \textcolor{stringliteral}{"eddy velocity, i.e. sqrt(2*MEKE). This should be less than 1 "}//&
1039                  \textcolor{stringliteral}{"to account for the surface intensification of MEKE."}, &
1040                  units=\textcolor{stringliteral}{"nondim"}, default=0.)
1041   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_CB"}, cs%MEKE\_Cb, &
1042                  \textcolor{stringliteral}{"A coefficient in the expression for the ratio of bottom projected "}//&
1043                  \textcolor{stringliteral}{"eddy energy and mean column energy (see Jansen et al. 2015)."},&
1044                  units=\textcolor{stringliteral}{"nondim"}, default=25.)
1045   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_MIN\_GAMMA2"}, cs%MEKE\_min\_gamma, &
1046                  \textcolor{stringliteral}{"The minimum allowed value of gamma\_b^2."},&
1047                  units=\textcolor{stringliteral}{"nondim"}, default=0.0001)
1048   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_CT"}, cs%MEKE\_Ct, &
1049                  \textcolor{stringliteral}{"A coefficient in the expression for the ratio of barotropic "}//&
1050                  \textcolor{stringliteral}{"eddy energy and mean column energy (see Jansen et al. 2015)."},&
1051                  units=\textcolor{stringliteral}{"nondim"}, default=50.)
1052   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_GMCOEFF"}, cs%MEKE\_GMcoeff, &
1053                  \textcolor{stringliteral}{"The efficiency of the conversion of potential energy "}//&
1054                  \textcolor{stringliteral}{"into MEKE by the thickness mixing parameterization. "}//&
1055                  \textcolor{stringliteral}{"If MEKE\_GMCOEFF is negative, this conversion is not "}//&
1056                  \textcolor{stringliteral}{"used or calculated."}, units=\textcolor{stringliteral}{"nondim"}, default=-1.0)
1057   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_GEOMETRIC"}, cs%MEKE\_GEOMETRIC, &
1058                  \textcolor{stringliteral}{"If MEKE\_GEOMETRIC is true, uses the GM coefficient formulation "}//&
1059                  \textcolor{stringliteral}{"from the GEOMETRIC framework (Marshall et al., 2012)."}, default=.false.)
1060   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_GEOMETRIC\_ALPHA"}, cs%MEKE\_GEOMETRIC\_alpha, &
1061                  \textcolor{stringliteral}{"The nondimensional coefficient governing the efficiency of the GEOMETRIC \(\backslash\)n"}//&
1062                  \textcolor{stringliteral}{"thickness diffusion."}, units=\textcolor{stringliteral}{"nondim"}, default=0.05)
1063   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_EQUILIBRIUM\_ALT"}, cs%MEKE\_equilibrium\_alt, &
1064                  \textcolor{stringliteral}{"If true, use an alternative formula for computing the (equilibrium)"}//&
1065                  \textcolor{stringliteral}{"initial value of MEKE."}, default=.false.)
1066   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_EQUILIBRIUM\_RESTORING"}, cs%MEKE\_equilibrium\_restoring, &
1067                  \textcolor{stringliteral}{"If true, restore MEKE back to its equilibrium value, which is calculated at "}//&
1068                  \textcolor{stringliteral}{"each time step."}, default=.false.)
1069   \textcolor{keywordflow}{if} (cs%MEKE\_equilibrium\_restoring) \textcolor{keywordflow}{then}
1070     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_RESTORING\_TIMESCALE"}, meke\_restoring\_timescale, &
1071                    \textcolor{stringliteral}{"The timescale used to nudge MEKE toward its equilibrium value."}, units=\textcolor{stringliteral}{"s"}, &
1072                    default=1e6, scale=us%T\_to\_s)
1073     cs%MEKE\_restoring\_rate = 1.0 / meke\_restoring\_timescale
1074 \textcolor{keywordflow}{  endif}
1075 
1076   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_FRCOEFF"}, cs%MEKE\_FrCoeff, &
1077                  \textcolor{stringliteral}{"The efficiency of the conversion of mean energy into "}//&
1078                  \textcolor{stringliteral}{"MEKE.  If MEKE\_FRCOEFF is negative, this conversion "}//&
1079                  \textcolor{stringliteral}{"is not used or calculated."}, units=\textcolor{stringliteral}{"nondim"}, default=-1.0)
1080   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_GMECOEFF"}, cs%MEKE\_GMECoeff, &
1081                  \textcolor{stringliteral}{"The efficiency of the conversion of MEKE into mean energy "}//&
1082                  \textcolor{stringliteral}{"by GME.  If MEKE\_GMECOEFF is negative, this conversion "}//&
1083                  \textcolor{stringliteral}{"is not used or calculated."}, units=\textcolor{stringliteral}{"nondim"}, default=-1.0)
1084   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_BGSRC"}, cs%MEKE\_BGsrc, &
1085                  \textcolor{stringliteral}{"A background energy source for MEKE."}, units=\textcolor{stringliteral}{"W kg-1"}, &
1086                  default=0.0, scale=us%m\_to\_L**2*us%T\_to\_s**3)
1087   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_KH"}, cs%MEKE\_Kh, &
1088                  \textcolor{stringliteral}{"A background lateral diffusivity of MEKE. "}//&
1089                  \textcolor{stringliteral}{"Use a negative value to not apply lateral diffusion to MEKE."}, &
1090                  units=\textcolor{stringliteral}{"m2 s-1"}, default=-1.0, scale=us%m\_to\_L**2*us%T\_to\_s)
1091   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_K4"}, cs%MEKE\_K4, &
1092                  \textcolor{stringliteral}{"A lateral bi-harmonic diffusivity of MEKE. "}//&
1093                  \textcolor{stringliteral}{"Use a negative value to not apply bi-harmonic diffusion to MEKE."}, &
1094                  units=\textcolor{stringliteral}{"m4 s-1"}, default=-1.0, scale=us%m\_to\_L**4*us%T\_to\_s)
1095   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_DTSCALE"}, cs%MEKE\_dtScale, &
1096                  \textcolor{stringliteral}{"A scaling factor to accelerate the time evolution of MEKE."}, &
1097                  units=\textcolor{stringliteral}{"nondim"}, default=1.0)
1098   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_KHCOEFF"}, cs%MEKE\_KhCoeff, &
1099                  \textcolor{stringliteral}{"A scaling factor in the expression for eddy diffusivity "}//&
1100                  \textcolor{stringliteral}{"which is otherwise proportional to the MEKE velocity- "}//&
1101                  \textcolor{stringliteral}{"scale times an eddy mixing-length. This factor "}//&
1102                  \textcolor{stringliteral}{"must be >0 for MEKE to contribute to the thickness/ "}//&
1103                  \textcolor{stringliteral}{"and tracer diffusivity in the rest of the model."}, &
1104                  units=\textcolor{stringliteral}{"nondim"}, default=1.0)
1105   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_USCALE"}, cs%MEKE\_Uscale, &
1106                  \textcolor{stringliteral}{"The background velocity that is combined with MEKE to "}//&
1107                  \textcolor{stringliteral}{"calculate the bottom drag."}, units=\textcolor{stringliteral}{"m s-1"}, default=0.0, scale=us%m\_s\_to\_L\_T)
1108   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_GM\_SRC\_ALT"}, cs%GM\_src\_alt, &
1109                  \textcolor{stringliteral}{"If true, use the GM energy conversion form S^2*N^2*kappa rather "}//&
1110                  \textcolor{stringliteral}{"than the streamfunction for the MEKE GM source term."}, default=.false.)
1111   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_VISC\_DRAG"}, cs%visc\_drag, &
1112                  \textcolor{stringliteral}{"If true, use the vertvisc\_type to calculate the bottom "}//&
1113                  \textcolor{stringliteral}{"drag acting on MEKE."}, default=.true.)
1114   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_KHTH\_FAC"}, meke%KhTh\_fac, &
1115                  \textcolor{stringliteral}{"A factor that maps MEKE%Kh to KhTh."}, units=\textcolor{stringliteral}{"nondim"}, &
1116                  default=0.0)
1117   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_KHTR\_FAC"}, meke%KhTr\_fac, &
1118                  \textcolor{stringliteral}{"A factor that maps MEKE%Kh to KhTr."}, units=\textcolor{stringliteral}{"nondim"}, &
1119                  default=0.0)
1120   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_KHMEKE\_FAC"}, cs%KhMEKE\_Fac, &
1121                  \textcolor{stringliteral}{"A factor that maps MEKE%Kh to Kh for MEKE itself."}, &
1122                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)
1123   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_OLD\_LSCALE"}, cs%use\_old\_lscale, &
1124                  \textcolor{stringliteral}{"If true, use the old formula for length scale which is "}//&
1125                  \textcolor{stringliteral}{"a function of grid spacing and deformation radius."},  &
1126                  default=.false.)
1127   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_MIN\_LSCALE"}, cs%use\_min\_lscale, &
1128                  \textcolor{stringliteral}{"If true, use a strict minimum of provided length scales "}//&
1129                  \textcolor{stringliteral}{"rather than harmonic mean."},  &
1130                  default=.false.)
1131   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_RD\_MAX\_SCALE"}, cs%Rd\_as\_max\_scale, &
1132                  \textcolor{stringliteral}{"If true, the length scale used by MEKE is the minimum of "}//&
1133                  \textcolor{stringliteral}{"the deformation radius or grid-spacing. Only used if "}//&
1134                  \textcolor{stringliteral}{"MEKE\_OLD\_LSCALE=True"}, units=\textcolor{stringliteral}{"nondim"}, default=.false.)
1135   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_VISCOSITY\_COEFF\_KU"}, cs%viscosity\_coeff\_Ku, &
1136                  \textcolor{stringliteral}{"If non-zero, is the scaling coefficient in the expression for"}//&
1137                  \textcolor{stringliteral}{"viscosity used to parameterize harmonic lateral momentum mixing by"}//&
1138                  \textcolor{stringliteral}{"unresolved eddies represented by MEKE. Can be negative to"}//&
1139                  \textcolor{stringliteral}{"represent backscatter from the unresolved eddies."}, &
1140                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)
1141   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_VISCOSITY\_COEFF\_AU"}, cs%viscosity\_coeff\_Au, &
1142                  \textcolor{stringliteral}{"If non-zero, is the scaling coefficient in the expression for"}//&
1143                  \textcolor{stringliteral}{"viscosity used to parameterize biharmonic lateral momentum mixing by"}//&
1144                  \textcolor{stringliteral}{"unresolved eddies represented by MEKE. Can be negative to"}//&
1145                  \textcolor{stringliteral}{"represent backscatter from the unresolved eddies."}, &
1146                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)
1147   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_FIXED\_MIXING\_LENGTH"}, cs%Lfixed, &
1148                  \textcolor{stringliteral}{"If positive, is a fixed length contribution to the expression "}//&
1149                  \textcolor{stringliteral}{"for mixing length used in MEKE-derived diffusivity."}, &
1150                  units=\textcolor{stringliteral}{"m"}, default=0.0, scale=us%m\_to\_L)
1151   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_ALPHA\_DEFORM"}, cs%aDeform, &
1152                  \textcolor{stringliteral}{"If positive, is a coefficient weighting the deformation scale "}//&
1153                  \textcolor{stringliteral}{"in the expression for mixing length used in MEKE-derived diffusivity."}, &
1154                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)
1155   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_ALPHA\_RHINES"}, cs%aRhines, &
1156                  \textcolor{stringliteral}{"If positive, is a coefficient weighting the Rhines scale "}//&
1157                  \textcolor{stringliteral}{"in the expression for mixing length used in MEKE-derived diffusivity."}, &
1158                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)
1159   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_ALPHA\_EADY"}, cs%aEady, &
1160                  \textcolor{stringliteral}{"If positive, is a coefficient weighting the Eady length scale "}//&
1161                  \textcolor{stringliteral}{"in the expression for mixing length used in MEKE-derived diffusivity."}, &
1162                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)
1163   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_ALPHA\_FRICT"}, cs%aFrict, &
1164                  \textcolor{stringliteral}{"If positive, is a coefficient weighting the frictional arrest scale "}//&
1165                  \textcolor{stringliteral}{"in the expression for mixing length used in MEKE-derived diffusivity."}, &
1166                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)
1167   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_ALPHA\_GRID"}, cs%aGrid, &
1168                  \textcolor{stringliteral}{"If positive, is a coefficient weighting the grid-spacing as a scale "}//&
1169                  \textcolor{stringliteral}{"in the expression for mixing length used in MEKE-derived diffusivity."}, &
1170                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)
1171   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_COLD\_START"}, coldstart, &
1172                  \textcolor{stringliteral}{"If true, initialize EKE to zero. Otherwise a local equilibrium solution "}//&
1173                  \textcolor{stringliteral}{"is used as an initial condition for EKE."}, default=.false.)
1174   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_BACKSCAT\_RO\_C"}, meke%backscatter\_Ro\_c, &
1175                  \textcolor{stringliteral}{"The coefficient in the Rossby number function for scaling the biharmonic "}//&
1176                  \textcolor{stringliteral}{"frictional energy source. Setting to non-zero enables the Rossby number function."}, &
1177                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)
1178   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_BACKSCAT\_RO\_POW"}, meke%backscatter\_Ro\_pow, &
1179                  \textcolor{stringliteral}{"The power in the Rossby number function for scaling the biharmonic "}//&
1180                  \textcolor{stringliteral}{"frictional energy source."}, units=\textcolor{stringliteral}{"nondim"}, default=0.0)
1181   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_ADVECTION\_FACTOR"}, cs%MEKE\_advection\_factor, &
1182                  \textcolor{stringliteral}{"A scale factor in front of advection of eddy energy. Zero turns advection off. "}//&
1183                  \textcolor{stringliteral}{"Using unity would be normal but other values could accommodate a mismatch "}//&
1184                  \textcolor{stringliteral}{"between the advecting barotropic flow and the vertical structure of MEKE."}, &
1185                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)
1186   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_TOPOGRAPHIC\_BETA"}, cs%MEKE\_topographic\_beta, &
1187                  \textcolor{stringliteral}{"A scale factor to determine how much topographic beta is weighed in "} //&
1188                  \textcolor{stringliteral}{"computing beta in the expression of Rhines scale. Use 1 if full "}//&
1189                  \textcolor{stringliteral}{"topographic beta effect is considered; use 0 if it's completely ignored."}, &
1190                  units=\textcolor{stringliteral}{"nondim"}, default=0.0)
1191 
1192   \textcolor{comment}{! Nonlocal module parameters}
1193   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CDRAG"}, cdrag, &
1194                  \textcolor{stringliteral}{"CDRAG is the drag coefficient relating the magnitude of "}//&
1195                  \textcolor{stringliteral}{"the velocity field to the bottom stress."}, units=\textcolor{stringliteral}{"nondim"}, &
1196                  default=0.003)
1197   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MEKE\_CDRAG"}, cs%cdrag, &
1198                  \textcolor{stringliteral}{"Drag coefficient relating the magnitude of the velocity "}//&
1199                  \textcolor{stringliteral}{"field to the bottom stress in MEKE."}, units=\textcolor{stringliteral}{"nondim"}, &
1200                  default=cdrag)
1201   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"LAPLACIAN"}, laplacian, default=.false., do\_not\_log=.true.)
1202   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BIHARMONIC"}, biharmonic, default=.false., do\_not\_log=.true.)
1203 
1204   \textcolor{keywordflow}{if} (cs%viscosity\_coeff\_Ku/=0. .and. .not. laplacian) \textcolor{keyword}{call }mom\_error(fatal, &
1205                  \textcolor{stringliteral}{"LAPLACIAN must be true if MEKE\_VISCOSITY\_COEFF\_KU is true."})
1206 
1207   \textcolor{keywordflow}{if} (cs%viscosity\_coeff\_Au/=0. .and. .not. biharmonic) \textcolor{keyword}{call }mom\_error(fatal, &
1208                  \textcolor{stringliteral}{"BIHARMONIC must be true if MEKE\_VISCOSITY\_COEFF\_AU is true."})
1209 
1210   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEBUG"}, cs%debug, default=.false., do\_not\_log=.true.)
1211 
1212   \textcolor{comment}{! Identify if any lateral diffusive processes are active}
1213   cs%kh\_flux\_enabled = .false.
1214   \textcolor{keywordflow}{if} ((cs%MEKE\_KH >= 0.0)  .or. (cs%KhMEKE\_FAC > 0.0) .or. (cs%MEKE\_advection\_factor > 0.0)) &
1215     cs%kh\_flux\_enabled = .true.
1216 
1217 \textcolor{comment}{! Register fields for output from this module.}
1218   cs%diag => diag
1219   cs%id\_MEKE = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE'}, diag%axesT1, time, &
1220      \textcolor{stringliteral}{'Mesoscale Eddy Kinetic Energy'}, \textcolor{stringliteral}{'m2 s-2'}, conversion=us%L\_T\_to\_m\_s**2)
1221   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(meke%MEKE)) cs%id\_MEKE = -1
1222   cs%id\_Kh = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_KH'}, diag%axesT1, time, &
1223      \textcolor{stringliteral}{'MEKE derived diffusivity'}, \textcolor{stringliteral}{'m2 s-1'}, conversion=us%L\_to\_m**2*us%s\_to\_T)
1224   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(meke%Kh)) cs%id\_Kh = -1
1225   cs%id\_Ku = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_KU'}, diag%axesT1, time, &
1226      \textcolor{stringliteral}{'MEKE derived lateral viscosity'}, \textcolor{stringliteral}{'m2 s-1'}, conversion=us%L\_to\_m**2*us%s\_to\_T)
1227   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(meke%Ku)) cs%id\_Ku = -1
1228   cs%id\_Au = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_AU'}, diag%axesT1, time, &
1229      \textcolor{stringliteral}{'MEKE derived lateral biharmonic viscosity'}, \textcolor{stringliteral}{'m4 s-1'}, conversion=us%L\_to\_m**4*us%s\_to\_T)
1230   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(meke%Au)) cs%id\_Au = -1
1231   cs%id\_Ue = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_Ue'}, diag%axesT1, time, &
1232      \textcolor{stringliteral}{'MEKE derived eddy-velocity scale'}, \textcolor{stringliteral}{'m s-1'}, conversion=us%L\_T\_to\_m\_s)
1233   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(meke%MEKE)) cs%id\_Ue = -1
1234   cs%id\_Ub = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_Ub'}, diag%axesT1, time, &
1235      \textcolor{stringliteral}{'MEKE derived bottom eddy-velocity scale'}, \textcolor{stringliteral}{'m s-1'}, conversion=us%L\_T\_to\_m\_s)
1236   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(meke%MEKE)) cs%id\_Ub = -1
1237   cs%id\_Ut = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_Ut'}, diag%axesT1, time, &
1238      \textcolor{stringliteral}{'MEKE derived barotropic eddy-velocity scale'}, \textcolor{stringliteral}{'m s-1'}, conversion=us%L\_T\_to\_m\_s)
1239   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(meke%MEKE)) cs%id\_Ut = -1
1240   cs%id\_src = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_src'}, diag%axesT1, time, &
1241      \textcolor{stringliteral}{'MEKE energy source'}, \textcolor{stringliteral}{'m2 s-3'}, conversion=(us%L\_T\_to\_m\_s**2)*us%s\_to\_T)
1242   cs%id\_decay = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_decay'}, diag%axesT1, time, &
1243      \textcolor{stringliteral}{'MEKE decay rate'}, \textcolor{stringliteral}{'s-1'}, conversion=us%s\_to\_T)
1244   cs%id\_GM\_src = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_GM\_src'}, diag%axesT1, time, &
1245      \textcolor{stringliteral}{'MEKE energy available from thickness mixing'}, &
1246      \textcolor{stringliteral}{'W m-2'}, conversion=us%RZ3\_T3\_to\_W\_m2*us%L\_to\_Z**2)
1247   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(meke%GM\_src)) cs%id\_GM\_src = -1
1248   cs%id\_mom\_src = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_mom\_src'},diag%axesT1, time, &
1249      \textcolor{stringliteral}{'MEKE energy available from momentum'}, &
1250      \textcolor{stringliteral}{'W m-2'}, conversion=us%RZ3\_T3\_to\_W\_m2*us%L\_to\_Z**2)
1251   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(meke%mom\_src)) cs%id\_mom\_src = -1
1252   cs%id\_GME\_snk = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_GME\_snk'},diag%axesT1, time, &
1253      \textcolor{stringliteral}{'MEKE energy lost to GME backscatter'}, &
1254      \textcolor{stringliteral}{'W m-2'}, conversion=us%RZ3\_T3\_to\_W\_m2*us%L\_to\_Z**2)
1255   \textcolor{keywordflow}{if} (.not. \textcolor{keyword}{associated}(meke%GME\_snk)) cs%id\_GME\_snk = -1
1256   cs%id\_Le = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_Le'}, diag%axesT1, time, &
1257      \textcolor{stringliteral}{'Eddy mixing length used in the MEKE derived eddy diffusivity'}, \textcolor{stringliteral}{'m'}, conversion=us%L\_to\_m)
1258   cs%id\_Lrhines = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_Lrhines'}, diag%axesT1, time, &
1259      \textcolor{stringliteral}{'Rhines length scale used in the MEKE derived eddy diffusivity'}, \textcolor{stringliteral}{'m'}, conversion=us%L\_to\_m)
1260   cs%id\_Leady = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_Leady'}, diag%axesT1, time, &
1261      \textcolor{stringliteral}{'Eady length scale used in the MEKE derived eddy diffusivity'}, \textcolor{stringliteral}{'m'}, conversion=us%L\_to\_m)
1262   cs%id\_gamma\_b = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_gamma\_b'}, diag%axesT1, time, &
1263      \textcolor{stringliteral}{'Ratio of bottom-projected eddy velocity to column-mean eddy velocity'}, \textcolor{stringliteral}{'nondim'})
1264   cs%id\_gamma\_t = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_gamma\_t'}, diag%axesT1, time, &
1265      \textcolor{stringliteral}{'Ratio of barotropic eddy velocity to column-mean eddy velocity'}, \textcolor{stringliteral}{'nondim'})
1266 
1267   \textcolor{keywordflow}{if} (cs%kh\_flux\_enabled) \textcolor{keywordflow}{then}
1268     cs%id\_KhMEKE\_u = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'KHMEKE\_u'}, diag%axesCu1, time, &
1269      \textcolor{stringliteral}{'Zonal diffusivity of MEKE'}, \textcolor{stringliteral}{'m2 s-1'}, conversion=us%L\_to\_m**2*us%s\_to\_T)
1270     cs%id\_KhMEKE\_v = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'KHMEKE\_v'}, diag%axesCv1, time, &
1271      \textcolor{stringliteral}{'Meridional diffusivity of MEKE'}, \textcolor{stringliteral}{'m2 s-1'}, conversion=us%L\_to\_m**2*us%s\_to\_T)
1272 \textcolor{keywordflow}{  endif}
1273 
1274   \textcolor{keywordflow}{if} (cs%MEKE\_equilibrium\_restoring) \textcolor{keywordflow}{then}
1275     cs%id\_MEKE\_equilibrium = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'MEKE\_equilibrium'}, diag%axesT1, time, &
1276      \textcolor{stringliteral}{'Equilibrated Mesoscale Eddy Kinetic Energy'}, \textcolor{stringliteral}{'m2 s-2'}, conversion=us%L\_T\_to\_m\_s**2)
1277 \textcolor{keywordflow}{  endif}
1278 
1279   cs%id\_clock\_pass = cpu\_clock\_id(\textcolor{stringliteral}{'(Ocean continuity halo updates)'}, grain=clock\_routine)
1280 
1281   \textcolor{comment}{! Detect whether this instance of MEKE\_init() is at the beginning of a run}
1282   \textcolor{comment}{! or after a restart. If at the beginning, we will initialize MEKE to a local}
1283   \textcolor{comment}{! equilibrium.}
1284   cs%initialize = .not.query\_initialized(meke%MEKE, \textcolor{stringliteral}{"MEKE"}, restart\_cs)
1285   \textcolor{keywordflow}{if} (coldstart) cs%initialize = .false.
1286   \textcolor{keywordflow}{if} (cs%initialize) \textcolor{keyword}{call }mom\_error(warning, &
1287                        \textcolor{stringliteral}{"MEKE\_init: Initializing MEKE with a local equilibrium balance."})
1288 
1289   \textcolor{comment}{! Account for possible changes in dimensional scaling for variables that have been}
1290   \textcolor{comment}{! read from a restart file.}
1291   i\_t\_rescale = 1.0
1292   \textcolor{keywordflow}{if} ((us%s\_to\_T\_restart /= 0.0) .and. (us%s\_to\_T\_restart /= us%s\_to\_T)) &
1293     i\_t\_rescale = us%s\_to\_T\_restart / us%s\_to\_T
1294   l\_rescale = 1.0
1295   \textcolor{keywordflow}{if} ((us%m\_to\_L\_restart /= 0.0) .and. (us%m\_to\_L\_restart /= us%m\_to\_L)) &
1296     l\_rescale = us%m\_to\_L / us%m\_to\_L\_restart
1297 
1298   \textcolor{keywordflow}{if} (l\_rescale*i\_t\_rescale /= 1.0) \textcolor{keywordflow}{then}
1299     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%MEKE)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (query\_initialized(meke%MEKE, \textcolor{stringliteral}{"MEKE\_MEKE"}, restart\_cs)) \textcolor{keywordflow}{then}
1300       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1301         meke%MEKE(i,j) = l\_rescale*i\_t\_rescale * meke%MEKE(i,j)
1302 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
1303 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}
1304 \textcolor{keywordflow}{  endif}
1305   \textcolor{keywordflow}{if} (l\_rescale**2*i\_t\_rescale /= 1.0) \textcolor{keywordflow}{then}
1306     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Kh)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (query\_initialized(meke%Kh, \textcolor{stringliteral}{"MEKE\_Kh"}, restart\_cs)) \textcolor{keywordflow}{then}
1307       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1308         meke%Kh(i,j) = l\_rescale**2*i\_t\_rescale * meke%Kh(i,j)
1309 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
1310 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}
1311     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Ku)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (query\_initialized(meke%Ku, \textcolor{stringliteral}{"MEKE\_Ku"}, restart\_cs)) \textcolor{keywordflow}{then}
1312       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1313         meke%Ku(i,j) = l\_rescale**2*i\_t\_rescale * meke%Ku(i,j)
1314 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
1315 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}
1316     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Kh\_diff)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (query\_initialized(meke%Kh, \textcolor{stringliteral}{"MEKE\_Kh\_diff"}, restart\_cs)) \textcolor{keywordflow}{then}
1317       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1318         meke%Kh\_diff(i,j) = l\_rescale**2*i\_t\_rescale * meke%Kh\_diff(i,j)
1319 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
1320 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}
1321 \textcolor{keywordflow}{  endif}
1322   \textcolor{keywordflow}{if} (l\_rescale**4*i\_t\_rescale /= 1.0) \textcolor{keywordflow}{then}
1323     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Au)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (query\_initialized(meke%Au, \textcolor{stringliteral}{"MEKE\_Au"}, restart\_cs)) \textcolor{keywordflow}{then}
1324       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
1325         meke%Au(i,j) = l\_rescale**4*i\_t\_rescale * meke%Au(i,j)
1326 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
1327 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}
1328 \textcolor{keywordflow}{  endif}
1329 
1330   \textcolor{comment}{! Set up group passes.  In the case of a restart, these fields need a halo update now.}
1331   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%MEKE)) \textcolor{keywordflow}{then}
1332     \textcolor{keyword}{call }create\_group\_pass(cs%pass\_MEKE, meke%MEKE, g%Domain)
1333     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Kh\_diff)) \textcolor{keyword}{call }create\_group\_pass(cs%pass\_MEKE, meke%Kh\_diff, g%Domain)
1334     \textcolor{keywordflow}{if} (.not.cs%initialize) \textcolor{keyword}{call }do\_group\_pass(cs%pass\_MEKE, g%Domain)
1335 \textcolor{keywordflow}{  endif}
1336   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Kh)) \textcolor{keyword}{call }create\_group\_pass(cs%pass\_Kh, meke%Kh, g%Domain)
1337   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Ku)) \textcolor{keyword}{call }create\_group\_pass(cs%pass\_Kh, meke%Ku, g%Domain)
1338   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Au)) \textcolor{keyword}{call }create\_group\_pass(cs%pass\_Kh, meke%Au, g%Domain)
1339 
1340   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Kh) .or. \textcolor{keyword}{associated}(meke%Ku) .or. \textcolor{keyword}{associated}(meke%Au)) &
1341     \textcolor{keyword}{call }do\_group\_pass(cs%pass\_Kh, g%Domain)
1342 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__meke_a8180d5d0cacf48bcbdffead9e6a06efd}\label{namespacemom__meke_a8180d5d0cacf48bcbdffead9e6a06efd}} 
\index{mom\+\_\+meke@{mom\+\_\+meke}!meke\+\_\+lengthscales@{meke\+\_\+lengthscales}}
\index{meke\+\_\+lengthscales@{meke\+\_\+lengthscales}!mom\+\_\+meke@{mom\+\_\+meke}}
\subsubsection{\texorpdfstring{meke\+\_\+lengthscales()}{meke\_lengthscales()}}
{\footnotesize\ttfamily subroutine mom\+\_\+meke\+::meke\+\_\+lengthscales (\begin{DoxyParamCaption}\item[{type(\hyperlink{structmom__meke_1_1meke__cs}{meke\+\_\+cs}), pointer}]{CS,  }\item[{type(meke\+\_\+type), pointer}]{M\+E\+KE,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(inout)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed), intent(in)}]{S\+N\+\_\+u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb), intent(in)}]{S\+N\+\_\+v,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed), intent(in)}]{E\+KE,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed), intent(out)}]{bottom\+Fac2,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed), intent(out)}]{barotr\+Fac2,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed), intent(out)}]{Lmix\+Scale }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Calculates the eddy mixing length scale and $\gamma_b$ and $\gamma_t$ functions that are ratios of either bottom or barotropic eddy energy to the column eddy energy, respectively. See \hyperlink{namespacemom__meke_section_MEKE_equations}{M\+E\+KE equations}. 


\begin{DoxyParams}[1]{Parameters}
 & {\em cs} & M\+E\+KE control structure.\\
\hline
 & {\em meke} & M\+E\+KE data.\\
\hline
\mbox{\tt in,out}  & {\em g} & Ocean grid.\\
\hline
\mbox{\tt in}  & {\em gv} & Ocean vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em sn\+\_\+u} & Eady growth rate at u-\/points \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em sn\+\_\+v} & Eady growth rate at v-\/points \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em eke} & Eddy kinetic energy \mbox{[}L2 T-\/2 $\sim$$>$ m2 s-\/2\mbox{]}.\\
\hline
\mbox{\tt out}  & {\em bottomfac2} & gamma\+\_\+b$^\wedge$2\\
\hline
\mbox{\tt out}  & {\em barotrfac2} & gamma\+\_\+t$^\wedge$2\\
\hline
\mbox{\tt out}  & {\em lmixscale} & Eddy mixing length \mbox{[}L $\sim$$>$ m\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 839 of file M\+O\+M\+\_\+\+M\+E\+K\+E.\+F90.


\begin{DoxyCode}
839   \textcolor{keywordtype}{type}(meke\_cs),                     \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{   !< MEKE control structure.}
840   \textcolor{keywordtype}{type}(meke\_type),                   \textcolor{keywordtype}{pointer}       :: meke\textcolor{comment}{ !< MEKE data.}
841   \textcolor{keywordtype}{type}(ocean\_grid\_type),             \textcolor{keywordtype}{intent(inout)} :: g\textcolor{comment}{    !< Ocean grid.}
842   \textcolor{keywordtype}{type}(verticalgrid\_type),           \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{   !< Ocean vertical grid structure.}
843   \textcolor{keywordtype}{type}(unit\_scale\_type),             \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{   !< A dimensional unit scaling type}
844   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))}, \textcolor{keywordtype}{intent(in)}    :: sn\_u\textcolor{comment}{ !< Eady growth rate at u-points [T-1 ~> s-1].}
845   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))}, \textcolor{keywordtype}{intent(in)}    :: sn\_v\textcolor{comment}{ !< Eady growth rate at v-points [T-1 ~> s-1].}
846   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))},  \textcolor{keywordtype}{intent(in)}    :: eke\textcolor{comment}{  !< Eddy kinetic energy [L2 T-2 ~> m2 s-2].}
847   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))},  \textcolor{keywordtype}{intent(out)}   :: bottomfac2\textcolor{comment}{ !< gamma\_b^2}
848   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))},  \textcolor{keywordtype}{intent(out)}   :: barotrfac2\textcolor{comment}{ !< gamma\_t^2}
849   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))},  \textcolor{keywordtype}{intent(out)}   :: lmixscale\textcolor{comment}{ !< Eddy mixing length [L ~> m].}
850   \textcolor{comment}{! Local variables}
851   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: lrhines, leady  \textcolor{comment}{! Possible mixing length scales [L ~> m]}
852   \textcolor{keywordtype}{real} :: beta \textcolor{comment}{! Combined topograpic and planetary vorticity gradient [T-1 L-1 ~> s-1 m-1]}
853   \textcolor{keywordtype}{real} :: sn   \textcolor{comment}{! The local Eady growth rate [T-1 ~> s-1]}
854   \textcolor{keywordtype}{real} :: fath \textcolor{comment}{! Coriolis parameter at h points [T-1 ~> s-1]}
855   \textcolor{keywordtype}{real} :: beta\_topo\_x, beta\_topo\_y  \textcolor{comment}{! Topographic PV gradients in x and y [T-1 L-1 ~> s-1 m-1]}
856   \textcolor{keywordtype}{integer} :: i, j, is, ie, js, je
857 
858   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec
859 
860 \textcolor{comment}{!$OMP do}
861   \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
862     \textcolor{keywordflow}{if} (.not.cs%use\_old\_lscale) \textcolor{keywordflow}{then}
863       \textcolor{keywordflow}{if} (cs%aEady > 0.) \textcolor{keywordflow}{then}
864         sn = 0.25 * ( (sn\_u(i,j) + sn\_u(i-1,j)) + (sn\_v(i,j) + sn\_v(i,j-1)) )
865       \textcolor{keywordflow}{else}
866         sn = 0.
867 \textcolor{keywordflow}{      endif}
868       fath = 0.25* ( ( g%CoriolisBu(i,j) + g%CoriolisBu(i-1,j-1) ) + &
869                      ( g%CoriolisBu(i-1,j) + g%CoriolisBu(i,j-1) ) )  \textcolor{comment}{! Coriolis parameter at h points}
870 
871       \textcolor{comment}{! If bathyT is zero, then a division by zero FPE will be raised.  In this}
872       \textcolor{comment}{! case, we apply Adcroft's rule of reciprocals and set the term to zero.}
873       \textcolor{comment}{! Since zero-bathymetry cells are masked, this should not affect values.}
874       \textcolor{keywordflow}{if} (cs%MEKE\_topographic\_beta == 0. .or. g%bathyT(i,j) == 0.0) \textcolor{keywordflow}{then}
875         beta\_topo\_x = 0. ; beta\_topo\_y = 0.
876       \textcolor{keywordflow}{else}
877         \textcolor{comment}{!### Consider different combinations of these estimates of topographic beta, and the use}
878         \textcolor{comment}{!    of the water column thickness instead of the bathymetric depth.}
879         beta\_topo\_x = -cs%MEKE\_topographic\_beta * fath * 0.5 * ( &
880                       (g%bathyT(i+1,j)-g%bathyT(i,j)) * g%IdxCu(i,j)  &
881                  / max(g%bathyT(i+1,j),g%bathyT(i,j), gv%H\_subroundoff) &
882               +       (g%bathyT(i,j)-g%bathyT(i-1,j)) * g%IdxCu(i-1,j) &
883                  / max(g%bathyT(i,j),g%bathyT(i-1,j), gv%H\_subroundoff) )
884         beta\_topo\_y = -cs%MEKE\_topographic\_beta * fath * 0.5 * ( &
885                       (g%bathyT(i,j+1)-g%bathyT(i,j)) * g%IdyCv(i,j)  &
886                  / max(g%bathyT(i,j+1),g%bathyT(i,j), gv%H\_subroundoff) + &
887                       (g%bathyT(i,j)-g%bathyT(i,j-1)) * g%IdyCv(i,j-1) &
888                  / max(g%bathyT(i,j),g%bathyT(i,j-1), gv%H\_subroundoff) )
889 \textcolor{keywordflow}{      endif}
890       beta =  sqrt((g%dF\_dx(i,j) + beta\_topo\_x)**2 + &
891                    (g%dF\_dy(i,j) + beta\_topo\_y)**2 )
892 
893     \textcolor{keywordflow}{else}
894       beta = 0.
895 \textcolor{keywordflow}{    endif}
896     \textcolor{comment}{! Returns bottomFac2, barotrFac2 and LmixScale}
897     \textcolor{keyword}{call }meke\_lengthscales\_0d(cs, us, g%areaT(i,j), beta, g%bathyT(i,j),  &
898                               meke%Rd\_dx\_h(i,j), sn, meke%MEKE(i,j), &
899                               bottomfac2(i,j), barotrfac2(i,j), lmixscale(i,j), &
900                               lrhines(i,j), leady(i,j))
901 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo}
902   \textcolor{keywordflow}{if} (cs%id\_Lrhines>0) \textcolor{keyword}{call }post\_data(cs%id\_LRhines, lrhines, cs%diag)
903   \textcolor{keywordflow}{if} (cs%id\_Leady>0) \textcolor{keyword}{call }post\_data(cs%id\_LEady, leady, cs%diag)
904 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__meke_aed5885cde342caa59b2b9cde72a3e1e7}\label{namespacemom__meke_aed5885cde342caa59b2b9cde72a3e1e7}} 
\index{mom\+\_\+meke@{mom\+\_\+meke}!meke\+\_\+lengthscales\+\_\+0d@{meke\+\_\+lengthscales\+\_\+0d}}
\index{meke\+\_\+lengthscales\+\_\+0d@{meke\+\_\+lengthscales\+\_\+0d}!mom\+\_\+meke@{mom\+\_\+meke}}
\subsubsection{\texorpdfstring{meke\+\_\+lengthscales\+\_\+0d()}{meke\_lengthscales\_0d()}}
{\footnotesize\ttfamily subroutine mom\+\_\+meke\+::meke\+\_\+lengthscales\+\_\+0d (\begin{DoxyParamCaption}\item[{type(\hyperlink{structmom__meke_1_1meke__cs}{meke\+\_\+cs}), pointer}]{CS,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{real, intent(in)}]{area,  }\item[{real, intent(in)}]{beta,  }\item[{real, intent(in)}]{depth,  }\item[{real, intent(in)}]{Rd\+\_\+dx,  }\item[{real, intent(in)}]{SN,  }\item[{real, intent(in)}]{E\+KE,  }\item[{real, intent(out)}]{bottom\+Fac2,  }\item[{real, intent(out)}]{barotr\+Fac2,  }\item[{real, intent(out)}]{Lmix\+Scale,  }\item[{real, intent(out)}]{Lrhines,  }\item[{real, intent(out)}]{Leady }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Calculates the eddy mixing length scale and $\gamma_b$ and $\gamma_t$ functions that are ratios of either bottom or barotropic eddy energy to the column eddy energy, respectively. See \hyperlink{namespacemom__meke_section_MEKE_equations}{M\+E\+KE equations}. 


\begin{DoxyParams}[1]{Parameters}
 & {\em cs} & M\+E\+KE control structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em area} & Grid cell area \mbox{[}L2 $\sim$$>$ m2\mbox{]}\\
\hline
\mbox{\tt in}  & {\em beta} & Planetary beta = $ \nabla f$ \mbox{[}T-\/1 L-\/1 $\sim$$>$ s-\/1 m-\/1\mbox{]}\\
\hline
\mbox{\tt in}  & {\em depth} & Ocean depth \mbox{[}Z $\sim$$>$ m\mbox{]}\\
\hline
\mbox{\tt in}  & {\em rd\+\_\+dx} & Resolution Ld/dx \mbox{[}nondim\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em sn} & Eady growth rate \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em eke} & Eddy kinetic energy \mbox{[}L2 T-\/2 $\sim$$>$ m2 s-\/2\mbox{]}.\\
\hline
\mbox{\tt out}  & {\em bottomfac2} & gamma\+\_\+b$^\wedge$2\\
\hline
\mbox{\tt out}  & {\em barotrfac2} & gamma\+\_\+t$^\wedge$2\\
\hline
\mbox{\tt out}  & {\em lmixscale} & Eddy mixing length \mbox{[}L $\sim$$>$ m\mbox{]}.\\
\hline
\mbox{\tt out}  & {\em lrhines} & Rhines length scale \mbox{[}L $\sim$$>$ m\mbox{]}.\\
\hline
\mbox{\tt out}  & {\em leady} & Eady length scale \mbox{[}L $\sim$$>$ m\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 912 of file M\+O\+M\+\_\+\+M\+E\+K\+E.\+F90.


\begin{DoxyCode}
912   \textcolor{keywordtype}{type}(meke\_cs), \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{         !< MEKE control structure.}
913   \textcolor{keywordtype}{type}(unit\_scale\_type), \textcolor{keywordtype}{intent(in)} :: us\textcolor{comment}{    !< A dimensional unit scaling type}
914   \textcolor{keywordtype}{real},          \textcolor{keywordtype}{intent(in)}    :: area\textcolor{comment}{       !< Grid cell area [L2 ~> m2]}
915   \textcolor{keywordtype}{real},          \textcolor{keywordtype}{intent(in)}    :: beta\textcolor{comment}{       !< Planetary beta = \(\backslash\)f$ \(\backslash\)nabla f\(\backslash\)f$  [T-1 L-1 ~> s-1 m-1]}
916   \textcolor{keywordtype}{real},          \textcolor{keywordtype}{intent(in)}    :: depth\textcolor{comment}{      !< Ocean depth [Z ~> m]}
917   \textcolor{keywordtype}{real},          \textcolor{keywordtype}{intent(in)}    :: rd\_dx\textcolor{comment}{      !< Resolution Ld/dx [nondim].}
918   \textcolor{keywordtype}{real},          \textcolor{keywordtype}{intent(in)}    :: sn\textcolor{comment}{         !< Eady growth rate [T-1 ~> s-1].}
919   \textcolor{keywordtype}{real},          \textcolor{keywordtype}{intent(in)}    :: eke\textcolor{comment}{        !< Eddy kinetic energy [L2 T-2 ~> m2 s-2].}
920 \textcolor{comment}{!  real,          intent(in)    :: Z\_to\_L     !< A conversion factor from depth units (Z) to}
921 \textcolor{comment}{!                                             !! the units for lateral distances (L).}
922   \textcolor{keywordtype}{real},          \textcolor{keywordtype}{intent(out)}   :: bottomfac2\textcolor{comment}{ !< gamma\_b^2}
923   \textcolor{keywordtype}{real},          \textcolor{keywordtype}{intent(out)}   :: barotrfac2\textcolor{comment}{ !< gamma\_t^2}
924   \textcolor{keywordtype}{real},          \textcolor{keywordtype}{intent(out)}   :: lmixscale\textcolor{comment}{  !< Eddy mixing length [L ~> m].}
925   \textcolor{keywordtype}{real},          \textcolor{keywordtype}{intent(out)}   :: lrhines\textcolor{comment}{    !< Rhines length scale [L ~> m].}
926   \textcolor{keywordtype}{real},          \textcolor{keywordtype}{intent(out)}   :: leady\textcolor{comment}{      !< Eady length scale [L ~> m].}
927   \textcolor{comment}{! Local variables}
928   \textcolor{keywordtype}{real} :: lgrid, ldeform, lfrict \textcolor{comment}{! Length scales [L ~> m]}
929   \textcolor{keywordtype}{real} :: ue  \textcolor{comment}{! An eddy velocity [L T-1 ~> m s-1]}
930 
931   \textcolor{comment}{! Length scale for MEKE derived diffusivity}
932   lgrid = sqrt(area)               \textcolor{comment}{! Grid scale}
933   ldeform = lgrid * rd\_dx          \textcolor{comment}{! Deformation scale}
934   lfrict = (us%Z\_to\_L * depth) / cs%cdrag  \textcolor{comment}{! Frictional arrest scale}
935   \textcolor{comment}{! gamma\_b^2 is the ratio of bottom eddy energy to mean column eddy energy}
936   \textcolor{comment}{! used in calculating bottom drag}
937   bottomfac2 = cs%MEKE\_CD\_SCALE**2
938   \textcolor{keywordflow}{if} (lfrict*cs%MEKE\_Cb>0.) bottomfac2 = bottomfac2 + 1./( 1. + cs%MEKE\_Cb*(ldeform/lfrict) )**0.8
939   bottomfac2 = max(bottomfac2, cs%MEKE\_min\_gamma)
940   \textcolor{comment}{! gamma\_t^2 is the ratio of barotropic eddy energy to mean column eddy energy}
941   \textcolor{comment}{! used in the velocity scale for diffusivity}
942   barotrfac2 = 1.
943   \textcolor{keywordflow}{if} (lfrict*cs%MEKE\_Ct>0.) barotrfac2 = 1. / ( 1. + cs%MEKE\_Ct*(ldeform/lfrict) )**0.25
944   barotrfac2 = max(barotrfac2, cs%MEKE\_min\_gamma)
945   \textcolor{keywordflow}{if} (cs%use\_old\_lscale) \textcolor{keywordflow}{then}
946     \textcolor{keywordflow}{if} (cs%Rd\_as\_max\_scale) \textcolor{keywordflow}{then}
947       lmixscale = min(ldeform, lgrid) \textcolor{comment}{! The smaller of Ld or dx}
948     \textcolor{keywordflow}{else}
949       lmixscale = lgrid
950 \textcolor{keywordflow}{    endif}
951   \textcolor{keywordflow}{else}
952     ue = sqrt( 2.0 * max( 0., barotrfac2*eke ) ) \textcolor{comment}{! Barotropic eddy flow scale}
953     lrhines = sqrt( ue / max( beta, 1.e-30*us%T\_to\_s*us%L\_to\_m ) )       \textcolor{comment}{! Rhines scale}
954     \textcolor{keywordflow}{if} (cs%aEady > 0.) \textcolor{keywordflow}{then}
955       leady = ue / max( sn, 1.e-15*us%T\_to\_s ) \textcolor{comment}{! Bound Eady time-scale < 1e15 seconds}
956     \textcolor{keywordflow}{else}
957       leady = 0.
958 \textcolor{keywordflow}{    endif}
959     \textcolor{keywordflow}{if} (cs%use\_min\_lscale) \textcolor{keywordflow}{then}
960       lmixscale = 1.e7
961       \textcolor{keywordflow}{if} (cs%aDeform*ldeform > 0.) lmixscale = min(lmixscale,cs%aDeform*ldeform)
962       \textcolor{keywordflow}{if} (cs%aFrict *lfrict  > 0.) lmixscale = min(lmixscale,cs%aFrict *lfrict)
963       \textcolor{keywordflow}{if} (cs%aRhines*lrhines > 0.) lmixscale = min(lmixscale,cs%aRhines*lrhines)
964       \textcolor{keywordflow}{if} (cs%aEady  *leady   > 0.) lmixscale = min(lmixscale,cs%aEady  *leady)
965       \textcolor{keywordflow}{if} (cs%aGrid  *lgrid   > 0.) lmixscale = min(lmixscale,cs%aGrid  *lgrid)
966       \textcolor{keywordflow}{if} (cs%Lfixed          > 0.) lmixscale = min(lmixscale,cs%Lfixed)
967     \textcolor{keywordflow}{else}
968       lmixscale = 0.
969       \textcolor{keywordflow}{if} (cs%aDeform*ldeform > 0.) lmixscale = lmixscale + 1./(cs%aDeform*ldeform)
970       \textcolor{keywordflow}{if} (cs%aFrict *lfrict  > 0.) lmixscale = lmixscale + 1./(cs%aFrict *lfrict)
971       \textcolor{keywordflow}{if} (cs%aRhines*lrhines > 0.) lmixscale = lmixscale + 1./(cs%aRhines*lrhines)
972       \textcolor{keywordflow}{if} (cs%aEady  *leady   > 0.) lmixscale = lmixscale + 1./(cs%aEady  *leady)
973       \textcolor{keywordflow}{if} (cs%aGrid  *lgrid   > 0.) lmixscale = lmixscale + 1./(cs%aGrid  *lgrid)
974       \textcolor{keywordflow}{if} (cs%Lfixed          > 0.) lmixscale = lmixscale + 1./cs%Lfixed
975       \textcolor{keywordflow}{if} (lmixscale > 0.) lmixscale = 1. / lmixscale
976 \textcolor{keywordflow}{    endif}
977 \textcolor{keywordflow}{  endif}
978 
\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__meke_a5f752f097ddeba7071e1703110e51bc2}\label{namespacemom__meke_a5f752f097ddeba7071e1703110e51bc2}} 
\index{mom\+\_\+meke@{mom\+\_\+meke}!step\+\_\+forward\+\_\+meke@{step\+\_\+forward\+\_\+meke}}
\index{step\+\_\+forward\+\_\+meke@{step\+\_\+forward\+\_\+meke}!mom\+\_\+meke@{mom\+\_\+meke}}
\subsubsection{\texorpdfstring{step\+\_\+forward\+\_\+meke()}{step\_forward\_meke()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+meke\+::step\+\_\+forward\+\_\+meke (\begin{DoxyParamCaption}\item[{type(meke\+\_\+type), pointer}]{M\+E\+KE,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{h,  }\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed), intent(in)}]{S\+N\+\_\+u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb), intent(in)}]{S\+N\+\_\+v,  }\item[{type(vertvisc\+\_\+type), intent(in)}]{visc,  }\item[{real, intent(in)}]{dt,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(inout)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\hyperlink{structmom__meke_1_1meke__cs}{meke\+\_\+cs}), pointer}]{CS,  }\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed, g \%ke), intent(in)}]{hu,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb, g \%ke), intent(in)}]{hv }\end{DoxyParamCaption})}



Integrates forward-\/in-\/time the M\+E\+KE eddy energy equation. See \hyperlink{namespacemom__meke_section_MEKE_equations}{M\+E\+KE equations}. 


\begin{DoxyParams}[1]{Parameters}
 & {\em meke} & M\+E\+KE data.\\
\hline
\mbox{\tt in,out}  & {\em g} & Ocean grid.\\
\hline
\mbox{\tt in}  & {\em gv} & Ocean vertical grid structure.\\
\hline
\mbox{\tt in}  & {\em us} & A dimensional unit scaling type\\
\hline
\mbox{\tt in}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em sn\+\_\+u} & Eady growth rate at u-\/points \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em sn\+\_\+v} & Eady growth rate at v-\/points \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em visc} & The vertical viscosity type.\\
\hline
\mbox{\tt in}  & {\em dt} & Model(baroclinic) time-\/step \mbox{[}T $\sim$$>$ s\mbox{]}.\\
\hline
 & {\em cs} & M\+E\+KE control structure.\\
\hline
\mbox{\tt in}  & {\em hu} & Accumlated zonal mass flux \mbox{[}H L2 $\sim$$>$ m3 or kg\mbox{]}.\\
\hline
\mbox{\tt in}  & {\em hv} & Accumlated meridional mass flux \mbox{[}H L2 $\sim$$>$ m3 or kg\mbox{]} \\
\hline
\end{DoxyParams}


Definition at line 112 of file M\+O\+M\+\_\+\+M\+E\+K\+E.\+F90.


\begin{DoxyCode}
112   \textcolor{keywordtype}{type}(meke\_type),                          \textcolor{keywordtype}{pointer}       :: meke\textcolor{comment}{ !< MEKE data.}
113   \textcolor{keywordtype}{type}(ocean\_grid\_type),                    \textcolor{keywordtype}{intent(inout)} :: g\textcolor{comment}{    !< Ocean grid.}
114   \textcolor{keywordtype}{type}(verticalgrid\_type),                  \textcolor{keywordtype}{intent(in)}    :: gv\textcolor{comment}{   !< Ocean vertical grid structure.}
115   \textcolor{keywordtype}{type}(unit\_scale\_type),                    \textcolor{keywordtype}{intent(in)}    :: us\textcolor{comment}{   !< A dimensional unit scaling type}
116   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{    !< Layer thickness [H ~> m or kg m-2].}
117   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))},        \textcolor{keywordtype}{intent(in)}    :: sn\_u\textcolor{comment}{ !< Eady growth rate at u-points [T-1 ~>
       s-1].}
118   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))},        \textcolor{keywordtype}{intent(in)}    :: sn\_v\textcolor{comment}{ !< Eady growth rate at v-points [T-1 ~>
       s-1].}
119   \textcolor{keywordtype}{type}(vertvisc\_type),                      \textcolor{keywordtype}{intent(in)}    :: visc\textcolor{comment}{ !< The vertical viscosity type.}
120   \textcolor{keywordtype}{real},                                     \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{   !< Model(baroclinic) time-step [T ~> s].}
121   \textcolor{keywordtype}{type}(meke\_cs),                            \textcolor{keywordtype}{pointer}       :: cs\textcolor{comment}{   !< MEKE control structure.}
122   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}   :: hu\textcolor{comment}{   !< Accumlated zonal mass flux [H L2 ~> m3
       or kg].}
123   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(G))}, \textcolor{keywordtype}{intent(in)}   :: hv\textcolor{comment}{   !< Accumlated meridional mass flux [H L2
       ~> m3 or kg]}
124 
125   \textcolor{comment}{! Local variables}
126   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G))} :: &
127     mass, &         \textcolor{comment}{! The total mass of the water column [R Z ~> kg m-2].}
128     i\_mass, &       \textcolor{comment}{! The inverse of mass [R-1 Z-1 ~> m2 kg-1].}
129     src, &          \textcolor{comment}{! The sum of all MEKE sources [L2 T-3 ~> W kg-1] (= m2 s-3).}
130     meke\_decay, &   \textcolor{comment}{! A diagnostic of the MEKE decay timescale [T-1 ~> s-1].}
131     drag\_rate\_visc, & \textcolor{comment}{! Near-bottom velocity contribution to bottom dratg [L T-1 ~> m s-1]}
132     drag\_rate, &    \textcolor{comment}{! The MEKE spindown timescale due to bottom drag [T-1 ~> s-1].}
133     drag\_rate\_j15, &  \textcolor{comment}{! The MEKE spindown timescale due to bottom drag with the Jansen 2015 scheme.}
134                     \textcolor{comment}{! Unfortunately, as written the units seem inconsistent. [T-1 ~> s-1].}
135     del2meke, &     \textcolor{comment}{! Laplacian of MEKE, used for bi-harmonic diffusion [T-2 ~> s-2].}
136     del4meke, &     \textcolor{comment}{! Time-integrated MEKE tendency arising from the biharmonic of MEKE [L2 T-2 ~> m2 s-2].}
137     lmixscale, &    \textcolor{comment}{! Eddy mixing length [L ~> m].}
138     barotrfac2, &   \textcolor{comment}{! Ratio of EKE\_barotropic / EKE [nondim]}
139     bottomfac2, &   \textcolor{comment}{! Ratio of EKE\_bottom / EKE [nondim]}
140     tmp             \textcolor{comment}{! Temporary variable for diagnostic computation}
141 
142   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))} :: &
143     meke\_uflux, &   \textcolor{comment}{! The zonal advective and diffusive flux of MEKE with units of [R Z L4 T-3 ~> kg m-2
       s-3].}
144                     \textcolor{comment}{! In one place, MEKE\_uflux is used as temporary work space with units of [L2 T-2 ~> m2
       s-2].}
145     kh\_u, &         \textcolor{comment}{! The zonal diffusivity that is actually used [L2 T-1 ~> m2 s-1].}
146     barohu, &       \textcolor{comment}{! Depth integrated accumulated zonal mass flux [R Z L2 ~> kg].}
147     drag\_vel\_u      \textcolor{comment}{! A (vertical) viscosity associated with bottom drag at}
148                     \textcolor{comment}{! u-points [Z T-1 ~> m s-1].}
149   \textcolor{keywordtype}{real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))} :: &
150     meke\_vflux, &   \textcolor{comment}{! The meridional advective and diffusive flux of MEKE with units of [R Z L4 T-3 ~> kg
       m-2 s-3].}
151                     \textcolor{comment}{! In one place, MEKE\_vflux is used as temporary work space with units of [L2 T-2 ~> m2
       s-2].}
152     kh\_v, &         \textcolor{comment}{! The meridional diffusivity that is actually used [L2 T-1 ~> m2 s-1].}
153     barohv, &       \textcolor{comment}{! Depth integrated accumulated meridional mass flux [R Z L2 ~> kg].}
154     drag\_vel\_v      \textcolor{comment}{! A (vertical) viscosity associated with bottom drag at}
155                     \textcolor{comment}{! v-points [Z T-1 ~> m s-1].}
156   \textcolor{keywordtype}{real} :: kh\_here   \textcolor{comment}{! The local horizontal viscosity [L2 T-1 ~> m2 s-1]}
157   \textcolor{keywordtype}{real} :: inv\_kh\_max \textcolor{comment}{! The inverse of the local horizontal viscosity [T L-2 ~> s m-2]}
158   \textcolor{keywordtype}{real} :: k4\_here   \textcolor{comment}{! The local horizontal biharmonic viscosity [L4 T-1 ~> m4 s-1]}
159   \textcolor{keywordtype}{real} :: inv\_k4\_max \textcolor{comment}{! The inverse of the local horizontal biharmonic viscosity [T L-4 ~> s m-4]}
160   \textcolor{keywordtype}{real} :: cdrag2
161   \textcolor{keywordtype}{real} :: advfac    \textcolor{comment}{! The product of the advection scaling factor and 1/dt [T-1 ~> s-1]}
162   \textcolor{keywordtype}{real} :: mass\_neglect \textcolor{comment}{! A negligible mass [R Z ~> kg m-2].}
163   \textcolor{keywordtype}{real} :: ldamping  \textcolor{comment}{! The MEKE damping rate [T-1 ~> s-1].}
164   \textcolor{keywordtype}{real} :: rho0      \textcolor{comment}{! A density used to convert mass to distance [R ~> kg m-3].}
165   \textcolor{keywordtype}{real} :: sdt       \textcolor{comment}{! dt to use locally [T ~> s] (could be scaled to accelerate)}
166   \textcolor{keywordtype}{real} :: sdt\_damp  \textcolor{comment}{! dt for damping [T ~> s] (sdt could be split).}
167   \textcolor{keywordtype}{logical} :: use\_drag\_rate \textcolor{comment}{! Flag to indicate drag\_rate is finite}
168   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, isq, ieq, jsq, jeq, nz
169 
170   is = g%isc ; ie = g%iec ; js = g%jsc ; je = g%jec ; nz = g%ke
171   isq = g%IscB ; ieq = g%IecB ; jsq = g%JscB ; jeq = g%JecB
172 
173   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal, &
174          \textcolor{stringliteral}{"MOM\_MEKE: Module must be initialized before it is used."})
175   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(meke)) \textcolor{keyword}{call }mom\_error(fatal, &
176          \textcolor{stringliteral}{"MOM\_MEKE: MEKE must be initialized before it is used."})
177 
178   \textcolor{keywordflow}{if} ((cs%MEKE\_damping > 0.0) .or. (cs%MEKE\_Cd\_scale > 0.0) .or. (cs%MEKE\_Cb>0.) &
179       .or. cs%visc\_drag) \textcolor{keywordflow}{then}
180     use\_drag\_rate = .true.
181   \textcolor{keywordflow}{else}
182     use\_drag\_rate = .false.
183 \textcolor{keywordflow}{  endif}
184 
185   \textcolor{comment}{! Only integrate the MEKE equations if MEKE is required.}
186   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(meke%MEKE)) \textcolor{keywordflow}{then}
187 \textcolor{comment}{!   call MOM\_error(FATAL, "MOM\_MEKE: MEKE%MEKE is not associated!")}
188     \textcolor{keywordflow}{return}
189 \textcolor{keywordflow}{  endif}
190 
191     \textcolor{keywordflow}{if} (cs%debug) \textcolor{keywordflow}{then}
192       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%mom\_src)) &
193         \textcolor{keyword}{call }hchksum(meke%mom\_src, \textcolor{stringliteral}{'MEKE mom\_src'}, g%HI, scale=us%RZ3\_T3\_to\_W\_m2*us%L\_to\_Z**2)
194       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%GME\_snk)) &
195         \textcolor{keyword}{call }hchksum(meke%GME\_snk, \textcolor{stringliteral}{'MEKE GME\_snk'}, g%HI, scale=us%RZ3\_T3\_to\_W\_m2*us%L\_to\_Z**2)
196       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%GM\_src)) &
197         \textcolor{keyword}{call }hchksum(meke%GM\_src, \textcolor{stringliteral}{'MEKE GM\_src'}, g%HI, scale=us%RZ3\_T3\_to\_W\_m2*us%L\_to\_Z**2)
198       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%MEKE)) \textcolor{keyword}{call }hchksum(meke%MEKE, \textcolor{stringliteral}{'MEKE MEKE'}, g%HI, scale=us%L\_T\_to\_m\_s**2)
199       \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"MEKE SN\_[uv]"}, sn\_u, sn\_v, g%HI, scale=us%s\_to\_T, &
200                     scalar\_pair=.true.)
201       \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"MEKE h[uv]"}, hu, hv, g%HI, haloshift=1, &
202                     scale=gv%H\_to\_m*(us%L\_to\_m**2))
203 \textcolor{keywordflow}{    endif}
204 
205     sdt = dt*cs%MEKE\_dtScale \textcolor{comment}{! Scaled dt to use for time-stepping}
206     rho0 = gv%Rho0
207     mass\_neglect = gv%H\_to\_RZ * gv%H\_subroundoff
208     cdrag2 = cs%cdrag**2
209 
210     \textcolor{comment}{! With a depth-dependent (and possibly strong) damping, it seems}
211     \textcolor{comment}{! advisable to use Strang splitting between the damping and diffusion.}
212     sdt\_damp = sdt ; \textcolor{keywordflow}{if} (cs%MEKE\_KH >= 0.0 .or. cs%MEKE\_K4 >= 0.) sdt\_damp = 0.5*sdt
213 
214     \textcolor{comment}{! Calculate depth integrated mass exchange if doing advection [R Z L2 ~> kg]}
215     \textcolor{keywordflow}{if} (cs%MEKE\_advection\_factor>0.) \textcolor{keywordflow}{then}
216       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-1,ie
217         barohu(i,j) = 0.
218 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
219       \textcolor{keywordflow}{do} k=1,nz
220         \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-1,ie
221           barohu(i,j) = hu(i,j,k) * gv%H\_to\_RZ
222 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}
223 \textcolor{keywordflow}{      enddo}
224       \textcolor{keywordflow}{do} j=js-1,je ; \textcolor{keywordflow}{do} i=is,ie
225         barohv(i,j) = 0.
226 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
227       \textcolor{keywordflow}{do} k=1,nz
228         \textcolor{keywordflow}{do} j=js-1,je ; \textcolor{keywordflow}{do} i=is,ie
229           barohv(i,j) = hv(i,j,k) * gv%H\_to\_RZ
230 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}
231 \textcolor{keywordflow}{      enddo}
232 \textcolor{keywordflow}{    endif}
233 
234     \textcolor{keywordflow}{if} (cs%MEKE\_Cd\_scale == 0.0 .and. .not. cs%visc\_drag) \textcolor{keywordflow}{then}
235       \textcolor{comment}{!$OMP parallel do default(shared) private(ldamping)}
236       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
237         drag\_rate(i,j) = 0. ; drag\_rate\_j15(i,j) = 0.
238 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
239 \textcolor{keywordflow}{    endif}
240 
241     \textcolor{comment}{! Calculate drag\_rate\_visc(i,j) which accounts for the model bottom mean flow}
242     \textcolor{keywordflow}{if} (cs%visc\_drag) \textcolor{keywordflow}{then}
243       \textcolor{comment}{!$OMP parallel do default(shared)}
244       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-1,ie
245         drag\_vel\_u(i,j) = 0.0
246         \textcolor{keywordflow}{if} ((g%mask2dCu(i,j) > 0.0) .and. (visc%bbl\_thick\_u(i,j) > 0.0)) &
247           drag\_vel\_u(i,j) = visc%Kv\_bbl\_u(i,j) / visc%bbl\_thick\_u(i,j)
248 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
249       \textcolor{comment}{!$OMP parallel do default(shared)}
250       \textcolor{keywordflow}{do} j=js-1,je ; \textcolor{keywordflow}{do} i=is,ie
251         drag\_vel\_v(i,j) = 0.0
252         \textcolor{keywordflow}{if} ((g%mask2dCv(i,j) > 0.0) .and. (visc%bbl\_thick\_v(i,j) > 0.0)) &
253           drag\_vel\_v(i,j) = visc%Kv\_bbl\_v(i,j) / visc%bbl\_thick\_v(i,j)
254 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
255 
256       \textcolor{comment}{!$OMP parallel do default(shared)}
257       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
258         drag\_rate\_visc(i,j) = (0.25*g%IareaT(i,j) * us%Z\_to\_L * &
259                 ((g%areaCu(i-1,j)*drag\_vel\_u(i-1,j) + &
260                   g%areaCu(i,j)*drag\_vel\_u(i,j)) + &
261                  (g%areaCv(i,j-1)*drag\_vel\_v(i,j-1) + &
262                   g%areaCv(i,j)*drag\_vel\_v(i,j)) ) )
263 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
264     \textcolor{keywordflow}{else}
265       \textcolor{comment}{!$OMP parallel do default(shared)}
266       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
267         drag\_rate\_visc(i,j) = 0.
268 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
269 \textcolor{keywordflow}{    endif}
270 
271     \textcolor{comment}{!$OMP parallel do default(shared)}
272     \textcolor{keywordflow}{do} j=js-1,je+1
273       \textcolor{keywordflow}{do} i=is-1,ie+1 ; mass(i,j) = 0.0 ;\textcolor{keywordflow}{ enddo}
274       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is-1,ie+1
275         mass(i,j) = mass(i,j) + g%mask2dT(i,j) * (gv%H\_to\_RZ * h(i,j,k)) \textcolor{comment}{! [R Z ~> kg m-2]}
276 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
277       \textcolor{keywordflow}{do} i=is-1,ie+1
278         i\_mass(i,j) = 0.0
279         \textcolor{keywordflow}{if} (mass(i,j) > 0.0) i\_mass(i,j) = 1.0 / mass(i,j) \textcolor{comment}{! [R-1 Z-1 ~> m2 kg-1]}
280 \textcolor{keywordflow}{      enddo}
281 \textcolor{keywordflow}{    enddo}
282 
283     \textcolor{keywordflow}{if} (cs%initialize) \textcolor{keywordflow}{then}
284       \textcolor{keyword}{call }meke\_equilibrium(cs, meke, g, gv, us, sn\_u, sn\_v, drag\_rate\_visc, i\_mass)
285       cs%initialize = .false.
286 \textcolor{keywordflow}{    endif}
287 
288     \textcolor{comment}{! Calculates bottomFac2, barotrFac2 and LmixScale}
289     \textcolor{keyword}{call }meke\_lengthscales(cs, meke, g, gv, us, sn\_u, sn\_v, meke%MEKE, bottomfac2, barotrfac2, lmixscale)
290     \textcolor{keywordflow}{if} (cs%debug) \textcolor{keywordflow}{then}
291       \textcolor{keywordflow}{if} (cs%visc\_drag) &
292         \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"MEKE drag\_vel\_[uv]"}, drag\_vel\_u, drag\_vel\_v, g%HI, &
293                       scale=us%Z\_to\_m*us%s\_to\_T, scalar\_pair=.true.)
294       \textcolor{keyword}{call }hchksum(mass, \textcolor{stringliteral}{'MEKE mass'},g%HI,haloshift=1, scale=us%RZ\_to\_kg\_m2)
295       \textcolor{keyword}{call }hchksum(drag\_rate\_visc, \textcolor{stringliteral}{'MEKE drag\_rate\_visc'}, g%HI, scale=us%L\_T\_to\_m\_s)
296       \textcolor{keyword}{call }hchksum(bottomfac2, \textcolor{stringliteral}{'MEKE bottomFac2'}, g%HI)
297       \textcolor{keyword}{call }hchksum(barotrfac2, \textcolor{stringliteral}{'MEKE barotrFac2'}, g%HI)
298       \textcolor{keyword}{call }hchksum(lmixscale, \textcolor{stringliteral}{'MEKE LmixScale'}, g%HI,scale=us%L\_to\_m)
299 \textcolor{keywordflow}{    endif}
300 
301     \textcolor{comment}{! Aggregate sources of MEKE (background, frictional and GM)}
302     \textcolor{comment}{!$OMP parallel do default(shared)}
303     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
304       src(i,j) = cs%MEKE\_BGsrc
305 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
306 
307     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%mom\_src)) \textcolor{keywordflow}{then}
308       \textcolor{comment}{!$OMP parallel do default(shared)}
309       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
310         src(i,j) = src(i,j) - cs%MEKE\_FrCoeff*i\_mass(i,j)*meke%mom\_src(i,j)
311 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
312 \textcolor{keywordflow}{    endif}
313 
314     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%GME\_snk)) \textcolor{keywordflow}{then}
315       \textcolor{comment}{!$OMP parallel do default(shared)}
316       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
317         src(i,j) = src(i,j) - cs%MEKE\_GMECoeff*i\_mass(i,j)*meke%GME\_snk(i,j)
318 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
319 \textcolor{keywordflow}{    endif}
320 
321     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%GM\_src)) \textcolor{keywordflow}{then}
322       \textcolor{keywordflow}{if} (cs%GM\_src\_alt) \textcolor{keywordflow}{then}
323         \textcolor{comment}{!$OMP parallel do default(shared)}
324         \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
325           src(i,j) = src(i,j) - cs%MEKE\_GMcoeff*meke%GM\_src(i,j) / &
326                      (gv%Rho0 * max(1.0*us%m\_to\_Z, g%bathyT(i,j)))
327 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}
328       \textcolor{keywordflow}{else}
329         \textcolor{comment}{!$OMP parallel do default(shared)}
330         \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
331           src(i,j) = src(i,j) - cs%MEKE\_GMcoeff*i\_mass(i,j)*meke%GM\_src(i,j)
332 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}
333 \textcolor{keywordflow}{      endif}
334 \textcolor{keywordflow}{    endif}
335 
336     \textcolor{keywordflow}{if} (cs%MEKE\_equilibrium\_restoring) \textcolor{keywordflow}{then}
337       \textcolor{keyword}{call }meke\_equilibrium\_restoring(cs, g, us, sn\_u, sn\_v)
338       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
339         src(i,j) = src(i,j) - cs%MEKE\_restoring\_rate*(meke%MEKE(i,j) - cs%equilibrium\_value(i,j))
340 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
341 \textcolor{keywordflow}{    endif}
342 
343     \textcolor{comment}{! Increase EKE by a full time-steps worth of source}
344     \textcolor{comment}{!$OMP parallel do default(shared)}
345     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
346       meke%MEKE(i,j) = (meke%MEKE(i,j) + sdt*src(i,j))*g%mask2dT(i,j)
347 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
348 
349     \textcolor{keywordflow}{if} (use\_drag\_rate) \textcolor{keywordflow}{then}
350       \textcolor{comment}{! Calculate a viscous drag rate (includes BBL contributions from mean flow and eddies)}
351       \textcolor{comment}{!$OMP parallel do default(shared)}
352       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
353         drag\_rate(i,j) = (us%L\_to\_Z*rho0 * i\_mass(i,j)) * sqrt( drag\_rate\_visc(i,j)**2 + &
354                  cdrag2 * ( max(0.0, 2.0*bottomfac2(i,j)*meke%MEKE(i,j)) + cs%MEKE\_Uscale**2 ) )
355 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
356 \textcolor{keywordflow}{    endif}
357 
358     \textcolor{comment}{! First stage of Strang splitting}
359     \textcolor{comment}{!$OMP parallel do default(shared)}
360     \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
361       ldamping = cs%MEKE\_damping + drag\_rate(i,j) * bottomfac2(i,j)
362       \textcolor{keywordflow}{if} (meke%MEKE(i,j) < 0.) ldamping = 0.
363       \textcolor{comment}{! notice that the above line ensures a damping only if MEKE is positive,}
364       \textcolor{comment}{! while leaving MEKE unchanged if it is negative}
365       meke%MEKE(i,j) =  meke%MEKE(i,j) / (1.0 + sdt\_damp*ldamping)
366       meke\_decay(i,j) = ldamping*g%mask2dT(i,j)
367 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}
368 
369     \textcolor{keywordflow}{if} (cs%kh\_flux\_enabled .or. cs%MEKE\_K4 >= 0.0) \textcolor{keywordflow}{then}
370       \textcolor{comment}{! Update MEKE in the halos for lateral or bi-harmonic diffusion}
371       \textcolor{keyword}{call }cpu\_clock\_begin(cs%id\_clock\_pass)
372       \textcolor{keyword}{call }do\_group\_pass(cs%pass\_MEKE, g%Domain)
373       \textcolor{keyword}{call }cpu\_clock\_end(cs%id\_clock\_pass)
374 \textcolor{keywordflow}{    endif}
375 
376     \textcolor{keywordflow}{if} (cs%MEKE\_K4 >= 0.0) \textcolor{keywordflow}{then}
377       \textcolor{comment}{! Calculate Laplacian of MEKE using MEKE\_uflux and MEKE\_vflux as temporary work space.}
378       \textcolor{comment}{!$OMP parallel do default(shared)}
379       \textcolor{keywordflow}{do} j=js-1,je+1 ; \textcolor{keywordflow}{do} i=is-2,ie+1
380         \textcolor{comment}{! MEKE\_uflux is used here as workspace with units of [L2 T-2 ~> m2 s-2].}
381         meke\_uflux(i,j) = ((g%dy\_Cu(i,j)*g%IdxCu(i,j)) * g%mask2dCu(i,j)) * &
382             (meke%MEKE(i+1,j) - meke%MEKE(i,j))
383       \textcolor{comment}{! This would have units of [R Z L2 T-2 ~> kg s-2]}
384       \textcolor{comment}{! MEKE\_uflux(I,j) = ((G%dy\_Cu(I,j)*G%IdxCu(I,j)) * &}
385       \textcolor{comment}{!     ((2.0*mass(i,j)*mass(i+1,j)) / ((mass(i,j)+mass(i+1,j)) + mass\_neglect)) ) * &}
386       \textcolor{comment}{!     (MEKE%MEKE(i+1,j) - MEKE%MEKE(i,j))}
387 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
388       \textcolor{comment}{!$OMP parallel do default(shared)}
389       \textcolor{keywordflow}{do} j=js-2,je+1 ; \textcolor{keywordflow}{do} i=is-1,ie+1
390         \textcolor{comment}{! MEKE\_vflux is used here as workspace with units of [L2 T-2 ~> m2 s-2].}
391         meke\_vflux(i,j) = ((g%dx\_Cv(i,j)*g%IdyCv(i,j)) * g%mask2dCv(i,j)) * &
392             (meke%MEKE(i,j+1) - meke%MEKE(i,j))
393       \textcolor{comment}{! This would have units of [R Z L2 T-2 ~> kg s-2]}
394       \textcolor{comment}{! MEKE\_vflux(i,J) = ((G%dx\_Cv(i,J)*G%IdyCv(i,J)) * &}
395       \textcolor{comment}{!     ((2.0*mass(i,j)*mass(i,j+1)) / ((mass(i,j)+mass(i,j+1)) + mass\_neglect)) ) * &}
396       \textcolor{comment}{!     (MEKE%MEKE(i,j+1) - MEKE%MEKE(i,j))}
397 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
398 
399       \textcolor{comment}{!$OMP parallel do default(shared)}
400       \textcolor{keywordflow}{do} j=js-1,je+1 ; \textcolor{keywordflow}{do} i=is-1,ie+1 \textcolor{comment}{! del2MEKE has units [T-2 ~> s-2].}
401         del2meke(i,j) = g%IareaT(i,j) * &
402             ((meke\_uflux(i,j) - meke\_uflux(i-1,j)) + (meke\_vflux(i,j) - meke\_vflux(i,j-1)))
403 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
404 
405       \textcolor{comment}{! Bi-harmonic diffusion of MEKE}
406       \textcolor{comment}{!$OMP parallel do default(shared) private(K4\_here,Inv\_K4\_max)}
407       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-1,ie
408         k4\_here = cs%MEKE\_K4 \textcolor{comment}{! [L4 T-1 ~> m4 s-1]}
409         \textcolor{comment}{! Limit Kh to avoid CFL violations.}
410         inv\_k4\_max = 64.0 * sdt * ((g%dy\_Cu(i,j)*g%IdxCu(i,j)) * &
411                      max(g%IareaT(i,j), g%IareaT(i+1,j)))**2
412         \textcolor{keywordflow}{if} (k4\_here*inv\_k4\_max > 0.3) k4\_here = 0.3 / inv\_k4\_max
413 
414         \textcolor{comment}{! Here the units of MEKE\_uflux are [R Z L4 T-3 ~> kg m2 s-3].}
415         meke\_uflux(i,j) = ((k4\_here * (g%dy\_Cu(i,j)*g%IdxCu(i,j))) * &
416             ((2.0*mass(i,j)*mass(i+1,j)) / ((mass(i,j)+mass(i+1,j)) + mass\_neglect)) ) * &
417             (del2meke(i+1,j) - del2meke(i,j))
418 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
419       \textcolor{comment}{!$OMP parallel do default(shared) private(K4\_here,Inv\_K4\_max)}
420       \textcolor{keywordflow}{do} j=js-1,je ; \textcolor{keywordflow}{do} i=is,ie
421         k4\_here = cs%MEKE\_K4 \textcolor{comment}{! [L4 T-1 ~> m4 s-1]}
422         inv\_k4\_max = 64.0 * sdt * ((g%dx\_Cv(i,j)*g%IdyCv(i,j)) * max(g%IareaT(i,j), g%IareaT(i,j+1)))**2
423         \textcolor{keywordflow}{if} (k4\_here*inv\_k4\_max > 0.3) k4\_here = 0.3 / inv\_k4\_max
424 
425         \textcolor{comment}{! Here the units of MEKE\_vflux are [R Z L4 T-3 ~> kg m2 s-3].}
426         meke\_vflux(i,j) = ((k4\_here * (g%dx\_Cv(i,j)*g%IdyCv(i,j))) * &
427             ((2.0*mass(i,j)*mass(i,j+1)) / ((mass(i,j)+mass(i,j+1)) + mass\_neglect)) ) * &
428             (del2meke(i,j+1) - del2meke(i,j))
429 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
430       \textcolor{comment}{! Store change in MEKE arising from the bi-harmonic in del4MEKE [L2 T-2 ~> m2 s-2].}
431       \textcolor{comment}{!$OMP parallel do default(shared)}
432       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
433         del4meke(i,j) = (sdt*(g%IareaT(i,j)*i\_mass(i,j))) * &
434             ((meke\_uflux(i-1,j) - meke\_uflux(i,j)) + &
435              (meke\_vflux(i,j-1) - meke\_vflux(i,j)))
436 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
437 \textcolor{keywordflow}{    endif} \textcolor{comment}{!}
438 
439     \textcolor{keywordflow}{if} (cs%kh\_flux\_enabled) \textcolor{keywordflow}{then}
440       \textcolor{comment}{! Lateral diffusion of MEKE}
441       kh\_here = max(0., cs%MEKE\_Kh)
442       \textcolor{comment}{!$OMP parallel do default(shared) firstprivate(Kh\_here) private(Inv\_Kh\_max)}
443       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-1,ie
444         \textcolor{comment}{! Limit Kh to avoid CFL violations.}
445         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Kh)) &
446           kh\_here = max(0., cs%MEKE\_Kh) + &
447               cs%KhMEKE\_Fac*0.5*(meke%Kh(i,j)+meke%Kh(i+1,j))
448         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Kh\_diff)) &
449           kh\_here = max(0.,cs%MEKE\_Kh) + &
450               cs%KhMEKE\_Fac*0.5*(meke%Kh\_diff(i,j)+meke%Kh\_diff(i+1,j))
451         inv\_kh\_max = 2.0*sdt * ((g%dy\_Cu(i,j)*g%IdxCu(i,j)) * &
452                      max(g%IareaT(i,j),g%IareaT(i+1,j)))
453         \textcolor{keywordflow}{if} (kh\_here*inv\_kh\_max > 0.25) kh\_here = 0.25 / inv\_kh\_max
454         kh\_u(i,j) = kh\_here
455 
456         \textcolor{comment}{! Here the units of MEKE\_uflux and MEKE\_vflux are [R Z L4 T-3 ~> kg m2 s-3].}
457         meke\_uflux(i,j) = ((kh\_here * (g%dy\_Cu(i,j)*g%IdxCu(i,j))) * &
458             ((2.0*mass(i,j)*mass(i+1,j)) / ((mass(i,j)+mass(i+1,j)) + mass\_neglect)) ) * &
459             (meke%MEKE(i,j) - meke%MEKE(i+1,j))
460 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
461       \textcolor{comment}{!$OMP parallel do default(shared) firstprivate(Kh\_here) private(Inv\_Kh\_max)}
462       \textcolor{keywordflow}{do} j=js-1,je ; \textcolor{keywordflow}{do} i=is,ie
463         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Kh)) &
464           kh\_here = max(0.,cs%MEKE\_Kh) + cs%KhMEKE\_Fac * 0.5*(meke%Kh(i,j)+meke%Kh(i,j+1))
465         \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Kh\_diff)) &
466           kh\_here = max(0.,cs%MEKE\_Kh) + cs%KhMEKE\_Fac * 0.5*(meke%Kh\_diff(i,j)+meke%Kh\_diff(i,j+1))
467         inv\_kh\_max = 2.0*sdt * ((g%dx\_Cv(i,j)*g%IdyCv(i,j)) * max(g%IareaT(i,j),g%IareaT(i,j+1)))
468         \textcolor{keywordflow}{if} (kh\_here*inv\_kh\_max > 0.25) kh\_here = 0.25 / inv\_kh\_max
469         kh\_v(i,j) = kh\_here
470 
471         \textcolor{comment}{! Here the units of MEKE\_uflux and MEKE\_vflux are [R Z L4 T-3 ~> kg m2 s-3].}
472         meke\_vflux(i,j) = ((kh\_here * (g%dx\_Cv(i,j)*g%IdyCv(i,j))) * &
473             ((2.0*mass(i,j)*mass(i,j+1)) / ((mass(i,j)+mass(i,j+1)) + mass\_neglect)) ) * &
474             (meke%MEKE(i,j) - meke%MEKE(i,j+1))
475 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
476       \textcolor{keywordflow}{if} (cs%MEKE\_advection\_factor>0.) \textcolor{keywordflow}{then}
477         advfac = cs%MEKE\_advection\_factor / sdt \textcolor{comment}{! [T-1 ~> s-1]}
478         \textcolor{comment}{!$OMP parallel do default(shared)}
479         \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is-1,ie
480           \textcolor{comment}{! Here the units of the quantities added to MEKE\_uflux are [R Z L4 T-3 ~> kg m2 s-3].}
481           \textcolor{keywordflow}{if} (barohu(i,j)>0.) \textcolor{keywordflow}{then}
482             meke\_uflux(i,j) = meke\_uflux(i,j) + barohu(i,j)*meke%MEKE(i,j)*advfac
483           \textcolor{keywordflow}{elseif} (barohu(i,j)<0.) \textcolor{keywordflow}{then}
484             meke\_uflux(i,j) = meke\_uflux(i,j) + barohu(i,j)*meke%MEKE(i+1,j)*advfac
485 \textcolor{keywordflow}{          endif}
486 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}
487         \textcolor{comment}{!$OMP parallel do default(shared)}
488         \textcolor{keywordflow}{do} j=js-1,je ; \textcolor{keywordflow}{do} i=is,ie
489           \textcolor{comment}{! Here the units of the quantities added to MEKE\_vflux are [R Z L4 T-3 ~> kg m2 s-3].}
490           \textcolor{keywordflow}{if} (barohv(i,j)>0.) \textcolor{keywordflow}{then}
491             meke\_vflux(i,j) = meke\_vflux(i,j) + barohv(i,j)*meke%MEKE(i,j)*advfac
492           \textcolor{keywordflow}{elseif} (barohv(i,j)<0.) \textcolor{keywordflow}{then}
493             meke\_vflux(i,j) = meke\_vflux(i,j) + barohv(i,j)*meke%MEKE(i,j+1)*advfac
494 \textcolor{keywordflow}{          endif}
495 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}
496 \textcolor{keywordflow}{      endif}
497 
498       \textcolor{comment}{!$OMP parallel do default(shared)}
499       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
500         meke%MEKE(i,j) = meke%MEKE(i,j) + (sdt*(g%IareaT(i,j)*i\_mass(i,j))) * &
501             ((meke\_uflux(i-1,j) - meke\_uflux(i,j)) + &
502              (meke\_vflux(i,j-1) - meke\_vflux(i,j)))
503 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
504 \textcolor{keywordflow}{    endif} \textcolor{comment}{! MEKE\_KH>0}
505 
506     \textcolor{comment}{! Add on bi-harmonic tendency}
507     \textcolor{keywordflow}{if} (cs%MEKE\_K4 >= 0.0) \textcolor{keywordflow}{then}
508       \textcolor{comment}{!$OMP parallel do default(shared)}
509       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
510         meke%MEKE(i,j) = meke%MEKE(i,j) + del4meke(i,j)
511 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
512 \textcolor{keywordflow}{    endif}
513 
514     \textcolor{comment}{! Second stage of Strang splitting}
515     \textcolor{keywordflow}{if} (cs%MEKE\_KH >= 0.0 .or. cs%MEKE\_K4 >= 0.0) \textcolor{keywordflow}{then}
516       \textcolor{keywordflow}{if} (sdt>sdt\_damp) \textcolor{keywordflow}{then}
517         \textcolor{comment}{! Recalculate the drag rate, since MEKE has changed.}
518         \textcolor{keywordflow}{if} (use\_drag\_rate) \textcolor{keywordflow}{then}
519           \textcolor{comment}{!$OMP parallel do default(shared)}
520           \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
521             drag\_rate(i,j) = (us%L\_to\_Z*rho0 * i\_mass(i,j)) * sqrt( drag\_rate\_visc(i,j)**2 + &
522                    cdrag2 * ( max(0.0, 2.0*bottomfac2(i,j)*meke%MEKE(i,j)) + cs%MEKE\_Uscale**2 ) )
523 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}
524           \textcolor{comment}{!$OMP parallel do default(shared)}
525           \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
526             ldamping = cs%MEKE\_damping + drag\_rate(i,j) * bottomfac2(i,j)
527             \textcolor{keywordflow}{if} (meke%MEKE(i,j) < 0.) ldamping = 0.
528             \textcolor{comment}{! notice that the above line ensures a damping only if MEKE is positive,}
529             \textcolor{comment}{! while leaving MEKE unchanged if it is negative}
530             meke%MEKE(i,j) =  meke%MEKE(i,j) / (1.0 + sdt\_damp*ldamping)
531             meke\_decay(i,j) = ldamping*g%mask2dT(i,j)
532 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}
533 \textcolor{keywordflow}{        endif}
534 \textcolor{keywordflow}{      endif}
535 \textcolor{keywordflow}{    endif} \textcolor{comment}{! MEKE\_KH>=0}
536 
537  \textcolor{comment}{!   do j=js,je ; do i=is,ie}
538  \textcolor{comment}{!     MEKE%MEKE(i,j) =  MAX(MEKE%MEKE(i,j),0.0)}
539  \textcolor{comment}{!   enddo ; enddo}
540 
541     \textcolor{keyword}{call }cpu\_clock\_begin(cs%id\_clock\_pass)
542     \textcolor{keyword}{call }do\_group\_pass(cs%pass\_MEKE, g%Domain)
543     \textcolor{keyword}{call }cpu\_clock\_end(cs%id\_clock\_pass)
544 
545     \textcolor{comment}{! Calculate diffusivity for main model to use}
546     \textcolor{keywordflow}{if} (cs%MEKE\_KhCoeff>0.) \textcolor{keywordflow}{then}
547       \textcolor{keywordflow}{if} (.not.cs%MEKE\_GEOMETRIC) \textcolor{keywordflow}{then}
548         \textcolor{keywordflow}{if} (cs%use\_old\_lscale) \textcolor{keywordflow}{then}
549           \textcolor{keywordflow}{if} (cs%Rd\_as\_max\_scale) \textcolor{keywordflow}{then}
550             \textcolor{comment}{!$OMP parallel do default(shared)}
551             \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
552               meke%Kh(i,j) = (cs%MEKE\_KhCoeff * &
553                          sqrt(2.*max(0.,barotrfac2(i,j)*meke%MEKE(i,j))*g%areaT(i,j)) ) * &
554                          min(meke%Rd\_dx\_h(i,j), 1.0)
555 \textcolor{keywordflow}{            enddo} ;\textcolor{keywordflow}{ enddo}
556           \textcolor{keywordflow}{else}
557             \textcolor{comment}{!$OMP parallel do default(shared)}
558             \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
559               meke%Kh(i,j) = cs%MEKE\_KhCoeff * &
560                   sqrt(2.*max(0., barotrfac2(i,j)*meke%MEKE(i,j))*g%areaT(i,j))
561 \textcolor{keywordflow}{            enddo} ;\textcolor{keywordflow}{ enddo}
562 \textcolor{keywordflow}{          endif}
563         \textcolor{keywordflow}{else}
564           \textcolor{comment}{!$OMP parallel do default(shared)}
565           \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
566             meke%Kh(i,j) = cs%MEKE\_KhCoeff * &
567                 sqrt(2.*max(0., barotrfac2(i,j)*meke%MEKE(i,j))) * lmixscale(i,j)
568 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}
569 \textcolor{keywordflow}{        endif}
570 \textcolor{keywordflow}{      endif}
571 \textcolor{keywordflow}{    endif}
572 
573     \textcolor{comment}{! Calculate viscosity for the main model to use}
574     \textcolor{keywordflow}{if} (cs%viscosity\_coeff\_Ku /=0.) \textcolor{keywordflow}{then}
575       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
576         meke%Ku(i,j) = cs%viscosity\_coeff\_Ku * sqrt(2.*max(0.,meke%MEKE(i,j))) * lmixscale(i,j)
577 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
578 \textcolor{keywordflow}{    endif}
579 
580     \textcolor{keywordflow}{if} (cs%viscosity\_coeff\_Au /=0.) \textcolor{keywordflow}{then}
581       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
582         meke%Au(i,j) = cs%viscosity\_coeff\_Au * sqrt(2.*max(0.,meke%MEKE(i,j))) * lmixscale(i,j)**3
583 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
584 \textcolor{keywordflow}{    endif}
585 
586     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(meke%Kh) .or. \textcolor{keyword}{associated}(meke%Ku) .or. \textcolor{keyword}{associated}(meke%Au)) \textcolor{keywordflow}{then}
587       \textcolor{keyword}{call }cpu\_clock\_begin(cs%id\_clock\_pass)
588       \textcolor{keyword}{call }do\_group\_pass(cs%pass\_Kh, g%Domain)
589       \textcolor{keyword}{call }cpu\_clock\_end(cs%id\_clock\_pass)
590 \textcolor{keywordflow}{    endif}
591 
592     \textcolor{comment}{! Offer fields for averaging.}
593     \textcolor{keywordflow}{if} (any([cs%id\_Ue, cs%id\_Ub, cs%id\_Ut] > 0)) &
594       tmp(:,:) = 0.
595     \textcolor{keywordflow}{if} (cs%id\_MEKE>0) \textcolor{keyword}{call }post\_data(cs%id\_MEKE, meke%MEKE, cs%diag)
596     \textcolor{keywordflow}{if} (cs%id\_Ue>0) \textcolor{keywordflow}{then}
597       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
598         tmp(i,j) = sqrt(max(0., 2. * meke%MEKE(i,j)))
599 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
600       \textcolor{keyword}{call }post\_data(cs%id\_Ue, tmp, cs%diag)
601 \textcolor{keywordflow}{    endif}
602     \textcolor{keywordflow}{if} (cs%id\_Ub>0) \textcolor{keywordflow}{then}
603       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
604         tmp(i,j) = sqrt(max(0., 2. * meke%MEKE(i,j) * bottomfac2(i,j)))
605 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
606       \textcolor{keyword}{call }post\_data(cs%id\_Ub, tmp, cs%diag)
607 \textcolor{keywordflow}{    endif}
608     \textcolor{keywordflow}{if} (cs%id\_Ut>0) \textcolor{keywordflow}{then}
609       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
610         tmp(i,j) = sqrt(max(0., 2. * meke%MEKE(i,j) * barotrfac2(i,j)))
611 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
612       \textcolor{keyword}{call }post\_data(cs%id\_Ut, tmp, cs%diag)
613 \textcolor{keywordflow}{    endif}
614     \textcolor{keywordflow}{if} (cs%id\_Kh>0) \textcolor{keyword}{call }post\_data(cs%id\_Kh, meke%Kh, cs%diag)
615     \textcolor{keywordflow}{if} (cs%id\_Ku>0) \textcolor{keyword}{call }post\_data(cs%id\_Ku, meke%Ku, cs%diag)
616     \textcolor{keywordflow}{if} (cs%id\_Au>0) \textcolor{keyword}{call }post\_data(cs%id\_Au, meke%Au, cs%diag)
617     \textcolor{keywordflow}{if} (cs%id\_KhMEKE\_u>0) \textcolor{keyword}{call }post\_data(cs%id\_KhMEKE\_u, kh\_u, cs%diag)
618     \textcolor{keywordflow}{if} (cs%id\_KhMEKE\_v>0) \textcolor{keyword}{call }post\_data(cs%id\_KhMEKE\_v, kh\_v, cs%diag)
619     \textcolor{keywordflow}{if} (cs%id\_src>0) \textcolor{keyword}{call }post\_data(cs%id\_src, src, cs%diag)
620     \textcolor{keywordflow}{if} (cs%id\_decay>0) \textcolor{keyword}{call }post\_data(cs%id\_decay, meke\_decay, cs%diag)
621     \textcolor{keywordflow}{if} (cs%id\_GM\_src>0) \textcolor{keyword}{call }post\_data(cs%id\_GM\_src, meke%GM\_src, cs%diag)
622     \textcolor{keywordflow}{if} (cs%id\_mom\_src>0) \textcolor{keyword}{call }post\_data(cs%id\_mom\_src, meke%mom\_src, cs%diag)
623     \textcolor{keywordflow}{if} (cs%id\_GME\_snk>0) \textcolor{keyword}{call }post\_data(cs%id\_GME\_snk, meke%GME\_snk, cs%diag)
624     \textcolor{keywordflow}{if} (cs%id\_Le>0) \textcolor{keyword}{call }post\_data(cs%id\_Le, lmixscale, cs%diag)
625     \textcolor{keywordflow}{if} (cs%id\_gamma\_b>0) \textcolor{keywordflow}{then}
626       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
627         bottomfac2(i,j) = sqrt(bottomfac2(i,j))
628 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
629       \textcolor{keyword}{call }post\_data(cs%id\_gamma\_b, bottomfac2, cs%diag)
630 \textcolor{keywordflow}{    endif}
631     \textcolor{keywordflow}{if} (cs%id\_gamma\_t>0) \textcolor{keywordflow}{then}
632       \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=is,ie
633         barotrfac2(i,j) = sqrt(barotrfac2(i,j))
634 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}
635       \textcolor{keyword}{call }post\_data(cs%id\_gamma\_t, barotrfac2, cs%diag)
636 \textcolor{keywordflow}{    endif}
637 
\end{DoxyCode}
