\hypertarget{namespacemom__vert__friction}{}\doxysection{mom\+\_\+vert\+\_\+friction Module Reference}
\label{namespacemom__vert__friction}\index{mom\_vert\_friction@{mom\_vert\_friction}}


\doxysubsection{Detailed Description}
Implements vertical viscosity (vertvisc) 

\begin{DoxyAuthor}{Author}
Robert Hallberg 
\end{DoxyAuthor}
\begin{DoxyDate}{Date}
April 1994 -\/ October 2006
\end{DoxyDate}
The vertical diffusion of momentum is fully implicit. This is necessary to allow for vanishingly small layers. The coupling is based on the distance between the centers of adjacent layers, except where a layer is close to the bottom compared with a bottom boundary layer thickness when a bottom drag law is used. A stress top b.\+c. and a no slip bottom b.\+c. are used. There is no limit on the time step for vertvisc.

Near the bottom, the horizontal thickness interpolation scheme changes to an upwind biased estimate to control the effect of spurious Montgomery potential gradients at the bottom where nearly massless layers layers ride over the topography. Within a few boundary layer depths of the bottom, the harmonic mean thickness (i.\+e. (2 h+ h-\/) / (h+ + h-\/) ) is used if the velocity is from the thinner side and the arithmetic mean thickness (i.\+e. (h+ + h-\/)/2) is used if the velocity is from the thicker side. Both of these thickness estimates are second order accurate. Above this the arithmetic mean thickness is used.

In addition, vertvisc truncates any velocity component that exceeds maxvel to truncvel. This basically keeps instabilities spatially localized. The number of times the velocity is truncated is reported each time the energies are saved, and if exceeds CSMaxtrunc the model will stop itself and change the time to a large value. This has proven very useful in (1) diagnosing model failures and (2) letting the model settle down to a meaningful integration from a poorly specified initial condition.

The same code is used for the two velocity components, by indirectly referencing the velocities and defining a handful of direction-\/specific defined variables.

Macros written all in capital letters are defined in \mbox{\hyperlink{MOM__memory_8h}{M\+O\+M\+\_\+memory.\+h}}.

A small fragment of the grid is shown below\+: \begin{DoxyVerb}    j+1  x ^ x ^ x   At x:  q
    j+1  > o > o >   At ^:  v, frhatv, tauy
    j    x ^ x ^ x   At >:  u, frhatu, taux
    j    > o > o >   At o:  h
    j-1  x ^ x ^ x
        i-1  i  i+1  At x & ^:
           i  i+1    At > & o:\end{DoxyVerb}


The boundaries always run through q grid points (x). \doxysubsection*{Data Types}
\begin{DoxyCompactItemize}
\item 
type \mbox{\hyperlink{structmom__vert__friction_1_1vertvisc__cs}{vertvisc\+\_\+cs}}
\begin{DoxyCompactList}\small\item\em The control structure with parameters and memory for the M\+O\+M\+\_\+vert\+\_\+friction module. \end{DoxyCompactList}\end{DoxyCompactItemize}
\doxysubsection*{Functions/\+Subroutines}
\begin{DoxyCompactItemize}
\item 
subroutine, public \mbox{\hyperlink{namespacemom__vert__friction_a8f1a390fa24fbe985068fed9ac26873c}{vertvisc}} (u, v, h, forces, visc, dt, O\+BC, A\+Dp, C\+Dp, G, GV, US, CS, taux\+\_\+bot, tauy\+\_\+bot, Waves)
\begin{DoxyCompactList}\small\item\em Perform a fully implicit vertical diffusion of momentum. Stress top and bottom boundary conditions are used. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__vert__friction_ad0b7bdc6695ee0c4207797bc94672863}{vertvisc\+\_\+remnant}} (visc, visc\+\_\+rem\+\_\+u, visc\+\_\+rem\+\_\+v, dt, G, GV, US, CS)
\begin{DoxyCompactList}\small\item\em Calculate the fraction of momentum originally in a layer that remains after a time-\/step of viscosity, and the fraction of a time-\/step\textquotesingle{}s worth of barotropic acceleration that a layer experiences after viscosity is applied. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__vert__friction_ac281f6595593b33436594112785e982b}{vertvisc\+\_\+coef}} (u, v, h, forces, visc, dt, G, GV, US, CS, O\+BC)
\begin{DoxyCompactList}\small\item\em Calculate the coupling coefficients (CSa\+\_\+u and CSa\+\_\+v) and effective layer thicknesses (CSh\+\_\+u and CSh\+\_\+v) for later use in the applying the implicit vertical viscosity via \mbox{\hyperlink{namespacemom__vert__friction_a8f1a390fa24fbe985068fed9ac26873c}{vertvisc()}}. \end{DoxyCompactList}\item 
subroutine \mbox{\hyperlink{namespacemom__vert__friction_aa9e6f1f0d75a54d85b0d0cdad874b41f}{find\+\_\+coupling\+\_\+coef}} (a\+\_\+cpl, hvel, do\+\_\+i, h\+\_\+harm, bbl\+\_\+thick, kv\+\_\+bbl, z\+\_\+i, h\+\_\+ml, dt, j, G, GV, US, CS, visc, forces, work\+\_\+on\+\_\+u, O\+BC, shelf)
\begin{DoxyCompactList}\small\item\em Calculate the \textquotesingle{}coupling coefficient\textquotesingle{} (a\+\_\+cpl) at the interfaces. If B\+O\+T\+T\+O\+M\+D\+R\+A\+G\+L\+AW is defined, the minimum of Hbbl and half the adjacent layer thicknesses are used to calculate a\+\_\+cpl near the bottom. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__vert__friction_ac14bc439f3b7ae3000fa9883c95ebfe2}{vertvisc\+\_\+limit\+\_\+vel}} (u, v, h, A\+Dp, C\+Dp, forces, visc, dt, G, GV, US, CS)
\begin{DoxyCompactList}\small\item\em Velocity components which exceed a threshold for physically reasonable values are truncated. Optionally, any column with excessive velocities may be sent to a diagnostic reporting subroutine. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__vert__friction_a4bd5c8772584e41890bff55ccd52507c}{vertvisc\+\_\+init}} (M\+IS, Time, G, GV, US, param\+\_\+file, diag, A\+Dp, dirs, ntrunc, CS)
\begin{DoxyCompactList}\small\item\em Initialize the vertical friction module. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__vert__friction_a62e586a80ed4bdd3fd27ab62ca4c054f}{updatecfltruncationvalue}} (Time, CS, activate)
\begin{DoxyCompactList}\small\item\em Update the C\+FL truncation value as a function of time. If called with the optional argument activate=.true., record the value of Time as the beginning of the ramp period. \end{DoxyCompactList}\item 
subroutine, public \mbox{\hyperlink{namespacemom__vert__friction_a158d29cf5c1d79ca7c5f327706855972}{vertvisc\+\_\+end}} (CS)
\begin{DoxyCompactList}\small\item\em Clean up and deallocate the vertical friction module. \end{DoxyCompactList}\end{DoxyCompactItemize}


\doxysubsection{Function/\+Subroutine Documentation}
\mbox{\Hypertarget{namespacemom__vert__friction_aa9e6f1f0d75a54d85b0d0cdad874b41f}\label{namespacemom__vert__friction_aa9e6f1f0d75a54d85b0d0cdad874b41f}} 
\index{mom\_vert\_friction@{mom\_vert\_friction}!find\_coupling\_coef@{find\_coupling\_coef}}
\index{find\_coupling\_coef@{find\_coupling\_coef}!mom\_vert\_friction@{mom\_vert\_friction}}
\doxysubsubsection{\texorpdfstring{find\_coupling\_coef()}{find\_coupling\_coef()}}
{\footnotesize\ttfamily subroutine mom\+\_\+vert\+\_\+friction\+::find\+\_\+coupling\+\_\+coef (\begin{DoxyParamCaption}\item[{real, dimension(szib\+\_\+(g),szk\+\_\+(gv)+1), intent(out)}]{a\+\_\+cpl,  }\item[{real, dimension(szib\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{hvel,  }\item[{logical, dimension(szib\+\_\+(g)), intent(in)}]{do\+\_\+i,  }\item[{real, dimension(szib\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{h\+\_\+harm,  }\item[{real, dimension(szib\+\_\+(g)), intent(in)}]{bbl\+\_\+thick,  }\item[{real, dimension(szib\+\_\+(g)), intent(in)}]{kv\+\_\+bbl,  }\item[{real, dimension(szib\+\_\+(g),szk\+\_\+(gv)+1), intent(in)}]{z\+\_\+i,  }\item[{real, dimension(szib\+\_\+(g)), intent(out)}]{h\+\_\+ml,  }\item[{real, intent(in)}]{dt,  }\item[{integer, intent(in)}]{j,  }\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__vert__friction_1_1vertvisc__cs}{vertvisc\+\_\+cs}}), pointer}]{CS,  }\item[{type(vertvisc\+\_\+type), intent(in)}]{visc,  }\item[{type(mech\+\_\+forcing), intent(in)}]{forces,  }\item[{logical, intent(in)}]{work\+\_\+on\+\_\+u,  }\item[{type(ocean\+\_\+obc\+\_\+type), pointer}]{O\+BC,  }\item[{logical, intent(in), optional}]{shelf }\end{DoxyParamCaption})\hspace{0.3cm}{\ttfamily [private]}}



Calculate the \textquotesingle{}coupling coefficient\textquotesingle{} (a\+\_\+cpl) at the interfaces. If B\+O\+T\+T\+O\+M\+D\+R\+A\+G\+L\+AW is defined, the minimum of Hbbl and half the adjacent layer thicknesses are used to calculate a\+\_\+cpl near the bottom. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & Ocean grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & Ocean vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ out}}  & {\em a\+\_\+cpl} & Coupling coefficient across interfaces \mbox{[}Z T-\/1 $\sim$$>$ m s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em hvel} & Thickness at velocity points \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em do\+\_\+i} & If true, determine coupling coefficient for a column \\
\hline
\mbox{\texttt{ in}}  & {\em h\+\_\+harm} & Harmonic mean of thicknesses around a velocity \\
\hline
\mbox{\texttt{ in}}  & {\em bbl\+\_\+thick} & Bottom boundary layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em kv\+\_\+bbl} & Bottom boundary layer viscosity \mbox{[}Z2 T-\/1 $\sim$$>$ m2 s-\/1\mbox{]}. \\
\hline
\mbox{\texttt{ in}}  & {\em z\+\_\+i} & Estimate of interface heights above the bottom, \\
\hline
\mbox{\texttt{ out}}  & {\em h\+\_\+ml} & Mixed layer depth \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em j} & j-\/index to find coupling coefficient for \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
 & {\em cs} & Vertical viscosity control structure \\
\hline
\mbox{\texttt{ in}}  & {\em visc} & Structure containing viscosities and bottom drag \\
\hline
\mbox{\texttt{ in}}  & {\em forces} & A structure with the driving mechanical forces \\
\hline
\mbox{\texttt{ in}}  & {\em work\+\_\+on\+\_\+u} & If true, u-\/points are being calculated, otherwise they are v-\/points \\
\hline
 & {\em obc} & Open boundary condition structure \\
\hline
\mbox{\texttt{ in}}  & {\em shelf} & If present and true, use a surface boundary condition appropriate for an ice shelf. \\
\hline
\end{DoxyParams}


