\hypertarget{namespacemom__kappa__shear}{}\section{mom\+\_\+kappa\+\_\+shear Module Reference}
\label{namespacemom__kappa__shear}\index{mom\_kappa\_shear@{mom\_kappa\_shear}}


\subsection{Detailed Description}
Shear-\/dependent mixing following Jackson et al. 2008. 

By Laura Jackson and Robert Hallberg, 2006-\/2008.

This file contains the subroutines that determine the diapycnal diffusivity driven by resolved shears, as specified by the parameterizations described in Jackson and Hallberg (J\+PO, 2008).

The technique by which the 6 equations (for kappa, T\+KE, u, v, T, and S) are solved simultaneously has been dramatically revised from the previous version. The previous version was not converging in some cases, especially near the surface mixed layer, while the revised version does. The revised version solves for kappa and T\+KE with shear and stratification fixed, then marches the density and velocities forward with an adaptive (and aggressive) time step in a predictor-\/corrector-\/corrector emulation of a trapezoidal scheme. Run-\/time-\/settable parameters determine the tolerence to which the kappa and T\+KE equations are solved and the minimum time step that can be taken. \subsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__kappa__shear_1_1kappa__shear__cs}{kappa\+\_\+shear\+\_\+cs}}
\begin{DoxyCompactList}\small\item\em This control structure holds the parameters that regulate shear mixing. \end{DoxyCompactList}\end{DoxyCompactItemize}
\subsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \mbox{\hyperlink{namespacemom__kappa__shear_a3f00b08e1174690d40c0c2065fa9a8b1}{calculate\+\_\+kappa\+\_\+shear}} (u\+\_\+in, v\+\_\+in, h, tv, p\+\_\+surf, kappa\+\_\+io, tke\+\_\+io, kv\+\_\+io, dt, G, GV, US, CS, initialize\+\_\+all)
\begin{DoxyCompactList}\small\item\em Subroutine for calculating shear-\/driven diffusivity and T\+KE in tracer columns. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__kappa__shear_a2d8e291656bab5f83179523c4bea4d85}{calc\+\_\+kappa\+\_\+shear\+\_\+vertex}} (u\+\_\+in, v\+\_\+in, h, T\+\_\+in, S\+\_\+in, tv, p\+\_\+surf, kappa\+\_\+io, tke\+\_\+io, kv\+\_\+io, dt, G, GV, US, CS, initialize\+\_\+all)
\begin{DoxyCompactList}\small\item\em Subroutine for calculating shear-\/driven diffusivity and T\+KE in corner columns. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__kappa__shear_a26cc5bb15545f04cfaf07e53410e09ec}{kappa\+\_\+shear\+\_\+column}} (kappa, tke, dt, nzc, f2, surface\+\_\+pres, dz, u0xdz, v0xdz, T0xdz, S0xdz, kappa\+\_\+avg, tke\+\_\+avg, tv, CS, GV, US, I\+\_\+\+Ld2\+\_\+1d, dz\+\_\+\+Int\+\_\+1d)
\begin{DoxyCompactList}\small\item\em This subroutine calculates shear-\/driven diffusivity and T\+KE in a single column. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__kappa__shear_a0b931b0b834d887e321eb6eb1924fa9a}{calculate\+\_\+projected\+\_\+state}} (kappa, u0, v0, T0, S0, dt, nz, dz, I\+\_\+dz\+\_\+int, dbuoy\+\_\+dT, dbuoy\+\_\+dS, u, v, T, Sal, GV, US, N2, S2, ks\+\_\+int, ke\+\_\+int, vel\+\_\+underflow)
\begin{DoxyCompactList}\small\item\em This subroutine calculates the velocities, temperature and salinity that the water column will have after mixing for dt with diffusivities kappa. It may also calculate the projected buoyancy frequency and shear. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__kappa__shear_a351d44e4fe5cfb5852d019a0c1e66100}{find\+\_\+kappa\+\_\+tke}} (N2, S2, kappa\+\_\+in, Idz, dz\+\_\+\+Int, I\+\_\+\+L2\+\_\+bdry, f2, nz, CS, GV, US, K\+\_\+Q, tke, kappa, kappa\+\_\+src, local\+\_\+src)
\begin{DoxyCompactList}\small\item\em This subroutine calculates new, consistent estimates of T\+KE and kappa. \end{DoxyCompactList}\item 
logical function, public \mbox{\hyperlink{namespacemom__kappa__shear_a82428168ba463cf7276ae96d3e32475c}{kappa\+\_\+shear\+\_\+init}} (Time, G, GV, US, param\+\_\+file, diag, CS)
\begin{DoxyCompactList}\small\item\em This subroutine initializes the parameters that regulate shear-\/driven mixing. \end{DoxyCompactList}\item 
logical function, public \mbox{\hyperlink{namespacemom__kappa__shear_ac7859c609e462000ca8fd763d68d141e}{kappa\+\_\+shear\+\_\+is\+\_\+used}} (param\+\_\+file)
\begin{DoxyCompactList}\small\item\em This function indicates to other modules whether the Jackson et al shear mixing parameterization will be used without needing to duplicate the log entry. \end{DoxyCompactList}\item 
logical function, public \mbox{\hyperlink{namespacemom__kappa__shear_ad4d87b0928aea195213e682b493eb555}{kappa\+\_\+shear\+\_\+at\+\_\+vertex}} (param\+\_\+file)
\begin{DoxyCompactList}\small\item\em This function indicates to other modules whether the Jackson et al shear mixing parameterization will be used at the vertices without needing to duplicate the log entry. It returns false if the Jackson et al scheme is not used or if it is used via calculations at the tracer points. \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__kappa__shear_a2d8e291656bab5f83179523c4bea4d85}\label{namespacemom__kappa__shear_a2d8e291656bab5f83179523c4bea4d85}} 
\index{mom\_kappa\_shear@{mom\_kappa\_shear}!calc\_kappa\_shear\_vertex@{calc\_kappa\_shear\_vertex}}
\index{calc\_kappa\_shear\_vertex@{calc\_kappa\_shear\_vertex}!mom\_kappa\_shear@{mom\_kappa\_shear}}
\subsubsection{\texorpdfstring{calc\_kappa\_shear\_vertex()}{calc\_kappa\_shear\_vertex()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+kappa\+\_\+shear\+::calc\+\_\+kappa\+\_\+shear\+\_\+vertex (\begin{DoxyParamCaption}\item[{real, dimension(szib\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{u\+\_\+in,  }\item[{real, dimension(szi\+\_\+(g),szjb\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{v\+\_\+in,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{h,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{T\+\_\+in,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{S\+\_\+in,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{real, dimension(\+:,\+:), pointer}]{p\+\_\+surf,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)+1), intent(out)}]{kappa\+\_\+io,  }\item[{real, dimension(szib\+\_\+(g),szjb\+\_\+(g),szk\+\_\+(gv)+1), intent(out)}]{tke\+\_\+io,  }\item[{real, dimension(szib\+\_\+(g),szjb\+\_\+(g),szk\+\_\+(gv)+1), intent(inout)}]{kv\+\_\+io,  }\item[{real, intent(in)}]{dt,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__kappa__shear_1_1kappa__shear__cs}{kappa\+\_\+shear\+\_\+cs}}), pointer}]{CS,  }\item[{logical, intent(in), optional}]{initialize\+\_\+all }\end{DoxyParamCaption})}



Subroutine for calculating shear-\/driven diffusivity and T\+KE in corner columns. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em u\+\_\+in} & Initial zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em v\+\_\+in} & Initial meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em t\+\_\+in} & Layer potential temperatures \mbox{[}degC\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em s\+\_\+in} & Layer salinities in ppt. \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs. \\
\hline
 & {\em p\+\_\+surf} & The pressure at the ocean surface \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} (or N\+U\+LL). \\
\hline
\mbox{\texttt{ out}}  & {\em kappa\+\_\+io} & The diapycnal diffusivity at each interface \\
\hline
\mbox{\texttt{ out}}  & {\em tke\+\_\+io} & The turbulent kinetic energy per unit mass at \\
\hline
\mbox{\texttt{ in,out}}  & {\em kv\+\_\+io} & The vertical viscosity at each interface \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
 & {\em cs} & The control structure returned by a previous call to kappa\+\_\+shear\+\_\+init. \\
\hline
\mbox{\texttt{ in}}  & {\em initialize\+\_\+all} & If present and false, the previous value of kappa is used to start the iterations \\
\hline
\end{DoxyParams}