Definition at line 1092 of file M\+O\+M\+\_\+vert\+\_\+friction.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1092   \textcolor{keywordtype}{type}(ocean\_grid\_type),     \textcolor{keywordtype}{intent(in)}  :: G\textcolor{comment}{  !< Ocean grid structure}}
\DoxyCodeLine{1093   \textcolor{keywordtype}{type}(verticalGrid\_type),   \textcolor{keywordtype}{intent(in)}  :: GV\textcolor{comment}{ !< Ocean vertical grid structure}}
\DoxyCodeLine{1094   \textcolor{keywordtype}{type}(unit\_scale\_type),     \textcolor{keywordtype}{intent(in)}  :: US\textcolor{comment}{ !< A dimensional unit scaling type}}
\DoxyCodeLine{1095 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZK\_(GV)+1)}, \&}
\DoxyCodeLine{1096                              \textcolor{keywordtype}{intent(out)} :: a\_cpl\textcolor{comment}{ !< Coupling coefficient across interfaces [Z T-\/1 ~> m s-\/1].}}
\DoxyCodeLine{1097 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1098                              \textcolor{keywordtype}{intent(in)}  :: hvel\textcolor{comment}{ !< Thickness at velocity points [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1099   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(SZIB\_(G))}, \&}
\DoxyCodeLine{1100                              \textcolor{keywordtype}{intent(in)}  :: do\_i\textcolor{comment}{ !< If true, determine coupling coefficient for a column}}
\DoxyCodeLine{1101 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1102                              \textcolor{keywordtype}{intent(in)}  :: h\_harm\textcolor{comment}{ !< Harmonic mean of thicknesses around a velocity}}
\DoxyCodeLine{1103 \textcolor{comment}{                                                   !! grid point [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1104 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G))}, \textcolor{keywordtype}{intent(in)}  :: bbl\_thick\textcolor{comment}{ !< Bottom boundary layer thickness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1105 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G))}, \textcolor{keywordtype}{intent(in)}  :: kv\_bbl\textcolor{comment}{ !< Bottom boundary layer viscosity [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{1106 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZK\_(GV)+1)}, \&}
\DoxyCodeLine{1107                              \textcolor{keywordtype}{intent(in)}  :: z\_i\textcolor{comment}{  !< Estimate of interface heights above the bottom,}}
\DoxyCodeLine{1108 \textcolor{comment}{                                                 !! normalized by the bottom boundary layer thickness}}
\DoxyCodeLine{1109 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G))}, \textcolor{keywordtype}{intent(out)} :: h\_ml\textcolor{comment}{ !< Mixed layer depth [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1110   \textcolor{keywordtype}{integer},                   \textcolor{keywordtype}{intent(in)}  :: j\textcolor{comment}{    !< j-\/index to find coupling coefficient for}}
\DoxyCodeLine{1111 \textcolor{keywordtype}{  real},                      \textcolor{keywordtype}{intent(in)}  :: dt\textcolor{comment}{   !< Time increment [T ~> s]}}
\DoxyCodeLine{1112   \textcolor{keywordtype}{type}(vertvisc\_CS),         \textcolor{keywordtype}{pointer}     :: CS\textcolor{comment}{   !< Vertical viscosity control structure}}
\DoxyCodeLine{1113   \textcolor{keywordtype}{type}(vertvisc\_type),       \textcolor{keywordtype}{intent(in)}  :: visc\textcolor{comment}{ !< Structure containing viscosities and bottom drag}}
\DoxyCodeLine{1114   \textcolor{keywordtype}{type}(mech\_forcing),        \textcolor{keywordtype}{intent(in)}  :: forces\textcolor{comment}{ !< A structure with the driving mechanical forces}}
\DoxyCodeLine{1115   \textcolor{keywordtype}{logical},                   \textcolor{keywordtype}{intent(in)}  :: work\_on\_u\textcolor{comment}{ !< If true, u-\/points are being calculated,}}
\DoxyCodeLine{1116 \textcolor{comment}{                                                  !! otherwise they are v-\/points}}
\DoxyCodeLine{1117   \textcolor{keywordtype}{type}(ocean\_OBC\_type),      \textcolor{keywordtype}{pointer}     :: OBC\textcolor{comment}{   !< Open boundary condition structure}}
\DoxyCodeLine{1118   \textcolor{keywordtype}{logical},         \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(in)}  :: shelf\textcolor{comment}{ !< If present and true, use a surface boundary}}
\DoxyCodeLine{1119 \textcolor{comment}{                                                  !! condition appropriate for an ice shelf.}}
\DoxyCodeLine{1120 }
\DoxyCodeLine{1121   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1122 }
\DoxyCodeLine{1123 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G))} :: \&}
\DoxyCodeLine{1124     u\_star, \&   \textcolor{comment}{! ustar at a velocity point [Z T-\/1 ~> m s-\/1].}}
\DoxyCodeLine{1125     absf, \&     \textcolor{comment}{! The average of the neighboring absolute values of f [T-\/1 ~> s-\/1].}}
\DoxyCodeLine{1126 \textcolor{comment}{!      h\_ml, \&  ! The mixed layer depth [H ~> m or kg m-\/2].}}
\DoxyCodeLine{1127     nk\_visc, \&  \textcolor{comment}{! The (real) interface index of the base of mixed layer.}}
\DoxyCodeLine{1128     z\_t, \&      \textcolor{comment}{! The distance from the top, sometimes normalized}}
\DoxyCodeLine{1129                 \textcolor{comment}{! by Hmix, [H ~> m or kg m-\/2] or [nondim].}}
\DoxyCodeLine{1130     kv\_tbl, \&   \textcolor{comment}{! The viscosity in a top boundary layer under ice [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{1131     tbl\_thick}
\DoxyCodeLine{1132 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZK\_(GV)+1)} :: \&}
\DoxyCodeLine{1133     Kv\_tot, \&   \textcolor{comment}{! The total viscosity at an interface [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{1134     Kv\_add      \textcolor{comment}{! A viscosity to add [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{1135 \textcolor{keywordtype}{  real} :: h\_shear \textcolor{comment}{! The distance over which shears occur [H ~> m or kg m-\/2].}}
\DoxyCodeLine{1136 \textcolor{keywordtype}{  real} :: r       \textcolor{comment}{! A thickness to compare with Hbbl [H ~> m or kg m-\/2].}}
\DoxyCodeLine{1137 \textcolor{keywordtype}{  real} :: visc\_ml \textcolor{comment}{! The mixed layer viscosity [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{1138 \textcolor{keywordtype}{  real} :: I\_Hmix  \textcolor{comment}{! The inverse of the mixed layer thickness [H-\/1 ~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{1139 \textcolor{keywordtype}{  real} :: a\_ml    \textcolor{comment}{! The layer coupling coefficient across an interface in}}
\DoxyCodeLine{1140                   \textcolor{comment}{! the mixed layer [Z T-\/1 ~> m s-\/1].}}
\DoxyCodeLine{1141 \textcolor{keywordtype}{  real} :: I\_amax  \textcolor{comment}{! The inverse of the maximum coupling coefficient [T Z-\/1 ~> s m-\/1].}}
\DoxyCodeLine{1142 \textcolor{keywordtype}{  real} :: temp1   \textcolor{comment}{! A temporary variable [H Z ~> m2 or kg m-\/1]}}
\DoxyCodeLine{1143 \textcolor{keywordtype}{  real} :: h\_neglect   \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{1144                       \textcolor{comment}{! in roundoff and can be neglected [H ~> m or kg m-\/2].}}
\DoxyCodeLine{1145 \textcolor{keywordtype}{  real} :: z2      \textcolor{comment}{! A copy of z\_i [nondim]}}
\DoxyCodeLine{1146 \textcolor{keywordtype}{  real} :: botfn   \textcolor{comment}{! A function that is 1 at the bottom and small far from it [nondim]}}
\DoxyCodeLine{1147 \textcolor{keywordtype}{  real} :: topfn   \textcolor{comment}{! A function that is 1 at the top and small far from it [nondim]}}
\DoxyCodeLine{1148 \textcolor{keywordtype}{  real} :: kv\_top  \textcolor{comment}{! A viscosity associated with the top boundary layer [Z2 T-\/1 ~> m2 s-\/1]}}
\DoxyCodeLine{1149   \textcolor{keywordtype}{logical} :: do\_shelf, do\_OBCs}
\DoxyCodeLine{1150   \textcolor{keywordtype}{integer} :: i, k, is, ie, max\_nk}
\DoxyCodeLine{1151   \textcolor{keywordtype}{integer} :: nz}
\DoxyCodeLine{1152 }
\DoxyCodeLine{1153   a\_cpl(:,:) = 0.0}
\DoxyCodeLine{1154   kv\_tot(:,:) = 0.0}
\DoxyCodeLine{1155 }
\DoxyCodeLine{1156   \textcolor{keywordflow}{if} (work\_on\_u) \textcolor{keywordflow}{then} ; is = g\%IscB ; ie = g\%IecB}
\DoxyCodeLine{1157   \textcolor{keywordflow}{else} ; is = g\%isc ; ie = g\%iec ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1158   nz = g\%ke}
\DoxyCodeLine{1159   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{1160 }
\DoxyCodeLine{1161   \textcolor{keywordflow}{if} (cs\%answers\_2018) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1162     \textcolor{comment}{!   The maximum coupling coefficent was originally introduced to avoid}}
\DoxyCodeLine{1163     \textcolor{comment}{! truncation error problems in the tridiagonal solver. Effectively, the 1e-\/10}}
\DoxyCodeLine{1164     \textcolor{comment}{! sets the maximum coupling coefficient increment to 1e10 m per timestep.}}
\DoxyCodeLine{1165     i\_amax = (1.0e-\/10*us\%Z\_to\_m) * dt}
\DoxyCodeLine{1166   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1167     i\_amax = 0.0}
\DoxyCodeLine{1168 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1169 }
\DoxyCodeLine{1170   do\_shelf = .false. ; \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(shelf)) do\_shelf = shelf}
\DoxyCodeLine{1171   do\_obcs = .false.}
\DoxyCodeLine{1172   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(obc)) \textcolor{keywordflow}{then} ; do\_obcs = (obc\%number\_of\_segments > 0) ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1173   h\_ml(:) = 0.0}
\DoxyCodeLine{1174 }
\DoxyCodeLine{1175 \textcolor{comment}{!    The following loop calculates the vertical average velocity and}}
\DoxyCodeLine{1176 \textcolor{comment}{!  surface mixed layer contributions to the vertical viscosity.}}
\DoxyCodeLine{1177   \textcolor{keywordflow}{do} i=is,ie ; kv\_tot(i,1) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1178   \textcolor{keywordflow}{if} ((gv\%nkml>0) .or. do\_shelf) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1179     \textcolor{keywordflow}{if} (do\_i(i)) kv\_tot(i,k) = cs\%Kv}
\DoxyCodeLine{1180 \textcolor{keywordflow}{  enddo} ;\textcolor{keywordflow}{ enddo} ; \textcolor{keywordflow}{else}}
\DoxyCodeLine{1181     i\_hmix = 1.0 / (cs\%Hmix + h\_neglect)}
\DoxyCodeLine{1182     \textcolor{keywordflow}{do} i=is,ie ; z\_t(i) = h\_neglect*i\_hmix ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1183     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1184       z\_t(i) = z\_t(i) + h\_harm(i,k-\/1)*i\_hmix}
\DoxyCodeLine{1185       kv\_tot(i,k) = cs\%Kv + cs\%Kvml / ((z\_t(i)*z\_t(i)) *  \&}
\DoxyCodeLine{1186                (1.0 + 0.09*z\_t(i)*z\_t(i)*z\_t(i)*z\_t(i)*z\_t(i)*z\_t(i)))}
\DoxyCodeLine{1187 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1188 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1189 }
\DoxyCodeLine{1190   \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1191     \textcolor{keywordflow}{if} (cs\%bottomdraglaw) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1192       r = hvel(i,nz)*0.5}
\DoxyCodeLine{1193       \textcolor{keywordflow}{if} (r < bbl\_thick(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1194         a\_cpl(i,nz+1) = kv\_bbl(i) / (i\_amax*kv\_bbl(i) + (r+h\_neglect)*gv\%H\_to\_Z)}
\DoxyCodeLine{1195       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1196         a\_cpl(i,nz+1) = kv\_bbl(i) / (i\_amax*kv\_bbl(i) + (bbl\_thick(i)+h\_neglect)*gv\%H\_to\_Z)}
\DoxyCodeLine{1197 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1198     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1199       a\_cpl(i,nz+1) = cs\%Kvbbl / ((0.5*hvel(i,nz)+h\_neglect)*gv\%H\_to\_Z + i\_amax*cs\%Kvbbl)}
\DoxyCodeLine{1200 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1201 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1202 }
\DoxyCodeLine{1203   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Kv\_shear)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1204     \textcolor{comment}{! The factor of 2 that used to be required in the viscosities is no longer needed.}}
\DoxyCodeLine{1205     \textcolor{keywordflow}{if} (work\_on\_u) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1206       \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1207         kv\_add(i,k) = 0.5*(visc\%Kv\_shear(i,j,k) + visc\%Kv\_shear(i+1,j,k))}
\DoxyCodeLine{1208 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1209       \textcolor{keywordflow}{if} (do\_obcs) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1210         \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i) .and. (obc\%segnum\_u(i,j) /= obc\_none)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1211           \textcolor{keywordflow}{if} (obc\%segment(obc\%segnum\_u(i,j))\%direction == obc\_direction\_e) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1212             \textcolor{keywordflow}{do} k=2,nz ; kv\_add(i,k) = visc\%Kv\_shear(i,j,k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1213           \textcolor{keywordflow}{elseif} (obc\%segment(obc\%segnum\_u(i,j))\%direction == obc\_direction\_w) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1214             \textcolor{keywordflow}{do} k=2,nz ; kv\_add(i,k) = visc\%Kv\_shear(i+1,j,k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1215 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1216 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1217 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1218       \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1219         kv\_tot(i,k) = kv\_tot(i,k) + kv\_add(i,k)}
\DoxyCodeLine{1220 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1221     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1222       \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1223         kv\_add(i,k) = 0.5*(visc\%Kv\_shear(i,j,k) + visc\%Kv\_shear(i,j+1,k))}
\DoxyCodeLine{1224 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1225       \textcolor{keywordflow}{if} (do\_obcs) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1226         \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i) .and. (obc\%segnum\_v(i,j) /= obc\_none)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1227           \textcolor{keywordflow}{if} (obc\%segment(obc\%segnum\_v(i,j))\%direction == obc\_direction\_n) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1228             \textcolor{keywordflow}{do} k=2,nz ; kv\_add(i,k) = visc\%Kv\_shear(i,j,k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1229           \textcolor{keywordflow}{elseif} (obc\%segment(obc\%segnum\_v(i,j))\%direction == obc\_direction\_s) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1230             \textcolor{keywordflow}{do} k=2,nz ; kv\_add(i,k) = visc\%Kv\_shear(i,j+1,k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1231 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1232 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1233 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1234       \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1235         kv\_tot(i,k) = kv\_tot(i,k) + kv\_add(i,k)}
\DoxyCodeLine{1236 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1237 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1238 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1239 }
\DoxyCodeLine{1240   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Kv\_shear\_Bu)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1241     \textcolor{keywordflow}{if} (work\_on\_u) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1242       \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{If} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1243         kv\_tot(i,k) = kv\_tot(i,k) + (0.5)*(visc\%Kv\_shear\_Bu(i,j-\/1,k) + visc\%Kv\_shear\_Bu(i,j,k))}
\DoxyCodeLine{1244 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1245     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1246       \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1247         kv\_tot(i,k) = kv\_tot(i,k) + (0.5)*(visc\%Kv\_shear\_Bu(i-\/1,j,k) + visc\%Kv\_shear\_Bu(i,j,k))}
\DoxyCodeLine{1248 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1249 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1250 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1251 }
\DoxyCodeLine{1252   \textcolor{keywordflow}{do} k=nz,2,-\/1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1253     \textcolor{comment}{!    botfn determines when a point is within the influence of the bottom}}
\DoxyCodeLine{1254     \textcolor{comment}{!  boundary layer, going from 1 at the bottom to 0 in the interior.}}
\DoxyCodeLine{1255     z2 = z\_i(i,k)}
\DoxyCodeLine{1256     botfn = 1.0 / (1.0 + 0.09*z2*z2*z2*z2*z2*z2)}
\DoxyCodeLine{1257 }
\DoxyCodeLine{1258     \textcolor{keywordflow}{if} (cs\%bottomdraglaw) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1259       kv\_tot(i,k) = kv\_tot(i,k) + (kv\_bbl(i) -\/ cs\%Kv)*botfn}
\DoxyCodeLine{1260       r = 0.5*(hvel(i,k) + hvel(i,k-\/1))}
\DoxyCodeLine{1261       \textcolor{keywordflow}{if} (r > bbl\_thick(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1262         h\_shear = ((1.0 -\/ botfn) * r + botfn*bbl\_thick(i)) + h\_neglect}
\DoxyCodeLine{1263       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1264         h\_shear = r + h\_neglect}
\DoxyCodeLine{1265 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1266     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1267       kv\_tot(i,k) = kv\_tot(i,k) + (cs\%Kvbbl-\/cs\%Kv)*botfn}
\DoxyCodeLine{1268       h\_shear = 0.5*(hvel(i,k) + hvel(i,k-\/1) + h\_neglect)}
\DoxyCodeLine{1269 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1270 }
\DoxyCodeLine{1271     \textcolor{comment}{! Calculate the coupling coefficients from the viscosities.}}
\DoxyCodeLine{1272     a\_cpl(i,k) = kv\_tot(i,k) / (h\_shear*gv\%H\_to\_Z + i\_amax*kv\_tot(i,k))}
\DoxyCodeLine{1273 \textcolor{keywordflow}{  endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i \& k loops}}
\DoxyCodeLine{1274 }
\DoxyCodeLine{1275   \textcolor{keywordflow}{if} (do\_shelf) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1276     \textcolor{comment}{! Set the coefficients to include the no-\/slip surface stress.}}
\DoxyCodeLine{1277     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1278       \textcolor{keywordflow}{if} (work\_on\_u) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1279         kv\_tbl(i) = visc\%Kv\_tbl\_shelf\_u(i,j)}
\DoxyCodeLine{1280         tbl\_thick(i) = visc\%tbl\_thick\_shelf\_u(i,j) * gv\%Z\_to\_H + h\_neglect}
\DoxyCodeLine{1281       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1282         kv\_tbl(i) = visc\%Kv\_tbl\_shelf\_v(i,j)}
\DoxyCodeLine{1283         tbl\_thick(i) = visc\%tbl\_thick\_shelf\_v(i,j) * gv\%Z\_to\_H + h\_neglect}
\DoxyCodeLine{1284 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1285       z\_t(i) = 0.0}
\DoxyCodeLine{1286 }
\DoxyCodeLine{1287       \textcolor{comment}{! If a\_cpl(i,1) were not already 0, it would be added here.}}
\DoxyCodeLine{1288       \textcolor{keywordflow}{if} (0.5*hvel(i,1) > tbl\_thick(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1289         a\_cpl(i,1) = kv\_tbl(i) / (tbl\_thick(i)*gv\%H\_to\_Z + i\_amax*kv\_tbl(i))}
\DoxyCodeLine{1290       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1291         a\_cpl(i,1) = kv\_tbl(i) / ((0.5*hvel(i,1)+h\_neglect)*gv\%H\_to\_Z + i\_amax*kv\_tbl(i))}
\DoxyCodeLine{1292 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1293 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1294 }
\DoxyCodeLine{1295     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie ;  \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1296       z\_t(i) = z\_t(i) + hvel(i,k-\/1) / tbl\_thick(i)}
\DoxyCodeLine{1297       topfn = 1.0 / (1.0 + 0.09 * z\_t(i)**6)}
\DoxyCodeLine{1298 }
\DoxyCodeLine{1299       r = 0.5*(hvel(i,k)+hvel(i,k-\/1))}
\DoxyCodeLine{1300       \textcolor{keywordflow}{if} (r > tbl\_thick(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1301         h\_shear = ((1.0 -\/ topfn) * r + topfn*tbl\_thick(i)) + h\_neglect}
\DoxyCodeLine{1302       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1303         h\_shear = r + h\_neglect}
\DoxyCodeLine{1304 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1305 }
\DoxyCodeLine{1306       kv\_top = topfn * kv\_tbl(i)}
\DoxyCodeLine{1307       a\_cpl(i,k) = a\_cpl(i,k) + kv\_top / (h\_shear*gv\%H\_to\_Z + i\_amax*kv\_top)}
\DoxyCodeLine{1308 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1309   \textcolor{keywordflow}{elseif} (cs\%dynamic\_viscous\_ML .or. (gv\%nkml>0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1310     max\_nk = 0}
\DoxyCodeLine{1311     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1312       \textcolor{keywordflow}{if} (gv\%nkml>0) nk\_visc(i) = real(gv\%nkml+1)}
\DoxyCodeLine{1313       \textcolor{keywordflow}{if} (work\_on\_u) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1314         u\_star(i) = 0.5*(forces\%ustar(i,j) + forces\%ustar(i+1,j))}
\DoxyCodeLine{1315         absf(i) = 0.5*(abs(g\%CoriolisBu(i,j-\/1)) + abs(g\%CoriolisBu(i,j)))}
\DoxyCodeLine{1316         \textcolor{keywordflow}{if} (cs\%dynamic\_viscous\_ML) nk\_visc(i) = visc\%nkml\_visc\_u(i,j) + 1}
\DoxyCodeLine{1317       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1318         u\_star(i) = 0.5*(forces\%ustar(i,j) + forces\%ustar(i,j+1))}
\DoxyCodeLine{1319         absf(i) = 0.5*(abs(g\%CoriolisBu(i-\/1,j)) + abs(g\%CoriolisBu(i,j)))}
\DoxyCodeLine{1320         \textcolor{keywordflow}{if} (cs\%dynamic\_viscous\_ML) nk\_visc(i) = visc\%nkml\_visc\_v(i,j) + 1}
\DoxyCodeLine{1321 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1322       h\_ml(i) = h\_neglect ; z\_t(i) = 0.0}
\DoxyCodeLine{1323       max\_nk = max(max\_nk,ceiling(nk\_visc(i) -\/ 1.0))}
\DoxyCodeLine{1324 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1325 }
\DoxyCodeLine{1326     \textcolor{keywordflow}{if} (do\_obcs) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (work\_on\_u) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1327       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i) .and. (obc\%segnum\_u(i,j) /= obc\_none)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1328         \textcolor{keywordflow}{if} (obc\%segment(obc\%segnum\_u(i,j))\%direction == obc\_direction\_e) \&}
\DoxyCodeLine{1329           u\_star(i) = forces\%ustar(i,j)}
\DoxyCodeLine{1330         \textcolor{keywordflow}{if} (obc\%segment(obc\%segnum\_u(i,j))\%direction == obc\_direction\_w) \&}
\DoxyCodeLine{1331           u\_star(i) = forces\%ustar(i+1,j)}
\DoxyCodeLine{1332 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1333     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1334       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i) .and. (obc\%segnum\_v(i,j) /= obc\_none)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1335         \textcolor{keywordflow}{if} (obc\%segment(obc\%segnum\_v(i,j))\%direction == obc\_direction\_n) \&}
\DoxyCodeLine{1336           u\_star(i) = forces\%ustar(i,j)}
\DoxyCodeLine{1337         \textcolor{keywordflow}{if} (obc\%segment(obc\%segnum\_v(i,j))\%direction == obc\_direction\_s) \&}
\DoxyCodeLine{1338           u\_star(i) = forces\%ustar(i,j+1)}
\DoxyCodeLine{1339 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1340 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1341 }
\DoxyCodeLine{1342     \textcolor{keywordflow}{do} k=1,max\_nk ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1343       \textcolor{keywordflow}{if} (k+1 <= nk\_visc(i)) \textcolor{keywordflow}{then} \textcolor{comment}{! This layer is all in the ML.}}
\DoxyCodeLine{1344         h\_ml(i) = h\_ml(i) + hvel(i,k)}
\DoxyCodeLine{1345       \textcolor{keywordflow}{elseif} (k < nk\_visc(i)) \textcolor{keywordflow}{then} \textcolor{comment}{! Part of this layer is in the ML.}}
\DoxyCodeLine{1346         h\_ml(i) = h\_ml(i) + (nk\_visc(i) -\/ k) * hvel(i,k)}
\DoxyCodeLine{1347 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1348 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1349 }
\DoxyCodeLine{1350     \textcolor{keywordflow}{do} k=2,max\_nk ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (k < nk\_visc(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1351       \textcolor{comment}{! Set the viscosity at the interfaces.}}
\DoxyCodeLine{1352       z\_t(i) = z\_t(i) + hvel(i,k-\/1)}
\DoxyCodeLine{1353       temp1 = (z\_t(i)*h\_ml(i) -\/ z\_t(i)*z\_t(i))*gv\%H\_to\_Z}
\DoxyCodeLine{1354       \textcolor{comment}{!   This viscosity is set to go to 0 at the mixed layer top and bottom (in a log-\/layer)}}
\DoxyCodeLine{1355       \textcolor{comment}{! and be further limited by rotation to give the natural Ekman length.}}
\DoxyCodeLine{1356       visc\_ml = u\_star(i) * 0.41 * (temp1*u\_star(i)) / (absf(i)*temp1 + (h\_ml(i)+h\_neglect)*u\_star(i))}
\DoxyCodeLine{1357       a\_ml = visc\_ml / (0.25*(hvel(i,k)+hvel(i,k-\/1) + h\_neglect) * gv\%H\_to\_Z + 0.5*i\_amax*visc\_ml)}
\DoxyCodeLine{1358       \textcolor{comment}{! Choose the largest estimate of a.}}
\DoxyCodeLine{1359       \textcolor{keywordflow}{if} (a\_ml > a\_cpl(i,k)) a\_cpl(i,k) = a\_ml}
\DoxyCodeLine{1360 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1361 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1362 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__vert__friction_a62e586a80ed4bdd3fd27ab62ca4c054f}\label{namespacemom__vert__friction_a62e586a80ed4bdd3fd27ab62ca4c054f}} 
\index{mom\_vert\_friction@{mom\_vert\_friction}!updatecfltruncationvalue@{updatecfltruncationvalue}}
\index{updatecfltruncationvalue@{updatecfltruncationvalue}!mom\_vert\_friction@{mom\_vert\_friction}}
\doxysubsubsection{\texorpdfstring{updatecfltruncationvalue()}{updatecfltruncationvalue()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+vert\+\_\+friction\+::updatecfltruncationvalue (\begin{DoxyParamCaption}\item[{type(time\+\_\+type), intent(in), target}]{Time,  }\item[{type(\mbox{\hyperlink{structmom__vert__friction_1_1vertvisc__cs}{vertvisc\+\_\+cs}}), pointer}]{CS,  }\item[{logical, intent(in), optional}]{activate }\end{DoxyParamCaption})}



Update the C\+FL truncation value as a function of time. If called with the optional argument activate=.true., record the value of Time as the beginning of the ramp period. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em time} & Current model time \\
\hline
 & {\em cs} & Vertical viscosity control structure \\
\hline
\mbox{\texttt{ in}}  & {\em activate} & Specifiy whether to record the value of Time as the beginning of the ramp period \\
\hline
\end{DoxyParams}


Definition at line 1852 of file M\+O\+M\+\_\+vert\+\_\+friction.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1852   \textcolor{keywordtype}{type}(time\_type), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{     !< Current model time}}
\DoxyCodeLine{1853   \textcolor{keywordtype}{type}(vertvisc\_CS),       \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{       !< Vertical viscosity control structure}}
\DoxyCodeLine{1854   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{optional},       \textcolor{keywordtype}{intent(in)}    :: activate\textcolor{comment}{ !< Specifiy whether to record the value of}}
\DoxyCodeLine{1855 \textcolor{comment}{                                                     !! Time as the beginning of the ramp period}}
\DoxyCodeLine{1856 }
\DoxyCodeLine{1857   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1858 \textcolor{keywordtype}{  real} :: deltaTime, wghtA}
\DoxyCodeLine{1859   \textcolor{keywordtype}{character(len=12)} :: msg}
\DoxyCodeLine{1860 }
\DoxyCodeLine{1861   \textcolor{keywordflow}{if} (cs\%truncRampTime==0.) \textcolor{keywordflow}{return} \textcolor{comment}{! This indicates to ramping is turned off}}
\DoxyCodeLine{1862 }
\DoxyCodeLine{1863   \textcolor{comment}{! We use the optional argument to indicate this Time should be recorded as the}}
\DoxyCodeLine{1864   \textcolor{comment}{! beginning of the ramp-\/up period.}}
\DoxyCodeLine{1865   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(activate)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1866     \textcolor{keywordflow}{if} (activate) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1867       cs\%rampStartTime = time \textcolor{comment}{! Record the current time}}
\DoxyCodeLine{1868       cs\%CFLrampingIsActivated = .true.}
\DoxyCodeLine{1869 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1870 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1871   \textcolor{keywordflow}{if} (.not.cs\%CFLrampingIsActivated) \textcolor{keywordflow}{return}}
\DoxyCodeLine{1872   deltatime = max( 0., time\_type\_to\_real( time -\/ cs\%rampStartTime ) )}
\DoxyCodeLine{1873   \textcolor{keywordflow}{if} (deltatime >= cs\%truncRampTime) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1874     cs\%CFL\_trunc = cs\%CFL\_truncE}
\DoxyCodeLine{1875     cs\%truncRampTime = 0. \textcolor{comment}{! This turns off ramping after this call}}
\DoxyCodeLine{1876   \textcolor{keywordflow}{else}}
\DoxyCodeLine{1877     wghta = min( 1., deltatime / cs\%truncRampTime ) \textcolor{comment}{! Linear profile in time}}
\DoxyCodeLine{1878     \textcolor{comment}{!wghtA = wghtA*wghtA ! Convert linear profile to parabolic profile in time}}
\DoxyCodeLine{1879     \textcolor{comment}{!wghtA = wghtA*wghtA*(3. -\/ 2.*wghtA) ! Convert linear profile to cosine profile}}
\DoxyCodeLine{1880     wghta = 1. -\/ ( (1. -\/ wghta)**2 ) \textcolor{comment}{! Convert linear profiel to nverted parabolic profile}}
\DoxyCodeLine{1881     cs\%CFL\_trunc = cs\%CFL\_truncS + wghta * ( cs\%CFL\_truncE -\/ cs\%CFL\_truncS )}
\DoxyCodeLine{1882 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1883   \textcolor{keyword}{write}(msg(1:12),\textcolor{stringliteral}{'(es12.3)'}) cs\%CFL\_trunc}
\DoxyCodeLine{1884   \textcolor{keyword}{call }mom\_error(note, \textcolor{stringliteral}{"MOM\_vert\_friction: updateCFLtruncationValue set CFL"}// \&}
\DoxyCodeLine{1885                        \textcolor{stringliteral}{" limit to "}//trim(msg))}

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__vert__friction_a8f1a390fa24fbe985068fed9ac26873c}\label{namespacemom__vert__friction_a8f1a390fa24fbe985068fed9ac26873c}} 
\index{mom\_vert\_friction@{mom\_vert\_friction}!vertvisc@{vertvisc}}
\index{vertvisc@{vertvisc}!mom\_vert\_friction@{mom\_vert\_friction}}
\doxysubsubsection{\texorpdfstring{vertvisc()}{vertvisc()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+vert\+\_\+friction\+::vertvisc (\begin{DoxyParamCaption}\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed, gv \%ke), intent(inout)}]{u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb, gv \%ke), intent(inout)}]{v,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsd\+: g \%jed, gv \%ke), intent(in)}]{h,  }\item[{type(mech\+\_\+forcing), intent(in)}]{forces,  }\item[{type(vertvisc\+\_\+type), intent(inout)}]{visc,  }\item[{real, intent(in)}]{dt,  }\item[{type(ocean\+\_\+obc\+\_\+type), pointer}]{O\+BC,  }\item[{type(accel\+\_\+diag\+\_\+ptrs), intent(inout)}]{A\+Dp,  }\item[{type(cont\+\_\+diag\+\_\+ptrs), intent(inout)}]{C\+Dp,  }\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__vert__friction_1_1vertvisc__cs}{vertvisc\+\_\+cs}}), pointer}]{CS,  }\item[{real, dimension( g \%isdb\+: g \%iedb, g \%jsd\+: g \%jed), intent(out), optional}]{taux\+\_\+bot,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb), intent(out), optional}]{tauy\+\_\+bot,  }\item[{type(wave\+\_\+parameters\+\_\+cs), optional, pointer}]{Waves }\end{DoxyParamCaption})}



Perform a fully implicit vertical diffusion of momentum. Stress top and bottom boundary conditions are used. 

This is solving the tridiagonal system \[ \left(h_k + a_{k + 1/2} + a_{k - 1/2} + r_k\right) u_k^{n+1} = h_k u_k^n + a_{k + 1/2} u_{k+1}^{n+1} + a_{k - 1/2} u_{k-1}^{n+1} \] where $a_{k + 1/2} = \Delta t \nu_{k + 1/2} / h_{k + 1/2}$ is the {\itshape interfacial coupling thickness per time step}, encompassing background viscosity as well as contributions from enhanced mixed and bottom layer viscosities. \$r\+\_\+k\$ is a Rayleight drag term due to channel drag. There is an additional stress term on the right-\/hand side if D\+I\+R\+E\+C\+T\+\_\+\+S\+T\+R\+E\+SS is true, applied to the surface layer. 
\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & Ocean grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & Ocean vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in,out}}  & {\em u} & Zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em v} & Meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em forces} & A structure with the driving mechanical forces \\
\hline
\mbox{\texttt{ in,out}}  & {\em visc} & Viscosities and bottom drag \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
 & {\em obc} & Open boundary condition structure \\
\hline
\mbox{\texttt{ in,out}}  & {\em adp} & Accelerations in the momentum equations for diagnostics \\
\hline
\mbox{\texttt{ in,out}}  & {\em cdp} & Continuity equation terms \\
\hline
 & {\em cs} & Vertical viscosity control structure \\
\hline
\mbox{\texttt{ out}}  & {\em taux\+\_\+bot} & Zonal bottom stress from ocean to \\
\hline
\mbox{\texttt{ out}}  & {\em tauy\+\_\+bot} & Meridional bottom stress from ocean to \\
\hline
 & {\em waves} & Container for wave/\+Stokes information \\
\hline
\end{DoxyParams}


Definition at line 159 of file M\+O\+M\+\_\+vert\+\_\+friction.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{159   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{      !< Ocean grid structure}}
\DoxyCodeLine{160   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{     !< Ocean vertical grid structure}}
\DoxyCodeLine{161   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{     !< A dimensional unit scaling type}}
\DoxyCodeLine{162 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{163                            \textcolor{keywordtype}{intent(inout)} :: u\textcolor{comment}{      !< Zonal velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{164 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{165                            \textcolor{keywordtype}{intent(inout)} :: v\textcolor{comment}{      !< Meridional velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{166 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{167                            \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{      !< Layer thickness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{168   \textcolor{keywordtype}{type}(mech\_forcing),    \textcolor{keywordtype}{intent(in)}      :: forces\textcolor{comment}{ !< A structure with the driving mechanical forces}}
\DoxyCodeLine{169   \textcolor{keywordtype}{type}(vertvisc\_type),   \textcolor{keywordtype}{intent(inout)}   :: visc\textcolor{comment}{   !< Viscosities and bottom drag}}
\DoxyCodeLine{170 \textcolor{keywordtype}{  real},                  \textcolor{keywordtype}{intent(in)}      :: dt\textcolor{comment}{     !< Time increment [T ~> s]}}
\DoxyCodeLine{171   \textcolor{keywordtype}{type}(ocean\_OBC\_type),  \textcolor{keywordtype}{pointer}         :: OBC\textcolor{comment}{    !< Open boundary condition structure}}
\DoxyCodeLine{172   \textcolor{keywordtype}{type}(accel\_diag\_ptrs), \textcolor{keywordtype}{intent(inout)}   :: ADp\textcolor{comment}{    !< Accelerations in the momentum}}
\DoxyCodeLine{173 \textcolor{comment}{                                                   !! equations for diagnostics}}
\DoxyCodeLine{174   \textcolor{keywordtype}{type}(cont\_diag\_ptrs),  \textcolor{keywordtype}{intent(inout)}   :: CDp\textcolor{comment}{    !< Continuity equation terms}}
\DoxyCodeLine{175   \textcolor{keywordtype}{type}(vertvisc\_CS),     \textcolor{keywordtype}{pointer}         :: CS\textcolor{comment}{     !< Vertical viscosity control structure}}
\DoxyCodeLine{176 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G))}, \&}
\DoxyCodeLine{177                    \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: taux\_bot\textcolor{comment}{ !< Zonal bottom stress from ocean to}}
\DoxyCodeLine{178 \textcolor{comment}{                                                     !! rock [R L Z T-\/2 ~> Pa]}}
\DoxyCodeLine{179 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G))}, \&}
\DoxyCodeLine{180                    \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{intent(out)} :: tauy\_bot\textcolor{comment}{ !< Meridional bottom stress from ocean to}}
\DoxyCodeLine{181 \textcolor{comment}{                                                     !! rock [R L Z T-\/2 ~> Pa]}}
\DoxyCodeLine{182   \textcolor{keywordtype}{type}(wave\_parameters\_CS), \&}
\DoxyCodeLine{183                    \textcolor{keywordtype}{optional}, \textcolor{keywordtype}{pointer}     :: Waves\textcolor{comment}{ !< Container for wave/Stokes information}}
\DoxyCodeLine{184 }
\DoxyCodeLine{185   \textcolor{comment}{! Fields from forces used in this subroutine:}}
\DoxyCodeLine{186   \textcolor{comment}{!   taux: Zonal wind stress [R L Z T-\/2 ~> Pa].}}
\DoxyCodeLine{187   \textcolor{comment}{!   tauy: Meridional wind stress [R L Z T-\/2 ~> Pa].}}
\DoxyCodeLine{188 }
\DoxyCodeLine{189   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{190 }
\DoxyCodeLine{191 \textcolor{keywordtype}{  real} :: b1(SZIB\_(G))          \textcolor{comment}{! A variable used by the tridiagonal solver [H-\/1 ~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{192 \textcolor{keywordtype}{  real} :: c1(SZIB\_(G),SZK\_(G))  \textcolor{comment}{! A variable used by the tridiagonal solver [nondim].}}
\DoxyCodeLine{193 \textcolor{keywordtype}{  real} :: d1(SZIB\_(G))          \textcolor{comment}{! d1=1-\/c1 is used by the tridiagonal solver [nondim].}}
\DoxyCodeLine{194 \textcolor{keywordtype}{  real} :: Ray(SZIB\_(G),SZK\_(G)) \textcolor{comment}{! Ray is the Rayleigh-\/drag velocity [Z T-\/1 ~> m s-\/1].}}
\DoxyCodeLine{195 \textcolor{keywordtype}{  real} :: b\_denom\_1             \textcolor{comment}{! The first term in the denominator of b1 [H ~> m or kg m-\/2].}}
\DoxyCodeLine{196 }
\DoxyCodeLine{197 \textcolor{keywordtype}{  real} :: Hmix             \textcolor{comment}{! The mixed layer thickness over which stress}}
\DoxyCodeLine{198                            \textcolor{comment}{! is applied with direct\_stress [H ~> m or kg m-\/2].}}
\DoxyCodeLine{199 \textcolor{keywordtype}{  real} :: I\_Hmix           \textcolor{comment}{! The inverse of Hmix [H-\/1 ~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{200 \textcolor{keywordtype}{  real} :: Idt              \textcolor{comment}{! The inverse of the time step [T-\/1 ~> s-\/1].}}
\DoxyCodeLine{201 \textcolor{keywordtype}{  real} :: dt\_Rho0          \textcolor{comment}{! The time step divided by the mean density [T H Z-\/1 R-\/1 ~> s m3 kg-\/1 or s].}}
\DoxyCodeLine{202 \textcolor{keywordtype}{  real} :: dt\_Z\_to\_H        \textcolor{comment}{! The time step times the conversion from Z to the}}
\DoxyCodeLine{203                            \textcolor{comment}{! units of thickness -\/ [T H Z-\/1 ~> s or s kg m-\/3].}}
\DoxyCodeLine{204 \textcolor{keywordtype}{  real} :: h\_neglect        \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{205                            \textcolor{comment}{! in roundoff and can be neglected [H ~> m or kg m-\/2].}}
\DoxyCodeLine{206 }
\DoxyCodeLine{207 \textcolor{keywordtype}{  real} :: stress           \textcolor{comment}{!   The surface stress times the time step, divided}}
\DoxyCodeLine{208                            \textcolor{comment}{! by the density [H L T-\/1 ~> m2 s-\/1 or kg m-\/1 s-\/1].}}
\DoxyCodeLine{209 \textcolor{keywordtype}{  real} :: zDS, hfr, h\_a    \textcolor{comment}{! Temporary variables used with direct\_stress.}}
\DoxyCodeLine{210 \textcolor{keywordtype}{  real} :: surface\_stress(SZIB\_(G))\textcolor{comment}{! The same as stress, unless the wind stress}}
\DoxyCodeLine{211                            \textcolor{comment}{! stress is applied as a body force [H L T-\/1 ~> m2 s-\/1 or kg m-\/1 s-\/1].}}
\DoxyCodeLine{212 }
\DoxyCodeLine{213 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{allocatable}, \textcolor{keywordtype}{dimension(:,:)} :: hf\_du\_dt\_visc\_2d \textcolor{comment}{! Depth sum of hf\_du\_dt\_visc [L T-\/2 ~> m s-\/2]}}
\DoxyCodeLine{214 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{allocatable}, \textcolor{keywordtype}{dimension(:,:)} :: hf\_dv\_dt\_visc\_2d \textcolor{comment}{! Depth sum of hf\_dv\_dt\_visc [L T-\/2 ~> m s-\/2]}}
\DoxyCodeLine{215 }
\DoxyCodeLine{216   \textcolor{keywordtype}{logical} :: do\_i(SZIB\_(G))}
\DoxyCodeLine{217   \textcolor{keywordtype}{logical} :: DoStokesMixing}
\DoxyCodeLine{218 }
\DoxyCodeLine{219   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, Isq, Ieq, Jsq, Jeq, nz, n}
\DoxyCodeLine{220   is = g\%isc ; ie = g\%iec; js = g\%jsc; je = g\%jec}
\DoxyCodeLine{221   isq = g\%IscB ; ieq = g\%IecB ; jsq = g\%JscB ; jeq = g\%JecB ; nz = g\%ke}
\DoxyCodeLine{222 }
\DoxyCodeLine{223   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"MOM\_vert\_friction(visc): "}// \&}
\DoxyCodeLine{224          \textcolor{stringliteral}{"Module must be initialized before it is used."})}
\DoxyCodeLine{225 }
\DoxyCodeLine{226   \textcolor{keywordflow}{if} (cs\%direct\_stress) \textcolor{keywordflow}{then}}
\DoxyCodeLine{227     hmix = cs\%Hmix\_stress}
\DoxyCodeLine{228     i\_hmix = 1.0 / hmix}
\DoxyCodeLine{229 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{230   dt\_rho0 = dt / gv\%H\_to\_RZ}
\DoxyCodeLine{231   dt\_z\_to\_h = dt*gv\%Z\_to\_H}
\DoxyCodeLine{232   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{233   idt = 1.0 / dt}
\DoxyCodeLine{234 }
\DoxyCodeLine{235   \textcolor{comment}{!Check if Stokes mixing allowed if requested (present and associated)}}
\DoxyCodeLine{236   dostokesmixing=.false.}
\DoxyCodeLine{237   \textcolor{keywordflow}{if} (cs\%StokesMixing) \textcolor{keywordflow}{then}}
\DoxyCodeLine{238     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(waves)) dostokesmixing = \textcolor{keyword}{associated}(waves)}
\DoxyCodeLine{239     \textcolor{keywordflow}{if} (.not. dostokesmixing) \&}
\DoxyCodeLine{240       \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"Stokes Mixing called without allocated"}//\&}
\DoxyCodeLine{241                      \textcolor{stringliteral}{"Waves Control Structure"})}
\DoxyCodeLine{242 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{243 }
\DoxyCodeLine{244   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq ; ray(i,k) = 0.0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{245 }
\DoxyCodeLine{246   \textcolor{comment}{!   Update the zonal velocity component using a modification of a standard}}
\DoxyCodeLine{247   \textcolor{comment}{! tridagonal solver.}}
\DoxyCodeLine{248 }
\DoxyCodeLine{249   \textcolor{comment}{!\$OMP parallel do default(shared) firstprivate(Ray) \&}}
\DoxyCodeLine{250   \textcolor{comment}{!\$OMP                 private(do\_i,surface\_stress,zDS,stress,h\_a,hfr, \&}}
\DoxyCodeLine{251   \textcolor{comment}{!\$OMP                         b\_denom\_1,b1,d1,c1)}}
\DoxyCodeLine{252   \textcolor{keywordflow}{do} j=g\%jsc,g\%jec}
\DoxyCodeLine{253     \textcolor{keywordflow}{do} i=isq,ieq ; do\_i(i) = (g\%mask2dCu(i,j) > 0) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{254 }
\DoxyCodeLine{255     \textcolor{comment}{! When mixing down Eulerian current + Stokes drift add before calling solver}}
\DoxyCodeLine{256     \textcolor{keywordflow}{if} (dostokesmixing) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{257       \textcolor{keywordflow}{if} (do\_i(i)) u(i,j,k) = u(i,j,k) + us\%m\_s\_to\_L\_T*waves\%Us\_x(i,j,k)}
\DoxyCodeLine{258 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{259 }
\DoxyCodeLine{260     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%du\_dt\_visc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{261       adp\%du\_dt\_visc(i,j,k) = u(i,j,k)}
\DoxyCodeLine{262 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{263 }
\DoxyCodeLine{264 \textcolor{comment}{!   One option is to have the wind stress applied as a body force}}
\DoxyCodeLine{265 \textcolor{comment}{! over the topmost Hmix fluid.  If DIRECT\_STRESS is not defined,}}
\DoxyCodeLine{266 \textcolor{comment}{! the wind stress is applied as a stress boundary condition.}}
\DoxyCodeLine{267     \textcolor{keywordflow}{if} (cs\%direct\_stress) \textcolor{keywordflow}{then}}
\DoxyCodeLine{268       \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{269         surface\_stress(i) = 0.0}
\DoxyCodeLine{270         zds = 0.0}
\DoxyCodeLine{271         stress = dt\_rho0 * forces\%taux(i,j)}
\DoxyCodeLine{272         \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{273           h\_a = 0.5 * (h(i,j,k) + h(i+1,j,k)) + h\_neglect}
\DoxyCodeLine{274           hfr = 1.0 ; \textcolor{keywordflow}{if} ((zds+h\_a) > hmix) hfr = (hmix -\/ zds) / h\_a}
\DoxyCodeLine{275           u(i,j,k) = u(i,j,k) + i\_hmix * hfr * stress}
\DoxyCodeLine{276           zds = zds + h\_a ; \textcolor{keywordflow}{if} (zds >= hmix) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{277 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{278 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! end of i loop}}
\DoxyCodeLine{279     \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{280       surface\_stress(i) = dt\_rho0 * (g\%mask2dCu(i,j)*forces\%taux(i,j))}
\DoxyCodeLine{281 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif} \textcolor{comment}{! direct\_stress}}
\DoxyCodeLine{282 }
\DoxyCodeLine{283     \textcolor{keywordflow}{if} (cs\%Channel\_drag) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{284       ray(i,k) = visc\%Ray\_u(i,j,k)}
\DoxyCodeLine{285 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{286 }
\DoxyCodeLine{287     \textcolor{comment}{! perform forward elimination on the tridiagonal system}}
\DoxyCodeLine{288     \textcolor{comment}{!}}
\DoxyCodeLine{289     \textcolor{comment}{! denote the diagonal of the system as b\_k, the subdiagonal as a\_k}}
\DoxyCodeLine{290     \textcolor{comment}{! and the superdiagonal as c\_k. The right-\/hand side terms are d\_k.}}
\DoxyCodeLine{291     \textcolor{comment}{!}}
\DoxyCodeLine{292     \textcolor{comment}{! ignoring the rayleigh drag contribution,}}
\DoxyCodeLine{293     \textcolor{comment}{! we have a\_k = -\/dt\_Z\_to\_H * a\_u(k)}}
\DoxyCodeLine{294     \textcolor{comment}{!         b\_k = h\_u(k) + dt\_Z\_to\_H * (a\_u(k) + a\_u(k+1))}}
\DoxyCodeLine{295     \textcolor{comment}{!         c\_k = -\/dt\_Z\_to\_H * a\_u(k+1)}}
\DoxyCodeLine{296     \textcolor{comment}{!}}
\DoxyCodeLine{297     \textcolor{comment}{! for forward elimination, we want to:}}
\DoxyCodeLine{298     \textcolor{comment}{! calculate c'\_k = -\/ c\_k                / (b\_k + a\_k c'\_(k-\/1))}}
\DoxyCodeLine{299     \textcolor{comment}{! and       d'\_k = (d\_k -\/ a\_k d'\_(k-\/1)) / (b\_k + a\_k c'\_(k-\/1))}}
\DoxyCodeLine{300     \textcolor{comment}{! where c'\_1 = c\_1/b\_1 and d'\_1 = d\_1/b\_1}}
\DoxyCodeLine{301     \textcolor{comment}{! (see Thomas' tridiagonal matrix algorithm)}}
\DoxyCodeLine{302     \textcolor{comment}{!}}
\DoxyCodeLine{303     \textcolor{comment}{! b1 is the denominator term 1 / (b\_k + a\_k c'\_(k-\/1))}}
\DoxyCodeLine{304     \textcolor{comment}{! b\_denom\_1 is (b\_k + a\_k + c\_k) -\/ a\_k(1 -\/ c'\_(k-\/1))}}
\DoxyCodeLine{305     \textcolor{comment}{!            = (b\_k + c\_k + c'\_(k-\/1))}}
\DoxyCodeLine{306     \textcolor{comment}{! this is done so that d1 = b1 * b\_denom\_1 = 1 -\/ c'\_(k-\/1)}}
\DoxyCodeLine{307     \textcolor{comment}{! c1(k) is -\/c'\_(k -\/ 1)}}
\DoxyCodeLine{308     \textcolor{comment}{! and the right-\/hand-\/side is destructively updated to be d'\_k}}
\DoxyCodeLine{309     \textcolor{comment}{!}}
\DoxyCodeLine{310     \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{311       b\_denom\_1 = cs\%h\_u(i,j,1) + dt\_z\_to\_h * (ray(i,1) + cs\%a\_u(i,j,1))}
\DoxyCodeLine{312       b1(i) = 1.0 / (b\_denom\_1 + dt\_z\_to\_h*cs\%a\_u(i,j,2))}
\DoxyCodeLine{313       d1(i) = b\_denom\_1 * b1(i)}
\DoxyCodeLine{314       u(i,j,1) = b1(i) * (cs\%h\_u(i,j,1) * u(i,j,1) + surface\_stress(i))}
\DoxyCodeLine{315 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{316     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{317       c1(i,k) = dt\_z\_to\_h * cs\%a\_u(i,j,k) * b1(i)}
\DoxyCodeLine{318       b\_denom\_1 = cs\%h\_u(i,j,k) + dt\_z\_to\_h * (ray(i,k) + cs\%a\_u(i,j,k)*d1(i))}
\DoxyCodeLine{319       b1(i) = 1.0 / (b\_denom\_1 + dt\_z\_to\_h * cs\%a\_u(i,j,k+1))}
\DoxyCodeLine{320       d1(i) = b\_denom\_1 * b1(i)}
\DoxyCodeLine{321       u(i,j,k) = (cs\%h\_u(i,j,k) * u(i,j,k) + \&}
\DoxyCodeLine{322                   dt\_z\_to\_h * cs\%a\_u(i,j,k) * u(i,j,k-\/1)) * b1(i)}
\DoxyCodeLine{323 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{324 }
\DoxyCodeLine{325     \textcolor{comment}{! back substitute to solve for the new velocities}}
\DoxyCodeLine{326     \textcolor{comment}{! u\_k = d'\_k -\/ c'\_k x\_(k+1)}}
\DoxyCodeLine{327     \textcolor{keywordflow}{do} k=nz-\/1,1,-\/1 ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{328       u(i,j,k) = u(i,j,k) + c1(i,k+1) * u(i,j,k+1)}
\DoxyCodeLine{329 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i and k loops}}
\DoxyCodeLine{330 }
\DoxyCodeLine{331     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%du\_dt\_visc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{332       adp\%du\_dt\_visc(i,j,k) = (u(i,j,k) -\/ adp\%du\_dt\_visc(i,j,k))*idt}
\DoxyCodeLine{333 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{334 }
\DoxyCodeLine{335     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%taux\_shelf)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{336       visc\%taux\_shelf(i,j) = -\/gv\%Rho0*cs\%a1\_shelf\_u(i,j)*u(i,j,1) \textcolor{comment}{! -\/ u\_shelf?}}
\DoxyCodeLine{337 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{338 }
\DoxyCodeLine{339     \textcolor{keywordflow}{if} (\textcolor{keyword}{PRESENT}(taux\_bot)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{340       \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{341         taux\_bot(i,j) = gv\%Rho0 * (u(i,j,nz)*cs\%a\_u(i,j,nz+1))}
\DoxyCodeLine{342 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{343       \textcolor{keywordflow}{if} (cs\%Channel\_drag) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{344         taux\_bot(i,j) = taux\_bot(i,j) + gv\%Rho0 * (ray(i,k)*u(i,j,k))}
\DoxyCodeLine{345 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{346 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{347 }
\DoxyCodeLine{348     \textcolor{comment}{! When mixing down Eulerian current + Stokes drift subtract after calling solver}}
\DoxyCodeLine{349     \textcolor{keywordflow}{if} (dostokesmixing) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{350       \textcolor{keywordflow}{if} (do\_i(i)) u(i,j,k) = u(i,j,k) -\/ us\%m\_s\_to\_L\_T*waves\%Us\_x(i,j,k)}
\DoxyCodeLine{351 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{352 }
\DoxyCodeLine{353 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! end u-\/component j loop}}
\DoxyCodeLine{354 }
\DoxyCodeLine{355   \textcolor{comment}{! Now work on the meridional velocity component.}}
\DoxyCodeLine{356 }
\DoxyCodeLine{357   \textcolor{comment}{!\$OMP parallel do default(shared) firstprivate(Ray) \&}}
\DoxyCodeLine{358   \textcolor{comment}{!\$OMP               private(do\_i,surface\_stress,zDS,stress,h\_a,hfr, \&}}
\DoxyCodeLine{359   \textcolor{comment}{!\$OMP                       b\_denom\_1,b1,d1,c1)}}
\DoxyCodeLine{360   \textcolor{keywordflow}{do} j=jsq,jeq}
\DoxyCodeLine{361     \textcolor{keywordflow}{do} i=is,ie ; do\_i(i) = (g\%mask2dCv(i,j) > 0) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{362 }
\DoxyCodeLine{363     \textcolor{comment}{! When mixing down Eulerian current + Stokes drift add before calling solver}}
\DoxyCodeLine{364     \textcolor{keywordflow}{if} (dostokesmixing) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{365       \textcolor{keywordflow}{if} (do\_i(i)) v(i,j,k) = v(i,j,k) + us\%m\_s\_to\_L\_T*waves\%Us\_y(i,j,k)}
\DoxyCodeLine{366 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{367 }
\DoxyCodeLine{368     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%dv\_dt\_visc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{369       adp\%dv\_dt\_visc(i,j,k) = v(i,j,k)}
\DoxyCodeLine{370 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{371 }
\DoxyCodeLine{372 \textcolor{comment}{!   One option is to have the wind stress applied as a body force}}
\DoxyCodeLine{373 \textcolor{comment}{! over the topmost Hmix fluid.  If DIRECT\_STRESS is not defined,}}
\DoxyCodeLine{374 \textcolor{comment}{! the wind stress is applied as a stress boundary condition.}}
\DoxyCodeLine{375     \textcolor{keywordflow}{if} (cs\%direct\_stress) \textcolor{keywordflow}{then}}
\DoxyCodeLine{376       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{377         surface\_stress(i) = 0.0}
\DoxyCodeLine{378         zds = 0.0}
\DoxyCodeLine{379         stress = dt\_rho0 * forces\%tauy(i,j)}
\DoxyCodeLine{380         \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{381           h\_a = 0.5 * (h(i,j,k) + h(i,j+1,k)) + h\_neglect}
\DoxyCodeLine{382           hfr = 1.0 ; \textcolor{keywordflow}{if} ((zds+h\_a) > hmix) hfr = (hmix -\/ zds) / h\_a}
\DoxyCodeLine{383           v(i,j,k) = v(i,j,k) + i\_hmix * hfr * stress}
\DoxyCodeLine{384           zds = zds + h\_a ; \textcolor{keywordflow}{if} (zds >= hmix) \textcolor{keywordflow}{exit}}
\DoxyCodeLine{385 \textcolor{keywordflow}{        enddo}}
\DoxyCodeLine{386 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! end of i loop}}
\DoxyCodeLine{387     \textcolor{keywordflow}{else} ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{388       surface\_stress(i) = dt\_rho0 * (g\%mask2dCv(i,j)*forces\%tauy(i,j))}
\DoxyCodeLine{389 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif} \textcolor{comment}{! direct\_stress}}
\DoxyCodeLine{390 }
\DoxyCodeLine{391     \textcolor{keywordflow}{if} (cs\%Channel\_drag) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{392       ray(i,k) = visc\%Ray\_v(i,j,k)}
\DoxyCodeLine{393 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{394 }
\DoxyCodeLine{395     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{396       b\_denom\_1 = cs\%h\_v(i,j,1) + dt\_z\_to\_h * (ray(i,1) + cs\%a\_v(i,j,1))}
\DoxyCodeLine{397       b1(i) = 1.0 / (b\_denom\_1 + dt\_z\_to\_h*cs\%a\_v(i,j,2))}
\DoxyCodeLine{398       d1(i) = b\_denom\_1 * b1(i)}
\DoxyCodeLine{399       v(i,j,1) = b1(i) * (cs\%h\_v(i,j,1) * v(i,j,1) + surface\_stress(i))}
\DoxyCodeLine{400 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{401     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{402       c1(i,k) = dt\_z\_to\_h * cs\%a\_v(i,j,k) * b1(i)}
\DoxyCodeLine{403       b\_denom\_1 = cs\%h\_v(i,j,k) + dt\_z\_to\_h * (ray(i,k) + cs\%a\_v(i,j,k)*d1(i))}
\DoxyCodeLine{404       b1(i) = 1.0 / (b\_denom\_1 + dt\_z\_to\_h * cs\%a\_v(i,j,k+1))}
\DoxyCodeLine{405       d1(i) = b\_denom\_1 * b1(i)}
\DoxyCodeLine{406       v(i,j,k) = (cs\%h\_v(i,j,k) * v(i,j,k) + dt\_z\_to\_h * cs\%a\_v(i,j,k) * v(i,j,k-\/1)) * b1(i)}
\DoxyCodeLine{407 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{408     \textcolor{keywordflow}{do} k=nz-\/1,1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{409       v(i,j,k) = v(i,j,k) + c1(i,k+1) * v(i,j,k+1)}
\DoxyCodeLine{410 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i and k loops}}
\DoxyCodeLine{411 }
\DoxyCodeLine{412     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(adp\%dv\_dt\_visc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{413       adp\%dv\_dt\_visc(i,j,k) = (v(i,j,k) -\/ adp\%dv\_dt\_visc(i,j,k))*idt}
\DoxyCodeLine{414 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{415 }
\DoxyCodeLine{416     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%tauy\_shelf)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{417       visc\%tauy\_shelf(i,j) = -\/gv\%Rho0*cs\%a1\_shelf\_v(i,j)*v(i,j,1) \textcolor{comment}{! -\/ v\_shelf?}}
\DoxyCodeLine{418 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{419 }
\DoxyCodeLine{420     \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(tauy\_bot)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{421       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{422         tauy\_bot(i,j) = gv\%Rho0 * (v(i,j,nz)*cs\%a\_v(i,j,nz+1))}
\DoxyCodeLine{423 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{424       \textcolor{keywordflow}{if} (cs\%Channel\_drag) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{425         tauy\_bot(i,j) = tauy\_bot(i,j) + gv\%Rho0 * (ray(i,k)*v(i,j,k))}
\DoxyCodeLine{426 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{427 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{428 }
\DoxyCodeLine{429     \textcolor{comment}{! When mixing down Eulerian current + Stokes drift subtract after calling solver}}
\DoxyCodeLine{430     \textcolor{keywordflow}{if} (dostokesmixing) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{431       \textcolor{keywordflow}{if} (do\_i(i)) v(i,j,k) = v(i,j,k) -\/ us\%m\_s\_to\_L\_T*waves\%Us\_y(i,j,k)}
\DoxyCodeLine{432 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{433 }
\DoxyCodeLine{434 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! end of v-\/component J loop}}
\DoxyCodeLine{435 }
\DoxyCodeLine{436   \textcolor{keyword}{call }vertvisc\_limit\_vel(u, v, h, adp, cdp, forces, visc, dt, g, gv, us, cs)}
\DoxyCodeLine{437 }
\DoxyCodeLine{438   \textcolor{comment}{! Here the velocities associated with open boundary conditions are applied.}}
\DoxyCodeLine{439   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(obc)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{440     \textcolor{keywordflow}{do} n=1,obc\%number\_of\_segments}
\DoxyCodeLine{441       \textcolor{keywordflow}{if} (obc\%segment(n)\%specified) \textcolor{keywordflow}{then}}
\DoxyCodeLine{442         \textcolor{keywordflow}{if} (obc\%segment(n)\%is\_N\_or\_S) \textcolor{keywordflow}{then}}
\DoxyCodeLine{443           j = obc\%segment(n)\%HI\%JsdB}
\DoxyCodeLine{444           \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=obc\%segment(n)\%HI\%isd,obc\%segment(n)\%HI\%ied}
\DoxyCodeLine{445             v(i,j,k) = obc\%segment(n)\%normal\_vel(i,j,k)}
\DoxyCodeLine{446 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{447         \textcolor{keywordflow}{elseif} (obc\%segment(n)\%is\_E\_or\_W) \textcolor{keywordflow}{then}}
\DoxyCodeLine{448           i = obc\%segment(n)\%HI\%IsdB}
\DoxyCodeLine{449           \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=obc\%segment(n)\%HI\%jsd,obc\%segment(n)\%HI\%jed}
\DoxyCodeLine{450             u(i,j,k) = obc\%segment(n)\%normal\_vel(i,j,k)}
\DoxyCodeLine{451 \textcolor{keywordflow}{          enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{452 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{453 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{454 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{455 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{456 }
\DoxyCodeLine{457 \textcolor{comment}{! Offer diagnostic fields for averaging.}}
\DoxyCodeLine{458   \textcolor{keywordflow}{if} (cs\%id\_du\_dt\_visc > 0) \&}
\DoxyCodeLine{459     \textcolor{keyword}{call }post\_data(cs\%id\_du\_dt\_visc, adp\%du\_dt\_visc, cs\%diag)}
\DoxyCodeLine{460   \textcolor{keywordflow}{if} (cs\%id\_dv\_dt\_visc > 0) \&}
\DoxyCodeLine{461     \textcolor{keyword}{call }post\_data(cs\%id\_dv\_dt\_visc, adp\%dv\_dt\_visc, cs\%diag)}
\DoxyCodeLine{462   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(taux\_bot) .and. (cs\%id\_taux\_bot > 0)) \&}
\DoxyCodeLine{463     \textcolor{keyword}{call }post\_data(cs\%id\_taux\_bot, taux\_bot, cs\%diag)}
\DoxyCodeLine{464   \textcolor{keywordflow}{if} (\textcolor{keyword}{present}(tauy\_bot) .and. (cs\%id\_tauy\_bot > 0)) \&}
\DoxyCodeLine{465     \textcolor{keyword}{call }post\_data(cs\%id\_tauy\_bot, tauy\_bot, cs\%diag)}
\DoxyCodeLine{466 }
\DoxyCodeLine{467   \textcolor{comment}{! Diagnostics for terms multiplied by fractional thicknesses}}
\DoxyCodeLine{468 }
\DoxyCodeLine{469   \textcolor{comment}{! 3D diagnostics hf\_du(dv)\_dt\_visc are commented because there is no clarity on proper remapping grid option.}}
\DoxyCodeLine{470   \textcolor{comment}{! The code is retained for degugging purposes in the future.}}
\DoxyCodeLine{471   \textcolor{comment}{!if (CS\%id\_hf\_du\_dt\_visc > 0) then}}
\DoxyCodeLine{472   \textcolor{comment}{!  do k=1,nz ; do j=js,je ; do I=Isq,Ieq}}
\DoxyCodeLine{473   \textcolor{comment}{!    CS\%hf\_du\_dt\_visc(I,j,k) = ADp\%du\_dt\_visc(I,j,k) * ADp\%diag\_hfrac\_u(I,j,k)}}
\DoxyCodeLine{474   \textcolor{comment}{!  enddo ; enddo ; enddo}}
\DoxyCodeLine{475   \textcolor{comment}{!  call post\_data(CS\%id\_hf\_du\_dt\_visc, CS\%hf\_du\_dt\_visc, CS\%diag)}}
\DoxyCodeLine{476   \textcolor{comment}{!endif}}
\DoxyCodeLine{477   \textcolor{comment}{!if (CS\%id\_hf\_dv\_dt\_visc > 0) then}}
\DoxyCodeLine{478   \textcolor{comment}{!  do k=1,nz ; do J=Jsq,Jeq ; do i=is,ie}}
\DoxyCodeLine{479   \textcolor{comment}{!    CS\%hf\_dv\_dt\_visc(i,J,k) = ADp\%dv\_dt\_visc(i,J,k) * ADp\%diag\_hfrac\_v(i,J,k)}}
\DoxyCodeLine{480   \textcolor{comment}{!  enddo ; enddo ; enddo}}
\DoxyCodeLine{481   \textcolor{comment}{!  call post\_data(CS\%id\_hf\_dv\_dt\_visc, CS\%hf\_dv\_dt\_visc, CS\%diag)}}
\DoxyCodeLine{482   \textcolor{comment}{!endif}}
\DoxyCodeLine{483   \textcolor{keywordflow}{if} (cs\%id\_hf\_du\_dt\_visc\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{484     \textcolor{keyword}{allocate}(hf\_du\_dt\_visc\_2d(g\%IsdB:g\%IedB,g\%jsd:g\%jed))}
\DoxyCodeLine{485     hf\_du\_dt\_visc\_2d(:,:) = 0.0}
\DoxyCodeLine{486     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{487       hf\_du\_dt\_visc\_2d(i,j) = hf\_du\_dt\_visc\_2d(i,j) + adp\%du\_dt\_visc(i,j,k) * adp\%diag\_hfrac\_u(i,j,k)}
\DoxyCodeLine{488 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{489     \textcolor{keyword}{call }post\_data(cs\%id\_hf\_du\_dt\_visc\_2d, hf\_du\_dt\_visc\_2d, cs\%diag)}
\DoxyCodeLine{490     \textcolor{keyword}{deallocate}(hf\_du\_dt\_visc\_2d)}
\DoxyCodeLine{491 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{492   \textcolor{keywordflow}{if} (cs\%id\_hf\_dv\_dt\_visc\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{493     \textcolor{keyword}{allocate}(hf\_dv\_dt\_visc\_2d(g\%isd:g\%ied,g\%JsdB:g\%JedB))}
\DoxyCodeLine{494     hf\_dv\_dt\_visc\_2d(:,:) = 0.0}
\DoxyCodeLine{495     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=jsq,jeq ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{496       hf\_dv\_dt\_visc\_2d(i,j) = hf\_dv\_dt\_visc\_2d(i,j) + adp\%dv\_dt\_visc(i,j,k) * adp\%diag\_hfrac\_v(i,j,k)}
\DoxyCodeLine{497 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{498     \textcolor{keyword}{call }post\_data(cs\%id\_hf\_dv\_dt\_visc\_2d, hf\_dv\_dt\_visc\_2d, cs\%diag)}
\DoxyCodeLine{499     \textcolor{keyword}{deallocate}(hf\_dv\_dt\_visc\_2d)}
\DoxyCodeLine{500 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{501 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__vert__friction_ac281f6595593b33436594112785e982b}\label{namespacemom__vert__friction_ac281f6595593b33436594112785e982b}} 
\index{mom\_vert\_friction@{mom\_vert\_friction}!vertvisc\_coef@{vertvisc\_coef}}
\index{vertvisc\_coef@{vertvisc\_coef}!mom\_vert\_friction@{mom\_vert\_friction}}
\doxysubsubsection{\texorpdfstring{vertvisc\_coef()}{vertvisc\_coef()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+vert\+\_\+friction\+::vertvisc\+\_\+coef (\begin{DoxyParamCaption}\item[{real, dimension(szib\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{u,  }\item[{real, dimension(szi\+\_\+(g),szjb\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{v,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{h,  }\item[{type(mech\+\_\+forcing), intent(in)}]{forces,  }\item[{type(vertvisc\+\_\+type), intent(in)}]{visc,  }\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__vert__friction_1_1vertvisc__cs}{vertvisc\+\_\+cs}}), pointer}]{CS,  }\item[{type(ocean\+\_\+obc\+\_\+type), pointer}]{O\+BC }\end{DoxyParamCaption})}



Calculate the coupling coefficients (CSa\+\_\+u and CSa\+\_\+v) and effective layer thicknesses (CSh\+\_\+u and CSh\+\_\+v) for later use in the applying the implicit vertical viscosity via \mbox{\hyperlink{namespacemom__vert__friction_a8f1a390fa24fbe985068fed9ac26873c}{vertvisc()}}. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & Ocean grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & Ocean vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em u} & Zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em v} & Meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em forces} & A structure with the driving mechanical forces \\
\hline
\mbox{\texttt{ in}}  & {\em visc} & Viscosities and bottom drag \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
 & {\em cs} & Vertical viscosity control structure \\
\hline
 & {\em obc} & Open boundary condition structure \\
\hline
\end{DoxyParams}


Definition at line 618 of file M\+O\+M\+\_\+vert\+\_\+friction.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{618   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{      !< Ocean grid structure}}
\DoxyCodeLine{619   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{     !< Ocean vertical grid structure}}
\DoxyCodeLine{620   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{     !< A dimensional unit scaling type}}
\DoxyCodeLine{621 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{622                            \textcolor{keywordtype}{intent(in)}    :: u\textcolor{comment}{      !< Zonal velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{623 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{624                            \textcolor{keywordtype}{intent(in)}    :: v\textcolor{comment}{      !< Meridional velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{625 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{626                            \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{      !< Layer thickness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{627   \textcolor{keywordtype}{type}(mech\_forcing),      \textcolor{keywordtype}{intent(in)}    :: forces\textcolor{comment}{ !< A structure with the driving mechanical forces}}
\DoxyCodeLine{628   \textcolor{keywordtype}{type}(vertvisc\_type),     \textcolor{keywordtype}{intent(in)}    :: visc\textcolor{comment}{   !< Viscosities and bottom drag}}
\DoxyCodeLine{629 \textcolor{keywordtype}{  real},                    \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{     !< Time increment [T ~> s]}}
\DoxyCodeLine{630   \textcolor{keywordtype}{type}(vertvisc\_CS),       \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{     !< Vertical viscosity control structure}}
\DoxyCodeLine{631   \textcolor{keywordtype}{type}(ocean\_OBC\_type),    \textcolor{keywordtype}{pointer}       :: OBC\textcolor{comment}{    !< Open boundary condition structure}}
\DoxyCodeLine{632 }
\DoxyCodeLine{633   \textcolor{comment}{! Field from forces used in this subroutine:}}
\DoxyCodeLine{634   \textcolor{comment}{!   ustar: the friction velocity [Z T-\/1 ~> m s-\/1], used here as the mixing}}
\DoxyCodeLine{635   \textcolor{comment}{!     velocity in the mixed layer if NKML > 1 in a bulk mixed layer.}}
\DoxyCodeLine{636 }
\DoxyCodeLine{637   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{638 }
\DoxyCodeLine{639 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZK\_(G))} :: \&}
\DoxyCodeLine{640     h\_harm, \&   \textcolor{comment}{! Harmonic mean of the thicknesses around a velocity grid point,}}
\DoxyCodeLine{641                 \textcolor{comment}{! given by 2*(h+ * h-\/)/(h+ + h-\/) [H ~> m or kg m-\/2].}}
\DoxyCodeLine{642     h\_arith, \&  \textcolor{comment}{! The arithmetic mean thickness [H ~> m or kg m-\/2].}}
\DoxyCodeLine{643     h\_delta, \&  \textcolor{comment}{! The lateral difference of thickness [H ~> m or kg m-\/2].}}
\DoxyCodeLine{644     hvel, \&     \textcolor{comment}{! hvel is the thickness used at a velocity grid point [H ~> m or kg m-\/2].}}
\DoxyCodeLine{645     hvel\_shelf  \textcolor{comment}{! The equivalent of hvel under shelves [H ~> m or kg m-\/2].}}
\DoxyCodeLine{646 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZK\_(G)+1)} :: \&}
\DoxyCodeLine{647     a\_cpl, \&    \textcolor{comment}{! The drag coefficients across interfaces [Z T-\/1 ~> m s-\/1].  a\_cpl times}}
\DoxyCodeLine{648                 \textcolor{comment}{! the velocity difference gives the stress across an interface.}}
\DoxyCodeLine{649     a\_shelf, \&  \textcolor{comment}{! The drag coefficients across interfaces in water columns under}}
\DoxyCodeLine{650                 \textcolor{comment}{! ice shelves [Z T-\/1 ~> m s-\/1].}}
\DoxyCodeLine{651     z\_i         \textcolor{comment}{! An estimate of each interface's height above the bottom,}}
\DoxyCodeLine{652                 \textcolor{comment}{! normalized by the bottom boundary layer thickness, nondim.}}
\DoxyCodeLine{653 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G))} :: \&}
\DoxyCodeLine{654     kv\_bbl, \&     \textcolor{comment}{! The bottom boundary layer viscosity [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{655     bbl\_thick, \&  \textcolor{comment}{! The bottom boundary layer thickness [H ~> m or kg m-\/2].}}
\DoxyCodeLine{656     I\_Hbbl, \&     \textcolor{comment}{! The inverse of the bottom boundary layer thickness [H-\/1 ~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{657     I\_Htbl, \&     \textcolor{comment}{! The inverse of the top boundary layer thickness [H-\/1 ~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{658     zcol1, \&      \textcolor{comment}{! The height of the interfaces to the north and south of a}}
\DoxyCodeLine{659     zcol2, \&      \textcolor{comment}{! v-\/point [H ~> m or kg m-\/2].}}
\DoxyCodeLine{660     Ztop\_min, \&   \textcolor{comment}{! The deeper of the two adjacent surface heights [H ~> m or kg m-\/2].}}
\DoxyCodeLine{661     Dmin, \&       \textcolor{comment}{! The shallower of the two adjacent bottom depths converted to}}
\DoxyCodeLine{662                   \textcolor{comment}{! thickness units [H ~> m or kg m-\/2].}}
\DoxyCodeLine{663     zh, \&         \textcolor{comment}{! An estimate of the interface's distance from the bottom}}
\DoxyCodeLine{664                   \textcolor{comment}{! based on harmonic mean thicknesses [H ~> m or kg m-\/2].}}
\DoxyCodeLine{665     h\_ml          \textcolor{comment}{! The mixed layer depth [H ~> m or kg m-\/2].}}
\DoxyCodeLine{666 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{allocatable}, \textcolor{keywordtype}{dimension(:,:)} :: hML\_u \textcolor{comment}{! Diagnostic of the mixed layer depth at u points [H ~> m or kg m-\/2].}}
\DoxyCodeLine{667 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{allocatable}, \textcolor{keywordtype}{dimension(:,:)} :: hML\_v \textcolor{comment}{! Diagnostic of the mixed layer depth at v points [H ~> m or kg m-\/2].}}
\DoxyCodeLine{668 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{allocatable}, \textcolor{keywordtype}{dimension(:,:,:)} :: Kv\_u\textcolor{comment}{ !< Total vertical viscosity at u-\/points [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{669 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{allocatable}, \textcolor{keywordtype}{dimension(:,:,:)} :: Kv\_v\textcolor{comment}{ !< Total vertical viscosity at v-\/points [Z2 T-\/1 ~> m2 s-\/1].}}
\DoxyCodeLine{670 \textcolor{keywordtype}{  real} :: zcol(SZI\_(G)) \textcolor{comment}{! The height of an interface at h-\/points [H ~> m or kg m-\/2].}}
\DoxyCodeLine{671 \textcolor{keywordtype}{  real} :: botfn   \textcolor{comment}{! A function which goes from 1 at the bottom to 0 much more}}
\DoxyCodeLine{672                   \textcolor{comment}{! than Hbbl into the interior.}}
\DoxyCodeLine{673 \textcolor{keywordtype}{  real} :: topfn   \textcolor{comment}{! A function which goes from 1 at the top to 0 much more}}
\DoxyCodeLine{674                   \textcolor{comment}{! than Htbl into the interior.}}
\DoxyCodeLine{675 \textcolor{keywordtype}{  real} :: z2      \textcolor{comment}{! The distance from the bottom, normalized by Hbbl, nondim.}}
\DoxyCodeLine{676 \textcolor{keywordtype}{  real} :: z2\_wt   \textcolor{comment}{! A nondimensional (0-\/1) weight used when calculating z2.}}
\DoxyCodeLine{677 \textcolor{keywordtype}{  real} :: z\_clear \textcolor{comment}{! The clearance of an interface above the surrounding topography [H ~> m or kg m-\/2].}}
\DoxyCodeLine{678 \textcolor{keywordtype}{  real} :: a\_cpl\_max  \textcolor{comment}{! The maximum drag doefficient across interfaces, set so that it will be}}
\DoxyCodeLine{679                      \textcolor{comment}{! representable as a 32-\/bit float in MKS units  [Z T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{680 \textcolor{keywordtype}{  real} :: h\_neglect  \textcolor{comment}{! A thickness that is so small it is usually lost}}
\DoxyCodeLine{681                      \textcolor{comment}{! in roundoff and can be neglected [H ~> m or kg m-\/2].}}
\DoxyCodeLine{682 }
\DoxyCodeLine{683 \textcolor{keywordtype}{  real} :: I\_valBL \textcolor{comment}{! The inverse of a scaling factor determining when water is}}
\DoxyCodeLine{684                   \textcolor{comment}{! still within the boundary layer, as determined by the sum}}
\DoxyCodeLine{685                   \textcolor{comment}{! of the harmonic mean thicknesses.}}
\DoxyCodeLine{686   \textcolor{keywordtype}{logical}, \textcolor{keywordtype}{dimension(SZIB\_(G))} :: do\_i, do\_i\_shelf}
\DoxyCodeLine{687   \textcolor{keywordtype}{logical} :: do\_any\_shelf}
\DoxyCodeLine{688   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{dimension(SZIB\_(G))} :: \&}
\DoxyCodeLine{689     zi\_dir   \textcolor{comment}{!  A trinary logical array indicating which thicknesses to use for}}
\DoxyCodeLine{690              \textcolor{comment}{!  finding z\_clear.}}
\DoxyCodeLine{691   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, Isq, Ieq, Jsq, Jeq, nz}
\DoxyCodeLine{692   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec}
\DoxyCodeLine{693   isq = g\%IscB ; ieq = g\%IecB ; jsq = g\%JscB ; jeq = g\%JecB ; nz = g\%ke}
\DoxyCodeLine{694 }
\DoxyCodeLine{695   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"MOM\_vert\_friction(coef): "}// \&}
\DoxyCodeLine{696          \textcolor{stringliteral}{"Module must be initialized before it is used."})}
\DoxyCodeLine{697 }
\DoxyCodeLine{698   h\_neglect = gv\%H\_subroundoff}
\DoxyCodeLine{699   a\_cpl\_max = 1.0e37 * us\%m\_to\_Z * us\%T\_to\_s}
\DoxyCodeLine{700   i\_hbbl(:) = 1.0 / (cs\%Hbbl + h\_neglect)}
\DoxyCodeLine{701   i\_valbl = 0.0 ; \textcolor{keywordflow}{if} (cs\%harm\_BL\_val > 0.0) i\_valbl = 1.0 / cs\%harm\_BL\_val}
\DoxyCodeLine{702 }
\DoxyCodeLine{703   \textcolor{keywordflow}{if} (cs\%id\_Kv\_u > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{704     \textcolor{keyword}{allocate}(kv\_u(g\%IsdB:g\%IedB,g\%jsd:g\%jed,g\%ke)) ; kv\_u(:,:,:) = 0.0}
\DoxyCodeLine{705 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{706 }
\DoxyCodeLine{707   \textcolor{keywordflow}{if} (cs\%id\_Kv\_v > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{708     \textcolor{keyword}{allocate}(kv\_v(g\%isd:g\%ied,g\%JsdB:g\%JedB,g\%ke)) ; kv\_v(:,:,:) = 0.0}
\DoxyCodeLine{709 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{710 }
\DoxyCodeLine{711   \textcolor{keywordflow}{if} (cs\%debug .or. (cs\%id\_hML\_u > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{712     \textcolor{keyword}{allocate}(hml\_u(g\%IsdB:g\%IedB,g\%jsd:g\%jed)) ; hml\_u(:,:) = 0.0}
\DoxyCodeLine{713 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{714   \textcolor{keywordflow}{if} (cs\%debug .or. (cs\%id\_hML\_v > 0)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{715     \textcolor{keyword}{allocate}(hml\_v(g\%isd:g\%ied,g\%JsdB:g\%JedB)) ; hml\_v(:,:) = 0.0}
\DoxyCodeLine{716 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{717 }
\DoxyCodeLine{718   \textcolor{keywordflow}{if} ((\textcolor{keyword}{associated}(visc\%taux\_shelf) .or. \textcolor{keyword}{associated}(forces\%frac\_shelf\_u)) .and. \&}
\DoxyCodeLine{719       .not.\textcolor{keyword}{associated}(cs\%a1\_shelf\_u)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{720     \textcolor{keyword}{allocate}(cs\%a1\_shelf\_u(g\%IsdB:g\%IedB,g\%jsd:g\%jed)) ; cs\%a1\_shelf\_u(:,:)=0.0}
\DoxyCodeLine{721 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{722   \textcolor{keywordflow}{if} ((\textcolor{keyword}{associated}(visc\%tauy\_shelf) .or. \textcolor{keyword}{associated}(forces\%frac\_shelf\_v)) .and. \&}
\DoxyCodeLine{723       .not.\textcolor{keyword}{associated}(cs\%a1\_shelf\_v)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{724     \textcolor{keyword}{allocate}(cs\%a1\_shelf\_v(g\%isd:g\%ied,g\%JsdB:g\%JedB)) ; cs\%a1\_shelf\_v(:,:)=0.0}
\DoxyCodeLine{725 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{726 }
\DoxyCodeLine{727   \textcolor{comment}{!\$OMP parallel do default(private) shared(G,GV,US,CS,visc,Isq,Ieq,nz,u,h,forces,hML\_u, \&}}
\DoxyCodeLine{728   \textcolor{comment}{!\$OMP                                     OBC,h\_neglect,dt,I\_valBL,Kv\_u,a\_cpl\_max) \&}}
\DoxyCodeLine{729   \textcolor{comment}{!\$OMP                     firstprivate(i\_hbbl)}}
\DoxyCodeLine{730   \textcolor{keywordflow}{do} j=g\%Jsc,g\%Jec}
\DoxyCodeLine{731     \textcolor{keywordflow}{do} i=isq,ieq ; do\_i(i) = (g\%mask2dCu(i,j) > 0) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{732 }
\DoxyCodeLine{733     \textcolor{keywordflow}{if} (cs\%bottomdraglaw) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{734       kv\_bbl(i) = visc\%Kv\_bbl\_u(i,j)}
\DoxyCodeLine{735       bbl\_thick(i) = visc\%bbl\_thick\_u(i,j) * gv\%Z\_to\_H + h\_neglect}
\DoxyCodeLine{736       \textcolor{keywordflow}{if} (do\_i(i)) i\_hbbl(i) = 1.0 / bbl\_thick(i)}
\DoxyCodeLine{737 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{738 }
\DoxyCodeLine{739     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{740       h\_harm(i,k) = 2.0*h(i,j,k)*h(i+1,j,k) / (h(i,j,k)+h(i+1,j,k)+h\_neglect)}
\DoxyCodeLine{741       h\_arith(i,k) = 0.5*(h(i+1,j,k)+h(i,j,k))}
\DoxyCodeLine{742       h\_delta(i,k) = h(i+1,j,k) -\/ h(i,j,k)}
\DoxyCodeLine{743 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{744     \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{745       dmin(i) = min(g\%bathyT(i,j), g\%bathyT(i+1,j)) * gv\%Z\_to\_H}
\DoxyCodeLine{746       zi\_dir(i) = 0}
\DoxyCodeLine{747 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{748 }
\DoxyCodeLine{749     \textcolor{comment}{! Project thickness outward across OBCs using a zero-\/gradient condition.}}
\DoxyCodeLine{750     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(obc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (obc\%number\_of\_segments > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{751       \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i) .and. (obc\%segnum\_u(i,j) /= obc\_none)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{752         \textcolor{keywordflow}{if} (obc\%segment(obc\%segnum\_u(i,j))\%direction == obc\_direction\_e) \textcolor{keywordflow}{then}}
\DoxyCodeLine{753           \textcolor{keywordflow}{do} k=1,nz ; h\_harm(i,k) = h(i,j,k) ; h\_arith(i,k) = h(i,j,k) ; h\_delta(i,k) = 0. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{754           dmin(i) = g\%bathyT(i,j) * gv\%Z\_to\_H}
\DoxyCodeLine{755           zi\_dir(i) = -\/1}
\DoxyCodeLine{756         \textcolor{keywordflow}{elseif} (obc\%segment(obc\%segnum\_u(i,j))\%direction == obc\_direction\_w) \textcolor{keywordflow}{then}}
\DoxyCodeLine{757           \textcolor{keywordflow}{do} k=1,nz ; h\_harm(i,k) = h(i+1,j,k) ; h\_arith(i,k) = h(i+1,j,k) ; h\_delta(i,k) = 0. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{758           dmin(i) = g\%bathyT(i+1,j) * gv\%Z\_to\_H}
\DoxyCodeLine{759           zi\_dir(i) = 1}
\DoxyCodeLine{760 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{761 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{762 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{763 }
\DoxyCodeLine{764 \textcolor{comment}{!    The following block calculates the thicknesses at velocity}}
\DoxyCodeLine{765 \textcolor{comment}{!  grid points for the vertical viscosity (hvel).  Near the}}
\DoxyCodeLine{766 \textcolor{comment}{!  bottom an upwind biased thickness is used to control the effect}}
\DoxyCodeLine{767 \textcolor{comment}{!  of spurious Montgomery potential gradients at the bottom where}}
\DoxyCodeLine{768 \textcolor{comment}{!  nearly massless layers layers ride over the topography.}}
\DoxyCodeLine{769     \textcolor{keywordflow}{if} (cs\%harmonic\_visc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{770       \textcolor{keywordflow}{do} i=isq,ieq ; z\_i(i,nz+1) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{771       \textcolor{keywordflow}{do} k=nz,1,-\/1 ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{772         hvel(i,k) = h\_harm(i,k)}
\DoxyCodeLine{773         \textcolor{keywordflow}{if} (u(i,j,k) * h\_delta(i,k) < 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{774           z2 = z\_i(i,k+1) ; botfn = 1.0 / (1.0 + 0.09*z2*z2*z2*z2*z2*z2)}
\DoxyCodeLine{775           hvel(i,k) = (1.0-\/botfn)*h\_harm(i,k) + botfn*h\_arith(i,k)}
\DoxyCodeLine{776 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{777         z\_i(i,k) =  z\_i(i,k+1) + h\_harm(i,k)*i\_hbbl(i)}
\DoxyCodeLine{778 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i \& k loops}}
\DoxyCodeLine{779     \textcolor{keywordflow}{else} \textcolor{comment}{! Not harmonic\_visc}}
\DoxyCodeLine{780       \textcolor{keywordflow}{do} i=isq,ieq ; zh(i) = 0.0 ; z\_i(i,nz+1) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{781       \textcolor{keywordflow}{do} i=isq,ieq+1 ; zcol(i) = -\/g\%bathyT(i,j) * gv\%Z\_to\_H ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{782       \textcolor{keywordflow}{do} k=nz,1,-\/1}
\DoxyCodeLine{783         \textcolor{keywordflow}{do} i=isq,ieq+1 ; zcol(i) = zcol(i) + h(i,j,k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{784         \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{785           zh(i) = zh(i) + h\_harm(i,k)}
\DoxyCodeLine{786 }
\DoxyCodeLine{787           z\_clear = max(zcol(i),zcol(i+1)) + dmin(i)}
\DoxyCodeLine{788           \textcolor{keywordflow}{if} (zi\_dir(i) < 0) z\_clear = zcol(i) + dmin(i)}
\DoxyCodeLine{789           \textcolor{keywordflow}{if} (zi\_dir(i) > 0) z\_clear = zcol(i+1) + dmin(i)}
\DoxyCodeLine{790 }
\DoxyCodeLine{791           z\_i(i,k) = max(zh(i), z\_clear) * i\_hbbl(i)}
\DoxyCodeLine{792 }
\DoxyCodeLine{793           hvel(i,k) = h\_arith(i,k)}
\DoxyCodeLine{794           \textcolor{keywordflow}{if} (u(i,j,k) * h\_delta(i,k) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{795             \textcolor{keywordflow}{if} (zh(i) * i\_hbbl(i) < cs\%harm\_BL\_val) \textcolor{keywordflow}{then}}
\DoxyCodeLine{796               hvel(i,k) = h\_harm(i,k)}
\DoxyCodeLine{797             \textcolor{keywordflow}{else}}
\DoxyCodeLine{798               z2\_wt = 1.0  ; \textcolor{keywordflow}{if} (zh(i) * i\_hbbl(i) < 2.0*cs\%harm\_BL\_val) \&}
\DoxyCodeLine{799                 z2\_wt = max(0.0, min(1.0, zh(i) * i\_hbbl(i) * i\_valbl -\/ 1.0))}
\DoxyCodeLine{800               z2 = z2\_wt * (max(zh(i), z\_clear) * i\_hbbl(i))}
\DoxyCodeLine{801               botfn = 1.0 / (1.0 + 0.09*z2*z2*z2*z2*z2*z2)}
\DoxyCodeLine{802               hvel(i,k) = (1.0-\/botfn)*h\_arith(i,k) + botfn*h\_harm(i,k)}
\DoxyCodeLine{803 \textcolor{keywordflow}{            endif}}
\DoxyCodeLine{804 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{805 }
\DoxyCodeLine{806 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i  loop}}
\DoxyCodeLine{807 \textcolor{keywordflow}{      enddo} \textcolor{comment}{! k loop}}
\DoxyCodeLine{808 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{809 }
\DoxyCodeLine{810     \textcolor{keyword}{call }find\_coupling\_coef(a\_cpl, hvel, do\_i, h\_harm, bbl\_thick, kv\_bbl, z\_i, h\_ml, \&}
\DoxyCodeLine{811                             dt, j, g, gv, us, cs, visc, forces, work\_on\_u=.true., obc=obc)}
\DoxyCodeLine{812     \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(hml\_u)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{813       \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then} ; hml\_u(i,j) = h\_ml(i) ;\textcolor{keywordflow}{ endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{814 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{815 }
\DoxyCodeLine{816     do\_any\_shelf = .false.}
\DoxyCodeLine{817     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(forces\%frac\_shelf\_u)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{818       \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{819         cs\%a1\_shelf\_u(i,j) = 0.0}
\DoxyCodeLine{820         do\_i\_shelf(i) = (do\_i(i) .and. forces\%frac\_shelf\_u(i,j) > 0.0)}
\DoxyCodeLine{821         \textcolor{keywordflow}{if} (do\_i\_shelf(i)) do\_any\_shelf = .true.}
\DoxyCodeLine{822 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{823       \textcolor{keywordflow}{if} (do\_any\_shelf) \textcolor{keywordflow}{then}}
\DoxyCodeLine{824         \textcolor{keywordflow}{if} (cs\%harmonic\_visc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{825           \textcolor{keyword}{call }find\_coupling\_coef(a\_shelf, hvel, do\_i\_shelf, h\_harm, bbl\_thick, \&}
\DoxyCodeLine{826                                   kv\_bbl, z\_i, h\_ml, dt, j, g, gv, us, cs, \&}
\DoxyCodeLine{827                                   visc, forces, work\_on\_u=.true., obc=obc, shelf=.true.)}
\DoxyCodeLine{828         \textcolor{keywordflow}{else}  \textcolor{comment}{! Find upwind-\/biased thickness near the surface.}}
\DoxyCodeLine{829           \textcolor{comment}{! Perhaps this needs to be done more carefully, via find\_eta.}}
\DoxyCodeLine{830           \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i\_shelf(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{831             zh(i) = 0.0 ; ztop\_min(i) = min(zcol(i), zcol(i+1))}
\DoxyCodeLine{832             i\_htbl(i) = 1.0 / (visc\%tbl\_thick\_shelf\_u(i,j)*gv\%Z\_to\_H + h\_neglect)}
\DoxyCodeLine{833 \textcolor{keywordflow}{          endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{834           \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{835             \textcolor{keywordflow}{do} i=isq,ieq+1 ; zcol(i) = zcol(i) -\/ h(i,j,k) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{836             \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i\_shelf(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{837               zh(i) = zh(i) + h\_harm(i,k)}
\DoxyCodeLine{838 }
\DoxyCodeLine{839               hvel\_shelf(i,k) = hvel(i,k)}
\DoxyCodeLine{840               \textcolor{keywordflow}{if} (u(i,j,k) * h\_delta(i,k) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{841                 \textcolor{keywordflow}{if} (zh(i) * i\_htbl(i) < cs\%harm\_BL\_val) \textcolor{keywordflow}{then}}
\DoxyCodeLine{842                   hvel\_shelf(i,k) = min(hvel(i,k), h\_harm(i,k))}
\DoxyCodeLine{843                 \textcolor{keywordflow}{else}}
\DoxyCodeLine{844                   z2\_wt = 1.0  ; \textcolor{keywordflow}{if} (zh(i) * i\_htbl(i) < 2.0*cs\%harm\_BL\_val) \&}
\DoxyCodeLine{845                     z2\_wt = max(0.0, min(1.0, zh(i) * i\_htbl(i) * i\_valbl -\/ 1.0))}
\DoxyCodeLine{846                   z2 = z2\_wt * (max(zh(i), ztop\_min(i) -\/ min(zcol(i),zcol(i+1))) * i\_htbl(i))}
\DoxyCodeLine{847                   topfn = 1.0 / (1.0 + 0.09*z2**6)}
\DoxyCodeLine{848                   hvel\_shelf(i,k) = min(hvel(i,k), (1.0-\/topfn)*h\_arith(i,k) + topfn*h\_harm(i,k))}
\DoxyCodeLine{849 \textcolor{keywordflow}{                endif}}
\DoxyCodeLine{850 \textcolor{keywordflow}{              endif}}
\DoxyCodeLine{851 \textcolor{keywordflow}{            endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{852 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{853           \textcolor{keyword}{call }find\_coupling\_coef(a\_shelf, hvel\_shelf, do\_i\_shelf, h\_harm, \&}
\DoxyCodeLine{854                                   bbl\_thick, kv\_bbl, z\_i, h\_ml, dt, j, g, gv, us, cs, \&}
\DoxyCodeLine{855                                   visc, forces, work\_on\_u=.true., obc=obc, shelf=.true.)}
\DoxyCodeLine{856 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{857         \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i\_shelf(i)) cs\%a1\_shelf\_u(i,j) = a\_shelf(i,1) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{858 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{859 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{860 }
\DoxyCodeLine{861     \textcolor{keywordflow}{if} (do\_any\_shelf) \textcolor{keywordflow}{then}}
\DoxyCodeLine{862       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i\_shelf(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{863         cs\%a\_u(i,j,k) = min(a\_cpl\_max, forces\%frac\_shelf\_u(i,j)  * a\_shelf(i,k) + \&}
\DoxyCodeLine{864                                        (1.0-\/forces\%frac\_shelf\_u(i,j)) * a\_cpl(i,k))}
\DoxyCodeLine{865 \textcolor{comment}{! This is Alistair's suggestion, but it destabilizes the model. I do not know why. RWH}}
\DoxyCodeLine{866 \textcolor{comment}{!        CS\%a\_u(I,j,K) = min(a\_cpl\_max, forces\%frac\_shelf\_u(I,j)  * max(a\_shelf(I,K), a\_cpl(I,K)) + \&}}
\DoxyCodeLine{867 \textcolor{comment}{!                                       (1.0-\/forces\%frac\_shelf\_u(I,j)) * a\_cpl(I,K))}}
\DoxyCodeLine{868       \textcolor{keywordflow}{elseif} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{869         cs\%a\_u(i,j,k) = min(a\_cpl\_max, a\_cpl(i,k))}
\DoxyCodeLine{870 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{871       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i\_shelf(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{872         \textcolor{comment}{! Should we instead take the inverse of the average of the inverses?}}
\DoxyCodeLine{873         cs\%h\_u(i,j,k) = forces\%frac\_shelf\_u(i,j)  * hvel\_shelf(i,k) + \&}
\DoxyCodeLine{874                    (1.0-\/forces\%frac\_shelf\_u(i,j)) * hvel(i,k) + h\_neglect}
\DoxyCodeLine{875       \textcolor{keywordflow}{elseif} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{876         cs\%h\_u(i,j,k) = hvel(i,k) + h\_neglect}
\DoxyCodeLine{877 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{878     \textcolor{keywordflow}{else}}
\DoxyCodeLine{879       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) cs\%a\_u(i,j,k) = min(a\_cpl\_max, a\_cpl(i,k)) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{880       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) cs\%h\_u(i,j,k) = hvel(i,k) + h\_neglect ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{881 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{882 }
\DoxyCodeLine{883     \textcolor{comment}{! Diagnose total Kv at u-\/points}}
\DoxyCodeLine{884     \textcolor{keywordflow}{if} (cs\%id\_Kv\_u > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{885       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{886         \textcolor{keywordflow}{if} (do\_i(i)) kv\_u(i,j,k) = 0.5 * gv\%H\_to\_Z*(cs\%a\_u(i,j,k)+cs\%a\_u(i,j,k+1)) * cs\%h\_u(i,j,k)}
\DoxyCodeLine{887 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{888 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{889 }
\DoxyCodeLine{890 \textcolor{keywordflow}{  enddo}}
\DoxyCodeLine{891 }
\DoxyCodeLine{892 }
\DoxyCodeLine{893   \textcolor{comment}{! Now work on v-\/points.}}
\DoxyCodeLine{894   \textcolor{comment}{!\$OMP parallel do default(private) shared(G,GV,CS,US,visc,is,ie,Jsq,Jeq,nz,v,h,forces,hML\_v, \&}}
\DoxyCodeLine{895   \textcolor{comment}{!\$OMP                                  OBC,h\_neglect,dt,I\_valBL,Kv\_v,a\_cpl\_max) \&}}
\DoxyCodeLine{896   \textcolor{comment}{!\$OMP                     firstprivate(i\_hbbl)}}
\DoxyCodeLine{897   \textcolor{keywordflow}{do} j=jsq,jeq}
\DoxyCodeLine{898     \textcolor{keywordflow}{do} i=is,ie ; do\_i(i) = (g\%mask2dCv(i,j) > 0) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{899 }
\DoxyCodeLine{900     \textcolor{keywordflow}{if} (cs\%bottomdraglaw) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{901       kv\_bbl(i) = visc\%Kv\_bbl\_v(i,j)}
\DoxyCodeLine{902       bbl\_thick(i) = visc\%bbl\_thick\_v(i,j) * gv\%Z\_to\_H + h\_neglect}
\DoxyCodeLine{903       \textcolor{keywordflow}{if} (do\_i(i)) i\_hbbl(i) = 1.0 / bbl\_thick(i)}
\DoxyCodeLine{904 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{905 }
\DoxyCodeLine{906     \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{907       h\_harm(i,k) = 2.0*h(i,j,k)*h(i,j+1,k) / (h(i,j,k)+h(i,j+1,k)+h\_neglect)}
\DoxyCodeLine{908       h\_arith(i,k) = 0.5*(h(i,j+1,k)+h(i,j,k))}
\DoxyCodeLine{909       h\_delta(i,k) = h(i,j+1,k) -\/ h(i,j,k)}
\DoxyCodeLine{910 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{911     \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{912       dmin(i) = min(g\%bathyT(i,j), g\%bathyT(i,j+1)) * gv\%Z\_to\_H}
\DoxyCodeLine{913       zi\_dir(i) = 0}
\DoxyCodeLine{914 \textcolor{keywordflow}{    enddo}}
\DoxyCodeLine{915 }
\DoxyCodeLine{916     \textcolor{comment}{! Project thickness outward across OBCs using a zero-\/gradient condition.}}
\DoxyCodeLine{917     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(obc)) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (obc\%number\_of\_segments > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{918       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i) .and. (obc\%segnum\_v(i,j) /= obc\_none)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{919         \textcolor{keywordflow}{if} (obc\%segment(obc\%segnum\_v(i,j))\%direction == obc\_direction\_n) \textcolor{keywordflow}{then}}
\DoxyCodeLine{920           \textcolor{keywordflow}{do} k=1,nz ; h\_harm(i,k) = h(i,j,k) ; h\_arith(i,k) = h(i,j,k) ; h\_delta(i,k) = 0. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{921           dmin(i) = g\%bathyT(i,j) * gv\%Z\_to\_H}
\DoxyCodeLine{922           zi\_dir(i) = -\/1}
\DoxyCodeLine{923         \textcolor{keywordflow}{elseif} (obc\%segment(obc\%segnum\_v(i,j))\%direction == obc\_direction\_s) \textcolor{keywordflow}{then}}
\DoxyCodeLine{924           \textcolor{keywordflow}{do} k=1,nz ; h\_harm(i,k) = h(i,j+1,k) ; h\_arith(i,k) = h(i,j+1,k) ; h\_delta(i,k) = 0. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{925           dmin(i) = g\%bathyT(i,j+1) * gv\%Z\_to\_H}
\DoxyCodeLine{926           zi\_dir(i) = 1}
\DoxyCodeLine{927 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{928 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{929 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{930 }
\DoxyCodeLine{931 \textcolor{comment}{!    The following block calculates the thicknesses at velocity}}
\DoxyCodeLine{932 \textcolor{comment}{!  grid points for the vertical viscosity (hvel).  Near the}}
\DoxyCodeLine{933 \textcolor{comment}{!  bottom an upwind biased thickness is used to control the effect}}
\DoxyCodeLine{934 \textcolor{comment}{!  of spurious Montgomery potential gradients at the bottom where}}
\DoxyCodeLine{935 \textcolor{comment}{!  nearly massless layers layers ride over the topography.}}
\DoxyCodeLine{936     \textcolor{keywordflow}{if} (cs\%harmonic\_visc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{937       \textcolor{keywordflow}{do} i=is,ie ; z\_i(i,nz+1) = 0.0 ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{938 }
\DoxyCodeLine{939       \textcolor{keywordflow}{do} k=nz,1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{940         hvel(i,k) = h\_harm(i,k)}
\DoxyCodeLine{941         \textcolor{keywordflow}{if} (v(i,j,k) * h\_delta(i,k) < 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{942           z2 = z\_i(i,k+1) ; botfn = 1.0 / (1.0 + 0.09*z2*z2*z2*z2*z2*z2)}
\DoxyCodeLine{943           hvel(i,k) = (1.0-\/botfn)*h\_harm(i,k) + botfn*h\_arith(i,k)}
\DoxyCodeLine{944 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{945         z\_i(i,k) = z\_i(i,k+1)  + h\_harm(i,k)*i\_hbbl(i)}
\DoxyCodeLine{946 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i \& k loops}}
\DoxyCodeLine{947     \textcolor{keywordflow}{else} \textcolor{comment}{! Not harmonic\_visc}}
\DoxyCodeLine{948       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{949         zh(i) = 0.0 ; z\_i(i,nz+1) = 0.0}
\DoxyCodeLine{950         zcol1(i) = -\/g\%bathyT(i,j) * gv\%Z\_to\_H}
\DoxyCodeLine{951         zcol2(i) = -\/g\%bathyT(i,j+1) * gv\%Z\_to\_H}
\DoxyCodeLine{952 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{953       \textcolor{keywordflow}{do} k=nz,1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{954         zh(i) = zh(i) + h\_harm(i,k)}
\DoxyCodeLine{955         zcol1(i) = zcol1(i) + h(i,j,k) ; zcol2(i) = zcol2(i) + h(i,j+1,k)}
\DoxyCodeLine{956 }
\DoxyCodeLine{957         z\_clear = max(zcol1(i),zcol2(i)) + dmin(i)}
\DoxyCodeLine{958         \textcolor{keywordflow}{if} (zi\_dir(i) < 0) z\_clear = zcol1(i) + dmin(i)}
\DoxyCodeLine{959         \textcolor{keywordflow}{if} (zi\_dir(i) > 0) z\_clear = zcol2(i) + dmin(i)}
\DoxyCodeLine{960 }
\DoxyCodeLine{961         z\_i(i,k) = max(zh(i), z\_clear) * i\_hbbl(i)}
\DoxyCodeLine{962 }
\DoxyCodeLine{963         hvel(i,k) = h\_arith(i,k)}
\DoxyCodeLine{964         \textcolor{keywordflow}{if} (v(i,j,k) * h\_delta(i,k) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{965           \textcolor{keywordflow}{if} (zh(i) * i\_hbbl(i) < cs\%harm\_BL\_val) \textcolor{keywordflow}{then}}
\DoxyCodeLine{966             hvel(i,k) = h\_harm(i,k)}
\DoxyCodeLine{967           \textcolor{keywordflow}{else}}
\DoxyCodeLine{968             z2\_wt = 1.0  ; \textcolor{keywordflow}{if} (zh(i) * i\_hbbl(i) < 2.0*cs\%harm\_BL\_val) \&}
\DoxyCodeLine{969               z2\_wt = max(0.0, min(1.0, zh(i) * i\_hbbl(i) * i\_valbl -\/ 1.0))}
\DoxyCodeLine{970             z2 = z2\_wt * (max(zh(i), max(zcol1(i),zcol2(i)) + dmin(i)) * i\_hbbl(i))}
\DoxyCodeLine{971             botfn = 1.0 / (1.0 + 0.09*z2*z2*z2*z2*z2*z2)}
\DoxyCodeLine{972             hvel(i,k) = (1.0-\/botfn)*h\_arith(i,k) + botfn*h\_harm(i,k)}
\DoxyCodeLine{973 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{974 \textcolor{keywordflow}{       endif}}
\DoxyCodeLine{975 }
\DoxyCodeLine{976 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i \& k loops}}
\DoxyCodeLine{977 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{978 }
\DoxyCodeLine{979     \textcolor{keyword}{call }find\_coupling\_coef(a\_cpl, hvel, do\_i, h\_harm, bbl\_thick, kv\_bbl, z\_i, h\_ml, \&}
\DoxyCodeLine{980                             dt, j, g, gv, us, cs, visc, forces, work\_on\_u=.false., obc=obc)}
\DoxyCodeLine{981     \textcolor{keywordflow}{if} ( \textcolor{keyword}{allocated}(hml\_v)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{982        \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then} ; hml\_v(i,j) = h\_ml(i) ;\textcolor{keywordflow}{ endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{983 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{984     do\_any\_shelf = .false.}
\DoxyCodeLine{985     \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(forces\%frac\_shelf\_v)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{986       \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{987         cs\%a1\_shelf\_v(i,j) = 0.0}
\DoxyCodeLine{988         do\_i\_shelf(i) = (do\_i(i) .and. forces\%frac\_shelf\_v(i,j) > 0.0)}
\DoxyCodeLine{989         \textcolor{keywordflow}{if} (do\_i\_shelf(i)) do\_any\_shelf = .true.}
\DoxyCodeLine{990 \textcolor{keywordflow}{      enddo}}
\DoxyCodeLine{991       \textcolor{keywordflow}{if} (do\_any\_shelf) \textcolor{keywordflow}{then}}
\DoxyCodeLine{992         \textcolor{keywordflow}{if} (cs\%harmonic\_visc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{993           \textcolor{keyword}{call }find\_coupling\_coef(a\_shelf, hvel, do\_i\_shelf, h\_harm, bbl\_thick, \&}
\DoxyCodeLine{994                                   kv\_bbl, z\_i, h\_ml, dt, j, g, gv, us, cs, visc, \&}
\DoxyCodeLine{995                                   forces, work\_on\_u=.false., obc=obc, shelf=.true.)}
\DoxyCodeLine{996         \textcolor{keywordflow}{else}  \textcolor{comment}{! Find upwind-\/biased thickness near the surface.}}
\DoxyCodeLine{997           \textcolor{comment}{! Perhaps this needs to be done more carefully, via find\_eta.}}
\DoxyCodeLine{998           \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i\_shelf(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{999             zh(i) = 0.0 ; ztop\_min(i) = min(zcol1(i), zcol2(i))}
\DoxyCodeLine{1000             i\_htbl(i) = 1.0 / (visc\%tbl\_thick\_shelf\_v(i,j)*gv\%Z\_to\_H + h\_neglect)}
\DoxyCodeLine{1001 \textcolor{keywordflow}{          endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1002           \textcolor{keywordflow}{do} k=1,nz}
\DoxyCodeLine{1003             \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i\_shelf(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1004               zcol1(i) = zcol1(i) -\/ h(i,j,k) ; zcol2(i) = zcol2(i) -\/ h(i,j+1,k)}
\DoxyCodeLine{1005               zh(i) = zh(i) + h\_harm(i,k)}
\DoxyCodeLine{1006 }
\DoxyCodeLine{1007               hvel\_shelf(i,k) = hvel(i,k)}
\DoxyCodeLine{1008               \textcolor{keywordflow}{if} (v(i,j,k) * h\_delta(i,k) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1009                 \textcolor{keywordflow}{if} (zh(i) * i\_htbl(i) < cs\%harm\_BL\_val) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1010                   hvel\_shelf(i,k) = min(hvel(i,k), h\_harm(i,k))}
\DoxyCodeLine{1011                 \textcolor{keywordflow}{else}}
\DoxyCodeLine{1012                   z2\_wt = 1.0  ; \textcolor{keywordflow}{if} (zh(i) * i\_htbl(i) < 2.0*cs\%harm\_BL\_val) \&}
\DoxyCodeLine{1013                     z2\_wt = max(0.0, min(1.0, zh(i) * i\_htbl(i) * i\_valbl -\/ 1.0))}
\DoxyCodeLine{1014                   z2 = z2\_wt * (max(zh(i), ztop\_min(i) -\/ min(zcol1(i),zcol2(i))) * i\_htbl(i))}
\DoxyCodeLine{1015                   topfn = 1.0 / (1.0 + 0.09*z2**6)}
\DoxyCodeLine{1016                   hvel\_shelf(i,k) = min(hvel(i,k), (1.0-\/topfn)*h\_arith(i,k) + topfn*h\_harm(i,k))}
\DoxyCodeLine{1017 \textcolor{keywordflow}{                endif}}
\DoxyCodeLine{1018 \textcolor{keywordflow}{             endif}}
\DoxyCodeLine{1019 \textcolor{keywordflow}{            endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1020 \textcolor{keywordflow}{          enddo}}
\DoxyCodeLine{1021           \textcolor{keyword}{call }find\_coupling\_coef(a\_shelf, hvel\_shelf, do\_i\_shelf, h\_harm, \&}
\DoxyCodeLine{1022                                   bbl\_thick, kv\_bbl, z\_i, h\_ml, dt, j, g, gv, us, cs, \&}
\DoxyCodeLine{1023                                   visc, forces, work\_on\_u=.false., obc=obc, shelf=.true.)}
\DoxyCodeLine{1024 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1025         \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i\_shelf(i)) cs\%a1\_shelf\_v(i,j) = a\_shelf(i,1) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1026 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1027 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1028 }
\DoxyCodeLine{1029     \textcolor{keywordflow}{if} (do\_any\_shelf) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1030       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i\_shelf(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1031         cs\%a\_v(i,j,k) = min(a\_cpl\_max, forces\%frac\_shelf\_v(i,j)  * a\_shelf(i,k) + \&}
\DoxyCodeLine{1032                                        (1.0-\/forces\%frac\_shelf\_v(i,j)) * a\_cpl(i,k))}
\DoxyCodeLine{1033 \textcolor{comment}{! This is Alistair's suggestion, but it destabilizes the model. I do not know why. RWH}}
\DoxyCodeLine{1034 \textcolor{comment}{!        CS\%a\_v(i,J,K) = min(a\_cpl\_max, forces\%frac\_shelf\_v(i,J)  * max(a\_shelf(i,K), a\_cpl(i,K)) + \&}}
\DoxyCodeLine{1035                     \textcolor{comment}{!                   (1.0-\/forces\%frac\_shelf\_v(i,J)) * a\_cpl(i,K))}}
\DoxyCodeLine{1036       \textcolor{keywordflow}{elseif} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1037         cs\%a\_v(i,j,k) = min(a\_cpl\_max, a\_cpl(i,k))}
\DoxyCodeLine{1038 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1039       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i\_shelf(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1040         \textcolor{comment}{! Should we instead take the inverse of the average of the inverses?}}
\DoxyCodeLine{1041         cs\%h\_v(i,j,k) = forces\%frac\_shelf\_v(i,j)  * hvel\_shelf(i,k) + \&}
\DoxyCodeLine{1042                    (1.0-\/forces\%frac\_shelf\_v(i,j)) * hvel(i,k) + h\_neglect}
\DoxyCodeLine{1043       \textcolor{keywordflow}{elseif} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1044         cs\%h\_v(i,j,k) = hvel(i,k) + h\_neglect}
\DoxyCodeLine{1045 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1046     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1047       \textcolor{keywordflow}{do} k=1,nz+1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) cs\%a\_v(i,j,k) = min(a\_cpl\_max, a\_cpl(i,k)) ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1048       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) cs\%h\_v(i,j,k) = hvel(i,k) + h\_neglect ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1049 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1050 }
\DoxyCodeLine{1051     \textcolor{comment}{! Diagnose total Kv at v-\/points}}
\DoxyCodeLine{1052     \textcolor{keywordflow}{if} (cs\%id\_Kv\_v > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1053       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1054         \textcolor{keywordflow}{if} (do\_i(i)) kv\_v(i,j,k) = 0.5 * gv\%H\_to\_Z*(cs\%a\_v(i,j,k)+cs\%a\_v(i,j,k+1)) * cs\%h\_v(i,j,k)}
\DoxyCodeLine{1055 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1056 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1057 }
\DoxyCodeLine{1058 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! end of v-\/point j loop}}
\DoxyCodeLine{1059 }
\DoxyCodeLine{1060   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1061     \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"vertvisc\_coef h\_[uv]"}, cs\%h\_u, cs\%h\_v, g\%HI, haloshift=0, \&}
\DoxyCodeLine{1062                   scale=gv\%H\_to\_m, scalar\_pair=.true.)}
\DoxyCodeLine{1063     \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"vertvisc\_coef a\_[uv]"}, cs\%a\_u, cs\%a\_v, g\%HI, haloshift=0, \&}
\DoxyCodeLine{1064                   scale=us\%Z\_to\_m*us\%s\_to\_T, scalar\_pair=.true.)}
\DoxyCodeLine{1065     \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(hml\_u) .and. \textcolor{keyword}{allocated}(hml\_v)) \&}
\DoxyCodeLine{1066       \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"vertvisc\_coef hML\_[uv]"}, hml\_u, hml\_v, g\%HI, \&}
\DoxyCodeLine{1067                     haloshift=0, scale=gv\%H\_to\_m, scalar\_pair=.true.)}
\DoxyCodeLine{1068 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1069 }
\DoxyCodeLine{1070 \textcolor{comment}{! Offer diagnostic fields for averaging.}}
\DoxyCodeLine{1071   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(visc\%Kv\_slow) .and. (cs\%id\_Kv\_slow > 0)) \&}
\DoxyCodeLine{1072       \textcolor{keyword}{call }post\_data(cs\%id\_Kv\_slow, visc\%Kv\_slow, cs\%diag)}
\DoxyCodeLine{1073   \textcolor{keywordflow}{if} (cs\%id\_Kv\_u > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kv\_u, kv\_u, cs\%diag)}
\DoxyCodeLine{1074   \textcolor{keywordflow}{if} (cs\%id\_Kv\_v > 0) \textcolor{keyword}{call }post\_data(cs\%id\_Kv\_v, kv\_v, cs\%diag)}
\DoxyCodeLine{1075   \textcolor{keywordflow}{if} (cs\%id\_au\_vv > 0) \textcolor{keyword}{call }post\_data(cs\%id\_au\_vv, cs\%a\_u, cs\%diag)}
\DoxyCodeLine{1076   \textcolor{keywordflow}{if} (cs\%id\_av\_vv > 0) \textcolor{keyword}{call }post\_data(cs\%id\_av\_vv, cs\%a\_v, cs\%diag)}
\DoxyCodeLine{1077   \textcolor{keywordflow}{if} (cs\%id\_h\_u > 0) \textcolor{keyword}{call }post\_data(cs\%id\_h\_u, cs\%h\_u, cs\%diag)}
\DoxyCodeLine{1078   \textcolor{keywordflow}{if} (cs\%id\_h\_v > 0) \textcolor{keyword}{call }post\_data(cs\%id\_h\_v, cs\%h\_v, cs\%diag)}
\DoxyCodeLine{1079   \textcolor{keywordflow}{if} (cs\%id\_hML\_u > 0) \textcolor{keyword}{call }post\_data(cs\%id\_hML\_u, hml\_u, cs\%diag)}
\DoxyCodeLine{1080   \textcolor{keywordflow}{if} (cs\%id\_hML\_v > 0) \textcolor{keyword}{call }post\_data(cs\%id\_hML\_v, hml\_v, cs\%diag)}
\DoxyCodeLine{1081 }
\DoxyCodeLine{1082   \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(hml\_u)) \textcolor{keyword}{deallocate}(hml\_u)}
\DoxyCodeLine{1083   \textcolor{keywordflow}{if} (\textcolor{keyword}{allocated}(hml\_v)) \textcolor{keyword}{deallocate}(hml\_v)}
\DoxyCodeLine{1084 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__vert__friction_a158d29cf5c1d79ca7c5f327706855972}\label{namespacemom__vert__friction_a158d29cf5c1d79ca7c5f327706855972}} 
\index{mom\_vert\_friction@{mom\_vert\_friction}!vertvisc\_end@{vertvisc\_end}}
\index{vertvisc\_end@{vertvisc\_end}!mom\_vert\_friction@{mom\_vert\_friction}}
\doxysubsubsection{\texorpdfstring{vertvisc\_end()}{vertvisc\_end()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+vert\+\_\+friction\+::vertvisc\+\_\+end (\begin{DoxyParamCaption}\item[{type(\mbox{\hyperlink{structmom__vert__friction_1_1vertvisc__cs}{vertvisc\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Clean up and deallocate the vertical friction module. 


\begin{DoxyParams}{Parameters}
{\em cs} & Vertical viscosity control structure that will be deallocated in this subroutine. \\
\hline
\end{DoxyParams}


Definition at line 1890 of file M\+O\+M\+\_\+vert\+\_\+friction.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1890   \textcolor{keywordtype}{type}(vertvisc\_CS), \textcolor{keywordtype}{pointer} :: CS\textcolor{comment}{ !< Vertical viscosity control structure that}}
\DoxyCodeLine{1891 \textcolor{comment}{                                   !! will be deallocated in this subroutine.}}
\DoxyCodeLine{1892 }
\DoxyCodeLine{1893   dealloc\_(cs\%a\_u) ; dealloc\_(cs\%h\_u)}
\DoxyCodeLine{1894   dealloc\_(cs\%a\_v) ; dealloc\_(cs\%h\_v)}
\DoxyCodeLine{1895   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs\%a1\_shelf\_u)) \textcolor{keyword}{deallocate}(cs\%a1\_shelf\_u)}
\DoxyCodeLine{1896   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs\%a1\_shelf\_v)) \textcolor{keyword}{deallocate}(cs\%a1\_shelf\_v)}
\DoxyCodeLine{1897   \textcolor{keyword}{deallocate}(cs)}

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__vert__friction_a4bd5c8772584e41890bff55ccd52507c}\label{namespacemom__vert__friction_a4bd5c8772584e41890bff55ccd52507c}} 
\index{mom\_vert\_friction@{mom\_vert\_friction}!vertvisc\_init@{vertvisc\_init}}
\index{vertvisc\_init@{vertvisc\_init}!mom\_vert\_friction@{mom\_vert\_friction}}
\doxysubsubsection{\texorpdfstring{vertvisc\_init()}{vertvisc\_init()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+vert\+\_\+friction\+::vertvisc\+\_\+init (\begin{DoxyParamCaption}\item[{type(ocean\+\_\+internal\+\_\+state), intent(in), target}]{M\+IS,  }\item[{type(time\+\_\+type), intent(in), target}]{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(accel\+\_\+diag\+\_\+ptrs), intent(inout)}]{A\+Dp,  }\item[{type(directories), intent(in)}]{dirs,  }\item[{integer, intent(inout), target}]{ntrunc,  }\item[{type(\mbox{\hyperlink{structmom__vert__friction_1_1vertvisc__cs}{vertvisc\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Initialize the vertical friction module. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em mis} & The \char`\"{}\+M\+O\+M Internal State\char`\"{}, a set of pointers \\
\hline
\mbox{\texttt{ in}}  & {\em time} & Current model time \\
\hline
\mbox{\texttt{ in}}  & {\em g} & Ocean grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & Ocean vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in}}  & {\em param\+\_\+file} & File to parse for parameters \\
\hline
\mbox{\texttt{ in,out}}  & {\em diag} & Diagnostic control structure \\
\hline
\mbox{\texttt{ in,out}}  & {\em adp} & Acceleration diagnostic pointers \\
\hline
\mbox{\texttt{ in}}  & {\em dirs} & Relevant directory paths \\
\hline
\mbox{\texttt{ in,out}}  & {\em ntrunc} & Number of velocity truncations \\
\hline
 & {\em cs} & Vertical viscosity control structure \\
\hline
\end{DoxyParams}


Definition at line 1580 of file M\+O\+M\+\_\+vert\+\_\+friction.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1580   \textcolor{keywordtype}{type}(ocean\_internal\_state), \&}
\DoxyCodeLine{1581                    \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(in)}    :: MIS\textcolor{comment}{    !< The "MOM Internal State", a set of pointers}}
\DoxyCodeLine{1582 \textcolor{comment}{                                                   !! to the fields and accelerations that make}}
\DoxyCodeLine{1583 \textcolor{comment}{                                                   !! up the ocean's physical state}}
\DoxyCodeLine{1584   \textcolor{keywordtype}{type}(time\_type), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(in)}    :: Time\textcolor{comment}{   !< Current model time}}
\DoxyCodeLine{1585   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{      !< Ocean grid structure}}
\DoxyCodeLine{1586   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{     !< Ocean vertical grid structure}}
\DoxyCodeLine{1587   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{     !< A dimensional unit scaling type}}
\DoxyCodeLine{1588   \textcolor{keywordtype}{type}(param\_file\_type),   \textcolor{keywordtype}{intent(in)}    :: param\_file\textcolor{comment}{ !< File to parse for parameters}}
\DoxyCodeLine{1589   \textcolor{keywordtype}{type}(diag\_ctrl), \textcolor{keywordtype}{target}, \textcolor{keywordtype}{intent(inout)} :: diag\textcolor{comment}{   !< Diagnostic control structure}}
\DoxyCodeLine{1590   \textcolor{keywordtype}{type}(accel\_diag\_ptrs),   \textcolor{keywordtype}{intent(inout)} :: ADp\textcolor{comment}{    !< Acceleration diagnostic pointers}}
\DoxyCodeLine{1591   \textcolor{keywordtype}{type}(directories),       \textcolor{keywordtype}{intent(in)}    :: dirs\textcolor{comment}{   !< Relevant directory paths}}
\DoxyCodeLine{1592   \textcolor{keywordtype}{integer}, \textcolor{keywordtype}{target},         \textcolor{keywordtype}{intent(inout)} :: ntrunc\textcolor{comment}{ !< Number of velocity truncations}}
\DoxyCodeLine{1593   \textcolor{keywordtype}{type}(vertvisc\_CS),       \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{     !< Vertical viscosity control structure}}
\DoxyCodeLine{1594 }
\DoxyCodeLine{1595   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1596 }
\DoxyCodeLine{1597 \textcolor{keywordtype}{  real} :: hmix\_str\_dflt}
\DoxyCodeLine{1598 \textcolor{keywordtype}{  real} :: Kv\_dflt \textcolor{comment}{! A default viscosity [m2 s-\/1].}}
\DoxyCodeLine{1599 \textcolor{keywordtype}{  real} :: Hmix\_m  \textcolor{comment}{! A boundary layer thickness [m].}}
\DoxyCodeLine{1600   \textcolor{keywordtype}{logical} :: default\_2018\_answers}
\DoxyCodeLine{1601   \textcolor{keywordtype}{integer} :: isd, ied, jsd, jed, IsdB, IedB, JsdB, JedB, nz}
\DoxyCodeLine{1602 \textcolor{comment}{! This include declares and sets the variable "version".}}
\DoxyCodeLine{1603 \textcolor{preprocessor}{\#include "version\_variable.h"}}
\DoxyCodeLine{1604 \textcolor{preprocessor}{}  \textcolor{keywordtype}{character(len=40)}  :: mdl = \textcolor{stringliteral}{"MOM\_vert\_friction"} \textcolor{comment}{! This module's name.}}
\DoxyCodeLine{1605   \textcolor{keywordtype}{character(len=40)}  :: thickness\_units}
\DoxyCodeLine{1606 }
\DoxyCodeLine{1607   \textcolor{keywordflow}{if} (\textcolor{keyword}{associated}(cs)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1608     \textcolor{keyword}{call }mom\_error(warning, \textcolor{stringliteral}{"vertvisc\_init called with an associated "}// \&}
\DoxyCodeLine{1609                             \textcolor{stringliteral}{"control structure."})}
\DoxyCodeLine{1610     \textcolor{keywordflow}{return}}
\DoxyCodeLine{1611 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1612   \textcolor{keyword}{allocate}(cs)}
\DoxyCodeLine{1613 }
\DoxyCodeLine{1614   \textcolor{keywordflow}{if} (gv\%Boussinesq) then; thickness\_units = \textcolor{stringliteral}{"m"}}
\DoxyCodeLine{1615   else; thickness\_units = \textcolor{stringliteral}{"kg m-\/2"};\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1616 }
\DoxyCodeLine{1617   isd = g\%isd ; ied = g\%ied ; jsd = g\%jsd ; jed = g\%jed ; nz = g\%ke}
\DoxyCodeLine{1618   isdb = g\%IsdB ; iedb = g\%IedB ; jsdb = g\%JsdB ; jedb = g\%JedB}
\DoxyCodeLine{1619 }
\DoxyCodeLine{1620   cs\%diag => diag ; cs\%ntrunc => ntrunc ; ntrunc = 0}
\DoxyCodeLine{1621 }
\DoxyCodeLine{1622 \textcolor{comment}{! Default, read and log parameters}}
\DoxyCodeLine{1623   \textcolor{keyword}{call }log\_version(param\_file, mdl, version, \textcolor{stringliteral}{""}, log\_to\_all=.true., debugging=.true.)}
\DoxyCodeLine{1624   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEFAULT\_2018\_ANSWERS"}, default\_2018\_answers, \&}
\DoxyCodeLine{1625                  \textcolor{stringliteral}{"This sets the default value for the various \_2018\_ANSWERS parameters."}, \&}
\DoxyCodeLine{1626                  default=.false.)}
\DoxyCodeLine{1627   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"VERT\_FRICTION\_2018\_ANSWERS"}, cs\%answers\_2018, \&}
\DoxyCodeLine{1628                  \textcolor{stringliteral}{"If true, use the order of arithmetic and expressions that recover the answers "}//\&}
\DoxyCodeLine{1629                  \textcolor{stringliteral}{"from the end of 2018.  Otherwise, use expressions that do not use an arbitrary "}//\&}
\DoxyCodeLine{1630                  \textcolor{stringliteral}{"hard-\/coded maximum viscous coupling coefficient between layers."}, \&}
\DoxyCodeLine{1631                  default=default\_2018\_answers)}
\DoxyCodeLine{1632   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"BOTTOMDRAGLAW"}, cs\%bottomdraglaw, \&}
\DoxyCodeLine{1633                  \textcolor{stringliteral}{"If true, the bottom stress is calculated with a drag "}//\&}
\DoxyCodeLine{1634                  \textcolor{stringliteral}{"law of the form c\_drag*|u|*u. The velocity magnitude "}//\&}
\DoxyCodeLine{1635                  \textcolor{stringliteral}{"may be an assumed value or it may be based on the "}//\&}
\DoxyCodeLine{1636                  \textcolor{stringliteral}{"actual velocity in the bottommost HBBL, depending on "}//\&}
\DoxyCodeLine{1637                  \textcolor{stringliteral}{"LINEAR\_DRAG."}, default=.true.)}
\DoxyCodeLine{1638   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CHANNEL\_DRAG"}, cs\%Channel\_drag, \&}
\DoxyCodeLine{1639                  \textcolor{stringliteral}{"If true, the bottom drag is exerted directly on each "}//\&}
\DoxyCodeLine{1640                  \textcolor{stringliteral}{"layer proportional to the fraction of the bottom it "}//\&}
\DoxyCodeLine{1641                  \textcolor{stringliteral}{"overlies."}, default=.false.)}
\DoxyCodeLine{1642   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DIRECT\_STRESS"}, cs\%direct\_stress, \&}
\DoxyCodeLine{1643                  \textcolor{stringliteral}{"If true, the wind stress is distributed over the "}//\&}
\DoxyCodeLine{1644                  \textcolor{stringliteral}{"topmost HMIX\_STRESS of fluid (like in HYCOM), and KVML "}//\&}
\DoxyCodeLine{1645                  \textcolor{stringliteral}{"may be set to a very small value."}, default=.false.)}
\DoxyCodeLine{1646   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DYNAMIC\_VISCOUS\_ML"}, cs\%dynamic\_viscous\_ML, \&}
\DoxyCodeLine{1647                  \textcolor{stringliteral}{"If true, use a bulk Richardson number criterion to "}//\&}
\DoxyCodeLine{1648                  \textcolor{stringliteral}{"determine the mixed layer thickness for viscosity."}, \&}
\DoxyCodeLine{1649                  default=.false.)}
\DoxyCodeLine{1650   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"U\_TRUNC\_FILE"}, cs\%u\_trunc\_file, \&}
\DoxyCodeLine{1651                  \textcolor{stringliteral}{"The absolute path to a file into which the accelerations "}//\&}
\DoxyCodeLine{1652                  \textcolor{stringliteral}{"leading to zonal velocity truncations are written. "}//\&}
\DoxyCodeLine{1653                  \textcolor{stringliteral}{"Undefine this for efficiency if this diagnostic is not "}//\&}
\DoxyCodeLine{1654                  \textcolor{stringliteral}{"needed."}, default=\textcolor{stringliteral}{" "}, debuggingparam=.true.)}
\DoxyCodeLine{1655   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"V\_TRUNC\_FILE"}, cs\%v\_trunc\_file, \&}
\DoxyCodeLine{1656                  \textcolor{stringliteral}{"The absolute path to a file into which the accelerations "}//\&}
\DoxyCodeLine{1657                  \textcolor{stringliteral}{"leading to meridional velocity truncations are written. "}//\&}
\DoxyCodeLine{1658                  \textcolor{stringliteral}{"Undefine this for efficiency if this diagnostic is not "}//\&}
\DoxyCodeLine{1659                  \textcolor{stringliteral}{"needed."}, default=\textcolor{stringliteral}{" "}, debuggingparam=.true.)}
\DoxyCodeLine{1660   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HARMONIC\_VISC"}, cs\%harmonic\_visc, \&}
\DoxyCodeLine{1661                  \textcolor{stringliteral}{"If true, use the harmonic mean thicknesses for "}//\&}
\DoxyCodeLine{1662                  \textcolor{stringliteral}{"calculating the vertical viscosity."}, default=.false.)}
\DoxyCodeLine{1663   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HARMONIC\_BL\_SCALE"}, cs\%harm\_BL\_val, \&}
\DoxyCodeLine{1664                  \textcolor{stringliteral}{"A scale to determine when water is in the boundary "}//\&}
\DoxyCodeLine{1665                  \textcolor{stringliteral}{"layers based solely on harmonic mean thicknesses for "}//\&}
\DoxyCodeLine{1666                  \textcolor{stringliteral}{"the purpose of determining the extent to which the "}//\&}
\DoxyCodeLine{1667                  \textcolor{stringliteral}{"thicknesses used in the viscosities are upwinded."}, \&}
\DoxyCodeLine{1668                  default=0.0, units=\textcolor{stringliteral}{"nondim"})}
\DoxyCodeLine{1669   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"DEBUG"}, cs\%debug, default=.false.)}
\DoxyCodeLine{1670 }
\DoxyCodeLine{1671   \textcolor{keywordflow}{if} (gv\%nkml < 1) \&}
\DoxyCodeLine{1672     \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HMIX\_FIXED"}, cs\%Hmix, \&}
\DoxyCodeLine{1673                  \textcolor{stringliteral}{"The prescribed depth over which the near-\/surface "}//\&}
\DoxyCodeLine{1674                  \textcolor{stringliteral}{"viscosity and diffusivity are elevated when the bulk "}//\&}
\DoxyCodeLine{1675                  \textcolor{stringliteral}{"mixed layer is not used."}, units=\textcolor{stringliteral}{"m"}, scale=gv\%m\_to\_H, \&}
\DoxyCodeLine{1676                  unscaled=hmix\_m, fail\_if\_missing=.true.)}
\DoxyCodeLine{1677   \textcolor{keywordflow}{if} (cs\%direct\_stress) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1678     \textcolor{keywordflow}{if} (gv\%nkml < 1) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1679       \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HMIX\_STRESS"}, cs\%Hmix\_stress, \&}
\DoxyCodeLine{1680                  \textcolor{stringliteral}{"The depth over which the wind stress is applied if "}//\&}
\DoxyCodeLine{1681                  \textcolor{stringliteral}{"DIRECT\_STRESS is true."}, units=\textcolor{stringliteral}{"m"}, default=hmix\_m, scale=gv\%m\_to\_H)}
\DoxyCodeLine{1682     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1683       \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HMIX\_STRESS"}, cs\%Hmix\_stress, \&}
\DoxyCodeLine{1684                  \textcolor{stringliteral}{"The depth over which the wind stress is applied if "}//\&}
\DoxyCodeLine{1685                  \textcolor{stringliteral}{"DIRECT\_STRESS is true."}, units=\textcolor{stringliteral}{"m"}, fail\_if\_missing=.true., scale=gv\%m\_to\_H)}
\DoxyCodeLine{1686 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1687     \textcolor{keywordflow}{if} (cs\%Hmix\_stress <= 0.0) \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"vertvisc\_init: "} // \&}
\DoxyCodeLine{1688        \textcolor{stringliteral}{"HMIX\_STRESS must be set to a positive value if DIRECT\_STRESS is true."})}
\DoxyCodeLine{1689 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1690   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KV"}, cs\%Kv, \&}
\DoxyCodeLine{1691                  \textcolor{stringliteral}{"The background kinematic viscosity in the interior. "}//\&}
\DoxyCodeLine{1692                  \textcolor{stringliteral}{"The molecular value, ~1e-\/6 m2 s-\/1, may be used."}, \&}
\DoxyCodeLine{1693                  units=\textcolor{stringliteral}{"m2 s-\/1"}, fail\_if\_missing=.true., scale=us\%m2\_s\_to\_Z2\_T, unscaled=kv\_dflt)}
\DoxyCodeLine{1694 }
\DoxyCodeLine{1695   \textcolor{keywordflow}{if} (gv\%nkml < 1) \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KVML"}, cs\%Kvml, \&}
\DoxyCodeLine{1696                  \textcolor{stringliteral}{"The kinematic viscosity in the mixed layer.  A typical "}//\&}
\DoxyCodeLine{1697                  \textcolor{stringliteral}{"value is ~1e-\/2 m2 s-\/1. KVML is not used if "}//\&}
\DoxyCodeLine{1698                  \textcolor{stringliteral}{"BULKMIXEDLAYER is true.  The default is set by KV."}, \&}
\DoxyCodeLine{1699                  units=\textcolor{stringliteral}{"m2 s-\/1"}, default=kv\_dflt, scale=us\%m2\_s\_to\_Z2\_T)}
\DoxyCodeLine{1700   \textcolor{keywordflow}{if} (.not.cs\%bottomdraglaw) \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"KVBBL"}, cs\%Kvbbl, \&}
\DoxyCodeLine{1701                  \textcolor{stringliteral}{"The kinematic viscosity in the benthic boundary layer. "}//\&}
\DoxyCodeLine{1702                  \textcolor{stringliteral}{"A typical value is ~1e-\/2 m2 s-\/1. KVBBL is not used if "}//\&}
\DoxyCodeLine{1703                  \textcolor{stringliteral}{"BOTTOMDRAGLAW is true.  The default is set by KV."}, \&}
\DoxyCodeLine{1704                  units=\textcolor{stringliteral}{"m2 s-\/1"}, default=kv\_dflt, scale=us\%m2\_s\_to\_Z2\_T)}
\DoxyCodeLine{1705   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"HBBL"}, cs\%Hbbl, \&}
\DoxyCodeLine{1706                  \textcolor{stringliteral}{"The thickness of a bottom boundary layer with a "}//\&}
\DoxyCodeLine{1707                  \textcolor{stringliteral}{"viscosity of KVBBL if BOTTOMDRAGLAW is not defined, or "}//\&}
\DoxyCodeLine{1708                  \textcolor{stringliteral}{"the thickness over which near-\/bottom velocities are "}//\&}
\DoxyCodeLine{1709                  \textcolor{stringliteral}{"averaged for the drag law if BOTTOMDRAGLAW is defined "}//\&}
\DoxyCodeLine{1710                  \textcolor{stringliteral}{"but LINEAR\_DRAG is not."}, units=\textcolor{stringliteral}{"m"}, fail\_if\_missing=.true., scale=gv\%m\_to\_H)}
\DoxyCodeLine{1711   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"MAXVEL"}, cs\%maxvel, \&}
\DoxyCodeLine{1712                  \textcolor{stringliteral}{"The maximum velocity allowed before the velocity "}//\&}
\DoxyCodeLine{1713                  \textcolor{stringliteral}{"components are truncated."}, units=\textcolor{stringliteral}{"m s-\/1"}, default=3.0e8, scale=us\%m\_s\_to\_L\_T)}
\DoxyCodeLine{1714   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CFL\_BASED\_TRUNCATIONS"}, cs\%CFL\_based\_trunc, \&}
\DoxyCodeLine{1715                  \textcolor{stringliteral}{"If true, base truncations on the CFL number, and not an "}//\&}
\DoxyCodeLine{1716                  \textcolor{stringliteral}{"absolute speed."}, default=.true.)}
\DoxyCodeLine{1717   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CFL\_TRUNCATE"}, cs\%CFL\_trunc, \&}
\DoxyCodeLine{1718                  \textcolor{stringliteral}{"The value of the CFL number that will cause velocity "}//\&}
\DoxyCodeLine{1719                  \textcolor{stringliteral}{"components to be truncated; instability can occur past 0.5."}, \&}
\DoxyCodeLine{1720                  units=\textcolor{stringliteral}{"nondim"}, default=0.5)}
\DoxyCodeLine{1721   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CFL\_REPORT"}, cs\%CFL\_report, \&}
\DoxyCodeLine{1722                  \textcolor{stringliteral}{"The value of the CFL number that causes accelerations "}//\&}
\DoxyCodeLine{1723                  \textcolor{stringliteral}{"to be reported; the default is CFL\_TRUNCATE."}, \&}
\DoxyCodeLine{1724                  units=\textcolor{stringliteral}{"nondim"}, default=cs\%CFL\_trunc)}
\DoxyCodeLine{1725   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CFL\_TRUNCATE\_RAMP\_TIME"}, cs\%truncRampTime, \&}
\DoxyCodeLine{1726                  \textcolor{stringliteral}{"The time over which the CFL truncation value is ramped "}//\&}
\DoxyCodeLine{1727                  \textcolor{stringliteral}{"up at the beginning of the run."}, \&}
\DoxyCodeLine{1728                  units=\textcolor{stringliteral}{"s"}, default=0.)}
\DoxyCodeLine{1729   cs\%CFL\_truncE = cs\%CFL\_trunc}
\DoxyCodeLine{1730   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"CFL\_TRUNCATE\_START"}, cs\%CFL\_truncS, \&}
\DoxyCodeLine{1731                  \textcolor{stringliteral}{"The start value of the truncation CFL number used when "}//\&}
\DoxyCodeLine{1732                  \textcolor{stringliteral}{"ramping up CFL\_TRUNC."}, \&}
\DoxyCodeLine{1733                  units=\textcolor{stringliteral}{"nondim"}, default=0.)}
\DoxyCodeLine{1734   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"STOKES\_MIXING\_COMBINED"}, cs\%StokesMixing, \&}
\DoxyCodeLine{1735                  \textcolor{stringliteral}{"Flag to use Stokes drift Mixing via the Lagrangian "}//\&}
\DoxyCodeLine{1736                  \textcolor{stringliteral}{" current (Eulerian plus Stokes drift). "}//\&}
\DoxyCodeLine{1737                  \textcolor{stringliteral}{" Still needs work and testing, so not recommended for use."},\&}
\DoxyCodeLine{1738                  default=.false.)}
\DoxyCodeLine{1739   \textcolor{comment}{!BGR 04/04/2018\{}}
\DoxyCodeLine{1740   \textcolor{comment}{! StokesMixing is required for MOM6 for some Langmuir mixing parameterization.}}
\DoxyCodeLine{1741   \textcolor{comment}{!   The code used here has not been developed for vanishing layers or in}}
\DoxyCodeLine{1742   \textcolor{comment}{!   conjunction with any bottom friction.  Therefore, the following line is}}
\DoxyCodeLine{1743   \textcolor{comment}{!   added so this functionality cannot be used without user intervention in}}
\DoxyCodeLine{1744   \textcolor{comment}{!   the code.  This will prevent general use of this functionality until proper}}
\DoxyCodeLine{1745   \textcolor{comment}{!   care is given to the previously mentioned issues.  Comment out the following}}
\DoxyCodeLine{1746   \textcolor{comment}{!   MOM\_error to use, but do so at your own risk and with these points in mind.}}
\DoxyCodeLine{1747   \textcolor{comment}{!\}}}
\DoxyCodeLine{1748   \textcolor{keywordflow}{if} (cs\%StokesMixing) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1749     \textcolor{keyword}{call }mom\_error(fatal, \textcolor{stringliteral}{"Stokes mixing requires user intervention in the code.\(\backslash\)n"}//\&}
\DoxyCodeLine{1750                           \textcolor{stringliteral}{"  Model now exiting.  See MOM\_vert\_friction.F90 for \(\backslash\)n"}//\&}
\DoxyCodeLine{1751                           \textcolor{stringliteral}{"  details (search 'BGR 04/04/2018' to locate comment)."})}
\DoxyCodeLine{1752 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1753   \textcolor{keyword}{call }get\_param(param\_file, mdl, \textcolor{stringliteral}{"VEL\_UNDERFLOW"}, cs\%vel\_underflow, \&}
\DoxyCodeLine{1754                  \textcolor{stringliteral}{"A negligibly small velocity magnitude below which velocity "}//\&}
\DoxyCodeLine{1755                  \textcolor{stringliteral}{"components are set to 0.  A reasonable value might be "}//\&}
\DoxyCodeLine{1756                  \textcolor{stringliteral}{"1e-\/30 m/s, which is less than an Angstrom divided by "}//\&}
\DoxyCodeLine{1757                  \textcolor{stringliteral}{"the age of the universe."}, units=\textcolor{stringliteral}{"m s-\/1"}, default=0.0, scale=us\%m\_s\_to\_L\_T)}
\DoxyCodeLine{1758 }
\DoxyCodeLine{1759   alloc\_(cs\%a\_u(isdb:iedb,jsd:jed,nz+1)) ; cs\%a\_u(:,:,:) = 0.0}
\DoxyCodeLine{1760   alloc\_(cs\%h\_u(isdb:iedb,jsd:jed,nz))   ; cs\%h\_u(:,:,:) = 0.0}
\DoxyCodeLine{1761   alloc\_(cs\%a\_v(isd:ied,jsdb:jedb,nz+1)) ; cs\%a\_v(:,:,:) = 0.0}
\DoxyCodeLine{1762   alloc\_(cs\%h\_v(isd:ied,jsdb:jedb,nz))   ; cs\%h\_v(:,:,:) = 0.0}
\DoxyCodeLine{1763 }
\DoxyCodeLine{1764   cs\%id\_Kv\_slow = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kv\_slow'}, diag\%axesTi, time, \&}
\DoxyCodeLine{1765      \textcolor{stringliteral}{'Slow varying vertical viscosity'}, \textcolor{stringliteral}{'m2 s-\/1'}, conversion=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{1766 }
\DoxyCodeLine{1767   cs\%id\_Kv\_u = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kv\_u'}, diag\%axesCuL, time, \&}
\DoxyCodeLine{1768      \textcolor{stringliteral}{'Total vertical viscosity at u-\/points'}, \textcolor{stringliteral}{'m2 s-\/1'}, conversion=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{1769 }
\DoxyCodeLine{1770   cs\%id\_Kv\_v = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Kv\_v'}, diag\%axesCvL, time, \&}
\DoxyCodeLine{1771      \textcolor{stringliteral}{'Total vertical viscosity at v-\/points'}, \textcolor{stringliteral}{'m2 s-\/1'}, conversion=us\%Z2\_T\_to\_m2\_s)}
\DoxyCodeLine{1772 }
\DoxyCodeLine{1773   cs\%id\_au\_vv = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'au\_visc'}, diag\%axesCui, time, \&}
\DoxyCodeLine{1774      \textcolor{stringliteral}{'Zonal Viscous Vertical Coupling Coefficient'}, \textcolor{stringliteral}{'m s-\/1'}, conversion=us\%Z\_to\_m*us\%s\_to\_T)}
\DoxyCodeLine{1775 }
\DoxyCodeLine{1776   cs\%id\_av\_vv = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'av\_visc'}, diag\%axesCvi, time, \&}
\DoxyCodeLine{1777      \textcolor{stringliteral}{'Meridional Viscous Vertical Coupling Coefficient'}, \textcolor{stringliteral}{'m s-\/1'}, conversion=us\%Z\_to\_m*us\%s\_to\_T)}
\DoxyCodeLine{1778 }
\DoxyCodeLine{1779   cs\%id\_h\_u = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Hu\_visc'}, diag\%axesCuL, time, \&}
\DoxyCodeLine{1780      \textcolor{stringliteral}{'Thickness at Zonal Velocity Points for Viscosity'}, thickness\_units, \&}
\DoxyCodeLine{1781      conversion=gv\%H\_to\_m)}
\DoxyCodeLine{1782 }
\DoxyCodeLine{1783   cs\%id\_h\_v = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'Hv\_visc'}, diag\%axesCvL, time, \&}
\DoxyCodeLine{1784      \textcolor{stringliteral}{'Thickness at Meridional Velocity Points for Viscosity'}, thickness\_units, \&}
\DoxyCodeLine{1785      conversion=gv\%H\_to\_m)}
\DoxyCodeLine{1786 }
\DoxyCodeLine{1787   cs\%id\_hML\_u = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'HMLu\_visc'}, diag\%axesCu1, time, \&}
\DoxyCodeLine{1788      \textcolor{stringliteral}{'Mixed Layer Thickness at Zonal Velocity Points for Viscosity'}, thickness\_units, \&}
\DoxyCodeLine{1789      conversion=gv\%H\_to\_m)}
\DoxyCodeLine{1790 }
\DoxyCodeLine{1791   cs\%id\_hML\_v = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'HMLv\_visc'}, diag\%axesCv1, time, \&}
\DoxyCodeLine{1792      \textcolor{stringliteral}{'Mixed Layer Thickness at Meridional Velocity Points for Viscosity'}, thickness\_units, \&}
\DoxyCodeLine{1793      conversion=gv\%H\_to\_m)}
\DoxyCodeLine{1794 }
\DoxyCodeLine{1795   cs\%id\_du\_dt\_visc = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'du\_dt\_visc'}, diag\%axesCuL, \&}
\DoxyCodeLine{1796      time, \textcolor{stringliteral}{'Zonal Acceleration from Vertical Viscosity'}, \textcolor{stringliteral}{'m s-\/2'}, conversion=us\%L\_T2\_to\_m\_s2)}
\DoxyCodeLine{1797   \textcolor{keywordflow}{if} (cs\%id\_du\_dt\_visc > 0) \textcolor{keyword}{call }safe\_alloc\_ptr(adp\%du\_dt\_visc,isdb,iedb,jsd,jed,nz)}
\DoxyCodeLine{1798   cs\%id\_dv\_dt\_visc = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'dv\_dt\_visc'}, diag\%axesCvL, \&}
\DoxyCodeLine{1799      time, \textcolor{stringliteral}{'Meridional Acceleration from Vertical Viscosity'}, \textcolor{stringliteral}{'m s-\/2'}, conversion=us\%L\_T2\_to\_m\_s2)}
\DoxyCodeLine{1800   \textcolor{keywordflow}{if} (cs\%id\_dv\_dt\_visc > 0) \textcolor{keyword}{call }safe\_alloc\_ptr(adp\%dv\_dt\_visc,isd,ied,jsdb,jedb,nz)}
\DoxyCodeLine{1801 }
\DoxyCodeLine{1802   cs\%id\_taux\_bot = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'taux\_bot'}, diag\%axesCu1, \&}
\DoxyCodeLine{1803      time, \textcolor{stringliteral}{'Zonal Bottom Stress from Ocean to Earth'}, \textcolor{stringliteral}{'Pa'}, \&}
\DoxyCodeLine{1804      conversion=us\%RZ\_to\_kg\_m2*us\%L\_T2\_to\_m\_s2)}
\DoxyCodeLine{1805   cs\%id\_tauy\_bot = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'tauy\_bot'}, diag\%axesCv1, \&}
\DoxyCodeLine{1806      time, \textcolor{stringliteral}{'Meridional Bottom Stress from Ocean to Earth'}, \textcolor{stringliteral}{'Pa'}, \&}
\DoxyCodeLine{1807      conversion=us\%RZ\_to\_kg\_m2*us\%L\_T2\_to\_m\_s2)}
\DoxyCodeLine{1808 }
\DoxyCodeLine{1809   \textcolor{comment}{!CS\%id\_hf\_du\_dt\_visc = register\_diag\_field('ocean\_model', 'hf\_du\_dt\_visc', diag\%axesCuL, Time, \&}}
\DoxyCodeLine{1810   \textcolor{comment}{!    'Fractional Thickness-\/weighted Zonal Acceleration from Vertical Viscosity', 'm s-\/2', \&}}
\DoxyCodeLine{1811   \textcolor{comment}{!    v\_extensive=.true., conversion=US\%L\_T2\_to\_m\_s2)}}
\DoxyCodeLine{1812   \textcolor{comment}{!if (CS\%id\_hf\_du\_dt\_visc > 0) then}}
\DoxyCodeLine{1813   \textcolor{comment}{!  call safe\_alloc\_ptr(CS\%hf\_du\_dt\_visc,IsdB,IedB,jsd,jed,nz)}}
\DoxyCodeLine{1814   \textcolor{comment}{!  call safe\_alloc\_ptr(ADp\%du\_dt\_visc,IsdB,IedB,jsd,jed,nz)}}
\DoxyCodeLine{1815   \textcolor{comment}{!  call safe\_alloc\_ptr(ADp\%diag\_hfrac\_u,IsdB,IedB,jsd,jed,nz)}}
\DoxyCodeLine{1816   \textcolor{comment}{!endif}}
\DoxyCodeLine{1817 }
\DoxyCodeLine{1818   \textcolor{comment}{!CS\%id\_hf\_dv\_dt\_visc = register\_diag\_field('ocean\_model', 'hf\_dv\_dt\_visc', diag\%axesCvL, Time, \&}}
\DoxyCodeLine{1819   \textcolor{comment}{!    'Fractional Thickness-\/weighted Meridional Acceleration from Vertical Viscosity', 'm s-\/2', \&}}
\DoxyCodeLine{1820   \textcolor{comment}{!    v\_extensive=.true., conversion=US\%L\_T2\_to\_m\_s2)}}
\DoxyCodeLine{1821   \textcolor{comment}{!if (CS\%id\_hf\_dv\_dt\_visc > 0) then}}
\DoxyCodeLine{1822   \textcolor{comment}{!  call safe\_alloc\_ptr(CS\%hf\_dv\_dt\_visc,isd,ied,JsdB,JedB,nz)}}
\DoxyCodeLine{1823   \textcolor{comment}{!  call safe\_alloc\_ptr(ADp\%dv\_dt\_visc,isd,ied,JsdB,JedB,nz)}}
\DoxyCodeLine{1824   \textcolor{comment}{!  call safe\_alloc\_ptr(ADp\%diag\_hfrac\_v,isd,ied,Jsd,JedB,nz)}}
\DoxyCodeLine{1825   \textcolor{comment}{!endif}}
\DoxyCodeLine{1826 }
\DoxyCodeLine{1827   cs\%id\_hf\_du\_dt\_visc\_2d = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'hf\_du\_dt\_visc\_2d'}, diag\%axesCu1, time, \&}
\DoxyCodeLine{1828       \textcolor{stringliteral}{'Depth-\/sum Fractional Thickness-\/weighted Zonal Acceleration from Vertical Viscosity'}, \textcolor{stringliteral}{'m s-\/2'}, \&}
\DoxyCodeLine{1829       conversion=us\%L\_T2\_to\_m\_s2)}
\DoxyCodeLine{1830   \textcolor{keywordflow}{if} (cs\%id\_hf\_du\_dt\_visc\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1831     \textcolor{keyword}{call }safe\_alloc\_ptr(adp\%du\_dt\_visc,isdb,iedb,jsd,jed,nz)}
\DoxyCodeLine{1832     \textcolor{keyword}{call }safe\_alloc\_ptr(adp\%diag\_hfrac\_u,isdb,iedb,jsd,jed,nz)}
\DoxyCodeLine{1833 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1834 }
\DoxyCodeLine{1835   cs\%id\_hf\_dv\_dt\_visc\_2d = register\_diag\_field(\textcolor{stringliteral}{'ocean\_model'}, \textcolor{stringliteral}{'hf\_dv\_dt\_visc\_2d'}, diag\%axesCv1, time, \&}
\DoxyCodeLine{1836       \textcolor{stringliteral}{'Depth-\/sum Fractional Thickness-\/weighted Meridional Acceleration from Vertical Viscosity'}, \textcolor{stringliteral}{'m s-\/2'}, \&}
\DoxyCodeLine{1837       conversion=us\%L\_T2\_to\_m\_s2)}
\DoxyCodeLine{1838   \textcolor{keywordflow}{if} (cs\%id\_hf\_dv\_dt\_visc\_2d > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1839     \textcolor{keyword}{call }safe\_alloc\_ptr(adp\%dv\_dt\_visc,isd,ied,jsdb,jedb,nz)}
\DoxyCodeLine{1840     \textcolor{keyword}{call }safe\_alloc\_ptr(adp\%diag\_hfrac\_v,isd,ied,jsd,jedb,nz)}
\DoxyCodeLine{1841 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1842 }
\DoxyCodeLine{1843   \textcolor{keywordflow}{if} ((len\_trim(cs\%u\_trunc\_file) > 0) .or. (len\_trim(cs\%v\_trunc\_file) > 0)) \&}
\DoxyCodeLine{1844     \textcolor{keyword}{call }pointaccel\_init(mis, time, g, param\_file, diag, dirs, cs\%PointAccel\_CSp)}
\DoxyCodeLine{1845 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__vert__friction_ac14bc439f3b7ae3000fa9883c95ebfe2}\label{namespacemom__vert__friction_ac14bc439f3b7ae3000fa9883c95ebfe2}} 
\index{mom\_vert\_friction@{mom\_vert\_friction}!vertvisc\_limit\_vel@{vertvisc\_limit\_vel}}
\index{vertvisc\_limit\_vel@{vertvisc\_limit\_vel}!mom\_vert\_friction@{mom\_vert\_friction}}
\doxysubsubsection{\texorpdfstring{vertvisc\_limit\_vel()}{vertvisc\_limit\_vel()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+vert\+\_\+friction\+::vertvisc\+\_\+limit\+\_\+vel (\begin{DoxyParamCaption}\item[{real, dimension(szib\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)), intent(inout)}]{u,  }\item[{real, dimension(szi\+\_\+(g),szjb\+\_\+(g),szk\+\_\+(gv)), intent(inout)}]{v,  }\item[{real, dimension(szi\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)), intent(in)}]{h,  }\item[{type(accel\+\_\+diag\+\_\+ptrs), intent(in)}]{A\+Dp,  }\item[{type(cont\+\_\+diag\+\_\+ptrs), intent(in)}]{C\+Dp,  }\item[{type(mech\+\_\+forcing), intent(in)}]{forces,  }\item[{type(vertvisc\+\_\+type), intent(in)}]{visc,  }\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__vert__friction_1_1vertvisc__cs}{vertvisc\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Velocity components which exceed a threshold for physically reasonable values are truncated. Optionally, any column with excessive velocities may be sent to a diagnostic reporting subroutine. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & Ocean grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & Ocean vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
\mbox{\texttt{ in,out}}  & {\em u} & Zonal velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in,out}}  & {\em v} & Meridional velocity \mbox{[}L T-\/1 $\sim$$>$ m s-\/1\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em h} & Layer thickness \mbox{[}H $\sim$$>$ m or kg m-\/2\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em adp} & Acceleration diagnostic pointers \\
\hline
\mbox{\texttt{ in}}  & {\em cdp} & Continuity diagnostic pointers \\
\hline
\mbox{\texttt{ in}}  & {\em forces} & A structure with the driving mechanical forces \\
\hline
\mbox{\texttt{ in}}  & {\em visc} & Viscosities and bottom drag \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
 & {\em cs} & Vertical viscosity control structure \\
\hline
\end{DoxyParams}


Definition at line 1369 of file M\+O\+M\+\_\+vert\+\_\+friction.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{1369   \textcolor{keywordtype}{type}(ocean\_grid\_type),   \textcolor{keywordtype}{intent(in)}    :: G\textcolor{comment}{      !< Ocean grid structure}}
\DoxyCodeLine{1370   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)}    :: GV\textcolor{comment}{     !< Ocean vertical grid structure}}
\DoxyCodeLine{1371   \textcolor{keywordtype}{type}(unit\_scale\_type),   \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{     !< A dimensional unit scaling type}}
\DoxyCodeLine{1372 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1373                            \textcolor{keywordtype}{intent(inout)} :: u\textcolor{comment}{      !< Zonal velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{1374 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1375                            \textcolor{keywordtype}{intent(inout)} :: v\textcolor{comment}{      !< Meridional velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{1376 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJ\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{1377                            \textcolor{keywordtype}{intent(in)}    :: h\textcolor{comment}{      !< Layer thickness [H ~> m or kg m-\/2]}}
\DoxyCodeLine{1378   \textcolor{keywordtype}{type}(accel\_diag\_ptrs),   \textcolor{keywordtype}{intent(in)}    :: ADp\textcolor{comment}{    !< Acceleration diagnostic pointers}}
\DoxyCodeLine{1379   \textcolor{keywordtype}{type}(cont\_diag\_ptrs),    \textcolor{keywordtype}{intent(in)}    :: CDp\textcolor{comment}{    !< Continuity diagnostic pointers}}
\DoxyCodeLine{1380   \textcolor{keywordtype}{type}(mech\_forcing),      \textcolor{keywordtype}{intent(in)}    :: forces\textcolor{comment}{ !< A structure with the driving mechanical forces}}
\DoxyCodeLine{1381   \textcolor{keywordtype}{type}(vertvisc\_type),     \textcolor{keywordtype}{intent(in)}    :: visc\textcolor{comment}{   !< Viscosities and bottom drag}}
\DoxyCodeLine{1382 \textcolor{keywordtype}{  real},                    \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{     !< Time increment [T ~> s]}}
\DoxyCodeLine{1383   \textcolor{keywordtype}{type}(vertvisc\_CS),       \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{     !< Vertical viscosity control structure}}
\DoxyCodeLine{1384 }
\DoxyCodeLine{1385   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{1386 }
\DoxyCodeLine{1387 \textcolor{keywordtype}{  real} :: maxvel           \textcolor{comment}{! Velocities components greater than maxvel}}
\DoxyCodeLine{1388 \textcolor{keywordtype}{  real} :: truncvel         \textcolor{comment}{! are truncated to truncvel, both [L T-\/1 ~> m s-\/1].}}
\DoxyCodeLine{1389 \textcolor{keywordtype}{  real} :: CFL              \textcolor{comment}{! The local CFL number.}}
\DoxyCodeLine{1390 \textcolor{keywordtype}{  real} :: H\_report         \textcolor{comment}{! A thickness below which not to report truncations.}}
\DoxyCodeLine{1391 \textcolor{keywordtype}{  real} :: dt\_Rho0          \textcolor{comment}{! The timestep divided by the Boussinesq density [m2 T2 s-\/1 L-\/1 Z-\/1 R-\/1 ~> s m3 kg-\/1].}}
\DoxyCodeLine{1392 \textcolor{keywordtype}{  real} :: vel\_report(SZIB\_(G),SZJB\_(G))   \textcolor{comment}{! The velocity to report [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{1393 \textcolor{keywordtype}{  real} :: u\_old(SZIB\_(G),SZJ\_(G),SZK\_(G)) \textcolor{comment}{! The previous u-\/velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{1394 \textcolor{keywordtype}{  real} :: v\_old(SZI\_(G),SZJB\_(G),SZK\_(G)) \textcolor{comment}{! The previous v-\/velocity [L T-\/1 ~> m s-\/1]}}
\DoxyCodeLine{1395   \textcolor{keywordtype}{logical} :: trunc\_any, dowrite(SZIB\_(G),SZJB\_(G))}
\DoxyCodeLine{1396   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, js, je, Isq, Ieq, Jsq, Jeq, nz}
\DoxyCodeLine{1397   is = g\%isc ; ie = g\%iec ; js = g\%jsc ; je = g\%jec ; nz = g\%ke}
\DoxyCodeLine{1398   isq = g\%IscB ; ieq = g\%IecB ; jsq = g\%JscB ; jeq = g\%JecB}
\DoxyCodeLine{1399 }
\DoxyCodeLine{1400   maxvel = cs\%maxvel}
\DoxyCodeLine{1401   truncvel = 0.9*maxvel}
\DoxyCodeLine{1402   h\_report = 6.0 * gv\%Angstrom\_H}
\DoxyCodeLine{1403   dt\_rho0 = (us\%L\_T\_to\_m\_s*us\%Z\_to\_m) * dt / gv\%Rho0}
\DoxyCodeLine{1404 }
\DoxyCodeLine{1405   \textcolor{keywordflow}{if} (len\_trim(cs\%u\_trunc\_file) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1406     \textcolor{comment}{!\$OMP parallel do default(shared) private(trunc\_any,CFL)}}
\DoxyCodeLine{1407     \textcolor{keywordflow}{do} j=js,je}
\DoxyCodeLine{1408       trunc\_any = .false.}
\DoxyCodeLine{1409       \textcolor{keywordflow}{do} i=isq,ieq ; dowrite(i,j) = .false. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1410       \textcolor{keywordflow}{if} (cs\%CFL\_based\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1411         \textcolor{keywordflow}{do} i=isq,ieq ; vel\_report(i,j) = 3.0e8*us\%m\_s\_to\_L\_T ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! Speed of light default.}}
\DoxyCodeLine{1412         \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{1413           \textcolor{keywordflow}{if} (abs(u(i,j,k)) < cs\%vel\_underflow) u(i,j,k) = 0.0}
\DoxyCodeLine{1414           \textcolor{keywordflow}{if} (u(i,j,k) < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1415             cfl = (-\/u(i,j,k) * dt) * (g\%dy\_Cu(i,j) * g\%IareaT(i+1,j))}
\DoxyCodeLine{1416           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1417             cfl = (u(i,j,k) * dt) * (g\%dy\_Cu(i,j) * g\%IareaT(i,j))}
\DoxyCodeLine{1418 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1419           \textcolor{keywordflow}{if} (cfl > cs\%CFL\_trunc) trunc\_any = .true.}
\DoxyCodeLine{1420           \textcolor{keywordflow}{if} (cfl > cs\%CFL\_report) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1421             dowrite(i,j) = .true.}
\DoxyCodeLine{1422             vel\_report(i,j) = min(vel\_report(i,j), abs(u(i,j,k)))}
\DoxyCodeLine{1423 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1424 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1425       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1426         \textcolor{keywordflow}{do} i=isq,ieq; vel\_report(i,j) = maxvel;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1427         \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{1428           \textcolor{keywordflow}{if} (abs(u(i,j,k)) < cs\%vel\_underflow) \textcolor{keywordflow}{then} ; u(i,j,k) = 0.0}
\DoxyCodeLine{1429           \textcolor{keywordflow}{elseif} (abs(u(i,j,k)) > maxvel) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1430             dowrite(i,j) = .true. ; trunc\_any = .true.}
\DoxyCodeLine{1431 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1432 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1433 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1434 }
\DoxyCodeLine{1435       \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (dowrite(i,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1436         u\_old(i,j,:) = u(i,j,:)}
\DoxyCodeLine{1437 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1438 }
\DoxyCodeLine{1439       \textcolor{keywordflow}{if} (trunc\_any) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (cs\%CFL\_based\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1440         \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{1441           \textcolor{keywordflow}{if} ((u(i,j,k) * (dt * g\%dy\_Cu(i,j))) * g\%IareaT(i+1,j) < -\/cs\%CFL\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1442             u(i,j,k) = (-\/0.9*cs\%CFL\_trunc) * (g\%areaT(i+1,j) / (dt * g\%dy\_Cu(i,j)))}
\DoxyCodeLine{1443             \textcolor{keywordflow}{if} (h(i,j,k) + h(i+1,j,k) > h\_report) cs\%ntrunc = cs\%ntrunc + 1}
\DoxyCodeLine{1444           \textcolor{keywordflow}{elseif} ((u(i,j,k) * (dt * g\%dy\_Cu(i,j))) * g\%IareaT(i,j) > cs\%CFL\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1445             u(i,j,k) = (0.9*cs\%CFL\_trunc) * (g\%areaT(i,j) / (dt * g\%dy\_Cu(i,j)))}
\DoxyCodeLine{1446             \textcolor{keywordflow}{if} (h(i,j,k) + h(i+1,j,k) > h\_report) cs\%ntrunc = cs\%ntrunc + 1}
\DoxyCodeLine{1447 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1448 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1449       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1450         \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (abs(u(i,j,k)) > maxvel) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1451           u(i,j,k) = sign(truncvel,u(i,j,k))}
\DoxyCodeLine{1452           \textcolor{keywordflow}{if} (h(i,j,k) + h(i+1,j,k) > h\_report) cs\%ntrunc = cs\%ntrunc + 1}
\DoxyCodeLine{1453 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1454 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1455 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! j-\/loop}}
\DoxyCodeLine{1456   \textcolor{keywordflow}{else}  \textcolor{comment}{! Do not report accelerations leading to large velocities.}}
\DoxyCodeLine{1457     \textcolor{keywordflow}{if} (cs\%CFL\_based\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1458 \textcolor{comment}{!\$OMP parallel do default(none) shared(nz,js,je,Isq,Ieq,u,dt,G,CS,h,H\_report)}}
\DoxyCodeLine{1459       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{1460         \textcolor{keywordflow}{if} (abs(u(i,j,k)) < cs\%vel\_underflow) \textcolor{keywordflow}{then} ; u(i,j,k) = 0.0}
\DoxyCodeLine{1461         \textcolor{keywordflow}{elseif} ((u(i,j,k) * (dt * g\%dy\_Cu(i,j))) * g\%IareaT(i+1,j) < -\/cs\%CFL\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1462           u(i,j,k) = (-\/0.9*cs\%CFL\_trunc) * (g\%areaT(i+1,j) / (dt * g\%dy\_Cu(i,j)))}
\DoxyCodeLine{1463           \textcolor{keywordflow}{if} (h(i,j,k) + h(i+1,j,k) > h\_report) cs\%ntrunc = cs\%ntrunc + 1}
\DoxyCodeLine{1464         \textcolor{keywordflow}{elseif} ((u(i,j,k) * (dt * g\%dy\_Cu(i,j))) * g\%IareaT(i,j) > cs\%CFL\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1465           u(i,j,k) = (0.9*cs\%CFL\_trunc) * (g\%areaT(i,j) / (dt * g\%dy\_Cu(i,j)))}
\DoxyCodeLine{1466           \textcolor{keywordflow}{if} (h(i,j,k) + h(i+1,j,k) > h\_report) cs\%ntrunc = cs\%ntrunc + 1}
\DoxyCodeLine{1467 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1468 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1469     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1470 \textcolor{comment}{!\$OMP parallel do default(none) shared(nz,js,je,Isq,Ieq,u,G,CS,truncvel,maxvel,h,H\_report)}}
\DoxyCodeLine{1471       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=js,je ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{1472         \textcolor{keywordflow}{if} (abs(u(i,j,k)) < cs\%vel\_underflow) \textcolor{keywordflow}{then} ; u(i,j,k) = 0.0}
\DoxyCodeLine{1473         \textcolor{keywordflow}{elseif} (abs(u(i,j,k)) > maxvel) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1474           u(i,j,k) = sign(truncvel, u(i,j,k))}
\DoxyCodeLine{1475           \textcolor{keywordflow}{if} (h(i,j,k) + h(i+1,j,k) > h\_report) cs\%ntrunc = cs\%ntrunc + 1}
\DoxyCodeLine{1476 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1477 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1478 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1479 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1480 }
\DoxyCodeLine{1481   \textcolor{keywordflow}{if} (len\_trim(cs\%u\_trunc\_file) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1482     \textcolor{keywordflow}{do} j=js,je; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (dowrite(i,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1483 \textcolor{comment}{!   Here the diagnostic reporting subroutines are called if}}
\DoxyCodeLine{1484 \textcolor{comment}{! unphysically large values were found.}}
\DoxyCodeLine{1485       \textcolor{keyword}{call }write\_u\_accel(i, j, u\_old, h, adp, cdp, dt, g, gv, us, cs\%PointAccel\_CSp, \&}
\DoxyCodeLine{1486                vel\_report(i,j), forces\%taux(i,j)*dt\_rho0, a=cs\%a\_u, hv=cs\%h\_u)}
\DoxyCodeLine{1487 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1488 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1489 }
\DoxyCodeLine{1490   \textcolor{keywordflow}{if} (len\_trim(cs\%v\_trunc\_file) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1491     \textcolor{comment}{!\$OMP parallel do default(shared) private(trunc\_any,CFL)}}
\DoxyCodeLine{1492     \textcolor{keywordflow}{do} j=jsq,jeq}
\DoxyCodeLine{1493       trunc\_any = .false.}
\DoxyCodeLine{1494       \textcolor{keywordflow}{do} i=is,ie ; dowrite(i,j) = .false. ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1495       \textcolor{keywordflow}{if} (cs\%CFL\_based\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1496         \textcolor{keywordflow}{do} i=is,ie ; vel\_report(i,j) = 3.0e8*us\%m\_s\_to\_L\_T ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! Speed of light default.}}
\DoxyCodeLine{1497         \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1498           \textcolor{keywordflow}{if} (abs(v(i,j,k)) < cs\%vel\_underflow) v(i,j,k) = 0.0}
\DoxyCodeLine{1499           \textcolor{keywordflow}{if} (v(i,j,k) < 0.0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1500             cfl = (-\/v(i,j,k) * dt) * (g\%dx\_Cv(i,j) * g\%IareaT(i,j+1))}
\DoxyCodeLine{1501           \textcolor{keywordflow}{else}}
\DoxyCodeLine{1502             cfl = (v(i,j,k) * dt) * (g\%dx\_Cv(i,j) * g\%IareaT(i,j))}
\DoxyCodeLine{1503 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1504           \textcolor{keywordflow}{if} (cfl > cs\%CFL\_trunc) trunc\_any = .true.}
\DoxyCodeLine{1505           \textcolor{keywordflow}{if} (cfl > cs\%CFL\_report) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1506             dowrite(i,j) = .true.}
\DoxyCodeLine{1507             vel\_report(i,j) = min(vel\_report(i,j), abs(v(i,j,k)))}
\DoxyCodeLine{1508 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1509 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1510       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1511         \textcolor{keywordflow}{do} i=is,ie ; vel\_report(i,j) = maxvel ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1512         \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1513           \textcolor{keywordflow}{if} (abs(v(i,j,k)) < cs\%vel\_underflow) \textcolor{keywordflow}{then} ; v(i,j,k) = 0.0}
\DoxyCodeLine{1514           \textcolor{keywordflow}{elseif} (abs(v(i,j,k)) > maxvel) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1515             dowrite(i,j) = .true. ; trunc\_any = .true.}
\DoxyCodeLine{1516 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1517 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1518 \textcolor{keywordflow}{      endif}}
\DoxyCodeLine{1519 }
\DoxyCodeLine{1520       \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (dowrite(i,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1521         v\_old(i,j,:) = v(i,j,:)}
\DoxyCodeLine{1522 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1523 }
\DoxyCodeLine{1524       \textcolor{keywordflow}{if} (trunc\_any) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{if} (cs\%CFL\_based\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1525         \textcolor{keywordflow}{do} k=1,nz; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1526           \textcolor{keywordflow}{if} ((v(i,j,k) * (dt * g\%dx\_Cv(i,j))) * g\%IareaT(i,j+1) < -\/cs\%CFL\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1527             v(i,j,k) = (-\/0.9*cs\%CFL\_trunc) * (g\%areaT(i,j+1) / (dt * g\%dx\_Cv(i,j)))}
\DoxyCodeLine{1528             \textcolor{keywordflow}{if} (h(i,j,k) + h(i,j+1,k) > h\_report) cs\%ntrunc = cs\%ntrunc + 1}
\DoxyCodeLine{1529           \textcolor{keywordflow}{elseif} ((v(i,j,k) * (dt * g\%dx\_Cv(i,j))) * g\%IareaT(i,j) > cs\%CFL\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1530             v(i,j,k) = (0.9*cs\%CFL\_trunc) * (g\%areaT(i,j) / (dt * g\%dx\_Cv(i,j)))}
\DoxyCodeLine{1531             \textcolor{keywordflow}{if} (h(i,j,k) + h(i,j+1,k) > h\_report) cs\%ntrunc = cs\%ntrunc + 1}
\DoxyCodeLine{1532 \textcolor{keywordflow}{          endif}}
\DoxyCodeLine{1533 \textcolor{keywordflow}{        enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1534       \textcolor{keywordflow}{else}}
\DoxyCodeLine{1535         \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (abs(v(i,j,k)) > maxvel) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1536           v(i,j,k) = sign(truncvel,v(i,j,k))}
\DoxyCodeLine{1537           \textcolor{keywordflow}{if} (h(i,j,k) + h(i,j+1,k) > h\_report) cs\%ntrunc = cs\%ntrunc + 1}
\DoxyCodeLine{1538 \textcolor{keywordflow}{        endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1539 \textcolor{keywordflow}{      endif} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{1540 \textcolor{keywordflow}{    enddo} \textcolor{comment}{! J-\/loop}}
\DoxyCodeLine{1541   \textcolor{keywordflow}{else}  \textcolor{comment}{! Do not report accelerations leading to large velocities.}}
\DoxyCodeLine{1542     \textcolor{keywordflow}{if} (cs\%CFL\_based\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1543       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{1544       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=jsq,jeq ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1545         \textcolor{keywordflow}{if} (abs(v(i,j,k)) < cs\%vel\_underflow) \textcolor{keywordflow}{then} ; v(i,j,k) = 0.0}
\DoxyCodeLine{1546         \textcolor{keywordflow}{elseif} ((v(i,j,k) * (dt * g\%dx\_Cv(i,j))) * g\%IareaT(i,j+1) < -\/cs\%CFL\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1547           v(i,j,k) = (-\/0.9*cs\%CFL\_trunc) * (g\%areaT(i,j+1) / (dt * g\%dx\_Cv(i,j)))}
\DoxyCodeLine{1548           \textcolor{keywordflow}{if} (h(i,j,k) + h(i,j+1,k) > h\_report) cs\%ntrunc = cs\%ntrunc + 1}
\DoxyCodeLine{1549         \textcolor{keywordflow}{elseif} ((v(i,j,k) * (dt * g\%dx\_Cv(i,j))) * g\%IareaT(i,j) > cs\%CFL\_trunc) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1550           v(i,j,k) = (0.9*cs\%CFL\_trunc) * (g\%areaT(i,j) / (dt * g\%dx\_Cv(i,j)))}
\DoxyCodeLine{1551           \textcolor{keywordflow}{if} (h(i,j,k) + h(i,j+1,k) > h\_report) cs\%ntrunc = cs\%ntrunc + 1}
\DoxyCodeLine{1552 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1553 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1554     \textcolor{keywordflow}{else}}
\DoxyCodeLine{1555       \textcolor{comment}{!\$OMP parallel do default(shared)}}
\DoxyCodeLine{1556       \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} j=jsq,jeq ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{1557         \textcolor{keywordflow}{if} (abs(v(i,j,k)) < cs\%vel\_underflow) \textcolor{keywordflow}{then} ; v(i,j,k) = 0.0}
\DoxyCodeLine{1558         \textcolor{keywordflow}{elseif} (abs(v(i,j,k)) > maxvel) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1559           v(i,j,k) = sign(truncvel, v(i,j,k))}
\DoxyCodeLine{1560           \textcolor{keywordflow}{if} (h(i,j,k) + h(i,j+1,k) > h\_report) cs\%ntrunc = cs\%ntrunc + 1}
\DoxyCodeLine{1561 \textcolor{keywordflow}{        endif}}
\DoxyCodeLine{1562 \textcolor{keywordflow}{      enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1563 \textcolor{keywordflow}{    endif}}
\DoxyCodeLine{1564 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1565 }
\DoxyCodeLine{1566   \textcolor{keywordflow}{if} (len\_trim(cs\%v\_trunc\_file) > 0) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1567     \textcolor{keywordflow}{do} j=jsq,jeq; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (dowrite(i,j)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{1568 \textcolor{comment}{!   Here the diagnostic reporting subroutines are called if}}
\DoxyCodeLine{1569 \textcolor{comment}{! unphysically large values were found.}}
\DoxyCodeLine{1570       \textcolor{keyword}{call }write\_v\_accel(i, j, v\_old, h, adp, cdp, dt, g, gv, us, cs\%PointAccel\_CSp, \&}
\DoxyCodeLine{1571                vel\_report(i,j), forces\%tauy(i,j)*dt\_rho0, a=cs\%a\_v, hv=cs\%h\_v)}
\DoxyCodeLine{1572 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{1573 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{1574 }

\end{DoxyCode}
\mbox{\Hypertarget{namespacemom__vert__friction_ad0b7bdc6695ee0c4207797bc94672863}\label{namespacemom__vert__friction_ad0b7bdc6695ee0c4207797bc94672863}} 
\index{mom\_vert\_friction@{mom\_vert\_friction}!vertvisc\_remnant@{vertvisc\_remnant}}
\index{vertvisc\_remnant@{vertvisc\_remnant}!mom\_vert\_friction@{mom\_vert\_friction}}
\doxysubsubsection{\texorpdfstring{vertvisc\_remnant()}{vertvisc\_remnant()}}
{\footnotesize\ttfamily subroutine, public mom\+\_\+vert\+\_\+friction\+::vertvisc\+\_\+remnant (\begin{DoxyParamCaption}\item[{type(vertvisc\+\_\+type), intent(in)}]{visc,  }\item[{real, dimension(szib\+\_\+(g),szj\+\_\+(g),szk\+\_\+(gv)), intent(inout)}]{visc\+\_\+rem\+\_\+u,  }\item[{real, dimension( g \%isd\+: g \%ied, g \%jsdb\+: g \%jedb, gv \%ke), intent(inout)}]{visc\+\_\+rem\+\_\+v,  }\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__vert__friction_1_1vertvisc__cs}{vertvisc\+\_\+cs}}), pointer}]{CS }\end{DoxyParamCaption})}



Calculate the fraction of momentum originally in a layer that remains after a time-\/step of viscosity, and the fraction of a time-\/step\textquotesingle{}s worth of barotropic acceleration that a layer experiences after viscosity is applied. 


\begin{DoxyParams}[1]{Parameters}
\mbox{\texttt{ in}}  & {\em g} & Ocean grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em gv} & Ocean vertical grid structure \\
\hline
\mbox{\texttt{ in}}  & {\em visc} & Viscosities and bottom drag \\
\hline
\mbox{\texttt{ in,out}}  & {\em visc\+\_\+rem\+\_\+u} & Fraction of a time-\/step\textquotesingle{}s worth of a \\
\hline
\mbox{\texttt{ in,out}}  & {\em visc\+\_\+rem\+\_\+v} & Fraction of a time-\/step\textquotesingle{}s worth of a \\
\hline
\mbox{\texttt{ in}}  & {\em dt} & Time increment \mbox{[}T $\sim$$>$ s\mbox{]} \\
\hline
\mbox{\texttt{ in}}  & {\em us} & A dimensional unit scaling type \\
\hline
 & {\em cs} & Vertical viscosity control structure \\
\hline
\end{DoxyParams}


Definition at line 509 of file M\+O\+M\+\_\+vert\+\_\+friction.\+F90.


\begin{DoxyCode}{0}
\DoxyCodeLine{509   \textcolor{keywordtype}{type}(ocean\_grid\_type), \textcolor{keywordtype}{intent(in)}   :: G\textcolor{comment}{    !< Ocean grid structure}}
\DoxyCodeLine{510   \textcolor{keywordtype}{type}(verticalGrid\_type), \textcolor{keywordtype}{intent(in)} :: GV\textcolor{comment}{   !< Ocean vertical grid structure}}
\DoxyCodeLine{511   \textcolor{keywordtype}{type}(vertvisc\_type),   \textcolor{keywordtype}{intent(in)}   :: visc\textcolor{comment}{ !< Viscosities and bottom drag}}
\DoxyCodeLine{512 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZIB\_(G),SZJ\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{513                          \textcolor{keywordtype}{intent(inout)} :: visc\_rem\_u\textcolor{comment}{ !< Fraction of a time-\/step's worth of a}}
\DoxyCodeLine{514 \textcolor{comment}{                                              !! barotopic acceleration that a layer experiences after}}
\DoxyCodeLine{515 \textcolor{comment}{                                              !! viscosity is applied in the zonal direction [nondim]}}
\DoxyCodeLine{516 \textcolor{keywordtype}{  real}, \textcolor{keywordtype}{dimension(SZI\_(G),SZJB\_(G),SZK\_(GV))}, \&}
\DoxyCodeLine{517                          \textcolor{keywordtype}{intent(inout)} :: visc\_rem\_v\textcolor{comment}{ !< Fraction of a time-\/step's worth of a}}
\DoxyCodeLine{518 \textcolor{comment}{                                              !! barotopic acceleration that a layer experiences after}}
\DoxyCodeLine{519 \textcolor{comment}{                                              !! viscosity is applied in the meridional direction [nondim]}}
\DoxyCodeLine{520 \textcolor{keywordtype}{  real},                  \textcolor{keywordtype}{intent(in)}    :: dt\textcolor{comment}{  !< Time increment [T ~> s]}}
\DoxyCodeLine{521   \textcolor{keywordtype}{type}(unit\_scale\_type), \textcolor{keywordtype}{intent(in)}    :: US\textcolor{comment}{  !< A dimensional unit scaling type}}
\DoxyCodeLine{522   \textcolor{keywordtype}{type}(vertvisc\_CS),     \textcolor{keywordtype}{pointer}       :: CS\textcolor{comment}{  !< Vertical viscosity control structure}}
\DoxyCodeLine{523 }
\DoxyCodeLine{524   \textcolor{comment}{! Local variables}}
\DoxyCodeLine{525 }
\DoxyCodeLine{526 \textcolor{keywordtype}{  real} :: b1(SZIB\_(G))          \textcolor{comment}{! A variable used by the tridiagonal solver [H-\/1 ~> m-\/1 or m2 kg-\/1].}}
\DoxyCodeLine{527 \textcolor{keywordtype}{  real} :: c1(SZIB\_(G),SZK\_(G))  \textcolor{comment}{! A variable used by the tridiagonal solver [nondim].}}
\DoxyCodeLine{528 \textcolor{keywordtype}{  real} :: d1(SZIB\_(G))          \textcolor{comment}{! d1=1-\/c1 is used by the tridiagonal solver [nondim].}}
\DoxyCodeLine{529 \textcolor{keywordtype}{  real} :: Ray(SZIB\_(G),SZK\_(G)) \textcolor{comment}{! Ray is the Rayleigh-\/drag velocity [Z T-\/1 ~> m s-\/1].}}
\DoxyCodeLine{530 \textcolor{keywordtype}{  real} :: b\_denom\_1   \textcolor{comment}{! The first term in the denominator of b1 [H ~> m or kg m-\/2].}}
\DoxyCodeLine{531 \textcolor{keywordtype}{  real} :: dt\_Z\_to\_H        \textcolor{comment}{! The time step times the conversion from Z to the}}
\DoxyCodeLine{532                            \textcolor{comment}{! units of thickness [T H Z-\/1 ~> s or s kg m-\/3].}}
\DoxyCodeLine{533   \textcolor{keywordtype}{logical} :: do\_i(SZIB\_(G))}
\DoxyCodeLine{534 }
\DoxyCodeLine{535   \textcolor{keywordtype}{integer} :: i, j, k, is, ie, Isq, Ieq, Jsq, Jeq, nz}
\DoxyCodeLine{536   is = g\%isc ; ie = g\%iec}
\DoxyCodeLine{537   isq = g\%IscB ; ieq = g\%IecB ; jsq = g\%JscB ; jeq = g\%JecB ; nz = g\%ke}
\DoxyCodeLine{538 }
\DoxyCodeLine{539   \textcolor{keywordflow}{if} (.not.\textcolor{keyword}{associated}(cs)) \textcolor{keyword}{call }mom\_error(fatal,\textcolor{stringliteral}{"MOM\_vert\_friction(visc): "}// \&}
\DoxyCodeLine{540          \textcolor{stringliteral}{"Module must be initialized before it is used."})}
\DoxyCodeLine{541 }
\DoxyCodeLine{542   dt\_z\_to\_h = dt*gv\%Z\_to\_H}
\DoxyCodeLine{543 }
\DoxyCodeLine{544   \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq ; ray(i,k) = 0.0 ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{545 }
\DoxyCodeLine{546   \textcolor{comment}{! Find the zonal viscous using a modification of a standard tridagonal solver.}}
\DoxyCodeLine{547 \textcolor{comment}{!\$OMP parallel do default(none) shared(G,Isq,Ieq,CS,nz,visc,dt\_Z\_to\_H,visc\_rem\_u) \&}}
\DoxyCodeLine{548 \textcolor{comment}{!\$OMP                     firstprivate(Ray)                                       \&}}
\DoxyCodeLine{549 \textcolor{comment}{!\$OMP                          private(do\_i,b\_denom\_1,b1,d1,c1)}}
\DoxyCodeLine{550   \textcolor{keywordflow}{do} j=g\%jsc,g\%jec}
\DoxyCodeLine{551     \textcolor{keywordflow}{do} i=isq,ieq ; do\_i(i) = (g\%mask2dCu(i,j) > 0) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{552 }
\DoxyCodeLine{553     \textcolor{keywordflow}{if} (cs\%Channel\_drag) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=isq,ieq}
\DoxyCodeLine{554       ray(i,k) = visc\%Ray\_u(i,j,k)}
\DoxyCodeLine{555 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{556 }
\DoxyCodeLine{557     \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{558       b\_denom\_1 = cs\%h\_u(i,j,1) + dt\_z\_to\_h * (ray(i,1) + cs\%a\_u(i,j,1))}
\DoxyCodeLine{559       b1(i) = 1.0 / (b\_denom\_1 + dt\_z\_to\_h*cs\%a\_u(i,j,2))}
\DoxyCodeLine{560       d1(i) = b\_denom\_1 * b1(i)}
\DoxyCodeLine{561       visc\_rem\_u(i,j,1) = b1(i) * cs\%h\_u(i,j,1)}
\DoxyCodeLine{562 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{563     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{564       c1(i,k) = dt\_z\_to\_h * cs\%a\_u(i,j,k)*b1(i)}
\DoxyCodeLine{565       b\_denom\_1 = cs\%h\_u(i,j,k) + dt\_z\_to\_h * (ray(i,k) + cs\%a\_u(i,j,k)*d1(i))}
\DoxyCodeLine{566       b1(i) = 1.0 / (b\_denom\_1 + dt\_z\_to\_h * cs\%a\_u(i,j,k+1))}
\DoxyCodeLine{567       d1(i) = b\_denom\_1 * b1(i)}
\DoxyCodeLine{568       visc\_rem\_u(i,j,k) = (cs\%h\_u(i,j,k) + dt\_z\_to\_h * cs\%a\_u(i,j,k) * visc\_rem\_u(i,j,k-\/1)) * b1(i)}
\DoxyCodeLine{569 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{570     \textcolor{keywordflow}{do} k=nz-\/1,1,-\/1 ; \textcolor{keywordflow}{do} i=isq,ieq ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{571       visc\_rem\_u(i,j,k) = visc\_rem\_u(i,j,k) + c1(i,k+1)*visc\_rem\_u(i,j,k+1)}
\DoxyCodeLine{572 }
\DoxyCodeLine{573 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i and k loops}}
\DoxyCodeLine{574 }
\DoxyCodeLine{575 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! end u-\/component j loop}}
\DoxyCodeLine{576 }
\DoxyCodeLine{577   \textcolor{comment}{! Now find the meridional viscous using a modification.}}
\DoxyCodeLine{578 \textcolor{comment}{!\$OMP parallel do default(none) shared(Jsq,Jeq,is,ie,G,CS,visc,dt\_Z\_to\_H,visc\_rem\_v,nz) \&}}
\DoxyCodeLine{579 \textcolor{comment}{!\$OMP                     firstprivate(Ray)                                             \&}}
\DoxyCodeLine{580 \textcolor{comment}{!\$OMP                          private(do\_i,b\_denom\_1,b1,d1,c1)}}
\DoxyCodeLine{581   \textcolor{keywordflow}{do} j=jsq,jeq}
\DoxyCodeLine{582     \textcolor{keywordflow}{do} i=is,ie ; do\_i(i) = (g\%mask2dCv(i,j) > 0) ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{583 }
\DoxyCodeLine{584     \textcolor{keywordflow}{if} (cs\%Channel\_drag) \textcolor{keywordflow}{then} ; \textcolor{keywordflow}{do} k=1,nz ; \textcolor{keywordflow}{do} i=is,ie}
\DoxyCodeLine{585       ray(i,k) = visc\%Ray\_v(i,j,k)}
\DoxyCodeLine{586 \textcolor{keywordflow}{    enddo} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ endif}}
\DoxyCodeLine{587 }
\DoxyCodeLine{588     \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{589       b\_denom\_1 = cs\%h\_v(i,j,1) + dt\_z\_to\_h * (ray(i,1) + cs\%a\_v(i,j,1))}
\DoxyCodeLine{590       b1(i) = 1.0 / (b\_denom\_1 + dt\_z\_to\_h*cs\%a\_v(i,j,2))}
\DoxyCodeLine{591       d1(i) = b\_denom\_1 * b1(i)}
\DoxyCodeLine{592       visc\_rem\_v(i,j,1) = b1(i) * cs\%h\_v(i,j,1)}
\DoxyCodeLine{593 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{594     \textcolor{keywordflow}{do} k=2,nz ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{595       c1(i,k) = dt\_z\_to\_h * cs\%a\_v(i,j,k)*b1(i)}
\DoxyCodeLine{596       b\_denom\_1 = cs\%h\_v(i,j,k) + dt\_z\_to\_h * (ray(i,k) + cs\%a\_v(i,j,k)*d1(i))}
\DoxyCodeLine{597       b1(i) = 1.0 / (b\_denom\_1 + dt\_z\_to\_h * cs\%a\_v(i,j,k+1))}
\DoxyCodeLine{598       d1(i) = b\_denom\_1 * b1(i)}
\DoxyCodeLine{599       visc\_rem\_v(i,j,k) = (cs\%h\_v(i,j,k) + dt\_z\_to\_h * cs\%a\_v(i,j,k) * visc\_rem\_v(i,j,k-\/1)) * b1(i)}
\DoxyCodeLine{600 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo}}
\DoxyCodeLine{601     \textcolor{keywordflow}{do} k=nz-\/1,1,-\/1 ; \textcolor{keywordflow}{do} i=is,ie ; \textcolor{keywordflow}{if} (do\_i(i)) \textcolor{keywordflow}{then}}
\DoxyCodeLine{602       visc\_rem\_v(i,j,k) = visc\_rem\_v(i,j,k) + c1(i,k+1)*visc\_rem\_v(i,j,k+1)}
\DoxyCodeLine{603 \textcolor{keywordflow}{    endif} ;\textcolor{keywordflow}{ enddo} ;\textcolor{keywordflow}{ enddo} \textcolor{comment}{! i and k loops}}
\DoxyCodeLine{604 \textcolor{keywordflow}{  enddo} \textcolor{comment}{! end of v-\/component J loop}}
\DoxyCodeLine{605 }
\DoxyCodeLine{606   \textcolor{keywordflow}{if} (cs\%debug) \textcolor{keywordflow}{then}}
\DoxyCodeLine{607     \textcolor{keyword}{call }uvchksum(\textcolor{stringliteral}{"visc\_rem\_[uv]"}, visc\_rem\_u, visc\_rem\_v, g\%HI, haloshift=0, \&}
\DoxyCodeLine{608                   scalar\_pair=.true.)}
\DoxyCodeLine{609 \textcolor{keywordflow}{  endif}}
\DoxyCodeLine{610 }

\end{DoxyCode}