Definition at line 344 of file M\+O\+M\+\_\+kappa\+\_\+shear.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{344   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{      !< The ocean's grid structure.}}
\DoxyCodeLine{345   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{     !< The ocean's vertical grid structure.}}
\DoxyCodeLine{346   \textcolor{keywordtype}{type}(unit\_scale\_type),    \textcolor{keywordtype}{intent(in)}   :: US\textcolor{comment}{     !< A dimensional unit scaling type}}
\DoxyCodeLine{347 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(GV))},   \&}
\DoxyCodeLine{348                            \textcolor{keywordtype}{intent(in)}    :: u\_in\textcolor{comment}{   !< Initial zonal velocity [L T-1 ~> m s-1].}}
\DoxyCodeLine{349 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(GV))},   \&}
\DoxyCodeLine{350                            \textcolor{keywordtype}{intent(in)}    :: v\_in\textcolor{comment}{   !< Initial meridional velocity [L T-1 ~> m s-1].}}
\DoxyCodeLine{351 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))},   \&}
\DoxyCodeLine{352                            \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{      !< Layer thicknesses [H ~> m or kg m-2].}}
\DoxyCodeLine{353 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))},   \&}
\DoxyCodeLine{354                            \textcolor{keywordtype}{intent(in)}    :: T\_in\textcolor{comment}{   !< Layer potential temperatures [degC]}}
\DoxyCodeLine{355 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))},   \&}
\DoxyCodeLine{356                            \textcolor{keywordtype}{intent(in)}    :: S\_in\textcolor{comment}{   !< Layer salinities in ppt.}}
\DoxyCodeLine{357   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),   \textcolor{keywordtype}{intent(in)}    :: tv\textcolor{comment}{     !< A structure containing pointers to any}}
\DoxyCodeLine{358 \textcolor{comment}{                                                   !! available thermodynamic fields. Absent fields}}
\DoxyCodeLine{359 \textcolor{comment}{                                                   !! have NULL ptrs.}}
\DoxyCodeLine{360 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(:,:)},    \textcolor{keywordtype}{pointer}       :: p\_surf\textcolor{comment}{ !< The pressure at the ocean surface [R L2 T-2 ~> Pa]}}
\DoxyCodeLine{361 \textcolor{comment}{                                                   !! (or NULL).}}
\DoxyCodeLine{362 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV)+1)}, \&}
\DoxyCodeLine{363                            \textcolor{keywordtype}{intent(out)}   :: kappa\_io\textcolor{comment}{ !< The diapycnal diffusivity at each interface}}
\DoxyCodeLine{364 \textcolor{comment}{                                                   !! (not layer!) [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{365 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJB\_(G),SZK\_(GV)+1)}, \&}
\DoxyCodeLine{366                            \textcolor{keywordtype}{intent(out)}   :: tke\_io\textcolor{comment}{ !< The turbulent kinetic energy per unit mass at}}
\DoxyCodeLine{367 \textcolor{comment}{                                                   !! each interface (not layer!) [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{368 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJB\_(G),SZK\_(GV)+1)}, \&}
\DoxyCodeLine{369                            \textcolor{keywordtype}{intent(inout)} :: kv\_io\textcolor{comment}{  !< The vertical viscosity at each interface [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{370 \textcolor{comment}{                                                   !! The previous value is used to initialize kappa}}
\DoxyCodeLine{371 \textcolor{comment}{                                                   !! in the vertex columes as Kappa = Kv/Prandtl}}
\DoxyCodeLine{372 \textcolor{comment}{                                                   !! to accelerate the iteration toward covergence.}}
\DoxyCodeLine{373 \textcolor{keywordtype}{  real},                    \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{     !< Time increment [T ~> s].}}
\DoxyCodeLine{374   \textcolor{keywordtype}{type}(Kappa\_shear\_CS),    \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{     !< The control structure returned by a previous}}
\DoxyCodeLine{375 \textcolor{comment}{                                                   !! call to kappa\_shear\_init.}}
\DoxyCodeLine{376   \textcolor{keywordtype}{logical},       \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: initialize\_all\textcolor{comment}{ !< If present and false, the previous}}
\DoxyCodeLine{377 \textcolor{comment}{                                                   !! value of kappa is used to start the iterations}}
\DoxyCodeLine{378 }
\DoxyCodeLine{379   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{380 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZK\_(GV))} :: \&}
\DoxyCodeLine{381     h\_2d, \&             \textcolor{comment}{! A 2-D version of h, but converted to [Z ~> m].}}
\DoxyCodeLine{382     u\_2d, v\_2d, \&       \textcolor{comment}{! 2-D versions of u\_in and v\_in, converted to [L T-1 ~> m s-1].}}
\DoxyCodeLine{383     T\_2d, S\_2d, rho\_2d  \textcolor{comment}{! 2-D versions of T [degC], S [ppt], and rho [R ~> kg m-3].}}
\DoxyCodeLine{384 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZK\_(GV)+1,2)} :: \&}
\DoxyCodeLine{385     kappa\_2d    \textcolor{comment}{! Quasi 2-D versions of kappa\_io [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{386 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZK\_(GV)+1)} :: \&}
\DoxyCodeLine{387     tke\_2d      \textcolor{comment}{! 2-D version tke\_io [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{388 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV))} :: \&}
\DoxyCodeLine{389     Idz, \&      \textcolor{comment}{! The inverse of the distance between TKE points [Z-1 ~> m-1].}}
\DoxyCodeLine{390     dz, \&       \textcolor{comment}{! The layer thickness [Z ~> m].}}
\DoxyCodeLine{391     u0xdz, \&    \textcolor{comment}{! The initial zonal velocity times dz [L Z T-1 ~> m2 s-1].}}
\DoxyCodeLine{392     v0xdz, \&    \textcolor{comment}{! The initial meridional velocity times dz [L Z T-1 ~> m2 s-1].}}
\DoxyCodeLine{393     T0xdz, \&    \textcolor{comment}{! The initial temperature times dz [degC Z ~> degC m].}}
\DoxyCodeLine{394     S0xdz       \textcolor{comment}{! The initial salinity times dz [ppt Z ~> ppt m].}}
\DoxyCodeLine{395 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV)+1)} :: \&}
\DoxyCodeLine{396     kappa, \&    \textcolor{comment}{! The shear-driven diapycnal diffusivity at an interface [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{397     tke, \&      \textcolor{comment}{! The Turbulent Kinetic Energy per unit mass at an interface [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{398     kappa\_avg, \& \textcolor{comment}{! The time-weighted average of kappa [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{399     tke\_avg     \textcolor{comment}{! The time-weighted average of TKE [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{400 \textcolor{keywordtype}{  real} :: f2   \textcolor{comment}{! The squared Coriolis parameter of each column [T-2 ~> s-2].}}
\DoxyCodeLine{401 \textcolor{keywordtype}{  real} :: surface\_pres  \textcolor{comment}{! The top surface pressure [R L2 T-2 ~> Pa].}}
\DoxyCodeLine{402 }
\DoxyCodeLine{403 \textcolor{keywordtype}{  real} :: dz\_in\_lay     \textcolor{comment}{!   The running sum of the thickness in a layer [Z ~> m].}}
\DoxyCodeLine{404 \textcolor{keywordtype}{  real} :: k0dt          \textcolor{comment}{! The background diffusivity times the timestep [Z2 ~> m2].}}
\DoxyCodeLine{405 \textcolor{keywordtype}{  real} :: dz\_massless   \textcolor{comment}{! A layer thickness that is considered massless [Z ~> m].}}
\DoxyCodeLine{406 \textcolor{keywordtype}{  real} :: I\_hwt         \textcolor{comment}{! The inverse of the masked thickness weights [H-1 ~> m-1 or m2 kg-1].}}
\DoxyCodeLine{407 \textcolor{keywordtype}{  real} :: I\_Prandtl     \textcolor{comment}{! The inverse of the turbulent Prandtl number [nondim].}}
\DoxyCodeLine{408   \textcolor{keywordtype}{logical} :: use\_temperature  \textcolor{comment}{!  If true, temperature and salinity have been}}
\DoxyCodeLine{409                         \textcolor{comment}{! allocated and are being used as state variables.}}
\DoxyCodeLine{410   \textcolor{keywordtype}{logical} :: new\_kappa = .true. \textcolor{comment}{! If true, ignore the value of kappa from the}}
\DoxyCodeLine{411                         \textcolor{comment}{! last call to this subroutine.}}
\DoxyCodeLine{412   \textcolor{keywordtype}{logical} :: do\_i       \textcolor{comment}{! If true, work on this column.}}
\DoxyCodeLine{413 }
\DoxyCodeLine{414   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZK\_(GV)+1)} :: kc \textcolor{comment}{! The index map between the original}}
\DoxyCodeLine{415                         \textcolor{comment}{! interfaces and the interfaces with massless layers}}
\DoxyCodeLine{416                         \textcolor{comment}{! merged into nearby massive layers.}}
\DoxyCodeLine{417 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV)+1)} :: kf \textcolor{comment}{! The fractional weight of interface kc+1 for}}
\DoxyCodeLine{418                         \textcolor{comment}{! interpolating back to the original index space [nondim].}}
\DoxyCodeLine{419   \textcolor{keywordtype}{integer} :: IsB, IeB, JsB, JeB, i, j, k, nz, nzc, J2, J2m1}
\DoxyCodeLine{420 }
\DoxyCodeLine{421   \textcolor{comment}{! Diagnostics that should be deleted?}}
\DoxyCodeLine{422   isb = g\%isc-1 ; ieb = g\%iecB ; jsb = g\%jsc-1 ; jeb = g\%jecB ; nz = gv\%ke}
\DoxyCodeLine{423 }
\DoxyCodeLine{424   use\_temperature = .false. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%T)) use\_temperature = .true.}
\DoxyCodeLine{425   new\_kappa = .true. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(initialize\_all)) new\_kappa = initialize\_all}
\DoxyCodeLine{426 }
\DoxyCodeLine{427   k0dt =  dt*cs\%kappa\_0}
\DoxyCodeLine{428   dz\_massless = 0.1*sqrt(k0dt)}
\DoxyCodeLine{429   i\_prandtl = 0.0 ; \textcolor{keywordflow}{if} (cs\%Prandtl\_turb > 0.0) i\_prandtl = 1.0 / cs\%Prandtl\_turb}
\DoxyCodeLine{430 }
\DoxyCodeLine{431   \textcolor{comment}{!\$OMP parallel do default(private) shared(jsB,jeB,isB,ieB,nz,h,u\_in,v\_in,use\_temperature,new\_kappa, \&}}
\DoxyCodeLine{432   \textcolor{comment}{!\$OMP                                tv,G,GV,US,CS,kappa\_io,dz\_massless,k0dt,p\_surf,dt,tke\_io,kv\_io,I\_Prandtl)}}
\DoxyCodeLine{433   \textcolor{keywordflow}{do} j=jsb,jeb}
\DoxyCodeLine{434     j2 = mod(j,2)+1 ; j2m1 = 3-j2 \textcolor{comment}{! = mod(J-1,2)+1}}
\DoxyCodeLine{435 }
\DoxyCodeLine{436     \textcolor{comment}{! Interpolate the various quantities to the corners, using masks.}}
\DoxyCodeLine{437     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isb,ieb}
\DoxyCodeLine{438       u\_2d(i,k) = (u\_in(i,j,k)   * (g\%mask2dCu(i,j)   * (h(i,j,k)   + h(i+1,j,k))) + \&}
\DoxyCodeLine{439                    u\_in(i,j+1,k) * (g\%mask2dCu(i,j+1) * (h(i,j+1,k) + h(i+1,j+1,k))) ) / \&}
\DoxyCodeLine{440                   ((g\%mask2dCu(i,j)   * (h(i,j,k)   + h(i+1,j,k)) + \&}
\DoxyCodeLine{441                     g\%mask2dCu(i,j+1) * (h(i,j+1,k) + h(i+1,j+1,k))) + gv\%H\_subroundoff)}
\DoxyCodeLine{442       v\_2d(i,k) = (v\_in(i,j,k)   * (g\%mask2dCv(i,j)   * (h(i,j,k)   + h(i,j+1,k))) + \&}
\DoxyCodeLine{443                    v\_in(i+1,j,k) * (g\%mask2dCv(i+1,j) * (h(i+1,j,k) + h(i+1,j+1,k))) ) / \&}
\DoxyCodeLine{444                   ((g\%mask2dCv(i,j)   * (h(i,j,k)   + h(i,j+1,k)) + \&}
\DoxyCodeLine{445                     g\%mask2dCv(i+1,j) * (h(i+1,j,k) + h(i+1,j+1,k))) + gv\%H\_subroundoff)}
\DoxyCodeLine{446       i\_hwt = 1.0 / (((g\%mask2dT(i,j) * h(i,j,k) + g\%mask2dT(i+1,j+1) * h(i+1,j+1,k)) + \&}
\DoxyCodeLine{447                       (g\%mask2dT(i+1,j) * h(i+1,j,k) + g\%mask2dT(i,j+1) * h(i,j+1,k))) + \&}
\DoxyCodeLine{448                      gv\%H\_subroundoff)}
\DoxyCodeLine{449       \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then}}
\DoxyCodeLine{450         t\_2d(i,k) = ( ((g\%mask2dT(i,j) * h(i,j,k)) * t\_in(i,j,k) + \&}
\DoxyCodeLine{451                        (g\%mask2dT(i+1,j+1) * h(i+1,j+1,k)) * t\_in(i+1,j+1,k)) + \&}
\DoxyCodeLine{452                       ((g\%mask2dT(i+1,j) * h(i+1,j,k)) * t\_in(i+1,j,k) + \&}
\DoxyCodeLine{453                        (g\%mask2dT(i,j+1) * h(i,j+1,k)) * t\_in(i,j+1,k)) ) * i\_hwt}
\DoxyCodeLine{454         s\_2d(i,k) = ( ((g\%mask2dT(i,j) * h(i,j,k)) * s\_in(i,j,k) + \&}
\DoxyCodeLine{455                        (g\%mask2dT(i+1,j+1) * h(i+1,j+1,k)) * s\_in(i+1,j+1,k)) + \&}
\DoxyCodeLine{456                       ((g\%mask2dT(i+1,j) * h(i+1,j,k)) * s\_in(i+1,j,k) + \&}
\DoxyCodeLine{457                        (g\%mask2dT(i,j+1) * h(i,j+1,k)) * s\_in(i,j+1,k)) ) * i\_hwt}
\DoxyCodeLine{458 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{459       h\_2d(i,k) = gv\%H\_to\_Z * ((g\%mask2dT(i,j) * h(i,j,k) + g\%mask2dT(i+1,j+1) * h(i+1,j+1,k)) + \&}
\DoxyCodeLine{460                                (g\%mask2dT(i+1,j) * h(i+1,j,k) + g\%mask2dT(i,j+1) * h(i,j+1,k)) ) / \&}
\DoxyCodeLine{461                               ((g\%mask2dT(i,j) + g\%mask2dT(i+1,j+1)) + \&}
\DoxyCodeLine{462                                (g\%mask2dT(i+1,j) + g\%mask2dT(i,j+1)) + 1.0e-36 )}
\DoxyCodeLine{463 \textcolor{comment}{!      h\_2d(I,k) = 0.25*((h(i,j,k) + h(i+1,j+1,k)) + (h(i+1,j,k) + h(i,j+1,k)))*GV\%H\_to\_Z}}
\DoxyCodeLine{464 \textcolor{comment}{!      h\_2d(I,k) = ((h(i,j,k)**2 + h(i+1,j+1,k)**2) + \&}}
\DoxyCodeLine{465 \textcolor{comment}{!                   (h(i+1,j,k)**2 + h(i,j+1,k)**2))*GV\%H\_to\_Z * I\_hwt}}
\DoxyCodeLine{466 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{467     \textcolor{keywordflow}{if} (.not.use\_temperature) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isb,ieb}
\DoxyCodeLine{468       rho\_2d(i,k) = gv\%Rlay(k)}
\DoxyCodeLine{469 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{470     \textcolor{keywordflow}{if} (.not.new\_kappa) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=isb,ieb}
\DoxyCodeLine{471       kappa\_2d(i,k,j2) = kv\_io(i,j,k) * i\_prandtl}
\DoxyCodeLine{472 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{473 }
\DoxyCodeLine{474 \textcolor{comment}{!---------------------------------------}}
\DoxyCodeLine{475 \textcolor{comment}{! Work on each column.}}
\DoxyCodeLine{476 \textcolor{comment}{!---------------------------------------}}
\DoxyCodeLine{477     \textcolor{keywordflow}{do} i=isb,ieb ; \textcolor{keywordflow}{if} ((g\%mask2dCu(i,j) + g\%mask2dCu(i,j+1)) + \&}
\DoxyCodeLine{478                        (g\%mask2dCv(i,j) + g\%mask2dCv(i+1,j)) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{479     \textcolor{comment}{! call cpu\_clock\_begin(Id\_clock\_setup)}}
\DoxyCodeLine{480       \textcolor{comment}{! Store a transposed version of the initial arrays.}}
\DoxyCodeLine{481       \textcolor{comment}{! Any elimination of massless layers would occur here.}}
\DoxyCodeLine{482       \textcolor{keywordflow}{if} (cs\%eliminate\_massless) \textcolor{keywordflow}{then}}
\DoxyCodeLine{483         nzc = 1}
\DoxyCodeLine{484         \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{485           \textcolor{comment}{! Zero out the thicknesses of all layers, even if they are unused.}}
\DoxyCodeLine{486           dz(k) = 0.0 ; u0xdz(k) = 0.0 ; v0xdz(k) = 0.0}
\DoxyCodeLine{487           t0xdz(k) = 0.0 ; s0xdz(k) = 0.0}
\DoxyCodeLine{488 }
\DoxyCodeLine{489           \textcolor{comment}{! Add a new layer if this one has mass.}}
\DoxyCodeLine{490 \textcolor{comment}{!          if ((dz(nzc) > 0.0) .and. (h\_2d(I,k) > dz\_massless)) nzc = nzc+1}}
\DoxyCodeLine{491           \textcolor{keywordflow}{if} ((k>cs\%nkml) .and. (dz(nzc) > 0.0) .and. \&}
\DoxyCodeLine{492               (h\_2d(i,k) > dz\_massless)) nzc = nzc+1}
\DoxyCodeLine{493 }
\DoxyCodeLine{494           \textcolor{comment}{! Only merge clusters of massless layers.}}
\DoxyCodeLine{495 \textcolor{comment}{!         if ((dz(nzc) > dz\_massless) .or. \&}}
\DoxyCodeLine{496 \textcolor{comment}{!             ((dz(nzc) > 0.0) .and. (h\_2d(I,k) > dz\_massless))) nzc = nzc+1}}
\DoxyCodeLine{497 }
\DoxyCodeLine{498           kc(k) = nzc}
\DoxyCodeLine{499           dz(nzc) = dz(nzc) + h\_2d(i,k)}
\DoxyCodeLine{500           u0xdz(nzc) = u0xdz(nzc) + u\_2d(i,k)*h\_2d(i,k)}
\DoxyCodeLine{501           v0xdz(nzc) = v0xdz(nzc) + v\_2d(i,k)*h\_2d(i,k)}
\DoxyCodeLine{502           \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then}}
\DoxyCodeLine{503             t0xdz(nzc) = t0xdz(nzc) + t\_2d(i,k)*h\_2d(i,k)}
\DoxyCodeLine{504             s0xdz(nzc) = s0xdz(nzc) + s\_2d(i,k)*h\_2d(i,k)}
\DoxyCodeLine{505           \textcolor{keywordflow}{else}}
\DoxyCodeLine{506             t0xdz(nzc) = t0xdz(nzc) + rho\_2d(i,k)*h\_2d(i,k)}
\DoxyCodeLine{507             s0xdz(nzc) = s0xdz(nzc) + rho\_2d(i,k)*h\_2d(i,k)}
\DoxyCodeLine{508 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{509 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{510         kc(nz+1) = nzc+1}
\DoxyCodeLine{511 }
\DoxyCodeLine{512         \textcolor{comment}{! Set up Idz as the inverse of layer thicknesses.}}
\DoxyCodeLine{513         \textcolor{keywordflow}{do} k=1,nzc ; idz(k) = 1.0 / dz(k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{514 }
\DoxyCodeLine{515         \textcolor{comment}{!   Now determine kf, the fractional weight of interface kc when}}
\DoxyCodeLine{516         \textcolor{comment}{! interpolating between interfaces kc and kc+1.}}
\DoxyCodeLine{517         kf(1) = 0.0 ; dz\_in\_lay = h\_2d(i,1)}
\DoxyCodeLine{518         \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{519           \textcolor{keywordflow}{if} (kc(k) > kc(k-1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{520             kf(k) = 0.0 ; dz\_in\_lay = h\_2d(i,k)}
\DoxyCodeLine{521           \textcolor{keywordflow}{else}}
\DoxyCodeLine{522             kf(k) = dz\_in\_lay*idz(kc(k)) ; dz\_in\_lay = dz\_in\_lay + h\_2d(i,k)}
\DoxyCodeLine{523 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{524 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{525         kf(nz+1) = 0.0}
\DoxyCodeLine{526       \textcolor{keywordflow}{else}}
\DoxyCodeLine{527         \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{528           dz(k) = h\_2d(i,k)}
\DoxyCodeLine{529           u0xdz(k) = u\_2d(i,k)*dz(k) ; v0xdz(k) = v\_2d(i,k)*dz(k)}
\DoxyCodeLine{530 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{531         \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then}}
\DoxyCodeLine{532           \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{533             t0xdz(k) = t\_2d(i,k)*dz(k) ; s0xdz(k) = s\_2d(i,k)*dz(k)}
\DoxyCodeLine{534 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{535         \textcolor{keywordflow}{else}}
\DoxyCodeLine{536           \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{537             t0xdz(k) = rho\_2d(i,k)*dz(k) ; s0xdz(k) = rho\_2d(i,k)*dz(k)}
\DoxyCodeLine{538 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{539 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{540         nzc = nz}
\DoxyCodeLine{541         \textcolor{keywordflow}{do} k=1,nzc+1 ; kc(k) = k ; kf(k) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{542 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{543       f2 = g\%CoriolisBu(i,j)**2}
\DoxyCodeLine{544       surface\_pres = 0.0}
\DoxyCodeLine{545       \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(p\_surf)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{546         \textcolor{keywordflow}{if} (cs\%psurf\_bug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{547           \textcolor{comment}{! This is wrong because it is averaging values from land in some places.}}
\DoxyCodeLine{548           surface\_pres = 0.25 * ((p\_surf(i,j) + p\_surf(i+1,j+1)) + \&}
\DoxyCodeLine{549                                  (p\_surf(i+1,j) + p\_surf(i,j+1)))}
\DoxyCodeLine{550         \textcolor{keywordflow}{else}}
\DoxyCodeLine{551           surface\_pres = ((g\%mask2dT(i,j) * p\_surf(i,j) + g\%mask2dT(i+1,j+1) * p\_surf(i+1,j+1)) + \&}
\DoxyCodeLine{552                           (g\%mask2dT(i+1,j) * p\_surf(i+1,j) + g\%mask2dT(i,j+1) * p\_surf(i,j+1)) ) / \&}
\DoxyCodeLine{553                          ((g\%mask2dT(i,j) + g\%mask2dT(i+1,j+1)) + \&}
\DoxyCodeLine{554                           (g\%mask2dT(i+1,j) + g\%mask2dT(i,j+1)) + 1.0e-36 )}
\DoxyCodeLine{555 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{556 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{557 }
\DoxyCodeLine{558     \textcolor{comment}{! ----------------------------------------------------}}
\DoxyCodeLine{559     \textcolor{comment}{! Set the initial guess for kappa, here defined at interfaces.}}
\DoxyCodeLine{560     \textcolor{comment}{! ----------------------------------------------------}}
\DoxyCodeLine{561       \textcolor{keywordflow}{if} (new\_kappa) \textcolor{keywordflow}{then}}
\DoxyCodeLine{562         \textcolor{keywordflow}{do} k=1,nzc+1 ; kappa(k) = us\%m2\_s\_to\_Z2\_T*1.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{563       \textcolor{keywordflow}{else}}
\DoxyCodeLine{564         \textcolor{keywordflow}{do} k=1,nzc+1 ; kappa(k) = kappa\_2d(i,k,j2) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{565 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{566 }
\DoxyCodeLine{567       \textcolor{keyword}{call }kappa\_shear\_column(kappa, tke, dt, nzc, f2, surface\_pres, \&}
\DoxyCodeLine{568                               dz, u0xdz, v0xdz, t0xdz, s0xdz, kappa\_avg, \&}
\DoxyCodeLine{569                               tke\_avg, tv, cs, gv, us)}
\DoxyCodeLine{570     \textcolor{comment}{! call cpu\_clock\_begin(Id\_clock\_setup)}}
\DoxyCodeLine{571     \textcolor{comment}{! Extrapolate from the vertically reduced grid back to the original layers.}}
\DoxyCodeLine{572       \textcolor{keywordflow}{if} (nz == nzc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{573         \textcolor{keywordflow}{do} k=1,nz+1}
\DoxyCodeLine{574           kappa\_2d(i,k,j2) = kappa\_avg(k)}
\DoxyCodeLine{575           \textcolor{keywordflow}{if} (cs\%all\_layer\_TKE\_bug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{576             tke\_2d(i,k) = tke(k)}
\DoxyCodeLine{577           \textcolor{keywordflow}{else}}
\DoxyCodeLine{578             tke\_2d(i,k) = tke\_avg(k)}
\DoxyCodeLine{579 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{580 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{581       \textcolor{keywordflow}{else}}
\DoxyCodeLine{582         \textcolor{keywordflow}{do} k=1,nz+1}
\DoxyCodeLine{583           \textcolor{keywordflow}{if} (kf(k) == 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{584             kappa\_2d(i,k,j2) = kappa\_avg(kc(k))}
\DoxyCodeLine{585             tke\_2d(i,k) = tke\_avg(kc(k))}
\DoxyCodeLine{586           \textcolor{keywordflow}{else}}
\DoxyCodeLine{587             kappa\_2d(i,k,j2) = (1.0-kf(k)) * kappa\_avg(kc(k)) + kf(k) * kappa\_avg(kc(k)+1)}
\DoxyCodeLine{588             tke\_2d(i,k) = (1.0-kf(k)) * tke\_avg(kc(k)) + kf(k) * tke\_avg(kc(k)+1)}
\DoxyCodeLine{589 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{590 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{591 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{592     \textcolor{comment}{! call cpu\_clock\_end(Id\_clock\_setup)}}
\DoxyCodeLine{593     \textcolor{keywordflow}{else}  \textcolor{comment}{! Land points, still inside the i-loop.}}
\DoxyCodeLine{594       \textcolor{keywordflow}{do} k=1,nz+1}
\DoxyCodeLine{595         kappa\_2d(i,k,j2) = 0.0 ; tke\_2d(i,k) = 0.0}
\DoxyCodeLine{596 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{597 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i-loop}}
\DoxyCodeLine{598 }
\DoxyCodeLine{599     \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=isb,ieb}
\DoxyCodeLine{600       tke\_io(i,j,k) = g\%mask2dBu(i,j) * tke\_2d(i,k)}
\DoxyCodeLine{601       kv\_io(i,j,k) = ( g\%mask2dBu(i,j) * kappa\_2d(i,k,j2) ) * cs\%Prandtl\_turb}
\DoxyCodeLine{602 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{603     \textcolor{keywordflow}{if} (j>=g\%jsc) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=g\%isc,g\%iec}
\DoxyCodeLine{604       \textcolor{comment}{! Set the diffusivities in tracer columns from the values at vertices.}}
\DoxyCodeLine{605       kappa\_io(i,j,k) = g\%mask2dT(i,j) * 0.25 * \&}
\DoxyCodeLine{606                         ((kappa\_2d(i-1,k,j2m1) + kappa\_2d(i,k,j2)) + \&}
\DoxyCodeLine{607                          (kappa\_2d(i-1,k,j2)   + kappa\_2d(i,k,j2m1)))}
\DoxyCodeLine{608 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{609 }
\DoxyCodeLine{610 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! end of J-loop}}
\DoxyCodeLine{611 }
\DoxyCodeLine{612   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{613     \textcolor{keyword}{call }hchksum(kappa\_io, \textcolor{stringliteral}{"kappa"}, g\%HI, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{614     \textcolor{keyword}{call }bchksum(tke\_io, \textcolor{stringliteral}{"tke"}, g\%HI, scale=us\%Z\_to\_m**2*us\%s\_to\_T**2)}
\DoxyCodeLine{615 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{616 }
\DoxyCodeLine{617   \textcolor{keywordflow}{if} (cs\%id\_Kd\_shear > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_shear, kappa\_io, cs\%diag)}
\DoxyCodeLine{618   \textcolor{keywordflow}{if} (cs\%id\_TKE > 0) \textcolor{keyword}{call }post\_data(cs\%id\_TKE, tke\_io, cs\%diag)}
\DoxyCodeLine{619 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__kappa__shear_a3f00b08e1174690d40c0c2065fa9a8b1}\label{namespacemom__kappa__shear_a3f00b08e1174690d40c0c2065fa9a8b1}} 
\index{mom\_kappa\_shear@{mom\_kappa\_shear}!calculate\_kappa\_shear@{calculate\_kappa\_shear}}
\index{calculate\_kappa\_shear@{calculate\_kappa\_shear}!mom\_kappa\_shear@{mom\_kappa\_shear}}
\subsubsection{\texorpdfstring{calculate\_kappa\_shear()}{calculate\_kappa\_shear()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+kappa\+\_\+shear\+::calculate\+\_\+kappa\+\_\+shear (\begin{DoxyParamCaption}\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{u\+\_\+in,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{v\+\_\+in,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{h,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{real, dimension(\+:,\+:), pointer}]{p\+\_\+surf,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)+1), intent(inout)}]{kappa\+\_\+io,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)+1), intent(out)}]{tke\+\_\+io,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)+1), intent(inout)}]{kv\+\_\+io,  }\item[{real, intent(in)}]{dt,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(\mbox{\hyperlink{structmom__kappa__shear_1_1kappa__shear__cs}{kappa\+\_\+shear\+\_\+cs}}), pointer}]{CS,  }\item[{logical, intent(in), optional}]{initialize\+\_\+all }\end{DoxyParamCaption})}



Subroutine for calculating shear-\/driven diffusivity and T\+KE in tracer columns. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em u\+\_\+in} & Initial zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em v\+\_\+in} & Initial meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thicknesses \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs. \\
\hline
 & {\em p\+\_\+surf} & The pressure at the ocean surface \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]} (or N\+U\+LL). \\
\hline
\mbox{\texttt{ in,out}}  & {\em kappa\+\_\+io} & The diapycnal diffusivity at each interface \\
\hline
\mbox{\texttt{ out}}  & {\em tke\+\_\+io} & The turbulent kinetic energy per unit mass at \\
\hline
\mbox{\texttt{ in,out}}  & {\em kv\+\_\+io} & The vertical viscosity at each interface \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
 & {\em cs} & The control structure returned by a previous call to kappa\+\_\+shear\+\_\+init. \\
\hline
\mbox{\texttt{ in}}  & {\em initialize\+\_\+all} & If present and false, the previous value of kappa is used to start the iterations \\
\hline
\end{DoxyParams}


Definition at line 111 of file M\+O\+M\+\_\+kappa\+\_\+shear.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{111   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{      !< The ocean's grid structure.}}
\DoxyCodeLine{112   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{     !< The ocean's vertical grid structure.}}
\DoxyCodeLine{113   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{     !< A dimensional unit scaling type}}
\DoxyCodeLine{114 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))},   \&}
\DoxyCodeLine{115                            \textcolor{keywordtype}{intent(in)}    :: u\_in\textcolor{comment}{   !< Initial zonal velocity [L T-1 ~> m s-1].}}
\DoxyCodeLine{116 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))},   \&}
\DoxyCodeLine{117                            \textcolor{keywordtype}{intent(in)}    :: v\_in\textcolor{comment}{   !< Initial meridional velocity [L T-1 ~> m s-1].}}
\DoxyCodeLine{118 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))},   \&}
\DoxyCodeLine{119                            \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{      !< Layer thicknesses [H ~> m or kg m-2].}}
\DoxyCodeLine{120   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),   \textcolor{keywordtype}{intent(in)}    :: tv\textcolor{comment}{     !< A structure containing pointers to any}}
\DoxyCodeLine{121 \textcolor{comment}{                                                   !! available thermodynamic fields. Absent fields}}
\DoxyCodeLine{122 \textcolor{comment}{                                                   !! have NULL ptrs.}}
\DoxyCodeLine{123 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(:,:)},    \textcolor{keywordtype}{pointer}       :: p\_surf\textcolor{comment}{ !< The pressure at the ocean surface [R L2 T-2 ~> Pa] (or NULL).}}
\DoxyCodeLine{124 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV)+1)}, \&}
\DoxyCodeLine{125                            \textcolor{keywordtype}{intent(inout)} :: kappa\_io\textcolor{comment}{ !< The diapycnal diffusivity at each interface}}
\DoxyCodeLine{126 \textcolor{comment}{                                                   !! (not layer!) [Z2 T-1 ~> m2 s-1].  Initially this is the}}
\DoxyCodeLine{127 \textcolor{comment}{                                                   !! value from the previous timestep, which may}}
\DoxyCodeLine{128 \textcolor{comment}{                                                   !! accelerate the iteration toward convergence.}}
\DoxyCodeLine{129 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV)+1)}, \&}
\DoxyCodeLine{130                            \textcolor{keywordtype}{intent(out)} :: tke\_io\textcolor{comment}{   !< The turbulent kinetic energy per unit mass at}}
\DoxyCodeLine{131 \textcolor{comment}{                                                   !! each interface (not layer!) [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{132 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV)+1)}, \&}
\DoxyCodeLine{133                            \textcolor{keywordtype}{intent(inout)} :: kv\_io\textcolor{comment}{  !< The vertical viscosity at each interface}}
\DoxyCodeLine{134 \textcolor{comment}{                                                   !! (not layer!) [Z2 T-1 ~> m2 s-1]. This discards any}}
\DoxyCodeLine{135 \textcolor{comment}{                                                   !! previous value (i.e. it is intent out) and}}
\DoxyCodeLine{136 \textcolor{comment}{                                                   !! simply sets Kv = Prandtl * Kd\_shear}}
\DoxyCodeLine{137 \textcolor{keywordtype}{  real},                    \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{     !< Time increment [T ~> s].}}
\DoxyCodeLine{138   \textcolor{keywordtype}{type}(Kappa\_shear\_CS),    \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{     !< The control structure returned by a previous}}
\DoxyCodeLine{139 \textcolor{comment}{                                                   !! call to kappa\_shear\_init.}}
\DoxyCodeLine{140   \textcolor{keywordtype}{logical},       \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}    :: initialize\_all\textcolor{comment}{ !< If present and false, the previous}}
\DoxyCodeLine{141 \textcolor{comment}{                                                   !! value of kappa is used to start the iterations}}
\DoxyCodeLine{142 }
\DoxyCodeLine{143   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{144 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV))} :: \&}
\DoxyCodeLine{145     h\_2d, \&             \textcolor{comment}{! A 2-D version of h, but converted to [Z ~> m].}}
\DoxyCodeLine{146     u\_2d, v\_2d, \&       \textcolor{comment}{! 2-D versions of u\_in and v\_in, converted to [L T-1 ~> m s-1].}}
\DoxyCodeLine{147     T\_2d, S\_2d, rho\_2d  \textcolor{comment}{! 2-D versions of T [degC], S [ppt], and rho [R ~> kg m-3].}}
\DoxyCodeLine{148 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZK\_(GV)+1)} :: \&}
\DoxyCodeLine{149     kappa\_2d, \& \textcolor{comment}{! 2-D version of kappa\_io [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{150     tke\_2d      \textcolor{comment}{! 2-D version tke\_io [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{151 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV))} :: \&}
\DoxyCodeLine{152     Idz, \&      \textcolor{comment}{! The inverse of the distance between TKE points [Z-1 ~> m-1].}}
\DoxyCodeLine{153     dz, \&       \textcolor{comment}{! The layer thickness [Z ~> m].}}
\DoxyCodeLine{154     u0xdz, \&    \textcolor{comment}{! The initial zonal velocity times dz [Z L T-1 ~> m2 s-1].}}
\DoxyCodeLine{155     v0xdz, \&    \textcolor{comment}{! The initial meridional velocity times dz [Z L T-1 ~> m2 s-1].}}
\DoxyCodeLine{156     T0xdz, \&    \textcolor{comment}{! The initial temperature times dz [degC Z ~> degC m].}}
\DoxyCodeLine{157     S0xdz       \textcolor{comment}{! The initial salinity times dz [ppt Z ~> ppt m].}}
\DoxyCodeLine{158 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV)+1)} :: \&}
\DoxyCodeLine{159     kappa, \&    \textcolor{comment}{! The shear-driven diapycnal diffusivity at an interface [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{160     tke, \&      \textcolor{comment}{! The Turbulent Kinetic Energy per unit mass at an interface [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{161     kappa\_avg, \& \textcolor{comment}{! The time-weighted average of kappa [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{162     tke\_avg     \textcolor{comment}{! The time-weighted average of TKE [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{163 \textcolor{keywordtype}{  real} :: f2   \textcolor{comment}{! The squared Coriolis parameter of each column [T-2 ~> s-2].}}
\DoxyCodeLine{164 \textcolor{keywordtype}{  real} :: surface\_pres  \textcolor{comment}{! The top surface pressure [R L2 T-2 ~> Pa].}}
\DoxyCodeLine{165 }
\DoxyCodeLine{166 \textcolor{keywordtype}{  real} :: dz\_in\_lay     \textcolor{comment}{!   The running sum of the thickness in a layer [Z ~> m].}}
\DoxyCodeLine{167 \textcolor{keywordtype}{  real} :: k0dt          \textcolor{comment}{! The background diffusivity times the timestep [Z2 ~> m2].}}
\DoxyCodeLine{168 \textcolor{keywordtype}{  real} :: dz\_massless   \textcolor{comment}{! A layer thickness that is considered massless [Z ~> m].}}
\DoxyCodeLine{169   \textcolor{keywordtype}{logical} :: use\_temperature  \textcolor{comment}{!  If true, temperature and salinity have been}}
\DoxyCodeLine{170                         \textcolor{comment}{! allocated and are being used as state variables.}}
\DoxyCodeLine{171   \textcolor{keywordtype}{logical} :: new\_kappa = .true. \textcolor{comment}{! If true, ignore the value of kappa from the}}
\DoxyCodeLine{172                         \textcolor{comment}{! last call to this subroutine.}}
\DoxyCodeLine{173 }
\DoxyCodeLine{174   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZK\_(GV)+1)} :: kc \textcolor{comment}{! The index map between the original}}
\DoxyCodeLine{175                         \textcolor{comment}{! interfaces and the interfaces with massless layers}}
\DoxyCodeLine{176                         \textcolor{comment}{! merged into nearby massive layers.}}
\DoxyCodeLine{177 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV)+1)} :: kf \textcolor{comment}{! The fractional weight of interface kc+1 for}}
\DoxyCodeLine{178                         \textcolor{comment}{! interpolating back to the original index space [nondim].}}
\DoxyCodeLine{179   \textcolor{keywordtype}{integer} :: is, ie, js, je, i, j, k, nz, nzc}
\DoxyCodeLine{180 }
\DoxyCodeLine{181   is = g\%isc ; ie = g\%iec; js = g\%jsc ; je = g\%jec ; nz = gv\%ke}
\DoxyCodeLine{182 }
\DoxyCodeLine{183   use\_temperature = .false. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%T)) use\_temperature = .true.}
\DoxyCodeLine{184   new\_kappa = .true. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(initialize\_all)) new\_kappa = initialize\_all}
\DoxyCodeLine{185 }
\DoxyCodeLine{186   k0dt = dt*cs\%kappa\_0}
\DoxyCodeLine{187   dz\_massless = 0.1*sqrt(k0dt)}
\DoxyCodeLine{188 }
\DoxyCodeLine{189   \textcolor{comment}{!\$OMP parallel do default(private) shared(js,je,is,ie,nz,h,u\_in,v\_in,use\_temperature,new\_kappa, \&}}
\DoxyCodeLine{190   \textcolor{comment}{!\$OMP                                tv,G,GV,US,CS,kappa\_io,dz\_massless,k0dt,p\_surf,dt,tke\_io,kv\_io)}}
\DoxyCodeLine{191   \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{192     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{193       h\_2d(i,k) = h(i,j,k)*gv\%H\_to\_Z}
\DoxyCodeLine{194       u\_2d(i,k) = u\_in(i,j,k) ; v\_2d(i,k) = v\_in(i,j,k)}
\DoxyCodeLine{195 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{196     \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{197       t\_2d(i,k) = tv\%T(i,j,k) ; s\_2d(i,k) = tv\%S(i,j,k)}
\DoxyCodeLine{198 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ; \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{199       rho\_2d(i,k) = gv\%Rlay(k) \textcolor{comment}{! Could be tv\%Rho(i,j,k) ?}}
\DoxyCodeLine{200 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{201     \textcolor{keywordflow}{if} (.not.new\_kappa) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{202       kappa\_2d(i,k) = kappa\_io(i,j,k)}
\DoxyCodeLine{203 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{204 }
\DoxyCodeLine{205 \textcolor{comment}{!---------------------------------------}}
\DoxyCodeLine{206 \textcolor{comment}{! Work on each column.}}
\DoxyCodeLine{207 \textcolor{comment}{!---------------------------------------}}
\DoxyCodeLine{208     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (g\%mask2dT(i,j) > 0.5) \textcolor{keywordflow}{then}}
\DoxyCodeLine{209     \textcolor{comment}{! call cpu\_clock\_begin(id\_clock\_setup)}}
\DoxyCodeLine{210       \textcolor{comment}{! Store a transposed version of the initial arrays.}}
\DoxyCodeLine{211       \textcolor{comment}{! Any elimination of massless layers would occur here.}}
\DoxyCodeLine{212       \textcolor{keywordflow}{if} (cs\%eliminate\_massless) \textcolor{keywordflow}{then}}
\DoxyCodeLine{213         nzc = 1}
\DoxyCodeLine{214         \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{215           \textcolor{comment}{! Zero out the thicknesses of all layers, even if they are unused.}}
\DoxyCodeLine{216           dz(k) = 0.0 ; u0xdz(k) = 0.0 ; v0xdz(k) = 0.0}
\DoxyCodeLine{217           t0xdz(k) = 0.0 ; s0xdz(k) = 0.0}
\DoxyCodeLine{218 }
\DoxyCodeLine{219           \textcolor{comment}{! Add a new layer if this one has mass.}}
\DoxyCodeLine{220 \textcolor{comment}{!          if ((dz(nzc) > 0.0) .and. (h\_2d(i,k) > dz\_massless)) nzc = nzc+1}}
\DoxyCodeLine{221           \textcolor{keywordflow}{if} ((k>cs\%nkml) .and. (dz(nzc) > 0.0) .and. \&}
\DoxyCodeLine{222               (h\_2d(i,k) > dz\_massless)) nzc = nzc+1}
\DoxyCodeLine{223 }
\DoxyCodeLine{224           \textcolor{comment}{! Only merge clusters of massless layers.}}
\DoxyCodeLine{225 \textcolor{comment}{!         if ((dz(nzc) > dz\_massless) .or. \&}}
\DoxyCodeLine{226 \textcolor{comment}{!             ((dz(nzc) > 0.0) .and. (h\_2d(i,k) > dz\_massless))) nzc = nzc+1}}
\DoxyCodeLine{227 }
\DoxyCodeLine{228           kc(k) = nzc}
\DoxyCodeLine{229           dz(nzc) = dz(nzc) + h\_2d(i,k)}
\DoxyCodeLine{230           u0xdz(nzc) = u0xdz(nzc) + u\_2d(i,k)*h\_2d(i,k)}
\DoxyCodeLine{231           v0xdz(nzc) = v0xdz(nzc) + v\_2d(i,k)*h\_2d(i,k)}
\DoxyCodeLine{232           \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then}}
\DoxyCodeLine{233             t0xdz(nzc) = t0xdz(nzc) + t\_2d(i,k)*h\_2d(i,k)}
\DoxyCodeLine{234             s0xdz(nzc) = s0xdz(nzc) + s\_2d(i,k)*h\_2d(i,k)}
\DoxyCodeLine{235           \textcolor{keywordflow}{else}}
\DoxyCodeLine{236             t0xdz(nzc) = t0xdz(nzc) + rho\_2d(i,k)*h\_2d(i,k)}
\DoxyCodeLine{237             s0xdz(nzc) = s0xdz(nzc) + rho\_2d(i,k)*h\_2d(i,k)}
\DoxyCodeLine{238 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{239 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{240         kc(nz+1) = nzc+1}
\DoxyCodeLine{241 }
\DoxyCodeLine{242         \textcolor{comment}{! Set up Idz as the inverse of layer thicknesses.}}
\DoxyCodeLine{243         \textcolor{keywordflow}{do} k=1,nzc ; idz(k) = 1.0 / dz(k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{244 }
\DoxyCodeLine{245         \textcolor{comment}{!   Now determine kf, the fractional weight of interface kc when}}
\DoxyCodeLine{246         \textcolor{comment}{! interpolating between interfaces kc and kc+1.}}
\DoxyCodeLine{247         kf(1) = 0.0 ; dz\_in\_lay = h\_2d(i,1)}
\DoxyCodeLine{248         \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{249           \textcolor{keywordflow}{if} (kc(k) > kc(k-1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{250             kf(k) = 0.0 ; dz\_in\_lay = h\_2d(i,k)}
\DoxyCodeLine{251           \textcolor{keywordflow}{else}}
\DoxyCodeLine{252             kf(k) = dz\_in\_lay*idz(kc(k)) ; dz\_in\_lay = dz\_in\_lay + h\_2d(i,k)}
\DoxyCodeLine{253 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{254 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{255         kf(nz+1) = 0.0}
\DoxyCodeLine{256       \textcolor{keywordflow}{else}}
\DoxyCodeLine{257         \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{258           dz(k) = h\_2d(i,k)}
\DoxyCodeLine{259           u0xdz(k) = u\_2d(i,k)*dz(k) ; v0xdz(k) = v\_2d(i,k)*dz(k)}
\DoxyCodeLine{260 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{261         \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then}}
\DoxyCodeLine{262           \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{263             t0xdz(k) = t\_2d(i,k)*dz(k) ; s0xdz(k) = s\_2d(i,k)*dz(k)}
\DoxyCodeLine{264 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{265         \textcolor{keywordflow}{else}}
\DoxyCodeLine{266           \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{267             t0xdz(k) = rho\_2d(i,k)*dz(k) ; s0xdz(k) = rho\_2d(i,k)*dz(k)}
\DoxyCodeLine{268 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{269 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{270         nzc = nz}
\DoxyCodeLine{271         \textcolor{keywordflow}{do} k=1,nzc+1 ; kc(k) = k ; kf(k) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{272 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{273       f2 = 0.25 * ((g\%CoriolisBu(i,j)**2 + g\%CoriolisBu(i-1,j-1)**2) + \&}
\DoxyCodeLine{274                    (g\%CoriolisBu(i,j-1)**2 + g\%CoriolisBu(i-1,j)**2))}
\DoxyCodeLine{275       surface\_pres = 0.0 ; \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(p\_surf)) surface\_pres = p\_surf(i,j)}
\DoxyCodeLine{276 }
\DoxyCodeLine{277     \textcolor{comment}{! ----------------------------------------------------    I\_Ld2\_1d, dz\_Int\_1d}}
\DoxyCodeLine{278 }
\DoxyCodeLine{279     \textcolor{comment}{! Set the initial guess for kappa, here defined at interfaces.}}
\DoxyCodeLine{280     \textcolor{comment}{! ----------------------------------------------------}}
\DoxyCodeLine{281       \textcolor{keywordflow}{if} (new\_kappa) \textcolor{keywordflow}{then}}
\DoxyCodeLine{282         \textcolor{keywordflow}{do} k=1,nzc+1 ; kappa(k) = us\%m2\_s\_to\_Z2\_T*1.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{283       \textcolor{keywordflow}{else}}
\DoxyCodeLine{284         \textcolor{keywordflow}{do} k=1,nzc+1 ; kappa(k) = kappa\_2d(i,k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{285 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{286 }
\DoxyCodeLine{287       \textcolor{keyword}{call }kappa\_shear\_column(kappa, tke, dt, nzc, f2, surface\_pres, \&}
\DoxyCodeLine{288                               dz, u0xdz, v0xdz, t0xdz, s0xdz, kappa\_avg, \&}
\DoxyCodeLine{289                               tke\_avg, tv, cs, gv, us)}
\DoxyCodeLine{290 }
\DoxyCodeLine{291     \textcolor{comment}{! call cpu\_clock\_begin(id\_clock\_setup)}}
\DoxyCodeLine{292     \textcolor{comment}{! Extrapolate from the vertically reduced grid back to the original layers.}}
\DoxyCodeLine{293       \textcolor{keywordflow}{if} (nz == nzc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{294         \textcolor{keywordflow}{do} k=1,nz+1}
\DoxyCodeLine{295           kappa\_2d(i,k) = kappa\_avg(k)}
\DoxyCodeLine{296           \textcolor{keywordflow}{if} (cs\%all\_layer\_TKE\_bug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{297             tke\_2d(i,k) = tke(k)}
\DoxyCodeLine{298           \textcolor{keywordflow}{else}}
\DoxyCodeLine{299             tke\_2d(i,k) = tke\_avg(k)}
\DoxyCodeLine{300 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{301 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{302       \textcolor{keywordflow}{else}}
\DoxyCodeLine{303         \textcolor{keywordflow}{do} k=1,nz+1}
\DoxyCodeLine{304           \textcolor{keywordflow}{if} (kf(k) == 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{305             kappa\_2d(i,k) = kappa\_avg(kc(k))}
\DoxyCodeLine{306             tke\_2d(i,k) = tke\_avg(kc(k))}
\DoxyCodeLine{307           \textcolor{keywordflow}{else}}
\DoxyCodeLine{308             kappa\_2d(i,k) = (1.0-kf(k)) * kappa\_avg(kc(k)) + \&}
\DoxyCodeLine{309                              kf(k) * kappa\_avg(kc(k)+1)}
\DoxyCodeLine{310             tke\_2d(i,k) = (1.0-kf(k)) * tke\_avg(kc(k)) + \&}
\DoxyCodeLine{311                            kf(k) * tke\_avg(kc(k)+1)}
\DoxyCodeLine{312 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{313 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{314 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{315     \textcolor{comment}{! call cpu\_clock\_end(id\_clock\_setup)}}
\DoxyCodeLine{316     \textcolor{keywordflow}{else}  \textcolor{comment}{! Land points, still inside the i-loop.}}
\DoxyCodeLine{317       \textcolor{keywordflow}{do} k=1,nz+1}
\DoxyCodeLine{318         kappa\_2d(i,k) = 0.0 ; tke\_2d(i,k) = 0.0}
\DoxyCodeLine{319 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{320 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i-loop}}
\DoxyCodeLine{321 }
\DoxyCodeLine{322     \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{323       kappa\_io(i,j,k) = g\%mask2dT(i,j) * kappa\_2d(i,k)}
\DoxyCodeLine{324       tke\_io(i,j,k) = g\%mask2dT(i,j) * tke\_2d(i,k)}
\DoxyCodeLine{325       kv\_io(i,j,k) = ( g\%mask2dT(i,j) * kappa\_2d(i,k) ) * cs\%Prandtl\_turb}
\DoxyCodeLine{326 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{327 }
\DoxyCodeLine{328 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! end of j-loop}}
\DoxyCodeLine{329 }
\DoxyCodeLine{330   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{331     \textcolor{keyword}{call }hchksum(kappa\_io, \textcolor{stringliteral}{"kappa"}, g\%HI, scale=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{332     \textcolor{keyword}{call }hchksum(tke\_io, \textcolor{stringliteral}{"tke"}, g\%HI, scale=us\%Z\_to\_m**2*us\%s\_to\_T**2)}
\DoxyCodeLine{333 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{334 }
\DoxyCodeLine{335   \textcolor{keywordflow}{if} (cs\%id\_Kd\_shear > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kd\_shear, kappa\_io, cs\%diag)}
\DoxyCodeLine{336   \textcolor{keywordflow}{if} (cs\%id\_TKE > 0) \textcolor{keyword}{call }post\_data(cs\%id\_TKE, tke\_io, cs\%diag)}
\DoxyCodeLine{337 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__kappa__shear_a0b931b0b834d887e321eb6eb1924fa9a}\label{namespacemom__kappa__shear_a0b931b0b834d887e321eb6eb1924fa9a}} 
\index{mom\_kappa\_shear@{mom\_kappa\_shear}!calculate\_projected\_state@{calculate\_projected\_state}}
\index{calculate\_projected\_state@{calculate\_projected\_state}!mom\_kappa\_shear@{mom\_kappa\_shear}}
\subsubsection{\texorpdfstring{calculate\_projected\_state()}{calculate\_projected\_state()}}
{\footnotesize\ttfamily subroutine mom\+\_\+kappa\+\_\+shear\+::calculate\+\_\+projected\+\_\+state (\begin{DoxyParamCaption}\item[{real, dimension(nz+1), intent(in)}]{kappa,  }\item[{real, dimension(nz), intent(in)}]{u0,  }\item[{real, dimension(nz), intent(in)}]{v0,  }\item[{real, dimension(nz), intent(in)}]{T0,  }\item[{real, dimension(nz), intent(in)}]{S0,  }\item[{real, intent(in)}]{dt,  }\item[{integer, intent(in)}]{nz,  }\item[{real, dimension(nz), intent(in)}]{dz,  }\item[{real, dimension(nz+1), intent(in)}]{I\+\_\+dz\+\_\+int,  }\item[{real, dimension(nz+1), intent(in)}]{dbuoy\+\_\+dT,  }\item[{real, dimension(nz+1), intent(in)}]{dbuoy\+\_\+dS,  }\item[{real, dimension(nz), intent(inout)}]{u,  }\item[{real, dimension(nz), intent(inout)}]{v,  }\item[{real, dimension(nz), intent(inout)}]{T,  }\item[{real, dimension(nz), intent(inout)}]{Sal,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{real, dimension(nz+1), intent(inout), optional}]{N2,  }\item[{real, dimension(nz+1), intent(inout), optional}]{S2,  }\item[{integer, intent(in), optional}]{ks\+\_\+int,  }\item[{integer, intent(in), optional}]{ke\+\_\+int,  }\item[{real, intent(in), optional}]{vel\+\_\+underflow }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine calculates the velocities, temperature and salinity that the water column will have after mixing for dt with diffusivities kappa. It may also calculate the projected buoyancy frequency and shear. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em nz} & The number of layers (after eliminating massless layers?). \\
\hline
\mbox{\texttt{ in}}  & {\em kappa} & The diapycnal diffusivity at interfaces, \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em u0} & The initial zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em v0} & The initial meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em t0} & The initial temperature \mbox{[}degC\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em s0} & The initial salinity \mbox{[}ppt\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dz} & The grid spacing of layers \mbox{[}Z $\sim$$>$ m\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em i\+\_\+dz\+\_\+int} & The inverse of the layer\textquotesingle{}s thicknesses \mbox{[}Z-\/1 $\sim$$>$ m-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dbuoy\+\_\+dt} & The partial derivative of buoyancy with temperature \mbox{[}Z T-\/2 deg\+C-\/1 $\sim$$>$ m s-\/2 deg\+C-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dbuoy\+\_\+ds} & The partial derivative of buoyancy with salinity \mbox{[}Z T-\/2 ppt-\/1 $\sim$$>$ m s-\/2 ppt-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & The time step \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em u} & The zonal velocity after dt \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em v} & The meridional velocity after dt \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em t} & The temperature after dt \mbox{[}degC\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em sal} & The salinity after dt \mbox{[}ppt\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in,out}}  & {\em n2} & The buoyancy frequency squared at interfaces \mbox{[}T-\/2 $\sim$$>$ s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in,out}}  & {\em s2} & The squared shear at interfaces \mbox{[}T-\/2 $\sim$$>$ s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em ks\+\_\+int} & The topmost k-\/index with a non-\/zero diffusivity. \\
\hline
\mbox{\texttt{ in}}  & {\em ke\+\_\+int} & The bottommost k-\/index with a non-\/zero diffusivity. \\
\hline
\mbox{\texttt{ in}}  & {\em vel\+\_\+underflow} & If present and true, any velocities that are smaller in magnitude than this value are set to 0 \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\end{DoxyParams}


Definition at line 1082 of file M\+O\+M\+\_\+kappa\+\_\+shear.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1082   \textcolor{keywordtype}{integer},               \textcolor{keywordtype}{intent(in)}    :: nz\textcolor{comment}{  !< The number of layers (after eliminating massless}}
\DoxyCodeLine{1083 \textcolor{comment}{                                              !! layers?).}}
\DoxyCodeLine{1084 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(in)}    :: kappa\textcolor{comment}{ !< The diapycnal diffusivity at interfaces,}}
\DoxyCodeLine{1085 \textcolor{comment}{                                              !! [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{1086 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(in)}    :: u0\textcolor{comment}{  !< The initial zonal velocity [L T-1 ~> m s-1].}}
\DoxyCodeLine{1087 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(in)}    :: v0\textcolor{comment}{  !< The initial meridional velocity [L T-1 ~> m s-1].}}
\DoxyCodeLine{1088 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(in)}    :: T0\textcolor{comment}{  !< The initial temperature [degC].}}
\DoxyCodeLine{1089 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(in)}    :: S0\textcolor{comment}{  !< The initial salinity [ppt].}}
\DoxyCodeLine{1090 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(in)}    :: dz\textcolor{comment}{  !< The grid spacing of layers [Z ~> m].}}
\DoxyCodeLine{1091 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(in)}    :: I\_dz\_int\textcolor{comment}{ !< The inverse of the layer's thicknesses}}
\DoxyCodeLine{1092 \textcolor{comment}{                                              !! [Z-1 ~> m-1].}}
\DoxyCodeLine{1093 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(in)}    :: dbuoy\_dT\textcolor{comment}{ !< The partial derivative of buoyancy with}}
\DoxyCodeLine{1094 \textcolor{comment}{                                              !! temperature [Z T-2 degC-1 ~> m s-2 degC-1].}}
\DoxyCodeLine{1095 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(in)}    :: dbuoy\_dS\textcolor{comment}{ !< The partial derivative of buoyancy with}}
\DoxyCodeLine{1096 \textcolor{comment}{                                              !! salinity [Z T-2 ppt-1 ~> m s-2 ppt-1].}}
\DoxyCodeLine{1097 \textcolor{keywordtype}{  real},                  \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{  !< The time step [T ~> s].}}
\DoxyCodeLine{1098 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(inout)} :: u\textcolor{comment}{   !< The zonal velocity after dt [L T-1 ~> m s-1].}}
\DoxyCodeLine{1099 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(inout)} :: v\textcolor{comment}{   !< The meridional velocity after dt [L T-1 ~> m s-1].}}
\DoxyCodeLine{1100 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(inout)} :: T\textcolor{comment}{   !< The temperature after dt [degC].}}
\DoxyCodeLine{1101 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(inout)} :: Sal\textcolor{comment}{ !< The salinity after dt [ppt].}}
\DoxyCodeLine{1102   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}  :: GV\textcolor{comment}{  !< The ocean's vertical grid structure.}}
\DoxyCodeLine{1103   \textcolor{keywordtype}{type}(unit\_scale\_type), \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{  !< A dimensional unit scaling type}}
\DoxyCodeLine{1104 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{optional}, \&}
\DoxyCodeLine{1105                          \textcolor{keywordtype}{intent(inout)} :: N2\textcolor{comment}{  !< The buoyancy frequency squared at interfaces [T-2 ~> s-2].}}
\DoxyCodeLine{1106 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{optional}, \&}
\DoxyCodeLine{1107                          \textcolor{keywordtype}{intent(inout)} :: S2\textcolor{comment}{  !< The squared shear at interfaces [T-2 ~> s-2].}}
\DoxyCodeLine{1108   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{optional},     \textcolor{keywordtype}{intent(in)}    :: ks\_int\textcolor{comment}{ !< The topmost k-index with a non-zero diffusivity.}}
\DoxyCodeLine{1109   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{optional},     \textcolor{keywordtype}{intent(in)}    :: ke\_int\textcolor{comment}{ !< The bottommost k-index with a non-zero}}
\DoxyCodeLine{1110 \textcolor{comment}{                                              !! diffusivity.}}
\DoxyCodeLine{1111 \textcolor{keywordtype}{  real},    \textcolor{keywordtype}{optional},     \textcolor{keywordtype}{intent(in)}    :: vel\_underflow\textcolor{comment}{ !< If present and true, any velocities that}}
\DoxyCodeLine{1112 \textcolor{comment}{                                              !! are smaller in magnitude than this value are}}
\DoxyCodeLine{1113 \textcolor{comment}{                                              !! set to 0 [L T-1 ~> m s-1].}}
\DoxyCodeLine{1114 }
\DoxyCodeLine{1115   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1116 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)} :: c1}
\DoxyCodeLine{1117 \textcolor{keywordtype}{  real} :: L2\_to\_Z2       \textcolor{comment}{! A conversion factor from horizontal length units to vertical depth}}
\DoxyCodeLine{1118                          \textcolor{comment}{! units squared [Z2 s2 T-2 m-2 ~> 1].}}
\DoxyCodeLine{1119 \textcolor{keywordtype}{  real} :: underflow\_vel  \textcolor{comment}{! Velocities smaller in magnitude than underflow\_vel are set to 0 [L T-1 ~> m s-1].}}
\DoxyCodeLine{1120 \textcolor{keywordtype}{  real} :: a\_a, a\_b, b1, d1, bd1, b1nz\_0}
\DoxyCodeLine{1121   \textcolor{keywordtype}{integer} :: k, ks, ke}
\DoxyCodeLine{1122 }
\DoxyCodeLine{1123   ks = 1 ; ke = nz}
\DoxyCodeLine{1124   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ks\_int)) ks = max(ks\_int-1,1)}
\DoxyCodeLine{1125   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(ke\_int)) ke = min(ke\_int,nz)}
\DoxyCodeLine{1126   underflow\_vel = 0.0 ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(vel\_underflow)) underflow\_vel = vel\_underflow}
\DoxyCodeLine{1127 }
\DoxyCodeLine{1128   \textcolor{keywordflow}{if} (ks > ke) \textcolor{keywordflow}{return}}
\DoxyCodeLine{1129 }
\DoxyCodeLine{1130   \textcolor{keywordflow}{if} (dt > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1131     a\_b = dt*(kappa(ks+1)*i\_dz\_int(ks+1))}
\DoxyCodeLine{1132     b1 = 1.0 / (dz(ks) + a\_b)}
\DoxyCodeLine{1133     c1(ks+1) = a\_b * b1 ; d1 = dz(ks) * b1 \textcolor{comment}{! = 1 - c1}}
\DoxyCodeLine{1134 }
\DoxyCodeLine{1135     u(ks) = (b1 * dz(ks))*u0(ks) ; v(ks) = (b1 * dz(ks))*v0(ks)}
\DoxyCodeLine{1136     t(ks) = (b1 * dz(ks))*t0(ks) ; sal(ks) = (b1 * dz(ks))*s0(ks)}
\DoxyCodeLine{1137     \textcolor{keywordflow}{do} k=ks+1,ke-1}
\DoxyCodeLine{1138       a\_a = a\_b}
\DoxyCodeLine{1139       a\_b = dt*(kappa(k+1)*i\_dz\_int(k+1))}
\DoxyCodeLine{1140       bd1 = dz(k) + d1*a\_a}
\DoxyCodeLine{1141       b1 = 1.0 / (bd1 + a\_b)}
\DoxyCodeLine{1142       c1(k+1) = a\_b * b1 ; d1 = bd1 * b1 \textcolor{comment}{! d1 = 1 - c1}}
\DoxyCodeLine{1143 }
\DoxyCodeLine{1144       u(k) = b1 * (dz(k)*u0(k) + a\_a*u(k-1))}
\DoxyCodeLine{1145       v(k) = b1 * (dz(k)*v0(k) + a\_a*v(k-1))}
\DoxyCodeLine{1146       t(k) = b1 * (dz(k)*t0(k) + a\_a*t(k-1))}
\DoxyCodeLine{1147       sal(k) = b1 * (dz(k)*s0(k) + a\_a*sal(k-1))}
\DoxyCodeLine{1148 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1149     \textcolor{comment}{!   T and S have insulating boundary conditions, u \& v use no-slip}}
\DoxyCodeLine{1150     \textcolor{comment}{! bottom boundary conditions at the solid bottom.}}
\DoxyCodeLine{1151 }
\DoxyCodeLine{1152     \textcolor{comment}{! For insulating boundary conditions or mixing simply stopping, use...}}
\DoxyCodeLine{1153     a\_a = a\_b}
\DoxyCodeLine{1154     b1 = 1.0 / (dz(ke) + d1*a\_a)}
\DoxyCodeLine{1155     t(ke) = b1 * (dz(ke)*t0(ke) + a\_a*t(ke-1))}
\DoxyCodeLine{1156     sal(ke) = b1 * (dz(ke)*s0(ke) + a\_a*sal(ke-1))}
\DoxyCodeLine{1157 }
\DoxyCodeLine{1158     \textcolor{comment}{!   There is no distinction between the effective boundary conditions for}}
\DoxyCodeLine{1159     \textcolor{comment}{! tracers and velocities if the mixing is separated from the bottom, but if}}
\DoxyCodeLine{1160     \textcolor{comment}{! the mixing goes all the way to the bottom, use no-slip BCs for velocities.}}
\DoxyCodeLine{1161     \textcolor{keywordflow}{if} (ke == nz) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1162       a\_b = dt*(kappa(nz+1)*i\_dz\_int(nz+1))}
\DoxyCodeLine{1163       b1nz\_0 = 1.0 / ((dz(nz) + d1*a\_a) + a\_b)}
\DoxyCodeLine{1164     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1165       b1nz\_0 = b1}
\DoxyCodeLine{1166 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1167     u(ke) = b1nz\_0 * (dz(ke)*u0(ke) + a\_a*u(ke-1))}
\DoxyCodeLine{1168     v(ke) = b1nz\_0 * (dz(ke)*v0(ke) + a\_a*v(ke-1))}
\DoxyCodeLine{1169     \textcolor{keywordflow}{if} (abs(u(ke)) < underflow\_vel) u(ke) = 0.0}
\DoxyCodeLine{1170     \textcolor{keywordflow}{if} (abs(v(ke)) < underflow\_vel) v(ke) = 0.0}
\DoxyCodeLine{1171 }
\DoxyCodeLine{1172     \textcolor{keywordflow}{do} k=ke-1,ks,-1}
\DoxyCodeLine{1173       u(k) = u(k) + c1(k+1)*u(k+1)}
\DoxyCodeLine{1174       v(k) = v(k) + c1(k+1)*v(k+1)}
\DoxyCodeLine{1175       \textcolor{keywordflow}{if} (abs(u(k)) < underflow\_vel) u(k) = 0.0}
\DoxyCodeLine{1176       \textcolor{keywordflow}{if} (abs(v(k)) < underflow\_vel) v(k) = 0.0}
\DoxyCodeLine{1177       t(k) = t(k) + c1(k+1)*t(k+1)}
\DoxyCodeLine{1178       sal(k) = sal(k) + c1(k+1)*sal(k+1)}
\DoxyCodeLine{1179 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1180   \textcolor{keywordflow}{else} \textcolor{comment}{! dt <= 0.0}}
\DoxyCodeLine{1181     \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{1182       u(k) = u0(k) ; v(k) = v0(k) ; t(k) = t0(k) ; sal(k) = s0(k)}
\DoxyCodeLine{1183       \textcolor{keywordflow}{if} (abs(u(k)) < underflow\_vel) u(k) = 0.0}
\DoxyCodeLine{1184       \textcolor{keywordflow}{if} (abs(v(k)) < underflow\_vel) v(k) = 0.0}
\DoxyCodeLine{1185 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1186 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1187 }
\DoxyCodeLine{1188   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(s2)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1189     \textcolor{comment}{! L2\_to\_Z2 = US\%m\_to\_Z**2 * US\%T\_to\_s**2}}
\DoxyCodeLine{1190     l2\_to\_z2 = us\%L\_to\_Z**2}
\DoxyCodeLine{1191     s2(1) = 0.0 ; s2(nz+1) = 0.0}
\DoxyCodeLine{1192     \textcolor{keywordflow}{if} (ks > 1) \&}
\DoxyCodeLine{1193       s2(ks) = ((u(ks)-u0(ks-1))**2 + (v(ks)-v0(ks-1))**2) * (l2\_to\_z2*i\_dz\_int(ks)**2)}
\DoxyCodeLine{1194     \textcolor{keywordflow}{do} k=ks+1,ke}
\DoxyCodeLine{1195       s2(k) = ((u(k)-u(k-1))**2 + (v(k)-v(k-1))**2) * (l2\_to\_z2*i\_dz\_int(k)**2)}
\DoxyCodeLine{1196 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1197     \textcolor{keywordflow}{if} (ke<nz) \&}
\DoxyCodeLine{1198       s2(ke+1) = ((u0(ke+1)-u(ke))**2 + (v0(ke+1)-v(ke))**2) * (l2\_to\_z2*i\_dz\_int(ke+1)**2)}
\DoxyCodeLine{1199 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1200 }
\DoxyCodeLine{1201   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(n2)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1202     n2(1) = 0.0 ; n2(nz+1) = 0.0}
\DoxyCodeLine{1203     \textcolor{keywordflow}{if} (ks > 1) \&}
\DoxyCodeLine{1204       n2(ks) = max(0.0, i\_dz\_int(ks) * \&}
\DoxyCodeLine{1205         (dbuoy\_dt(ks) * (t0(ks-1)-t(ks)) + dbuoy\_ds(ks) * (s0(ks-1)-sal(ks))))}
\DoxyCodeLine{1206     \textcolor{keywordflow}{do} k=ks+1,ke}
\DoxyCodeLine{1207       n2(k) = max(0.0, i\_dz\_int(k) * \&}
\DoxyCodeLine{1208         (dbuoy\_dt(k) * (t(k-1)-t(k)) + dbuoy\_ds(k) * (sal(k-1)-sal(k))))}
\DoxyCodeLine{1209 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1210     \textcolor{keywordflow}{if} (ke<nz) \&}
\DoxyCodeLine{1211       n2(ke+1) = max(0.0, i\_dz\_int(ke+1) * \&}
\DoxyCodeLine{1212         (dbuoy\_dt(ke+1) * (t(ke)-t0(ke+1)) + dbuoy\_ds(ke+1) * (sal(ke)-s0(ke+1))))}
\DoxyCodeLine{1213 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1214 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__kappa__shear_a351d44e4fe5cfb5852d019a0c1e66100}\label{namespacemom__kappa__shear_a351d44e4fe5cfb5852d019a0c1e66100}} 
\index{mom\_kappa\_shear@{mom\_kappa\_shear}!find\_kappa\_tke@{find\_kappa\_tke}}
\index{find\_kappa\_tke@{find\_kappa\_tke}!mom\_kappa\_shear@{mom\_kappa\_shear}}
\subsubsection{\texorpdfstring{find\_kappa\_tke()}{find\_kappa\_tke()}}
{\footnotesize\ttfamily subroutine mom\+\_\+kappa\+\_\+shear\+::find\+\_\+kappa\+\_\+tke (\begin{DoxyParamCaption}\item[{real, dimension(nz+1), intent(in)}]{N2,  }\item[{real, dimension(nz+1), intent(in)}]{S2,  }\item[{real, dimension(nz+1), intent(in)}]{kappa\+\_\+in,  }\item[{real, dimension(nz), intent(in)}]{Idz,  }\item[{real, dimension(nz+1), intent(in)}]{dz\+\_\+\+Int,  }\item[{real, dimension(nz+1), intent(in)}]{I\+\_\+\+L2\+\_\+bdry,  }\item[{real, intent(in)}]{f2,  }\item[{integer, intent(in)}]{nz,  }\item[{type(\mbox{\hyperlink{structmom__kappa__shear_1_1kappa__shear__cs}{kappa\+\_\+shear\+\_\+cs}}), pointer}]{CS,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{real, dimension(nz+1), intent(inout)}]{K\+\_\+Q,  }\item[{real, dimension(nz+1), intent(out)}]{tke,  }\item[{real, dimension(nz+1), intent(out)}]{kappa,  }\item[{real, dimension(nz+1), intent(out), optional}]{kappa\+\_\+src,  }\item[{real, dimension(nz+1), intent(out), optional}]{local\+\_\+src }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine calculates new, consistent estimates of T\+KE and kappa. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em nz} & The number of layers to work on. \\
\hline
\mbox{\texttt{ in}}  & {\em n2} & The buoyancy frequency squared at interfaces \mbox{[}T-\/2 $\sim$$>$ s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em s2} & The squared shear at interfaces \mbox{[}T-\/2 $\sim$$>$ s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em kappa\+\_\+in} & The initial guess at the diffusivity \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dz\+\_\+int} & The thicknesses associated with interfaces \mbox{[}Z-\/1 $\sim$$>$ m-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em i\+\_\+l2\+\_\+bdry} & The inverse of the squared distance to boundaries \mbox{[}Z-\/2 $\sim$$>$ m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em idz} & The inverse grid spacing of layers \mbox{[}Z-\/1 $\sim$$>$ m-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em f2} & The squared Coriolis parameter \mbox{[}T-\/2 $\sim$$>$ s-\/2\mbox{]}. \\
\hline
 & {\em cs} & A pointer to this module\textquotesingle{}s control structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in,out}}  & {\em k\+\_\+q} & The shear-\/driven diapycnal diffusivity divided by the turbulent kinetic energy per unit mass at interfaces \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em tke} & The turbulent kinetic energy per unit mass at interfaces \mbox{[}Z2 T-\/2 $\sim$$>$ m2 s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em kappa} & The diapycnal diffusivity at interfaces \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em kappa\+\_\+src} & The source term for kappa \mbox{[}T-\/1 $\sim$$>$ s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em local\+\_\+src} & The sum of all local sources for kappa, \\
\hline
\end{DoxyParams}


Definition at line 1220 of file M\+O\+M\+\_\+kappa\+\_\+shear.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1220   \textcolor{keywordtype}{integer},               \textcolor{keywordtype}{intent(in)}    :: nz\textcolor{comment}{  !< The number of layers to work on.}}
\DoxyCodeLine{1221 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(in)}    :: N2\textcolor{comment}{  !< The buoyancy frequency squared at interfaces [T-2 ~> s-2].}}
\DoxyCodeLine{1222 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(in)}    :: S2\textcolor{comment}{  !< The squared shear at interfaces [T-2 ~> s-2].}}
\DoxyCodeLine{1223 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(in)}    :: kappa\_in\textcolor{comment}{  !< The initial guess at the diffusivity}}
\DoxyCodeLine{1224 \textcolor{comment}{                                              !! [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{1225 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(in)}    :: dz\_Int\textcolor{comment}{ !< The thicknesses associated with interfaces}}
\DoxyCodeLine{1226 \textcolor{comment}{                                              !! [Z-1 ~> m-1].}}
\DoxyCodeLine{1227 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(in)}    :: I\_L2\_bdry\textcolor{comment}{ !< The inverse of the squared distance to}}
\DoxyCodeLine{1228 \textcolor{comment}{                                              !! boundaries [Z-2 ~> m-2].}}
\DoxyCodeLine{1229 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz)},   \textcolor{keywordtype}{intent(in)}    :: Idz\textcolor{comment}{ !< The inverse grid spacing of layers [Z-1 ~> m-1].}}
\DoxyCodeLine{1230 \textcolor{keywordtype}{  real},                  \textcolor{keywordtype}{intent(in)}    :: f2\textcolor{comment}{  !< The squared Coriolis parameter [T-2 ~> s-2].}}
\DoxyCodeLine{1231   \textcolor{keywordtype}{type}(Kappa\_shear\_CS),  \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{  !< A pointer to this module's control structure.}}
\DoxyCodeLine{1232   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}  :: GV\textcolor{comment}{  !< The ocean's vertical grid structure.}}
\DoxyCodeLine{1233   \textcolor{keywordtype}{type}(unit\_scale\_type), \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{  !< A dimensional unit scaling type}}
\DoxyCodeLine{1234 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(inout)} :: K\_Q\textcolor{comment}{ !< The shear-driven diapycnal diffusivity divided by}}
\DoxyCodeLine{1235 \textcolor{comment}{                                              !! the turbulent kinetic energy per unit mass at}}
\DoxyCodeLine{1236 \textcolor{comment}{                                              !! interfaces [T ~> s].}}
\DoxyCodeLine{1237 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(out)}   :: tke\textcolor{comment}{ !< The turbulent kinetic energy per unit mass at}}
\DoxyCodeLine{1238 \textcolor{comment}{                                              !! interfaces [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{1239 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{intent(out)}   :: kappa\textcolor{comment}{  !< The diapycnal diffusivity at interfaces}}
\DoxyCodeLine{1240 \textcolor{comment}{                                              !! [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{1241 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{optional}, \&}
\DoxyCodeLine{1242                          \textcolor{keywordtype}{intent(out)}   :: kappa\_src\textcolor{comment}{ !< The source term for kappa [T-1 ~> s-1].}}
\DoxyCodeLine{1243 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)}, \textcolor{keywordtype}{optional}, \&}
\DoxyCodeLine{1244                          \textcolor{keywordtype}{intent(out)}   :: local\_src\textcolor{comment}{ !< The sum of all local sources for kappa,}}
\DoxyCodeLine{1245 \textcolor{comment}{                                              !! [T-1 ~> s-1].}}
\DoxyCodeLine{1246   \textcolor{comment}{! This subroutine calculates new, consistent estimates of TKE and kappa.}}
\DoxyCodeLine{1247 }
\DoxyCodeLine{1248   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1249 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz)} :: \&}
\DoxyCodeLine{1250     aQ, \&       \textcolor{comment}{! aQ is the coupling between adjacent interfaces in the TKE equations [Z T-1 ~> m s-1].}}
\DoxyCodeLine{1251     dQdz        \textcolor{comment}{! Half the partial derivative of TKE with depth [Z T-2 ~> m s-2].}}
\DoxyCodeLine{1252 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)} :: \&}
\DoxyCodeLine{1253     dK, \&         \textcolor{comment}{! The change in kappa [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{1254     dQ, \&         \textcolor{comment}{! The change in TKE [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{1255     cQ, cK, \&     \textcolor{comment}{! cQ and cK are the upward influences in the tridiagonal and}}
\DoxyCodeLine{1256                   \textcolor{comment}{! hexadiagonal solvers for the TKE and kappa equations [nondim].}}
\DoxyCodeLine{1257     i\_ld2, \&      \textcolor{comment}{! 1/Ld\string^2, where Ld is the effective decay length scale for kappa [Z-2 ~> m-2].}}
\DoxyCodeLine{1258     tke\_decay, \&  \textcolor{comment}{! The local TKE decay rate [T-1 ~> s-1].}}
\DoxyCodeLine{1259     k\_src, \&      \textcolor{comment}{! The source term in the kappa equation [T-1 ~> s-1].}}
\DoxyCodeLine{1260     dqmdk, \&      \textcolor{comment}{! With Newton's method the change in dQ(k-1) due to dK(k) [T ~> s].}}
\DoxyCodeLine{1261     dkdq, \&       \textcolor{comment}{! With Newton's method the change in dK(k) due to dQ(k) [T-1 ~> s-1].}}
\DoxyCodeLine{1262     e1            \textcolor{comment}{! The fractional change in a layer TKE due to a change in the}}
\DoxyCodeLine{1263                   \textcolor{comment}{! TKE of the layer above when all the kappas below are 0.}}
\DoxyCodeLine{1264                   \textcolor{comment}{! e1 is nondimensional, and 0 < e1 < 1.}}
\DoxyCodeLine{1265 \textcolor{keywordtype}{  real} :: tke\_src       \textcolor{comment}{! The net source of TKE due to mixing against the shear}}
\DoxyCodeLine{1266                         \textcolor{comment}{! and stratification [Z2 T-3 ~> m2 s-3].  (For convenience,}}
\DoxyCodeLine{1267                         \textcolor{comment}{! a term involving the non-dissipation of q0 is also}}
\DoxyCodeLine{1268                         \textcolor{comment}{! included here.)}}
\DoxyCodeLine{1269 \textcolor{keywordtype}{  real} :: bQ            \textcolor{comment}{! The inverse of the pivot in the tridiagonal equations [T Z-1 ~> s m-1].}}
\DoxyCodeLine{1270 \textcolor{keywordtype}{  real} :: bK            \textcolor{comment}{! The inverse of the pivot in the tridiagonal equations [Z-1 ~> m-1].}}
\DoxyCodeLine{1271 \textcolor{keywordtype}{  real} :: bQd1          \textcolor{comment}{! A term in the denominator of bQ [Z T-1 ~> m s-1].}}
\DoxyCodeLine{1272 \textcolor{keywordtype}{  real} :: bKd1          \textcolor{comment}{! A term in the denominator of bK [Z ~> m].}}
\DoxyCodeLine{1273 \textcolor{keywordtype}{  real} :: cQcomp, cKcomp \textcolor{comment}{! 1 - cQ or 1 - cK in the tridiagonal equations.}}
\DoxyCodeLine{1274 \textcolor{keywordtype}{  real} :: c\_s2          \textcolor{comment}{!   The coefficient for the decay of TKE due to}}
\DoxyCodeLine{1275                         \textcolor{comment}{! shear (i.e. proportional to |S|*tke), nondimensional.}}
\DoxyCodeLine{1276 \textcolor{keywordtype}{  real} :: c\_n2          \textcolor{comment}{!   The coefficient for the decay of TKE due to}}
\DoxyCodeLine{1277                         \textcolor{comment}{! stratification (i.e. proportional to N*tke) [nondim].}}
\DoxyCodeLine{1278 \textcolor{keywordtype}{  real} :: Ri\_crit       \textcolor{comment}{!   The critical shear Richardson number for shear-}}
\DoxyCodeLine{1279                         \textcolor{comment}{! driven mixing. The theoretical value is 0.25.}}
\DoxyCodeLine{1280 \textcolor{keywordtype}{  real} :: q0            \textcolor{comment}{!   The background level of TKE [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{1281 \textcolor{keywordtype}{  real} :: Ilambda2      \textcolor{comment}{! 1.0 / CS\%lambda**2 [nondim]}}
\DoxyCodeLine{1282 \textcolor{keywordtype}{  real} :: TKE\_min       \textcolor{comment}{!   The minimum value of shear-driven TKE that can be}}
\DoxyCodeLine{1283                         \textcolor{comment}{! solved for [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{1284 \textcolor{keywordtype}{  real} :: kappa0        \textcolor{comment}{! The background diapycnal diffusivity [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{1285 \textcolor{keywordtype}{  real} :: kappa\_trunc   \textcolor{comment}{! Diffusivities smaller than this are rounded to 0 [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{1286 }
\DoxyCodeLine{1287 \textcolor{keywordtype}{  real} :: eden1, eden2, I\_eden, ome  \textcolor{comment}{! Variables used in calculating e1.}}
\DoxyCodeLine{1288 \textcolor{keywordtype}{  real} :: diffusive\_src \textcolor{comment}{! The diffusive source in the kappa equation [Z T-1 ~> m s-1].}}
\DoxyCodeLine{1289 \textcolor{keywordtype}{  real} :: chg\_by\_k0     \textcolor{comment}{! The value of k\_src that leads to an increase of}}
\DoxyCodeLine{1290                         \textcolor{comment}{! kappa\_0 if only the diffusive term is a sink [T-1 ~> s-1].}}
\DoxyCodeLine{1291 }
\DoxyCodeLine{1292 \textcolor{keywordtype}{  real} :: kappa\_mean    \textcolor{comment}{! A mean value of kappa [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{1293 \textcolor{keywordtype}{  real} :: Newton\_test   \textcolor{comment}{! The value of relative error that will cause the next}}
\DoxyCodeLine{1294                         \textcolor{comment}{! iteration to use Newton's method.}}
\DoxyCodeLine{1295   \textcolor{comment}{! Temporary variables used in the Newton's method iterations.}}
\DoxyCodeLine{1296 \textcolor{keywordtype}{  real} :: decay\_term\_k  \textcolor{comment}{! The decay term in the diffusivity equation}}
\DoxyCodeLine{1297 \textcolor{keywordtype}{  real} :: decay\_term\_Q  \textcolor{comment}{! The decay term in the TKE equation - proportional to [T-1 ~> s-1]}}
\DoxyCodeLine{1298 \textcolor{keywordtype}{  real} :: I\_Q           \textcolor{comment}{! The inverse of TKE [T2 Z-2 ~> s2 m-2]}}
\DoxyCodeLine{1299 \textcolor{keywordtype}{  real} :: kap\_src}
\DoxyCodeLine{1300 \textcolor{keywordtype}{  real} :: v1            \textcolor{comment}{! A temporary variable proportional to [T-1 ~> s-1]}}
\DoxyCodeLine{1301 \textcolor{keywordtype}{  real} :: v2}
\DoxyCodeLine{1302 \textcolor{keywordtype}{  real} :: tol\_err        \textcolor{comment}{! The tolerance for max\_err that determines when to}}
\DoxyCodeLine{1303                          \textcolor{comment}{! stop iterating.}}
\DoxyCodeLine{1304 \textcolor{keywordtype}{  real} :: Newton\_err     \textcolor{comment}{! The tolerance for max\_err that determines when to}}
\DoxyCodeLine{1305                          \textcolor{comment}{! start using Newton's method.  Empirically, an initial}}
\DoxyCodeLine{1306                          \textcolor{comment}{! value of about 0.2 seems to be most efficient.}}
\DoxyCodeLine{1307 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{parameter} :: roundoff = 1.0e-16 \textcolor{comment}{! A negligible fractional change in TKE.}}
\DoxyCodeLine{1308                          \textcolor{comment}{! This could be larger but performance gains are small.}}
\DoxyCodeLine{1309 }
\DoxyCodeLine{1310   \textcolor{keywordtype}{logical} :: tke\_noflux\_bottom\_BC = .false. \textcolor{comment}{! Specify the boundary conditions}}
\DoxyCodeLine{1311   \textcolor{keywordtype}{logical} :: tke\_noflux\_top\_BC = .false.    \textcolor{comment}{! that are applied to the TKE eqns.}}
\DoxyCodeLine{1312   \textcolor{keywordtype}{logical} :: do\_Newton    \textcolor{comment}{! If .true., use Newton's method for the next iteration.}}
\DoxyCodeLine{1313   \textcolor{keywordtype}{logical} :: abort\_Newton \textcolor{comment}{! If .true., an Newton's method has encountered a 0}}
\DoxyCodeLine{1314                           \textcolor{comment}{! pivot, and should not have been used.}}
\DoxyCodeLine{1315   \textcolor{keywordtype}{logical} :: was\_Newton   \textcolor{comment}{! The value of do\_Newton before checking convergence.}}
\DoxyCodeLine{1316   \textcolor{keywordtype}{logical} :: within\_tolerance \textcolor{comment}{! If .true., all points are within tolerance to}}
\DoxyCodeLine{1317                           \textcolor{comment}{! enable this subroutine to return.}}
\DoxyCodeLine{1318   \textcolor{keywordtype}{integer} :: ks\_src, ke\_src \textcolor{comment}{! The range indices that have nonzero k\_src.}}
\DoxyCodeLine{1319   \textcolor{keywordtype}{integer} :: ks\_kappa, ke\_kappa, ke\_tke   \textcolor{comment}{! The ranges of k-indices that are or}}
\DoxyCodeLine{1320   \textcolor{keywordtype}{integer} :: ks\_kappa\_prev, ke\_kappa\_prev \textcolor{comment}{! were being worked on.}}
\DoxyCodeLine{1321   \textcolor{keywordtype}{integer} :: itt, k, k2}
\DoxyCodeLine{1322 }
\DoxyCodeLine{1323   \textcolor{comment}{! These variables are used only for debugging.}}
\DoxyCodeLine{1324   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{parameter} :: debug\_soln = .false.}
\DoxyCodeLine{1325 \textcolor{keywordtype}{  real} :: K\_err\_lin, Q\_err\_lin, TKE\_src\_norm}
\DoxyCodeLine{1326 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nz+1)} :: \&}
\DoxyCodeLine{1327     I\_Ld2\_debug, \& \textcolor{comment}{! A separate version of I\_Ld2 for debugging [Z-2 ~> m-2].}}
\DoxyCodeLine{1328     kappa\_prev, \& \textcolor{comment}{! The value of kappa at the start of the current iteration [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{1329     TKE\_prev   \textcolor{comment}{! The value of TKE at the start of the current iteration [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{1330 }
\DoxyCodeLine{1331   c\_n2 = cs\%C\_N**2 ; c\_s2 = cs\%C\_S**2}
\DoxyCodeLine{1332   q0 = cs\%TKE\_bg ; kappa0 = cs\%kappa\_0}
\DoxyCodeLine{1333   tke\_min = max(cs\%TKE\_bg, 1.0e-20*us\%m\_to\_Z**2*us\%T\_to\_s**2)}
\DoxyCodeLine{1334   ri\_crit = cs\%Rino\_crit}
\DoxyCodeLine{1335   ilambda2 = 1.0 / cs\%lambda**2}
\DoxyCodeLine{1336   kappa\_trunc = cs\%kappa\_trunc}
\DoxyCodeLine{1337   do\_newton = .false. ; abort\_newton = .false.}
\DoxyCodeLine{1338   tol\_err = cs\%kappa\_tol\_err}
\DoxyCodeLine{1339   newton\_err = 0.2     \textcolor{comment}{! This initial value may be automatically reduced later.}}
\DoxyCodeLine{1340 }
\DoxyCodeLine{1341   ks\_kappa = 2 ; ke\_kappa = nz ; ks\_kappa\_prev = 2 ; ke\_kappa\_prev = nz}
\DoxyCodeLine{1342 }
\DoxyCodeLine{1343   ke\_src = 0 ; ks\_src = nz+1}
\DoxyCodeLine{1344   \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{1345     \textcolor{keywordflow}{if} (n2(k) < ri\_crit * s2(k)) \textcolor{keywordflow}{then} \textcolor{comment}{! Equivalent to Ri < Ri\_crit.}}
\DoxyCodeLine{1346 \textcolor{comment}{!       Ri = N2(K) / S2(K)}}
\DoxyCodeLine{1347 \textcolor{comment}{!       k\_src(K) = (2.0 * CS\%Shearmix\_rate * sqrt(S2(K))) * \&}}
\DoxyCodeLine{1348 \textcolor{comment}{!                  ((Ri\_crit - Ri) / (Ri\_crit + CS\%FRi\_curvature*Ri))}}
\DoxyCodeLine{1349       k\_src(k) = (2.0 * cs\%Shearmix\_rate * sqrt(s2(k))) * \&}
\DoxyCodeLine{1350                  ((ri\_crit*s2(k) - n2(k)) / (ri\_crit*s2(k) + cs\%FRi\_curvature*n2(k)))}
\DoxyCodeLine{1351       ke\_src = k}
\DoxyCodeLine{1352       \textcolor{keywordflow}{if} (ks\_src > k) ks\_src = k}
\DoxyCodeLine{1353     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1354       k\_src(k) = 0.0}
\DoxyCodeLine{1355 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1356 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1357 }
\DoxyCodeLine{1358   \textcolor{comment}{! If there is no source anywhere, return kappa(K) = 0.}}
\DoxyCodeLine{1359   \textcolor{keywordflow}{if} (ks\_src > ke\_src) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1360     \textcolor{keywordflow}{do} k=1,nz+1}
\DoxyCodeLine{1361       kappa(k) = 0.0 ; k\_q(k) = 0.0 ; tke(k) = tke\_min}
\DoxyCodeLine{1362 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1363     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kappa\_src)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz+1 ; kappa\_src(k) = 0.0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1364     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(local\_src)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz+1 ; local\_src(k) = 0.0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1365     \textcolor{keywordflow}{return}}
\DoxyCodeLine{1366 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1367 }
\DoxyCodeLine{1368   \textcolor{keywordflow}{do} k=1,nz+1}
\DoxyCodeLine{1369     kappa(k) = kappa\_in(k)}
\DoxyCodeLine{1370 \textcolor{comment}{!     TKE\_decay(K) = c\_n*sqrt(N2(K)) + c\_s*sqrt(S2(K)) ! The expression in JHL.}}
\DoxyCodeLine{1371     tke\_decay(k) = sqrt(c\_n2*n2(k) + c\_s2*s2(k))}
\DoxyCodeLine{1372     \textcolor{keywordflow}{if} ((kappa(k) > 0.0) .and. (k\_q(k) > 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1373       tke(k) = kappa(k) / k\_q(k) \textcolor{comment}{! Perhaps take the max with TKE\_min}}
\DoxyCodeLine{1374     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1375       tke(k) = tke\_min}
\DoxyCodeLine{1376 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1377 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1378   \textcolor{comment}{! Apply boundary conditions to kappa.}}
\DoxyCodeLine{1379   kappa(1) = 0.0 ; kappa(nz+1) = 0.0}
\DoxyCodeLine{1380 }
\DoxyCodeLine{1381   \textcolor{comment}{! Calculate the term (e1) that allows changes in TKE to be calculated quickly}}
\DoxyCodeLine{1382   \textcolor{comment}{! below the deepest nonzero value of kappa.  If kappa = 0, below interface}}
\DoxyCodeLine{1383   \textcolor{comment}{! k-1, the final changes in TKE are related by dQ(K+1) = e1(K+1)*dQ(K).}}
\DoxyCodeLine{1384   eden2 = kappa0 * idz(nz)}
\DoxyCodeLine{1385   \textcolor{keywordflow}{if} (tke\_noflux\_bottom\_bc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1386     eden1 = dz\_int(nz+1)*tke\_decay(nz+1)}
\DoxyCodeLine{1387     i\_eden = 1.0 / (eden2 + eden1)}
\DoxyCodeLine{1388     e1(nz+1) = eden2 * i\_eden ; ome = eden1 * i\_eden}
\DoxyCodeLine{1389   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1390     e1(nz+1) = 0.0 ; ome = 1.0}
\DoxyCodeLine{1391 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1392   \textcolor{keywordflow}{do} k=nz,2,-1}
\DoxyCodeLine{1393     eden1 = dz\_int(k)*tke\_decay(k) + ome * eden2}
\DoxyCodeLine{1394     eden2 = kappa0 * idz(k-1)}
\DoxyCodeLine{1395     i\_eden = 1.0 / (eden2 + eden1)}
\DoxyCodeLine{1396     e1(k) = eden2 * i\_eden ; ome = eden1 * i\_eden \textcolor{comment}{! = 1-e1}}
\DoxyCodeLine{1397 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1398   e1(1) = 0.0}
\DoxyCodeLine{1399 }
\DoxyCodeLine{1400 }
\DoxyCodeLine{1401   \textcolor{comment}{! Iterate here to convergence to within some tolerance of order tol\_err.}}
\DoxyCodeLine{1402   \textcolor{keywordflow}{do} itt=1,cs\%max\_RiNo\_it}
\DoxyCodeLine{1403 }
\DoxyCodeLine{1404   \textcolor{comment}{! ----------------------------------------------------}}
\DoxyCodeLine{1405   \textcolor{comment}{! Calculate TKE}}
\DoxyCodeLine{1406   \textcolor{comment}{! ----------------------------------------------------}}
\DoxyCodeLine{1407 }
\DoxyCodeLine{1408     \textcolor{keywordflow}{if} (debug\_soln) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz+1 ; kappa\_prev(k) = kappa(k) ; tke\_prev(k) = tke(k) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1409 }
\DoxyCodeLine{1410     \textcolor{keywordflow}{if} (.not.do\_newton) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1411       \textcolor{comment}{!   Use separate steps of the TKE and kappa equations, that are}}
\DoxyCodeLine{1412       \textcolor{comment}{! explicit in the nonlinear source terms, implicit in a linearized}}
\DoxyCodeLine{1413       \textcolor{comment}{! version of the nonlinear sink terms, and implicit in the linear}}
\DoxyCodeLine{1414       \textcolor{comment}{! terms.}}
\DoxyCodeLine{1415 }
\DoxyCodeLine{1416       ke\_tke = max(ke\_kappa,ke\_kappa\_prev)+1}
\DoxyCodeLine{1417       \textcolor{comment}{! aQ is the coupling between adjacent interfaces [Z T-1 ~> m s-1].}}
\DoxyCodeLine{1418       \textcolor{keywordflow}{do} k=1,min(ke\_tke,nz)}
\DoxyCodeLine{1419         aq(k) = (0.5*(kappa(k)+kappa(k+1)) + kappa0) * idz(k)}
\DoxyCodeLine{1420 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1421       dq(1) = -tke(1)}
\DoxyCodeLine{1422       \textcolor{keywordflow}{if} (tke\_noflux\_top\_bc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1423         tke\_src = kappa0*s2(1) + q0 * tke\_decay(1) \textcolor{comment}{! Uses that kappa(1) = 0}}
\DoxyCodeLine{1424         bqd1 = dz\_int(1) * tke\_decay(1)}
\DoxyCodeLine{1425         bq = 1.0 / (bqd1 +  aq(1))}
\DoxyCodeLine{1426         tke(1) = bq * (dz\_int(1)*tke\_src)}
\DoxyCodeLine{1427         cq(2) = aq(1) * bq ; cqcomp = bqd1 * bq \textcolor{comment}{! = 1 - cQ}}
\DoxyCodeLine{1428       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1429         tke(1) = q0 ; cq(2) = 0.0 ; cqcomp = 1.0}
\DoxyCodeLine{1430 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1431       \textcolor{keywordflow}{do} k=2,ke\_tke-1}
\DoxyCodeLine{1432         dq(k) = -tke(k)}
\DoxyCodeLine{1433         tke\_src = (kappa(k) + kappa0)*s2(k) + q0*tke\_decay(k)}
\DoxyCodeLine{1434         bqd1 = dz\_int(k)*(tke\_decay(k) + n2(k)*k\_q(k)) + cqcomp*aq(k-1)}
\DoxyCodeLine{1435         bq = 1.0 / (bqd1 + aq(k))}
\DoxyCodeLine{1436         tke(k) = bq * (dz\_int(k)*tke\_src + aq(k-1)*tke(k-1))}
\DoxyCodeLine{1437         cq(k+1) = aq(k) * bq ; cqcomp = bqd1 * bq \textcolor{comment}{! = 1 - cQ}}
\DoxyCodeLine{1438 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1439       \textcolor{keywordflow}{if} ((ke\_tke == nz+1) .and. .not.(tke\_noflux\_bottom\_bc)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1440         tke(nz+1) = tke\_min}
\DoxyCodeLine{1441         dq(nz+1) = 0.0}
\DoxyCodeLine{1442       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1443         k = ke\_tke}
\DoxyCodeLine{1444         tke\_src = kappa0*s2(k) + q0*tke\_decay(k) \textcolor{comment}{! Uses that kappa(ke\_tke) = 0}}
\DoxyCodeLine{1445         \textcolor{keywordflow}{if} (k == nz+1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1446           dq(k) = -tke(k)}
\DoxyCodeLine{1447           bq = 1.0 / (dz\_int(k)*tke\_decay(k) + cqcomp*aq(k-1))}
\DoxyCodeLine{1448           tke(k) = max(tke\_min, bq * (dz\_int(k)*tke\_src + aq(k-1)*tke(k-1)))}
\DoxyCodeLine{1449           dq(k) = tke(k) + dq(k)}
\DoxyCodeLine{1450         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1451           bq = 1.0 / ((dz\_int(k)*tke\_decay(k) + cqcomp*aq(k-1)) + aq(k))}
\DoxyCodeLine{1452           cq(k+1) = aq(k) * bq}
\DoxyCodeLine{1453           \textcolor{comment}{! Account for all changes deeper in the water column.}}
\DoxyCodeLine{1454           dq(k) = -tke(k)}
\DoxyCodeLine{1455           tke(k) = max((bq * (dz\_int(k)*tke\_src + aq(k-1)*tke(k-1)) + \&}
\DoxyCodeLine{1456                         cq(k+1)*(tke(k+1) - e1(k+1)*tke(k))) / (1.0 - cq(k+1)*e1(k+1)), tke\_min)}
\DoxyCodeLine{1457           dq(k) = tke(k) + dq(k)}
\DoxyCodeLine{1458 }
\DoxyCodeLine{1459           \textcolor{comment}{! Adjust TKE deeper in the water column in case ke\_tke increases.}}
\DoxyCodeLine{1460           \textcolor{comment}{! This might not be strictly necessary?}}
\DoxyCodeLine{1461           \textcolor{keywordflow}{do} k=ke\_tke+1,nz+1}
\DoxyCodeLine{1462             dq(k) = e1(k)*dq(k-1)}
\DoxyCodeLine{1463             tke(k) = max(tke(k) + dq(k), tke\_min)}
\DoxyCodeLine{1464             \textcolor{keywordflow}{if} (abs(dq(k)) < roundoff*tke(k)) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1465 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{1466           \textcolor{keywordflow}{do} k2=k+1,nz}
\DoxyCodeLine{1467             \textcolor{keywordflow}{if} (dq(k2) == 0.0) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1468             dq(k2) = 0.0}
\DoxyCodeLine{1469 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{1470 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1471 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1472       \textcolor{keywordflow}{do} k=ke\_tke-1,1,-1}
\DoxyCodeLine{1473         tke(k) = max(tke(k) + cq(k+1)*tke(k+1), tke\_min)}
\DoxyCodeLine{1474         dq(k) = tke(k) + dq(k)}
\DoxyCodeLine{1475 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1476 }
\DoxyCodeLine{1477   \textcolor{comment}{! ----------------------------------------------------}}
\DoxyCodeLine{1478   \textcolor{comment}{! Calculate kappa, here defined at interfaces.}}
\DoxyCodeLine{1479   \textcolor{comment}{! ----------------------------------------------------}}
\DoxyCodeLine{1480 }
\DoxyCodeLine{1481       ke\_kappa\_prev = ke\_kappa ; ks\_kappa\_prev = ks\_kappa}
\DoxyCodeLine{1482 }
\DoxyCodeLine{1483       dk(1) = 0.0 \textcolor{comment}{! kappa takes boundary values of 0.}}
\DoxyCodeLine{1484       ck(2) = 0.0 ; ckcomp = 1.0}
\DoxyCodeLine{1485       \textcolor{keywordflow}{if} (itt == 1) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{dO} k=2,nz}
\DoxyCodeLine{1486         i\_ld2(k) = (n2(k)*ilambda2 + f2) / tke(k) + i\_l2\_bdry(k)}
\DoxyCodeLine{1487 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1488       \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{1489         dk(k) = -kappa(k)}
\DoxyCodeLine{1490         \textcolor{keywordflow}{if} (itt>1) \&}
\DoxyCodeLine{1491           i\_ld2(k) = (n2(k)*ilambda2 + f2) / tke(k) + i\_l2\_bdry(k)}
\DoxyCodeLine{1492         bkd1 = dz\_int(k)*i\_ld2(k) + ckcomp*idz(k-1)}
\DoxyCodeLine{1493         bk = 1.0 / (bkd1 + idz(k))}
\DoxyCodeLine{1494 }
\DoxyCodeLine{1495         kappa(k) = bk * (idz(k-1)*kappa(k-1) + dz\_int(k) * k\_src(k))}
\DoxyCodeLine{1496         ck(k+1) = idz(k) * bk ; ckcomp = bkd1 * bk \textcolor{comment}{! = 1 - cK(K+1)}}
\DoxyCodeLine{1497 }
\DoxyCodeLine{1498         \textcolor{comment}{! Neglect values that are smaller than kappa\_trunc.}}
\DoxyCodeLine{1499         \textcolor{keywordflow}{if} (kappa(k) < ckcomp*kappa\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1500           kappa(k) = 0.0}
\DoxyCodeLine{1501           \textcolor{keywordflow}{if} (k > ke\_src) \textcolor{keywordflow}{then} ; ke\_kappa = k-1 ; k\_q(k) = 0.0 ; \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1502         \textcolor{keywordflow}{elseif} (kappa(k) < 2.0*ckcomp*kappa\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1503           kappa(k) = 2.0 * (kappa(k) - ckcomp*kappa\_trunc)}
\DoxyCodeLine{1504 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1505 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1506       k\_q(ke\_kappa) = kappa(ke\_kappa) / tke(ke\_kappa)}
\DoxyCodeLine{1507       dk(ke\_kappa) = dk(ke\_kappa) + kappa(ke\_kappa)}
\DoxyCodeLine{1508       \textcolor{keywordflow}{do} k=ke\_kappa+2,ke\_kappa\_prev}
\DoxyCodeLine{1509         dk(k) = -kappa(k) ; kappa(k) = 0.0 ; k\_q(k) = 0.0}
\DoxyCodeLine{1510 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1511       \textcolor{keywordflow}{do} k=ke\_kappa-1,2,-1}
\DoxyCodeLine{1512         kappa(k) = kappa(k) + ck(k+1)*kappa(k+1)}
\DoxyCodeLine{1513         \textcolor{comment}{! Neglect values that are smaller than kappa\_trunc.}}
\DoxyCodeLine{1514         \textcolor{keywordflow}{if} (kappa(k) <= kappa\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1515           kappa(k) = 0.0}
\DoxyCodeLine{1516           \textcolor{keywordflow}{if} (k < ks\_src) \textcolor{keywordflow}{then} ; ks\_kappa = k+1 ; k\_q(k) = 0.0 ; \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1517         \textcolor{keywordflow}{elseif} (kappa(k) < 2.0*kappa\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1518           kappa(k) = 2.0 * (kappa(k) - kappa\_trunc)}
\DoxyCodeLine{1519 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1520 }
\DoxyCodeLine{1521         dk(k) = dk(k) + kappa(k)}
\DoxyCodeLine{1522         k\_q(k) = kappa(k) / tke(k)}
\DoxyCodeLine{1523 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1524       \textcolor{keywordflow}{do} k=ks\_kappa\_prev,ks\_kappa-2 ; kappa(k) = 0.0 ; k\_q(k) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1525 }
\DoxyCodeLine{1526     \textcolor{keywordflow}{else} \textcolor{comment}{! do\_Newton is .true.}}
\DoxyCodeLine{1527 \textcolor{comment}{!   Once the solutions are close enough, use a Newton's method solver of the}}
\DoxyCodeLine{1528 \textcolor{comment}{!  whole system to accelerate convergence.}}
\DoxyCodeLine{1529       ks\_kappa\_prev = ks\_kappa ; ke\_kappa\_prev = ke\_kappa ; ke\_kappa = nz}
\DoxyCodeLine{1530       ks\_kappa = 2}
\DoxyCodeLine{1531       dk(1) = 0.0 ; ck(2) = 0.0 ; ckcomp = 1.0 ; dkdq(1) = 0.0}
\DoxyCodeLine{1532       aq(1) = (0.5*(kappa(1)+kappa(2))+kappa0) * idz(1)}
\DoxyCodeLine{1533       dqdz(1) = 0.5*(tke(1) - tke(2))*idz(1)}
\DoxyCodeLine{1534       \textcolor{keywordflow}{if} (tke\_noflux\_top\_bc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1535         tke\_src = dz\_int(1) * (kappa0*s2(1) - (tke(1) - q0)*tke\_decay(1)) - \&}
\DoxyCodeLine{1536                   aq(1) * (tke(1) - tke(2))}
\DoxyCodeLine{1537 }
\DoxyCodeLine{1538         bq = 1.0 / (aq(1) + dz\_int(1)*tke\_decay(1))}
\DoxyCodeLine{1539         cq(2) = aq(1) * bq}
\DoxyCodeLine{1540         cqcomp = (dz\_int(1)*tke\_decay(1)) * bq \textcolor{comment}{! = 1 - cQ(2)}}
\DoxyCodeLine{1541         dqmdk(2) = -dqdz(1) * bq}
\DoxyCodeLine{1542         dq(1) = bq * tke\_src}
\DoxyCodeLine{1543       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1544         dq(1) = 0.0 ; cq(2) = 0.0 ; cqcomp = 1.0 ; dqmdk(2) = 0.0}
\DoxyCodeLine{1545 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1546       \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{1547         i\_q = 1.0 / tke(k)}
\DoxyCodeLine{1548         i\_ld2(k) = (n2(k)*ilambda2 + f2) * i\_q + i\_l2\_bdry(k)}
\DoxyCodeLine{1549 }
\DoxyCodeLine{1550         kap\_src = dz\_int(k) * (k\_src(k) - i\_ld2(k)*kappa(k)) + \&}
\DoxyCodeLine{1551                             idz(k-1)*(kappa(k-1)-kappa(k)) - idz(k)*(kappa(k)-kappa(k+1))}
\DoxyCodeLine{1552 }
\DoxyCodeLine{1553         \textcolor{comment}{! Ensure that the pivot is always positive, and that 0 <= cK <= 1.}}
\DoxyCodeLine{1554         \textcolor{comment}{! Otherwise do not use Newton's method.}}
\DoxyCodeLine{1555         decay\_term\_k = -idz(k-1)*dqmdk(k)*dkdq(k-1) + dz\_int(k)*i\_ld2(k)}
\DoxyCodeLine{1556         \textcolor{keywordflow}{if} (decay\_term\_k < 0.0) \textcolor{keywordflow}{then} ; abort\_newton = .true. ; \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1557         bk = 1.0 / (idz(k) + idz(k-1)*ckcomp + decay\_term\_k)}
\DoxyCodeLine{1558 }
\DoxyCodeLine{1559         ck(k+1) = bk * idz(k)}
\DoxyCodeLine{1560         ckcomp = bk * (idz(k-1)*ckcomp + decay\_term\_k) \textcolor{comment}{! = 1-cK(K+1)}}
\DoxyCodeLine{1561         \textcolor{keywordflow}{if} (cs\%dKdQ\_iteration\_bug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1562           dkdq(k) = bk * (idz(k-1)*dkdq(k-1)*cq(k) + \&}
\DoxyCodeLine{1563                       us\%m\_to\_Z*(n2(k)*ilambda2 + f2) * i\_q**2 * kappa(k) )}
\DoxyCodeLine{1564         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1565           dkdq(k) = bk * (idz(k-1)*dkdq(k-1)*cq(k) + \&}
\DoxyCodeLine{1566                       dz\_int(k)*(n2(k)*ilambda2 + f2) * i\_q**2 * kappa(k) )}
\DoxyCodeLine{1567 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1568         dk(k) = bk * (kap\_src + idz(k-1)*dk(k-1) + idz(k-1)*dkdq(k-1)*dq(k-1))}
\DoxyCodeLine{1569 }
\DoxyCodeLine{1570         \textcolor{comment}{! Truncate away negligibly small values of kappa.}}
\DoxyCodeLine{1571         \textcolor{keywordflow}{if} (dk(k) <= ckcomp*(kappa\_trunc - kappa(k))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1572           dk(k) = -ckcomp*kappa(k)}
\DoxyCodeLine{1573 \textcolor{comment}{!         if (K > ke\_src) then ; ke\_kappa = k-1 ; K\_Q(K) = 0.0 ; exit ; endif}}
\DoxyCodeLine{1574         \textcolor{keywordflow}{elseif} (dk(k) < ckcomp*(2.0*kappa\_trunc - kappa(k))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1575           dk(k) = 2.0 * dk(k) - ckcomp*(2.0*kappa\_trunc - kappa(k))}
\DoxyCodeLine{1576 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1577 }
\DoxyCodeLine{1578         \textcolor{comment}{! Solve for dQ(K)...}}
\DoxyCodeLine{1579         aq(k) = (0.5*(kappa(k)+kappa(k+1))+kappa0) * idz(k)}
\DoxyCodeLine{1580         dqdz(k) = 0.5*(tke(k) - tke(k+1))*idz(k)}
\DoxyCodeLine{1581         tke\_src = dz\_int(k) * (((kappa(k) + kappa0)*s2(k) - kappa(k)*n2(k)) - \&}
\DoxyCodeLine{1582                                   (tke(k) - q0)*tke\_decay(k)) - \&}
\DoxyCodeLine{1583                   (aq(k) * (tke(k) - tke(k+1)) - aq(k-1) * (tke(k-1) - tke(k)))}
\DoxyCodeLine{1584         v1 = aq(k-1) + dqdz(k-1)*dkdq(k-1)}
\DoxyCodeLine{1585         v2 = (v1*dqmdk(k) + dqdz(k-1)*ck(k)) + \&}
\DoxyCodeLine{1586              ((dqdz(k-1) - dqdz(k)) + dz\_int(k)*(s2(k) - n2(k)))}
\DoxyCodeLine{1587 }
\DoxyCodeLine{1588         \textcolor{comment}{! Ensure that the pivot is always positive, and that 0 <= cQ <= 1.}}
\DoxyCodeLine{1589         \textcolor{comment}{! Otherwise do not use Newton's method.}}
\DoxyCodeLine{1590         decay\_term\_q = dz\_int(k)*tke\_decay(k) - dqdz(k-1)*dkdq(k-1)*cq(k) - v2*dkdq(k)}
\DoxyCodeLine{1591         \textcolor{keywordflow}{if} (decay\_term\_q < 0.0) \textcolor{keywordflow}{then} ; abort\_newton = .true. ; \textcolor{keywordflow}{exit} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1592         bq = 1.0 / (aq(k) + (cqcomp*aq(k-1) + decay\_term\_q))}
\DoxyCodeLine{1593 }
\DoxyCodeLine{1594         cq(k+1) = aq(k) * bq}
\DoxyCodeLine{1595         cqcomp = (cqcomp*aq(k-1) + decay\_term\_q) * bq}
\DoxyCodeLine{1596         dqmdk(k+1) = (v2 * ck(k+1) - dqdz(k)) * bq}
\DoxyCodeLine{1597 }
\DoxyCodeLine{1598         \textcolor{comment}{! Ensure that TKE+dQ will not drop below 0.5*TKE.}}
\DoxyCodeLine{1599         dq(k) = max(bq * ((v1 * dq(k-1) + dqdz(k-1)*dk(k-1)) + \&}
\DoxyCodeLine{1600                           (v2 * dk(k) + tke\_src)), cqcomp*(-0.5*tke(k)))}
\DoxyCodeLine{1601 }
\DoxyCodeLine{1602         \textcolor{comment}{! Check whether the next layer will be affected by any nonzero kappas.}}
\DoxyCodeLine{1603         \textcolor{keywordflow}{if} ((itt > 1) .and. (k > ke\_src) .and. (dk(k) == 0.0) .and. \&}
\DoxyCodeLine{1604             ((kappa(k) + kappa(k+1)) == 0.0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1605         \textcolor{comment}{! Could also do  .and. (bQ*abs(tke\_src) < roundoff*TKE(K)) then}}
\DoxyCodeLine{1606           ke\_kappa = k-1 ; \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1607 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1608 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1609       \textcolor{keywordflow}{if} ((ke\_kappa == nz) .and. (.not. abort\_newton)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1610         dk(nz+1) = 0.0 ; dkdq(nz+1) = 0.0}
\DoxyCodeLine{1611         \textcolor{keywordflow}{if} (tke\_noflux\_bottom\_bc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1612           k = nz+1}
\DoxyCodeLine{1613           tke\_src = dz\_int(k) * (kappa0*s2(k) - (tke(k) - q0)*tke\_decay(k)) + \&}
\DoxyCodeLine{1614                     aq(k-1) * (tke(k-1) - tke(k))}
\DoxyCodeLine{1615 }
\DoxyCodeLine{1616           v1 = aq(k-1) + dqdz(k-1)*dkdq(k-1)}
\DoxyCodeLine{1617           decay\_term\_q = max(0.0, dz\_int(k)*tke\_decay(k) - dqdz(k-1)*dkdq(k-1)*cq(k))}
\DoxyCodeLine{1618           \textcolor{keywordflow}{if} (decay\_term\_q < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1619             abort\_newton = .true.}
\DoxyCodeLine{1620           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1621             bq = 1.0 / (aq(k) + (cqcomp*aq(k-1) + decay\_term\_q))}
\DoxyCodeLine{1622           \textcolor{comment}{! Ensure that TKE+dQ will not drop below 0.5*TKE.}}
\DoxyCodeLine{1623             dq(k) = max(bq * ((v1 * dq(k-1) + dqdz(k-1)*dk(k-1)) + tke\_src), -0.5*tke(k))}
\DoxyCodeLine{1624             tke(k) = max(tke(k) + dq(k), tke\_min)}
\DoxyCodeLine{1625 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1626         \textcolor{keywordflow}{else}}
\DoxyCodeLine{1627           dq(nz+1) = 0.0}
\DoxyCodeLine{1628 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1629       \textcolor{keywordflow}{elseif} (.not. abort\_newton) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1630         \textcolor{comment}{! Alter the first-guess determination of dQ(K).}}
\DoxyCodeLine{1631         dq(ke\_kappa+1) = dq(ke\_kappa+1) / (1.0 - cq(ke\_kappa+2)*e1(ke\_kappa+2))}
\DoxyCodeLine{1632         tke(ke\_kappa+1) = max(tke(ke\_kappa+1) + dq(ke\_kappa+1), tke\_min)}
\DoxyCodeLine{1633         \textcolor{keywordflow}{do} k=ke\_kappa+2,nz+1}
\DoxyCodeLine{1634           \textcolor{keywordflow}{if} (debug\_soln .and. (k < nz+1)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1635           \textcolor{comment}{! Ignore this source?}}
\DoxyCodeLine{1636             aq(k) = (0.5*(kappa(k)+kappa(k+1))+kappa0) * idz(k)}
\DoxyCodeLine{1637         \textcolor{comment}{!    tke\_src\_norm = (dz\_Int(K) * (kappa0*S2(K) - (TKE(K)-q0)*TKE\_decay(K)) - \&}}
\DoxyCodeLine{1638         \textcolor{comment}{!                   (aQ(k) * (TKE(K) - TKE(K+1)) - aQ(k-1) * (TKE(K-1) - TKE(K))) ) / \&}}
\DoxyCodeLine{1639         \textcolor{comment}{!                   (aQ(k) + (aQ(k-1) + dz\_Int(K)*TKE\_decay(K)))}}
\DoxyCodeLine{1640 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1641           dk(k) = 0.0}
\DoxyCodeLine{1642         \textcolor{comment}{! Ensure that TKE+dQ will not drop below 0.5*TKE.}}
\DoxyCodeLine{1643           dq(k) = max(e1(k)*dq(k-1),-0.5*tke(k))}
\DoxyCodeLine{1644           tke(k) = max(tke(k) + dq(k), tke\_min)}
\DoxyCodeLine{1645           \textcolor{keywordflow}{if} (abs(dq(k)) < roundoff*tke(k)) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1646 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{1647         \textcolor{keywordflow}{if} (debug\_soln) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k2=k+1,nz+1 ; dq(k2) = 0.0 ; dk(k2) = 0.0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1648 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1649       \textcolor{keywordflow}{if} (.not. abort\_newton) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1650         \textcolor{keywordflow}{do} k=ke\_kappa,2,-1}
\DoxyCodeLine{1651           \textcolor{comment}{! Ensure that TKE+dQ will not drop below 0.5*TKE.}}
\DoxyCodeLine{1652           dq(k) = max(dq(k) + (cq(k+1)*dq(k+1) + dqmdk(k+1) * dk(k+1)), -0.5*tke(k))}
\DoxyCodeLine{1653           tke(k) = max(tke(k) + dq(k), tke\_min)}
\DoxyCodeLine{1654           dk(k) = dk(k) + (ck(k+1)*dk(k+1) + dkdq(k) * dq(k))}
\DoxyCodeLine{1655           \textcolor{comment}{! Truncate away negligibly small values of kappa.}}
\DoxyCodeLine{1656           \textcolor{keywordflow}{if} (dk(k) <= kappa\_trunc - kappa(k)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1657             dk(k) = -kappa(k)}
\DoxyCodeLine{1658             kappa(k) = 0.0}
\DoxyCodeLine{1659             \textcolor{keywordflow}{if} ((k < ks\_src) .and. (k+1 > ks\_kappa)) ks\_kappa = k+1}
\DoxyCodeLine{1660           \textcolor{keywordflow}{elseif} (dk(k) < 2.0*kappa\_trunc - kappa(k)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1661             dk(k) =  2.0*dk(k) - (2.0*kappa\_trunc - kappa(k))}
\DoxyCodeLine{1662             kappa(k) = max(kappa(k) + dk(k), 0.0) \textcolor{comment}{! The max is for paranoia.}}
\DoxyCodeLine{1663             \textcolor{keywordflow}{if} (k<=ks\_kappa) ks\_kappa = 2}
\DoxyCodeLine{1664           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1665             kappa(k) = kappa(k) + dk(k)}
\DoxyCodeLine{1666             \textcolor{keywordflow}{if} (k<=ks\_kappa) ks\_kappa = 2}
\DoxyCodeLine{1667 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1668 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{1669         dq(1) = max(dq(1) + cq(2)*dq(2) + dqmdk(2) * dk(2), tke\_min - tke(1))}
\DoxyCodeLine{1670         tke(1) = max(tke(1) + dq(1), tke\_min)}
\DoxyCodeLine{1671         dk(1) = 0.0}
\DoxyCodeLine{1672 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1673 }
\DoxyCodeLine{1674       \textcolor{comment}{! Check these solutions for consistency.}}
\DoxyCodeLine{1675       \textcolor{comment}{!  The unit conversions here have not been carefully tested.}}
\DoxyCodeLine{1676       \textcolor{keywordflow}{if} (debug\_soln) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{1677         \textcolor{comment}{! In these equations, K\_err\_lin and Q\_err\_lin should be at round-off levels}}
\DoxyCodeLine{1678         \textcolor{comment}{! compared with the dominant terms, perhaps, dz\_Int*I\_Ld2*kappa and}}
\DoxyCodeLine{1679         \textcolor{comment}{! dz\_Int*TKE\_decay*TKE.  The exception is where, either 1) the decay term has been}}
\DoxyCodeLine{1680         \textcolor{comment}{! been increased to ensure a positive pivot, or 2) negative TKEs have been}}
\DoxyCodeLine{1681         \textcolor{comment}{! truncated, or 3) small or negative kappas have been rounded toward 0.}}
\DoxyCodeLine{1682         i\_q = 1.0 / tke(k)}
\DoxyCodeLine{1683         i\_ld2\_debug(k) = (n2(k)*ilambda2 + f2) * i\_q + i\_l2\_bdry(k)}
\DoxyCodeLine{1684 }
\DoxyCodeLine{1685         kap\_src = dz\_int(k) * (k\_src(k) - i\_ld2(k)*kappa\_prev(k)) + \&}
\DoxyCodeLine{1686                             (idz(k-1)*(kappa\_prev(k-1)-kappa\_prev(k)) - \&}
\DoxyCodeLine{1687                              idz(k)*(kappa\_prev(k)-kappa\_prev(k+1)))}
\DoxyCodeLine{1688         k\_err\_lin = -idz(k-1)*(dk(k-1)-dk(k)) + idz(k)*(dk(k)-dk(k+1)) + \&}
\DoxyCodeLine{1689                      dz\_int(k)*i\_ld2\_debug(k)*dk(k) - kap\_src - \&}
\DoxyCodeLine{1690                      dz\_int(k)*(n2(k)*ilambda2 + f2)*i\_q**2*kappa\_prev(k) * dq(k)}
\DoxyCodeLine{1691 }
\DoxyCodeLine{1692         tke\_src = dz\_int(k) * ((kappa\_prev(k) + kappa0)*s2(k) - \&}
\DoxyCodeLine{1693                      kappa\_prev(k)*n2(k) - (tke\_prev(k) - q0)*tke\_decay(k)) - \&}
\DoxyCodeLine{1694                   (aq(k) * (tke\_prev(k) - tke\_prev(k+1)) -  aq(k-1) * (tke\_prev(k-1) - tke\_prev(k)))}
\DoxyCodeLine{1695         q\_err\_lin = tke\_src + (aq(k-1) * (dq(k-1)-dq(k)) - aq(k) * (dq(k)-dq(k+1))) - \&}
\DoxyCodeLine{1696                     0.5*(tke\_prev(k)-tke\_prev(k+1))*idz(k)  * (dk(k) + dk(k+1)) - \&}
\DoxyCodeLine{1697                     0.5*(tke\_prev(k)-tke\_prev(k-1))*idz(k-1)* (dk(k-1) + dk(k)) + \&}
\DoxyCodeLine{1698                     dz\_int(k) * (dk(k) * (s2(k) - n2(k)) - dq(k)*tke\_decay(k))}
\DoxyCodeLine{1699 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1700 }
\DoxyCodeLine{1701 \textcolor{keywordflow}{    endif}  \textcolor{comment}{! End of the Newton's method solver.}}
\DoxyCodeLine{1702 }
\DoxyCodeLine{1703     \textcolor{comment}{! Test kappa for convergence...}}
\DoxyCodeLine{1704     \textcolor{keywordflow}{if} ((tol\_err < newton\_err) .and. (.not.abort\_newton)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1705       \textcolor{comment}{!  A lower tolerance is used to switch to Newton's method than to switch back.}}
\DoxyCodeLine{1706       newton\_test = newton\_err ; \textcolor{keywordflow}{if} (do\_newton) newton\_test = 2.0*newton\_err}
\DoxyCodeLine{1707       was\_newton = do\_newton}
\DoxyCodeLine{1708       within\_tolerance = .true. ; do\_newton = .true.}
\DoxyCodeLine{1709       \textcolor{keywordflow}{do} k=min(ks\_kappa,ks\_kappa\_prev),max(ke\_kappa,ke\_kappa\_prev)}
\DoxyCodeLine{1710         kappa\_mean = kappa0 + (kappa(k) - 0.5*dk(k))}
\DoxyCodeLine{1711         \textcolor{keywordflow}{if} (abs(dk(k)) > newton\_test * kappa\_mean) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1712           \textcolor{keywordflow}{if} (do\_newton) abort\_newton = .true.}
\DoxyCodeLine{1713           within\_tolerance = .false. ; do\_newton = .false. ; \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1714         \textcolor{keywordflow}{elseif} (abs(dk(k)) > tol\_err * kappa\_mean) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1715           within\_tolerance = .false. ; \textcolor{keywordflow}{if} (.not.do\_newton) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1716 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1717         \textcolor{keywordflow}{if} (abs(dq(k)) > newton\_test*(tke(k) - 0.5*dq(k))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1718           \textcolor{keywordflow}{if} (do\_newton) abort\_newton = .true.}
\DoxyCodeLine{1719           do\_newton = .false. ; \textcolor{keywordflow}{if} (.not.within\_tolerance) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1720 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1721 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1722 }
\DoxyCodeLine{1723     \textcolor{keywordflow}{else}  \textcolor{comment}{! Newton's method will not be used again, so no need to check.}}
\DoxyCodeLine{1724       within\_tolerance = .true.}
\DoxyCodeLine{1725       \textcolor{keywordflow}{do} k=min(ks\_kappa,ks\_kappa\_prev),max(ke\_kappa,ke\_kappa\_prev)}
\DoxyCodeLine{1726         \textcolor{keywordflow}{if} (abs(dk(k)) > tol\_err * (kappa0 + (kappa(k) - 0.5*dk(k)))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1727           within\_tolerance = .false. ;  \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1728 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1729 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1730 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1731 }
\DoxyCodeLine{1732     \textcolor{keywordflow}{if} (abort\_newton) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1733       do\_newton = .false. ; abort\_newton = .false.}
\DoxyCodeLine{1734       \textcolor{comment}{! We went to Newton too quickly last time, so restrict the tolerance.}}
\DoxyCodeLine{1735       newton\_err = 0.5*newton\_err}
\DoxyCodeLine{1736       ke\_kappa\_prev = nz}
\DoxyCodeLine{1737       \textcolor{keywordflow}{do} k=2,nz ; k\_q(k) = kappa(k) / max(tke(k), tke\_min) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1738 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1739 }
\DoxyCodeLine{1740     \textcolor{keywordflow}{if} (within\_tolerance) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1741 }
\DoxyCodeLine{1742 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{1743 }
\DoxyCodeLine{1744   \textcolor{keywordflow}{if} (do\_newton) \textcolor{keywordflow}{then}  \textcolor{comment}{! K\_Q needs to be calculated.}}
\DoxyCodeLine{1745     \textcolor{keywordflow}{do} k=1,ks\_kappa-1 ;  k\_q(k) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1746     \textcolor{keywordflow}{do} k=ks\_kappa,ke\_kappa ; k\_q(k) = kappa(k) / tke(k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1747     \textcolor{keywordflow}{do} k=ke\_kappa+1,nz+1 ; k\_q(k) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1748 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1749 }
\DoxyCodeLine{1750   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(local\_src)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1751     local\_src(1) = 0.0 ; local\_src(nz+1) = 0.0}
\DoxyCodeLine{1752     \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{1753       diffusive\_src = idz(k-1)*(kappa(k-1)-kappa(k)) + idz(k)*(kappa(k+1)-kappa(k))}
\DoxyCodeLine{1754       chg\_by\_k0 = kappa0 * ((idz(k-1)+idz(k)) / dz\_int(k) + i\_ld2(k))}
\DoxyCodeLine{1755       \textcolor{keywordflow}{if} (diffusive\_src <= 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1756         local\_src(k) = k\_src(k) + chg\_by\_k0}
\DoxyCodeLine{1757       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1758         local\_src(k) = (k\_src(k) + chg\_by\_k0) + diffusive\_src / dz\_int(k)}
\DoxyCodeLine{1759 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1760 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1761 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1762   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(kappa\_src)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1763     kappa\_src(1) = 0.0 ; kappa\_src(nz+1) = 0.0}
\DoxyCodeLine{1764     \textcolor{keywordflow}{do} k=2,nz}
\DoxyCodeLine{1765       kappa\_src(k) = k\_src(k)}
\DoxyCodeLine{1766 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1767 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1768 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__kappa__shear_ad4d87b0928aea195213e682b493eb555}\label{namespacemom__kappa__shear_ad4d87b0928aea195213e682b493eb555}} 
\index{mom\_kappa\_shear@{mom\_kappa\_shear}!kappa\_shear\_at\_vertex@{kappa\_shear\_at\_vertex}}
\index{kappa\_shear\_at\_vertex@{kappa\_shear\_at\_vertex}!mom\_kappa\_shear@{mom\_kappa\_shear}}
\subsubsection{\texorpdfstring{kappa\_shear\_at\_vertex()}{kappa\_shear\_at\_vertex()}}
{\footnotesize\ttfamily logical function, public mom\+\_\+kappa\+\_\+shear\+::kappa\+\_\+shear\+\_\+at\+\_\+vertex (\begin{DoxyParamCaption}\item[{type(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file }\end{DoxyParamCaption})}



This function indicates to other modules whether the Jackson et al shear mixing parameterization will be used at the vertices without needing to duplicate the log entry. It returns false if the Jackson et al scheme is not used or if it is used via calculations at the tracer points. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em param\+\_\+file} & A structure to parse for run-\/time parameters \\
\hline
\end{DoxyParams}


Definition at line 1975 of file M\+O\+M\+\_\+kappa\+\_\+shear.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1975   \textcolor{keywordtype}{type}(param\_file\_type), \textcolor{keywordtype}{intent(in)} :: param\_file\textcolor{comment}{ !< A structure to parse for run-time parameters}}
\DoxyCodeLine{1976 }
\DoxyCodeLine{1977   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1978   \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_kappa\_shear"}  \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{1979   \textcolor{keywordtype}{logical} :: do\_kappa\_shear}
\DoxyCodeLine{1980   \textcolor{comment}{! This function returns true only if the parameters "USE\_JACKSON\_PARAM" and "VERTEX\_SHEAR" are both true.}}
\DoxyCodeLine{1981 }
\DoxyCodeLine{1982   kappa\_shear\_at\_vertex = .false.}
\DoxyCodeLine{1983 }
\DoxyCodeLine{1984   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_JACKSON\_PARAM"}, do\_kappa\_shear, \&}
\DoxyCodeLine{1985                  default=.false., do\_not\_log=.true.)}
\DoxyCodeLine{1986   \textcolor{keywordflow}{if} (do\_kappa\_shear) \&}
\DoxyCodeLine{1987     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"VERTEX\_SHEAR"}, kappa\_shear\_at\_vertex, \&}
\DoxyCodeLine{1988                  \textcolor{stringliteral}{"If true, do the calculations of the shear-driven mixing "}//\&}
\DoxyCodeLine{1989                  \textcolor{stringliteral}{"at the cell vertices (i.e., the vorticity points)."}, \&}
\DoxyCodeLine{1990                  default=.false., do\_not\_log=.true.)}
\DoxyCodeLine{1991 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__kappa__shear_a26cc5bb15545f04cfaf07e53410e09ec}\label{namespacemom__kappa__shear_a26cc5bb15545f04cfaf07e53410e09ec}} 
\index{mom\_kappa\_shear@{mom\_kappa\_shear}!kappa\_shear\_column@{kappa\_shear\_column}}
\index{kappa\_shear\_column@{kappa\_shear\_column}!mom\_kappa\_shear@{mom\_kappa\_shear}}
\subsubsection{\texorpdfstring{kappa\_shear\_column()}{kappa\_shear\_column()}}
{\footnotesize\ttfamily subroutine mom\+\_\+kappa\+\_\+shear\+::kappa\+\_\+shear\+\_\+column (\begin{DoxyParamCaption}\item[{real, dimension( gv \%ke+1), intent(inout)}]{kappa,  }\item[{real, dimension( gv \%ke+1), intent(out)}]{tke,  }\item[{real, intent(in)}]{dt,  }\item[{integer, intent(in)}]{nzc,  }\item[{real, intent(in)}]{f2,  }\item[{real, intent(in)}]{surface\+\_\+pres,  }\item[{real, dimension( gv \%ke), intent(in)}]{dz,  }\item[{real, dimension( gv \%ke), intent(in)}]{u0xdz,  }\item[{real, dimension( gv \%ke), intent(in)}]{v0xdz,  }\item[{real, dimension( gv \%ke), intent(in)}]{T0xdz,  }\item[{real, dimension( gv \%ke), intent(in)}]{S0xdz,  }\item[{real, dimension( gv \%ke+1), intent(out)}]{kappa\+\_\+avg,  }\item[{real, dimension( gv \%ke+1), intent(out)}]{tke\+\_\+avg,  }\item[{type(thermo\+\_\+var\+\_\+ptrs), intent(in)}]{tv,  }\item[{type(\mbox{\hyperlink{structmom__kappa__shear_1_1kappa__shear__cs}{kappa\+\_\+shear\+\_\+cs}}), pointer}]{CS,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{real, dimension( gv \%ke+1), intent(out), optional}]{I\+\_\+\+Ld2\+\_\+1d,  }\item[{real, dimension( gv \%ke+1), intent(out), optional}]{dz\+\_\+\+Int\+\_\+1d }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



This subroutine calculates shear-\/driven diffusivity and T\+KE in a single column. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in,out}}  & {\em kappa} & The time-\/weighted average of kappa \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em tke} & The Turbulent Kinetic Energy per unit mass at \\
\hline
\mbox{\texttt{ in}}  & {\em nzc} & The number of active layers in the column. \\
\hline
\mbox{\texttt{ in}}  & {\em f2} & The square of the Coriolis parameter \mbox{[}T-\/2 $\sim$$>$ s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em surface\+\_\+pres} & The surface pressure \mbox{[}R L2 T-\/2 $\sim$$>$ Pa\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dz} & The layer thickness \mbox{[}Z $\sim$$>$ m\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em u0xdz} & The initial zonal velocity times dz \mbox{[}Z L T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em v0xdz} & The initial meridional velocity times dz \mbox{[}Z L T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em t0xdz} & The initial temperature times dz \mbox{[}degC Z $\sim$$>$ degC m\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em s0xdz} & The initial salinity times dz \mbox{[}ppt Z $\sim$$>$ ppt m\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em kappa\+\_\+avg} & The time-\/weighted average of kappa \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em tke\+\_\+avg} & The time-\/weighted average of T\+KE \mbox{[}Z2 T-\/2 $\sim$$>$ m2 s-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em tv} & A structure containing pointers to any available thermodynamic fields. Absent fields have N\+U\+LL ptrs. \\
\hline
 & {\em cs} & The control structure returned by a previous call to kappa\+\_\+shear\+\_\+init. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ out}}  & {\em i\+\_\+ld2\+\_\+1d} & The inverse of the squared mixing length \mbox{[}Z-\/2 $\sim$$>$ m-\/2\mbox{]}. \\
\hline
\mbox{\texttt{ out}}  & {\em dz\+\_\+int\+\_\+1d} & The extent of a finite-\/volume space surrounding an interface, \\
\hline
\end{DoxyParams}


Definition at line 627 of file M\+O\+M\+\_\+kappa\+\_\+shear.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{627   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{ !< The ocean's vertical grid structure.}}
\DoxyCodeLine{628 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV)+1)}, \&}
\DoxyCodeLine{629                      \textcolor{keywordtype}{intent(inout)} :: kappa\textcolor{comment}{ !< The time-weighted average of kappa [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{630 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV)+1)}, \&}
\DoxyCodeLine{631                      \textcolor{keywordtype}{intent(out)}   :: tke\textcolor{comment}{  !< The Turbulent Kinetic Energy per unit mass at}}
\DoxyCodeLine{632 \textcolor{comment}{                                           !! an interface [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{633   \textcolor{keywordtype}{integer},           \textcolor{keywordtype}{intent(in)}    :: nzc\textcolor{comment}{  !< The number of active layers in the column.}}
\DoxyCodeLine{634 \textcolor{keywordtype}{  real},              \textcolor{keywordtype}{intent(in)}    :: f2\textcolor{comment}{   !< The square of the Coriolis parameter [T-2 ~> s-2].}}
\DoxyCodeLine{635 \textcolor{keywordtype}{  real},              \textcolor{keywordtype}{intent(in)}    :: surface\_pres\textcolor{comment}{  !< The surface pressure [R L2 T-2 ~> Pa].}}
\DoxyCodeLine{636 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV))}, \&}
\DoxyCodeLine{637                      \textcolor{keywordtype}{intent(in)}    :: dz\textcolor{comment}{   !< The layer thickness [Z ~> m].}}
\DoxyCodeLine{638 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV))}, \&}
\DoxyCodeLine{639                      \textcolor{keywordtype}{intent(in)}    :: u0xdz\textcolor{comment}{ !< The initial zonal velocity times dz [Z L T-1 ~> m2 s-1].}}
\DoxyCodeLine{640 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV))}, \&}
\DoxyCodeLine{641                      \textcolor{keywordtype}{intent(in)}    :: v0xdz\textcolor{comment}{ !< The initial meridional velocity times dz [Z L T-1 ~> m2 s-1].}}
\DoxyCodeLine{642 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV))}, \&}
\DoxyCodeLine{643                      \textcolor{keywordtype}{intent(in)}    :: T0xdz\textcolor{comment}{ !< The initial temperature times dz [degC Z ~> degC m].}}
\DoxyCodeLine{644 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV))}, \&}
\DoxyCodeLine{645                      \textcolor{keywordtype}{intent(in)}    :: S0xdz\textcolor{comment}{ !< The initial salinity times dz [ppt Z ~> ppt m].}}
\DoxyCodeLine{646 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV)+1)}, \&}
\DoxyCodeLine{647                      \textcolor{keywordtype}{intent(out)}   :: kappa\_avg\textcolor{comment}{ !< The time-weighted average of kappa [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{648 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZK\_(GV)+1)}, \&}
\DoxyCodeLine{649                      \textcolor{keywordtype}{intent(out)}   :: tke\_avg\textcolor{comment}{  !< The time-weighted average of TKE [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{650 \textcolor{keywordtype}{  real},                    \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{ !< Time increment [T ~> s].}}
\DoxyCodeLine{651   \textcolor{keywordtype}{type}(thermo\_var\_ptrs),   \textcolor{keywordtype}{intent(in)}    :: tv\textcolor{comment}{ !< A structure containing pointers to any}}
\DoxyCodeLine{652 \textcolor{comment}{                                               !! available thermodynamic fields. Absent fields}}
\DoxyCodeLine{653 \textcolor{comment}{                                               !! have NULL ptrs.}}
\DoxyCodeLine{654   \textcolor{keywordtype}{type}(Kappa\_shear\_CS),    \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{ !< The control structure returned by a previous}}
\DoxyCodeLine{655 \textcolor{comment}{                                               !! call to kappa\_shear\_init.}}
\DoxyCodeLine{656   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{ !< A dimensional unit scaling type}}
\DoxyCodeLine{657 \textcolor{keywordtype}{  real},  \textcolor{keywordtype}{dimension(SZK\_(GV)+1)}, \&}
\DoxyCodeLine{658            \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)}   :: I\_Ld2\_1d\textcolor{comment}{ !< The inverse of the squared mixing length [Z-2 ~> m-2].}}
\DoxyCodeLine{659 \textcolor{keywordtype}{  real},  \textcolor{keywordtype}{dimension(SZK\_(GV)+1)}, \&}
\DoxyCodeLine{660            \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)}   :: dz\_Int\_1d\textcolor{comment}{ !< The extent of a finite-volume space surrounding an interface,}}
\DoxyCodeLine{661 \textcolor{comment}{                                               !! as used in calculating kappa and TKE [Z ~> m].}}
\DoxyCodeLine{662 }
\DoxyCodeLine{663   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{664 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nzc)} :: \&}
\DoxyCodeLine{665     u, \&        \textcolor{comment}{! The zonal velocity after a timestep of mixing [L T-1 ~> m s-1].}}
\DoxyCodeLine{666     v, \&        \textcolor{comment}{! The meridional velocity after a timestep of mixing [L T-1 ~> m s-1].}}
\DoxyCodeLine{667     Idz, \&      \textcolor{comment}{! The inverse of the distance between TKE points [Z-1 ~> m-1].}}
\DoxyCodeLine{668     T, \&        \textcolor{comment}{! The potential temperature after a timestep of mixing [degC].}}
\DoxyCodeLine{669     Sal, \&      \textcolor{comment}{! The salinity after a timestep of mixing [ppt].}}
\DoxyCodeLine{670     u\_test, v\_test, \& \textcolor{comment}{! Temporary velocities [L T-1 ~> m s-1].}}
\DoxyCodeLine{671     T\_test, S\_test \textcolor{comment}{! Temporary temperatures [degC] and salinities [ppt].}}
\DoxyCodeLine{672 }
\DoxyCodeLine{673 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(nzc+1)} :: \&}
\DoxyCodeLine{674     N2, \&       \textcolor{comment}{! The squared buoyancy frequency at an interface [T-2 ~> s-2].}}
\DoxyCodeLine{675     dz\_Int, \&   \textcolor{comment}{! The extent of a finite-volume space surrounding an interface,}}
\DoxyCodeLine{676                 \textcolor{comment}{! as used in calculating kappa and TKE [Z ~> m].}}
\DoxyCodeLine{677     i\_dz\_int, \& \textcolor{comment}{! The inverse of the distance between velocity \& density points}}
\DoxyCodeLine{678                 \textcolor{comment}{! above and below an interface [Z-1 ~> m-1].  This is used to}}
\DoxyCodeLine{679                 \textcolor{comment}{! calculate N2, shear, and fluxes, and it might differ from}}
\DoxyCodeLine{680                 \textcolor{comment}{! 1/dz\_Int, as they have different uses.}}
\DoxyCodeLine{681     s2, \&       \textcolor{comment}{! The squared shear at an interface [T-2 ~> s-2].}}
\DoxyCodeLine{682     a1, \&       \textcolor{comment}{! a1 is the coupling between adjacent interfaces in the TKE,}}
\DoxyCodeLine{683                 \textcolor{comment}{! velocity, and density equations [Z s-1 ~> m s-1] or [Z ~> m]}}
\DoxyCodeLine{684     c1, \&       \textcolor{comment}{! c1 is used in the tridiagonal (and similar) solvers.}}
\DoxyCodeLine{685     k\_src, \&    \textcolor{comment}{! The shear-dependent source term in the kappa equation [T-1 ~> s-1].}}
\DoxyCodeLine{686     kappa\_src, \& \textcolor{comment}{! The shear-dependent source term in the kappa equation [T-1 ~> s-1].}}
\DoxyCodeLine{687     kappa\_out, \& \textcolor{comment}{! The kappa that results from the kappa equation [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{688     kappa\_mid, \& \textcolor{comment}{! The average of the initial and predictor estimates of kappa [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{689     tke\_pred, \& \textcolor{comment}{! The value of TKE from a predictor step [Z2 T-2 ~> m2 s-2].}}
\DoxyCodeLine{690     kappa\_pred, \& \textcolor{comment}{! The value of kappa from a predictor step [Z2 T-1 ~> m2 s-1].}}
\DoxyCodeLine{691     pressure, \& \textcolor{comment}{! The pressure at an interface [R L2 T-2 ~> Pa].}}
\DoxyCodeLine{692     t\_int, \&    \textcolor{comment}{! The temperature interpolated to an interface [degC].}}
\DoxyCodeLine{693     sal\_int, \&  \textcolor{comment}{! The salinity interpolated to an interface [ppt].}}
\DoxyCodeLine{694     dbuoy\_dt, \& \textcolor{comment}{! The partial derivatives of buoyancy with changes in temperature}}
\DoxyCodeLine{695     dbuoy\_ds, \& \textcolor{comment}{! and salinity, [Z T-2 degC-1 ~> m s-2 degC-1] and [Z T-2 ppt-1 ~> m s-2 ppt-1].}}
\DoxyCodeLine{696     i\_l2\_bdry, \&   \textcolor{comment}{! The inverse of the square of twice the harmonic mean}}
\DoxyCodeLine{697                    \textcolor{comment}{! distance to the top and bottom boundaries [Z-2 ~> m-2].}}
\DoxyCodeLine{698     k\_q, \&         \textcolor{comment}{! Diffusivity divided by TKE [T ~> s].}}
\DoxyCodeLine{699     k\_q\_tmp, \&     \textcolor{comment}{! A temporary copy of diffusivity divided by TKE [T ~> s].}}
\DoxyCodeLine{700     local\_src\_avg, \& \textcolor{comment}{! The time-integral of the local source [nondim].}}
\DoxyCodeLine{701     tol\_min, \& \textcolor{comment}{! Minimum tolerated ksrc for the corrector step [T-1 ~> s-1].}}
\DoxyCodeLine{702     tol\_max, \& \textcolor{comment}{! Maximum tolerated ksrc for the corrector step [T-1 ~> s-1].}}
\DoxyCodeLine{703     tol\_chg, \& \textcolor{comment}{! The tolerated kappa change integrated over a timestep [nondim].}}
\DoxyCodeLine{704     dist\_from\_top, \&  \textcolor{comment}{! The distance from the top surface [Z ~> m].}}
\DoxyCodeLine{705     local\_src     \textcolor{comment}{! The sum of all sources of kappa, including kappa\_src and}}
\DoxyCodeLine{706                   \textcolor{comment}{! sources from the elliptic term [T-1 ~> s-1].}}
\DoxyCodeLine{707 }
\DoxyCodeLine{708 \textcolor{keywordtype}{  real} :: dist\_from\_bot \textcolor{comment}{! The distance from the bottom surface [Z ~> m].}}
\DoxyCodeLine{709 \textcolor{keywordtype}{  real} :: b1            \textcolor{comment}{! The inverse of the pivot in the tridiagonal equations.}}
\DoxyCodeLine{710 \textcolor{keywordtype}{  real} :: bd1           \textcolor{comment}{! A term in the denominator of b1.}}
\DoxyCodeLine{711 \textcolor{keywordtype}{  real} :: d1            \textcolor{comment}{! 1 - c1 in the tridiagonal equations.}}
\DoxyCodeLine{712 \textcolor{keywordtype}{  real} :: gR0           \textcolor{comment}{! A conversion factor from Z to pressure, given by Rho\_0 times g}}
\DoxyCodeLine{713                         \textcolor{comment}{! [R L2 T-2 Z-1 ~> kg m-2 s-2].}}
\DoxyCodeLine{714 \textcolor{keywordtype}{  real} :: g\_R0          \textcolor{comment}{! g\_R0 is a rescaled version of g/Rho [Z R-1 T-2 ~> m4 kg-1 s-2].}}
\DoxyCodeLine{715 \textcolor{keywordtype}{  real} :: Norm          \textcolor{comment}{! A factor that normalizes two weights to 1 [Z-2 ~> m-2].}}
\DoxyCodeLine{716 \textcolor{keywordtype}{  real} :: tol\_dksrc     \textcolor{comment}{! Tolerance for the change in the kappa source within an iteration}}
\DoxyCodeLine{717                         \textcolor{comment}{! relative to the local source [nondim].  This must be greater than 1.}}
\DoxyCodeLine{718 \textcolor{keywordtype}{  real} :: tol2          \textcolor{comment}{! The tolerance for the change in the kappa source within an iteration}}
\DoxyCodeLine{719                         \textcolor{comment}{! relative to the average local source over previous iterations [nondim].}}
\DoxyCodeLine{720 \textcolor{keywordtype}{  real} :: tol\_dksrc\_low \textcolor{comment}{! The tolerance for the fractional decrease in ksrc}}
\DoxyCodeLine{721                         \textcolor{comment}{! within an iteration [nondim].  0 < tol\_dksrc\_low < 1.}}
\DoxyCodeLine{722 \textcolor{keywordtype}{  real} :: Ri\_crit       \textcolor{comment}{!   The critical shear Richardson number for shear-}}
\DoxyCodeLine{723                         \textcolor{comment}{! driven mixing [nondim]. The theoretical value is 0.25.}}
\DoxyCodeLine{724 \textcolor{keywordtype}{  real} :: dt\_rem        \textcolor{comment}{!   The remaining time to advance the solution [T ~> s].}}
\DoxyCodeLine{725 \textcolor{keywordtype}{  real} :: dt\_now        \textcolor{comment}{!   The time step used in the current iteration [T ~> s].}}
\DoxyCodeLine{726 \textcolor{keywordtype}{  real} :: dt\_wt         \textcolor{comment}{!   The fractional weight of the current iteration [nondim].}}
\DoxyCodeLine{727 \textcolor{keywordtype}{  real} :: dt\_test       \textcolor{comment}{!   A time-step that is being tested for whether it}}
\DoxyCodeLine{728                         \textcolor{comment}{! gives acceptably small changes in k\_src [T ~> s].}}
\DoxyCodeLine{729 \textcolor{keywordtype}{  real} :: Idtt          \textcolor{comment}{!   Idtt = 1 / dt\_test [T-1 ~> s-1].}}
\DoxyCodeLine{730 \textcolor{keywordtype}{  real} :: dt\_inc        \textcolor{comment}{!   An increment to dt\_test that is being tested [T ~> s].}}
\DoxyCodeLine{731 }
\DoxyCodeLine{732 \textcolor{keywordtype}{  real} :: k0dt          \textcolor{comment}{! The background diffusivity times the timestep [Z2 ~> m2].}}
\DoxyCodeLine{733   \textcolor{keywordtype}{logical} :: valid\_dt   \textcolor{comment}{! If true, all levels so far exhibit acceptably small changes in k\_src.}}
\DoxyCodeLine{734   \textcolor{keywordtype}{logical} :: use\_temperature  \textcolor{comment}{!  If true, temperature and salinity have been}}
\DoxyCodeLine{735                         \textcolor{comment}{! allocated and are being used as state variables.}}
\DoxyCodeLine{736   \textcolor{keywordtype}{integer} :: ks\_kappa, ke\_kappa  \textcolor{comment}{! The k-range with nonzero kappas.}}
\DoxyCodeLine{737   \textcolor{keywordtype}{integer} :: dt\_halvings   \textcolor{comment}{! The number of times that the time-step is halved}}
\DoxyCodeLine{738                            \textcolor{comment}{! in seeking an acceptable timestep.  If none is}}
\DoxyCodeLine{739                            \textcolor{comment}{! found, dt\_rem*0.5\string^dt\_halvings is used.}}
\DoxyCodeLine{740   \textcolor{keywordtype}{integer} :: dt\_refinements \textcolor{comment}{! The number of 2-fold refinements that will be used}}
\DoxyCodeLine{741                            \textcolor{comment}{! to estimate the maximum permitted time step.  I.e.,}}
\DoxyCodeLine{742                            \textcolor{comment}{! the resolution is 1/2\string^dt\_refinements.}}
\DoxyCodeLine{743   \textcolor{keywordtype}{integer} :: k, itt, itt\_dt}
\DoxyCodeLine{744 }
\DoxyCodeLine{745   \textcolor{comment}{! This calculation of N2 is for debugging only.}}
\DoxyCodeLine{746   \textcolor{comment}{! real, dimension(SZK\_(GV)+1) :: \&}}
\DoxyCodeLine{747   \textcolor{comment}{!   N2\_debug, \& ! A version of N2 for debugging [T-2 ~> s-2]}}
\DoxyCodeLine{748 }
\DoxyCodeLine{749   ri\_crit = cs\%Rino\_crit}
\DoxyCodeLine{750   gr0 = gv\%Rho0 * gv\%g\_Earth}
\DoxyCodeLine{751   g\_r0 = (us\%L\_to\_Z**2 * gv\%g\_Earth) / (gv\%Rho0)}
\DoxyCodeLine{752   k0dt = dt*cs\%kappa\_0}
\DoxyCodeLine{753 }
\DoxyCodeLine{754   tol\_dksrc = cs\%kappa\_src\_max\_chg}
\DoxyCodeLine{755   \textcolor{keywordflow}{if} (tol\_dksrc == 10.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{756     \textcolor{comment}{! This is equivalent to the expression below, but avoids changes at roundoff for the default value.}}
\DoxyCodeLine{757     tol\_dksrc\_low = 0.95}
\DoxyCodeLine{758   \textcolor{keywordflow}{else}}
\DoxyCodeLine{759     tol\_dksrc\_low = (tol\_dksrc - 0.5)/tol\_dksrc}
\DoxyCodeLine{760 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{761   tol2 = 2.0*cs\%kappa\_tol\_err}
\DoxyCodeLine{762   dt\_refinements = 5 \textcolor{comment}{! Selected so that 1/2\string^dt\_refinements < 1-tol\_dksrc\_low}}
\DoxyCodeLine{763   use\_temperature = .false. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(tv\%T)) use\_temperature = .true.}
\DoxyCodeLine{764 }
\DoxyCodeLine{765 }
\DoxyCodeLine{766   \textcolor{comment}{! Set up Idz as the inverse of layer thicknesses.}}
\DoxyCodeLine{767   \textcolor{keywordflow}{do} k=1,nzc ; idz(k) = 1.0 / dz(k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{768   \textcolor{comment}{!   Set up I\_dz\_int as the inverse of the distance between}}
\DoxyCodeLine{769   \textcolor{comment}{! adjacent layer centers.}}
\DoxyCodeLine{770   i\_dz\_int(1) = 2.0 / dz(1)}
\DoxyCodeLine{771   dist\_from\_top(1) = 0.0}
\DoxyCodeLine{772   \textcolor{keywordflow}{do} k=2,nzc}
\DoxyCodeLine{773     i\_dz\_int(k) = 2.0 / (dz(k-1) + dz(k))}
\DoxyCodeLine{774     dist\_from\_top(k) = dist\_from\_top(k-1) + dz(k-1)}
\DoxyCodeLine{775 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{776   i\_dz\_int(nzc+1) = 2.0 / dz(nzc)}
\DoxyCodeLine{777 }
\DoxyCodeLine{778   \textcolor{comment}{!   Determine the velocities and thicknesses after eliminating massless}}
\DoxyCodeLine{779   \textcolor{comment}{! layers and applying a time-step of background diffusion.}}
\DoxyCodeLine{780   \textcolor{keywordflow}{if} (nzc > 1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{781     a1(2) = k0dt*i\_dz\_int(2)}
\DoxyCodeLine{782     b1 = 1.0 / (dz(1) + a1(2))}
\DoxyCodeLine{783     u(1) = b1 * u0xdz(1) ; v(1) = b1 * v0xdz(1)}
\DoxyCodeLine{784     t(1) = b1 * t0xdz(1) ; sal(1) = b1 * s0xdz(1)}
\DoxyCodeLine{785     c1(2) = a1(2) * b1 ; d1 = dz(1) * b1 \textcolor{comment}{! = 1 - c1}}
\DoxyCodeLine{786     \textcolor{keywordflow}{do} k=2,nzc-1}
\DoxyCodeLine{787       bd1 = dz(k) + d1*a1(k)}
\DoxyCodeLine{788       a1(k+1) = k0dt*i\_dz\_int(k+1)}
\DoxyCodeLine{789       b1 = 1.0 / (bd1 + a1(k+1))}
\DoxyCodeLine{790       u(k) = b1 * (u0xdz(k) + a1(k)*u(k-1))}
\DoxyCodeLine{791       v(k) = b1 * (v0xdz(k) + a1(k)*v(k-1))}
\DoxyCodeLine{792       t(k) = b1 * (t0xdz(k) + a1(k)*t(k-1))}
\DoxyCodeLine{793       sal(k) = b1 * (s0xdz(k) + a1(k)*sal(k-1))}
\DoxyCodeLine{794       c1(k+1) = a1(k+1) * b1 ; d1 = bd1 * b1 \textcolor{comment}{! d1 = 1 - c1}}
\DoxyCodeLine{795 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{796     \textcolor{comment}{! rho or T and S have insulating boundary conditions, u \& v use no-slip}}
\DoxyCodeLine{797     \textcolor{comment}{! bottom boundary conditions (if kappa0 > 0).}}
\DoxyCodeLine{798     \textcolor{comment}{! For no-slip bottom boundary conditions}}
\DoxyCodeLine{799     b1 = 1.0 / ((dz(nzc) + d1*a1(nzc)) + k0dt*i\_dz\_int(nzc+1))}
\DoxyCodeLine{800     u(nzc) = b1 * (u0xdz(nzc) + a1(nzc)*u(nzc-1))}
\DoxyCodeLine{801     v(nzc) = b1 * (v0xdz(nzc) + a1(nzc)*v(nzc-1))}
\DoxyCodeLine{802     \textcolor{comment}{! For insulating boundary conditions}}
\DoxyCodeLine{803     b1 = 1.0 / (dz(nzc) + d1*a1(nzc))}
\DoxyCodeLine{804     t(nzc) = b1 * (t0xdz(nzc) + a1(nzc)*t(nzc-1))}
\DoxyCodeLine{805     sal(nzc) = b1 * (s0xdz(nzc) + a1(nzc)*sal(nzc-1))}
\DoxyCodeLine{806     \textcolor{keywordflow}{do} k=nzc-1,1,-1}
\DoxyCodeLine{807       u(k) = u(k) + c1(k+1)*u(k+1) ; v(k) = v(k) + c1(k+1)*v(k+1)}
\DoxyCodeLine{808       t(k) = t(k) + c1(k+1)*t(k+1) ; sal(k) = sal(k) + c1(k+1)*sal(k+1)}
\DoxyCodeLine{809 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{810   \textcolor{keywordflow}{else}}
\DoxyCodeLine{811     \textcolor{comment}{! This is correct, but probably unnecessary.}}
\DoxyCodeLine{812     b1 = 1.0 / (dz(1) + k0dt*i\_dz\_int(2))}
\DoxyCodeLine{813     u(1) = b1 * u0xdz(1) ; v(1) = b1 * v0xdz(1)}
\DoxyCodeLine{814     b1 = 1.0 / dz(1)}
\DoxyCodeLine{815     t(1) = b1 * t0xdz(1) ; sal(1) = b1 * s0xdz(1)}
\DoxyCodeLine{816 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{817 }
\DoxyCodeLine{818   \textcolor{comment}{! This uses half the harmonic mean of thicknesses to provide two estimates}}
\DoxyCodeLine{819   \textcolor{comment}{! of the boundary between cells, and the inverse of the harmonic mean to}}
\DoxyCodeLine{820   \textcolor{comment}{! weight the two estimates.  The net effect is that interfaces around thin}}
\DoxyCodeLine{821   \textcolor{comment}{! layers have thin cells, and the total thickness adds up properly.}}
\DoxyCodeLine{822   \textcolor{comment}{! The top- and bottom- interfaces have zero thickness, consistent with}}
\DoxyCodeLine{823   \textcolor{comment}{! adding additional zero thickness layers.}}
\DoxyCodeLine{824   dz\_int(1) = 0.0 ; dz\_int(2) = dz(1)}
\DoxyCodeLine{825   \textcolor{keywordflow}{do} k=2,nzc-1}
\DoxyCodeLine{826     norm = 1.0 / (dz(k)*(dz(k-1)+dz(k+1)) + 2.0*dz(k-1)*dz(k+1))}
\DoxyCodeLine{827     dz\_int(k) = dz\_int(k) + dz(k) * ( ((dz(k)+dz(k+1)) * dz(k-1)) * norm)}
\DoxyCodeLine{828     dz\_int(k+1) = dz(k) * ( ((dz(k-1)+dz(k)) * dz(k+1)) * norm)}
\DoxyCodeLine{829 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{830   dz\_int(nzc) = dz\_int(nzc) + dz(nzc) ; dz\_int(nzc+1) = 0.0}
\DoxyCodeLine{831 }
\DoxyCodeLine{832   dist\_from\_bot = 0.0}
\DoxyCodeLine{833   \textcolor{keywordflow}{do} k=nzc,2,-1}
\DoxyCodeLine{834     dist\_from\_bot = dist\_from\_bot + dz(k)}
\DoxyCodeLine{835     i\_l2\_bdry(k) = (dist\_from\_top(k) + dist\_from\_bot)**2 / \&}
\DoxyCodeLine{836                    (dist\_from\_top(k) * dist\_from\_bot)**2}
\DoxyCodeLine{837 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{838 }
\DoxyCodeLine{839   \textcolor{comment}{! Calculate thermodynamic coefficients and an initial estimate of N2.}}
\DoxyCodeLine{840   \textcolor{keywordflow}{if} (use\_temperature) \textcolor{keywordflow}{then}}
\DoxyCodeLine{841     pressure(1) = surface\_pres}
\DoxyCodeLine{842     \textcolor{keywordflow}{do} k=2,nzc}
\DoxyCodeLine{843       pressure(k) = pressure(k-1) + gr0*dz(k-1)}
\DoxyCodeLine{844       t\_int(k) = 0.5*(t(k-1) + t(k))}
\DoxyCodeLine{845       sal\_int(k) = 0.5*(sal(k-1) + sal(k))}
\DoxyCodeLine{846 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{847     \textcolor{keyword}{call }calculate\_density\_derivs(t\_int, sal\_int, pressure, dbuoy\_dt, dbuoy\_ds, \&}
\DoxyCodeLine{848                                   tv\%eqn\_of\_state, (/2,nzc/), scale=-g\_r0 )}
\DoxyCodeLine{849   \textcolor{keywordflow}{else}}
\DoxyCodeLine{850     \textcolor{keywordflow}{do} k=1,nzc+1 ; dbuoy\_dt(k) = -g\_r0 ; dbuoy\_ds(k) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{851 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{852 }
\DoxyCodeLine{853   \textcolor{comment}{! N2\_debug(1) = 0.0 ; N2\_debug(nzc+1) = 0.0}}
\DoxyCodeLine{854   \textcolor{comment}{! do K=2,nzc}}
\DoxyCodeLine{855   \textcolor{comment}{!   N2\_debug(K) = max((dbuoy\_dT(K) * (T0xdz(k-1)*Idz(k-1) - T0xdz(k)*Idz(k)) + \&}}
\DoxyCodeLine{856   \textcolor{comment}{!                      dbuoy\_dS(K) * (S0xdz(k-1)*Idz(k-1) - S0xdz(k)*Idz(k))) * \&}}
\DoxyCodeLine{857   \textcolor{comment}{!                      I\_dz\_int(K), 0.0)}}
\DoxyCodeLine{858   \textcolor{comment}{! enddo}}
\DoxyCodeLine{859 }
\DoxyCodeLine{860   \textcolor{comment}{! This call just calculates N2 and S2.}}
\DoxyCodeLine{861   \textcolor{keyword}{call }calculate\_projected\_state(kappa, u, v, t, sal, 0.0, nzc, dz, i\_dz\_int, \&}
\DoxyCodeLine{862                                  dbuoy\_dt, dbuoy\_ds, u, v, t, sal, gv, us, \&}
\DoxyCodeLine{863                                  n2=n2, s2=s2, vel\_underflow=cs\%vel\_underflow)}
\DoxyCodeLine{864 \textcolor{comment}{! ----------------------------------------------------}}
\DoxyCodeLine{865 \textcolor{comment}{! Iterate}}
\DoxyCodeLine{866 \textcolor{comment}{! ----------------------------------------------------}}
\DoxyCodeLine{867   dt\_rem = dt}
\DoxyCodeLine{868   \textcolor{keywordflow}{do} k=1,nzc+1}
\DoxyCodeLine{869     k\_q(k) = 0.0}
\DoxyCodeLine{870     kappa\_avg(k) = 0.0 ; tke\_avg(k) = 0.0}
\DoxyCodeLine{871     local\_src\_avg(k) = 0.0}
\DoxyCodeLine{872     \textcolor{comment}{! Use the grid spacings to scale errors in the source.}}
\DoxyCodeLine{873     \textcolor{keywordflow}{if} ( dz\_int(k) > 0.0 ) \&}
\DoxyCodeLine{874       local\_src\_avg(k) = 0.1 * k0dt * i\_dz\_int(k) / dz\_int(k)}
\DoxyCodeLine{875 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{876 }
\DoxyCodeLine{877 \textcolor{comment}{! call cpu\_clock\_end(id\_clock\_setup)}}
\DoxyCodeLine{878 }
\DoxyCodeLine{879 \textcolor{comment}{! do itt=1,CS\%max\_RiNo\_it}}
\DoxyCodeLine{880   \textcolor{keywordflow}{do} itt=1,cs\%max\_KS\_it}
\DoxyCodeLine{881 }
\DoxyCodeLine{882 \textcolor{comment}{! ----------------------------------------------------}}
\DoxyCodeLine{883 \textcolor{comment}{! Calculate new values of u, v, rho, N\string^2 and S.}}
\DoxyCodeLine{884 \textcolor{comment}{! ----------------------------------------------------}}
\DoxyCodeLine{885 }
\DoxyCodeLine{886   \textcolor{comment}{! call cpu\_clock\_begin(id\_clock\_KQ)}}
\DoxyCodeLine{887     \textcolor{keyword}{call }find\_kappa\_tke(n2, s2, kappa, idz, dz\_int, i\_l2\_bdry, f2, \&}
\DoxyCodeLine{888                         nzc, cs, gv, us, k\_q, tke, kappa\_out, kappa\_src, local\_src)}
\DoxyCodeLine{889   \textcolor{comment}{! call cpu\_clock\_end(id\_clock\_KQ)}}
\DoxyCodeLine{890 }
\DoxyCodeLine{891   \textcolor{comment}{! call cpu\_clock\_begin(id\_clock\_avg)}}
\DoxyCodeLine{892     \textcolor{comment}{! Determine the range of non-zero values of kappa\_out.}}
\DoxyCodeLine{893     ks\_kappa = gv\%ke+1 ; ke\_kappa = 0}
\DoxyCodeLine{894     \textcolor{keywordflow}{do} k=2,nzc ; \textcolor{keywordflow}{if} (kappa\_out(k) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{895       ks\_kappa = k ; \textcolor{keywordflow}{exit}}
\DoxyCodeLine{896 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{897     \textcolor{keywordflow}{do} k=nzc,ks\_kappa,-1 ; \textcolor{keywordflow}{if} (kappa\_out(k) > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{898       ke\_kappa = k ; \textcolor{keywordflow}{exit}}
\DoxyCodeLine{899 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{900     \textcolor{keywordflow}{if} (ke\_kappa == nzc) kappa\_out(nzc+1) = 0.0}
\DoxyCodeLine{901   \textcolor{comment}{! call cpu\_clock\_end(id\_clock\_avg)}}
\DoxyCodeLine{902 }
\DoxyCodeLine{903     \textcolor{comment}{! Determine how long to use this value of kappa (dt\_now).}}
\DoxyCodeLine{904 }
\DoxyCodeLine{905   \textcolor{comment}{! call cpu\_clock\_begin(id\_clock\_project)}}
\DoxyCodeLine{906     \textcolor{keywordflow}{if} ((ke\_kappa < ks\_kappa) .or. (itt==cs\%max\_KS\_it)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{907       dt\_now = dt\_rem}
\DoxyCodeLine{908     \textcolor{keywordflow}{else}}
\DoxyCodeLine{909       \textcolor{comment}{! Limit dt\_now so that |k\_src(k)-kappa\_src(k)| < tol * local\_src(k)}}
\DoxyCodeLine{910       dt\_test = dt\_rem}
\DoxyCodeLine{911       \textcolor{keywordflow}{do} k=2,nzc}
\DoxyCodeLine{912         tol\_max(k) = kappa\_src(k) + tol\_dksrc * local\_src(k)}
\DoxyCodeLine{913         tol\_min(k) = kappa\_src(k) - tol\_dksrc\_low * local\_src(k)}
\DoxyCodeLine{914         tol\_chg(k) = tol2 * local\_src\_avg(k)}
\DoxyCodeLine{915 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{916 }
\DoxyCodeLine{917       \textcolor{keywordflow}{do} itt\_dt=1,(cs\%max\_KS\_it+1-itt)/2}
\DoxyCodeLine{918         \textcolor{comment}{!   The maximum number of times that the time-step is halved in}}
\DoxyCodeLine{919         \textcolor{comment}{! seeking an acceptable timestep is reduced with each iteration,}}
\DoxyCodeLine{920         \textcolor{comment}{! so that as the maximum number of iterations is approached, the}}
\DoxyCodeLine{921         \textcolor{comment}{! whole remaining timestep is used.  Typically, an acceptable}}
\DoxyCodeLine{922         \textcolor{comment}{! timestep is found long before the minimum is reached, so the}}
\DoxyCodeLine{923         \textcolor{comment}{! value of max\_KS\_it may be unimportant, especially if it is large}}
\DoxyCodeLine{924         \textcolor{comment}{! enough.}}
\DoxyCodeLine{925         \textcolor{keyword}{call }calculate\_projected\_state(kappa\_out, u, v, t, sal, 0.5*dt\_test, nzc, dz, i\_dz\_int, \&}
\DoxyCodeLine{926                                        dbuoy\_dt, dbuoy\_ds, u\_test, v\_test, t\_test, s\_test, \&}
\DoxyCodeLine{927                                        gv, us, n2, s2, ks\_int=ks\_kappa, ke\_int=ke\_kappa, \&}
\DoxyCodeLine{928                                        vel\_underflow=cs\%vel\_underflow)}
\DoxyCodeLine{929         valid\_dt = .true.}
\DoxyCodeLine{930         idtt = 1.0 / dt\_test}
\DoxyCodeLine{931         \textcolor{keywordflow}{do} k=max(ks\_kappa-1,2),min(ke\_kappa+1,nzc)}
\DoxyCodeLine{932           \textcolor{keywordflow}{if} (n2(k) < ri\_crit * s2(k)) \textcolor{keywordflow}{then} \textcolor{comment}{! Equivalent to Ri < Ri\_crit.}}
\DoxyCodeLine{933             k\_src(k) = (2.0 * cs\%Shearmix\_rate * sqrt(s2(k))) * \&}
\DoxyCodeLine{934                        ((ri\_crit*s2(k) - n2(k)) / (ri\_crit*s2(k) + cs\%FRi\_curvature*n2(k)))}
\DoxyCodeLine{935             \textcolor{keywordflow}{if} (cs\%restrictive\_tolerance\_check) \textcolor{keywordflow}{then}}
\DoxyCodeLine{936               \textcolor{keywordflow}{if} ((k\_src(k) > min(tol\_max(k), kappa\_src(k) + idtt*tol\_chg(k))) .or. \&}
\DoxyCodeLine{937                   (k\_src(k) < max(tol\_min(k), kappa\_src(k) - idtt*tol\_chg(k)))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{938                 valid\_dt = .false. ; \textcolor{keywordflow}{exit}}
\DoxyCodeLine{939 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{940             \textcolor{keywordflow}{else}}
\DoxyCodeLine{941               \textcolor{keywordflow}{if} ((k\_src(k) > max(tol\_max(k), kappa\_src(k) + idtt*tol\_chg(k))) .or. \&}
\DoxyCodeLine{942                   (k\_src(k) < min(tol\_min(k), kappa\_src(k) - idtt*tol\_chg(k)))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{943                 valid\_dt = .false. ; \textcolor{keywordflow}{exit}}
\DoxyCodeLine{944 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{945 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{946           \textcolor{keywordflow}{else}}
\DoxyCodeLine{947             \textcolor{keywordflow}{if} (0.0 < min(tol\_min(k), kappa\_src(k) - idtt*tol\_chg(k))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{948               valid\_dt = .false. ; k\_src(k) = 0.0 ; \textcolor{keywordflow}{exit}}
\DoxyCodeLine{949 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{950 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{951 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{952 }
\DoxyCodeLine{953         \textcolor{keywordflow}{if} (valid\_dt) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{954         dt\_test = 0.5*dt\_test}
\DoxyCodeLine{955 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{956       \textcolor{keywordflow}{if} ((dt\_test < dt\_rem) .and. valid\_dt) \textcolor{keywordflow}{then}}
\DoxyCodeLine{957         dt\_inc = 0.5*dt\_test}
\DoxyCodeLine{958         \textcolor{keywordflow}{do} itt\_dt=1,dt\_refinements}
\DoxyCodeLine{959           \textcolor{keyword}{call }calculate\_projected\_state(kappa\_out, u, v, t, sal, 0.5*(dt\_test+dt\_inc), \&}
\DoxyCodeLine{960                    nzc, dz, i\_dz\_int, dbuoy\_dt, dbuoy\_ds, u\_test, v\_test, t\_test, s\_test, \&}
\DoxyCodeLine{961                    gv, us, n2, s2, ks\_int=ks\_kappa, ke\_int=ke\_kappa, vel\_underflow=cs\%vel\_underflow)}
\DoxyCodeLine{962           valid\_dt = .true.}
\DoxyCodeLine{963           idtt = 1.0 / (dt\_test+dt\_inc)}
\DoxyCodeLine{964           \textcolor{keywordflow}{do} k=max(ks\_kappa-1,2),min(ke\_kappa+1,nzc)}
\DoxyCodeLine{965             \textcolor{keywordflow}{if} (n2(k) < ri\_crit * s2(k)) \textcolor{keywordflow}{then} \textcolor{comment}{! Equivalent to Ri < Ri\_crit.}}
\DoxyCodeLine{966               k\_src(k) = (2.0 * cs\%Shearmix\_rate * sqrt(s2(k))) * \&}
\DoxyCodeLine{967                          ((ri\_crit*s2(k) - n2(k)) / (ri\_crit*s2(k) + cs\%FRi\_curvature*n2(k)))}
\DoxyCodeLine{968               \textcolor{keywordflow}{if} ((k\_src(k) > max(tol\_max(k), kappa\_src(k) + idtt*tol\_chg(k))) .or. \&}
\DoxyCodeLine{969                   (k\_src(k) < min(tol\_min(k), kappa\_src(k) - idtt*tol\_chg(k)))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{970                 valid\_dt = .false. ; \textcolor{keywordflow}{exit}}
\DoxyCodeLine{971 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{972             \textcolor{keywordflow}{else}}
\DoxyCodeLine{973               \textcolor{keywordflow}{if} (0.0 < min(tol\_min(k), kappa\_src(k) - idtt*tol\_chg(k))) \textcolor{keywordflow}{then}}
\DoxyCodeLine{974                 valid\_dt = .false. ; k\_src(k) = 0.0 ; \textcolor{keywordflow}{exit}}
\DoxyCodeLine{975 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{976 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{977 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{978 }
\DoxyCodeLine{979           \textcolor{keywordflow}{if} (valid\_dt) dt\_test = dt\_test + dt\_inc}
\DoxyCodeLine{980           dt\_inc = 0.5*dt\_inc}
\DoxyCodeLine{981 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{982       \textcolor{keywordflow}{else}}
\DoxyCodeLine{983         dt\_inc = 0.0}
\DoxyCodeLine{984 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{985 }
\DoxyCodeLine{986       dt\_now = min(dt\_test*(1.0+cs\%kappa\_tol\_err)+dt\_inc, dt\_rem)}
\DoxyCodeLine{987       \textcolor{keywordflow}{do} k=2,nzc}
\DoxyCodeLine{988         local\_src\_avg(k) = local\_src\_avg(k) + dt\_now * local\_src(k)}
\DoxyCodeLine{989 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{990 \textcolor{keywordflow}{    endif}  \textcolor{comment}{! Are all the values of kappa\_out 0?}}
\DoxyCodeLine{991   \textcolor{comment}{! call cpu\_clock\_end(id\_clock\_project)}}
\DoxyCodeLine{992 }
\DoxyCodeLine{993     \textcolor{comment}{! The state has already been projected forward. Now find new values of kappa.}}
\DoxyCodeLine{994 }
\DoxyCodeLine{995     \textcolor{keywordflow}{if} (ke\_kappa < ks\_kappa) \textcolor{keywordflow}{then}}
\DoxyCodeLine{996       \textcolor{comment}{! There is no mixing now, and will not be again.}}
\DoxyCodeLine{997     \textcolor{comment}{! call cpu\_clock\_begin(id\_clock\_avg)}}
\DoxyCodeLine{998       dt\_wt = dt\_rem / dt ; dt\_rem = 0.0}
\DoxyCodeLine{999       \textcolor{keywordflow}{do} k=1,nzc+1}
\DoxyCodeLine{1000         kappa\_mid(k) = 0.0}
\DoxyCodeLine{1001         \textcolor{comment}{! This would be here but does nothing.}}
\DoxyCodeLine{1002         \textcolor{comment}{! kappa\_avg(K) = kappa\_avg(K) + kappa\_mid(K)*dt\_wt}}
\DoxyCodeLine{1003         tke\_avg(k) = tke\_avg(k) + dt\_wt*tke(k)}
\DoxyCodeLine{1004 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1005     \textcolor{comment}{! call cpu\_clock\_end(id\_clock\_avg)}}
\DoxyCodeLine{1006     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1007     \textcolor{comment}{! call cpu\_clock\_begin(id\_clock\_project)}}
\DoxyCodeLine{1008       \textcolor{keyword}{call }calculate\_projected\_state(kappa\_out, u, v, t, sal, dt\_now, nzc, dz, i\_dz\_int, \&}
\DoxyCodeLine{1009                                      dbuoy\_dt, dbuoy\_ds, u\_test, v\_test, t\_test, s\_test, \&}
\DoxyCodeLine{1010                                      gv, us, n2=n2, s2=s2, ks\_int=ks\_kappa, ke\_int=ke\_kappa, \&}
\DoxyCodeLine{1011                                      vel\_underflow=cs\%vel\_underflow)}
\DoxyCodeLine{1012     \textcolor{comment}{! call cpu\_clock\_end(id\_clock\_project)}}
\DoxyCodeLine{1013 }
\DoxyCodeLine{1014     \textcolor{comment}{! call cpu\_clock\_begin(id\_clock\_KQ)}}
\DoxyCodeLine{1015       \textcolor{keywordflow}{do} k=1,nzc+1 ; k\_q\_tmp(k) = k\_q(k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1016       \textcolor{keyword}{call }find\_kappa\_tke(n2, s2, kappa\_out, idz, dz\_int, i\_l2\_bdry, f2, \&}
\DoxyCodeLine{1017                           nzc, cs, gv, us, k\_q\_tmp, tke\_pred, kappa\_pred)}
\DoxyCodeLine{1018     \textcolor{comment}{! call cpu\_clock\_end(id\_clock\_KQ)}}
\DoxyCodeLine{1019 }
\DoxyCodeLine{1020       ks\_kappa = gv\%ke+1 ; ke\_kappa = 0}
\DoxyCodeLine{1021       \textcolor{keywordflow}{do} k=1,nzc+1}
\DoxyCodeLine{1022         kappa\_mid(k) = 0.5*(kappa\_out(k) + kappa\_pred(k))}
\DoxyCodeLine{1023         \textcolor{keywordflow}{if} ((kappa\_mid(k) > 0.0) .and. (k<ks\_kappa)) ks\_kappa = k}
\DoxyCodeLine{1024         \textcolor{keywordflow}{if} (kappa\_mid(k) > 0.0) ke\_kappa = k}
\DoxyCodeLine{1025 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1026 }
\DoxyCodeLine{1027     \textcolor{comment}{! call cpu\_clock\_begin(id\_clock\_project)}}
\DoxyCodeLine{1028       \textcolor{keyword}{call }calculate\_projected\_state(kappa\_mid, u, v, t, sal, dt\_now, nzc, dz, i\_dz\_int, \&}
\DoxyCodeLine{1029                                      dbuoy\_dt, dbuoy\_ds, u\_test, v\_test, t\_test, s\_test, \&}
\DoxyCodeLine{1030                                      gv, us, n2=n2, s2=s2, ks\_int=ks\_kappa, ke\_int=ke\_kappa, \&}
\DoxyCodeLine{1031                                      vel\_underflow=cs\%vel\_underflow)}
\DoxyCodeLine{1032     \textcolor{comment}{! call cpu\_clock\_end(id\_clock\_project)}}
\DoxyCodeLine{1033 }
\DoxyCodeLine{1034     \textcolor{comment}{! call cpu\_clock\_begin(id\_clock\_KQ)}}
\DoxyCodeLine{1035       \textcolor{keyword}{call }find\_kappa\_tke(n2, s2, kappa\_out, idz, dz\_int, i\_l2\_bdry, f2, \&}
\DoxyCodeLine{1036                           nzc, cs, gv, us, k\_q, tke\_pred, kappa\_pred)}
\DoxyCodeLine{1037     \textcolor{comment}{! call cpu\_clock\_end(id\_clock\_KQ)}}
\DoxyCodeLine{1038 }
\DoxyCodeLine{1039     \textcolor{comment}{! call cpu\_clock\_begin(id\_clock\_avg)}}
\DoxyCodeLine{1040       dt\_wt = dt\_now / dt ; dt\_rem = dt\_rem - dt\_now}
\DoxyCodeLine{1041       \textcolor{keywordflow}{do} k=1,nzc+1}
\DoxyCodeLine{1042         kappa\_mid(k) = 0.5*(kappa\_out(k) + kappa\_pred(k))}
\DoxyCodeLine{1043         kappa\_avg(k) = kappa\_avg(k) + kappa\_mid(k)*dt\_wt}
\DoxyCodeLine{1044         tke\_avg(k) = tke\_avg(k) + dt\_wt*0.5*(tke\_pred(k) + tke(k))}
\DoxyCodeLine{1045         kappa(k) = kappa\_pred(k) \textcolor{comment}{! First guess for the next iteration.}}
\DoxyCodeLine{1046 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{1047     \textcolor{comment}{! call cpu\_clock\_end(id\_clock\_avg)}}
\DoxyCodeLine{1048 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1049 }
\DoxyCodeLine{1050     \textcolor{keywordflow}{if} (dt\_rem > 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1051       \textcolor{comment}{! Update the values of u, v, T, Sal, N2, and S2 for the next iteration.}}
\DoxyCodeLine{1052     \textcolor{comment}{! call cpu\_clock\_begin(id\_clock\_project)}}
\DoxyCodeLine{1053       \textcolor{keyword}{call }calculate\_projected\_state(kappa\_mid, u, v, t, sal, dt\_now, nzc, \&}
\DoxyCodeLine{1054                                      dz, i\_dz\_int, dbuoy\_dt, dbuoy\_ds, u, v, t, sal, \&}
\DoxyCodeLine{1055                                      gv, us, n2, s2, vel\_underflow=cs\%vel\_underflow)}
\DoxyCodeLine{1056     \textcolor{comment}{! call cpu\_clock\_end(id\_clock\_project)}}
\DoxyCodeLine{1057 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1058 }
\DoxyCodeLine{1059     \textcolor{keywordflow}{if} (dt\_rem <= 0.0) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{1060 }
\DoxyCodeLine{1061 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! end itt loop}}
\DoxyCodeLine{1062 }
\DoxyCodeLine{1063   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(i\_ld2\_1d)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1064     \textcolor{keywordflow}{do} k=1,gv\%ke+1 ; i\_ld2\_1d(k) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1065     \textcolor{keywordflow}{do} k=2,nzc ; \textcolor{keywordflow}{if} (tke(k) > 0.0) \&}
\DoxyCodeLine{1066       i\_ld2\_1d(k) = i\_l2\_bdry(k) + (n2(k) / cs\%lambda**2 + f2) / tke(k)}
\DoxyCodeLine{1067 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{1068 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1069   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(dz\_int\_1d)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1070     \textcolor{keywordflow}{do} k=1,nzc+1 ; dz\_int\_1d(k) = dz\_int(k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1071     \textcolor{keywordflow}{do} k=nzc+2,gv\%ke ; dz\_int\_1d(k) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1072 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1073 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__kappa__shear_a82428168ba463cf7276ae96d3e32475c}\label{namespacemom__kappa__shear_a82428168ba463cf7276ae96d3e32475c}} 
\index{mom\_kappa\_shear@{mom\_kappa\_shear}!kappa\_shear\_init@{kappa\_shear\_init}}
\index{kappa\_shear\_init@{kappa\_shear\_init}!mom\_kappa\_shear@{mom\_kappa\_shear}}
\subsubsection{\texorpdfstring{kappa\_shear\_init()}{kappa\_shear\_init()}}
{\footnotesize\ttfamily logical function, public mom\+\_\+kappa\+\_\+shear\+::kappa\+\_\+shear\+\_\+init (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), intent(in)}]{Time,  }\item[{type(ocean\+\_\+grid\+\_\+type), intent(in)}]{G,  }\item[{type(verticalgrid\+\_\+type), intent(in)}]{GV,  }\item[{type(unit\+\_\+scale\+\_\+type), intent(in)}]{US,  }\item[{type(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file,  }\item[{type(diag\+\_\+ctrl), intent(inout), target}]{diag,  }\item[{type(\mbox{\hyperlink{structmom__kappa__shear_1_1kappa__shear__cs}{kappa\+\_\+shear\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



This subroutine initializes the parameters that regulate shear-\/driven mixing. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em time} & The current model time. \\
\hline
\mbox{\texttt{ in}}  & {\em g} & The ocean\textquotesingle{}s grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & The ocean\textquotesingle{}s vertical grid structure. \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em param\+\_\+file} & A structure to parse for run-\/time parameters. \\
\hline
\mbox{\texttt{ in,out}}  & {\em diag} & A structure that is used to regulate diagnostic output. \\
\hline
 & {\em cs} & A pointer that is set to point to the control structure for this module \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
True if module is to be used, False otherwise 
\end{DoxyReturn}


Definition at line 1773 of file M\+O\+M\+\_\+kappa\+\_\+shear.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1773   \textcolor{keywordtype}{type}(time\_type),         \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{ !< The current model time.}}
\DoxyCodeLine{1774   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{    !< The ocean's grid structure.}}
\DoxyCodeLine{1775   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{   !< The ocean's vertical grid structure.}}
\DoxyCodeLine{1776   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{   !< A dimensional unit scaling type}}
\DoxyCodeLine{1777   \textcolor{keywordtype}{type}(param\_file\_type),   \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< A structure to parse for run-time}}
\DoxyCodeLine{1778 \textcolor{comment}{                                                 !! parameters.}}
\DoxyCodeLine{1779   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{ !< A structure that is used to regulate diagnostic}}
\DoxyCodeLine{1780 \textcolor{comment}{                                                 !! output.}}
\DoxyCodeLine{1781   \textcolor{keywordtype}{type}(Kappa\_shear\_CS),    \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{   !< A pointer that is set to point to the control}}
\DoxyCodeLine{1782 \textcolor{comment}{                                                 !! structure for this module}}
\DoxyCodeLine{1783   \textcolor{keywordtype}{logical} :: kappa\_shear\_init\textcolor{comment}{ !< True if module is to be used, False otherwise}}
\DoxyCodeLine{1784 }
\DoxyCodeLine{1785   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1786   \textcolor{keywordtype}{logical} :: merge\_mixedlayer}
\DoxyCodeLine{1787   \textcolor{keywordtype}{logical} :: debug\_shear}
\DoxyCodeLine{1788   \textcolor{keywordtype}{logical} :: just\_read \textcolor{comment}{! If true, this module is not used, so only read the parameters.}}
\DoxyCodeLine{1789   \textcolor{comment}{! This include declares and sets the variable "version".}}
\DoxyCodeLine{1790 \textcolor{preprocessor}{\# include "version\_variable.h"}}
\DoxyCodeLine{1791 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_kappa\_shear"}  \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{1792 \textcolor{keywordtype}{  real} :: kappa\_0\_unscaled  \textcolor{comment}{! The value of kappa\_0 in MKS units [m2 s-1]}}
\DoxyCodeLine{1793 \textcolor{keywordtype}{  real} :: KD\_normal \textcolor{comment}{! The KD of the main model, read here only as a parameter}}
\DoxyCodeLine{1794                     \textcolor{comment}{! for setting the default of KD\_SMOOTH in MKS units [m2 s-1]}}
\DoxyCodeLine{1795 }
\DoxyCodeLine{1796   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1797     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"kappa\_shear\_init called with an associated "}// \&}
\DoxyCodeLine{1798                             \textcolor{stringliteral}{"control structure."})}
\DoxyCodeLine{1799     \textcolor{keywordflow}{return}}
\DoxyCodeLine{1800 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1801   \textcolor{keyword}{allocate}(cs)}
\DoxyCodeLine{1802 }
\DoxyCodeLine{1803   \textcolor{comment}{!   The Jackson-Hallberg-Legg shear mixing parameterization uses the following}}
\DoxyCodeLine{1804   \textcolor{comment}{! 6 nondimensional coefficients.  That paper gives 3 best fit parameter sets.}}
\DoxyCodeLine{1805   \textcolor{comment}{!    Ri\_Crit  Rate    FRi\_Curv  K\_buoy  TKE\_N  TKE\_Shear}}
\DoxyCodeLine{1806   \textcolor{comment}{! p1: 0.25    0.089    -0.97     0.82    0.24    0.14}}
\DoxyCodeLine{1807   \textcolor{comment}{! p2: 0.30    0.085    -0.94     0.86    0.26    0.13}}
\DoxyCodeLine{1808   \textcolor{comment}{! p3: 0.35    0.088    -0.89     0.81    0.28    0.12}}
\DoxyCodeLine{1809   \textcolor{comment}{!   Future research will reveal how these should be modified to take}}
\DoxyCodeLine{1810   \textcolor{comment}{! subgridscale inhomogeneity into account.}}
\DoxyCodeLine{1811 }
\DoxyCodeLine{1812 \textcolor{comment}{! Set default, read and log parameters}}
\DoxyCodeLine{1813   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_JACKSON\_PARAM"}, kappa\_shear\_init, default=.false., do\_not\_log=.true.)}
\DoxyCodeLine{1814   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \&}
\DoxyCodeLine{1815     \textcolor{stringliteral}{"Parameterization of shear-driven turbulence following Jackson, Hallberg and Legg, JPO 2008"}, \&}
\DoxyCodeLine{1816     log\_to\_all=.true., debugging=kappa\_shear\_init, all\_default=.not.kappa\_shear\_init)}
\DoxyCodeLine{1817   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_JACKSON\_PARAM"}, kappa\_shear\_init, \&}
\DoxyCodeLine{1818                  \textcolor{stringliteral}{"If true, use the Jackson-Hallberg-Legg (JPO 2008) "}//\&}
\DoxyCodeLine{1819                  \textcolor{stringliteral}{"shear mixing parameterization."}, default=.false.)}
\DoxyCodeLine{1820   just\_read = .not.kappa\_shear\_init}
\DoxyCodeLine{1821   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"VERTEX\_SHEAR"}, cs\%KS\_at\_vertex, \&}
\DoxyCodeLine{1822                  \textcolor{stringliteral}{"If true, do the calculations of the shear-driven mixing "}//\&}
\DoxyCodeLine{1823                  \textcolor{stringliteral}{"at the cell vertices (i.e., the vorticity points)."}, \&}
\DoxyCodeLine{1824                  default=.false., do\_not\_log=just\_read)}
\DoxyCodeLine{1825   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"RINO\_CRIT"}, cs\%RiNo\_crit, \&}
\DoxyCodeLine{1826                  \textcolor{stringliteral}{"The critical Richardson number for shear mixing."}, \&}
\DoxyCodeLine{1827                  units=\textcolor{stringliteral}{"nondim"}, default=0.25, do\_not\_log=just\_read)}
\DoxyCodeLine{1828   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"SHEARMIX\_RATE"}, cs\%Shearmix\_rate, \&}
\DoxyCodeLine{1829                  \textcolor{stringliteral}{"A nondimensional rate scale for shear-driven entrainment. "}//\&}
\DoxyCodeLine{1830                  \textcolor{stringliteral}{"Jackson et al find values in the range of 0.085-0.089."}, \&}
\DoxyCodeLine{1831                  units=\textcolor{stringliteral}{"nondim"}, default=0.089, do\_not\_log=just\_read)}
\DoxyCodeLine{1832   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MAX\_RINO\_IT"}, cs\%max\_RiNo\_it, \&}
\DoxyCodeLine{1833                  \textcolor{stringliteral}{"The maximum number of iterations that may be used to "}//\&}
\DoxyCodeLine{1834                  \textcolor{stringliteral}{"estimate the Richardson number driven mixing."}, \&}
\DoxyCodeLine{1835                  units=\textcolor{stringliteral}{"nondim"}, default=50, do\_not\_log=just\_read)}
\DoxyCodeLine{1836   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KD"}, kd\_normal, default=0.0, do\_not\_log=.true.)}
\DoxyCodeLine{1837   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KD\_KAPPA\_SHEAR\_0"}, cs\%kappa\_0, \&}
\DoxyCodeLine{1838                  \textcolor{stringliteral}{"The background diffusivity that is used to smooth the "}//\&}
\DoxyCodeLine{1839                  \textcolor{stringliteral}{"density and shear profiles before solving for the "}//\&}
\DoxyCodeLine{1840                  \textcolor{stringliteral}{"diffusivities.  The default is the greater of KD and 1e-7 m2 s-1."}, \&}
\DoxyCodeLine{1841                  units=\textcolor{stringliteral}{"m2 s-1"}, default=max(kd\_normal, 1.0e-7), scale=us\%m2\_s\_to\_Z2\_T, \&}
\DoxyCodeLine{1842                  unscaled=kappa\_0\_unscaled, do\_not\_log=just\_read)}
\DoxyCodeLine{1843   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KD\_TRUNC\_KAPPA\_SHEAR"}, cs\%kappa\_trunc, \&}
\DoxyCodeLine{1844                  \textcolor{stringliteral}{"The value of shear-driven diffusivity that is considered negligible "}//\&}
\DoxyCodeLine{1845                  \textcolor{stringliteral}{"and is rounded down to 0. The default is 1\% of KD\_KAPPA\_SHEAR\_0."}, \&}
\DoxyCodeLine{1846                  units=\textcolor{stringliteral}{"m2 s-1"}, default=0.01*kappa\_0\_unscaled, scale=us\%m2\_s\_to\_Z2\_T, do\_not\_log=just\_read)}
\DoxyCodeLine{1847   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"FRI\_CURVATURE"}, cs\%FRi\_curvature, \&}
\DoxyCodeLine{1848                  \textcolor{stringliteral}{"The nondimensional curvature of the function of the "}//\&}
\DoxyCodeLine{1849                  \textcolor{stringliteral}{"Richardson number in the kappa source term in the "}//\&}
\DoxyCodeLine{1850                  \textcolor{stringliteral}{"Jackson et al. scheme."}, units=\textcolor{stringliteral}{"nondim"}, default=-0.97, do\_not\_log=just\_read)}
\DoxyCodeLine{1851   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"TKE\_N\_DECAY\_CONST"}, cs\%C\_N, \&}
\DoxyCodeLine{1852                  \textcolor{stringliteral}{"The coefficient for the decay of TKE due to "}//\&}
\DoxyCodeLine{1853                  \textcolor{stringliteral}{"stratification (i.e. proportional to N*tke). "}//\&}
\DoxyCodeLine{1854                  \textcolor{stringliteral}{"The values found by Jackson et al. are 0.24-0.28."}, \&}
\DoxyCodeLine{1855                  units=\textcolor{stringliteral}{"nondim"}, default=0.24, do\_not\_log=just\_read)}
\DoxyCodeLine{1856 \textcolor{comment}{!  call get\_param(param\_file, mdl, "LAYER\_KAPPA\_STAGGER", CS\%layer\_stagger, \&}}
\DoxyCodeLine{1857 \textcolor{comment}{!                 default=.false., do\_not\_log=just\_read)}}
\DoxyCodeLine{1858   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"TKE\_SHEAR\_DECAY\_CONST"}, cs\%C\_S, \&}
\DoxyCodeLine{1859                  \textcolor{stringliteral}{"The coefficient for the decay of TKE due to shear (i.e. "}//\&}
\DoxyCodeLine{1860                  \textcolor{stringliteral}{"proportional to |S|*tke). The values found by Jackson "}//\&}
\DoxyCodeLine{1861                  \textcolor{stringliteral}{"et al. are 0.14-0.12."}, units=\textcolor{stringliteral}{"nondim"}, default=0.14, do\_not\_log=just\_read)}
\DoxyCodeLine{1862   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KAPPA\_BUOY\_SCALE\_COEF"}, cs\%lambda, \&}
\DoxyCodeLine{1863                  \textcolor{stringliteral}{"The coefficient for the buoyancy length scale in the "}//\&}
\DoxyCodeLine{1864                  \textcolor{stringliteral}{"kappa equation.  The values found by Jackson et al. are "}//\&}
\DoxyCodeLine{1865                  \textcolor{stringliteral}{"in the range of 0.81-0.86."}, units=\textcolor{stringliteral}{"nondim"}, default=0.82, do\_not\_log=just\_read)}
\DoxyCodeLine{1866   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KAPPA\_N\_OVER\_S\_SCALE\_COEF2"}, cs\%lambda2\_N\_S, \&}
\DoxyCodeLine{1867                  \textcolor{stringliteral}{"The square of the ratio of the coefficients of the "}//\&}
\DoxyCodeLine{1868                  \textcolor{stringliteral}{"buoyancy and shear scales in the diffusivity equation, "}//\&}
\DoxyCodeLine{1869                  \textcolor{stringliteral}{"Set this to 0 (the default) to eliminate the shear scale. "}//\&}
\DoxyCodeLine{1870                  \textcolor{stringliteral}{"This is only used if USE\_JACKSON\_PARAM is true."}, \&}
\DoxyCodeLine{1871                  units=\textcolor{stringliteral}{"nondim"}, default=0.0, do\_not\_log=just\_read)}
\DoxyCodeLine{1872   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KAPPA\_SHEAR\_TOL\_ERR"}, cs\%kappa\_tol\_err, \&}
\DoxyCodeLine{1873                  \textcolor{stringliteral}{"The fractional error in kappa that is tolerated. "}//\&}
\DoxyCodeLine{1874                  \textcolor{stringliteral}{"Iteration stops when changes between subsequent "}//\&}
\DoxyCodeLine{1875                  \textcolor{stringliteral}{"iterations are smaller than this everywhere in a "}//\&}
\DoxyCodeLine{1876                  \textcolor{stringliteral}{"column.  The peak diffusivities usually converge most "}//\&}
\DoxyCodeLine{1877                  \textcolor{stringliteral}{"rapidly, and have much smaller errors than this."}, \&}
\DoxyCodeLine{1878                  units=\textcolor{stringliteral}{"nondim"}, default=0.1, do\_not\_log=just\_read)}
\DoxyCodeLine{1879   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"TKE\_BACKGROUND"}, cs\%TKE\_bg, \&}
\DoxyCodeLine{1880                  \textcolor{stringliteral}{"A background level of TKE used in the first iteration "}//\&}
\DoxyCodeLine{1881                  \textcolor{stringliteral}{"of the kappa equation.  TKE\_BACKGROUND could be 0."}, \&}
\DoxyCodeLine{1882                  units=\textcolor{stringliteral}{"m2 s-2"}, default=0.0, scale=us\%m\_to\_Z**2*us\%T\_to\_s**2)}
\DoxyCodeLine{1883   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KAPPA\_SHEAR\_ELIM\_MASSLESS"}, cs\%eliminate\_massless, \&}
\DoxyCodeLine{1884                  \textcolor{stringliteral}{"If true, massless layers are merged with neighboring "}//\&}
\DoxyCodeLine{1885                  \textcolor{stringliteral}{"massive layers in this calculation.  The default is "}//\&}
\DoxyCodeLine{1886                  \textcolor{stringliteral}{"true and I can think of no good reason why it should "}//\&}
\DoxyCodeLine{1887                  \textcolor{stringliteral}{"be false. This is only used if USE\_JACKSON\_PARAM is true."}, \&}
\DoxyCodeLine{1888                  default=.true., do\_not\_log=just\_read)}
\DoxyCodeLine{1889   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MAX\_KAPPA\_SHEAR\_IT"}, cs\%max\_KS\_it, \&}
\DoxyCodeLine{1890                  \textcolor{stringliteral}{"The maximum number of iterations that may be used to "}//\&}
\DoxyCodeLine{1891                  \textcolor{stringliteral}{"estimate the time-averaged diffusivity."}, units=\textcolor{stringliteral}{"nondim"}, \&}
\DoxyCodeLine{1892                  default=13, do\_not\_log=just\_read)}
\DoxyCodeLine{1893   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"PRANDTL\_TURB"}, cs\%Prandtl\_turb, \&}
\DoxyCodeLine{1894                  \textcolor{stringliteral}{"The turbulent Prandtl number applied to shear instability."}, \&}
\DoxyCodeLine{1895                  units=\textcolor{stringliteral}{"nondim"}, default=1.0, do\_not\_log=.true.)}
\DoxyCodeLine{1896   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"VEL\_UNDERFLOW"}, cs\%vel\_underflow, \&}
\DoxyCodeLine{1897                  \textcolor{stringliteral}{"A negligibly small velocity magnitude below which velocity components are set "}//\&}
\DoxyCodeLine{1898                  \textcolor{stringliteral}{"to 0.  A reasonable value might be 1e-30 m/s, which is less than an "}//\&}
\DoxyCodeLine{1899                  \textcolor{stringliteral}{"Angstrom divided by the age of the universe."}, \&}
\DoxyCodeLine{1900                  units=\textcolor{stringliteral}{"m s-1"}, default=0.0, scale=us\%m\_s\_to\_L\_T, do\_not\_log=just\_read)}
\DoxyCodeLine{1901   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KAPPA\_SHEAR\_MAX\_KAP\_SRC\_CHG"}, cs\%kappa\_src\_max\_chg, \&}
\DoxyCodeLine{1902                  \textcolor{stringliteral}{"The maximum permitted increase in the kappa source within an iteration relative "}//\&}
\DoxyCodeLine{1903                  \textcolor{stringliteral}{"to the local source; this must be greater than 1.  The lower limit for the "}//\&}
\DoxyCodeLine{1904                  \textcolor{stringliteral}{"permitted fractional decrease is (1 - 0.5/kappa\_src\_max\_chg).  These limits "}//\&}
\DoxyCodeLine{1905                  \textcolor{stringliteral}{"could perhaps be made dynamic with an improved iterative solver."}, \&}
\DoxyCodeLine{1906                  default=10.0, units=\textcolor{stringliteral}{"nondim"}, do\_not\_log=just\_read)}
\DoxyCodeLine{1907 }
\DoxyCodeLine{1908   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEBUG"}, cs\%debug, \&}
\DoxyCodeLine{1909                  \textcolor{stringliteral}{"If true, write out verbose debugging data."}, \&}
\DoxyCodeLine{1910                  default=.false., debuggingparam=.true., do\_not\_log=just\_read)}
\DoxyCodeLine{1911   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEBUG\_KAPPA\_SHEAR"}, debug\_shear, \&}
\DoxyCodeLine{1912                  \textcolor{stringliteral}{"If true, write debugging data for the kappa-shear code."}, \&}
\DoxyCodeLine{1913                  default=.false., debuggingparam=.true., do\_not\_log=.true.)}
\DoxyCodeLine{1914   \textcolor{keywordflow}{if} (debug\_shear) cs\%debug = .true.}
\DoxyCodeLine{1915   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KAPPA\_SHEAR\_VERTEX\_PSURF\_BUG"}, cs\%psurf\_bug, \&}
\DoxyCodeLine{1916                  \textcolor{stringliteral}{"If true, do a simple average of the cell surface pressures to get a pressure "}//\&}
\DoxyCodeLine{1917                  \textcolor{stringliteral}{"at the corner if VERTEX\_SHEAR=True.  Otherwise mask out any land points in "}//\&}
\DoxyCodeLine{1918                  \textcolor{stringliteral}{"the average."}, default=.true., do\_not\_log=(just\_read .or. (.not.cs\%KS\_at\_vertex)))}
\DoxyCodeLine{1919 }
\DoxyCodeLine{1920   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KAPPA\_SHEAR\_ITER\_BUG"}, cs\%dKdQ\_iteration\_bug, \&}
\DoxyCodeLine{1921                  \textcolor{stringliteral}{"If true, use an older, dimensionally inconsistent estimate of the "}//\&}
\DoxyCodeLine{1922                  \textcolor{stringliteral}{"derivative of diffusivity with energy in the Newton's method iteration.  "}//\&}
\DoxyCodeLine{1923                  \textcolor{stringliteral}{"The bug causes undercorrections when dz > 1 m."}, default=.false., do\_not\_log=just\_read)}
\DoxyCodeLine{1924   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KAPPA\_SHEAR\_ALL\_LAYER\_TKE\_BUG"}, cs\%all\_layer\_TKE\_bug, \&}
\DoxyCodeLine{1925                  \textcolor{stringliteral}{"If true, report back the latest estimate of TKE instead of the time average "}//\&}
\DoxyCodeLine{1926                  \textcolor{stringliteral}{"TKE when there is mass in all layers.  Otherwise always report the time "}//\&}
\DoxyCodeLine{1927                  \textcolor{stringliteral}{"averaged TKE, as is currently done when there are some massless layers."}, \&}
\DoxyCodeLine{1928                  default=.false., do\_not\_log=just\_read)}
\DoxyCodeLine{1929   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_RESTRICTIVE\_TOLERANCE\_CHECK"}, cs\%restrictive\_tolerance\_check, \&}
\DoxyCodeLine{1930                  \textcolor{stringliteral}{"If true, uses the more restrictive tolerance check to determine if a timestep "}//\&}
\DoxyCodeLine{1931                  \textcolor{stringliteral}{"is acceptable for the KS\_it outer iteration loop.  False uses the original less "}//\&}
\DoxyCodeLine{1932                  \textcolor{stringliteral}{"restrictive check."}, default=.false., do\_not\_log=just\_read)}
\DoxyCodeLine{1933 \textcolor{comment}{!    id\_clock\_KQ = cpu\_clock\_id('Ocean KS kappa\_shear', grain=CLOCK\_ROUTINE)}}
\DoxyCodeLine{1934 \textcolor{comment}{!    id\_clock\_avg = cpu\_clock\_id('Ocean KS avg', grain=CLOCK\_ROUTINE)}}
\DoxyCodeLine{1935 \textcolor{comment}{!    id\_clock\_project = cpu\_clock\_id('Ocean KS project', grain=CLOCK\_ROUTINE)}}
\DoxyCodeLine{1936 \textcolor{comment}{!    id\_clock\_setup = cpu\_clock\_id('Ocean KS setup', grain=CLOCK\_ROUTINE)}}
\DoxyCodeLine{1937 }
\DoxyCodeLine{1938   cs\%nkml = 1}
\DoxyCodeLine{1939   \textcolor{keywordflow}{if} (gv\%nkml>0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1940     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KAPPA\_SHEAR\_MERGE\_ML"},merge\_mixedlayer, \&}
\DoxyCodeLine{1941                  \textcolor{stringliteral}{"If true, combine the mixed layers together before solving the "}//\&}
\DoxyCodeLine{1942                  \textcolor{stringliteral}{"kappa-shear equations."}, default=.true., do\_not\_log=just\_read)}
\DoxyCodeLine{1943     \textcolor{keywordflow}{if} (merge\_mixedlayer) cs\%nkml = gv\%nkml}
\DoxyCodeLine{1944 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1945 }
\DoxyCodeLine{1946 \textcolor{comment}{! Forego remainder of initialization if not using this scheme}}
\DoxyCodeLine{1947   \textcolor{keywordflow}{if} (.not. kappa\_shear\_init) \textcolor{keywordflow}{return}}
\DoxyCodeLine{1948 }
\DoxyCodeLine{1949   cs\%diag => diag}
\DoxyCodeLine{1950 }
\DoxyCodeLine{1951   cs\%id\_Kd\_shear = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\textcolor{stringliteral}{'Kd\_shear'}, diag\%axesTi, time, \&}
\DoxyCodeLine{1952       \textcolor{stringliteral}{'Shear-driven Diapycnal Diffusivity'}, \textcolor{stringliteral}{'m2 s-1'}, conversion=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{1953   cs\%id\_TKE = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'},\textcolor{stringliteral}{'TKE\_shear'}, diag\%axesTi, time, \&}
\DoxyCodeLine{1954       \textcolor{stringliteral}{'Shear-driven Turbulent Kinetic Energy'}, \textcolor{stringliteral}{'m2 s-2'}, conversion=us\%Z\_to\_m**2*us\%s\_to\_T**2)}
\DoxyCodeLine{1955 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__kappa__shear_ac7859c609e462000ca8fd763d68d141e}\label{namespacemom__kappa__shear_ac7859c609e462000ca8fd763d68d141e}} 
\index{mom\_kappa\_shear@{mom\_kappa\_shear}!kappa\_shear\_is\_used@{kappa\_shear\_is\_used}}
\index{kappa\_shear\_is\_used@{kappa\_shear\_is\_used}!mom\_kappa\_shear@{mom\_kappa\_shear}}
\subsubsection{\texorpdfstring{kappa\_shear\_is\_used()}{kappa\_shear\_is\_used()}}
{\footnotesize\ttfamily logical function, public mom\+\_\+kappa\+\_\+shear\+::kappa\+\_\+shear\+\_\+is\+\_\+used (\begin{DoxyParamCaption}\item[{type(param\+\_\+file\+\_\+type), intent(in)}]{param\+\_\+file }\end{DoxyParamCaption})}



This function indicates to other modules whether the Jackson et al shear mixing parameterization will be used without needing to duplicate the log entry. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em param\+\_\+file} & A structure to parse for run-\/time parameters \\
\hline
\end{DoxyParams}


Definition at line 1961 of file M\+O\+M\+\_\+kappa\+\_\+shear.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1961   \textcolor{keywordtype}{type}(param\_file\_type), \textcolor{keywordtype}{intent(in)} :: param\_file\textcolor{comment}{ !< A structure to parse for run-time parameters}}
\DoxyCodeLine{1962 }
\DoxyCodeLine{1963   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1964   \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_kappa\_shear"}  \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{1965   \textcolor{comment}{! This function reads the parameter "USE\_JACKSON\_PARAM" and returns its value.}}
\DoxyCodeLine{1966 }
\DoxyCodeLine{1967   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"USE\_JACKSON\_PARAM"}, kappa\_shear\_is\_used, \&}
\DoxyCodeLine{1968                  default=.false., do\_not\_log=.true.)}

\end{DoxyCode}
